I think a bunch of compilers nowadays are smart enough to see these kinds of hacks and compile them to an equivalent on the target that runs best. Favourite example of this is writing a duff's device to unroll a loop, and then gcc completely ignores it and replaces it with a vectorised loop.
FabHK|1 year ago
https://en.wikipedia.org/wiki/Hacker%27s_Delight
mike_hock|1 year ago
> On the other hand, if you prefer the result be either -1, 0, or +1, then use:
> sign = (v != 0) | -(int)((unsigned int)((int)v) >> (sizeof(int) * CHAR_BIT - 1));
> // Or, for more speed but less portability:
> sign = (v != 0) | (v >> (sizeof(int) * CHAR_BIT - 1)); // -1, 0, or +1
> // Or, for portability, brevity, and (perhaps) speed:
> sign = (v > 0) - (v < 0); // -1, 0, or +1
Or if you prefer portability, speed, and readability at the same time:
gets compiled to which is the bit shift hack.(still useful as a reference for implementing an optimizer, to be read as pseudo code)
jimbobthrowawy|1 year ago
anthk|1 year ago
http://www.hakmem.org/
richrichie|1 year ago