Hacker News new | past | comments | ask | show | jobs | submit login
A micro-optimization you will never need (romainguy.dev)
3 points by yen223 17 days ago | hide | past | favorite | 1 comment



This 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




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

Search: