Labor of Division (Episode I)

1 · · Feb. 15, 2010, 8 a.m.
Here's how you divide an unsigned int by 13 in C: unsigned divide(unsigned x) { return x / 13; } It gets better, I promise. Here's the corresponding x64 assembly that gcc produces: _divide: movl $1321528399, %edx movl %edi, %eax mull %edx shrl $2, %edx movl %edx, %eax ret Instead of division, there's multiplication by a bizarre number: 1321528399. What wickedness is this? 1321528399 is an example of a "magic number:" a number ...