c - 为什么在这个 Linux 内核代码中截断参数?

标签 c linux kernel

我对 Linux 内核中 kernel/sched/sched.h 中的代码感到困惑。 就像 kernel.org 中的最新版本代码一样。 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/sched/sched.h?h=v5.3-rc4 第 285-299 行。

__dl_update(dl_b, (s32)tsk_bw / cpus);

函数“__dl_update”需要在第二个参数中输入 s64 类型。 tsk_bw 是 u64 类型。 为什么使用“s32”而不是“s64”???

static inline void __dl_update(struct dl_bw *dl_b, s64 bw);

static inline
void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
{
    dl_b->total_bw -= tsk_bw;
    __dl_update(dl_b, (s32)tsk_bw / cpus);
}

static inline
void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
{
    dl_b->total_bw += tsk_bw;
    __dl_update(dl_b, -((s32)tsk_bw / cpus));
}

static inline
bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
{
    return dl_b->bw != -1 &&
           dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
}

最佳答案

一个原因可能是 32 位有符号除法需要 26 个周期,而 64 位除法在 Skylake 上需要 42-95 个周期。 64 位有符号除法是最昂贵的除法,因此尽可能避免它是有意义的。参见 Agner Fog's Instruction tables .

关于c - 为什么在这个 Linux 内核代码中截断参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57505179/

相关文章:

c - 尝试 makefile 时出错?

c - 不兼容指针类型警告问题的分配

python - 使用Ctypes在Python程序中调用call by reference c程序

linux - Cygwin 串行端口权限被拒绝错误

javascript - Node shell 脚本上没有这样的文件或目录

linux - 通过python进行glob函数后的文件编辑

linux - 如何在 menuconfig 中反向查找 linux 内核配置选项

linux - "build"和 "source"链接在 "/lib/modules/<kernel-version>"中做什么

c - 新数据正在替换旧数据,而不是在 C 中添加新数据

linux - 如何通过 RDMA (Infiniband) 从 KVM 代码中向另一个 KVM 实例发送消息?