Skip to content

Commit 52f24ad

Browse files
committed
MathExtras: avoid unnecessarily widening types
Several multi-argument functions unnecessarily widen types beyond the argument types. Template'ize the functions, and use std::common_type_t to avoid this, hence optimizing the functions. A requirement of this patch is to change the overflow behavior of alignTo, to only overflow when the result is not representable in the return type.
1 parent ef01c75 commit 52f24ad

File tree

2 files changed

+103
-29
lines changed

2 files changed

+103
-29
lines changed

llvm/include/llvm/Support/MathExtras.h

Lines changed: 85 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
#include <type_traits>
2424

2525
namespace llvm {
26+
/// Some template parameter helpers to optimize for bitwidth, for functions that
27+
/// take multiple arguments.
28+
29+
// We can't verify signedness, since callers rely on implicit coercions to
30+
// signed/unsigned.
31+
template <typename T, typename U>
32+
using enableif_int =
33+
std::enable_if_t<std::is_integral_v<T> && std::is_integral_v<U>>;
34+
35+
// Use std::common_type_t to widen only up to the widest argument.
36+
template <typename T, typename U, typename = enableif_int<T, U>>
37+
using common_uint =
38+
std::common_type_t<std::make_unsigned_t<T>, std::make_unsigned_t<U>>;
39+
template <typename T, typename U, typename = enableif_int<T, U>>
40+
using common_sint =
41+
std::common_type_t<std::make_signed_t<T>, std::make_signed_t<U>>;
2642

2743
/// Mathematical constants.
2844
namespace numbers {
@@ -346,7 +362,8 @@ inline unsigned Log2_64_Ceil(uint64_t Value) {
346362

347363
/// A and B are either alignments or offsets. Return the minimum alignment that
348364
/// may be assumed after adding the two together.
349-
constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
365+
template <typename U, typename V, typename T = common_uint<U, V>>
366+
constexpr T MinAlign(U A, V B) {
350367
// The largest power of 2 that divides both A and B.
351368
//
352369
// Replace "-Value" by "1+~Value" in the following commented code to avoid
@@ -355,6 +372,11 @@ constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
355372
return (A | B) & (1 + ~(A | B));
356373
}
357374

375+
/// Fallback when arguments aren't integral.
376+
constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
377+
return (A | B) & (1 + ~(A | B));
378+
}
379+
358380
/// Returns the next power of two (in 64-bits) that is strictly greater than A.
359381
/// Returns zero on overflow.
360382
constexpr inline uint64_t NextPowerOf2(uint64_t A) {
@@ -375,7 +397,7 @@ inline uint64_t PowerOf2Ceil(uint64_t A) {
375397
return UINT64_C(1) << Log2_64_Ceil(A);
376398
}
377399

378-
/// Returns the next integer (mod 2**64) that is greater than or equal to
400+
/// Returns the next integer (mod 2**nbits) that is greater than or equal to
379401
/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
380402
///
381403
/// Examples:
@@ -386,19 +408,30 @@ inline uint64_t PowerOf2Ceil(uint64_t A) {
386408
/// alignTo(321, 255) = 510
387409
/// \endcode
388410
///
389-
/// May overflow.
390-
inline uint64_t alignTo(uint64_t Value, uint64_t Align) {
411+
/// Will overflow only if result is not representable in T.
412+
template <typename U, typename V, typename T = common_uint<U, V>>
413+
constexpr T alignTo(U Value, V Align) {
414+
assert(Align != 0u && "Align can't be 0.");
415+
T Bias = (Value != 0);
416+
T CeilDiv = (Value - Bias) / Align + Bias;
417+
return CeilDiv * Align;
418+
}
419+
420+
/// Fallback when arguments aren't integral.
421+
constexpr inline uint64_t alignTo(uint64_t Value, uint64_t Align) {
391422
assert(Align != 0u && "Align can't be 0.");
392-
return (Value + Align - 1) / Align * Align;
423+
uint64_t Bias = (Value != 0);
424+
uint64_t CeilDiv = (Value - Bias) / Align + Bias;
425+
return CeilDiv * Align;
393426
}
394427

395-
inline uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align) {
428+
constexpr inline uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align) {
396429
assert(Align != 0 && (Align & (Align - 1)) == 0 &&
397430
"Align must be a power of 2");
398431
// Replace unary minus to avoid compilation error on Windows:
399432
// "unary minus operator applied to unsigned type, result still unsigned"
400-
uint64_t negAlign = (~Align) + 1;
401-
return (Value + Align - 1) & negAlign;
433+
uint64_t NegAlign = (~Align) + 1;
434+
return (Value + Align - 1) & NegAlign;
402435
}
403436

404437
/// If non-zero \p Skew is specified, the return value will be a minimal integer
@@ -413,72 +446,97 @@ inline uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align) {
413446
/// alignTo(~0LL, 8, 3) = 3
414447
/// alignTo(321, 255, 42) = 552
415448
/// \endcode
416-
inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew) {
449+
///
450+
/// Will overflow only if result is not representable in T.
451+
template <typename U, typename V, typename W,
452+
typename T = common_uint<common_uint<U, V>, W>>
453+
constexpr T alignTo(U Value, V Align, W Skew) {
417454
assert(Align != 0u && "Align can't be 0.");
418455
Skew %= Align;
419456
return alignTo(Value - Skew, Align) + Skew;
420457
}
421458

422-
/// Returns the next integer (mod 2**64) that is greater than or equal to
459+
/// Returns the next integer (mod 2**nbits) that is greater than or equal to
423460
/// \p Value and is a multiple of \c Align. \c Align must be non-zero.
424-
template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
461+
///
462+
/// Will overflow only if result is not representable in T.
463+
template <auto Align, typename V, typename T = common_uint<decltype(Align), V>>
464+
constexpr T alignTo(V Value) {
425465
static_assert(Align != 0u, "Align must be non-zero");
426-
return (Value + Align - 1) / Align * Align;
466+
T Bias = (Value != 0);
467+
T CeilDiv = (Value - Bias) / Align + Bias;
468+
return CeilDiv * Align;
427469
}
428470

429471
/// Returns the integer ceil(Numerator / Denominator). Unsigned version.
430472
/// Guaranteed to never overflow.
431-
inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
473+
template <typename U, typename V, typename T = common_uint<U, V>>
474+
constexpr T divideCeil(U Numerator, V Denominator) {
475+
assert(Denominator && "Division by zero");
476+
T Bias = (Numerator != 0);
477+
return (Numerator - Bias) / Denominator + Bias;
478+
}
479+
480+
/// Fallback when arguments aren't integral.
481+
constexpr inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
432482
assert(Denominator && "Division by zero");
433483
uint64_t Bias = (Numerator != 0);
434484
return (Numerator - Bias) / Denominator + Bias;
435485
}
436486

437487
/// Returns the integer ceil(Numerator / Denominator). Signed version.
438488
/// Guaranteed to never overflow.
439-
inline int64_t divideCeilSigned(int64_t Numerator, int64_t Denominator) {
489+
template <typename U, typename V, typename T = common_sint<U, V>>
490+
constexpr T divideCeilSigned(U Numerator, V Denominator) {
440491
assert(Denominator && "Division by zero");
441492
if (!Numerator)
442493
return 0;
443494
// C's integer division rounds towards 0.
444-
int64_t Bias = (Denominator >= 0 ? 1 : -1);
495+
T Bias = Denominator >= 0 ? 1 : -1;
445496
bool SameSign = (Numerator >= 0) == (Denominator >= 0);
446497
return SameSign ? (Numerator - Bias) / Denominator + 1
447498
: Numerator / Denominator;
448499
}
449500

450501
/// Returns the integer floor(Numerator / Denominator). Signed version.
451502
/// Guaranteed to never overflow.
452-
inline int64_t divideFloorSigned(int64_t Numerator, int64_t Denominator) {
503+
template <typename U, typename V, typename T = common_sint<U, V>>
504+
constexpr T divideFloorSigned(U Numerator, V Denominator) {
453505
assert(Denominator && "Division by zero");
454506
if (!Numerator)
455507
return 0;
456508
// C's integer division rounds towards 0.
457-
int64_t Bias = Denominator >= 0 ? -1 : 1;
509+
T Bias = Denominator >= 0 ? -1 : 1;
458510
bool SameSign = (Numerator >= 0) == (Denominator >= 0);
459511
return SameSign ? Numerator / Denominator
460512
: (Numerator - Bias) / Denominator - 1;
461513
}
462514

463515
/// Returns the remainder of the Euclidean division of LHS by RHS. Result is
464516
/// always non-negative.
465-
inline int64_t mod(int64_t Numerator, int64_t Denominator) {
517+
template <typename U, typename V, typename T = common_sint<U, V>>
518+
constexpr T mod(U Numerator, V Denominator) {
466519
assert(Denominator >= 1 && "Mod by non-positive number");
467-
int64_t Mod = Numerator % Denominator;
520+
T Mod = Numerator % Denominator;
468521
return Mod < 0 ? Mod + Denominator : Mod;
469522
}
470523

471524
/// Returns (Numerator / Denominator) rounded by round-half-up. Guaranteed to
472525
/// never overflow.
473-
inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) {
526+
template <typename U, typename V, typename T = common_uint<U, V>>
527+
constexpr T divideNearest(U Numerator, V Denominator) {
474528
assert(Denominator && "Division by zero");
475-
uint64_t Mod = Numerator % Denominator;
476-
return (Numerator / Denominator) + (Mod > (Denominator - 1) / 2);
529+
T Mod = Numerator % Denominator;
530+
return (Numerator / Denominator) +
531+
(Mod > (static_cast<T>(Denominator) - 1) / 2);
477532
}
478533

479-
/// Returns the largest uint64_t less than or equal to \p Value and is
480-
/// \p Skew mod \p Align. \p Align must be non-zero
481-
inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
534+
/// Returns the largest unsigned integer less than or equal to \p Value and is
535+
/// \p Skew mod \p Align. \p Align must be non-zero. Guaranteed to never
536+
/// overflow.
537+
template <typename U, typename V, typename W = uint8_t,
538+
typename T = common_uint<common_uint<U, V>, W>>
539+
constexpr T alignDown(U Value, V Align, W Skew = 0) {
482540
assert(Align != 0u && "Align can't be 0.");
483541
Skew %= Align;
484542
return (Value - Skew) / Align * Align + Skew;
@@ -522,8 +580,8 @@ inline int64_t SignExtend64(uint64_t X, unsigned B) {
522580

523581
/// Subtract two unsigned integers, X and Y, of type T and return the absolute
524582
/// value of the result.
525-
template <typename T>
526-
std::enable_if_t<std::is_unsigned_v<T>, T> AbsoluteDifference(T X, T Y) {
583+
template <typename U, typename V, typename T = common_uint<U, V>>
584+
constexpr T AbsoluteDifference(U X, V Y) {
527585
return X > Y ? (X - Y) : (Y - X);
528586
}
529587

llvm/unittests/Support/MathExtrasTest.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,37 @@ TEST(MathExtras, AlignTo) {
189189
EXPECT_EQ(8u, alignTo(5, 8));
190190
EXPECT_EQ(24u, alignTo(17, 8));
191191
EXPECT_EQ(0u, alignTo(~0LL, 8));
192-
EXPECT_EQ(static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()) + 1,
193-
alignTo(std::numeric_limits<uint32_t>::max(), 2));
192+
EXPECT_EQ(8u, alignTo(5ULL, 8ULL));
193+
EXPECT_EQ(254u,
194+
alignTo(static_cast<uint8_t>(200), static_cast<uint8_t>(127)));
195+
196+
EXPECT_EQ(8u, alignTo<8>(5));
197+
EXPECT_EQ(24u, alignTo<8>(17));
198+
EXPECT_EQ(0u, alignTo<8>(~0LL));
199+
EXPECT_EQ(254u,
200+
alignTo<static_cast<uint8_t>(127)>(static_cast<uint8_t>(200)));
194201

195202
EXPECT_EQ(7u, alignTo(5, 8, 7));
196203
EXPECT_EQ(17u, alignTo(17, 8, 1));
197204
EXPECT_EQ(3u, alignTo(~0LL, 8, 3));
198205
EXPECT_EQ(552u, alignTo(321, 255, 42));
199206
EXPECT_EQ(std::numeric_limits<uint32_t>::max(),
200207
alignTo(std::numeric_limits<uint32_t>::max(), 2, 1));
208+
209+
// Overflow.
210+
EXPECT_EQ(0u, alignTo(static_cast<uint8_t>(200), static_cast<uint8_t>(128)));
211+
EXPECT_EQ(0u, alignTo<static_cast<uint8_t>(128)>(static_cast<uint8_t>(200)));
212+
EXPECT_EQ(0u, alignTo(static_cast<uint8_t>(200), static_cast<uint8_t>(128),
213+
static_cast<uint8_t>(0)));
214+
EXPECT_EQ(0u, alignTo(std::numeric_limits<uint32_t>::max(), 2));
201215
}
202216

203217
TEST(MathExtras, AlignToPowerOf2) {
218+
EXPECT_EQ(0u, alignToPowerOf2(0u, 8));
204219
EXPECT_EQ(8u, alignToPowerOf2(5, 8));
205220
EXPECT_EQ(24u, alignToPowerOf2(17, 8));
206221
EXPECT_EQ(0u, alignToPowerOf2(~0LL, 8));
222+
EXPECT_EQ(240u, alignToPowerOf2(240, 16));
207223
EXPECT_EQ(static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()) + 1,
208224
alignToPowerOf2(std::numeric_limits<uint32_t>::max(), 2));
209225
}

0 commit comments

Comments
 (0)