Skip to content

Instantly share code, notes, and snippets.

@corlaez
Last active June 2, 2023 20:40
Show Gist options
  • Save corlaez/2350fc218371f57d35b28e9a02bf90e1 to your computer and use it in GitHub Desktop.
Save corlaez/2350fc218371f57d35b28e9a02bf90e1 to your computer and use it in GitHub Desktop.
Fast Inverse Square Root in Kotlin
import kotlin.math.pow
fun fastInverseSqrt(number: Float): Float {
val ieee754FloatBits = number.toBits() // evil floating point bit hack, avoided
val invSqrt = Float.fromBits(0x5f3759df - (floatsBits shr 1)) // what the fish
return invSqrt * (1.5F - (0.5F * number * invSqrt * invSqrt)) // Newton iteration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment