Skip to content

Instantly share code, notes, and snippets.

@denysvitali
Created April 2, 2019 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denysvitali/d84ca5d8718e7581f24ca12ec912083e to your computer and use it in GitHub Desktop.
Save denysvitali/d84ca5d8718e7581f24ca12ec912083e to your computer and use it in GitHub Desktop.
package main
import (
"github.com/denysvitali/gc_log"
"math"
)
type vec struct {
x float64
y float64
z float64
w float64
}
func clamp(v float64, minv float64, maxv float64) float64 {
return math.Min(math.Max(v, minv), maxv)
}
func main() {
v1 := vec{
0, -1, 0, 0,
}
x := int32(math.Round(clamp(v1.x, -1.0, 1.0) * 511.0))
y := int32(math.Round(clamp(v1.y, -1.0, 1.0) * 511.0))
z := int32(math.Round(clamp(v1.z, -1.0, 1.0) * 511.0))
w := int32(math.Round(clamp(v1.w, -1.0, 1.0) * 1.0))
var packed uint32 = 0
packed += uint32(x) & 0x3FF << 20
packed += uint32(y) & 0x3FF << 10
packed += uint32(z) & 0x3FF << 2
packed += uint32(w) & 0x02 << 0
log.Info("%v %v", []int32{x,y,z,w}, packed)
if packed != 525312 {
panic(packed)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment