188 Integer Optimizations Chapter 8
25112 Rev. 3.06 September 2005
Software Optimization Guide for AMD64 Processors
j = (((U64)(0xffffffff)) % ((U64)(t)));
k = (((U64)(1)) << (32 + l)) / ((U64)(0xffffffff - j));
m_low = (((U64)(1)) << (32 + l)) / t;
m_high = ((((U64)(1)) << (32 + l)) + k) / t;
while (((m_low >> 1) < (m_high >> 1)) && (l > 0)) {
m_low = m_low >> 1;
m_high = m_high >> 1;
l = l - 1;
}
if ((m_high >> 32) == 0) {
m = ((U32)(m_high));
s = l;
a = 0;
}
/* Generate m and s for algorithm 1. Based on: Magenheimer, D.J.; et al:
"Integer Multiplication and Division on the HP Precision Architecture."
IEEE Transactions on Computers, Vol. 37, No. 8, August 1988, page 980.
*/
else {
s = log2(t);
m_low = (((U64)(1)) << (32 + s)) / ((U64)(t));
r = ((U32)((((U64)(1)) << (32 + s)) % ((U64)(t))));
m = (r < ((t >> 1) + 1)) ? ((U32)(m_low)) : ((U32)(m_low)) + 1;
a = 1;
}
/* Reduce multiplier for either algorithm to smallest possible. */
while (!(m & 1)) {
m = m >> 1;
s--;
}
/* Adjust multiplier for reduction of even divisors. */
s += n;
if (a) {
printf("; dividend: register other than EAX or memory location\n");
printf("\n");
printf("MOV EAX, 0%08lXh\n", m);
printf("MUL dividend\n");
printf("ADD EAX, 0%08lXh\n", m);
printf("ADC EDX, 0\n");
if (s) printf("SHR EDX, %d\n", s);
printf("\n");
printf("; quotient now in EDX\n");
}
else {