llvm.org GIT mirror llvm / d9103df
Add an argument to APInt's magic udiv calculation to specify the number of bits that are known zero in the divided number. This will come in handy soon. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@127828 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 9 years ago
3 changed file(s) with 7 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
13711371
13721372 /// Calculate the magic number for unsigned division by a constant.
13731373 struct mu;
1374 mu magicu() const;
1374 mu magicu(unsigned LeadingZeros = 0) const;
13751375
13761376 /// @}
13771377 /// @name Building-block Operations for APInt and APFloat
15161516 /// division by a constant as a sequence of multiplies, adds and shifts.
15171517 /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry
15181518 /// S. Warren, Jr., chapter 10.
1519 APInt::mu APInt::magicu() const {
1519 /// LeadingZeros can be used to simplify the calculation if the upper bits
1520 /// of the devided value are known zero.
1521 APInt::mu APInt::magicu(unsigned LeadingZeros) const {
15201522 const APInt& d = *this;
15211523 unsigned p;
15221524 APInt nc, delta, q1, r1, q2, r2;
15231525 struct mu magu;
15241526 magu.a = 0; // initialize "add" indicator
1525 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth());
1527 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros);
15261528 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
15271529 APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
15281530
347347 EXPECT_EQ(APInt(32, 5).magicu().s, 2U);
348348 EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16));
349349 EXPECT_EQ(APInt(32, 7).magicu().s, 3U);
350 EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16));
351 EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U);
350352 }
351353
352354 #ifdef GTEST_HAS_DEATH_TEST