AMD 250 Computer Hardware User Manual


 
Chapter 2 C and C++ Source-Level Optimizations 55
Software Optimization Guide for AMD64 Processors
25112 Rev. 3.06 September 2005
to positive constants. In that case, the user must first determine the integer representation of that
floating-point constant. This can be accomplished with the following C code snippet:
float x;
scanf("%g", &x);
printf("%08X\n", (*((int *)(&x))));
The replacement code is IEEE-754 compliant for all classes of floating-point operands except NaNs.
However, NaNs do not occur in properly working software.
Examples
Intial definitions:
#define FLOAT2INTCAST(f) (*((int *)(&f)))
#define FLOAT2UINTCAST(f) (*((unsigned int *)(&f)))
Table 3: Comparisons against Zero
Use this … Instead of this.
if (FLOAT2UINTCAST(f) > 0x80000000U) if (f < 0.0f)
if (FLOAT2INCAST(f) <= 0) if (f <= 0.0f)
if (FLOAT2INTCAST(f) > 0) if (f > 0.0f)
if (FLOAT2UINTCAST(f) <= 0x80000000U) if (f >= 0.0f)
Table 4: Comparisons against Positive Constant
Use this … Instead of this.
if (FLOAT2INTCAST(f) < 0x40400000) if (f < 3.0f)
if (FLOAT2INTCAST(f) <= 0x40400000) if (f <= 3.0f)
if (FLOAT2INTCAST(f) > 0x40400000) if (f > 3.0f)
if (FLOAT2INTCAST(f) >= 0x40400000) if (f >= 3.0f)
Table 5: Comparisons among Two Floats
Use this … Instead of this.
float t = f1 - f2;
if (FLOAT2UINTCAST(t) > 0x80000000U)
if (f1 < f2)