llvm.org GIT mirror llvm / 8d7285d
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). Raw diff Collapse all Expand all
15041504 r2 = r2 - ad;
15051505 }
15061506 delta = ad - r2;
1507 } while (q1.ule(delta) || (q1 == delta && r1 == 0));
1507 } while (q1.ult(delta) || (q1 == delta && r1 == 0));
15081508
15091509 mag.m = q2 + 1;
15101510 if (d.isNegative()) mag.m = -mag.m; // resulting magic number
18871887
18881888 _t: ## @t
18891889 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
18961894 addl %ecx, %eax
18971895 movsbl %al, %eax
18981896 ret
3939 %div = sdiv i16 %x, 33 ; [#uses=1]
4040 ret i16 %div
4141 ; CHECK: test4:
42 ; CHECK: imull $-1985, %ecx, %ecx
42 ; CHECK: imull $1986, %eax, %eax
4343 }
4444
4545 define i32 @test5(i32 %A) nounwind {
329329 EXPECT_EQ(APInt(15, 9).logBase2(), 3U);
330330 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U);
331331 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);
332350 }
333351
334352 #ifdef GTEST_HAS_DEATH_TEST