Kirika/vector/shared.go

23 lines
609 B
Go
Raw Normal View History

2022-07-19 12:02:41 +00:00
package vector
import (
"golang.org/x/exp/slices"
"runtime"
2022-07-19 12:02:41 +00:00
"unsafe"
)
func Int8ToBytes(data []int8) (buf []byte) {
defer runtime.KeepAlive(data)
2023-04-09 11:10:30 +00:00
return slices.Clone(unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(data))), len(data)))
}
2022-07-19 12:02:41 +00:00
func Int16ToBytes(data []int16) (buf []byte) {
defer runtime.KeepAlive(data)
2023-04-09 11:10:30 +00:00
return slices.Clone(unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(data))), len(data)*2))
}
2022-07-19 12:02:41 +00:00
func Float32ToBytes(data []float32) (buf []byte) {
defer runtime.KeepAlive(data)
2023-04-09 11:10:30 +00:00
return slices.Clone(unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(data))), len(data)*4))
2022-07-19 12:02:41 +00:00
}