Created
May 23, 2021 19:04
-
-
Save soypat/9e938959e3ee899fc2142fdb288f48bf to your computer and use it in GitHub Desktop.
Fast Inverse Root from Quake implemented in Golang
This file contains 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
package quake | |
import "unsafe" | |
type float = float32 | |
// FastISqrt implements Quake fast inverse square root | |
func FastISqrt(number float) float { | |
type long = uint64 | |
const threehalf = 3. / 2. | |
const wtf long = 0x5f3759df | |
var i long | |
var x2, y float | |
x2 = number * 0.5 | |
y = number | |
i = *(*long)(unsafe.Pointer(&y)) | |
i = wtf - (i >> 1) | |
y = *(*float)(unsafe.Pointer(&i)) | |
y = y * (threehalf - (x2 * y * y)) | |
return y | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment