[cairo] Transform+Repeat patch for libpixman

Bill Spitzak spitzak at d2.com
Tue Oct 12 10:37:27 PDT 2004

On Tuesday 12 October 2004 08:21 am, Carl Worth wrote:
> With C99, integer / and % are well-defined. There, / rounds toward 0 and
> % is defined such that, (assuming a/b is representable):
>         (a/b)*b + a%b == a
> Which means that even then, we have to do the fiddling to get the mod we
> want. And it seems to me we should be able to come up with something a
> bit tighter than what we have above.
> Any suggestions?

If we assumme C99 behavior and b is > 0 then:

#define mod(a,b) ((a)<0 ? (b)+((a)+1)%(b)-1 : (a)%(b))

Also in graphics there are lots of divide-by-2, I very much recommend that >> 
be used for this because it also will obey modulus arithmetic and round 
toward negative. To find the offset to center a in b, use ((b-a)>>1), 
otherwise a will shift by one pixel when it grows larger than b, an extremely 
annoying effect.

Of course it would help if back in the 50's the people who designed Fortran 
had defined these to do correct modulus arithemetic. I have never seen 
anybody actually rely on the current behavior of division or %, except to 
write macros like the above, which unfortunately will break if the definition 
were ever fixed, so we are stuck...

More information about the cairo mailing list