github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/div/div.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 div
    16  
    17  /*
    18  #include "mo.h"
    19  
    20  #cgo CFLAGS: -I../../../cgo
    21  #cgo LDFLAGS: -L../../../cgo -lmo -lm
    22  */
    23  import "C"
    24  
    25  import (
    26  	"unsafe"
    27  
    28  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    29  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    30  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    31  	"golang.org/x/exp/constraints"
    32  )
    33  
    34  const (
    35  	LEFT_IS_SCALAR  = 1
    36  	RIGHT_IS_SCALAR = 2
    37  )
    38  
    39  func NumericDivFloat[T constraints.Float](xs, ys, rs *vector.Vector) error {
    40  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
    41  	flag := 0
    42  	if xs.IsScalar() {
    43  		flag |= LEFT_IS_SCALAR
    44  	}
    45  	if ys.IsScalar() {
    46  		flag |= RIGHT_IS_SCALAR
    47  	}
    48  
    49  	rc := C.Float_VecDiv(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]),
    50  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(rs.Typ.TypeSize()))
    51  	if rc != 0 {
    52  		return moerr.NewDivByZeroNoCtx()
    53  	}
    54  	return nil
    55  }
    56  
    57  func NumericIntegerDivFloat[T constraints.Float](xs, ys, rs *vector.Vector) error {
    58  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[int64](rs)
    59  	flag := 0
    60  	if xs.IsScalar() {
    61  		flag |= LEFT_IS_SCALAR
    62  	}
    63  	if ys.IsScalar() {
    64  		flag |= RIGHT_IS_SCALAR
    65  	}
    66  
    67  	rc := C.Float_VecIntegerDiv(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]),
    68  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(rs.Typ.TypeSize()))
    69  	if rc != 0 {
    70  		return moerr.NewDivByZeroNoCtx()
    71  	}
    72  	return nil
    73  }