github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/bin/bin.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package bin 16 17 import ( 18 "math/bits" 19 20 "github.com/matrixorigin/matrixone/pkg/container/vector" 21 "github.com/matrixorigin/matrixone/pkg/sql/plan/function/builtin/binary" 22 "github.com/matrixorigin/matrixone/pkg/vm/process" 23 "golang.org/x/exp/constraints" 24 ) 25 26 var ( 27 Int8BitLen = bitLen[int8] 28 Int16BitLen = bitLen[int16] 29 Int32BitLen = bitLen[int32] 30 Int64BitLen = bitLen[int64] 31 32 Uint8BitLen = bitLen[uint8] 33 Uint16BitLen = bitLen[uint16] 34 Uint32BitLen = bitLen[uint32] 35 Uint64BitLen = bitLen[uint64] 36 37 Float32BitLen = bitLen[float32] 38 Float64BitLen = bitLen[float64] 39 40 Int8ToBinary = toBinary[int8] 41 Int16ToBinary = toBinary[int16] 42 Int32ToBinary = toBinary[int32] 43 Int64ToBinary = toBinary[int64] 44 45 Uint8ToBinary = toBinary[uint8] 46 Uint16ToBinary = toBinary[uint16] 47 Uint32ToBinary = toBinary[uint32] 48 Uint64ToBinary = toBinary[uint64] 49 50 Float32ToBinary = toBinary[float32] 51 Float64ToBinary = toBinary[float64] 52 ) 53 54 func bitLen[T constraints.Integer | constraints.Float](xs []T) int64 { 55 var r int64 56 for _, x := range xs { 57 // convert to uint64 to count bits 58 val := uint64(x) 59 if val == 0 { 60 // special case for zero 61 r += 1 62 } else { 63 r += int64(bits.Len64(val)) 64 } 65 } 66 return r 67 } 68 69 func toBinary[T constraints.Integer | constraints.Float](xs []T, result []string) []string { 70 for i, x := range xs { 71 result[i] = uintToBinary(uint64(x)) 72 } 73 return result 74 } 75 76 func uintToBinary(x uint64) string { 77 if x == 0 { 78 return "0" 79 } 80 b, i := [64]byte{}, 63 81 for x > 0 { 82 if x&1 == 1 { 83 b[i] = '1' 84 } else { 85 b[i] = '0' 86 } 87 x >>= 1 88 i -= 1 89 } 90 91 return string(b[i+1:]) 92 } 93 94 func Bin[T constraints.Unsigned | constraints.Signed](intputVector, resultVector *vector.Vector, proc *process.Process) error { 95 xs := vector.MustTCols[T](intputVector) 96 rs := make([]string, len(xs)) 97 for idx := range xs { 98 res := uintToBinary(uint64(xs[idx])) 99 rs[idx] = res 100 } 101 vector.AppendString(resultVector, rs, proc.Mp()) 102 return nil 103 } 104 105 func BinFloat[T constraints.Float](intputVector, resultVector *vector.Vector, proc *process.Process) error { 106 xs := vector.MustTCols[T](intputVector) 107 err := binary.NumericToNumericOverflow(proc.Ctx, xs, []int64{}) 108 if err != nil { 109 return err 110 } 111 rs := make([]string, len(xs)) 112 for idx, v := range xs { 113 rs[idx] = uintToBinary(uint64(int64(v))) 114 } 115 vector.AppendString(resultVector, rs, proc.Mp()) 116 return nil 117 }