Skip to content

Instantly share code, notes, and snippets.

@james4k
Created September 16, 2012 03:31
Show Gist options
  • Save james4k/3730918 to your computer and use it in GitHub Desktop.
Save james4k/3730918 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "unsafe"
func FastInvSqrt(x float32) float32 {
xhalf := float32(0.5) * x
i := *(*int32)(unsafe.Pointer(&x))
i = int32(0x5f3759df) - int32(i>>1)
x = *(*float32)(unsafe.Pointer(&i))
x = x * (1.5 - (xhalf * x * x))
return x
}
func main() {
fmt.Printf("inv sqrt: %f\n", FastInvSqrt(4))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment