lidavidm commented on a change in pull request #10530:
URL: https://github.com/apache/arrow/pull/10530#discussion_r651715368
##########
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:
A left shift is undefined on a negative number.
> if E1 has a signed type and non-negative value, and E1×2^E2 is
representable in the corresponding unsigned type of the result type, then that
value, converted to the result type, is the resulting value; otherwise, the
behavior is undefined
[C++11 standard 5.8.2 (page
118)](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf) or see
the [MSVC
page](https://docs.microsoft.com/en-us/cpp/cpp/left-shift-and-right-shift-operators-input-and-output?view=msvc-160#left-shifts)
##########
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");
+ return lhs;
+ }
+ if (ARROW_PREDICT_FALSE(rhs >= std::numeric_limits<Arg0>::digits)) {
+ *st = Status::Invalid("overflow");
+ return lhs;
+ }
+ if (ARROW_PREDICT_FALSE(lhs > (std::numeric_limits<Arg0>::max() >> rhs))) {
+ *st = Status::Invalid("overflow");
+ return lhs;
+ }
+ return lhs << rhs;
+ }
+};
+
+struct ShiftRightArithmetic {
+ template <typename T, typename Arg0, typename Arg1>
+ static T Call(KernelContext*, Arg0 lhs, Arg1 rhs, Status*) {
+ using Signed = typename std::make_signed<Arg0>::type;
+ static_assert(std::is_same<T, Arg0>::value, "");
+ // N.B. this is implementation-defined but GCC and MSVC document
+ // this as arithmetic right shift. References:
+ //
https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html#Integers-implementation
+ //
https://docs.microsoft.com/en-us/cpp/cpp/left-shift-and-right-shift-operators-input-and-output?view=msvc-160#right-shifts
+ // Clang doesn't document their behavior.
+ return static_cast<T>(static_cast<Signed>(lhs) >> rhs);
Review comment:
Fair. I wanted to have it explicit which type of shift was being done
but I can let it instead depend on the signedness of the input and condense it
into a single kernel.
##########
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;
Review comment:
Good point, will fix.
--
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:
[email protected]