Hacker News new | past | comments | ask | show | jobs | submit login

It's possible to use a lookup table (sort of) in a register. With x86-64-v2 and later, the code is larger, but potentially faster due to the shorter dependency chain:

int div7 (int x) { return 9 - __builtin_popcountll (0x4081020408102040ULL >> x); }

https://godbolt.org/z/Wj6P6jYGM

I think any monotonic function on 0 .. 63 can be written this way, so should be possible to fold in the outer 63 - nlz expression, too.




This is a very cool technique. Someone ought to benchmark this vs the method described in the original article.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: