[cairo] Re: Some other bits...

Bill Spitzak spitzak at d2.com
Wed Aug 17 12:16:26 PDT 2005



Bertram Felgenhauer wrote:
> On 8/17/05, Bill Spitzak <spitzak at d2.com> wrote:
> 
>>int
>>  _cairo_fixed_integer_part (cairo_fixed_t f)
>>  {
>>      return f >> CAIRO_FIXED_FLOAT_BITS;
>>  }
>>
>>  int
>>  _cairo_fixed_integer_floor (cairo_fixed_t f)
>>  {
>>      if (f >= 0)
>>          return f >> CAIRO_FIXED_FLOAT_BITS;
>>      else
>>          return -((-f - 1) >> CAIRO_FIXED_FLOAT_BITS) - 1;
>>  }
>>
>>These functions return identical values! I.e. integer_part() is
>>returning floor(), in a much more efficient way.
> 
> 
> 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.

In fact I'm fairly certain that this assumption happens tens of 
thousands of times in Cairo and X and every program that will use Cairo, 
thus trying to fix it is futile and serves only to slow programs down 
and make the code hard to read.



More information about the cairo mailing list