diff --git a/flang/include/flang/Common/uint128.h b/flang/include/flang/Common/uint128.h --- a/flang/include/flang/Common/uint128.h +++ b/flang/include/flang/Common/uint128.h @@ -20,6 +20,7 @@ #endif #include "leading-zero-bit-count.h" +#include #include #include @@ -152,6 +153,13 @@ } constexpr Int128 operator/(Int128 that) const { + assert(that != 0 && "The divisor cannot be 0!"); + if (high_ == 0 && that.high_ == 0) { + return low_ / that.low_; + } + if (*this < that) { + return 0; + } int j{LeadingZeroes()}; Int128 bits{*this}; bits <<= j; @@ -165,7 +173,7 @@ bits <<= 1; quotient <<= 1; if (numerator >= that) { - ++quotient; + quotient |= 1; numerator -= that; } } @@ -173,6 +181,13 @@ } constexpr Int128 operator%(Int128 that) const { + assert(that != 0 && "The divisor cannot be 0!"); + if (high_ == 0 && that.high_ == 0) { + return low_ % that.low_; + } + if (*this < that) { + return *this; + } int j{LeadingZeroes()}; Int128 bits{*this}; bits <<= j;