Hacker News new | ask | show | jobs
by gus_massa 500 days ago
If the floating point trick worth it? When I was young, floating point operations were very slow and I still try to avoid them like the plague.

I remembered there was a trick to avoid popcount, but I didn't remember it. So I found https://stackoverflow.com/questions/51387998/count-bits-1-on...

That version counts, the 1 bit's but here we already know the number is not 0 and we want to know that the inverted number it has exactly 1 bit set, so instead of

  popcnt(n) == 1
we can use

  n & (n-1) == 0

Moreover, n is the inverted number

  n = 0b1111111111 - s
so an alternative is to run the check in the original number, and I think that this does the trick:

  s | (s+1) == 0b1111111111
1 comments

floating point has the same bandwidth and ~3x the latency of integer+/-