math/mpx-mul4-test.c: Compare and print test outputs by value.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 4 Nov 2019 11:57:53 +0000 (11:57 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 9 May 2020 19:57:33 +0000 (20:57 +0100)
Different implementations may find it useful to calculate redundant-
representation outputs in different ways.  Compare these by value rather
than by raw representation.

math/mpx-mul4-test.c

index 414974b..30e7691 100644 (file)
@@ -107,7 +107,37 @@ TESTOPS(DECLSTUB)
 
 /*----- Conversion functions ----------------------------------------------*/
 
-#define DEFTYPE(ty, ld, st, nby)                                       \
+static mp *combine_mpw(mp *d, const mpw *v, size_t n, unsigned off)
+{
+  size_t i;
+  unsigned o;
+  mp m, *t = d;
+  mpw w[1];
+
+  d = MP_ZERO;
+  for (i = 0, o = 0; i < n; i++, o += off) {
+    w[0] = v[i]; mp_build(&m, w, w + 1);
+    t = mp_lsl(t, &m, o); d = mp_add(d, d, t);
+  }
+  mp_drop(t); return (d);
+}
+
+static mp *combine_mpd(mp *d, const mpd *v, size_t n, unsigned off)
+{
+  size_t i;
+  unsigned o;
+  mp m, *t = d;
+  mpw w[2];
+
+  d = MP_ZERO;
+  for (i = 0, o = 0; i < n; i++, o += off) {
+    w[0] = MPW(v[i]); w[1] = MPW(v[i] >> MPW_BITS); mp_build(&m, w, w + 2);
+    t = mp_lsl(t, &m, o); d = mp_add(d, d, t);
+  }
+  mp_drop(t); return (d);
+}
+
+#define DEFTYPE(ty, ld, st, nby, combfn, off)                          \
                                                                        \
   static void cvt_##ty(const char *buf, dstr *d)                       \
   {                                                                    \
@@ -129,6 +159,7 @@ TESTOPS(DECLSTUB)
     dstr dd = DSTR_INIT;                                               \
     int i;                                                             \
     const ty *x = (const ty *)d->buf;                                  \
+    mp *xx = combfn(MP_NEW, x->w, N(x->w), off);                       \
     octet *p;                                                          \
                                                                        \
     dstr_ensure(&dd, N(x->w)*nby); p = (octet *)dd.buf;                        \
@@ -136,22 +167,26 @@ TESTOPS(DECLSTUB)
     dd.len = N(x->w)*nby;                                              \
     type_hex.dump(&dd, fp);                                            \
     dstr_destroy(&dd);                                                 \
+                                                                       \
+    fputs(" = 0x", fp); mp_writefile(xx, fp, 16);                      \
+    fputs(" = ", fp); mp_writefile(xx, fp, 10);                                \
+    MP_DROP(xx);                                                       \
   }                                                                    \
                                                                        \
   static int eq_##ty(const ty *x, const ty *y)                         \
   {                                                                    \
-    int i;                                                             \
-                                                                       \
-    for (i = 0; i < N(x->w); i++)                                      \
-      if (x->w[i] != y->w[i]) return (0);                              \
-    return (1);                                                                \
+    mp *xx = combfn(MP_NEW, x->w, N(x->w), off),                       \
+      *yy = combfn(MP_NEW, y->w, N(y->w), off);                                \
+    int rc = MP_EQ(xx, yy);                                            \
+    MP_DROP(xx); MP_DROP(yy);                                          \
+    return (rc);                                                       \
   }                                                                    \
                                                                        \
   static const struct test_type type_##ty = { cvt_##ty, dump_##ty };
 
-DEFTYPE(p128, LDW, STW, NWBY)
-DEFTYPE(x128, LDW, STW, NWBY)
-DEFTYPE(carry, LDD, STD, NDBY)
+DEFTYPE(p128, LDW, STW, NWBY, combine_mpw, MPW_BITS)
+DEFTYPE(x128, LDW, STW, NWBY, combine_mpw, MPW_BITS/2)
+DEFTYPE(carry, LDD, STD, NDBY, combine_mpd, MPW_BITS/2)
 
 /*----- Test functions ----------------------------------------------------*/