cyb70289 commented on a change in pull request #9841:
URL: https://github.com/apache/arrow/pull/9841#discussion_r606046786
##########
File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
##########
@@ -233,6 +235,184 @@ struct DivideChecked {
}
};
+struct Power {
+ template <typename T, typename Arg0, typename Arg1>
+ static enable_if_signed_integer<T> Call(KernelContext* ctx, Arg0 left, Arg1
right) {
+ if (right < 0) {
+ ctx->SetStatus(
+ Status::Invalid("integers to negative integer powers are not
allowed"));
+ }
+ if (left == 0 && right != 0) {
+ return 0;
+ }
+ Arg0 result = 1;
+ for (Arg1 i = 0; i < right; i++) {
+ MultiplyWithOverflow(result, left, &result);
+ }
+ return result;
+ }
+
+ template <typename T, typename Arg0, typename Arg1>
+ static enable_if_unsigned_integer<T> Call(KernelContext* ctx, Arg0 left,
Arg1 right) {
+ if (left == 0 && right != 0) {
+ return 0;
+ }
+ Arg0 result = 1;
+ for (Arg1 i = 0; i < right; i++) {
+ MultiplyWithOverflow(result, left, &result);
+ }
+ return result;
+ }
+
+ template <typename T, typename Arg0, typename Arg1>
+ static enable_if_floating_point<T> Call(KernelContext* ctx, Arg0 left, Arg1
right) {
+ if (std::isnan(left) || std::isnan(right)) {
+ return NAN;
+ }
Review comment:
I think pow() can handle this case.
##########
File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
##########
@@ -233,6 +235,184 @@ struct DivideChecked {
}
};
+struct Power {
+ template <typename T, typename Arg0, typename Arg1>
+ static enable_if_signed_integer<T> Call(KernelContext* ctx, Arg0 left, Arg1
right) {
+ if (right < 0) {
+ ctx->SetStatus(
+ Status::Invalid("integers to negative integer powers are not
allowed"));
+ }
+ if (left == 0 && right != 0) {
+ return 0;
+ }
+ Arg0 result = 1;
+ for (Arg1 i = 0; i < right; i++) {
+ MultiplyWithOverflow(result, left, &result);
+ }
+ return result;
+ }
+
+ template <typename T, typename Arg0, typename Arg1>
+ static enable_if_unsigned_integer<T> Call(KernelContext* ctx, Arg0 left,
Arg1 right) {
+ if (left == 0 && right != 0) {
+ return 0;
+ }
+ Arg0 result = 1;
+ for (Arg1 i = 0; i < right; i++) {
+ MultiplyWithOverflow(result, left, &result);
+ }
+ return result;
+ }
+
+ template <typename T, typename Arg0, typename Arg1>
+ static enable_if_floating_point<T> Call(KernelContext* ctx, Arg0 left, Arg1
right) {
+ if (std::isnan(left) || std::isnan(right)) {
+ return NAN;
+ }
+ if (left == 0 && right < 0) {
+ ctx->SetStatus(Status::Invalid("divide by zero"));
+ }
+ return pow(left, right);
+ }
+};
+
+struct PowerChecked {
Review comment:
I see many similar codes in this change. Should refine to keep code
concise.
##########
File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
##########
@@ -233,6 +235,184 @@ struct DivideChecked {
}
};
+struct Power {
+ template <typename T, typename Arg0, typename Arg1>
+ static enable_if_signed_integer<T> Call(KernelContext* ctx, Arg0 left, Arg1
right) {
+ if (right < 0) {
+ ctx->SetStatus(
+ Status::Invalid("integers to negative integer powers are not
allowed"));
+ }
+ if (left == 0 && right != 0) {
+ return 0;
+ }
+ Arg0 result = 1;
+ for (Arg1 i = 0; i < right; i++) {
+ MultiplyWithOverflow(result, left, &result);
Review comment:
Without checking the return value? So why bother this costly overflow
guarded multiplication?
PS: Looks old comment about a more efficient implementation is not
addressed. I'm okay to start with easier approach. Then optimize later.
##########
File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
##########
@@ -233,6 +235,184 @@ struct DivideChecked {
}
};
+struct Power {
+ template <typename T, typename Arg0, typename Arg1>
+ static enable_if_signed_integer<T> Call(KernelContext* ctx, Arg0 left, Arg1
right) {
+ if (right < 0) {
+ ctx->SetStatus(
+ Status::Invalid("integers to negative integer powers are not
allowed"));
+ }
+ if (left == 0 && right != 0) {
+ return 0;
+ }
+ Arg0 result = 1;
+ for (Arg1 i = 0; i < right; i++) {
+ MultiplyWithOverflow(result, left, &result);
+ }
+ return result;
+ }
+
+ template <typename T, typename Arg0, typename Arg1>
+ static enable_if_unsigned_integer<T> Call(KernelContext* ctx, Arg0 left,
Arg1 right) {
Review comment:
Is it possible to use a single function for integers, no matter signed
or unsigned?
--
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]