[llvm] r364599 - [NFC][APInt] Add (exhaustive) test for multiplicativeInverse()

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 27 14:51:55 PDT 2019


Author: lebedevri
Date: Thu Jun 27 14:51:54 2019
New Revision: 364599

URL: http://llvm.org/viewvc/llvm-project?rev=364599&view=rev
Log:
[NFC][APInt] Add (exhaustive) test for multiplicativeInverse()

Else there is no direct test coverage at all.
The function should either return '0' or precise answer.

Modified:
    llvm/trunk/unittests/ADT/APIntTest.cpp

Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=364599&r1=364598&r2=364599&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Thu Jun 27 14:51:54 2019
@@ -2505,4 +2505,21 @@ TEST(APIntTest, SolveQuadraticEquationWr
     Iterate(i);
 }
 
+TEST(APIntTest, MultiplicativeInverseExaustive) {
+  for (unsigned BitWidth = 1; BitWidth <= 16; ++BitWidth) {
+    for (unsigned Value = 0; Value < (1 << BitWidth); ++Value) {
+      APInt V = APInt(BitWidth, Value);
+      APInt MulInv =
+          V.zext(BitWidth + 1)
+              .multiplicativeInverse(APInt::getSignedMinValue(BitWidth + 1))
+              .trunc(BitWidth);
+      APInt One = V * MulInv;
+      EXPECT_TRUE(MulInv.isNullValue() || One.isOneValue())
+          << " bitwidth = " << BitWidth << ", value = " << Value
+          << ", computed multiplicative inverse = " << MulInv
+          << ", value * multiplicative inverse = " << One << " (should be 1)";
+    }
+  }
+}
+
 } // end anonymous namespace




More information about the llvm-commits mailing list