<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 17, 2015 at 4:52 PM, Nathan Slingerland via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: slingn<br>
Date: Tue Nov 17 18:52:43 2015<br>
New Revision: 253412<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=253412&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=253412&view=rev</a><br>
Log:<br>
[llvm-profdata] Add SaturatingAdd/SaturatingMultiply Helper Functions<br>
<br>
Summary:<br>
This change adds MathExtras helper functions for handling unsigned, saturating addition and multiplication. It also updates the instrumentation and sample profile merge implementations to use them.<br>
<br>
No functional changes.<br>
<br>
Reviewers: dnovillo, bogner, davidxl<br>
<br>
Subscribers: davidxl, llvm-commits<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D14720" rel="noreferrer" target="_blank">http://reviews.llvm.org/D14720</a><br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/ProfileData/InstrProf.h<br>
    llvm/trunk/include/llvm/ProfileData/SampleProf.h<br>
    llvm/trunk/include/llvm/Support/MathExtras.h<br>
    llvm/trunk/unittests/Support/MathExtrasTest.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ProfileData/InstrProf.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/InstrProf.h?rev=253412&r1=253411&r2=253412&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/InstrProf.h?rev=253412&r1=253411&r2=253412&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ProfileData/InstrProf.h (original)<br>
+++ llvm/trunk/include/llvm/ProfileData/InstrProf.h Tue Nov 17 18:52:43 2015<br>
@@ -226,7 +226,7 @@ struct InstrProfValueSiteRecord {<br>
       while (I != IE && I->Value < J->Value)<br>
         ++I;<br>
       if (I != IE && I->Value == J->Value) {<br>
-        I->Count += J->Count;<br>
+        I->Count = SaturatingAdd(I->Count, J->Count);<br>
         ++I;<br>
         continue;<br>
       }<br>
<br>
Modified: llvm/trunk/include/llvm/ProfileData/SampleProf.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProf.h?rev=253412&r1=253411&r2=253412&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProf.h?rev=253412&r1=253411&r2=253412&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ProfileData/SampleProf.h (original)<br>
+++ llvm/trunk/include/llvm/ProfileData/SampleProf.h Tue Nov 17 18:52:43 2015<br>
@@ -173,10 +173,7 @@ public:<br>
   /// Sample counts accumulate using saturating arithmetic, to avoid wrapping<br>
   /// around unsigned integers.<br>
   void addSamples(uint64_t S) {<br>
-    if (NumSamples <= std::numeric_limits<uint64_t>::max() - S)<br>
-      NumSamples += S;<br>
-    else<br>
-      NumSamples = std::numeric_limits<uint64_t>::max();<br>
+    NumSamples = SaturatingAdd(NumSamples, S);<br>
   }<br>
<br>
   /// Add called function \p F with samples \p S.<br>
@@ -185,10 +182,7 @@ public:<br>
   /// around unsigned integers.<br>
   void addCalledTarget(StringRef F, uint64_t S) {<br>
     uint64_t &TargetSamples = CallTargets[F];<br>
-    if (TargetSamples <= std::numeric_limits<uint64_t>::max() - S)<br>
-      TargetSamples += S;<br>
-    else<br>
-      TargetSamples = std::numeric_limits<uint64_t>::max();<br>
+    TargetSamples = SaturatingAdd(TargetSamples, S);<br>
   }<br>
<br>
   /// Return true if this sample record contains function calls.<br>
<br>
Modified: llvm/trunk/include/llvm/Support/MathExtras.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=253412&r1=253411&r2=253412&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=253412&r1=253411&r2=253412&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)<br>
+++ llvm/trunk/include/llvm/Support/MathExtras.h Tue Nov 17 18:52:43 2015<br>
@@ -653,6 +653,32 @@ inline int64_t SignExtend64(uint64_t X,<br>
   return int64_t(X << (64 - B)) >> (64 - B);<br>
 }<br>
<br>
+/// \brief Add two unsigned integers, X and Y, of type T.<br>
+/// Clamp the result to the maximum representable value of T on overflow.<br>
+template <typename T><br>
+typename std::enable_if<std::is_unsigned<T>::value, T>::type<br>
+SaturatingAdd(T X, T Y) {<br>
+  // Hacker's Delight, p. 29<br>
+  T Z = X + Y;<br>
+  if (Z < X || Z < Y)<br>
+    return std::numeric_limits<T>::max();<br>
+  else<br>
+    return Z;<br>
+}<br>
+<br>
+/// \brief Multiply two unsigned integers, X and Y, of type T.<br>
+/// Clamp the result to the maximum representable value of T on overflow.<br>
+template <typename T><br>
+typename std::enable_if<std::is_unsigned<T>::value, T>::type<br>
+SaturatingMultiply(T X, T Y) {<br>
+  // Hacker's Delight, p. 30<br>
+  T Z = X * Y;<br>
+  if (Y != 0 && Z / Y != X)<br>
+    return std::numeric_limits<T>::max();<br>
+  else<br>
+    return Z;<br>
+}<br>
+<br>
 extern const float huge_valf;<br>
 } // End llvm namespace<br>
<br>
<br>
Modified: llvm/trunk/unittests/Support/MathExtrasTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/MathExtrasTest.cpp?rev=253412&r1=253411&r2=253412&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/MathExtrasTest.cpp?rev=253412&r1=253411&r2=253412&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/Support/MathExtrasTest.cpp (original)<br>
+++ llvm/trunk/unittests/Support/MathExtrasTest.cpp Tue Nov 17 18:52:43 2015<br>
@@ -190,4 +190,52 @@ TEST(MathExtras, RoundUpToAlignment) {<br>
   EXPECT_EQ(552u, RoundUpToAlignment(321, 255, 42));<br>
 }<br>
<br>
+template<typename T><br>
+void SaturatingAddTestHelper()<br>
+{<br>
+  EXPECT_EQ(static_cast<T>(3),<br>
+            SaturatingAdd(static_cast<T>(1), static_cast<T>(2)));<br>
+  EXPECT_EQ(std::numeric_limits<T>::max(),<br>
+            SaturatingAdd(std::numeric_limits<T>::max(), static_cast<T>(1)));<br>
+  EXPECT_EQ(std::numeric_limits<T>::max(),<br>
+            SaturatingAdd(static_cast<T>(1), std::numeric_limits<T>::max()));<br>
+  EXPECT_EQ(std::numeric_limits<T>::max(),<br>
+            SaturatingAdd(std::numeric_limits<T>::max(),<br>
+                          std::numeric_limits<T>::max()));<br>
+}<br>
+<br>
+TEST(MathExtras, SaturatingAdd) {<br>
+  SaturatingAddTestHelper<uint8_t>();<br>
+  SaturatingAddTestHelper<uint16_t>();<br>
+  SaturatingAddTestHelper<uint32_t>();<br>
+  SaturatingAddTestHelper<uint64_t>();<br>
+}<br>
+<br>
+template<typename T><br>
+void SaturatingMultiplyTestHelper()<br>
+{<br>
+  EXPECT_EQ(static_cast<T>(0),<br>
+            SaturatingMultiply(static_cast<T>(1), static_cast<T>(0)));<br>
+  EXPECT_EQ(static_cast<T>(0),<br>
+            SaturatingMultiply(static_cast<T>(0), static_cast<T>(1)));<br>
+  EXPECT_EQ(static_cast<T>(6),<br>
+            SaturatingMultiply(static_cast<T>(2), static_cast<T>(3)));<br>
+  EXPECT_EQ(std::numeric_limits<T>::max(),<br>
+            SaturatingMultiply(std::numeric_limits<T>::max(),<br>
+                               static_cast<T>(2)));<br>
+  EXPECT_EQ(std::numeric_limits<T>::max(),<br>
+            SaturatingMultiply(static_cast<T>(2),<br>
+                               std::numeric_limits<T>::max()));<br>
+  EXPECT_EQ(std::numeric_limits<T>::max(),<br>
+            SaturatingMultiply(std::numeric_limits<T>::max(),<br>
+                          std::numeric_limits<T>::max()));<br></blockquote><div><br></div><div>I think adding a helper:</div><div><br></div><div>T Max = std::numeric_limits<T>::max();</div><div><br></div><div>would help clean these up. Also, I think instead of `static_cast<T>(1)` you can do just `T(1)`.</div><div><br></div><div>So for example that second-to-last one becomes:<br></div><div><br></div><div>EXPECT_EQ(Max, SaturatingMultiply(T(2), Max))</div><div><br></div><div>which seems like a nice improvement.</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
+}<br>
+<br>
+TEST(MathExtras, SaturatingMultiply) {<br>
+  SaturatingMultiplyTestHelper<uint8_t>();<br>
+  SaturatingMultiplyTestHelper<uint16_t>();<br>
+  SaturatingMultiplyTestHelper<uint32_t>();<br>
+  SaturatingMultiplyTestHelper<uint64_t>();<br>
+}<br>
+<br>
 }<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>