Skip to content

[flang][runtime] Add limit check to MOD/MODULO #80026

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

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions flang/runtime/numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,19 @@ inline RT_API_ATTRS T RealMod(
} else if (std::isinf(p)) {
return a;
}
if (auto aInt{static_cast<std::int64_t>(a)}; a == aInt) {
if (auto pInt{static_cast<std::int64_t>(p)}; p == pInt) {
// Fast exact case for integer operands
auto mod{aInt - (aInt / pInt) * pInt};
if (IS_MODULO && (aInt > 0) != (pInt > 0)) {
mod += pInt;
T aAbs{std::abs(a)};
T pAbs{std::abs(p)};
if (aAbs <= static_cast<T>(std::numeric_limits<std::int64_t>::max()) &&
pAbs <= static_cast<T>(std::numeric_limits<std::int64_t>::max())) {
if (auto aInt{static_cast<std::int64_t>(a)}; a == aInt) {
if (auto pInt{static_cast<std::int64_t>(p)}; p == pInt) {
// Fast exact case for integer operands
auto mod{aInt - (aInt / pInt) * pInt};
if (IS_MODULO && (aInt > 0) != (pInt > 0)) {
mod += pInt;
}
return static_cast<T>(mod);
}
return static_cast<T>(mod);
}
}
if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double> ||
Expand Down Expand Up @@ -183,9 +188,8 @@ inline RT_API_ATTRS T RealMod(
// what's left is the desired remainder. This is basically
// the same algorithm as arbitrary precision binary long division,
// discarding the quotient.
T tmp{std::abs(a)};
T pAbs{std::abs(p)};
for (T adj{SetExponent(pAbs, Exponent<int>(tmp))}; tmp >= pAbs; adj /= 2) {
T tmp{aAbs};
for (T adj{SetExponent(pAbs, Exponent<int>(aAbs))}; tmp >= pAbs; adj /= 2) {
if (tmp >= adj) {
tmp -= adj;
if (tmp == 0) {
Expand Down