2 ms·
A micro-optimization you will never need
- bhaney 2y agoThis doesn't really have anything to do with the article, but I got nerd-sniped into further optimizing the code it gave, and shaved off another 3 instructions with a little bit twiddling. fun bitsForSize(value: Int): Int { val x = 32 - value.countLeadingZeroBits() if (x < 13) return 13 if (x > 18) return 0 return x + ((x xor (x ushr 2)) and 1) } I'd like to get it branchless, but can't think of a better way to do the weird clamping. Godbolt playground I'm using: https://godbolt.org/z/7nr4Gb5G3 https://godbolt.org/z/7nr4Gb5G3