X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FMathExtras.h;h=b7b3c02dcfe76be2cf9572cb4308f4aed4dc9905;hp=2c515bbd242a13b725b37c5ad30ad4478a836418;hb=4918b66f8428f0d5b4559da8f966e3aa54c3b1ba;hpb=9eaef595284c567310bc6f0009f83d77a554b881;ds=sidebyside diff --git a/include/llvm/Support/MathExtras.h b/include/llvm/Support/MathExtras.h index 2c515bbd242..b7b3c02dcfe 100644 --- a/include/llvm/Support/MathExtras.h +++ b/include/llvm/Support/MathExtras.h @@ -653,6 +653,32 @@ inline int64_t SignExtend64(uint64_t X, unsigned B) { return int64_t(X << (64 - B)) >> (64 - B); } +/// \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) { + // Hacker's Delight, p. 29 + T Z = X + Y; + if (Z < X || Z < Y) + return std::numeric_limits::max(); + else + 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) { + // Hacker's Delight, p. 30 + T Z = X * Y; + if (Y != 0 && Z / Y != X) + return std::numeric_limits::max(); + else + return Z; +} + extern const float huge_valf; } // End llvm namespace