aboutsummaryrefslogtreecommitdiff
path: root/llvm
diff options
context:
space:
mode:
authorRamkumar Ramachandra <ramkumar.ramachandra@codasip.com>2024-06-15 10:22:53 +0100
committerGitHub <noreply@github.com>2024-06-15 10:22:53 +0100
commitbfd95a003139a8f930874b2234c3cab545d504a1 (patch)
tree79e08321585afcfe2968510ed940c35f3ce55a07 /llvm
parent23c1b488fee99ac598203b6c3972be3b404dfbbe (diff)
downloadllvm-bfd95a003139a8f930874b2234c3cab545d504a1.zip
llvm-bfd95a003139a8f930874b2234c3cab545d504a1.tar.gz
llvm-bfd95a003139a8f930874b2234c3cab545d504a1.tar.bz2
MathExtras: rewrite some methods to never overflow (#95556)
Rewrite divideCeil, divideNearest, divideFloorSigned, and divideCeilSigned to never overflow.
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/Support/MathExtras.h34
-rw-r--r--llvm/unittests/Support/MathExtrasTest.cpp38
2 files changed, 58 insertions, 14 deletions
diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 05d87e1..5bcefe4 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -385,6 +385,8 @@ inline uint64_t PowerOf2Ceil(uint64_t A) {
/// alignTo(~0LL, 8) = 0
/// alignTo(321, 255) = 510
/// \endcode
+///
+/// May overflow.
inline uint64_t alignTo(uint64_t Value, uint64_t Align) {
assert(Align != 0u && "Align can't be 0.");
return (Value + Align - 1) / Align * Align;
@@ -424,33 +426,38 @@ template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
return (Value + Align - 1) / Align * Align;
}
-/// Returns the integer ceil(Numerator / Denominator). Unsigned integer version.
+/// Returns the integer ceil(Numerator / Denominator). Unsigned version.
+/// Guaranteed to never overflow.
inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
- return alignTo(Numerator, Denominator) / Denominator;
+ assert(Denominator && "Division by zero");
+ uint64_t Bias = (Numerator != 0);
+ return (Numerator - Bias) / Denominator + Bias;
}
-/// Returns the integer ceil(Numerator / Denominator). Signed integer version.
+/// Returns the integer ceil(Numerator / Denominator). Signed version.
+/// Guaranteed to never overflow.
inline int64_t divideCeilSigned(int64_t Numerator, int64_t Denominator) {
assert(Denominator && "Division by zero");
if (!Numerator)
return 0;
// C's integer division rounds towards 0.
- int64_t X = (Denominator > 0) ? -1 : 1;
- bool SameSign = (Numerator > 0) == (Denominator > 0);
- return SameSign ? ((Numerator + X) / Denominator) + 1
+ int64_t Bias = (Denominator >= 0 ? 1 : -1);
+ bool SameSign = (Numerator >= 0) == (Denominator >= 0);
+ return SameSign ? (Numerator - Bias) / Denominator + 1
: Numerator / Denominator;
}
-/// Returns the integer floor(Numerator / Denominator). Signed integer version.
+/// Returns the integer floor(Numerator / Denominator). Signed version.
+/// Guaranteed to never overflow.
inline int64_t divideFloorSigned(int64_t Numerator, int64_t Denominator) {
assert(Denominator && "Division by zero");
if (!Numerator)
return 0;
// C's integer division rounds towards 0.
- int64_t X = (Denominator > 0) ? -1 : 1;
- bool SameSign = (Numerator > 0) == (Denominator > 0);
+ int64_t Bias = Denominator >= 0 ? -1 : 1;
+ bool SameSign = (Numerator >= 0) == (Denominator >= 0);
return SameSign ? Numerator / Denominator
- : -((-Numerator + X) / Denominator) - 1;
+ : (Numerator - Bias) / Denominator - 1;
}
/// Returns the remainder of the Euclidean division of LHS by RHS. Result is
@@ -461,9 +468,12 @@ inline int64_t mod(int64_t Numerator, int64_t Denominator) {
return Mod < 0 ? Mod + Denominator : Mod;
}
-/// Returns the integer nearest(Numerator / Denominator).
+/// Returns (Numerator / Denominator) rounded by round-half-up. Guaranteed to
+/// never overflow.
inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) {
- return (Numerator + (Denominator / 2)) / Denominator;
+ assert(Denominator && "Division by zero");
+ uint64_t Mod = Numerator % Denominator;
+ return (Numerator / Denominator) + (Mod > (Denominator - 1) / 2);
}
/// Returns the largest uint64_t less than or equal to \p Value and is
diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp
index bcccb96..bd09bab 100644
--- a/llvm/unittests/Support/MathExtrasTest.cpp
+++ b/llvm/unittests/Support/MathExtrasTest.cpp
@@ -459,15 +459,37 @@ TEST(MathExtras, DivideNearest) {
EXPECT_EQ(divideNearest(14, 3), 5u);
EXPECT_EQ(divideNearest(15, 3), 5u);
EXPECT_EQ(divideNearest(0, 3), 0u);
+ EXPECT_EQ(divideNearest(5, 4), 1u);
+ EXPECT_EQ(divideNearest(6, 4), 2u);
+ EXPECT_EQ(divideNearest(3, 1), 3u);
+ EXPECT_EQ(divideNearest(3, 6), 1u);
+ EXPECT_EQ(divideNearest(3, 7), 0u);
EXPECT_EQ(divideNearest(std::numeric_limits<uint32_t>::max(), 2),
- 2147483648u);
+ std::numeric_limits<uint32_t>::max() / 2 + 1);
+ EXPECT_EQ(divideNearest(std::numeric_limits<uint64_t>::max(), 2),
+ std::numeric_limits<uint64_t>::max() / 2 + 1);
+ EXPECT_EQ(divideNearest(std::numeric_limits<uint64_t>::max(), 1),
+ std::numeric_limits<uint64_t>::max());
+ EXPECT_EQ(divideNearest(std::numeric_limits<uint64_t>::max() - 1,
+ std::numeric_limits<uint64_t>::max()),
+ 1u);
}
TEST(MathExtras, DivideCeil) {
EXPECT_EQ(divideCeil(14, 3), 5u);
EXPECT_EQ(divideCeil(15, 3), 5u);
EXPECT_EQ(divideCeil(0, 3), 0u);
- EXPECT_EQ(divideCeil(std::numeric_limits<uint32_t>::max(), 2), 2147483648u);
+ EXPECT_EQ(divideCeil(5, 4), 2u);
+ EXPECT_EQ(divideCeil(6, 4), 2u);
+ EXPECT_EQ(divideCeil(3, 1), 3u);
+ EXPECT_EQ(divideCeil(3, 6), 1u);
+ EXPECT_EQ(divideCeil(3, 7), 1u);
+ EXPECT_EQ(divideCeil(std::numeric_limits<uint32_t>::max(), 2),
+ std::numeric_limits<uint32_t>::max() / 2 + 1);
+ EXPECT_EQ(divideCeil(std::numeric_limits<uint64_t>::max(), 2),
+ std::numeric_limits<uint64_t>::max() / 2 + 1);
+ EXPECT_EQ(divideCeil(std::numeric_limits<uint64_t>::max(), 1),
+ std::numeric_limits<uint64_t>::max());
EXPECT_EQ(divideCeilSigned(14, 3), 5);
EXPECT_EQ(divideCeilSigned(15, 3), 5);
@@ -479,8 +501,14 @@ TEST(MathExtras, DivideCeil) {
EXPECT_EQ(divideCeilSigned(0, -3), 0);
EXPECT_EQ(divideCeilSigned(std::numeric_limits<int32_t>::max(), 2),
std::numeric_limits<int32_t>::max() / 2 + 1);
+ EXPECT_EQ(divideCeilSigned(std::numeric_limits<int64_t>::max(), 2),
+ std::numeric_limits<int64_t>::max() / 2 + 1);
EXPECT_EQ(divideCeilSigned(std::numeric_limits<int32_t>::max(), -2),
std::numeric_limits<int32_t>::min() / 2 + 1);
+ EXPECT_EQ(divideCeilSigned(std::numeric_limits<int64_t>::max(), -2),
+ std::numeric_limits<int64_t>::min() / 2 + 1);
+ EXPECT_EQ(divideCeilSigned(std::numeric_limits<int64_t>::min(), 1),
+ std::numeric_limits<int64_t>::min());
}
TEST(MathExtras, DivideFloorSigned) {
@@ -494,8 +522,14 @@ TEST(MathExtras, DivideFloorSigned) {
EXPECT_EQ(divideFloorSigned(0, -3), 0);
EXPECT_EQ(divideFloorSigned(std::numeric_limits<int32_t>::max(), 2),
std::numeric_limits<int32_t>::max() / 2);
+ EXPECT_EQ(divideFloorSigned(std::numeric_limits<int64_t>::max(), 2),
+ std::numeric_limits<int64_t>::max() / 2);
EXPECT_EQ(divideFloorSigned(std::numeric_limits<int32_t>::max(), -2),
std::numeric_limits<int32_t>::min() / 2);
+ EXPECT_EQ(divideFloorSigned(std::numeric_limits<int64_t>::max(), -2),
+ std::numeric_limits<int64_t>::min() / 2);
+ EXPECT_EQ(divideFloorSigned(std::numeric_limits<int64_t>::min(), 1),
+ std::numeric_limits<int64_t>::min());
}
TEST(MathExtras, Mod) {