|
Absolute value of a float |
|
Code | |
Explanation |
IEEE 754 floats' highest bit is the so-called sign bit: set to 1 for negative and 0 for positive numbers (incl. zero). We just always set it to 0 − and we are done ! The data type "float" requires 32 bits. Unfortunately, C does not allow any bit operations on floats. To work around this issue, these 32 bits are re-interpreted as a 32 bit integer (line 4). Then clearing the sign bit becomes simple and easy (line 6), however, the required code looks a bit nasty. The built-in C function fabs() is translated into its FABS intrinsic when the data value is already on the FPU stack. When data is stored in main memory and will remain there after fabs, the above trick outperforms the FPU by far because one FPU load and one store operation can be saved. These FPU load/store can vastly skew the performance chart, so please be careful with interpreting the results. |
Restrictions |
• designed for 32 bit IEEE floats |
These ads help me to pay my server bills |
|
Performance |
• constant time, data independent + Intel® Pentium™ D: • myAbs: ≈ 1 cycle per number • fabs: ≈ 3 cycles per number (if on FPU stack) + Intel® Core™ 2: • myAbs: ≈ 1 cycle per number • fabs: ≈ 1 cycle per number (if on FPU stack) + Intel® Core™ i7: • myAbs: ≈ 1 cycle per number • fabs: ≈ 1 cycle per number (if on FPU stack) CPU cycles (full optimization, lower values are better) |
Assembler Output |
myAbs:
bits.stephan-brumme.com
|
Download | The full source code including a test program is available for download. |
References |
none |
More Twiddled Bits |
... or go to the index |