The signed version of our "magic number" computation for the integer approximation

of a constant had a minor typo introduced when copying it from the book, which
caused it to favor negative approximations over positive approximations in many
cases. Positive approximations require fewer operations beyond the multiplication.

In the case of division by 3, we still generate code that is a single instruction
larger than GCC's code.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126097 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Cameron Zwarich
2011-02-21 00:22:02 +00:00
parent 0b85d07d46
commit 8d7285d0e5
4 changed files with 24 additions and 8 deletions

View File

@@ -332,6 +332,24 @@ TEST(APIntTest, Log2) {
EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
}
TEST(APIntTest, magic) {
EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16));
EXPECT_EQ(APInt(32, 3).magic().s, 0U);
EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16));
EXPECT_EQ(APInt(32, 5).magic().s, 1U);
EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16));
EXPECT_EQ(APInt(32, 7).magic().s, 2U);
}
TEST(APIntTest, magicu) {
EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16));
EXPECT_EQ(APInt(32, 3).magicu().s, 1U);
EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16));
EXPECT_EQ(APInt(32, 5).magicu().s, 2U);
EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16));
EXPECT_EQ(APInt(32, 7).magicu().s, 3U);
}
#ifdef GTEST_HAS_DEATH_TEST
#ifndef NDEBUG
TEST(APIntTest, StringDeath) {