我对 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/