Skip to content

Instantly share code, notes, and snippets.

@alco
Forked from james4k/gist:3730918
Created September 16, 2012 07:03
Show Gist options
  • Select an option

  • Save alco/3731366 to your computer and use it in GitHub Desktop.

Select an option

Save alco/3731366 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "math"
func FastInvSqrt(x float32) float32 {
xhalf := float32(0.5) * x
i := math.Float32bits(x)
i = 0x5f3759df - i>>1
x = math.Float32frombits(i)
x = x * (1.5 - (xhalf * x * x))
return x
}
func main() {
fmt.Printf("inv sqrt: %f\n", FastInvSqrt(4))
}
@alco
Copy link
Copy Markdown
Author

alco commented Sep 16, 2012

Ha. Turns out the implementations of math.Float32bits and math.Float32frombits use the unsafe package (source). Still, this version is more readable.

@bazhenovc
Copy link
Copy Markdown

Reminds me q3 sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment