github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/abs/abs.go (about) 1 // Copyright 2022 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 abs 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/common/moerr" 19 "github.com/matrixorigin/matrixone/pkg/container/types" 20 "golang.org/x/exp/constraints" 21 ) 22 23 var ( 24 AbsUint8 func([]uint8, []uint8) []uint8 25 AbsUint16 func([]uint16, []uint16) []uint16 26 AbsUint32 func([]uint32, []uint32) []uint32 27 AbsUint64 func([]uint64, []uint64) []uint64 28 AbsInt8 func([]int8, []int8) []int8 29 AbsInt16 func([]int16, []int16) []int16 30 AbsInt32 func([]int32, []int32) []int32 31 AbsInt64 func([]int64, []int64) []int64 32 AbsFloat32 func([]float32, []float32) []float32 33 AbsFloat64 func([]float64, []float64) []float64 34 AbsDecimal128 func([]types.Decimal128, []types.Decimal128) []types.Decimal128 35 ) 36 37 func init() { 38 AbsUint8 = absUnsigned[uint8] 39 AbsUint16 = absUnsigned[uint16] 40 AbsUint32 = absUnsigned[uint32] 41 AbsUint64 = absUnsigned[uint64] 42 AbsInt8 = absSigned[int8] 43 AbsInt16 = absSigned[int16] 44 AbsInt32 = absSigned[int32] 45 AbsInt64 = absSigned[int64] 46 AbsFloat32 = absSigned[float32] 47 AbsFloat64 = absSigned[float64] 48 AbsDecimal128 = absDecimal128 49 } 50 51 func absUnsigned[T constraints.Unsigned](xs, rs []T) []T { 52 copy(rs, xs) 53 return rs 54 } 55 56 // Signed, flip sign and check out of range. 57 func absSigned[T constraints.Signed | constraints.Float](xs, rs []T) []T { 58 for i := range xs { 59 if xs[i] < 0 { 60 rs[i] = -xs[i] 61 } else { 62 rs[i] = xs[i] 63 } 64 if rs[i] < 0 { 65 panic(moerr.NewOutOfRangeNoCtx("int", "'%v'", xs[i])) 66 } 67 } 68 return rs 69 } 70 71 func absDecimal128(xs []types.Decimal128, rs []types.Decimal128) []types.Decimal128 { 72 for i := range xs { 73 if xs[i].Lt(types.Decimal128_FromInt32(0)) { 74 rs[i] = types.NegDecimal128(xs[i]) 75 } else { 76 rs[i] = xs[i] 77 } 78 } 79 return rs 80 }