|
Sign comparison |
|
Code |
01: bool sameSign(int x, int y)
bits.stephan-brumme.com
|
Explanation |
The highest bit of an integer is called the sign bit. It is set to 1 for all negative values or 0 for zero and all positive values. For two values x and y, the following holds true: signX XOR signY is only 1 if their sign differs. If XOR is applied to all bits of these values (not just the sign bits), we can check the sign of the result: It is only positive if both have the same sign. The obvious approach of sameSign suffers from the high latency of teh setge assembler instruction. sameSignFast32 shifts the sign bit to the lowest bits and clears all other bits. The resulting value (0 for same sign, 1 for different sign) must be negated before converting to a bool. The performance figures for float values are identical to integers' if these floats are stored in main memory. |
Restrictions |
• sameSignFast32 designed for 32 bits, can be easily modified for 64 bits • sameSignFloat may not work with infinity and/or degenerated numbers |
These ads help me to pay my server bills |
|
Performance |
• constant execution time because branch free + Intel® Pentium™ D: • sameSignSimple: ≈ 5 cycles per comparison • sameSign: ≈ 10 cycles per comparison • sameSignFast32: ≈ 4 cycles per comparison + Intel® Core™ 2: • sameSignSimple: ≈ 10 cycles per comparison • sameSign: ≈ 3.75 cycles per comparison • sameSignFast32: ≈ 4 cycles per comparison + Intel® Core™ i7: • sameSignSimple: ≈ 7 cycles per comparison • sameSign: ≈ 4 cycles per comparison • sameSignFast32: ≈ 4 cycles per comparison CPU cycles (full optimization, lower values are better) |
Assembler Output |
sameSign:
bits.stephan-brumme.com
|
Download | The full source code including a test program is available for download. |
References |
multiple sources, author unknown |
More Twiddled Bits |
... or go to the index |