C

Asked • 06/05/19

Why does GCC use multiplication by a strange number in implementing integer division?

I've been reading about `div` and `mul` assembly operations, and I decided to see them in action by writing a simple program in C:###File division.c #include <stdlib.h> #include <stdio.h> int main() { size_t i = 9; size_t j = i / 5; printf("%zu\\n",j); return 0; }And then generating assembly language code with: gcc -S division.c -O0 -masm=intelBut looking at generated `division.s` file, it doesn't contain any div operations! Instead, it does some kind of black magic with bit shifting and magic numbers. Here's a code snippet that computes `i/5`: mov rax, QWORD PTR [rbp-16] ; Move i (=9) to RAX movabs rdx, -3689348814741910323 ; Move some magic number to RDX (?) mul rdx ; Multiply 9 by magic number mov rax, rdx ; Take only the upper 64 bits of the result shr rax, 2 ; Shift these bits 2 places to the right (?) mov QWORD PTR [rbp-8], rax ; Magically, RAX contains 9/5=1 now, ; so we can assign it to jWhat's going on here? Why doesn't GCC use div at all? How does it generate this magic number and why does everything work?

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.