rok commented on a change in pull request #9841: URL: https://github.com/apache/arrow/pull/9841#discussion_r609138857
########## File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc ########## @@ -233,6 +236,104 @@ struct DivideChecked { } }; +template <typename T> +inline T integer_power(KernelContext* ctx, T left, T right) { + if (right < 0) { + ctx->SetStatus( + Status::Invalid("integers to negative integer powers are not allowed")); + } + T result = 1; + if (left == 0 && right != 0) { + return 0; + } + while (true) { + if (right % 2) { + result *= left; + } + right /= 2; + if (!right) { + break; + } + left *= left; + } + return result; +} + +template <typename T, typename Arg0, typename Arg1> +inline T integer_power_checked(KernelContext* ctx, Arg0 left, Arg1 right) { + if (right < 0) { + ctx->SetStatus( + Status::Invalid("integers to negative integer powers are not allowed")); + } + T result = 1; + if (left == 0 && right != 0) { + return 0; + } + while (true) { + if (right % 2) { + if (ARROW_PREDICT_FALSE(MultiplyWithOverflow(result, left, &result))) { + ctx->SetStatus(Status::Invalid("overflow")); + } + } + right /= 2; + if (!right) { + break; + } + if (ARROW_PREDICT_FALSE(MultiplyWithOverflow(left, left, &left))) { + ctx->SetStatus(Status::Invalid("overflow")); + } + } + return result; +} + +template <typename T, typename Arg0, typename Arg1> +inline T power(KernelContext* ctx, Arg0 left, Arg1 right) { + if (left == 0 && right < 0) { + ctx->SetStatus(Status::Invalid("divide by zero")); + } + if (left == 0 && right != 0) { + return 0; + } + return pow(left, right); Review comment: Done. -- 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