Chapter 8 Integer Optimizations 187
Software Optimization Guide for AMD64 Processors
25112 Rev. 3.06 September 2005
fprintf(stderr, "enter divisor: ");
scanf("%lu", &d);
printf("\n");
if (d == 0) goto printed_code;
if (d >= 0x80000000UL) {
printf("; dividend: register or memory location\n");
printf("\n");
printf("CMP dividend, 0%08lXh\n", d);
printf("MOV EDX, 0\n");
printf("SBB EDX, -1\n");
printf("\n");
printf("; quotient now in EDX\n");
goto printed_code;
}
/* Reduce divisor until it becomes odd. */
n = 0;
t = d;
while (!(t & 1)) {
t >>= 1;
n++;
}
if (t == 1) {
if (n == 0) {
printf("; dividend: register or memory location\n");
printf("\n");
printf("MOV EDX, dividend\n", n);
printf("\n");
printf("; quotient now in EDX\n");
}
else {
printf("; dividend: register or memory location\n");
printf("\n");
printf("SHR dividend, %d\n", n);
printf("\n");
printf("; quotient replaced dividend\n");
}
goto printed_code;
}
/* Generate m, s for algorithm 0. Based on: Granlund, T.; Montgomery,
P.L.: "Division by Invariant Integers using Multiplication."
SIGPLAN Notices, Vol. 29, June 1994, page 61.
*/
l = log2(t) + 1;