Skip to contentSkip to conversation

sched: CPU_THRESHOLD

PATCH Ricardo Nabinger Sanchez 2 participants 2 messages Aug 23, 2003 – Aug 25, 2003

lore
Share & tools
lore
#0 lore raw

hello Frank,

Review

Quoted 2 lines
(quoted diff) +1 -1
-	*imbalance = (max_load - nr_running) / 2;
+	*imbalance = (max_load - nr_running) >> 1;

I think it is a good coding practice to keep things human-readable. In this code snippet, the division by 2 is quickly understood by most readers (specially those who didn't write it). The right shift may obfuscate the real meaning of this operation, which is a single division by 2, not a bit-oriented expression.

Assuming that sched.c will be compiled with optimizations enabled, the compiler will change the human-readable division by a fast machine right shift operation, whenever possible (gcc surely will).

Thus, we keep the kernel code more readable, and sometimes let the compiler apply newer (and hopefully faster) optimizations than some tricks we have known as fastest available.

Regards, and please let me know what do you think about it.

Signature · 3 lines
Ricardo Nabinger Sanchez
GNU/Linux #140696 [http://counter.li.org]
Slackware Linux
#1 lore raw
Quoted 16 lines · Ricardo Nabinger Sanchez

hello Frank,

Review

Quoted 2 lines
(quoted diff) +1 -1
-	*imbalance = (max_load - nr_running) / 2;
+	*imbalance = (max_load - nr_running) >> 1;

I think it is a good coding practice to keep things human-readable. In this code snippet, the division by 2 is quickly understood by most readers (specially those who didn't write it). The right shift may
obfuscate the real meaning of this operation, which is a single
division by 2, not a bit-oriented expression.

Assuming that sched.c will be compiled with optimizations enabled, the compiler will change the human-readable division by a fast machine right shift operation, whenever possible (gcc surely will).

Quoted 6 lines

Thus, we keep the kernel code more readable, and sometimes let the compiler apply newer (and hopefully faster) optimizations than some tricks we have known as fastest available.

Regards, and please let me know what do you think about it.

Note that right-shift rounds towards negative infinity, while divide rounds towards zero.

If the operands are unsigned, then rsh and divide will yield the same instructions (on x86). OTOH, if they are signed, then you get different results. For this rsh, obviously, you get a shift, but for divide, you get something like this:

; divide edx by 2
mov ecx,edx
lsr ecx,31
asr edx,1
add edx,ecx