From 44cf74d554d73f9c243750939138d618089fa19e Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Mon, 4 Nov 2019 11:57:53 +0000 Subject: [PATCH] math/mpx-mul4-test.c: Compare and print test outputs by value. 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 | 53 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/math/mpx-mul4-test.c b/math/mpx-mul4-test.c index 414974bf..30e76912 100644 --- a/math/mpx-mul4-test.c +++ b/math/mpx-mul4-test.c @@ -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 ----------------------------------------------------*/ -- 2.11.0