summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Avison <bavison@riscosopen.org>2015-08-14 14:54:03 +0100
committerBen Avison <bavison@riscosopen.org>2015-10-15 15:15:10 +0100
commitf709107ae2b731d23d594961cd4fac52d2df1c64 (patch)
treebcc29e770e7276ca80353736b937526849b812f0
parent70680f7c4f07e1b2c96a28be1f03be6a447d0b60 (diff)
Resolve implementation-defined behaviour for division rounded to -infinity
The previous implementations of DIV and MOD relied upon the built-in / and % operators performing round-to-zero. This is true for C99, but rounding is implementation-defined for C89 when divisor and/or dividend is negative, and I believe Pixman is still supposed to support C89.
-rw-r--r--pixman/pixman-private.h8
1 files changed, 4 insertions, 4 deletions
diff --git a/pixman/pixman-private.h b/pixman/pixman-private.h
index b933943..cd2d084 100644
--- a/pixman/pixman-private.h
+++ b/pixman/pixman-private.h
@@ -891,12 +891,12 @@ pixman_list_move_to_front (pixman_list_t *list, pixman_link_t *link)
#endif
/* Integer division that rounds towards -infinity */
-#define DIV(a, b) \
- ((((a) < 0) == ((b) < 0)) ? (a) / (b) : \
- ((a) - (b) + 1 - (((b) < 0) << 1)) / (b))
+#define DIV(a, b) \
+ ((a) / (b) - ((a) % (b) != 0 && ((a) % (b) < 0) != ((b) < 0) ? 1 : 0))
/* Modulus that produces the remainder wrt. DIV */
-#define MOD(a, b) ((a) < 0 ? ((b) - ((-(a) - 1) % (b))) - 1 : (a) % (b))
+#define MOD(a, b) \
+ ((a) % (b) + ((a) % (b) != 0 && ((a) % (b) < 0) != ((b) < 0) ? (b) : 0))
#define CLIP(v, low, high) ((v) < (low) ? (low) : ((v) > (high) ? (high) : (v)))