Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
ARROW-9022: [C++] Add/Sub/Mul arithmetic kernels with overflow check#7420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,6 +18,10 @@ | ||
| #include "arrow/compute/kernels/common.h" | ||
| #include "arrow/util/int_util.h" | ||
| #ifndef __has_builtin | ||
| #define __has_builtin(x) 0 | ||
| #endif | ||
| namespace arrow { | ||
| namespace compute { | ||
| @@ -35,6 +39,10 @@ using enable_if_signed_integer = enable_if_t<is_signed_integer<T>::value, T>; | ||
| template <typename T> | ||
| using enable_if_unsigned_integer = enable_if_t<is_unsigned_integer<T>::value, T>; | ||
| template <typename T> | ||
| using enable_if_integer = | ||
| enable_if_t<is_signed_integer<T>::value || is_unsigned_integer<T>::value, T>; | ||
| template <typename T> | ||
| using enable_if_floating_point = enable_if_t<std::is_floating_point<T>::value, T>; | ||
| @@ -60,6 +68,42 @@ struct Add { | ||
| } | ||
| }; | ||
| struct AddChecked { | ||
| #if __has_builtin(__builtin_add_overflow) | ||
| template <typename T> | ||
| static enable_if_integer<T> Call(KernelContext* ctx, T left, T right) { | ||
| T result; | ||
| if (__builtin_add_overflow(left, right, &result)) { | ||
| ctx->SetStatus(Status::Invalid("overflow")); | ||
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which error should we raise? ExecutionError? Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fine for now | ||
| } | ||
| return result; | ||
| } | ||
| #else | ||
| template <typename T> | ||
| static enable_if_unsigned_integer<T> Call(KernelContext* ctx, T left, T right) { | ||
| if (arrow::internal::HasAdditionOverflow(left, right)) { | ||
| ctx->SetStatus(Status::Invalid("overflow")); | ||
| } | ||
| return left + right; | ||
| } | ||
| template <typename T> | ||
| static enable_if_signed_integer<T> Call(KernelContext* ctx, T left, T right) { | ||
| auto unsigned_left = to_unsigned(left); | ||
| auto unsigned_right = to_unsigned(right); | ||
| if (arrow::internal::HasAdditionOverflow(unsigned_left, unsigned_right)) { | ||
| ctx->SetStatus(Status::Invalid("overflow")); | ||
| } | ||
| return unsigned_left + unsigned_right; | ||
| } | ||
| #endif | ||
| template <typename T> | ||
| static constexpr enable_if_floating_point<T> Call(KernelContext*, T left, T right) { | ||
| return left + right; | ||
| } | ||
| }; | ||
| struct Subtract { | ||
| template <typename T> | ||
| static constexpr enable_if_floating_point<T> Call(KernelContext*, T left, T right) { | ||
| @@ -77,6 +121,40 @@ struct Subtract { | ||
| } | ||
| }; | ||
| struct SubtractChecked { | ||
| #if __has_builtin(__builtin_sub_overflow) | ||
| template <typename T> | ||
| static enable_if_integer<T> Call(KernelContext* ctx, T left, T right) { | ||
| T result; | ||
| if (__builtin_sub_overflow(left, right, &result)) { | ||
| ctx->SetStatus(Status::Invalid("overflow")); | ||
| } | ||
| return result; | ||
| } | ||
| #else | ||
| template <typename T> | ||
| static enable_if_unsigned_integer<T> Call(KernelContext* ctx, T left, T right) { | ||
| if (arrow::internal::HasSubtractionOverflow(left, right)) { | ||
| ctx->SetStatus(Status::Invalid("overflow")); | ||
| } | ||
| return left - right; | ||
| } | ||
| template <typename T> | ||
| static enable_if_signed_integer<T> Call(KernelContext* ctx, T left, T right) { | ||
| if (arrow::internal::HasSubtractionOverflow(left, right)) { | ||
| ctx->SetStatus(Status::Invalid("overflow")); | ||
| } | ||
| return to_unsigned(left) - to_unsigned(right); | ||
| } | ||
| #endif | ||
| template <typename T> | ||
| static constexpr enable_if_floating_point<T> Call(KernelContext*, T left, T right) { | ||
| return left - right; | ||
| } | ||
| }; | ||
| struct Multiply { | ||
| static_assert(std::is_same<decltype(int8_t() * int8_t()), int32_t>::value, ""); | ||
| static_assert(std::is_same<decltype(uint8_t() * uint8_t()), int32_t>::value, ""); | ||
| @@ -116,6 +194,29 @@ struct Multiply { | ||
| } | ||
| }; | ||
| struct MultiplyChecked { | ||
| template <typename T> | ||
| static enable_if_integer<T> Call(KernelContext* ctx, T left, T right) { | ||
| T result; | ||
| #if __has_builtin(__builtin_mul_overflow) | ||
| if (__builtin_mul_overflow(left, right, &result)) { | ||
| ctx->SetStatus(Status::Invalid("overflow")); | ||
| } | ||
| #else | ||
| result = Multiply::Call(ctx, left, right); | ||
| if (left != 0 && result / left != right) { | ||
| ctx->SetStatus(Status::Invalid("overflow")); | ||
| } | ||
| #endif | ||
| return result; | ||
| } | ||
| template <typename T> | ||
| static constexpr enable_if_floating_point<T> Call(KernelContext*, T left, T right) { | ||
| return left * right; | ||
| } | ||
| }; | ||
| namespace codegen { | ||
| // Generate a kernel given an arithmetic functor | ||
| @@ -168,8 +269,11 @@ namespace internal { | ||
| void RegisterScalarArithmetic(FunctionRegistry* registry) { | ||
| codegen::AddBinaryFunction<Add>("add", registry); | ||
| codegen::AddBinaryFunction<AddChecked>("add_checked", registry); | ||
| codegen::AddBinaryFunction<Subtract>("subtract", registry); | ||
| codegen::AddBinaryFunction<SubtractChecked>("subtract_checked", registry); | ||
| codegen::AddBinaryFunction<Multiply>("multiply", registry); | ||
| codegen::AddBinaryFunction<MultiplyChecked>("multiply_checked", registry); | ||
| } | ||
| } // namespace internal | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to document this.
This breaks CI: https://github.com/apache/arrow/runs/786249626
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it in #7492
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!