-
Notifications
You must be signed in to change notification settings - Fork 4.3k
GH-46901: [C++][Compute] Add remainder and modulo kernels #48914
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
Open
fangchenli
wants to merge
1
commit into
apache:main
Choose a base branch
from
fangchenli:add-remainder-mod-kernels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ namespace arrow { | |
|
|
||
| using internal::AddWithOverflow; | ||
| using internal::DivideWithOverflow; | ||
| using internal::ModuloWithOverflow; | ||
| using internal::MultiplyWithOverflow; | ||
| using internal::NegateWithOverflow; | ||
| using internal::SubtractWithOverflow; | ||
|
|
@@ -468,6 +469,172 @@ struct FloatingDivideChecked { | |
| // TODO: Add decimal | ||
| }; | ||
|
|
||
| // Remainder (truncated): result has same sign as dividend (C/C++ semantics) | ||
| struct Remainder { | ||
|
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. This currently duplicates some logic across all four functors. You could introduce enum class RemainderMode { kTruncated, kFloored };
template <RemainderMode Mode, typename T, typename Divisor>
T FinishRemainder(T remainder, Divisor divisor) {
if constexpr (Mode == RemainderMode::kTruncated) {
return remainder;
}
if constexpr (std::is_floating_point_v<T>) {
if (remainder == 0) {
// Preserve the sign based on the divisor for zero results.
return std::copysign(remainder, divisor);
}
}
if constexpr (!std::is_unsigned_v<T>) {
const T zero{};
if ((remainder > zero && divisor < zero) || (remainder < zero && divisor > zero)) {
remainder += divisor;
}
}
return remainder;
}
template <RemainderMode Mode, bool Checked>
struct RemainderImpl {
template <typename T, typename Arg0, typename Arg1>
static enable_if_floating_value<T> Call(KernelContext*, Arg0 left, Arg1 right,
Status* st) {
static_assert(std::is_same_v<T, Arg0> && std::is_same_v<T, Arg1>);
if constexpr (Checked) {
if (ARROW_PREDICT_FALSE(right == 0)) {
*st = Status::Invalid("divide by zero");
return {};
}
}
return FinishRemainder<Mode>(std::fmod(left, right), right);
}
template <typename T, typename Arg0, typename Arg1>
static enable_if_integer_value<T> Call(KernelContext*, Arg0 left, Arg1 right,
Status* st) {
static_assert(std::is_same_v<T, Arg0> && std::is_same_v<T, Arg1>);
T result{};
if (ARROW_PREDICT_FALSE(ModuloWithOverflow(left, right, &result))) {
if (right == 0) {
*st = Status::Invalid("divide by zero");
} else if constexpr (Checked) {
*st = Status::Invalid("overflow");
}
return {};
}
return FinishRemainder<Mode>(result, right);
}
template <typename T, typename Arg0, typename Arg1>
static enable_if_decimal_value<T> Call(KernelContext*, Arg0 left, Arg1 right,
Status* st) {
static_assert(std::is_same_v<T, Arg0> && std::is_same_v<T, Arg1>);
if (ARROW_PREDICT_FALSE(right == 0)) {
*st = Status::Invalid("divide by zero");
return {};
}
return FinishRemainder<Mode>(left % right, right);
}
};
using Remainder = RemainderImpl<RemainderMode::kTruncated, false>;
using RemainderChecked = RemainderImpl<RemainderMode::kTruncated, true>;
using Modulo = RemainderImpl<RemainderMode::kFloored, false>;
using ModuloChecked = RemainderImpl<RemainderMode::kFloored, true>; |
||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_floating_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status*) { | ||
| return std::fmod(left, right); | ||
| } | ||
|
|
||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_integer_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| T result; | ||
| if (ARROW_PREDICT_FALSE(ModuloWithOverflow(left, right, &result))) { | ||
| if (right == 0) { | ||
| *st = Status::Invalid("divide by zero"); | ||
| } else { | ||
| // INT_MIN % -1 overflow case, result is 0 | ||
| result = 0; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_decimal_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| if (right == Arg1()) { | ||
| *st = Status::Invalid("divide by zero"); | ||
| return T(); | ||
| } | ||
| return left % right; | ||
| } | ||
| }; | ||
|
|
||
| struct RemainderChecked { | ||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_floating_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| static_assert(std::is_same<T, Arg0>::value && std::is_same<T, Arg1>::value, ""); | ||
| if (ARROW_PREDICT_FALSE(right == 0)) { | ||
| *st = Status::Invalid("divide by zero"); | ||
| return 0; | ||
| } | ||
| return std::fmod(left, right); | ||
| } | ||
|
|
||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_integer_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| static_assert(std::is_same<T, Arg0>::value && std::is_same<T, Arg1>::value, ""); | ||
| T result; | ||
| if (ARROW_PREDICT_FALSE(ModuloWithOverflow(left, right, &result))) { | ||
| if (right == 0) { | ||
| *st = Status::Invalid("divide by zero"); | ||
| } else { | ||
| *st = Status::Invalid("overflow"); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_decimal_value<T> Call(KernelContext* ctx, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| return Remainder::Call<T>(ctx, left, right, st); | ||
| } | ||
| }; | ||
|
|
||
| // Helper: Convert truncated remainder to floored modulo for signed types. | ||
| // Floored modulo has the same sign as the divisor (Python semantics). | ||
| template <typename T> | ||
| T AdjustRemainderToFloored(T rem, T right) { | ||
| if constexpr (std::is_signed_v<T>) { | ||
| if ((rem > 0 && right < 0) || (rem < 0 && right > 0)) { | ||
| rem += right; | ||
| } | ||
| } | ||
| return rem; | ||
| } | ||
|
|
||
| // Modulo (floored): result has same sign as divisor (Python semantics) | ||
| struct Modulo { | ||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_floating_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status*) { | ||
| T rem = std::fmod(left, right); | ||
| if (rem == 0) { | ||
| // Preserve the sign based on divisor for zero results | ||
| return std::copysign(rem, right); | ||
| } | ||
| return AdjustRemainderToFloored(rem, right); | ||
| } | ||
|
|
||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_integer_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| T result; | ||
| if (ARROW_PREDICT_FALSE(ModuloWithOverflow(left, right, &result))) { | ||
| if (right == 0) { | ||
| *st = Status::Invalid("divide by zero"); | ||
| } else { | ||
| // INT_MIN % -1 overflow case, result is 0 | ||
| result = 0; | ||
| } | ||
| return result; | ||
| } | ||
| return AdjustRemainderToFloored(result, right); | ||
| } | ||
|
|
||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_decimal_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| static const T kZero{}; | ||
| if (right == kZero) { | ||
| *st = Status::Invalid("divide by zero"); | ||
| return T(); | ||
| } | ||
| T rem = left % right; | ||
| // Convert truncated to floored: adjust if signs differ | ||
| if ((rem > kZero && right < kZero) || (rem < kZero && right > kZero)) { | ||
| rem = rem + right; | ||
| } | ||
| return rem; | ||
| } | ||
| }; | ||
|
|
||
| struct ModuloChecked { | ||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_floating_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| static_assert(std::is_same<T, Arg0>::value && std::is_same<T, Arg1>::value, ""); | ||
| if (ARROW_PREDICT_FALSE(right == 0)) { | ||
| *st = Status::Invalid("divide by zero"); | ||
| return 0; | ||
| } | ||
| T rem = std::fmod(left, right); | ||
| if (rem == 0) { | ||
| // Preserve the sign based on divisor for zero results | ||
| return std::copysign(rem, right); | ||
| } | ||
| return AdjustRemainderToFloored(rem, right); | ||
| } | ||
|
|
||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_integer_value<T> Call(KernelContext*, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| static_assert(std::is_same<T, Arg0>::value && std::is_same<T, Arg1>::value, ""); | ||
| T result; | ||
| if (ARROW_PREDICT_FALSE(ModuloWithOverflow(left, right, &result))) { | ||
| if (right == 0) { | ||
| *st = Status::Invalid("divide by zero"); | ||
| } else { | ||
| *st = Status::Invalid("overflow"); | ||
| } | ||
| return result; | ||
| } | ||
| return AdjustRemainderToFloored(result, right); | ||
| } | ||
|
|
||
| template <typename T, typename Arg0, typename Arg1> | ||
| static enable_if_decimal_value<T> Call(KernelContext* ctx, Arg0 left, Arg1 right, | ||
| Status* st) { | ||
| return Modulo::Call<T>(ctx, left, right, st); | ||
| } | ||
| }; | ||
|
|
||
| struct Negate { | ||
| template <typename T, typename Arg> | ||
| static constexpr enable_if_floating_value<T> Call(KernelContext*, Arg arg, Status*) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.