3 * Testing optimized 128-bit multipliers
5 * (c) 2017 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 #ifdef ENABLE_ASM_DEBUG
41 #include <mLib/macros.h>
42 #include <mLib/report.h>
43 #include <mLib/testrig.h>
50 /*----- CPU feature detection ---------------------------------------------*/
53 # define VARIANT _x86_sse2
55 static int cpu_features_p(void) { return (cpu_feature_p(CPUFEAT_X86_SSE2)); }
59 # define VARIANT _amd64_sse2
61 static int cpu_features_p(void) { return (cpu_feature_p(CPUFEAT_X86_SSE2)); }
65 # define VARIANT _arm_neon
67 static int cpu_features_p(void) { return (cpu_feature_p(CPUFEAT_ARM_NEON)); }
71 # define VARIANT _arm64_simd
73 static int cpu_features_p(void) { return (1); }
77 # error "Unsupported CPU family."
87 typedef struct { mpw w[4]; } p128;
88 typedef struct { mpw w[8]; } x128;
89 typedef struct { mpd w[6]; } carry;
92 /*----- Test operation table ----------------------------------------------*/
95 /* a c u x v y z' y' c' */ \
96 _(dmul4, NIL, CARRY, P128, P128, P128, P128, P128, NIL, CARRY) \
97 _(dmla4, P128, CARRY, P128, P128, P128, P128, P128, NIL, CARRY) \
98 _(mul4, NIL, CARRY, NIL, P128, NIL, P128, P128, NIL, CARRY) \
99 _(mul4zc,NIL, NIL, NIL, P128, NIL, P128, P128, NIL, CARRY) \
100 _(mla4, P128, CARRY, NIL, P128, NIL, P128, P128, NIL, CARRY) \
101 _(mla4zc,P128, NIL, NIL, P128, NIL, P128, P128, NIL, CARRY) \
102 _(mmul4, NIL, NIL, P128, P128, P128, P128, P128, X128, CARRY) \
103 _(mmla4, P128, NIL, P128, P128, P128, P128, P128, X128, CARRY) \
104 _(mont4, P128, NIL, NIL, P128, NIL, P128, P128, X128, CARRY)
106 /*----- Assembler test interface ------------------------------------------*/
110 #define PARAM(v, ty) ty *v,
111 #define PARAM_NIL(v, q)
112 #define PARAM_P128(v, q) PARAM(v, q p128)
113 #define PARAM_X128(v, q) PARAM(v, q x128)
114 #define PARAM_CARRY(v, q) PARAM(v, q carry)
116 #define DECLSTUB(fn, tya, tyc, tyu, tyx, tyv, tyy, tyzz, tyyy, tycc) \
117 extern void test_##fn(PARAM_##tyzz(zz, EMPTY) PARAM_##tycc(cc, EMPTY) \
118 PARAM_##tyyy(yy, EMPTY) \
119 PARAM_##tyu(u, const) PARAM_##tyx(x, const) \
120 PARAM_##tyv(v, const) PARAM_##tyy(y, const) \
121 unsigned n, unsigned long long *cyv);
124 /*----- Conversion functions ----------------------------------------------*/
126 static mp *combine_mpw(mp *d, const mpw *v, size_t n, unsigned off)
134 for (i = 0, o = 0; i < n; i++, o += off) {
135 w[0] = v[i]; mp_build(&m, w, w + 1);
136 t = mp_lsl(t, &m, o); d = mp_add(d, d, t);
138 mp_drop(t); return (d);
141 static mp *combine_mpd(mp *d, const mpd *v, size_t n, unsigned off)
149 for (i = 0, o = 0; i < n; i++, o += off) {
150 w[0] = MPW(v[i]); w[1] = MPW(v[i] >> MPW_BITS); mp_build(&m, w, w + 2);
151 t = mp_lsl(t, &m, o); d = mp_add(d, d, t);
153 mp_drop(t); return (d);
156 #define DEFTYPE(ty, ld, st, nby, combfn, off) \
158 static void cvt_##ty(const char *buf, dstr *d) \
160 dstr dd = DSTR_INIT; \
165 type_hex.cvt(buf, &dd); \
166 if (dd.len != N(x->w)*nby) die(1, "invalid length for " #ty); \
167 dstr_ensure(d, sizeof(*x)); d->len = sizeof(*x); \
168 x = (ty *)d->buf; p = (const octet *)dd.buf; \
169 for (i = 0; i < N(x->w); i++) { x->w[i] = ld(p); p += nby; } \
173 static void dump_##ty(dstr *d, FILE *fp) \
175 dstr dd = DSTR_INIT; \
177 const ty *x = (const ty *)d->buf; \
178 mp *xx = combfn(MP_NEW, x->w, N(x->w), off); \
181 dstr_ensure(&dd, N(x->w)*nby); p = (octet *)dd.buf; \
182 for (i = 0; i < N(x->w); i++) { st(p, x->w[i]); p += nby; } \
183 dd.len = N(x->w)*nby; \
184 type_hex.dump(&dd, fp); \
187 fputs(" = 0x", fp); mp_writefile(xx, fp, 16); \
188 fputs(" = ", fp); mp_writefile(xx, fp, 10); \
192 static int eq_##ty(const ty *x, const ty *y) \
194 mp *xx = combfn(MP_NEW, x->w, N(x->w), off), \
195 *yy = combfn(MP_NEW, y->w, N(y->w), off); \
196 int rc = MP_EQ(xx, yy); \
197 MP_DROP(xx); MP_DROP(yy); \
201 static const struct test_type type_##ty = { cvt_##ty, dump_##ty };
203 DEFTYPE(p128, LDW, STW, NWBY, combine_mpw, MPW_BITS)
204 DEFTYPE(x128, LDW, STW, NWBY, combine_mpw, MPW_BITS/2)
205 DEFTYPE(carry, LDD, STD, NDBY, combine_mpd, MPW_BITS/2)
207 /*----- Test functions ----------------------------------------------------*/
209 #define DECL_IN(v, ty) \
210 dstr *d_##v = dp++; const ty *v = (const ty *)d_##v->buf;
211 #define DECL_IN_NIL(v)
212 #define DECL_IN_P128(v) DECL_IN(v, p128)
213 #define DECL_IN_X128(v) DECL_IN(v, x128)
214 #define DECL_IN_CARRY(v) DECL_IN(v, carry)
216 #define DECL_OUT(v, ty) \
217 dstr dd_##v = DSTR_INIT, *d_##v = &dd_##v; ty *v;
218 #define DECL_OUT_NIL(v)
219 #define DECL_OUT_P128(v) DECL_OUT(v, p128)
220 #define DECL_OUT_X128(v) DECL_OUT(v, x128)
221 #define DECL_OUT_CARRY(v) DECL_OUT(v, carry)
223 #define INIT_OUT(v, ty) \
224 dstr_ensure(d_##v, sizeof(ty)); v = (ty *)d_##v->buf;
225 #define INIT_OUT_NIL(v)
226 #define INIT_OUT_P128(v) INIT_OUT(v, p128)
227 #define INIT_OUT_X128(v) INIT_OUT(v, x128)
228 #define INIT_OUT_CARRY(v) INIT_OUT(v, carry)
232 #define ARG_P128(v) ARG(v)
233 #define ARG_X128(v) ARG(v)
234 #define ARG_CARRY(v) ARG(v)
236 #define CHECK(v, vv, ty) if (!eq_##ty(v, vv)) ok = 0;
237 #define CHECK_NIL(v, vv)
238 #define CHECK_P128(v, vv) CHECK(v, vv, p128)
239 #define CHECK_X128(v, vv) CHECK(v, vv, x128)
240 #define CHECK_CARRY(v, vv) CHECK(v, vv, carry)
242 #define DUMP(v, ty) \
243 fprintf(stderr, "\n\t%-6s = ", #v); dump_##ty(d_##v, stderr);
245 #define DUMP_P128(v) DUMP(v, p128)
246 #define DUMP_X128(v) DUMP(v, x128)
247 #define DUMP_CARRY(v) DUMP(v, carry)
249 #define COPY(v, vv, ty) *v = *vv;
250 #define COPY_NIL(v, vv)
251 #define COPY_P128(v, vv) COPY(v, vv, p128)
252 #define COPY_X128(v, vv) COPY(v, vv, x128)
253 #define COPY_CARRY(v, vv) COPY(v, vv, carry)
255 #define FREE_OUT(v, ty) dstr_destroy(d_##v);
256 #define FREE_OUT_NIL(v)
257 #define FREE_OUT_P128(v) FREE_OUT(v, p128)
258 #define FREE_OUT_X128(v) FREE_OUT(v, x128)
259 #define FREE_OUT_CARRY(v) FREE_OUT(v, carry)
261 #define DEFTESTFN(fn, tya, tyc, tyu, tyx, tyv, tyy, tyzz, tyyy, tycc) \
263 static int v##fn(dstr dv[]) \
272 DECL_IN_##tyzz(zz_exp) \
273 DECL_IN_##tyyy(yy_exp) \
274 DECL_IN_##tycc(cc_exp) \
275 DECL_OUT_##tyzz(zz_out) \
276 DECL_OUT_##tyyy(yy_out) \
277 DECL_OUT_##tycc(cc_out) \
278 unsigned long long cyv[1]; \
281 INIT_OUT_##tyzz(zz_out) \
282 INIT_OUT_##tyyy(yy_out) \
283 INIT_OUT_##tycc(cc_out) \
285 COPY_##tya(zz_out, a); \
286 COPY_##tyc(cc_out, c); \
287 test_##fn(zz_out ARG_##tycc(cc_out) ARG_##tyyy(yy_out) \
288 ARG_##tyu(u) ARG_##tyx(x) ARG_##tyv(v) ARG_##tyy(y), \
290 CHECK_##tyzz(zz_exp, zz_out) \
291 CHECK_##tyyy(yy_exp, yy_out) \
292 CHECK_##tycc(cc_exp, cc_out) \
295 fputs(#fn " failed", stderr); \
302 DUMP_##tyzz(zz_exp) \
303 DUMP_##tyzz(zz_out) \
304 DUMP_##tyyy(yy_exp) \
305 DUMP_##tyyy(yy_out) \
306 DUMP_##tycc(cc_exp) \
307 DUMP_##tycc(cc_out) \
308 fputc('\n', stderr); \
311 FREE_OUT_##tyzz(zz_out); \
312 FREE_OUT_##tyyy(yy_out); \
313 FREE_OUT_##tycc(cc_out); \
320 /*----- Main code ---------------------------------------------------------*/
323 #define P128 &type_p128,
324 #define X128 &type_x128,
325 #define CARRY &type_carry,
327 static test_chunk tests[] = {
328 #define DEFCHUNK(fn, tya, tyc, tyu, tyx, tyv, tyy, tyzz, tyyy, tycc) \
329 { #fn, v##fn, { tya tyc tyu tyx tyv tyy tyzz tyyy tycc } },
335 int main(int argc, char *argv[])
338 #ifdef ENABLE_ASM_DEBUG
341 if (!cpu_features_p())
342 { fprintf(stderr, "required cpu feature not available\n"); exit(77); }
343 test_run(argc, argv, tests, SRCDIR "/t/mpx-mul4");
347 /*----- That's all, folks -------------------------------------------------*/