github.com/matrixorigin/matrixone@v1.2.0/pkg/vectorize/sum/sum.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 sum
    16  
    17  import (
    18  	"golang.org/x/exp/constraints"
    19  )
    20  
    21  var (
    22  	Int8Sum      = signedSum[int8]
    23  	Int16Sum     = signedSum[int16]
    24  	Int32Sum     = signedSum[int32]
    25  	Int64Sum     = signedSum[int64]
    26  	Int8SumSels  = signedSumSels[int8]
    27  	Int16SumSels = signedSumSels[int16]
    28  	Int32SumSels = signedSumSels[int32]
    29  	Int64SumSels = signedSumSels[int64]
    30  
    31  	Uint8Sum      = unsignedSum[uint8]
    32  	Uint16Sum     = unsignedSum[uint16]
    33  	Uint32Sum     = unsignedSum[uint32]
    34  	Uint64Sum     = unsignedSum[uint64]
    35  	Uint8SumSels  = unsignedSumSels[uint8]
    36  	Uint16SumSels = unsignedSumSels[uint16]
    37  	Uint32SumSels = unsignedSumSels[uint32]
    38  	Uint64SumSels = unsignedSumSels[uint64]
    39  
    40  	Float32Sum     = floatSum[float32]
    41  	Float64Sum     = floatSum[float64]
    42  	Float32SumSels = floatSumSels[float32]
    43  	Float64SumSels = floatSumSels[float64]
    44  )
    45  
    46  func signedSum[T constraints.Signed](xs []T) int64 {
    47  	var res int64
    48  
    49  	for _, x := range xs {
    50  		res += int64(x)
    51  	}
    52  	return res
    53  }
    54  
    55  func signedSumSels[T constraints.Signed](xs []T, sels []int64) int64 {
    56  	var res int64
    57  
    58  	for _, sel := range sels {
    59  		res += int64(xs[sel])
    60  	}
    61  	return res
    62  }
    63  
    64  func unsignedSum[T constraints.Unsigned](xs []T) uint64 {
    65  	var res uint64
    66  
    67  	for _, x := range xs {
    68  		res += uint64(x)
    69  	}
    70  	return res
    71  }
    72  
    73  func unsignedSumSels[T constraints.Unsigned](xs []T, sels []int64) uint64 {
    74  	var res uint64
    75  
    76  	for _, sel := range sels {
    77  		res += uint64(xs[sel])
    78  	}
    79  	return res
    80  }
    81  
    82  func floatSum[T constraints.Float](xs []T) T {
    83  	var res T
    84  
    85  	for _, x := range xs {
    86  		res += x
    87  	}
    88  	return res
    89  }
    90  
    91  func floatSumSels[T constraints.Float](xs []T, sels []int64) T {
    92  	var res T
    93  
    94  	for _, sel := range sels {
    95  		res += xs[sel]
    96  	}
    97  	return res
    98  }
    99  
   100  /*
   101  func VecSum(rs, vs []uint64, start int64, count int64, vps []uint64, zs []int64, nulls bitmap) {
   102  	for i := int64(0); i < count; i++ {
   103  		if vps[i] == 0 {
   104  			continue
   105  		}
   106  		if nulls.Contains(i + start) {
   107  			continue
   108  		}
   109  		rs[vps[i]-1] += vs[i+start] * zs[i+start]
   110  	}
   111  }
   112  
   113  func VecSumDecimal64(rs, vs []types.Decimal64, start int64, count int64, vps []uint64, zs []int64, nulls bitmap)
   114  
   115  func VecSumDecimal64ToDecimal128(rs []types.Decimal128, vs []types.Decimal64, start int64, count int64, vps []uint64, zs []int64, nulls bitmap)
   116  
   117  func VecSumDecimal128(rs, vs []types.Decimal128, start int64, count int64, vps []uint64, zs []int64, nulls bitmap)
   118  */