github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/mod/mod.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 mod
    16  
    17  /*
    18  #include "mo.h"
    19  
    20  #cgo CFLAGS: -I../../../cgo
    21  #cgo LDFLAGS: -L../../../cgo -lmo -lm
    22  */
    23  import "C"
    24  import (
    25  	"unsafe"
    26  
    27  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    28  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    29  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    30  	"golang.org/x/exp/constraints"
    31  )
    32  
    33  const (
    34  	LEFT_IS_SCALAR  = 1
    35  	RIGHT_IS_SCALAR = 2
    36  )
    37  
    38  func NumericModSigned[T constraints.Signed](xs, ys, rs *vector.Vector) error {
    39  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
    40  	flag := 0
    41  	if xs.IsScalar() {
    42  		flag |= LEFT_IS_SCALAR
    43  	}
    44  	if ys.IsScalar() {
    45  		flag |= RIGHT_IS_SCALAR
    46  	}
    47  
    48  	rc := C.SignedInt_VecMod(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]),
    49  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(rs.Typ.TypeSize()))
    50  	if rc != 0 {
    51  		return moerr.NewOutOfRangeNoCtx("int", "int MOD")
    52  	}
    53  	return nil
    54  }
    55  
    56  func NumericModUnsigned[T constraints.Unsigned](xs, ys, rs *vector.Vector) error {
    57  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
    58  	flag := 0
    59  	if xs.IsScalar() {
    60  		flag |= LEFT_IS_SCALAR
    61  	}
    62  	if ys.IsScalar() {
    63  		flag |= RIGHT_IS_SCALAR
    64  	}
    65  
    66  	rc := C.UnsignedInt_VecMod(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]),
    67  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(rs.Typ.TypeSize()))
    68  	if rc != 0 {
    69  		return moerr.NewOutOfRangeNoCtx("unsigned int", "unsigned int MOD")
    70  	}
    71  	return nil
    72  }
    73  
    74  func NumericModFloat[T constraints.Float](xs, ys, rs *vector.Vector) error {
    75  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
    76  	flag := 0
    77  	if xs.IsScalar() {
    78  		flag |= LEFT_IS_SCALAR
    79  	}
    80  	if ys.IsScalar() {
    81  		flag |= RIGHT_IS_SCALAR
    82  	}
    83  
    84  	rc := C.Float_VecMod(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]),
    85  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(rs.Typ.TypeSize()))
    86  	if rc != 0 {
    87  		return moerr.NewOutOfRangeNoCtx("float", "float MOD")
    88  	}
    89  	return nil
    90  }