Signed overflow optimization hazards in the kernel
Signed overflow optimization hazards in the kernel
Posted Aug 16, 2012 22:46 UTC (Thu) by PaulMcKenney (✭ supporter ✭, #9624)In reply to: Signed overflow optimization hazards in the kernel by baldrick
Parent article: Signed overflow optimization hazards in the kernel
Hmmm... If I compile the following with gcc 4.6.1 with -O2:
unsigned long long signed_cast(unsigned long long a, unsigned long long b)
{
return (signed)((unsigned)a - (unsigned)b);
}
The following x86 object code is generated:
120: 8b 54 24 04 mov 0x4(%esp),%edx 124: 2b 54 24 0c sub 0xc(%esp),%edx 128: 89 d0 mov %edx,%eax 12a: c1 fa 1f sar $0x1f,%edx 12d: c3 ret
This is a 32-bit subtraction on 64-bit quantities. And when I plugged 2^32=4294967296 for a and 2^31-1=2147483647 for b, a-b evaluated to 2147483649 (as expected), but
signed_cast(a, b) evaluated to -2147483647.
So unless I misunderstood what you are suggesting, I will be sticking with the baroque comparisons for RCU.
