Use Alternative Code When Multiplying By A Constant - AMD Athlon Processor x86 Optimization Manual

X86 code optimization
Table of Contents

Advertisement

22007E/0—November 1999
Remainder of Signed
n
n
Integer 2
or –(2
)

Use Alternative Code When Multiplying by a Constant

Use Alternative Code When Multiplying by a Constant
;IN:EAX = dividend
;OUT:EAX = remainder
CDQ
AND
EDX, (2^n–1)
ADD
EAX, EDX
AND
EAX, (2^n–1)
SUB
EAX, EDX
MOV
[remainder], EAX
A 32-bit integer multiply by a constant has a latency of five
cycles. Therefore, use alternative code when multiplying by
certain constants. In addition, because there is just one
multiply unit, the replacement code may provide better
throughput.
The following code samples are designed such that the original
source also receives the final result. Other sequences are
possible if the result is in a different register. Adds have been
favored over shifts to keep code size small. Generally, there is a
fast replacement if the constant has very few 1 bits in binary.
More constants are found in the file multiply_by_constants.txt
located in the same directory where this document is located in
the SDK.
by 2:
ADD
REG1, REG1
by 3:
LEA
REG1, [REG1*2+REG1]
by 4:
SHL
REG1, 2
by 5:
LEA
REG1, [REG1*4+REG1]
by 6:
LEA
REG2, [REG1*4+REG1]
ADD
REG1, REG2
by 7:
MOV
REG2, REG1
SHL
REG1, 3
SUB
REG1, REG2
by 8:
SHL
REG1, 3
by 9:
LEA
REG1, [REG1*8+REG1]
by 10:
LEA
REG2, [REG1*8+REG1]
ADD
REG1, REG2
AMD Athlon™ Processor x86 Code Optimization
;Sign extend into EDX
;Mask correction (abs(divison)–1)
;Apply pre-correction
;Mask out remainder (abs(divison)–1)
;Apply pre-correction, if necessary
;1 cycle
;2 cycles
;1 cycle
;2 cycles
;3 cycles
;2 cycles
;1 cycle
;2 cycles
;3 cycles
81

Advertisement

Table of Contents
loading

Table of Contents