[cairo] Re: Some other bits...

Kristian Høgsberg krh at bitplanet.net
Thu Aug 18 15:12:50 PDT 2005


Bertram Felgenhauer wrote:
> On 8/17/05, Bill Spitzak <spitzak at d2.com> wrote:
> 
>>Bertram Felgenhauer wrote:
>>
>>>A problem here is that the C standard does not define the
>>>result of >> when applied to negative numbers, making this
>>>optimization unportable. It's certainly a good optimization
>>>when it works though.
>>
>>But then _integer_part would not work for negative numbers either, it
>>would have to be implemented like _integer_floor above. And all the
>>other code that uses >> would fail on negative numbers and have to be
>>rewritten as well.
> 
> 
> That's true. I expect this works on all architectures that provide a signed
> shift right operation - compiler writers would be stupid not to use it -
> and I'm not aware of any architecture that doesn't.
> I think this explains why _integer_floor was written the way it was though.

There's a standard trick for this, though.  If you want to guarantee 
arithmetic (signed) right shift, you can say:

	a = (b >> shift) | ~(-1 >> shift);

On architectures that implement >> as arithmetic right shift, the last 
part of the expression, ~(-1 >> shift), should optimize to 0 and not 
cause any overhead.  On architectures that implement >> as logical 
shift, the ~(-1 >> shift) evaluates to exactly the missing bits.

It does feel a bit like a theoretical exercise, though.

Kristian


More information about the cairo mailing list