(no title)
whb07 | 3 years ago
I Can imagine in the past, this was “faster”, yet clang/gcc can emit the same by just writing a basic A/B function.
Seems the win goes to readability by reducing some of these old school hacks.
What say you, greybeards ?
dhosek|3 years ago
Similarly, a fast divisibility test (we’ll assume we’re dividing n by some odd prime p):
1. Shift the bits of p right so that there is a 1 in the last position.
2. If n = p then p∣n, if n < p then p∤n, otherwise continue.
3. Subtract p and go back to step 1.
(One of my ADD habits during meetings is to find the prime factors phone numbers or anything else very long. I do something similar with the numbers in decimal, but for this, I’ll subtract multiples of the potential divisor to get 0s at the end of the number in decimal. I remember co-workers puzzling over a piece of paper with my notes I left behind in a conference room trying to figure out what the numbers represented and how the process worked.)
unwind|3 years ago
cornstalks|3 years ago
For example, see the article and discussion on Bitwise Division from two days ago: https://news.ycombinator.com/item?id=34981027
mras0|3 years ago
Of course that tiny bit of extra work is usually negligible, but might explain why the idiom has stuck around longer than you might otherwise expect.
benj111|3 years ago
AlotOfReading|3 years ago
But yes, fast inverse sqrt is obsolete.
zh3|3 years ago
Makes me wonder who pays attention to this sort of thing these days :)
JohnFen|3 years ago
Usually, it can't -- but sometimes...
JohnFen|3 years ago
Oh, yes. I used to do that sort of thing frequently because the time savings was significant enough. As you say, though, compilers have improved a great deal since then, so it's not generally needed anymore.
If stupid bit tricks like that aren't necessary, they shouldn't be used. They do bring a readability/mental load cost with them.
moonchild|3 years ago
nomemory|3 years ago
With -O1 it performed the optimisation.