4 ms·
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
by fweimer 4y ago
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 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.
- gregschlom 4y agoThis is a very cool technique. Someone ought to benchmark this vs the method described in the original article.