[llvm-commits] [llvm] r127828 - in /llvm/trunk: include/llvm/ADT/APInt.h lib/Support/APInt.cpp unittests/ADT/APIntTest.cpp

Eli Friedman eli.friedman at gmail.com
Fri Mar 18 01:03:37 PDT 2011


On Thu, Mar 17, 2011 at 1:39 PM, Benjamin Kramer
<benny.kra at googlemail.com> wrote:
> Author: d0k
> Date: Thu Mar 17 15:39:06 2011
> New Revision: 127828
>
> URL: http://llvm.org/viewvc/llvm-project?rev=127828&view=rev
> Log:
> 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.

Hmm... what exactly is the effect of using LeadingZeros as opposed to
truncating the input APInt?  As an experiment, I just tried making
BuildUDIV call ComputeMaskedBits to get some leading zeros; effect on
the following C code:

short a(unsigned short x) { return x / 28; }

ARM trunk (r127829):
	lsr	r1, r0, #2
	ldr	r2, .LCPI0_0
	umull	r3, r0, r1, r2
	bx	lr
@ BB#1:
	.align	2
.LCPI0_0:
	.long	613566757               @ 0x24924925

ARM patched with ComputeMaskedBits call:
	ldr	r1, .LCPI0_0
	umull	r2, r3, r0, r1
	mov	r0, r3
	bx	lr
@ BB#1:
	.align	2
.LCPI0_0:
	.long	153391690               @ 0x924924a

x86-64:
	shrl	$2, %edi
	imull	$18725, %edi, %eax      # imm = 0x4925
	shrl	$17, %eax
	ret

The kind of output on x86 (which differs because i16 is legal) seems
preferable, but I'm not sure how that generalizes.

-Eli

> Modified:
>    llvm/trunk/include/llvm/ADT/APInt.h
>    llvm/trunk/lib/Support/APInt.cpp
>    llvm/trunk/unittests/ADT/APIntTest.cpp
>
> Modified: llvm/trunk/include/llvm/ADT/APInt.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=127828&r1=127827&r2=127828&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/APInt.h (original)
> +++ llvm/trunk/include/llvm/ADT/APInt.h Thu Mar 17 15:39:06 2011
> @@ -1372,7 +1372,7 @@
>
>   /// Calculate the magic number for unsigned division by a constant.
>   struct mu;
> -  mu magicu() const;
> +  mu magicu(unsigned LeadingZeros = 0) const;
>
>   /// @}
>   /// @name Building-block Operations for APInt and APFloat
>
> Modified: llvm/trunk/lib/Support/APInt.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APInt.cpp?rev=127828&r1=127827&r2=127828&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Support/APInt.cpp (original)
> +++ llvm/trunk/lib/Support/APInt.cpp Thu Mar 17 15:39:06 2011
> @@ -1517,13 +1517,15 @@
>  /// division by a constant as a sequence of multiplies, adds and shifts.
>  /// Requires that the divisor not be 0.  Taken from "Hacker's Delight", Henry
>  /// S. Warren, Jr., chapter 10.
> -APInt::mu APInt::magicu() const {
> +/// LeadingZeros can be used to simplify the calculation if the upper bits
> +/// of the devided value are known zero.
> +APInt::mu APInt::magicu(unsigned LeadingZeros) const {
>   const APInt& d = *this;
>   unsigned p;
>   APInt nc, delta, q1, r1, q2, r2;
>   struct mu magu;
>   magu.a = 0;               // initialize "add" indicator
> -  APInt allOnes = APInt::getAllOnesValue(d.getBitWidth());
> +  APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros);
>   APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
>   APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
>
>
> Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=127828&r1=127827&r2=127828&view=diff
> ==============================================================================
> --- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
> +++ llvm/trunk/unittests/ADT/APIntTest.cpp Thu Mar 17 15:39:06 2011
> @@ -348,6 +348,8 @@
>   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);
> +  EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16));
> +  EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U);
>  }
>
>  #ifdef GTEST_HAS_DEATH_TEST
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>




More information about the llvm-commits mailing list