By Constants; Shift Factor - AMD Athlon Processor x86 Optimization Manual

X86 code optimization
Table of Contents

Advertisement

22007E/0—November 1999
Derivation of Multiplier Used for Integer Division by
Constants
Unsigned Derivation for Algorithm, Multiplier, and Shift Factor
Derivation of Multiplier Used for Integer Division by Constants
ADD
EAX, EDX
MOV
EDX, EDX
SHR
EAX, 4
ADD
EAX, EDX
AND
EAX, 00F0F0F0Fh ;y = (x + (x >> 4) & 0x0F0F0F0F)
IMUL
EAX, 001010101h ;y * 0x01010101
SHR
EAX, 24
MOV
retVal, EAX
}
return (retVal);
}
The utility udiv.exe was compiled using the code shown in this
section.
The following code derives the multiplier value used when
performing integer division by constants. The code works for
unsigned integer division and for odd divisors between 1 and
31
2
–1, inclusive. For divisors of the form d = d'*2
is the same as for d' and the shift factor is s + n.
/* Code snippet to determine algorithm (a), multiplier (m),
and shift factor (s) to perform division on unsigned 32-bit
integers
by
constant
Microsoft Visual C compiler. */
/*
In:
d = divisor, 1 <= d < 2^31, d odd
Out:
a = algorithm
m = multiplier
s = shift factor
;algorithm 0
MOV
EDX, dividend
MOV
EAX, m
MUL
EDX
SHR
EDX, s
;EDX=quotient
AMD Athlon™ Processor x86 Code Optimization
;x = (w & 0x33333333) + ((w >> 2) &
; 0x33333333)
;x
;x >> 4
;x + (x >> 4)
;population count = (y *
; 0x01010101) >> 24
;store result
divisor.
Code
is
n
, the multiplier
written
for
the
93

Advertisement

Table of Contents
loading

Table of Contents