Hacker News new | ask | show | jobs
by youarentrightjr 83 days ago
> The same trick can also be used for the other direction to save a division: NewValue = OldValue >> 3; This is basically the same as NewValue = OldValue / 8; RCT does this trick all the time, and even in its OpenRCT2 version, this syntax hasn’t been changed, since compilers won’t do this optimization for you.

(emphasis mine)

Not at all true. Assuming the types are such that >> is equivalent to /, modern compilers will implement division by a power of two as a shift every single time.

4 comments

Yeah. I'm surprised this along with the money thing are listed in the article at all. These are the sort of things you learn within the first month of writing assembly, and were widely used across the industry at the time (and times prior). The bit shifting optimization is performed by GCC even at -O0, and likely already was at the time, as it's one of the simpler optimizations to make. It's like calling "xor eax, eax" a masterful optimization tactic for clearing a register.

Looking at the macro-level optimizations like the rest of the article does is significantly more interesting.

"XOR AXAX" was my license plate in the 90s.
I had "PUSH EAX" and "BX LR" :)
they will do it for unsigned. for signed they will do a bit more to do the same rounding as C promises
Here is how that looks like:

https://godbolt.org/z/rooee4esd

It's unfortunate that C tied overflow behavior to the signedness of integer types.
That whole section is kind of weird. The mention of operator overloading also seems out of place, since the operator is not overloaded here at all.
Yeah and this can be verified with the minimum level of research you'd expect for this kind of article, e.g. by firing up compiler explorer.