altos: Add 64-bit add/mul/shift for SDCC
authorKeith Packard <keithp@keithp.com>
Wed, 22 May 2013 21:32:50 +0000 (14:32 -0700)
committerKeith Packard <keithp@keithp.com>
Mon, 26 Aug 2013 05:24:01 +0000 (22:24 -0700)
SDCC doeesn't provide a native 64-bit type (sigh), so
implement the minimal operations necessary for the MS5607 conversion
routine.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/core/ao_int64.c [new file with mode: 0644]
src/core/ao_int64.h [new file with mode: 0644]
src/test/ao_int64_test.c [new file with mode: 0644]

diff --git a/src/core/ao_int64.c b/src/core/ao_int64.c
new file mode 100644 (file)
index 0000000..1fe717d
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * Copyright © 2013 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <ao_int64.h>
+
+void ao_plus64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
+       uint32_t        t;
+
+       r->low = t = a->low + b->low;
+       r->high = a->high + b->high;
+       if (t < a->low)
+               r->high++;
+}
+
+void ao_rshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d) {
+       if (d < 32) {
+               r->high = (int32_t) a->high >> d;
+               r->low = a->low >> d;
+               if (d)
+                       r->low |= a->high << (32 - d);
+       } else {
+               d &= 0x1f;
+               r->high = 0;
+               r->low = (int32_t) a->high >> d;
+       }
+}
+
+void ao_lshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d) {
+       if (d < 32) {
+               r->high = a->high << d;
+               r->low = a->low << d;
+               if (d)
+                       r->high |= a->low >> (32 - d);
+       } else {
+               d &= 0x1f;
+               r->low = 0;
+               r->high = a->low << d;
+       }
+}
+
+void ao_umul64(ao_int64_t *r, uint32_t a, uint32_t b)
+{
+       uint32_t        r1;
+       uint32_t        r2, r3, r4;
+       ao_int64_t      s,t,u,v;
+       r1 = (uint32_t) (uint16_t) a * (uint16_t) b;
+       r2 = (uint32_t) (uint16_t) (a >> 16) * (uint16_t) b;
+       r3 = (uint32_t) (uint16_t) a * (uint16_t) (b >> 16);
+       r4 = (uint32_t) (uint16_t) (a >> 16) * (uint16_t) (b >> 16);
+
+       s.low = r1;
+       s.high = r4;
+
+       t.high = (uint32_t) r2 >> 16;
+       t.low = r2 << 16;
+       ao_plus64(&u, &s, &t);
+
+       v.high = (int32_t) r3 >> 16;
+       v.low = r3 << 16;
+       ao_plus64(r, &u, &v);
+}
+
+void ao_neg64(ao_int64_t *r, ao_int64_t *a) {
+       r->high = ~a->high;
+       r->low = ~a->low;
+       if (!++r->low)
+               r->high++;
+}
+
+void ao_mul64(ao_int64_t *r, int32_t a, int32_t b) {
+       uint8_t         negative = 0;
+
+       if (a < 0) {
+               a = -a;
+               negative = 1;
+       }
+       if (b < 0) {
+               b = -b;
+               negative = !negative;
+       }
+       ao_umul64(r, a, b);
+       if (negative)
+               ao_neg64(r, r);
+}
+
+void ao_umul64_16(ao_int64_t *r, ao_int64_t *a, uint16_t b) {
+       uint32_t        low = a->low;
+       ao_umul64(r, (uint32_t) low >> 1, (uint32_t) b << 1);
+       if (low & 1) {
+               if ((uint32_t) (r->low += b) < (uint32_t) b)
+                       r->high++;
+       }
+       r->high += a->high * b;
+}
+
+void ao_mul64_16(ao_int64_t *r, ao_int64_t *a, uint16_t b) {
+       if ((int32_t) a->high < 0) {
+               ao_int64_t      t;
+
+               ao_neg64(&t, a);
+               ao_umul64_16(r, &t, b);
+               ao_neg64(r, r);
+       } else
+               ao_umul64_16(r, a, b);
+}
diff --git a/src/core/ao_int64.h b/src/core/ao_int64.h
new file mode 100644 (file)
index 0000000..93aa87e
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright © 2013 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#ifndef _AO_INT64_H_
+#define _AO_INT64_H_
+
+#include <stdint.h>
+
+typedef struct {
+       uint32_t        high;
+       uint32_t        low;
+} ao_int64_t;
+
+void ao_plus64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b);
+void ao_rshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d);
+void ao_lshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d);
+void ao_mul64(ao_int64_t *r, int32_t a, int32_t b);
+void ao_mul64_16(ao_int64_t *r, ao_int64_t *a, uint16_t b);
+
+#define ao_int64_init32(r, a) (((r)->high = 0), (r)->low = (a))
+#define ao_int64_init64(r, a, b) (((r)->high = (a)), (r)->low = (b))
+
+#define ao_cast64(a) (((int64_t) (a)->high << 32) | (a)->low)
+
+#endif /* _AO_INT64_H_ */
diff --git a/src/test/ao_int64_test.c b/src/test/ao_int64_test.c
new file mode 100644 (file)
index 0000000..67ba6ec
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright © 2013 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <ao_int64.h>
+#include <ao_int64.c>
+#include <stdio.h>
+#include <stdlib.h>
+
+int    errors;
+
+#define test(op,func,a,b,ao_a,ao_b) do {                               \
+               r = (a) op (b);                                         \
+               func(&ao_r, ao_a, ao_b);                                \
+               c = ao_cast64(&ao_r);                                   \
+               if (c != r) {                                           \
+                       printf ("trial %4d: %lld " #func " %lld = %lld (should be %lld)\n", \
+                               trial, (int64_t) (a), (int64_t) b, c, r); \
+                       ++errors;                                       \
+               }                                                       \
+       } while (0)
+
+
+void
+do_test(int trial, int64_t a, int64_t b)
+{
+       int64_t r, c;
+       ao_int64_t      ao_a, ao_b, ao_r;
+
+       ao_int64_init64(&ao_a, a >> 32, a);
+       ao_int64_init64(&ao_b, b >> 32, b);
+
+       test(+, ao_plus64, a, b, &ao_a, &ao_b);
+       test(*, ao_mul64,(int64_t) (int32_t) a, (int32_t) b, (int32_t) a, (int32_t) b);
+       test(>>, ao_rshift64, a, (uint8_t) b & 0x3f, &ao_a, (uint8_t) b & 0x3f);
+       test(<<, ao_lshift64, a, (uint8_t) b & 0x3f, &ao_a, (uint8_t) b & 0x3f);
+       test(*, ao_mul64_16, a, (uint16_t) b, &ao_a, (uint16_t) b);
+}
+
+#define TESTS  10000000
+
+static int64_t
+random64(void)
+{
+       return (int64_t) random() + ((int64_t) random() << 31) /* + ((int64_t) random() << 33) */;
+}
+
+int
+main (int argc, char **argv)
+{
+       int     i, start;
+
+       if (argv[1])
+               start = atoi(argv[1]);
+       else
+               start = 0;
+       srandom(1000);
+       for (i = 0; i < TESTS; i++) {
+               int64_t a = random64();
+               int64_t b = random64();
+               if (i >= start)
+                       do_test(i, a, b);
+       }
+       return errors;
+}