# [Pixman] [PATCH] Resolve implementation-defined behaviour for division rounded to -infinity

Bill Spitzak spitzak at gmail.com
Fri Aug 14 17:06:14 PDT 2015

```Can't this just assume/require b to be positive? That would make them a lot
simpler:

#define DIV(a,b) ((a) / (b) - ((a) % (b) < 0 ? (b) : 0))
#define MOD(a,b) ((a) % (b) + ((a) % (b) < 0 ? (b) : 0))

On Fri, Aug 14, 2015 at 7:06 AM, Ben Avison <bavison at riscosopen.org> wrote:

> 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.
> ---
>  pixman/pixman-private.h |    8 ++++----
>  1 files changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/pixman/pixman-private.h b/pixman/pixman-private.h
> index 73108a0..80506be 100644
> --- a/pixman/pixman-private.h
> +++ b/pixman/pixman-private.h
> @@ -889,12 +889,12 @@ pixman_list_move_to_front (pixman_list_t *list,
>  #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)))
>
> --
> 1.7.5.4
>
> _______________________________________________
> Pixman mailing list
> Pixman at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/pixman
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/pixman/attachments/20150814/57d5646a/attachment.html>
```