Last active
June 2, 2023 20:40
-
-
Save corlaez/2350fc218371f57d35b28e9a02bf90e1 to your computer and use it in GitHub Desktop.
Fast Inverse Square Root in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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