• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why is UInt32 << 32 a no-op?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why is UInt32 << 32 a no-op?


  • Subject: Re: Why is UInt32 << 32 a no-op?
  • From: Jocelyn Houle <email@hidden>
  • Date: Tue, 17 Nov 2009 12:19:21 -0500

The Intel instruction uses a 5-bit value to define the amount of shift, as spec'd here:
  http://www.intel.com/Assets/PDF/manual/253667.pdf

"The destination operand can be a register or a memory location. The count operand can be an immediate value or the CL register. The count is masked to 5 bits (or 6 bits if in 64-bit mode and REX.W is used). The count range is limited to 0 to 31 (or 63 if 64-bit mode and REX.W is used). A special opcode encoding is provided for a count of 1."

So, it's a hardware limitation... (an extra sticky bit could have handled all possible cases, I guess).


For your case, you can work around the problem in a number of ways.

Here is the code I've been using to generate bitmask of 0 to 32 bits, inclusive:
   inline uint32_t bitmask( uint32_t s )
   {
      uint32_t s_2 = (s >> 1);
      return (1<<s_2<<(s-s_2)) - 1;
   }

The code is an extension of a simpler:
  mask = (1 << s) - 1;
which works for 0 to 31 bits.

All I did was do 2 successive shifts of half the number of bits.  Keeps the code branchless.  Some might argue that using a LUT could be faster in certain circumstances, but I never needed to optimize such a code that much.

On 2009-11-17, at 11:56, Jens Alfke wrote:

> Surprisingly, the expression "n << 32", where n is a 32-bit integer, is a no-op. I would expect it to result in zero for all values of n.
>
> 	UInt32 n = (UInt32)-1L;
> 	int shift = 32;
> 	n = n << shift;
> 	assert(n==0);		// fails; n is actually unchanged
>
> This is breaking some code of mine that masks out the upper b bits of a number, where 0≤b≤8; it fails when b==0 because the mask it generates is all 1s instead of all 0s. I'm going to have to add a special case.
>
> Any C expert know whether this is in-spec or not?
>
> —Jens _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Why is UInt32 << 32 a no-op?
      • From: Alastair Houghton <email@hidden>
References: 
 >Why is UInt32 << 32 a no-op? (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Why is UInt32 << 32 a no-op?
  • Next by Date: Re: Why is UInt32 << 32 a no-op?
  • Previous by thread: Re: Why is UInt32 << 32 a no-op?
  • Next by thread: Re: Why is UInt32 << 32 a no-op?
  • Index(es):
    • Date
    • Thread