cyb70289 commented on a change in pull request #10530:
URL: https://github.com/apache/arrow/pull/10530#discussion_r651405645



##########
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:
       For unsigned char 128, arithmetic right shift 1 bit results to 192, 
looks a bit surprising.
   Looks arithmetic right shift is more meaningful for signed integers, and 
logical right shift for unsigned integers.

##########
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:
       Though it's not a checked kernel, I think it's still necessary to make 
sure no UB can happen.




-- 
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


Reply via email to