github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/convert/complex-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  // UnsafeComplex64SliceToByteSlice returns ...
    11  func UnsafeComplex64SliceToByteSlice(req []complex64) (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 * 8
    16  	resStruct.Cap = reqStruct.Cap * 8
    17  	return
    18  }
    19  
    20  // UnsafeByteSliceToComplex64Slice returns ...
    21  func UnsafeByteSliceToComplex64Slice(req []byte) (res []complex64) {
    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 / 8
    26  	resStruct.Cap = reqStruct.Cap / 8
    27  	return
    28  }
    29  
    30  // UnsafeComplex128SliceToByteSlice returns ...
    31  func UnsafeComplex128SliceToByteSlice(req []complex128) (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 * 16
    36  	resStruct.Cap = reqStruct.Cap * 16
    37  	return
    38  }
    39  
    40  // UnsafeByteSliceToComplex128Slice returns ...
    41  func UnsafeByteSliceToComplex128Slice(req []byte) (res []complex128) {
    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 / 16
    46  	resStruct.Cap = reqStruct.Cap / 16
    47  	return
    48  }