A micro-optimization you will never need
romainguy.dev
romainguy.dev
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