pitrou commented on a change in pull request #10530: URL: https://github.com/apache/arrow/pull/10530#discussion_r651641148
########## File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc ########## @@ -397,6 +397,130 @@ struct PowerChecked { } }; +// Bitwise operations + +struct BitWiseNot { + template <typename T, typename Arg> + static T Call(KernelContext*, Arg arg, Status*) { + return ~arg; + } +}; + +struct BitWiseAnd { + template <typename T, typename Arg0, typename Arg1> + static T Call(KernelContext*, Arg0 lhs, Arg1 rhs, Status*) { + return lhs & rhs; + } +}; + +struct BitWiseOr { + template <typename T, typename Arg0, typename Arg1> + static T Call(KernelContext*, Arg0 lhs, Arg1 rhs, Status*) { + return lhs | rhs; + } +}; + +struct BitWiseXor { + template <typename T, typename Arg0, typename Arg1> + static T Call(KernelContext*, Arg0 lhs, Arg1 rhs, Status*) { + return lhs ^ rhs; + } +}; + +struct ShiftLeftLogical { + template <typename T, typename Arg0, typename Arg1> + static T Call(KernelContext*, Arg0 lhs, Arg1 rhs, Status*) { + static_assert(std::is_same<T, Arg0>::value, ""); + return lhs << rhs; + } +}; + +// See SEI CERT C Coding Standard rule INT34-C +struct ShiftLeftLogicalChecked { + template <typename T, typename Arg0, typename Arg1> + static enable_if_unsigned_integer<T> Call(KernelContext*, Arg0 lhs, Arg1 rhs, + Status* st) { + static_assert(std::is_same<T, Arg0>::value, ""); + if (ARROW_PREDICT_FALSE(rhs < 0)) { + *st = Status::Invalid("Both operands must be non-negative"); + return lhs; + } + if (ARROW_PREDICT_FALSE(rhs >= std::numeric_limits<Arg0>::digits)) { + *st = Status::Invalid("overflow"); + return lhs; + } + return lhs << rhs; + } + + template <typename T, typename Arg0, typename Arg1> + static enable_if_signed_integer<T> Call(KernelContext*, Arg0 lhs, Arg1 rhs, + Status* st) { + static_assert(std::is_same<T, Arg0>::value, ""); + if (ARROW_PREDICT_FALSE(lhs < 0 || rhs < 0)) { + *st = Status::Invalid("Both operands must be non-negative"); Review comment: I don't understand why `lhs` cannot be negative? Left-shifting a negative number works fine (unless the value overflows, of course). -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org