X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FMathExtras.h;h=8111aeebe6ee228b989c45de86a3437361e6a1c3;hp=0d0a2efa7ad24e4ab6a4ed6741e44437f9b3abe5;hb=09a7daad01b02482a58fb2ad6972e80871040c84;hpb=cf1e58c00294ee005b4b5269a75de4c80096ba50 diff --git a/include/llvm/Support/MathExtras.h b/include/llvm/Support/MathExtras.h index 0d0a2efa7ad..8111aeebe6e 100644 --- a/include/llvm/Support/MathExtras.h +++ b/include/llvm/Support/MathExtras.h @@ -659,38 +659,34 @@ inline int64_t SignExtend64(uint64_t X, unsigned B) { /// representable value of type T. template typename std::enable_if::value, T>::type -SaturatingAdd(T X, T Y, bool &ResultOverflowed) { +SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) { + bool Dummy; + bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; // Hacker's Delight, p. 29 T Z = X + Y; - ResultOverflowed = (Z < X || Z < Y); - if (ResultOverflowed) + Overflowed = (Z < X || Z < Y); + if (Overflowed) return std::numeric_limits::max(); else return Z; } -/// \brief Add two unsigned integers, X and Y, of type T. -/// Clamp the result to the maximum representable value of T on overflow. -template -typename std::enable_if::value, T>::type -SaturatingAdd(T X, T Y) { - bool ResultOverflowed; - return SaturatingAdd(X, Y, ResultOverflowed); -} - /// \brief Multiply two unsigned integers, X and Y, of type T. /// Clamp the result to the maximum representable value of T on overflow. /// ResultOverflowed indicates if the result is larger than the maximum /// representable value of type T. template typename std::enable_if::value, T>::type -SaturatingMultiply(T X, T Y, bool &ResultOverflowed) { +SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) { + bool Dummy; + bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; + // Hacker's Delight, p. 30 has a different algorithm, but we don't use that // because it fails for uint16_t (where multiplication can have undefined // behavior due to promotion to int), and requires a division in addition // to the multiplication. - ResultOverflowed = false; + Overflowed = false; // Log2(Z) would be either Log2Z or Log2Z + 1. // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z @@ -702,7 +698,7 @@ SaturatingMultiply(T X, T Y, bool &ResultOverflowed) { return X * Y; } if (Log2Z > Log2Max) { - ResultOverflowed = true; + Overflowed = true; return Max; } @@ -711,7 +707,7 @@ SaturatingMultiply(T X, T Y, bool &ResultOverflowed) { // that on at the end. T Z = (X >> 1) * Y; if (Z & ~(Max >> 1)) { - ResultOverflowed = true; + Overflowed = true; return Max; } Z <<= 1; @@ -721,15 +717,6 @@ SaturatingMultiply(T X, T Y, bool &ResultOverflowed) { return Z; } -/// \brief Multiply two unsigned integers, X and Y, of type T. -/// Clamp the result to the maximum representable value of T on overflow. -template -typename std::enable_if::value, T>::type -SaturatingMultiply(T X, T Y) { - bool ResultOverflowed; - return SaturatingMultiply(X, Y, ResultOverflowed); -} - extern const float huge_valf; } // End llvm namespace