xref: /freebsd/contrib/arm-optimized-routines/math/test/mathtest.c (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 /*
2  * mathtest.c - test rig for mathlib
3  *
4  * Copyright (c) 1998-2019, Arm Limited.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <setjmp.h>
13 #include <ctype.h>
14 #include <math.h>
15 #include <errno.h>
16 #include <limits.h>
17 #include <fenv.h>
18 #include "mathlib.h"
19 
20 #ifndef math_errhandling
21 # define math_errhandling 0
22 #endif
23 
24 #ifdef __cplusplus
25  #define EXTERN_C extern "C"
26 #else
27  #define EXTERN_C extern
28 #endif
29 
30 #ifndef TRUE
31 #define TRUE 1
32 #endif
33 #ifndef FALSE
34 #define FALSE 0
35 #endif
36 
37 #ifdef IMPORT_SYMBOL
38 #define STR2(x) #x
39 #define STR(x) STR2(x)
40 _Pragma(STR(import IMPORT_SYMBOL))
41 #endif
42 
43 int dmsd, dlsd;
44 int quiet = 0;
45 int doround = 0;
46 unsigned statusmask = FE_ALL_EXCEPT;
47 
48 #define EXTRABITS (12)
49 #define ULPUNIT (1<<EXTRABITS)
50 
51 typedef int (*test) (void);
52 
53 /*
54   struct to hold info about a function (which could actually be a macro)
55 */
56 typedef struct {
57     enum {
58         t_func, t_macro
59     } type;
60     enum {
61         at_d, at_s,      /* double or single precision float */
62         at_d2, at_s2,    /* same, but taking two args */
63         at_di, at_si,    /* double/single and an int */
64         at_dip, at_sip,  /* double/single and an int ptr */
65         at_ddp, at_ssp,  /* d/s and a d/s ptr */
66         at_dc, at_sc,    /* double or single precision complex */
67         at_dc2, at_sc2   /* same, but taking two args */
68     } argtype;
69     enum {
70         rt_d, rt_s, rt_i, /* double, single, int */
71         rt_dc, rt_sc,     /* double, single precision complex */
72         rt_d2, rt_s2      /* also use res2 */
73     } rettype;
74     union {
75         void* ptr;
76         double (*d_d_ptr)(double);
77         float (*s_s_ptr)(float);
78         int (*d_i_ptr)(double);
79         int (*s_i_ptr)(float);
80         double (*d2_d_ptr)(double, double);
81         float (*s2_s_ptr)(float, float);
82         double (*di_d_ptr)(double,int);
83         float (*si_s_ptr)(float,int);
84         double (*dip_d_ptr)(double,int*);
85         float (*sip_s_ptr)(float,int*);
86         double (*ddp_d_ptr)(double,double*);
87         float (*ssp_s_ptr)(float,float*);
88     } func;
89     enum {
90         m_none,
91         m_isfinite, m_isfinitef,
92         m_isgreater, m_isgreaterequal,
93         m_isgreaterequalf, m_isgreaterf,
94         m_isinf, m_isinff,
95         m_isless, m_islessequal,
96         m_islessequalf, m_islessf,
97         m_islessgreater, m_islessgreaterf,
98         m_isnan, m_isnanf,
99         m_isnormal, m_isnormalf,
100         m_isunordered, m_isunorderedf,
101         m_fpclassify, m_fpclassifyf,
102         m_signbit, m_signbitf,
103         /* not actually a macro, but makes things easier */
104         m_rred, m_rredf,
105         m_cadd, m_csub, m_cmul, m_cdiv,
106         m_caddf, m_csubf, m_cmulf, m_cdivf
107     } macro_name; /* only used if a macro/something that can't be done using func */
108     long long tolerance;
109     const char* name;
110 } test_func;
111 
112 /* used in qsort */
113 int compare_tfuncs(const void* a, const void* b) {
114     return strcmp(((test_func*)a)->name, ((test_func*)b)->name);
115 }
116 
117 int is_double_argtype(int argtype) {
118     switch(argtype) {
119     case at_d:
120     case at_d2:
121     case at_dc:
122     case at_dc2:
123         return 1;
124     default:
125         return 0;
126     }
127 }
128 
129 int is_single_argtype(int argtype) {
130     switch(argtype) {
131     case at_s:
132     case at_s2:
133     case at_sc:
134     case at_sc2:
135         return 1;
136     default:
137         return 0;
138     }
139 }
140 
141 int is_double_rettype(int rettype) {
142     switch(rettype) {
143     case rt_d:
144     case rt_dc:
145     case rt_d2:
146         return 1;
147     default:
148         return 0;
149     }
150 }
151 
152 int is_single_rettype(int rettype) {
153     switch(rettype) {
154     case rt_s:
155     case rt_sc:
156     case rt_s2:
157         return 1;
158     default:
159         return 0;
160     }
161 }
162 
163 int is_complex_argtype(int argtype) {
164     switch(argtype) {
165     case at_dc:
166     case at_sc:
167     case at_dc2:
168     case at_sc2:
169         return 1;
170     default:
171         return 0;
172     }
173 }
174 
175 int is_complex_rettype(int rettype) {
176     switch(rettype) {
177     case rt_dc:
178     case rt_sc:
179         return 1;
180     default:
181         return 0;
182     }
183 }
184 
185 /*
186  * Special-case flags indicating that some functions' error
187  * tolerance handling is more complicated than a fixed relative
188  * error bound.
189  */
190 #define ABSLOWERBOUND 0x4000000000000000LL
191 #define PLUSMINUSPIO2 0x1000000000000000LL
192 
193 #define ARM_PREFIX(x) x
194 
195 #define TFUNC(arg,ret,name,tolerance) { t_func, arg, ret, (void*)&name, m_none, tolerance, #name }
196 #define TFUNCARM(arg,ret,name,tolerance) { t_func, arg, ret, (void*)& ARM_PREFIX(name), m_none, tolerance, #name }
197 #define MFUNC(arg,ret,name,tolerance) { t_macro, arg, ret, NULL, m_##name, tolerance, #name }
198 
199 /* sincosf wrappers for easier testing.  */
200 static float sincosf_sinf(float x) { float s,c; sincosf(x, &s, &c); return s; }
201 static float sincosf_cosf(float x) { float s,c; sincosf(x, &s, &c); return c; }
202 
203 test_func tfuncs[] = {
204     /* trigonometric */
205     TFUNC(at_d,rt_d, acos, 4*ULPUNIT),
206     TFUNC(at_d,rt_d, asin, 4*ULPUNIT),
207     TFUNC(at_d,rt_d, atan, 4*ULPUNIT),
208     TFUNC(at_d2,rt_d, atan2, 4*ULPUNIT),
209 
210     TFUNC(at_d,rt_d, tan, 2*ULPUNIT),
211     TFUNC(at_d,rt_d, sin, 2*ULPUNIT),
212     TFUNC(at_d,rt_d, cos, 2*ULPUNIT),
213 
214     TFUNC(at_s,rt_s, acosf, 4*ULPUNIT),
215     TFUNC(at_s,rt_s, asinf, 4*ULPUNIT),
216     TFUNC(at_s,rt_s, atanf, 4*ULPUNIT),
217     TFUNC(at_s2,rt_s, atan2f, 4*ULPUNIT),
218     TFUNCARM(at_s,rt_s, tanf, 4*ULPUNIT),
219     TFUNCARM(at_s,rt_s, sinf, 3*ULPUNIT/4),
220     TFUNCARM(at_s,rt_s, cosf, 3*ULPUNIT/4),
221     TFUNCARM(at_s,rt_s, sincosf_sinf, 3*ULPUNIT/4),
222     TFUNCARM(at_s,rt_s, sincosf_cosf, 3*ULPUNIT/4),
223 
224     /* hyperbolic */
225     TFUNC(at_d, rt_d, atanh, 4*ULPUNIT),
226     TFUNC(at_d, rt_d, asinh, 4*ULPUNIT),
227     TFUNC(at_d, rt_d, acosh, 4*ULPUNIT),
228     TFUNC(at_d,rt_d, tanh, 4*ULPUNIT),
229     TFUNC(at_d,rt_d, sinh, 4*ULPUNIT),
230     TFUNC(at_d,rt_d, cosh, 4*ULPUNIT),
231 
232     TFUNC(at_s, rt_s, atanhf, 4*ULPUNIT),
233     TFUNC(at_s, rt_s, asinhf, 4*ULPUNIT),
234     TFUNC(at_s, rt_s, acoshf, 4*ULPUNIT),
235     TFUNC(at_s,rt_s, tanhf, 4*ULPUNIT),
236     TFUNC(at_s,rt_s, sinhf, 4*ULPUNIT),
237     TFUNC(at_s,rt_s, coshf, 4*ULPUNIT),
238 
239     /* exponential and logarithmic */
240     TFUNC(at_d,rt_d, log, 3*ULPUNIT/4),
241     TFUNC(at_d,rt_d, log10, 3*ULPUNIT),
242     TFUNC(at_d,rt_d, log2, 3*ULPUNIT/4),
243     TFUNC(at_d,rt_d, log1p, 2*ULPUNIT),
244     TFUNC(at_d,rt_d, exp, 3*ULPUNIT/4),
245     TFUNC(at_d,rt_d, exp2, 3*ULPUNIT/4),
246     TFUNC(at_d,rt_d, expm1, ULPUNIT),
247     TFUNCARM(at_s,rt_s, logf, ULPUNIT),
248     TFUNC(at_s,rt_s, log10f, 3*ULPUNIT),
249     TFUNCARM(at_s,rt_s, log2f, ULPUNIT),
250     TFUNC(at_s,rt_s, log1pf, 2*ULPUNIT),
251     TFUNCARM(at_s,rt_s, expf, 3*ULPUNIT/4),
252     TFUNCARM(at_s,rt_s, exp2f, 3*ULPUNIT/4),
253     TFUNC(at_s,rt_s, expm1f, ULPUNIT),
254 
255     /* power */
256     TFUNC(at_d2,rt_d, pow, 3*ULPUNIT/4),
257     TFUNC(at_d,rt_d, sqrt, ULPUNIT/2),
258     TFUNC(at_d,rt_d, cbrt, 2*ULPUNIT),
259     TFUNC(at_d2, rt_d, hypot, 4*ULPUNIT),
260 
261     TFUNCARM(at_s2,rt_s, powf, ULPUNIT),
262     TFUNC(at_s,rt_s, sqrtf, ULPUNIT/2),
263     TFUNC(at_s,rt_s, cbrtf, 2*ULPUNIT),
264     TFUNC(at_s2, rt_s, hypotf, 4*ULPUNIT),
265 
266     /* error function */
267     TFUNC(at_d,rt_d, erf, 16*ULPUNIT),
268     TFUNC(at_s,rt_s, erff, 16*ULPUNIT),
269     TFUNC(at_d,rt_d, erfc, 16*ULPUNIT),
270     TFUNC(at_s,rt_s, erfcf, 16*ULPUNIT),
271 
272     /* gamma functions */
273     TFUNC(at_d,rt_d, tgamma, 16*ULPUNIT),
274     TFUNC(at_s,rt_s, tgammaf, 16*ULPUNIT),
275     TFUNC(at_d,rt_d, lgamma, 16*ULPUNIT | ABSLOWERBOUND),
276     TFUNC(at_s,rt_s, lgammaf, 16*ULPUNIT | ABSLOWERBOUND),
277 
278     TFUNC(at_d,rt_d, ceil, 0),
279     TFUNC(at_s,rt_s, ceilf, 0),
280     TFUNC(at_d2,rt_d, copysign, 0),
281     TFUNC(at_s2,rt_s, copysignf, 0),
282     TFUNC(at_d,rt_d, floor, 0),
283     TFUNC(at_s,rt_s, floorf, 0),
284     TFUNC(at_d2,rt_d, fmax, 0),
285     TFUNC(at_s2,rt_s, fmaxf, 0),
286     TFUNC(at_d2,rt_d, fmin, 0),
287     TFUNC(at_s2,rt_s, fminf, 0),
288     TFUNC(at_d2,rt_d, fmod, 0),
289     TFUNC(at_s2,rt_s, fmodf, 0),
290     MFUNC(at_d, rt_i, fpclassify, 0),
291     MFUNC(at_s, rt_i, fpclassifyf, 0),
292     TFUNC(at_dip,rt_d, frexp, 0),
293     TFUNC(at_sip,rt_s, frexpf, 0),
294     MFUNC(at_d, rt_i, isfinite, 0),
295     MFUNC(at_s, rt_i, isfinitef, 0),
296     MFUNC(at_d, rt_i, isgreater, 0),
297     MFUNC(at_d, rt_i, isgreaterequal, 0),
298     MFUNC(at_s, rt_i, isgreaterequalf, 0),
299     MFUNC(at_s, rt_i, isgreaterf, 0),
300     MFUNC(at_d, rt_i, isinf, 0),
301     MFUNC(at_s, rt_i, isinff, 0),
302     MFUNC(at_d, rt_i, isless, 0),
303     MFUNC(at_d, rt_i, islessequal, 0),
304     MFUNC(at_s, rt_i, islessequalf, 0),
305     MFUNC(at_s, rt_i, islessf, 0),
306     MFUNC(at_d, rt_i, islessgreater, 0),
307     MFUNC(at_s, rt_i, islessgreaterf, 0),
308     MFUNC(at_d, rt_i, isnan, 0),
309     MFUNC(at_s, rt_i, isnanf, 0),
310     MFUNC(at_d, rt_i, isnormal, 0),
311     MFUNC(at_s, rt_i, isnormalf, 0),
312     MFUNC(at_d, rt_i, isunordered, 0),
313     MFUNC(at_s, rt_i, isunorderedf, 0),
314     TFUNC(at_di,rt_d, ldexp, 0),
315     TFUNC(at_si,rt_s, ldexpf, 0),
316     TFUNC(at_ddp,rt_d2, modf, 0),
317     TFUNC(at_ssp,rt_s2, modff, 0),
318 #ifndef BIGRANGERED
319     MFUNC(at_d, rt_d, rred, 2*ULPUNIT),
320 #else
321     MFUNC(at_d, rt_d, m_rred, ULPUNIT),
322 #endif
323     MFUNC(at_d, rt_i, signbit, 0),
324     MFUNC(at_s, rt_i, signbitf, 0),
325 };
326 
327 /*
328  * keywords are: func size op1 op2 result res2 errno op1r op1i op2r op2i resultr resulti
329  * also we ignore: wrongresult wrongres2 wrongerrno
330  * op1 equivalent to op1r, same with op2 and result
331  */
332 
333 typedef struct {
334     test_func *func;
335     unsigned op1r[2]; /* real part, also used for non-complex numbers */
336     unsigned op1i[2]; /* imaginary part */
337     unsigned op2r[2];
338     unsigned op2i[2];
339     unsigned resultr[3];
340     unsigned resulti[3];
341     enum {
342         rc_none, rc_zero, rc_infinity, rc_nan, rc_finite
343     } resultc; /* special complex results, rc_none means use resultr and resulti as normal */
344     unsigned res2[2];
345     unsigned status;                   /* IEEE status return, if any */
346     unsigned maybestatus;             /* for optional status, or allowance for spurious */
347     int nresult;                       /* number of result words */
348     int in_err, in_err_limit;
349     int err;
350     int maybeerr;
351     int valid;
352     int comment;
353     int random;
354 } testdetail;
355 
356 enum {                                 /* keywords */
357     k_errno, k_errno_in, k_error, k_func, k_maybeerror, k_maybestatus, k_op1, k_op1i, k_op1r, k_op2, k_op2i, k_op2r,
358     k_random, k_res2, k_result, k_resultc, k_resulti, k_resultr, k_status,
359     k_wrongres2, k_wrongresult, k_wrongstatus, k_wrongerrno
360 };
361 char *keywords[] = {
362     "errno", "errno_in", "error", "func", "maybeerror", "maybestatus", "op1", "op1i", "op1r", "op2", "op2i", "op2r",
363     "random", "res2", "result", "resultc", "resulti", "resultr", "status",
364     "wrongres2", "wrongresult", "wrongstatus", "wrongerrno"
365 };
366 
367 enum {
368     e_0, e_EDOM, e_ERANGE,
369 
370     /*
371      * This enum makes sure that we have the right number of errnos in the
372      * errno[] array
373      */
374     e_number_of_errnos
375 };
376 char *errnos[] = {
377     "0", "EDOM", "ERANGE"
378 };
379 
380 enum {
381     e_none, e_divbyzero, e_domain, e_overflow, e_underflow
382 };
383 char *errors[] = {
384     "0", "divbyzero", "domain", "overflow", "underflow"
385 };
386 
387 static int verbose, fo, strict;
388 
389 /* state toggled by random=on / random=off */
390 static int randomstate;
391 
392 /* Canonify a double NaN: SNaNs all become 7FF00000.00000001 and QNaNs
393  * all become 7FF80000.00000001 */
394 void canon_dNaN(unsigned a[2]) {
395     if ((a[0] & 0x7FF00000) != 0x7FF00000)
396         return;                        /* not Inf or NaN */
397     if (!(a[0] & 0xFFFFF) && !a[1])
398         return;                        /* Inf */
399     a[0] &= 0x7FF80000;                /* canonify top word */
400     a[1] = 0x00000001;                 /* canonify bottom word */
401 }
402 
403 /* Canonify a single NaN: SNaNs all become 7F800001 and QNaNs
404  * all become 7FC00001. Returns classification of the NaN. */
405 void canon_sNaN(unsigned a[1]) {
406     if ((a[0] & 0x7F800000) != 0x7F800000)
407         return;                        /* not Inf or NaN */
408     if (!(a[0] & 0x7FFFFF))
409         return;                        /* Inf */
410     a[0] &= 0x7FC00000;                /* canonify most bits */
411     a[0] |= 0x00000001;                /* canonify bottom bit */
412 }
413 
414 /*
415  * Detect difficult operands for FO mode.
416  */
417 int is_dhard(unsigned a[2])
418 {
419     if ((a[0] & 0x7FF00000) == 0x7FF00000)
420         return TRUE;                   /* inf or NaN */
421     if ((a[0] & 0x7FF00000) == 0 &&
422         ((a[0] & 0x7FFFFFFF) | a[1]) != 0)
423         return TRUE;                   /* denormal */
424     return FALSE;
425 }
426 int is_shard(unsigned a[1])
427 {
428     if ((a[0] & 0x7F800000) == 0x7F800000)
429         return TRUE;                   /* inf or NaN */
430     if ((a[0] & 0x7F800000) == 0 &&
431         (a[0] & 0x7FFFFFFF) != 0)
432         return TRUE;                   /* denormal */
433     return FALSE;
434 }
435 
436 /*
437  * Normalise all zeroes into +0, for FO mode.
438  */
439 void dnormzero(unsigned a[2])
440 {
441     if (a[0] == 0x80000000 && a[1] == 0)
442         a[0] = 0;
443 }
444 void snormzero(unsigned a[1])
445 {
446     if (a[0] == 0x80000000)
447         a[0] = 0;
448 }
449 
450 static int find(char *word, char **array, int asize) {
451     int i, j;
452 
453     asize /= sizeof(char *);
454 
455     i = -1; j = asize;                 /* strictly between i and j */
456     while (j-i > 1) {
457         int k = (i+j) / 2;
458         int c = strcmp(word, array[k]);
459         if (c > 0)
460             i = k;
461         else if (c < 0)
462             j = k;
463         else                           /* found it! */
464             return k;
465     }
466     return -1;                         /* not found */
467 }
468 
469 static test_func* find_testfunc(char *word) {
470     int i, j, asize;
471 
472     asize = sizeof(tfuncs)/sizeof(test_func);
473 
474     i = -1; j = asize;                 /* strictly between i and j */
475     while (j-i > 1) {
476         int k = (i+j) / 2;
477         int c = strcmp(word, tfuncs[k].name);
478         if (c > 0)
479             i = k;
480         else if (c < 0)
481             j = k;
482         else                           /* found it! */
483             return tfuncs + k;
484     }
485     return NULL;                         /* not found */
486 }
487 
488 static long long calc_error(unsigned a[2], unsigned b[3], int shift, int rettype) {
489     unsigned r0, r1, r2;
490     int sign, carry;
491     long long result;
492 
493     /*
494      * If either number is infinite, require exact equality. If
495      * either number is NaN, require that both are NaN. If either
496      * of these requirements is broken, return INT_MAX.
497      */
498     if (is_double_rettype(rettype)) {
499         if ((a[0] & 0x7FF00000) == 0x7FF00000 ||
500             (b[0] & 0x7FF00000) == 0x7FF00000) {
501             if (((a[0] & 0x800FFFFF) || a[1]) &&
502                 ((b[0] & 0x800FFFFF) || b[1]) &&
503                 (a[0] & 0x7FF00000) == 0x7FF00000 &&
504                 (b[0] & 0x7FF00000) == 0x7FF00000)
505                 return 0;              /* both NaN - OK */
506             if (!((a[0] & 0xFFFFF) || a[1]) &&
507                 !((b[0] & 0xFFFFF) || b[1]) &&
508                 a[0] == b[0])
509                 return 0;              /* both same sign of Inf - OK */
510             return LLONG_MAX;
511         }
512     } else {
513         if ((a[0] & 0x7F800000) == 0x7F800000 ||
514             (b[0] & 0x7F800000) == 0x7F800000) {
515             if ((a[0] & 0x807FFFFF) &&
516                 (b[0] & 0x807FFFFF) &&
517                 (a[0] & 0x7F800000) == 0x7F800000 &&
518                 (b[0] & 0x7F800000) == 0x7F800000)
519                 return 0;              /* both NaN - OK */
520             if (!(a[0] & 0x7FFFFF) &&
521                 !(b[0] & 0x7FFFFF) &&
522                 a[0] == b[0])
523                 return 0;              /* both same sign of Inf - OK */
524             return LLONG_MAX;
525         }
526     }
527 
528     /*
529      * Both finite. Return INT_MAX if the signs differ.
530      */
531     if ((a[0] ^ b[0]) & 0x80000000)
532         return LLONG_MAX;
533 
534     /*
535      * Now it's just straight multiple-word subtraction.
536      */
537     if (is_double_rettype(rettype)) {
538         r2 = -b[2]; carry = (r2 == 0);
539         r1 = a[1] + ~b[1] + carry; carry = (r1 < a[1] || (carry && r1 == a[1]));
540         r0 = a[0] + ~b[0] + carry;
541     } else {
542         r2 = -b[1]; carry = (r2 == 0);
543         r1 = a[0] + ~b[0] + carry; carry = (r1 < a[0] || (carry && r1 == a[0]));
544         r0 = ~0 + carry;
545     }
546 
547     /*
548      * Forgive larger errors in specialised cases.
549      */
550     if (shift > 0) {
551         if (shift > 32*3)
552             return 0;                  /* all errors are forgiven! */
553         while (shift >= 32) {
554             r2 = r1;
555             r1 = r0;
556             r0 = -(r0 >> 31);
557             shift -= 32;
558         }
559 
560         if (shift > 0) {
561             r2 = (r2 >> shift) | (r1 << (32-shift));
562             r1 = (r1 >> shift) | (r0 << (32-shift));
563             r0 = (r0 >> shift) | ((-(r0 >> 31)) << (32-shift));
564         }
565     }
566 
567     if (r0 & 0x80000000) {
568         sign = 1;
569         r2 = ~r2; carry = (r2 == 0);
570         r1 = 0 + ~r1 + carry; carry = (carry && (r2 == 0));
571         r0 = 0 + ~r0 + carry;
572     } else {
573         sign = 0;
574     }
575 
576     if (r0 >= (1LL<<(31-EXTRABITS)))
577         return LLONG_MAX;                /* many ulps out */
578 
579     result = (r2 >> (32-EXTRABITS)) & (ULPUNIT-1);
580     result |= r1 << EXTRABITS;
581     result |= (long long)r0 << (32+EXTRABITS);
582     if (sign)
583         result = -result;
584     return result;
585 }
586 
587 /* special named operands */
588 
589 typedef struct {
590     unsigned op1, op2;
591     char* name;
592 } special_op;
593 
594 static special_op special_ops_double[] = {
595     {0x00000000,0x00000000,"0"},
596     {0x3FF00000,0x00000000,"1"},
597     {0x7FF00000,0x00000000,"inf"},
598     {0x7FF80000,0x00000001,"qnan"},
599     {0x7FF00000,0x00000001,"snan"},
600     {0x3ff921fb,0x54442d18,"pi2"},
601     {0x400921fb,0x54442d18,"pi"},
602     {0x3fe921fb,0x54442d18,"pi4"},
603     {0x4002d97c,0x7f3321d2,"3pi4"},
604 };
605 
606 static special_op special_ops_float[] = {
607     {0x00000000,0,"0"},
608     {0x3f800000,0,"1"},
609     {0x7f800000,0,"inf"},
610     {0x7fc00000,0,"qnan"},
611     {0x7f800001,0,"snan"},
612     {0x3fc90fdb,0,"pi2"},
613     {0x40490fdb,0,"pi"},
614     {0x3f490fdb,0,"pi4"},
615     {0x4016cbe4,0,"3pi4"},
616 };
617 
618 /*
619    This is what is returned by the below functions.
620    We need it to handle the sign of the number
621 */
622 static special_op tmp_op = {0,0,0};
623 
624 special_op* find_special_op_from_op(unsigned op1, unsigned op2, int is_double) {
625     int i;
626     special_op* sop;
627     if(is_double) {
628         sop = special_ops_double;
629     } else {
630         sop = special_ops_float;
631     }
632     for(i = 0; i < sizeof(special_ops_double)/sizeof(special_op); i++) {
633         if(sop->op1 == (op1&0x7fffffff) && sop->op2 == op2) {
634             if(tmp_op.name) free(tmp_op.name);
635             tmp_op.name = malloc(strlen(sop->name)+2);
636             if(op1>>31) {
637                 sprintf(tmp_op.name,"-%s",sop->name);
638             } else {
639                 strcpy(tmp_op.name,sop->name);
640             }
641             return &tmp_op;
642         }
643         sop++;
644     }
645     return NULL;
646 }
647 
648 special_op* find_special_op_from_name(const char* name, int is_double) {
649     int i, neg=0;
650     special_op* sop;
651     if(is_double) {
652         sop = special_ops_double;
653     } else {
654         sop = special_ops_float;
655     }
656     if(*name=='-') {
657         neg=1;
658         name++;
659     } else if(*name=='+') {
660         name++;
661     }
662     for(i = 0; i < sizeof(special_ops_double)/sizeof(special_op); i++) {
663         if(0 == strcmp(name,sop->name)) {
664             tmp_op.op1 = sop->op1;
665             if(neg) {
666                 tmp_op.op1 |= 0x80000000;
667             }
668             tmp_op.op2 = sop->op2;
669             return &tmp_op;
670         }
671         sop++;
672     }
673     return NULL;
674 }
675 
676 /*
677    helper function for the below
678    type=0 for single, 1 for double, 2 for no sop
679 */
680 int do_op(char* q, unsigned* op, const char* name, int num, int sop_type) {
681     int i;
682     int n=num;
683     special_op* sop = NULL;
684     for(i = 0; i < num; i++) {
685         op[i] = 0;
686     }
687     if(sop_type<2) {
688         sop = find_special_op_from_name(q,sop_type);
689     }
690     if(sop != NULL) {
691         op[0] = sop->op1;
692         op[1] = sop->op2;
693     } else {
694         switch(num) {
695         case 1: n = sscanf(q, "%x", &op[0]); break;
696         case 2: n = sscanf(q, "%x.%x", &op[0], &op[1]); break;
697         case 3: n = sscanf(q, "%x.%x.%x", &op[0], &op[1], &op[2]); break;
698         default: return -1;
699         }
700     }
701     if (verbose) {
702         printf("%s=",name);
703         for (i = 0; (i < n); ++i) printf("%x.", op[i]);
704         printf(" (n=%d)\n", n);
705     }
706     return n;
707 }
708 
709 testdetail parsetest(char *testbuf, testdetail oldtest) {
710     char *p; /* Current part of line: Option name */
711     char *q; /* Current part of line: Option value */
712     testdetail ret; /* What we return */
713     int k; /* Function enum from k_* */
714     int n; /* Used as returns for scanfs */
715     int argtype=2, rettype=2; /* for do_op */
716 
717     /* clear ret */
718     memset(&ret, 0, sizeof(ret));
719 
720     if (verbose) printf("Parsing line: %s\n", testbuf);
721     while (*testbuf && isspace(*testbuf)) testbuf++;
722     if (testbuf[0] == ';' || testbuf[0] == '#' || testbuf[0] == '!' ||
723         testbuf[0] == '>' || testbuf[0] == '\0') {
724         ret.comment = 1;
725         if (verbose) printf("Line is a comment\n");
726         return ret;
727     }
728     ret.comment = 0;
729 
730     if (*testbuf == '+') {
731         if (oldtest.valid) {
732             ret = oldtest;             /* structure copy */
733         } else {
734             fprintf(stderr, "copy from invalid: ignored\n");
735         }
736         testbuf++;
737     }
738 
739     ret.random = randomstate;
740 
741     ret.in_err = 0;
742     ret.in_err_limit = e_number_of_errnos;
743 
744     p = strtok(testbuf, " \t");
745     while (p != NULL) {
746         q = strchr(p, '=');
747         if (!q)
748             goto balderdash;
749         *q++ = '\0';
750         k = find(p, keywords, sizeof(keywords));
751         switch (k) {
752         case k_random:
753             randomstate = (!strcmp(q, "on"));
754             ret.comment = 1;
755             return ret;                /* otherwise ignore this line */
756         case k_func:
757             if (verbose) printf("func=%s ", q);
758             //ret.func = find(q, funcs, sizeof(funcs));
759             ret.func = find_testfunc(q);
760             if (ret.func == NULL)
761                 {
762                     if (verbose) printf("(id=unknown)\n");
763                     goto balderdash;
764                 }
765             if(is_single_argtype(ret.func->argtype))
766                 argtype = 0;
767             else if(is_double_argtype(ret.func->argtype))
768                 argtype = 1;
769             if(is_single_rettype(ret.func->rettype))
770                 rettype = 0;
771             else if(is_double_rettype(ret.func->rettype))
772                 rettype = 1;
773             //ret.size = sizes[ret.func];
774             if (verbose) printf("(name=%s) (size=%d)\n", ret.func->name, ret.func->argtype);
775             break;
776         case k_op1:
777         case k_op1r:
778             n = do_op(q,ret.op1r,"op1r",2,argtype);
779             if (n < 1)
780                 goto balderdash;
781             break;
782         case k_op1i:
783             n = do_op(q,ret.op1i,"op1i",2,argtype);
784             if (n < 1)
785                 goto balderdash;
786             break;
787         case k_op2:
788         case k_op2r:
789             n = do_op(q,ret.op2r,"op2r",2,argtype);
790             if (n < 1)
791                 goto balderdash;
792             break;
793         case k_op2i:
794             n = do_op(q,ret.op2i,"op2i",2,argtype);
795             if (n < 1)
796                 goto balderdash;
797             break;
798         case k_resultc:
799             puts(q);
800             if(strncmp(q,"inf",3)==0) {
801                 ret.resultc = rc_infinity;
802             } else if(strcmp(q,"zero")==0) {
803                 ret.resultc = rc_zero;
804             } else if(strcmp(q,"nan")==0) {
805                 ret.resultc = rc_nan;
806             } else if(strcmp(q,"finite")==0) {
807                 ret.resultc = rc_finite;
808             } else {
809                 goto balderdash;
810             }
811             break;
812         case k_result:
813         case k_resultr:
814             n = (do_op)(q,ret.resultr,"resultr",3,rettype);
815             if (n < 1)
816                 goto balderdash;
817             ret.nresult = n; /* assume real and imaginary have same no. words */
818             break;
819         case k_resulti:
820             n = do_op(q,ret.resulti,"resulti",3,rettype);
821             if (n < 1)
822                 goto balderdash;
823             break;
824         case k_res2:
825             n = do_op(q,ret.res2,"res2",2,rettype);
826             if (n < 1)
827                 goto balderdash;
828             break;
829         case k_status:
830             while (*q) {
831                 if (*q == 'i') ret.status |= FE_INVALID;
832                 if (*q == 'z') ret.status |= FE_DIVBYZERO;
833                 if (*q == 'o') ret.status |= FE_OVERFLOW;
834                 if (*q == 'u') ret.status |= FE_UNDERFLOW;
835                 q++;
836             }
837             break;
838         case k_maybeerror:
839             n = find(q, errors, sizeof(errors));
840             if (n < 0)
841                 goto balderdash;
842             if(math_errhandling&MATH_ERREXCEPT) {
843                 switch(n) {
844                 case e_domain: ret.maybestatus |= FE_INVALID; break;
845                 case e_divbyzero: ret.maybestatus |= FE_DIVBYZERO; break;
846                 case e_overflow: ret.maybestatus |= FE_OVERFLOW; break;
847                 case e_underflow: ret.maybestatus |= FE_UNDERFLOW; break;
848                 }
849             }
850             {
851                 switch(n) {
852                 case e_domain:
853                     ret.maybeerr = e_EDOM; break;
854                 case e_divbyzero:
855                 case e_overflow:
856                 case e_underflow:
857                     ret.maybeerr = e_ERANGE; break;
858                 }
859             }
860         case k_maybestatus:
861             while (*q) {
862                 if (*q == 'i') ret.maybestatus |= FE_INVALID;
863                 if (*q == 'z') ret.maybestatus |= FE_DIVBYZERO;
864                 if (*q == 'o') ret.maybestatus |= FE_OVERFLOW;
865                 if (*q == 'u') ret.maybestatus |= FE_UNDERFLOW;
866                 q++;
867             }
868             break;
869         case k_error:
870             n = find(q, errors, sizeof(errors));
871             if (n < 0)
872                 goto balderdash;
873             if(math_errhandling&MATH_ERREXCEPT) {
874                 switch(n) {
875                 case e_domain: ret.status |= FE_INVALID; break;
876                 case e_divbyzero: ret.status |= FE_DIVBYZERO; break;
877                 case e_overflow: ret.status |= FE_OVERFLOW; break;
878                 case e_underflow: ret.status |= FE_UNDERFLOW; break;
879                 }
880             }
881             if(math_errhandling&MATH_ERRNO) {
882                 switch(n) {
883                 case e_domain:
884                     ret.err = e_EDOM; break;
885                 case e_divbyzero:
886                 case e_overflow:
887                 case e_underflow:
888                     ret.err = e_ERANGE; break;
889                 }
890             }
891             if(!(math_errhandling&MATH_ERRNO)) {
892                 switch(n) {
893                 case e_domain:
894                     ret.maybeerr = e_EDOM; break;
895                 case e_divbyzero:
896                 case e_overflow:
897                 case e_underflow:
898                     ret.maybeerr = e_ERANGE; break;
899                 }
900             }
901             break;
902         case k_errno:
903             ret.err = find(q, errnos, sizeof(errnos));
904             if (ret.err < 0)
905                 goto balderdash;
906             break;
907         case k_errno_in:
908             ret.in_err = find(q, errnos, sizeof(errnos));
909             if (ret.err < 0)
910                 goto balderdash;
911             ret.in_err_limit = ret.in_err + 1;
912             break;
913         case k_wrongresult:
914         case k_wrongstatus:
915         case k_wrongres2:
916         case k_wrongerrno:
917             /* quietly ignore these keys */
918             break;
919         default:
920             goto balderdash;
921         }
922         p = strtok(NULL, " \t");
923     }
924     ret.valid = 1;
925     return ret;
926 
927     /* come here from almost any error */
928  balderdash:
929     ret.valid = 0;
930     return ret;
931 }
932 
933 typedef enum {
934     test_comment,                      /* deliberately not a test */
935     test_invalid,                      /* accidentally not a test */
936     test_decline,                      /* was a test, and wasn't run */
937     test_fail,                         /* was a test, and failed */
938     test_pass                          /* was a test, and passed */
939 } testresult;
940 
941 char failtext[512];
942 
943 typedef union {
944     unsigned i[2];
945     double f;
946     double da[2];
947 } dbl;
948 
949 typedef union {
950     unsigned i;
951     float f;
952     float da[2];
953 } sgl;
954 
955 /* helper function for runtest */
956 void print_error(int rettype, unsigned *result, char* text, char** failp) {
957     special_op *sop;
958     char *str;
959 
960     if(result) {
961         *failp += sprintf(*failp," %s=",text);
962         sop = find_special_op_from_op(result[0],result[1],is_double_rettype(rettype));
963         if(sop) {
964             *failp += sprintf(*failp,"%s",sop->name);
965         } else {
966             if(is_double_rettype(rettype)) {
967                 str="%08x.%08x";
968             } else {
969                 str="%08x";
970             }
971             *failp += sprintf(*failp,str,result[0],result[1]);
972         }
973     }
974 }
975 
976 
977 void print_ulps_helper(const char *name, long long ulps, char** failp) {
978     if(ulps == LLONG_MAX) {
979         *failp += sprintf(*failp, " %s=HUGE", name);
980     } else {
981         *failp += sprintf(*failp, " %s=%.3f", name, (double)ulps / ULPUNIT);
982     }
983 }
984 
985 /* for complex args make ulpsr or ulpsri = 0 to not print */
986 void print_ulps(int rettype, long long ulpsr, long long ulpsi, char** failp) {
987     if(is_complex_rettype(rettype)) {
988         if (ulpsr) print_ulps_helper("ulpsr",ulpsr,failp);
989         if (ulpsi) print_ulps_helper("ulpsi",ulpsi,failp);
990     } else {
991         if (ulpsr) print_ulps_helper("ulps",ulpsr,failp);
992     }
993 }
994 
995 int runtest(testdetail t) {
996     int err, status;
997 
998     dbl d_arg1, d_arg2, d_res, d_res2;
999     sgl s_arg1, s_arg2, s_res, s_res2;
1000 
1001     int deferred_decline = FALSE;
1002     char *failp = failtext;
1003 
1004     unsigned int intres=0;
1005 
1006     int res2_adjust = 0;
1007 
1008     if (t.comment)
1009         return test_comment;
1010     if (!t.valid)
1011         return test_invalid;
1012 
1013     /* Set IEEE status to mathlib-normal */
1014     feclearexcept(FE_ALL_EXCEPT);
1015 
1016     /* Deal with operands */
1017 #define DO_DOP(arg,op) arg.i[dmsd] = t.op[0]; arg.i[dlsd] = t.op[1]
1018     DO_DOP(d_arg1,op1r);
1019     DO_DOP(d_arg2,op2r);
1020     s_arg1.i = t.op1r[0]; s_arg2.i = t.op2r[0];
1021 
1022     /*
1023      * Detect NaNs, infinities and denormals on input, and set a
1024      * deferred decline flag if we're in FO mode.
1025      *
1026      * (We defer the decline rather than doing it immediately
1027      * because even in FO mode the operation is not permitted to
1028      * crash or tight-loop; so we _run_ the test, and then ignore
1029      * all the results.)
1030      */
1031     if (fo) {
1032         if (is_double_argtype(t.func->argtype) && is_dhard(t.op1r))
1033             deferred_decline = TRUE;
1034         if (t.func->argtype==at_d2 && is_dhard(t.op2r))
1035             deferred_decline = TRUE;
1036         if (is_single_argtype(t.func->argtype) && is_shard(t.op1r))
1037             deferred_decline = TRUE;
1038         if (t.func->argtype==at_s2 && is_shard(t.op2r))
1039             deferred_decline = TRUE;
1040         if (is_double_rettype(t.func->rettype) && is_dhard(t.resultr))
1041             deferred_decline = TRUE;
1042         if (t.func->rettype==rt_d2 && is_dhard(t.res2))
1043             deferred_decline = TRUE;
1044         if (is_single_argtype(t.func->rettype) && is_shard(t.resultr))
1045             deferred_decline = TRUE;
1046         if (t.func->rettype==rt_s2 && is_shard(t.res2))
1047             deferred_decline = TRUE;
1048         if (t.err == e_ERANGE)
1049             deferred_decline = TRUE;
1050     }
1051 
1052     /*
1053      * Perform the operation
1054      */
1055 
1056     errno = t.in_err == e_EDOM ? EDOM : t.in_err == e_ERANGE ? ERANGE : 0;
1057     if (t.err == e_0)
1058         t.err = t.in_err;
1059     if (t.maybeerr == e_0)
1060         t.maybeerr = t.in_err;
1061 
1062     if(t.func->type == t_func) {
1063         switch(t.func->argtype) {
1064         case at_d: d_res.f = t.func->func.d_d_ptr(d_arg1.f); break;
1065         case at_s: s_res.f = t.func->func.s_s_ptr(s_arg1.f); break;
1066         case at_d2: d_res.f = t.func->func.d2_d_ptr(d_arg1.f, d_arg2.f); break;
1067         case at_s2: s_res.f = t.func->func.s2_s_ptr(s_arg1.f, s_arg2.f); break;
1068         case at_di: d_res.f = t.func->func.di_d_ptr(d_arg1.f, d_arg2.i[dmsd]); break;
1069         case at_si: s_res.f = t.func->func.si_s_ptr(s_arg1.f, s_arg2.i); break;
1070         case at_dip: d_res.f = t.func->func.dip_d_ptr(d_arg1.f, (int*)&intres); break;
1071         case at_sip: s_res.f = t.func->func.sip_s_ptr(s_arg1.f, (int*)&intres); break;
1072         case at_ddp: d_res.f = t.func->func.ddp_d_ptr(d_arg1.f, &d_res2.f); break;
1073         case at_ssp: s_res.f = t.func->func.ssp_s_ptr(s_arg1.f, &s_res2.f); break;
1074         default:
1075             printf("unhandled function: %s\n",t.func->name);
1076             return test_fail;
1077         }
1078     } else {
1079         /* printf("macro: name=%s, num=%i, s1.i=0x%08x s1.f=%f\n",t.func->name, t.func->macro_name, s_arg1.i, (double)s_arg1.f); */
1080         switch(t.func->macro_name) {
1081         case m_isfinite: intres = isfinite(d_arg1.f); break;
1082         case m_isinf: intres = isinf(d_arg1.f); break;
1083         case m_isnan: intres = isnan(d_arg1.f); break;
1084         case m_isnormal: intres = isnormal(d_arg1.f); break;
1085         case m_signbit: intres = signbit(d_arg1.f); break;
1086         case m_fpclassify: intres = fpclassify(d_arg1.f); break;
1087         case m_isgreater: intres = isgreater(d_arg1.f, d_arg2.f); break;
1088         case m_isgreaterequal: intres = isgreaterequal(d_arg1.f, d_arg2.f); break;
1089         case m_isless: intres = isless(d_arg1.f, d_arg2.f); break;
1090         case m_islessequal: intres = islessequal(d_arg1.f, d_arg2.f); break;
1091         case m_islessgreater: intres = islessgreater(d_arg1.f, d_arg2.f); break;
1092         case m_isunordered: intres = isunordered(d_arg1.f, d_arg2.f); break;
1093 
1094         case m_isfinitef: intres = isfinite(s_arg1.f); break;
1095         case m_isinff: intres = isinf(s_arg1.f); break;
1096         case m_isnanf: intres = isnan(s_arg1.f); break;
1097         case m_isnormalf: intres = isnormal(s_arg1.f); break;
1098         case m_signbitf: intres = signbit(s_arg1.f); break;
1099         case m_fpclassifyf: intres = fpclassify(s_arg1.f); break;
1100         case m_isgreaterf: intres = isgreater(s_arg1.f, s_arg2.f); break;
1101         case m_isgreaterequalf: intres = isgreaterequal(s_arg1.f, s_arg2.f); break;
1102         case m_islessf: intres = isless(s_arg1.f, s_arg2.f); break;
1103         case m_islessequalf: intres = islessequal(s_arg1.f, s_arg2.f); break;
1104         case m_islessgreaterf: intres = islessgreater(s_arg1.f, s_arg2.f); break;
1105         case m_isunorderedf: intres = isunordered(s_arg1.f, s_arg2.f); break;
1106 
1107         default:
1108             printf("unhandled macro: %s\n",t.func->name);
1109             return test_fail;
1110         }
1111     }
1112 
1113     /*
1114      * Decline the test if the deferred decline flag was set above.
1115      */
1116     if (deferred_decline)
1117         return test_decline;
1118 
1119     /* printf("intres=%i\n",intres); */
1120 
1121     /* Clear the fail text (indicating a pass unless we change it) */
1122     failp[0] = '\0';
1123 
1124     /* Check the IEEE status bits (except INX, which we disregard).
1125      * We don't bother with this for complex numbers, because the
1126      * complex functions are hard to get exactly right and we don't
1127      * have to anyway (C99 annex G is only informative). */
1128     if (!(is_complex_argtype(t.func->argtype) || is_complex_rettype(t.func->rettype))) {
1129         status = fetestexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW);
1130         if ((status|t.maybestatus|~statusmask) != (t.status|t.maybestatus|~statusmask)) {
1131             if (quiet) failtext[0]='x';
1132             else {
1133                 failp += sprintf(failp,
1134                                  " wrongstatus=%s%s%s%s%s",
1135                                  (status & FE_INVALID ? "i" : ""),
1136                                  (status & FE_DIVBYZERO ? "z" : ""),
1137                                  (status & FE_OVERFLOW ? "o" : ""),
1138                                  (status & FE_UNDERFLOW ? "u" : ""),
1139                                  (status ? "" : "OK"));
1140             }
1141         }
1142     }
1143 
1144     /* Check the result */
1145     {
1146         unsigned resultr[2], resulti[2];
1147         unsigned tresultr[3], tresulti[3], wres;
1148 
1149         switch(t.func->rettype) {
1150         case rt_d:
1151         case rt_d2:
1152             tresultr[0] = t.resultr[0];
1153             tresultr[1] = t.resultr[1];
1154             resultr[0] = d_res.i[dmsd]; resultr[1] = d_res.i[dlsd];
1155             wres = 2;
1156             break;
1157         case rt_i:
1158             tresultr[0] = t.resultr[0];
1159             resultr[0] = intres;
1160             wres = 1;
1161             break;
1162         case rt_s:
1163         case rt_s2:
1164             tresultr[0] = t.resultr[0];
1165             resultr[0] = s_res.i;
1166             wres = 1;
1167             break;
1168         default:
1169             puts("unhandled rettype in runtest");
1170             wres = 0;
1171         }
1172         if(t.resultc != rc_none) {
1173             int err = 0;
1174             switch(t.resultc) {
1175             case rc_zero:
1176                 if(resultr[0] != 0 || resulti[0] != 0 ||
1177                    (wres==2 && (resultr[1] != 0 || resulti[1] != 0))) {
1178                     err = 1;
1179                 }
1180                 break;
1181             case rc_infinity:
1182                 if(wres==1) {
1183                     if(!((resultr[0]&0x7fffffff)==0x7f800000 ||
1184                          (resulti[0]&0x7fffffff)==0x7f800000)) {
1185                         err = 1;
1186                     }
1187                 } else {
1188                   if(!(((resultr[0]&0x7fffffff)==0x7ff00000 && resultr[1]==0) ||
1189                        ((resulti[0]&0x7fffffff)==0x7ff00000 && resulti[1]==0))) {
1190                         err = 1;
1191                     }
1192                 }
1193                 break;
1194             case rc_nan:
1195                 if(wres==1) {
1196                     if(!((resultr[0]&0x7fffffff)>0x7f800000 ||
1197                          (resulti[0]&0x7fffffff)>0x7f800000)) {
1198                         err = 1;
1199                     }
1200                 } else {
1201                     canon_dNaN(resultr);
1202                     canon_dNaN(resulti);
1203                     if(!(((resultr[0]&0x7fffffff)>0x7ff00000 && resultr[1]==1) ||
1204                          ((resulti[0]&0x7fffffff)>0x7ff00000 && resulti[1]==1))) {
1205                         err = 1;
1206                     }
1207                 }
1208                 break;
1209             case rc_finite:
1210                 if(wres==1) {
1211                     if(!((resultr[0]&0x7fffffff)<0x7f800000 ||
1212                          (resulti[0]&0x7fffffff)<0x7f800000)) {
1213                         err = 1;
1214                     }
1215                 } else {
1216                     if(!((resultr[0]&0x7fffffff)<0x7ff00000 ||
1217                          (resulti[0]&0x7fffffff)<0x7ff00000)) {
1218                         err = 1;
1219                     }
1220                 }
1221                 break;
1222             default:
1223                 break;
1224             }
1225             if(err) {
1226                 print_error(t.func->rettype,resultr,"wrongresultr",&failp);
1227                 print_error(t.func->rettype,resulti,"wrongresulti",&failp);
1228             }
1229         } else if (t.nresult > wres) {
1230             /*
1231              * The test case data has provided the result to more
1232              * than double precision. Instead of testing exact
1233              * equality, we test against our maximum error
1234              * tolerance.
1235              */
1236             int rshift, ishift;
1237             long long ulpsr, ulpsi, ulptolerance;
1238 
1239             tresultr[wres] = t.resultr[wres] << (32-EXTRABITS);
1240             tresulti[wres] = t.resulti[wres] << (32-EXTRABITS);
1241             if(strict) {
1242                 ulptolerance = 4096; /* one ulp */
1243             } else {
1244                 ulptolerance = t.func->tolerance;
1245             }
1246             rshift = ishift = 0;
1247             if (ulptolerance & ABSLOWERBOUND) {
1248                 /*
1249                  * Hack for the lgamma functions, which have an
1250                  * error behaviour that can't conveniently be
1251                  * characterised in pure ULPs. Really, we want to
1252                  * say that the error in lgamma is "at most N ULPs,
1253                  * or at most an absolute error of X, whichever is
1254                  * larger", for appropriately chosen N,X. But since
1255                  * these two functions are the only cases where it
1256                  * arises, I haven't bothered to do it in a nice way
1257                  * in the function table above.
1258                  *
1259                  * (The difficult cases arise with negative input
1260                  * values such that |gamma(x)| is very near to 1; in
1261                  * this situation implementations tend to separately
1262                  * compute lgamma(|x|) and the log of the correction
1263                  * term from the Euler reflection formula, and
1264                  * subtract - which catastrophically loses
1265                  * significance.)
1266                  *
1267                  * As far as I can tell, nobody cares about this:
1268                  * GNU libm doesn't get those cases right either,
1269                  * and OpenCL explicitly doesn't state a ULP error
1270                  * limit for lgamma. So my guess is that this is
1271                  * simply considered acceptable error behaviour for
1272                  * this particular function, and hence I feel free
1273                  * to allow for it here.
1274                  */
1275                 ulptolerance &= ~ABSLOWERBOUND;
1276                 if (t.op1r[0] & 0x80000000) {
1277                     if (t.func->rettype == rt_d)
1278                         rshift = 0x400 - ((tresultr[0] >> 20) & 0x7ff);
1279                     else if (t.func->rettype == rt_s)
1280                         rshift = 0x80 - ((tresultr[0] >> 23) & 0xff);
1281                     if (rshift < 0)
1282                         rshift = 0;
1283                 }
1284             }
1285             if (ulptolerance & PLUSMINUSPIO2) {
1286                 ulptolerance &= ~PLUSMINUSPIO2;
1287                 /*
1288                  * Hack for range reduction, which can reduce
1289                  * borderline cases in the wrong direction, i.e.
1290                  * return a value just outside one end of the interval
1291                  * [-pi/4,+pi/4] when it could have returned a value
1292                  * just inside the other end by subtracting an
1293                  * adjacent multiple of pi/2.
1294                  *
1295                  * We tolerate this, up to a point, because the
1296                  * trigonometric functions making use of the output of
1297                  * rred can cope and because making the range reducer
1298                  * do the exactly right thing in every case would be
1299                  * more expensive.
1300                  */
1301                 if (wres == 1) {
1302                     /* Upper bound of overshoot derived in rredf.h */
1303                     if ((resultr[0]&0x7FFFFFFF) <= 0x3f494b02 &&
1304                         (resultr[0]&0x7FFFFFFF) > 0x3f490fda &&
1305                         (resultr[0]&0x80000000) != (tresultr[0]&0x80000000)) {
1306                         unsigned long long val;
1307                         val = tresultr[0];
1308                         val = (val << 32) | tresultr[1];
1309                         /*
1310                          * Compute the alternative permitted result by
1311                          * subtracting from the sum of the extended
1312                          * single-precision bit patterns of +pi/4 and
1313                          * -pi/4. This is a horrible hack which only
1314                          * works because we can be confident that
1315                          * numbers in this range all have the same
1316                          * exponent!
1317                          */
1318                         val = 0xfe921fb54442d184ULL - val;
1319                         tresultr[0] = val >> 32;
1320                         tresultr[1] = (val >> (32-EXTRABITS)) << (32-EXTRABITS);
1321                         /*
1322                          * Also, expect a correspondingly different
1323                          * value of res2 as a result of this change.
1324                          * The adjustment depends on whether we just
1325                          * flipped the result from + to - or vice
1326                          * versa.
1327                          */
1328                         if (resultr[0] & 0x80000000) {
1329                             res2_adjust = +1;
1330                         } else {
1331                             res2_adjust = -1;
1332                         }
1333                     }
1334                 }
1335             }
1336             ulpsr = calc_error(resultr, tresultr, rshift, t.func->rettype);
1337             if(is_complex_rettype(t.func->rettype)) {
1338                 ulpsi = calc_error(resulti, tresulti, ishift, t.func->rettype);
1339             } else {
1340                 ulpsi = 0;
1341             }
1342             unsigned *rr = (ulpsr > ulptolerance || ulpsr < -ulptolerance) ? resultr : NULL;
1343             unsigned *ri = (ulpsi > ulptolerance || ulpsi < -ulptolerance) ? resulti : NULL;
1344 /*             printf("tolerance=%i, ulpsr=%i, ulpsi=%i, rr=%p, ri=%p\n",ulptolerance,ulpsr,ulpsi,rr,ri); */
1345             if (rr || ri) {
1346                 if (quiet) failtext[0]='x';
1347                 else {
1348                     print_error(t.func->rettype,rr,"wrongresultr",&failp);
1349                     print_error(t.func->rettype,ri,"wrongresulti",&failp);
1350                     print_ulps(t.func->rettype,rr ? ulpsr : 0, ri ? ulpsi : 0,&failp);
1351                 }
1352             }
1353         } else {
1354             if(is_complex_rettype(t.func->rettype))
1355                 /*
1356                  * Complex functions are not fully supported,
1357                  * this is unreachable, but prevents warnings.
1358                  */
1359                 abort();
1360             /*
1361              * The test case data has provided the result in
1362              * exactly the output precision. Therefore we must
1363              * complain about _any_ violation.
1364              */
1365             switch(t.func->rettype) {
1366             case rt_dc:
1367                 canon_dNaN(tresulti);
1368                 canon_dNaN(resulti);
1369                 if (fo) {
1370                     dnormzero(tresulti);
1371                     dnormzero(resulti);
1372                 }
1373                 /* deliberate fall-through */
1374             case rt_d:
1375                 canon_dNaN(tresultr);
1376                 canon_dNaN(resultr);
1377                 if (fo) {
1378                     dnormzero(tresultr);
1379                     dnormzero(resultr);
1380                 }
1381                 break;
1382             case rt_sc:
1383                 canon_sNaN(tresulti);
1384                 canon_sNaN(resulti);
1385                 if (fo) {
1386                     snormzero(tresulti);
1387                     snormzero(resulti);
1388                 }
1389                 /* deliberate fall-through */
1390             case rt_s:
1391                 canon_sNaN(tresultr);
1392                 canon_sNaN(resultr);
1393                 if (fo) {
1394                     snormzero(tresultr);
1395                     snormzero(resultr);
1396                 }
1397                 break;
1398             default:
1399                 break;
1400             }
1401             if(is_complex_rettype(t.func->rettype)) {
1402                 unsigned *rr, *ri;
1403                 if(resultr[0] != tresultr[0] ||
1404                    (wres > 1 && resultr[1] != tresultr[1])) {
1405                     rr = resultr;
1406                 } else {
1407                     rr = NULL;
1408                 }
1409                 if(resulti[0] != tresulti[0] ||
1410                    (wres > 1 && resulti[1] != tresulti[1])) {
1411                     ri = resulti;
1412                 } else {
1413                     ri = NULL;
1414                 }
1415                 if(rr || ri) {
1416                     if (quiet) failtext[0]='x';
1417                     print_error(t.func->rettype,rr,"wrongresultr",&failp);
1418                     print_error(t.func->rettype,ri,"wrongresulti",&failp);
1419                 }
1420             } else if (resultr[0] != tresultr[0] ||
1421                        (wres > 1 && resultr[1] != tresultr[1])) {
1422                 if (quiet) failtext[0]='x';
1423                 print_error(t.func->rettype,resultr,"wrongresult",&failp);
1424             }
1425         }
1426         /*
1427          * Now test res2, for those functions (frexp, modf, rred)
1428          * which use it.
1429          */
1430         if (t.func->func.ptr == &frexp || t.func->func.ptr == &frexpf ||
1431             t.func->macro_name == m_rred || t.func->macro_name == m_rredf) {
1432             unsigned tres2 = t.res2[0];
1433             if (res2_adjust) {
1434                 /* Fix for range reduction, propagated from further up */
1435                 tres2 = (tres2 + res2_adjust) & 3;
1436             }
1437             if (tres2 != intres) {
1438                 if (quiet) failtext[0]='x';
1439                 else {
1440                     failp += sprintf(failp,
1441                                      " wrongres2=%08x", intres);
1442                 }
1443             }
1444         } else if (t.func->func.ptr == &modf || t.func->func.ptr == &modff) {
1445             tresultr[0] = t.res2[0];
1446             tresultr[1] = t.res2[1];
1447             if (is_double_rettype(t.func->rettype)) {
1448                 canon_dNaN(tresultr);
1449                 resultr[0] = d_res2.i[dmsd];
1450                 resultr[1] = d_res2.i[dlsd];
1451                 canon_dNaN(resultr);
1452                 if (fo) {
1453                     dnormzero(tresultr);
1454                     dnormzero(resultr);
1455                 }
1456             } else {
1457                 canon_sNaN(tresultr);
1458                 resultr[0] = s_res2.i;
1459                 resultr[1] = s_res2.i;
1460                 canon_sNaN(resultr);
1461                 if (fo) {
1462                     snormzero(tresultr);
1463                     snormzero(resultr);
1464                 }
1465             }
1466             if (resultr[0] != tresultr[0] ||
1467                 (wres > 1 && resultr[1] != tresultr[1])) {
1468                 if (quiet) failtext[0]='x';
1469                 else {
1470                     if (is_double_rettype(t.func->rettype))
1471                         failp += sprintf(failp, " wrongres2=%08x.%08x",
1472                                          resultr[0], resultr[1]);
1473                     else
1474                         failp += sprintf(failp, " wrongres2=%08x",
1475                                          resultr[0]);
1476                 }
1477             }
1478         }
1479     }
1480 
1481     /* Check errno */
1482     err = (errno == EDOM ? e_EDOM : errno == ERANGE ? e_ERANGE : e_0);
1483     if (err != t.err && err != t.maybeerr) {
1484         if (quiet) failtext[0]='x';
1485         else {
1486             failp += sprintf(failp, " wrongerrno=%s expecterrno=%s ", errnos[err], errnos[t.err]);
1487         }
1488     }
1489 
1490     return *failtext ? test_fail : test_pass;
1491 }
1492 
1493 int passed, failed, declined;
1494 
1495 void runtests(char *name, FILE *fp) {
1496     char testbuf[512], linebuf[512];
1497     int lineno = 1;
1498     testdetail test;
1499 
1500     test.valid = 0;
1501 
1502     if (verbose) printf("runtests: %s\n", name);
1503     while (fgets(testbuf, sizeof(testbuf), fp)) {
1504         int res, print_errno;
1505         testbuf[strcspn(testbuf, "\r\n")] = '\0';
1506         strcpy(linebuf, testbuf);
1507         test = parsetest(testbuf, test);
1508         print_errno = 0;
1509         while (test.in_err < test.in_err_limit) {
1510             res = runtest(test);
1511             if (res == test_pass) {
1512                 if (verbose)
1513                     printf("%s:%d: pass\n", name, lineno);
1514                 ++passed;
1515             } else if (res == test_decline) {
1516                 if (verbose)
1517                     printf("%s:%d: declined\n", name, lineno);
1518                 ++declined;
1519             } else if (res == test_fail) {
1520                 if (!quiet)
1521                     printf("%s:%d: FAIL%s: %s%s%s%s\n", name, lineno,
1522                            test.random ? " (random)" : "",
1523                            linebuf,
1524                            print_errno ? " errno_in=" : "",
1525                            print_errno ? errnos[test.in_err] : "",
1526                            failtext);
1527                 ++failed;
1528             } else if (res == test_invalid) {
1529                 printf("%s:%d: malformed: %s\n", name, lineno, linebuf);
1530                 ++failed;
1531             }
1532             test.in_err++;
1533             print_errno = 1;
1534         }
1535         lineno++;
1536     }
1537 }
1538 
1539 int main(int ac, char **av) {
1540     char **files;
1541     int i, nfiles = 0;
1542     dbl d;
1543 
1544 #ifdef MICROLIB
1545     /*
1546      * Invent argc and argv ourselves.
1547      */
1548     char *argv[256];
1549     char args[256];
1550     {
1551         int sargs[2];
1552         char *p;
1553 
1554         ac = 0;
1555 
1556         sargs[0]=(int)args;
1557         sargs[1]=(int)sizeof(args);
1558         if (!__semihost(0x15, sargs)) {
1559             args[sizeof(args)-1] = '\0';   /* just in case */
1560             p = args;
1561             while (1) {
1562                 while (*p == ' ' || *p == '\t') p++;
1563                 if (!*p) break;
1564                 argv[ac++] = p;
1565                 while (*p && *p != ' ' && *p != '\t') p++;
1566                 if (*p) *p++ = '\0';
1567             }
1568         }
1569 
1570         av = argv;
1571     }
1572 #endif
1573 
1574     /* Sort tfuncs */
1575     qsort(tfuncs, sizeof(tfuncs)/sizeof(test_func), sizeof(test_func), &compare_tfuncs);
1576 
1577     /*
1578      * Autodetect the `double' endianness.
1579      */
1580     dmsd = 0;
1581     d.f = 1.0;                       /* 0x3ff00000 / 0x00000000 */
1582     if (d.i[dmsd] == 0) {
1583         dmsd = 1;
1584     }
1585     /*
1586      * Now dmsd denotes what the compiler thinks we're at. Let's
1587      * check that it agrees with what the runtime thinks.
1588      */
1589     d.i[0] = d.i[1] = 0x11111111;/* a random +ve number */
1590     d.f /= d.f;                    /* must now be one */
1591     if (d.i[dmsd] == 0) {
1592         fprintf(stderr, "YIKES! Compiler and runtime disagree on endianness"
1593                 " of `double'. Bailing out\n");
1594         return 1;
1595     }
1596     dlsd = !dmsd;
1597 
1598     /* default is terse */
1599     verbose = 0;
1600     fo = 0;
1601     strict = 0;
1602 
1603     files = (char **)malloc((ac+1) * sizeof(char *));
1604     if (!files) {
1605         fprintf(stderr, "initial malloc failed!\n");
1606         return 1;
1607     }
1608 #ifdef NOCMDLINE
1609     files[nfiles++] = "testfile";
1610 #endif
1611 
1612     while (--ac) {
1613         char *p = *++av;
1614         if (*p == '-') {
1615             static char *options[] = {
1616                 "-fo",
1617 #if 0
1618                 "-noinexact",
1619                 "-noround",
1620 #endif
1621                 "-nostatus",
1622                 "-quiet",
1623                 "-strict",
1624                 "-v",
1625                 "-verbose",
1626             };
1627             enum {
1628                 op_fo,
1629 #if 0
1630                 op_noinexact,
1631                 op_noround,
1632 #endif
1633                 op_nostatus,
1634                 op_quiet,
1635                 op_strict,
1636                 op_v,
1637                 op_verbose,
1638             };
1639             switch (find(p, options, sizeof(options))) {
1640             case op_quiet:
1641                 quiet = 1;
1642                 break;
1643 #if 0
1644             case op_noinexact:
1645                 statusmask &= 0x0F;    /* remove bit 4 */
1646                 break;
1647             case op_noround:
1648                 doround = 0;
1649                 break;
1650 #endif
1651             case op_nostatus:        /* no status word => noinx,noround */
1652                 statusmask = 0;
1653                 doround = 0;
1654                 break;
1655             case op_v:
1656             case op_verbose:
1657                 verbose = 1;
1658                 break;
1659             case op_fo:
1660                 fo = 1;
1661                 break;
1662             case op_strict: /* tolerance is 1 ulp */
1663                 strict = 1;
1664                 break;
1665             default:
1666                 fprintf(stderr, "unrecognised option: %s\n", p);
1667                 break;
1668             }
1669         } else {
1670             files[nfiles++] = p;
1671         }
1672     }
1673 
1674     passed = failed = declined = 0;
1675 
1676     if (nfiles) {
1677         for (i = 0; i < nfiles; i++) {
1678             FILE *fp = fopen(files[i], "r");
1679             if (!fp) {
1680                 fprintf(stderr, "Couldn't open %s\n", files[i]);
1681             } else
1682                 runtests(files[i], fp);
1683         }
1684     } else
1685         runtests("(stdin)", stdin);
1686 
1687     printf("Completed. Passed %d, failed %d (total %d",
1688            passed, failed, passed+failed);
1689     if (declined)
1690         printf(" plus %d declined", declined);
1691     printf(")\n");
1692     if (failed || passed == 0)
1693         return 1;
1694     printf("** TEST PASSED OK **\n");
1695     return 0;
1696 }
1697 
1698 void undef_func() {
1699     failed++;
1700     puts("ERROR: undefined function called");
1701 }
1702