cyb70289 commented on code in PR #14255:
URL: https://github.com/apache/arrow/pull/14255#discussion_r981984818


##########
go/arrow/compute/internal/kernels/_lib/base_arithmetic_avx2_amd64.s:
##########
@@ -0,0 +1,12469 @@
+       .text
+       .intel_syntax noprefix
+       .file   "base_arithmetic.cc"
+       .globl  arithmetic_avx2                 # -- Begin function 
arithmetic_avx2
+       .p2align        4, 0x90
+       .type   arithmetic_avx2,@function
+arithmetic_avx2:                        # @arithmetic_avx2

Review Comment:
   Looks this file is to generate another assembly file compatible to go, is it 
strictly necessary to check in this file?



##########
go/arrow/compute/arithmetic_test.go:
##########
@@ -0,0 +1,229 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package compute_test
+
+import (
+       "context"
+       "fmt"
+       "strings"
+       "testing"
+
+       "github.com/apache/arrow/go/v10/arrow"
+       "github.com/apache/arrow/go/v10/arrow/array"
+       "github.com/apache/arrow/go/v10/arrow/compute"
+       "github.com/apache/arrow/go/v10/arrow/compute/internal/exec"
+       "github.com/apache/arrow/go/v10/arrow/memory"
+       "github.com/apache/arrow/go/v10/arrow/scalar"
+       "github.com/stretchr/testify/suite"
+)
+
+type binaryFunc = func(context.Context, compute.ArithmeticOptions, 
compute.Datum, compute.Datum) (compute.Datum, error)
+
+type BinaryArithmeticSuite[T exec.NumericTypes] struct {
+       suite.Suite
+
+       mem  *memory.CheckedAllocator
+       opts compute.ArithmeticOptions
+       ctx  context.Context
+}
+
+func (BinaryArithmeticSuite[T]) DataType() arrow.DataType {
+       return exec.GetDataType[T]()
+}
+
+func (b *BinaryArithmeticSuite[T]) SetupTest() {
+       b.mem = memory.NewCheckedAllocator(memory.DefaultAllocator)
+       b.opts.CheckOverflow = false
+       b.ctx = compute.WithAllocator(context.TODO(), b.mem)
+}
+
+func (b *BinaryArithmeticSuite[T]) TearDownTest() {
+       b.mem.AssertSize(b.T(), 0)
+}
+
+func (b *BinaryArithmeticSuite[T]) makeNullScalar() scalar.Scalar {
+       return scalar.MakeNullScalar(b.DataType())
+}
+
+func (b *BinaryArithmeticSuite[T]) makeScalar(val T) scalar.Scalar {
+       return scalar.MakeScalar(val)
+}
+
+func (b *BinaryArithmeticSuite[T]) assertBinopScalars(fn binaryFunc, lhs, rhs 
T, expected T) {
+       left, right := b.makeScalar(lhs), b.makeScalar(rhs)
+       exp := b.makeScalar(expected)
+
+       actual, err := fn(b.ctx, b.opts, &compute.ScalarDatum{Value: left}, 
&compute.ScalarDatum{Value: right})
+       b.NoError(err)
+       sc := actual.(*compute.ScalarDatum).Value
+
+       b.Truef(scalar.Equals(exp, sc), "expected: %s\ngot: %s", exp, sc)
+}
+
+func (b *BinaryArithmeticSuite[T]) assertBinopScArr(fn binaryFunc, lhs T, rhs, 
expected string) {
+       left := b.makeScalar(lhs)
+       b.assertBinopScalarArr(fn, left, rhs, expected)
+}
+
+func (b *BinaryArithmeticSuite[T]) assertBinopScalarArr(fn binaryFunc, lhs 
scalar.Scalar, rhs, expected string) {
+       right, _, _ := array.FromJSON(b.mem, b.DataType(), 
strings.NewReader(rhs))
+       defer right.Release()
+       exp, _, _ := array.FromJSON(b.mem, b.DataType(), 
strings.NewReader(expected))
+       defer exp.Release()
+
+       actual, err := fn(b.ctx, b.opts, &compute.ScalarDatum{Value: lhs}, 
&compute.ArrayDatum{Value: right.Data()})
+       b.NoError(err)
+       defer actual.Release()
+       assertDatumsEqual(b.T(), &compute.ArrayDatum{Value: exp.Data()}, actual)
+}
+
+func (b *BinaryArithmeticSuite[T]) assertBinopArrSc(fn binaryFunc, lhs string, 
rhs T, expected string) {
+       right := b.makeScalar(rhs)
+       b.assertBinopArrScalar(fn, lhs, right, expected)
+}
+
+func (b *BinaryArithmeticSuite[T]) assertBinopArrScalar(fn binaryFunc, lhs 
string, rhs scalar.Scalar, expected string) {
+       left, _, _ := array.FromJSON(b.mem, b.DataType(), 
strings.NewReader(lhs))
+       defer left.Release()
+       exp, _, _ := array.FromJSON(b.mem, b.DataType(), 
strings.NewReader(expected))
+       defer exp.Release()
+
+       actual, err := fn(b.ctx, b.opts, &compute.ArrayDatum{Value: 
left.Data()}, &compute.ScalarDatum{Value: rhs})
+       b.NoError(err)
+       defer actual.Release()
+       assertDatumsEqual(b.T(), &compute.ArrayDatum{Value: exp.Data()}, actual)
+}
+
+func (b *BinaryArithmeticSuite[T]) assertBinopArrays(fn binaryFunc, lhs, rhs, 
expected string) {
+       left, _, _ := array.FromJSON(b.mem, b.DataType(), 
strings.NewReader(lhs))
+       defer left.Release()
+       right, _, _ := array.FromJSON(b.mem, b.DataType(), 
strings.NewReader(rhs))
+       defer right.Release()
+       exp, _, _ := array.FromJSON(b.mem, b.DataType(), 
strings.NewReader(expected))
+       defer exp.Release()
+
+       b.assertBinop(fn, left, right, exp)
+}
+
+func (b *BinaryArithmeticSuite[T]) assertBinop(fn binaryFunc, left, right, 
expected arrow.Array) {
+       actual, err := fn(b.ctx, b.opts, &compute.ArrayDatum{Value: 
left.Data()}, &compute.ArrayDatum{Value: right.Data()})
+       b.Require().NoError(err)
+       defer actual.Release()
+       assertDatumsEqual(b.T(), &compute.ArrayDatum{Value: expected.Data()}, 
actual)
+
+       // also check (Scalar, Scalar) operations
+       for i := 0; i < expected.Len(); i++ {
+               s, err := scalar.GetScalar(expected, i)
+               b.Require().NoError(err)
+               lhs, _ := scalar.GetScalar(left, i)
+               rhs, _ := scalar.GetScalar(right, i)
+
+               actual, err := fn(b.ctx, b.opts, &compute.ScalarDatum{Value: 
lhs}, &compute.ScalarDatum{Value: rhs})
+               b.NoError(err)
+               b.Truef(scalar.Equals(s, actual.(*compute.ScalarDatum).Value), 
"expected: %s\ngot: %s", s, actual)
+       }
+}
+
+func (b *BinaryArithmeticSuite[T]) setOverflowCheck(value bool) {
+       b.opts.CheckOverflow = value
+}
+
+func (b *BinaryArithmeticSuite[T]) assertBinopErr(fn binaryFunc, lhs, rhs, 
expectedMsg string) {
+       left, _, _ := array.FromJSON(b.mem, b.DataType(), 
strings.NewReader(lhs))
+       defer left.Release()
+       right, _, _ := array.FromJSON(b.mem, b.DataType(), 
strings.NewReader(rhs))
+       defer right.Release()
+
+       _, err := fn(b.ctx, b.opts, &compute.ArrayDatum{left.Data()}, 
&compute.ArrayDatum{Value: right.Data()})
+       b.ErrorIs(err, arrow.ErrInvalid)
+       b.ErrorContains(err, expectedMsg)
+}
+
+func (b *BinaryArithmeticSuite[T]) TestAdd() {
+       b.Run(b.DataType().String(), func() {
+               for _, overflow := range []bool{false, true} {
+                       b.Run(fmt.Sprintf("overflow=%t", overflow), func() {
+                               b.setOverflowCheck(overflow)

Review Comment:
   Add tests that will trigger overflow?



##########
go/arrow/compute/internal/kernels/_lib/base_arithmetic.cc:
##########
@@ -0,0 +1,255 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <arch.h>
+#include <stdint.h>
+#include "types.h"
+#include "vendored/safe-math.h"
+
+// Define functions AddWithOverflow, SubtractWithOverflow, MultiplyWithOverflow
+// with the signature `bool(T u, T v, T* out)` where T is an integer type.
+// On overflow, these functions return true.  Otherwise, false is returned
+// and `out` is updated with the result of the operation.
+
+#define OP_WITH_OVERFLOW(_func_name, _psnip_op, _type, _psnip_type) \
+  static inline bool _func_name(_type u, _type v, _type* out) {     \
+    return !psnip_safe_##_psnip_type##_##_psnip_op(out, u, v);      \
+  }
+
+#define OPS_WITH_OVERFLOW(_func_name, _psnip_op)            \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, int8_t, int8)     \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, int16_t, int16)   \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, int32_t, int32)   \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, int64_t, int64)   \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, uint8_t, uint8)   \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, uint16_t, uint16) \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, uint32_t, uint32) \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, uint64_t, uint64)
+
+OPS_WITH_OVERFLOW(AddWithOverflow, add)
+OPS_WITH_OVERFLOW(SubtractWithOverflow, sub)
+OPS_WITH_OVERFLOW(MultiplyWithOverflow, mul)
+OPS_WITH_OVERFLOW(DivideWithOverflow, div)
+
+// Corresponds to equivalent ArithmeticOp enum in base_arithmetic.go
+// for passing across which operation to perform. This allows simpler
+// implementation at the cost of having to pass the extra int8 and
+// perform a switch.
+//
+// In cases of small arrays, this is completely negligible. In cases
+// of large arrays, the time saved by using SIMD here is significantly
+// worth the cost.
+enum class optype : int8_t {
+    ADD,
+    ADD_CHECKED,
+    SUB, 
+    SUB_CHECKED,
+};
+
+template <typename T>
+using is_unsigned_integer_value = bool_constant<is_integral_v<T> && 
is_unsigned_v<T>>;
+
+template <typename T>
+using is_signed_integer_value = bool_constant<is_integral_v<T> && 
is_signed_v<T>>;
+
+template <typename T, typename R = T>
+using enable_if_signed_integer_t = 
enable_if_t<is_signed_integer_value<T>::value, R>;
+
+template <typename T, typename R = T>
+using enable_if_unsigned_integer_t = 
enable_if_t<is_unsigned_integer_value<T>::value, R>;
+
+template <typename T, typename R = T>
+using enable_if_integer_t = enable_if_t<
+    is_signed_integer_value<T>::value || is_unsigned_integer_value<T>::value, 
R>;
+
+template <typename T, typename R = T>
+using enable_if_floating_t = enable_if_t<is_floating_point_v<T>, R>;
+
+struct Add {
+    template <typename T, typename Arg0, typename Arg1>
+    static constexpr enable_if_floating_t<T> Call(Arg0 left, Arg1 right, 
bool*) {
+        return left + right;
+    }
+
+    template <typename T, typename Arg0, typename Arg1>
+    static constexpr enable_if_integer_t<T> Call(Arg0 left, Arg1 right, bool*) 
{
+        return left + right;
+    }

Review Comment:
   Same implementation?



##########
go/arrow/compute/arithmetic_test.go:
##########
@@ -0,0 +1,229 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package compute_test
+
+import (
+       "context"
+       "fmt"
+       "strings"
+       "testing"
+
+       "github.com/apache/arrow/go/v10/arrow"

Review Comment:
   Just curious about how to update the version `v10` -> `v11`. There are some 
tools to do it automatically?



##########
go/arrow/compute/internal/kernels/_lib/base_arithmetic.cc:
##########
@@ -0,0 +1,255 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <arch.h>
+#include <stdint.h>
+#include "types.h"
+#include "vendored/safe-math.h"
+
+// Define functions AddWithOverflow, SubtractWithOverflow, MultiplyWithOverflow
+// with the signature `bool(T u, T v, T* out)` where T is an integer type.
+// On overflow, these functions return true.  Otherwise, false is returned
+// and `out` is updated with the result of the operation.
+
+#define OP_WITH_OVERFLOW(_func_name, _psnip_op, _type, _psnip_type) \
+  static inline bool _func_name(_type u, _type v, _type* out) {     \
+    return !psnip_safe_##_psnip_type##_##_psnip_op(out, u, v);      \
+  }
+
+#define OPS_WITH_OVERFLOW(_func_name, _psnip_op)            \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, int8_t, int8)     \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, int16_t, int16)   \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, int32_t, int32)   \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, int64_t, int64)   \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, uint8_t, uint8)   \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, uint16_t, uint16) \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, uint32_t, uint32) \
+  OP_WITH_OVERFLOW(_func_name, _psnip_op, uint64_t, uint64)
+
+OPS_WITH_OVERFLOW(AddWithOverflow, add)
+OPS_WITH_OVERFLOW(SubtractWithOverflow, sub)
+OPS_WITH_OVERFLOW(MultiplyWithOverflow, mul)
+OPS_WITH_OVERFLOW(DivideWithOverflow, div)
+
+// Corresponds to equivalent ArithmeticOp enum in base_arithmetic.go
+// for passing across which operation to perform. This allows simpler
+// implementation at the cost of having to pass the extra int8 and
+// perform a switch.
+//
+// In cases of small arrays, this is completely negligible. In cases
+// of large arrays, the time saved by using SIMD here is significantly
+// worth the cost.
+enum class optype : int8_t {
+    ADD,
+    ADD_CHECKED,
+    SUB, 
+    SUB_CHECKED,
+};
+
+template <typename T>
+using is_unsigned_integer_value = bool_constant<is_integral_v<T> && 
is_unsigned_v<T>>;
+
+template <typename T>
+using is_signed_integer_value = bool_constant<is_integral_v<T> && 
is_signed_v<T>>;
+
+template <typename T, typename R = T>
+using enable_if_signed_integer_t = 
enable_if_t<is_signed_integer_value<T>::value, R>;
+
+template <typename T, typename R = T>
+using enable_if_unsigned_integer_t = 
enable_if_t<is_unsigned_integer_value<T>::value, R>;
+
+template <typename T, typename R = T>
+using enable_if_integer_t = enable_if_t<
+    is_signed_integer_value<T>::value || is_unsigned_integer_value<T>::value, 
R>;
+
+template <typename T, typename R = T>
+using enable_if_floating_t = enable_if_t<is_floating_point_v<T>, R>;
+
+struct Add {
+    template <typename T, typename Arg0, typename Arg1>
+    static constexpr enable_if_floating_t<T> Call(Arg0 left, Arg1 right, 
bool*) {
+        return left + right;
+    }
+
+    template <typename T, typename Arg0, typename Arg1>
+    static constexpr enable_if_integer_t<T> Call(Arg0 left, Arg1 right, bool*) 
{
+        return left + right;
+    }
+};
+
+struct Sub {
+    template <typename T, typename Arg0, typename Arg1>
+    static constexpr enable_if_floating_t<T> Call(Arg0 left, Arg1 right, 
bool*) {
+        return left - right;
+    }
+
+    template <typename T, typename Arg0, typename Arg1>
+    static constexpr enable_if_integer_t<T> Call(Arg0 left, Arg1 right, bool*) 
{
+        return left - right;
+    }
+};
+
+struct AddChecked {
+    template <typename T, typename Arg0, typename Arg1>
+    static constexpr enable_if_floating_t<T> Call(Arg0 left, Arg1 right, 
bool*) {
+        return left + right;
+    }
+
+    template <typename T, typename Arg0, typename Arg1>
+    static constexpr enable_if_integer_t<T> Call(Arg0 left, Arg1 right, bool* 
failure) {

Review Comment:
   What about replace SFINA with `if constexpr`? Should be easier to read.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to