2022-07-19 12:02:41 +00:00
|
|
|
package vector
|
|
|
|
|
|
|
|
import (
|
2022-10-03 09:24:03 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2022-07-22 22:12:37 +00:00
|
|
|
"runtime"
|
2022-07-19 12:02:41 +00:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2022-07-19 13:47:19 +00:00
|
|
|
func Int8ToBytes(data []int8) (buf []byte) {
|
2022-10-03 09:24:03 +00:00
|
|
|
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 13:47:19 +00:00
|
|
|
}
|
2022-07-19 12:02:41 +00:00
|
|
|
|
2022-07-19 13:47:19 +00:00
|
|
|
func Int16ToBytes(data []int16) (buf []byte) {
|
2022-10-03 09:24:03 +00:00
|
|
|
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 13:47:19 +00:00
|
|
|
}
|
2022-07-19 12:02:41 +00:00
|
|
|
|
2022-07-19 13:47:19 +00:00
|
|
|
func Float32ToBytes(data []float32) (buf []byte) {
|
2022-10-03 09:24:03 +00:00
|
|
|
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
|
|
|
}
|