5 ms·
No way to leverage popcount instruction for this?
by gameswithgo 5y ago
No way to leverage popcount instruction for this?
- Scaevolus 5y agolog2 is generally implemented with some variant of the "count leading zeros" instruction, but the general popcount is irrelevant
- pklausler 5y agoint->float conversion, followed by extraction and unbiasing of the exponent, also works for log2 and is more portable.
- failwhaleshark 5y agoI don't normally convert my ints to floats, but when I do, I *(float *)(&x). ;-) (or fild)
- chrisseaton 5y agolzcnt is the first instruction in his solution. Seems it's left as implicit in this post, but if you go back through the chain of posts it's there.
- failwhaleshark 5y agoLZCNT (base 2) would get you to a range of possible decimal digit (base 10) lengths that would need further processing, but it's still more expensive than a table-lookup direct solution directly to the base you're interested in.
- chrisseaton 5y ago> LZCNT (base 2) would get you to a range of possible decimal digit (base 10) lengths that would need further processing, but it's still more expensive than a table-lookup direct solution directly to the base you're interested in. Are you saying you've got a better solution than the blog post that does a direct table lookup? Can you share it?
- failwhaleshark 5y agoWhy did you quote what I said that has nothing to do with it? Do you have a better solution?
- pbsd 5y agoIf you insist, you could calculate the log2 as something like x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return popcount(x-1); but there isn't really much of an advantage to it. lzcnt/bsr are better.