
104
Check Argument Range of Trigonometric Instructions
AMD Athlon™ Processor x86 Code Optimization
22007E/0—November 1999
If an “argument out of range” is detected, a range reduction
subroutine is invoked which reduces the argument to less than
2^63 before the instruction is attempted again. While an
argument > 2^63 is unusual, it often indicates a problem
elsewhere in the code and the code may completely fail in the
absence of a properly guarded trigonometric instruction. For
example, in the case of FSIN or FCOS generated from a sin() or
cos() function invocation in the HLL, the downstream code
might reasonably expect that the returned result is in the range
[-1,1].
A naive solution for guarding a trigonometric instruction may
check the C2 bit in the FPU status word after each FSIN, FCOS,
FPTAN, and FSINCOS instruction, and take appropriate action
if it is set (indicating an argument out of range).
Example 1 (Avoid):
FLD
QWORD PTR [x] ;argument
FSIN
;compute sine
FSTSW AX
;store FPU status word to AX
TEST
AX, 0400h
;is the C2 bit set?
JZ
$in_range
;nope, argument was in range, all OK
CALL
$reduce_range ;reduce argument in ST(0) to < 2^63
FSIN
;compute sine (in-range argument
; guaranteed)
$in_range:
Such a solution is inefficient since the FSTSW instruction is
serializing with respect to all x87/3DNow!/MMX instructions
and should thus be avoided (see the section “Floating-Point
Compare Instructions” on page 98). Use of FSTSW in the above
fashion slows down the common path through the code.
Instead, it is advisable to check the argument before one of the
trigonometric instructions is invoked.
Example 2 (Preferred):
FLD
QWORD PTR [x]
;argument
FLD
DWORD PTR [two_to_the_63]
;2^63
FCOMIP ST,ST(1)
;argument <= 2^63 ?
JBE
$in_range
;Yes, It is in range.
CALL
$reduce_range
;reduce argument in ST(0) to < 2^63
$in_range:
FSIN
;compute sine (in-range argument
; guaranteed)
Содержание Athlon Processor x86
Страница 1: ...AMD Athlon Processor x86 Code Optimization Guide TM...
Страница 12: ...xii List of Figures AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Страница 16: ...xvi Revision History AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Страница 60: ...44 Code Padding Using Neutral Code Fillers AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Страница 92: ...76 Push Memory Data Carefully AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Страница 122: ...106 Take Advantage of the FSINCOS Instruction AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Страница 156: ...140 AMD Athlon Processor Microarchitecture AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Страница 176: ...160 Write Combining Operations AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Страница 202: ...186 Page Attribute Table PAT AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Страница 252: ...236 VectorPath Instructions AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Страница 256: ...240 Index AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...