zeroshade commented on code in PR #14255: URL: https://github.com/apache/arrow/pull/14255#discussion_r992448122
########## go/arrow/compute/internal/kernels/_lib/base_arithmetic.cc: ########## @@ -0,0 +1,175 @@ +// 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" + +// 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, + SUB, + + // this impl doesn't actually perform any overflow checks as we need + // to only run overflow checks on non-null entries + ADD_CHECKED, + SUB_CHECKED, +}; + +struct Add { + template <typename T, typename Arg0, typename Arg1> + static constexpr T Call(Arg0 left, Arg1 right) { + if constexpr (is_arithmetic_v<T>) + return left + right; + } +}; + +struct Sub { + template <typename T, typename Arg0, typename Arg1> + static constexpr T Call(Arg0 left, Arg1 right) { + if constexpr (is_arithmetic_v<T>) + return left - right; + } +}; + +struct AddChecked { + template <typename T, typename Arg0, typename Arg1> + static constexpr T Call(Arg0 left, Arg1 right) { + static_assert(is_same<T, Arg0>::value && is_same<T, Arg1>::value, ""); + if constexpr(is_arithmetic_v<T>) { + return left + right; + } Review Comment: Not sure what you mean as this could be instantiated with *any* type that overloads the `operator+` without the `is_arithmetic_v`. I wanted this limited to *only* arithmetic types. As for the overflow-checking: checking is only performed on integral types in which case we need to explicitly only check the non-null values. For floating point types we can speed things up by just performing the addition/subtraction across the board without having to perform the non-null splitting with the bitmaps. Rather than implement all of the logic for handling the null bitmap here, I explicitly commented (in the Go code where it is called) that we don't actually perform any overflow checking in here, and the `AddChecked` and `SubChecked` are only called by the Go code with floating point values. -- 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]
