github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/convert/float-slice-unsafe.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package convert
     4  
     5  import (
     6  	"reflect"
     7  	"unsafe"
     8  )
     9  
    10  // UnsafeFloat32SliceToByteSlice returns ...
    11  func UnsafeFloat32SliceToByteSlice(req []float32) (res []byte) {
    12  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    13  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    14  	resStruct.Data = reqStruct.Data
    15  	resStruct.Len = reqStruct.Len * 4
    16  	resStruct.Cap = reqStruct.Cap * 4
    17  	return
    18  }
    19  
    20  // UnsafeByteSliceToFloat32Slice returns ...
    21  func UnsafeByteSliceToFloat32Slice(req []byte) (res []float32) {
    22  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    23  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    24  	resStruct.Data = reqStruct.Data
    25  	resStruct.Len = reqStruct.Len / 4
    26  	resStruct.Cap = reqStruct.Cap / 4
    27  	return
    28  }
    29  
    30  // UnsafeFloat64SliceToByteSlice returns ...
    31  func UnsafeFloat64SliceToByteSlice(req []float64) (res []byte) {
    32  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    33  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    34  	resStruct.Data = reqStruct.Data
    35  	resStruct.Len = reqStruct.Len * 8
    36  	resStruct.Cap = reqStruct.Cap * 8
    37  	return
    38  }
    39  
    40  // UnsafeByteSliceToFloat64Slice returns ...
    41  func UnsafeByteSliceToFloat64Slice(req []byte) (res []float64) {
    42  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    43  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    44  	resStruct.Data = reqStruct.Data
    45  	resStruct.Len = reqStruct.Len / 8
    46  	resStruct.Cap = reqStruct.Cap / 8
    47  	return
    48  }