[cairo] Some other bits...

Behdad Esfahbod behdad at cs.toronto.edu
Wed Aug 17 10:39:36 PDT 2005


On Wed, 17 Aug 2005, Bill Spitzak wrote:

> Behdad Esfahbod wrote:
>
> int
>   _cairo_fixed_integer_part (cairo_fixed_t f)
>   {
> -    return f >> 16;
> +    return f >> CAIRO_FIXED_FLOAT_BITS;
>   }
>
>   int
>   _cairo_fixed_integer_floor (cairo_fixed_t f)
>   {
>       if (f >= 0)
> -	return f >> 16;
> +	return f >> CAIRO_FIXED_FLOAT_BITS;
>       else
> -	return -((-f - 1) >> 16) - 1;
> +	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.

Absolutely correct.

So the ceil function can be implemented as:

  return -((-f)>>16);

right?

behdad





> Test program that exits with zero:
>
> int main() {
>    int f; for (f = -1; f > -0x7fffffff; f--) {
>      int a,b;
>      a = f>>16;
>      b = -((-f-1)>>16)-1;
>      if (a != b) {printf("%x %x %x\n", f, a,b); return 1;}
>    }
>    return 0;
> }
>
> I recommend you make the functions identical.
>
> Rounding toward zero is complicated, but of absolutly no interest for
> graphics programs, so there is no reason to implement it.
>
>

--behdad
http://behdad.org/


More information about the cairo mailing list