llvm.org GIT mirror
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 Cameron Zwarich 9 years ago
4 changed file(s) with 24 addition(s) and 8 deletion(s).
 1504 1504 r2 = r2 - ad; 1505 1505 } 1506 1506 delta = ad - r2; 1507 } while (q1.ule(delta) || (q1 == delta && r1 == 0));⏎ 1507 } while (q1.ult(delta) || (q1 == delta && r1 == 0));⏎ 1508 1508 1509 1509 mag.m = q2 + 1; 1510 1510 if (d.isNegative()) mag.m = -mag.m; // resulting magic number
 1887 1887 1888 1888 _t: ## @t 1889 1889 movslq %edi, %rax 1890 imulq \$-1431655765, %rax, %rcx ## imm = 0xFFFFFFFFAAAAAAAB 1891 shrq \$32, %rcx 1892 addl %ecx, %eax 1893 movl %eax, %ecx 1894 shrl \$31, %ecx 1895 shrl %eax⏎ 1890 imulq \$1431655766, %rax, %rax ## imm = 0x55555556⏎ 1891 movq %rax, %rcx 1892 shrq \$63, %rcx 1893 shrq \$32, %rax 1896 1894 addl %ecx, %eax 1897 1895 movsbl %al, %eax 1898 1896 ret
 39 39 %div = sdiv i16 %x, 33 ; [#uses=1] 40 40 ret i16 %div 41 41 ; CHECK: test4: 42 ; CHECK: imull \$-1985, %ecx, %ecx ⏎ 42 ; CHECK: imull \$1986, %eax, %eax ⏎ 43 43 } 44 44 45 45 define i32 @test5(i32 %A) nounwind {
 329 329 EXPECT_EQ(APInt(15, 9).logBase2(), 3U); 330 330 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U); 331 331 EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1); 332 } 333 334 TEST(APIntTest, magic) { 335 EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16)); 336 EXPECT_EQ(APInt(32, 3).magic().s, 0U); 337 EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16)); 338 EXPECT_EQ(APInt(32, 5).magic().s, 1U); 339 EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16)); 340 EXPECT_EQ(APInt(32, 7).magic().s, 2U); 341 } 342 343 TEST(APIntTest, magicu) { 344 EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16)); 345 EXPECT_EQ(APInt(32, 3).magicu().s, 1U); 346 EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16)); 347 EXPECT_EQ(APInt(32, 5).magicu().s, 2U); 348 EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16)); 349 EXPECT_EQ(APInt(32, 7).magicu().s, 3U); 332 350 } 333 351 334 352 #ifdef GTEST_HAS_DEATH_TEST