GCC为什么用奇数乘法来实现整数除法?
divmul
#include <stdlib.h>#include <stdio.h>int main(){
size_t i = 9;
size_t j = i / 5;
printf("%zu\n",j);
return 0;}gcc -S division.c -O0 -masm=intel
division.si/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 j
catspeake
相关分类