Skip to content

Commit

Permalink
Preliminary work on floating-point support.
Browse files Browse the repository at this point in the history
  • Loading branch information
feyeleanor committed Feb 24, 2010
1 parent ecf0dd1 commit 0da15f7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vm/buffer.go
Expand Up @@ -6,6 +6,7 @@ package vm

import . "container/vector"
import "unsafe"
import "math"

func max(x, y int) int { if x > y { return x}; return y }
func min(x, y int) int { if x < y { return x}; return y }
Expand Down Expand Up @@ -52,5 +53,9 @@ func (b *Buffer) GreaterThan(i, j int) bool { a := b.IntVector; return a[i] >
func (b *Buffer) GreaterThanZero(i int) bool { a := b.IntVector; return a[i] > 0 }
func (b *Buffer) Copy(i, j int) { a := b.IntVector; a[i] = a[j] }

//func (b *Buffer) FloatAt(i int) float { a := b.IntVector; return *(*float)(unsafe.Pointer(&a[i])) }
func (b *Buffer) FloatAt(i int) float32 { return math.Float32frombits(uint32(b.IntVector[i])) }
//func (b *Buffer) FloatSet(i int, f float) { }

func (b *Buffer) GetBuffer(i int) *Buffer { return (*Buffer)(unsafe.Pointer(uintptr(b.At(i)))) }
func (b *Buffer) PutBuffer(i int, p *Buffer) { b.Set(i, int(uintptr(unsafe.Pointer(p)))) }
43 changes: 43 additions & 0 deletions vm/buffer_benchmark_test.go
@@ -1,6 +1,28 @@
package vm
import "testing"
import "container/vector"
import "unsafe"

func floatBuffer() []float {
return []float{1.0, 2.0, 3.0, 4.5, 5.4, 6.0, 7.0, 8.0, 9.0, 10.0}
}

func intsToFloats(i []int) []float { return *(*[]float)(unsafe.Pointer(&i)) }
func floatsToInts(f []float) []int { return *(*[]int)(unsafe.Pointer(&f)) }

func BenchmarkBufferFloatsToInts(b *testing.B) {
b.StopTimer()
a := floatBuffer()
b.StartTimer()
for i := 0; i < b.N; i++ { floatsToInts(a) }
}

func BenchmarkBufferFloatsFromInts(b *testing.B) {
b.StopTimer()
a := floatsToInts(floatBuffer())
b.StartTimer()
for i := 0; i < b.N; i++ { f := intsToFloats(a); f[0] += 2.0 }
}

func BenchmarkBufferAt(b *testing.B) {
b.StopTimer()
Expand All @@ -9,13 +31,34 @@ func BenchmarkBufferAt(b *testing.B) {
for i := 0; i < b.N; i++ { buffer.At(0) }
}

func BenchmarkBufferFloatAt(b *testing.B) {
b.StopTimer()
buffer := defaultBuffer()
b.StartTimer()
for i := 0; i < b.N; i++ { buffer.FloatAt(0) }
}

func BenchmarkBufferFloatAdds(b *testing.B) {
b.StopTimer()
a := []float{ 3.2, 4.7, 0.0 }
b.StartTimer()
for i := 0; i < b.N; i++ { a[2] = a[0] + a[1] }
}

func BenchmarkBufferSet(b *testing.B) {
b.StopTimer()
buffer := defaultBuffer()
b.StartTimer()
for i := 0; i < b.N; i++ { buffer.Set(0, 1) }
}

func BenchmarkBufferFloatSet(b *testing.B) {
b.StopTimer()
buffer := defaultBuffer()
b.StartTimer()
for i := 0; i < b.N; i++ { a := 1.099; buffer.Set(0, *(*int)(unsafe.Pointer(&a))) }
}

func BenchmarkBufferAdd(b *testing.B) {
b.StopTimer()
buffer := defaultBuffer()
Expand Down

0 comments on commit 0da15f7

Please sign in to comment.