[cairo] Some other bits...

Behdad Esfahbod behdad at cs.toronto.edu
Tue Aug 16 21:24:37 PDT 2005


Hi,

In an attempt to figure out how much we depend on the 16.16
split, I had a jouney in the code that produced some bits.

The first part of the patch simply replaces the magic number 16
with CAIRO_FIXED_FLOAT_BITS in cairo-fixed.c.  Note that this
doesn't include the tiny bug in _cairo_fixed_integer_ceil() that
I reported a few hours ago.

Then in cairo-pattern.c, there are a couple of calls to
cairo_fixed_*_double instead of hardcoding the conversion.

Following that in cairo-pattern.c is a small change that improves
readability.  It's just replacing (three times)

  x -= x & 0xffff0000;

by

  x &= 0xffff;

which are equivalent.


In cairo-traps.c, a couple cairo_fixed_16_16_t's are replaced by
cairo_fixed_t.  There may have been a reason for defining them
that way though.

Then I have added some casts that actually fix a bug.  The bug is
that subtracting to cairo_fixed_t then assigning to
cairo_fixed_48_16_t is broken.  The subtraction should be done
after cast.

Finally, it adds CAIRO_BEGIN_CDECLS to cairoint.h, just in case
somebody decides to compile Cairo using a C++ compiler, but it
should work without them too.

[The patch was produced from inside src/]


Cheers

--behdad
http://behdad.org/
-------------- next part --------------
Index: cairo-fixed.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-fixed.c,v
retrieving revision 1.10
diff -u -p -r1.10 cairo-fixed.c
--- cairo-fixed.c	22 Feb 2005 19:35:03 -0000	1.10
+++ cairo-fixed.c	17 Aug 2005 04:11:31 -0000
@@ -36,6 +36,11 @@
 
 #include "cairoint.h"
 
+#define CAIRO_FIXED_FLOAT_BITS 16
+
+#define CAIRO_FIXED_SCALE ((double) (1 << CAIRO_FIXED_FLOAT_BITS))
+
+
 cairo_fixed_t
 _cairo_fixed_from_int (int i)
 {
@@ -45,47 +50,47 @@ _cairo_fixed_from_int (int i)
 cairo_fixed_t
 _cairo_fixed_from_double (double d)
 {
-    return (cairo_fixed_t) (d * 65536);
+    return (cairo_fixed_t) (d * CAIRO_FIXED_SCALE);
 }
 
 cairo_fixed_t
 _cairo_fixed_from_26_6 (uint32_t i)
 {
-    return i << 10;
+    return i << (CAIRO_FIXED_FLOAT_BITS - 10);
 }
 
 double
 _cairo_fixed_to_double (cairo_fixed_t f)
 {
-    return ((double) f) / 65536.0;
+    return ((double) f) / CAIRO_FIXED_SCALE;
 }
 
 int
 _cairo_fixed_is_integer (cairo_fixed_t f)
 {
-    return (f & 0xFFFF) == 0;
+    return (f & ((1 << CAIRO_FIXED_FLOAT_BITS) - 1)) == 0;
 }
 
 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;
 }
 
 int
 _cairo_fixed_integer_ceil (cairo_fixed_t f)
 {
-    if (f >= 0)
-	return ((f - 1)>>16) + 1;
+    if (f > 0)
+	return ((f - 1)>>CAIRO_FIXED_FLOAT_BITS) + 1;
     else
-	return - (-f >> 16);
+	return - (-f >> CAIRO_FIXED_FLOAT_BITS);
 }
Index: cairo-pattern.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-pattern.c,v
retrieving revision 1.55
diff -u -p -r1.55 cairo-pattern.c
--- cairo-pattern.c	13 Aug 2005 11:22:46 -0000	1.55
+++ cairo-pattern.c	17 Aug 2005 03:55:32 -0000
@@ -822,9 +822,9 @@ _cairo_pattern_shader_gaussian (unsigned
 				cairo_fixed_t factor,
 				uint32_t      *pixel)
 {
-    double f = ((double) factor) / 65536.0;
+    double f = _cairo_fixed_to_double (factor);
     
-    factor = (cairo_fixed_t) (((exp (f * f) - 1.0) / E_MINUS_ONE) * 65536);
+    factor = _cairo_fixed_from_double ((exp (f * f) - 1.0) / E_MINUS_ONE);
     
     *pixel = ((INTERPOLATE_COLOR_LINEAR (color0[3], color1[3], factor) << 24) |
 	      (INTERPOLATE_COLOR_LINEAR (color0[0], color1[0], factor) << 16) |
@@ -958,14 +958,14 @@ _cairo_pattern_calc_color_at_pixel (cair
 
     switch (op->extend) {
     case CAIRO_EXTEND_REPEAT:
-	factor -= factor & 0xffff0000;
+	factor &= 0xffff;
 	break;
     case CAIRO_EXTEND_REFLECT:
 	if (factor < 0 || factor > 65536) {
 	    if ((factor >> 16) % 2)
-		factor = 65536 - (factor - (factor & 0xffff0000));
+		factor = 65536 - (factor & 0xffff);
 	    else
-		factor -= factor & 0xffff0000;
+		factor &= 0xffff;
 	}
 	break;
     case CAIRO_EXTEND_NONE:
Index: cairo-traps.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-traps.c,v
retrieving revision 1.29
diff -u -p -r1.29 cairo-traps.c
--- cairo-traps.c	5 Aug 2005 05:45:59 -0000	1.29
+++ cairo-traps.c	17 Aug 2005 03:55:33 -0000
@@ -60,7 +60,7 @@ _compare_cairo_edge_by_top (const void *
 static int
 _compare_cairo_edge_by_slope (const void *av, const void *bv);
 
-static cairo_fixed_16_16_t
+static cairo_fixed_t
 _compute_x (cairo_line_t *line, cairo_fixed_t y);
 
 static int
@@ -263,7 +263,7 @@ _cairo_traps_tessellate_triangle (cairo_
 {
     cairo_status_t status;
     cairo_line_t line;
-    cairo_fixed_16_16_t intersect;
+    cairo_fixed_t intersect;
     cairo_point_t tsort[3];
 
     memcpy (tsort, t, 3 * sizeof (cairo_point_t));
@@ -378,10 +378,10 @@ _compare_cairo_edge_by_slope (const void
     const cairo_edge_t *a = av, *b = bv;
     cairo_fixed_32_32_t d;
 
-    cairo_fixed_48_16_t a_dx = a->edge.p2.x - a->edge.p1.x;
-    cairo_fixed_48_16_t a_dy = a->edge.p2.y - a->edge.p1.y;
-    cairo_fixed_48_16_t b_dx = b->edge.p2.x - b->edge.p1.x;
-    cairo_fixed_48_16_t b_dy = b->edge.p2.y - b->edge.p1.y;
+    cairo_fixed_48_16_t a_dx = (cairo_fixed_48_16_t) a->edge.p2.x - a->edge.p1.x;
+    cairo_fixed_48_16_t a_dy = (cairo_fixed_48_16_t) a->edge.p2.y - a->edge.p1.y;
+    cairo_fixed_48_16_t b_dx = (cairo_fixed_48_16_t) b->edge.p2.x - b->edge.p1.x;
+    cairo_fixed_48_16_t b_dy = (cairo_fixed_48_16_t) b->edge.p2.y - b->edge.p1.y;
 
     d = b_dy * a_dx - a_dy * b_dx;
 
Index: cairoint.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairoint.h,v
retrieving revision 1.196
diff -u -p -r1.196 cairoint.h
--- cairoint.h	14 Aug 2005 00:38:23 -0000	1.196
+++ cairoint.h	17 Aug 2005 03:55:34 -0000
@@ -46,6 +46,10 @@
 #ifndef _CAIROINT_H_
 #define _CAIROINT_H_
 
+#include <cairo-features.h>
+
+CAIRO_BEGIN_DECLS
+
 #if HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -2038,4 +2052,6 @@ slim_hidden_proto(cairo_save)
 slim_hidden_proto(cairo_stroke_preserve)
 slim_hidden_proto(cairo_surface_destroy)
 
+CAIRO_END_DECLS
+
 #endif


More information about the cairo mailing list