[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-mes] Simple division algorithm
From: |
Jan Nieuwenhuizen |
Subject: |
Re: [bug-mes] Simple division algorithm |
Date: |
Wed, 20 Feb 2019 19:14:04 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) |
Danny Milosavljevic writes:
> ARM doesn't have an integer division instruction.
>
> I suggest a function like this:
That looks fine.
> diff --git a/lib/mes/ntoab.c b/lib/mes/ntoab.c
> index 640fcc6a..55eee1be 100644
> --- a/lib/mes/ntoab.c
> +++ b/lib/mes/ntoab.c
> @@ -20,6 +20,30 @@
>
> #include <mes/lib.h>
>
> +static int __idiv(long a, long b, int* remainder)
> +{
> + *remainder = 0;
> + if (b == 1)
> + return a;
> + else if (b == 0)
> + return 42; /* FIXME how to abort */
What about (copied partly frcom mes/fdungetc, but adding abort (we
should do that in fdungetc too).
eputs (" ***MES C LIB*** divide by zero numerator=");
eputs (itoa (a));
eputs ("\n");
assert (0);
> + else
> + {
> + long x;
> + int negative_result = (a < 0) ^ (b < 0);
> + if (a < 0)
> + a = -a;
> + if (b < 0)
> + b = -b;
> + for (x = 0; a >= b; a -= b)
> + ++x;
> + *remainder = a;
> + if (negative_result)
> + x = -x;
> + return x;
> + }
> +}
> +
>
> Functions whose names start with "__" are reserved in C and can thus be used
> for "builtins".
>
> Where should we put the builtins?
What about lib/mes/__idiv.c and #include it in lib/libc.c (assuming
that is where we need it).
> Is the name OK?
Yes, I think so.
thanks!
janneke
--
Jan Nieuwenhuizen <address@hidden> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | AvatarĀ® http://AvatarAcademy.com