@@ -17,6 +17,7 @@ import (
1717
1818 "github.com/dgraph-io/dgraph/v25/protos/pb"
1919 c "github.com/dgraph-io/dgraph/v25/tok/constraints"
20+ "github.com/stretchr/testify/require"
2021 "github.com/viterin/vek/vek32"
2122 "google.golang.org/protobuf/proto"
2223)
@@ -263,13 +264,13 @@ func BenchmarkEncodeDecodeUint64Matrix(b *testing.B) {
263264 })
264265}
265266
266- func dotProductT [T c.Float ](a , b []T , floatBits int ) {
267- var dotProduct T
267+ func dotProductT [T c.Float ](a , b []T ) {
268+ var product T
268269 if len (a ) != len (b ) {
269270 return
270271 }
271272 for i := range a {
272- dotProduct += a [i ] * b [i ]
273+ product += a [i ] * b [i ]
273274 }
274275}
275276
@@ -295,7 +296,7 @@ func BenchmarkDotProduct(b *testing.B) {
295296 b .Run (fmt .Sprintf ("vek:size=%d" , len (data )),
296297 func (b * testing.B ) {
297298 temp := make ([]float32 , num )
298- BytesAsFloatArray [ float32 ] (data , & temp , 32 )
299+ BytesAsFloatArray (data , & temp , 32 )
299300 for k := 0 ; k < b .N ; k ++ {
300301 vek32 .Dot (temp , temp )
301302 }
@@ -305,7 +306,7 @@ func BenchmarkDotProduct(b *testing.B) {
305306 func (b * testing.B ) {
306307
307308 temp := make ([]float32 , num )
308- BytesAsFloatArray [ float32 ] (data , & temp , 32 )
309+ BytesAsFloatArray (data , & temp , 32 )
309310 for k := 0 ; k < b .N ; k ++ {
310311 dotProduct (temp , temp )
311312 }
@@ -316,9 +317,9 @@ func BenchmarkDotProduct(b *testing.B) {
316317 func (b * testing.B ) {
317318
318319 temp := make ([]float32 , num )
319- BytesAsFloatArray [ float32 ] (data , & temp , 32 )
320+ BytesAsFloatArray (data , & temp , 32 )
320321 for k := 0 ; k < b .N ; k ++ {
321- dotProductT [ float32 ] (temp , temp , 32 )
322+ dotProductT (temp , temp )
322323 }
323324 })
324325}
@@ -379,7 +380,7 @@ func littleEndianBytesAsFloatArray[T c.Float](encoded []byte, retVal *[]T, float
379380 }
380381}
381382
382- func BenchmarkFloatConverstion (b * testing.B ) {
383+ func BenchmarkFloatConversion (b * testing.B ) {
383384 num := 1500
384385 data := make ([]byte , 64 * num )
385386 _ , err := rand .Read (data )
@@ -391,23 +392,41 @@ func BenchmarkFloatConverstion(b *testing.B) {
391392 func (b * testing.B ) {
392393 temp := make ([]float32 , num )
393394 for k := 0 ; k < b .N ; k ++ {
394- BytesAsFloatArray [ float32 ] (data , & temp , 64 )
395+ BytesAsFloatArray (data , & temp , 64 )
395396 }
396397 })
397398
398399 b .Run (fmt .Sprintf ("pointerFloat:size=%d" , len (data )),
399400 func (b * testing.B ) {
400401 temp := make ([]float32 , num )
401402 for k := 0 ; k < b .N ; k ++ {
402- pointerFloatConversion [ float32 ] (data , & temp , 64 )
403+ pointerFloatConversion (data , & temp , 64 )
403404 }
404405 })
405406
406407 b .Run (fmt .Sprintf ("littleEndianFloat:size=%d" , len (data )),
407408 func (b * testing.B ) {
408409 temp := make ([]float32 , num )
409410 for k := 0 ; k < b .N ; k ++ {
410- littleEndianBytesAsFloatArray [ float32 ] (data , & temp , 64 )
411+ littleEndianBytesAsFloatArray (data , & temp , 64 )
411412 }
412413 })
413414}
415+
416+ func TestBytesAsFloatArray (t * testing.T ) {
417+ var out32 []float32
418+ in32 := []float32 {0.1 , 1.123456789 , 50000.00005 }
419+
420+ inf32 := unsafe .Slice ((* byte )(unsafe .Pointer (& in32 [0 ])), len (in32 )* int (unsafe .Sizeof (in32 [0 ])))
421+
422+ BytesAsFloatArray (inf32 , & out32 , 32 )
423+ require .Equal (t , in32 , out32 )
424+
425+ var out64 []float64
426+ in64 := []float64 {0.1 , 1.123456789 , 50000.00005 }
427+
428+ inf64 := unsafe .Slice ((* byte )(unsafe .Pointer (& in64 [0 ])), len (in64 )* int (unsafe .Sizeof (in64 [0 ])))
429+
430+ BytesAsFloatArray (inf64 , & out64 , 64 )
431+ require .Equal (t , in64 , out64 )
432+ }
0 commit comments