bug-mes
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-mes] Simple division algorithm


From: Danny Milosavljevic
Subject: [bug-mes] Simple division algorithm
Date: Wed, 20 Feb 2019 14:31:18 +0100

ARM doesn't have an integer division instruction.

I suggest a function like this:

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 */
+       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;
+       }
+}
+
 char *
 ntoab (long x, int base, int signed_p)
 {

Functions whose names start with "__" are reserved in C and can thus be used 
for "builtins".

Where should we put the builtins?

Is the name OK?

Attachment: pgpr2n4L2sa9f.pgp
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]