xref: /freebsd-src/contrib/arm-optimized-routines/math/test/mathtest.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
131914882SAlex Richardson /*
231914882SAlex Richardson  * mathtest.c - test rig for mathlib
331914882SAlex Richardson  *
4*f3087befSAndrew Turner  * Copyright (c) 1998-2024, Arm Limited.
5072a4ba8SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson  */
7*f3087befSAndrew Turner /* clang-format off */
831914882SAlex Richardson 
9*f3087befSAndrew Turner #define _GNU_SOURCE
1031914882SAlex Richardson #include <assert.h>
1131914882SAlex Richardson #include <stdio.h>
1231914882SAlex Richardson #include <stdlib.h>
1331914882SAlex Richardson #include <string.h>
1431914882SAlex Richardson #include <setjmp.h>
1531914882SAlex Richardson #include <ctype.h>
1631914882SAlex Richardson #include <math.h>
1731914882SAlex Richardson #include <errno.h>
1831914882SAlex Richardson #include <limits.h>
1931914882SAlex Richardson #include <fenv.h>
2031914882SAlex Richardson #include "mathlib.h"
2131914882SAlex Richardson 
2231914882SAlex Richardson #ifndef math_errhandling
2331914882SAlex Richardson # define math_errhandling 0
2431914882SAlex Richardson #endif
2531914882SAlex Richardson 
2631914882SAlex Richardson #ifdef __cplusplus
2731914882SAlex Richardson  #define EXTERN_C extern "C"
2831914882SAlex Richardson #else
2931914882SAlex Richardson  #define EXTERN_C extern
3031914882SAlex Richardson #endif
3131914882SAlex Richardson 
3231914882SAlex Richardson #ifndef TRUE
3331914882SAlex Richardson #define TRUE 1
3431914882SAlex Richardson #endif
3531914882SAlex Richardson #ifndef FALSE
3631914882SAlex Richardson #define FALSE 0
3731914882SAlex Richardson #endif
3831914882SAlex Richardson 
3931914882SAlex Richardson #ifdef IMPORT_SYMBOL
4031914882SAlex Richardson #define STR2(x) #x
4131914882SAlex Richardson #define STR(x) STR2(x)
4231914882SAlex Richardson _Pragma(STR(import IMPORT_SYMBOL))
4331914882SAlex Richardson #endif
4431914882SAlex Richardson 
4531914882SAlex Richardson int dmsd, dlsd;
4631914882SAlex Richardson int quiet = 0;
4731914882SAlex Richardson int doround = 0;
4831914882SAlex Richardson unsigned statusmask = FE_ALL_EXCEPT;
4931914882SAlex Richardson 
5031914882SAlex Richardson #define EXTRABITS (12)
5131914882SAlex Richardson #define ULPUNIT (1<<EXTRABITS)
5231914882SAlex Richardson 
5331914882SAlex Richardson typedef int (*test) (void);
5431914882SAlex Richardson 
5531914882SAlex Richardson /*
5631914882SAlex Richardson   struct to hold info about a function (which could actually be a macro)
5731914882SAlex Richardson */
5831914882SAlex Richardson typedef struct {
5931914882SAlex Richardson     enum {
6031914882SAlex Richardson         t_func, t_macro
6131914882SAlex Richardson     } type;
6231914882SAlex Richardson     enum {
6331914882SAlex Richardson         at_d, at_s,      /* double or single precision float */
6431914882SAlex Richardson         at_d2, at_s2,    /* same, but taking two args */
6531914882SAlex Richardson         at_di, at_si,    /* double/single and an int */
6631914882SAlex Richardson         at_dip, at_sip,  /* double/single and an int ptr */
6731914882SAlex Richardson         at_ddp, at_ssp,  /* d/s and a d/s ptr */
6831914882SAlex Richardson         at_dc, at_sc,    /* double or single precision complex */
6931914882SAlex Richardson         at_dc2, at_sc2   /* same, but taking two args */
7031914882SAlex Richardson     } argtype;
7131914882SAlex Richardson     enum {
7231914882SAlex Richardson         rt_d, rt_s, rt_i, /* double, single, int */
7331914882SAlex Richardson         rt_dc, rt_sc,     /* double, single precision complex */
7431914882SAlex Richardson         rt_d2, rt_s2      /* also use res2 */
7531914882SAlex Richardson     } rettype;
7631914882SAlex Richardson     union {
7731914882SAlex Richardson         void* ptr;
7831914882SAlex Richardson         double (*d_d_ptr)(double);
7931914882SAlex Richardson         float (*s_s_ptr)(float);
8031914882SAlex Richardson         int (*d_i_ptr)(double);
8131914882SAlex Richardson         int (*s_i_ptr)(float);
8231914882SAlex Richardson         double (*d2_d_ptr)(double, double);
8331914882SAlex Richardson         float (*s2_s_ptr)(float, float);
8431914882SAlex Richardson         double (*di_d_ptr)(double,int);
8531914882SAlex Richardson         float (*si_s_ptr)(float,int);
8631914882SAlex Richardson         double (*dip_d_ptr)(double,int*);
8731914882SAlex Richardson         float (*sip_s_ptr)(float,int*);
8831914882SAlex Richardson         double (*ddp_d_ptr)(double,double*);
8931914882SAlex Richardson         float (*ssp_s_ptr)(float,float*);
9031914882SAlex Richardson     } func;
9131914882SAlex Richardson     enum {
9231914882SAlex Richardson         m_none,
9331914882SAlex Richardson         m_isfinite, m_isfinitef,
9431914882SAlex Richardson         m_isgreater, m_isgreaterequal,
9531914882SAlex Richardson         m_isgreaterequalf, m_isgreaterf,
9631914882SAlex Richardson         m_isinf, m_isinff,
9731914882SAlex Richardson         m_isless, m_islessequal,
9831914882SAlex Richardson         m_islessequalf, m_islessf,
9931914882SAlex Richardson         m_islessgreater, m_islessgreaterf,
10031914882SAlex Richardson         m_isnan, m_isnanf,
10131914882SAlex Richardson         m_isnormal, m_isnormalf,
10231914882SAlex Richardson         m_isunordered, m_isunorderedf,
10331914882SAlex Richardson         m_fpclassify, m_fpclassifyf,
10431914882SAlex Richardson         m_signbit, m_signbitf,
10531914882SAlex Richardson         /* not actually a macro, but makes things easier */
10631914882SAlex Richardson         m_rred, m_rredf,
10731914882SAlex Richardson         m_cadd, m_csub, m_cmul, m_cdiv,
10831914882SAlex Richardson         m_caddf, m_csubf, m_cmulf, m_cdivf
10931914882SAlex Richardson     } macro_name; /* only used if a macro/something that can't be done using func */
11031914882SAlex Richardson     long long tolerance;
11131914882SAlex Richardson     const char* name;
11231914882SAlex Richardson } test_func;
11331914882SAlex Richardson 
11431914882SAlex Richardson /* used in qsort */
11531914882SAlex Richardson int compare_tfuncs(const void* a, const void* b) {
11631914882SAlex Richardson     return strcmp(((test_func*)a)->name, ((test_func*)b)->name);
11731914882SAlex Richardson }
11831914882SAlex Richardson 
11931914882SAlex Richardson int is_double_argtype(int argtype) {
12031914882SAlex Richardson     switch(argtype) {
12131914882SAlex Richardson     case at_d:
12231914882SAlex Richardson     case at_d2:
12331914882SAlex Richardson     case at_dc:
12431914882SAlex Richardson     case at_dc2:
12531914882SAlex Richardson         return 1;
12631914882SAlex Richardson     default:
12731914882SAlex Richardson         return 0;
12831914882SAlex Richardson     }
12931914882SAlex Richardson }
13031914882SAlex Richardson 
13131914882SAlex Richardson int is_single_argtype(int argtype) {
13231914882SAlex Richardson     switch(argtype) {
13331914882SAlex Richardson     case at_s:
13431914882SAlex Richardson     case at_s2:
13531914882SAlex Richardson     case at_sc:
13631914882SAlex Richardson     case at_sc2:
13731914882SAlex Richardson         return 1;
13831914882SAlex Richardson     default:
13931914882SAlex Richardson         return 0;
14031914882SAlex Richardson     }
14131914882SAlex Richardson }
14231914882SAlex Richardson 
14331914882SAlex Richardson int is_double_rettype(int rettype) {
14431914882SAlex Richardson     switch(rettype) {
14531914882SAlex Richardson     case rt_d:
14631914882SAlex Richardson     case rt_dc:
14731914882SAlex Richardson     case rt_d2:
14831914882SAlex Richardson         return 1;
14931914882SAlex Richardson     default:
15031914882SAlex Richardson         return 0;
15131914882SAlex Richardson     }
15231914882SAlex Richardson }
15331914882SAlex Richardson 
15431914882SAlex Richardson int is_single_rettype(int rettype) {
15531914882SAlex Richardson     switch(rettype) {
15631914882SAlex Richardson     case rt_s:
15731914882SAlex Richardson     case rt_sc:
15831914882SAlex Richardson     case rt_s2:
15931914882SAlex Richardson         return 1;
16031914882SAlex Richardson     default:
16131914882SAlex Richardson         return 0;
16231914882SAlex Richardson     }
16331914882SAlex Richardson }
16431914882SAlex Richardson 
16531914882SAlex Richardson int is_complex_argtype(int argtype) {
16631914882SAlex Richardson     switch(argtype) {
16731914882SAlex Richardson     case at_dc:
16831914882SAlex Richardson     case at_sc:
16931914882SAlex Richardson     case at_dc2:
17031914882SAlex Richardson     case at_sc2:
17131914882SAlex Richardson         return 1;
17231914882SAlex Richardson     default:
17331914882SAlex Richardson         return 0;
17431914882SAlex Richardson     }
17531914882SAlex Richardson }
17631914882SAlex Richardson 
17731914882SAlex Richardson int is_complex_rettype(int rettype) {
17831914882SAlex Richardson     switch(rettype) {
17931914882SAlex Richardson     case rt_dc:
18031914882SAlex Richardson     case rt_sc:
18131914882SAlex Richardson         return 1;
18231914882SAlex Richardson     default:
18331914882SAlex Richardson         return 0;
18431914882SAlex Richardson     }
18531914882SAlex Richardson }
18631914882SAlex Richardson 
18731914882SAlex Richardson /*
18831914882SAlex Richardson  * Special-case flags indicating that some functions' error
18931914882SAlex Richardson  * tolerance handling is more complicated than a fixed relative
19031914882SAlex Richardson  * error bound.
19131914882SAlex Richardson  */
19231914882SAlex Richardson #define ABSLOWERBOUND 0x4000000000000000LL
19331914882SAlex Richardson #define PLUSMINUSPIO2 0x1000000000000000LL
19431914882SAlex Richardson 
19531914882SAlex Richardson #define ARM_PREFIX(x) x
19631914882SAlex Richardson 
19731914882SAlex Richardson #define TFUNC(arg,ret,name,tolerance) { t_func, arg, ret, (void*)&name, m_none, tolerance, #name }
19831914882SAlex Richardson #define TFUNCARM(arg,ret,name,tolerance) { t_func, arg, ret, (void*)& ARM_PREFIX(name), m_none, tolerance, #name }
19931914882SAlex Richardson #define MFUNC(arg,ret,name,tolerance) { t_macro, arg, ret, NULL, m_##name, tolerance, #name }
20031914882SAlex Richardson 
20131914882SAlex Richardson /* sincosf wrappers for easier testing.  */
20231914882SAlex Richardson static float sincosf_sinf(float x) { float s,c; sincosf(x, &s, &c); return s; }
20331914882SAlex Richardson static float sincosf_cosf(float x) { float s,c; sincosf(x, &s, &c); return c; }
20431914882SAlex Richardson 
20531914882SAlex Richardson test_func tfuncs[] = {
20631914882SAlex Richardson     /* trigonometric */
20731914882SAlex Richardson     TFUNC(at_d,rt_d, acos, 4*ULPUNIT),
20831914882SAlex Richardson     TFUNC(at_d,rt_d, asin, 4*ULPUNIT),
20931914882SAlex Richardson     TFUNC(at_d,rt_d, atan, 4*ULPUNIT),
21031914882SAlex Richardson     TFUNC(at_d2,rt_d, atan2, 4*ULPUNIT),
21131914882SAlex Richardson 
21231914882SAlex Richardson     TFUNC(at_d,rt_d, tan, 2*ULPUNIT),
21331914882SAlex Richardson     TFUNC(at_d,rt_d, sin, 2*ULPUNIT),
21431914882SAlex Richardson     TFUNC(at_d,rt_d, cos, 2*ULPUNIT),
21531914882SAlex Richardson 
21631914882SAlex Richardson     TFUNC(at_s,rt_s, acosf, 4*ULPUNIT),
21731914882SAlex Richardson     TFUNC(at_s,rt_s, asinf, 4*ULPUNIT),
21831914882SAlex Richardson     TFUNC(at_s,rt_s, atanf, 4*ULPUNIT),
21931914882SAlex Richardson     TFUNC(at_s2,rt_s, atan2f, 4*ULPUNIT),
22031914882SAlex Richardson     TFUNCARM(at_s,rt_s, tanf, 4*ULPUNIT),
22131914882SAlex Richardson     TFUNCARM(at_s,rt_s, sinf, 3*ULPUNIT/4),
22231914882SAlex Richardson     TFUNCARM(at_s,rt_s, cosf, 3*ULPUNIT/4),
22331914882SAlex Richardson     TFUNCARM(at_s,rt_s, sincosf_sinf, 3*ULPUNIT/4),
22431914882SAlex Richardson     TFUNCARM(at_s,rt_s, sincosf_cosf, 3*ULPUNIT/4),
225*f3087befSAndrew Turner 
22631914882SAlex Richardson     /* hyperbolic */
22731914882SAlex Richardson     TFUNC(at_d, rt_d, atanh, 4*ULPUNIT),
22831914882SAlex Richardson     TFUNC(at_d, rt_d, asinh, 4*ULPUNIT),
22931914882SAlex Richardson     TFUNC(at_d, rt_d, acosh, 4*ULPUNIT),
23031914882SAlex Richardson     TFUNC(at_d,rt_d, tanh, 4*ULPUNIT),
23131914882SAlex Richardson     TFUNC(at_d,rt_d, sinh, 4*ULPUNIT),
23231914882SAlex Richardson     TFUNC(at_d,rt_d, cosh, 4*ULPUNIT),
23331914882SAlex Richardson 
23431914882SAlex Richardson     TFUNC(at_s, rt_s, atanhf, 4*ULPUNIT),
23531914882SAlex Richardson     TFUNC(at_s, rt_s, asinhf, 4*ULPUNIT),
23631914882SAlex Richardson     TFUNC(at_s, rt_s, acoshf, 4*ULPUNIT),
23731914882SAlex Richardson     TFUNC(at_s,rt_s, tanhf, 4*ULPUNIT),
23831914882SAlex Richardson     TFUNC(at_s,rt_s, sinhf, 4*ULPUNIT),
23931914882SAlex Richardson     TFUNC(at_s,rt_s, coshf, 4*ULPUNIT),
24031914882SAlex Richardson 
24131914882SAlex Richardson     /* exponential and logarithmic */
24231914882SAlex Richardson     TFUNC(at_d,rt_d, log, 3*ULPUNIT/4),
24331914882SAlex Richardson     TFUNC(at_d,rt_d, log10, 3*ULPUNIT),
24431914882SAlex Richardson     TFUNC(at_d,rt_d, log2, 3*ULPUNIT/4),
24531914882SAlex Richardson     TFUNC(at_d,rt_d, log1p, 2*ULPUNIT),
24631914882SAlex Richardson     TFUNC(at_d,rt_d, exp, 3*ULPUNIT/4),
24731914882SAlex Richardson     TFUNC(at_d,rt_d, exp2, 3*ULPUNIT/4),
24831914882SAlex Richardson     TFUNC(at_d,rt_d, expm1, ULPUNIT),
24931914882SAlex Richardson     TFUNCARM(at_s,rt_s, logf, ULPUNIT),
25031914882SAlex Richardson     TFUNC(at_s,rt_s, log10f, 3*ULPUNIT),
25131914882SAlex Richardson     TFUNCARM(at_s,rt_s, log2f, ULPUNIT),
25231914882SAlex Richardson     TFUNC(at_s,rt_s, log1pf, 2*ULPUNIT),
25331914882SAlex Richardson     TFUNCARM(at_s,rt_s, expf, 3*ULPUNIT/4),
25431914882SAlex Richardson     TFUNCARM(at_s,rt_s, exp2f, 3*ULPUNIT/4),
25531914882SAlex Richardson     TFUNC(at_s,rt_s, expm1f, ULPUNIT),
256*f3087befSAndrew Turner #if WANT_EXP10_TESTS
2575a02ffc3SAndrew Turner     TFUNC(at_d,rt_d, exp10, ULPUNIT),
258*f3087befSAndrew Turner #endif
25931914882SAlex Richardson 
26031914882SAlex Richardson     /* power */
26131914882SAlex Richardson     TFUNC(at_d2,rt_d, pow, 3*ULPUNIT/4),
26231914882SAlex Richardson     TFUNC(at_d,rt_d, sqrt, ULPUNIT/2),
26331914882SAlex Richardson     TFUNC(at_d,rt_d, cbrt, 2*ULPUNIT),
26431914882SAlex Richardson     TFUNC(at_d2, rt_d, hypot, 4*ULPUNIT),
26531914882SAlex Richardson 
26631914882SAlex Richardson     TFUNCARM(at_s2,rt_s, powf, ULPUNIT),
26731914882SAlex Richardson     TFUNC(at_s,rt_s, sqrtf, ULPUNIT/2),
26831914882SAlex Richardson     TFUNC(at_s,rt_s, cbrtf, 2*ULPUNIT),
26931914882SAlex Richardson     TFUNC(at_s2, rt_s, hypotf, 4*ULPUNIT),
27031914882SAlex Richardson 
27131914882SAlex Richardson     /* error function */
27231914882SAlex Richardson     TFUNC(at_d,rt_d, erf, 16*ULPUNIT),
27331914882SAlex Richardson     TFUNC(at_s,rt_s, erff, 16*ULPUNIT),
27431914882SAlex Richardson     TFUNC(at_d,rt_d, erfc, 16*ULPUNIT),
27531914882SAlex Richardson     TFUNC(at_s,rt_s, erfcf, 16*ULPUNIT),
27631914882SAlex Richardson 
27731914882SAlex Richardson     /* gamma functions */
27831914882SAlex Richardson     TFUNC(at_d,rt_d, tgamma, 16*ULPUNIT),
27931914882SAlex Richardson     TFUNC(at_s,rt_s, tgammaf, 16*ULPUNIT),
28031914882SAlex Richardson     TFUNC(at_d,rt_d, lgamma, 16*ULPUNIT | ABSLOWERBOUND),
28131914882SAlex Richardson     TFUNC(at_s,rt_s, lgammaf, 16*ULPUNIT | ABSLOWERBOUND),
28231914882SAlex Richardson 
28331914882SAlex Richardson     TFUNC(at_d,rt_d, ceil, 0),
28431914882SAlex Richardson     TFUNC(at_s,rt_s, ceilf, 0),
28531914882SAlex Richardson     TFUNC(at_d2,rt_d, copysign, 0),
28631914882SAlex Richardson     TFUNC(at_s2,rt_s, copysignf, 0),
28731914882SAlex Richardson     TFUNC(at_d,rt_d, floor, 0),
28831914882SAlex Richardson     TFUNC(at_s,rt_s, floorf, 0),
28931914882SAlex Richardson     TFUNC(at_d2,rt_d, fmax, 0),
29031914882SAlex Richardson     TFUNC(at_s2,rt_s, fmaxf, 0),
29131914882SAlex Richardson     TFUNC(at_d2,rt_d, fmin, 0),
29231914882SAlex Richardson     TFUNC(at_s2,rt_s, fminf, 0),
29331914882SAlex Richardson     TFUNC(at_d2,rt_d, fmod, 0),
29431914882SAlex Richardson     TFUNC(at_s2,rt_s, fmodf, 0),
29531914882SAlex Richardson     MFUNC(at_d, rt_i, fpclassify, 0),
29631914882SAlex Richardson     MFUNC(at_s, rt_i, fpclassifyf, 0),
29731914882SAlex Richardson     TFUNC(at_dip,rt_d, frexp, 0),
29831914882SAlex Richardson     TFUNC(at_sip,rt_s, frexpf, 0),
29931914882SAlex Richardson     MFUNC(at_d, rt_i, isfinite, 0),
30031914882SAlex Richardson     MFUNC(at_s, rt_i, isfinitef, 0),
30131914882SAlex Richardson     MFUNC(at_d, rt_i, isgreater, 0),
30231914882SAlex Richardson     MFUNC(at_d, rt_i, isgreaterequal, 0),
30331914882SAlex Richardson     MFUNC(at_s, rt_i, isgreaterequalf, 0),
30431914882SAlex Richardson     MFUNC(at_s, rt_i, isgreaterf, 0),
30531914882SAlex Richardson     MFUNC(at_d, rt_i, isinf, 0),
30631914882SAlex Richardson     MFUNC(at_s, rt_i, isinff, 0),
30731914882SAlex Richardson     MFUNC(at_d, rt_i, isless, 0),
30831914882SAlex Richardson     MFUNC(at_d, rt_i, islessequal, 0),
30931914882SAlex Richardson     MFUNC(at_s, rt_i, islessequalf, 0),
31031914882SAlex Richardson     MFUNC(at_s, rt_i, islessf, 0),
31131914882SAlex Richardson     MFUNC(at_d, rt_i, islessgreater, 0),
31231914882SAlex Richardson     MFUNC(at_s, rt_i, islessgreaterf, 0),
31331914882SAlex Richardson     MFUNC(at_d, rt_i, isnan, 0),
31431914882SAlex Richardson     MFUNC(at_s, rt_i, isnanf, 0),
31531914882SAlex Richardson     MFUNC(at_d, rt_i, isnormal, 0),
31631914882SAlex Richardson     MFUNC(at_s, rt_i, isnormalf, 0),
31731914882SAlex Richardson     MFUNC(at_d, rt_i, isunordered, 0),
31831914882SAlex Richardson     MFUNC(at_s, rt_i, isunorderedf, 0),
31931914882SAlex Richardson     TFUNC(at_di,rt_d, ldexp, 0),
32031914882SAlex Richardson     TFUNC(at_si,rt_s, ldexpf, 0),
32131914882SAlex Richardson     TFUNC(at_ddp,rt_d2, modf, 0),
32231914882SAlex Richardson     TFUNC(at_ssp,rt_s2, modff, 0),
32331914882SAlex Richardson #ifndef BIGRANGERED
32431914882SAlex Richardson     MFUNC(at_d, rt_d, rred, 2*ULPUNIT),
32531914882SAlex Richardson #else
32631914882SAlex Richardson     MFUNC(at_d, rt_d, m_rred, ULPUNIT),
32731914882SAlex Richardson #endif
32831914882SAlex Richardson     MFUNC(at_d, rt_i, signbit, 0),
32931914882SAlex Richardson     MFUNC(at_s, rt_i, signbitf, 0),
33031914882SAlex Richardson };
33131914882SAlex Richardson 
33231914882SAlex Richardson /*
33331914882SAlex Richardson  * keywords are: func size op1 op2 result res2 errno op1r op1i op2r op2i resultr resulti
33431914882SAlex Richardson  * also we ignore: wrongresult wrongres2 wrongerrno
33531914882SAlex Richardson  * op1 equivalent to op1r, same with op2 and result
33631914882SAlex Richardson  */
33731914882SAlex Richardson 
33831914882SAlex Richardson typedef struct {
33931914882SAlex Richardson     test_func *func;
34031914882SAlex Richardson     unsigned op1r[2]; /* real part, also used for non-complex numbers */
34131914882SAlex Richardson     unsigned op1i[2]; /* imaginary part */
34231914882SAlex Richardson     unsigned op2r[2];
34331914882SAlex Richardson     unsigned op2i[2];
34431914882SAlex Richardson     unsigned resultr[3];
34531914882SAlex Richardson     unsigned resulti[3];
34631914882SAlex Richardson     enum {
34731914882SAlex Richardson         rc_none, rc_zero, rc_infinity, rc_nan, rc_finite
34831914882SAlex Richardson     } resultc; /* special complex results, rc_none means use resultr and resulti as normal */
34931914882SAlex Richardson     unsigned res2[2];
35031914882SAlex Richardson     unsigned status;                   /* IEEE status return, if any */
35131914882SAlex Richardson     unsigned maybestatus;             /* for optional status, or allowance for spurious */
35231914882SAlex Richardson     int nresult;                       /* number of result words */
35331914882SAlex Richardson     int in_err, in_err_limit;
35431914882SAlex Richardson     int err;
35531914882SAlex Richardson     int maybeerr;
35631914882SAlex Richardson     int valid;
35731914882SAlex Richardson     int comment;
35831914882SAlex Richardson     int random;
35931914882SAlex Richardson } testdetail;
36031914882SAlex Richardson 
36131914882SAlex Richardson enum {                                 /* keywords */
36231914882SAlex Richardson     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,
36331914882SAlex Richardson     k_random, k_res2, k_result, k_resultc, k_resulti, k_resultr, k_status,
36431914882SAlex Richardson     k_wrongres2, k_wrongresult, k_wrongstatus, k_wrongerrno
36531914882SAlex Richardson };
36631914882SAlex Richardson char *keywords[] = {
36731914882SAlex Richardson     "errno", "errno_in", "error", "func", "maybeerror", "maybestatus", "op1", "op1i", "op1r", "op2", "op2i", "op2r",
36831914882SAlex Richardson     "random", "res2", "result", "resultc", "resulti", "resultr", "status",
36931914882SAlex Richardson     "wrongres2", "wrongresult", "wrongstatus", "wrongerrno"
37031914882SAlex Richardson };
37131914882SAlex Richardson 
37231914882SAlex Richardson enum {
37331914882SAlex Richardson     e_0, e_EDOM, e_ERANGE,
37431914882SAlex Richardson 
37531914882SAlex Richardson     /*
37631914882SAlex Richardson      * This enum makes sure that we have the right number of errnos in the
37731914882SAlex Richardson      * errno[] array
37831914882SAlex Richardson      */
37931914882SAlex Richardson     e_number_of_errnos
38031914882SAlex Richardson };
38131914882SAlex Richardson char *errnos[] = {
38231914882SAlex Richardson     "0", "EDOM", "ERANGE"
38331914882SAlex Richardson };
38431914882SAlex Richardson 
38531914882SAlex Richardson enum {
38631914882SAlex Richardson     e_none, e_divbyzero, e_domain, e_overflow, e_underflow
38731914882SAlex Richardson };
38831914882SAlex Richardson char *errors[] = {
38931914882SAlex Richardson     "0", "divbyzero", "domain", "overflow", "underflow"
39031914882SAlex Richardson };
39131914882SAlex Richardson 
39231914882SAlex Richardson static int verbose, fo, strict;
39331914882SAlex Richardson 
39431914882SAlex Richardson /* state toggled by random=on / random=off */
39531914882SAlex Richardson static int randomstate;
39631914882SAlex Richardson 
39731914882SAlex Richardson /* Canonify a double NaN: SNaNs all become 7FF00000.00000001 and QNaNs
39831914882SAlex Richardson  * all become 7FF80000.00000001 */
39931914882SAlex Richardson void canon_dNaN(unsigned a[2]) {
40031914882SAlex Richardson     if ((a[0] & 0x7FF00000) != 0x7FF00000)
40131914882SAlex Richardson         return;                        /* not Inf or NaN */
40231914882SAlex Richardson     if (!(a[0] & 0xFFFFF) && !a[1])
40331914882SAlex Richardson         return;                        /* Inf */
40431914882SAlex Richardson     a[0] &= 0x7FF80000;                /* canonify top word */
40531914882SAlex Richardson     a[1] = 0x00000001;                 /* canonify bottom word */
40631914882SAlex Richardson }
40731914882SAlex Richardson 
40831914882SAlex Richardson /* Canonify a single NaN: SNaNs all become 7F800001 and QNaNs
40931914882SAlex Richardson  * all become 7FC00001. Returns classification of the NaN. */
41031914882SAlex Richardson void canon_sNaN(unsigned a[1]) {
41131914882SAlex Richardson     if ((a[0] & 0x7F800000) != 0x7F800000)
41231914882SAlex Richardson         return;                        /* not Inf or NaN */
41331914882SAlex Richardson     if (!(a[0] & 0x7FFFFF))
41431914882SAlex Richardson         return;                        /* Inf */
41531914882SAlex Richardson     a[0] &= 0x7FC00000;                /* canonify most bits */
41631914882SAlex Richardson     a[0] |= 0x00000001;                /* canonify bottom bit */
41731914882SAlex Richardson }
41831914882SAlex Richardson 
41931914882SAlex Richardson /*
42031914882SAlex Richardson  * Detect difficult operands for FO mode.
42131914882SAlex Richardson  */
42231914882SAlex Richardson int is_dhard(unsigned a[2])
42331914882SAlex Richardson {
42431914882SAlex Richardson     if ((a[0] & 0x7FF00000) == 0x7FF00000)
42531914882SAlex Richardson         return TRUE;                   /* inf or NaN */
42631914882SAlex Richardson     if ((a[0] & 0x7FF00000) == 0 &&
42731914882SAlex Richardson         ((a[0] & 0x7FFFFFFF) | a[1]) != 0)
42831914882SAlex Richardson         return TRUE;                   /* denormal */
42931914882SAlex Richardson     return FALSE;
43031914882SAlex Richardson }
43131914882SAlex Richardson int is_shard(unsigned a[1])
43231914882SAlex Richardson {
43331914882SAlex Richardson     if ((a[0] & 0x7F800000) == 0x7F800000)
43431914882SAlex Richardson         return TRUE;                   /* inf or NaN */
43531914882SAlex Richardson     if ((a[0] & 0x7F800000) == 0 &&
43631914882SAlex Richardson         (a[0] & 0x7FFFFFFF) != 0)
43731914882SAlex Richardson         return TRUE;                   /* denormal */
43831914882SAlex Richardson     return FALSE;
43931914882SAlex Richardson }
44031914882SAlex Richardson 
44131914882SAlex Richardson /*
44231914882SAlex Richardson  * Normalise all zeroes into +0, for FO mode.
44331914882SAlex Richardson  */
44431914882SAlex Richardson void dnormzero(unsigned a[2])
44531914882SAlex Richardson {
44631914882SAlex Richardson     if (a[0] == 0x80000000 && a[1] == 0)
44731914882SAlex Richardson         a[0] = 0;
44831914882SAlex Richardson }
44931914882SAlex Richardson void snormzero(unsigned a[1])
45031914882SAlex Richardson {
45131914882SAlex Richardson     if (a[0] == 0x80000000)
45231914882SAlex Richardson         a[0] = 0;
45331914882SAlex Richardson }
45431914882SAlex Richardson 
45531914882SAlex Richardson static int find(char *word, char **array, int asize) {
45631914882SAlex Richardson     int i, j;
45731914882SAlex Richardson 
45831914882SAlex Richardson     asize /= sizeof(char *);
45931914882SAlex Richardson 
46031914882SAlex Richardson     i = -1; j = asize;                 /* strictly between i and j */
46131914882SAlex Richardson     while (j-i > 1) {
46231914882SAlex Richardson         int k = (i+j) / 2;
46331914882SAlex Richardson         int c = strcmp(word, array[k]);
46431914882SAlex Richardson         if (c > 0)
46531914882SAlex Richardson             i = k;
46631914882SAlex Richardson         else if (c < 0)
46731914882SAlex Richardson             j = k;
46831914882SAlex Richardson         else                           /* found it! */
46931914882SAlex Richardson             return k;
47031914882SAlex Richardson     }
47131914882SAlex Richardson     return -1;                         /* not found */
47231914882SAlex Richardson }
47331914882SAlex Richardson 
47431914882SAlex Richardson static test_func* find_testfunc(char *word) {
47531914882SAlex Richardson     int i, j, asize;
47631914882SAlex Richardson 
47731914882SAlex Richardson     asize = sizeof(tfuncs)/sizeof(test_func);
47831914882SAlex Richardson 
47931914882SAlex Richardson     i = -1; j = asize;                 /* strictly between i and j */
48031914882SAlex Richardson     while (j-i > 1) {
48131914882SAlex Richardson         int k = (i+j) / 2;
48231914882SAlex Richardson         int c = strcmp(word, tfuncs[k].name);
48331914882SAlex Richardson         if (c > 0)
48431914882SAlex Richardson             i = k;
48531914882SAlex Richardson         else if (c < 0)
48631914882SAlex Richardson             j = k;
48731914882SAlex Richardson         else                           /* found it! */
48831914882SAlex Richardson             return tfuncs + k;
48931914882SAlex Richardson     }
49031914882SAlex Richardson     return NULL;                         /* not found */
49131914882SAlex Richardson }
49231914882SAlex Richardson 
49331914882SAlex Richardson static long long calc_error(unsigned a[2], unsigned b[3], int shift, int rettype) {
49431914882SAlex Richardson     unsigned r0, r1, r2;
49531914882SAlex Richardson     int sign, carry;
49631914882SAlex Richardson     long long result;
49731914882SAlex Richardson 
49831914882SAlex Richardson     /*
49931914882SAlex Richardson      * If either number is infinite, require exact equality. If
50031914882SAlex Richardson      * either number is NaN, require that both are NaN. If either
50131914882SAlex Richardson      * of these requirements is broken, return INT_MAX.
50231914882SAlex Richardson      */
50331914882SAlex Richardson     if (is_double_rettype(rettype)) {
50431914882SAlex Richardson         if ((a[0] & 0x7FF00000) == 0x7FF00000 ||
50531914882SAlex Richardson             (b[0] & 0x7FF00000) == 0x7FF00000) {
50631914882SAlex Richardson             if (((a[0] & 0x800FFFFF) || a[1]) &&
50731914882SAlex Richardson                 ((b[0] & 0x800FFFFF) || b[1]) &&
50831914882SAlex Richardson                 (a[0] & 0x7FF00000) == 0x7FF00000 &&
50931914882SAlex Richardson                 (b[0] & 0x7FF00000) == 0x7FF00000)
51031914882SAlex Richardson                 return 0;              /* both NaN - OK */
51131914882SAlex Richardson             if (!((a[0] & 0xFFFFF) || a[1]) &&
51231914882SAlex Richardson                 !((b[0] & 0xFFFFF) || b[1]) &&
51331914882SAlex Richardson                 a[0] == b[0])
51431914882SAlex Richardson                 return 0;              /* both same sign of Inf - OK */
51531914882SAlex Richardson             return LLONG_MAX;
51631914882SAlex Richardson         }
51731914882SAlex Richardson     } else {
51831914882SAlex Richardson         if ((a[0] & 0x7F800000) == 0x7F800000 ||
51931914882SAlex Richardson             (b[0] & 0x7F800000) == 0x7F800000) {
52031914882SAlex Richardson             if ((a[0] & 0x807FFFFF) &&
52131914882SAlex Richardson                 (b[0] & 0x807FFFFF) &&
52231914882SAlex Richardson                 (a[0] & 0x7F800000) == 0x7F800000 &&
52331914882SAlex Richardson                 (b[0] & 0x7F800000) == 0x7F800000)
52431914882SAlex Richardson                 return 0;              /* both NaN - OK */
52531914882SAlex Richardson             if (!(a[0] & 0x7FFFFF) &&
52631914882SAlex Richardson                 !(b[0] & 0x7FFFFF) &&
52731914882SAlex Richardson                 a[0] == b[0])
52831914882SAlex Richardson                 return 0;              /* both same sign of Inf - OK */
52931914882SAlex Richardson             return LLONG_MAX;
53031914882SAlex Richardson         }
53131914882SAlex Richardson     }
53231914882SAlex Richardson 
53331914882SAlex Richardson     /*
53431914882SAlex Richardson      * Both finite. Return INT_MAX if the signs differ.
53531914882SAlex Richardson      */
53631914882SAlex Richardson     if ((a[0] ^ b[0]) & 0x80000000)
53731914882SAlex Richardson         return LLONG_MAX;
53831914882SAlex Richardson 
53931914882SAlex Richardson     /*
54031914882SAlex Richardson      * Now it's just straight multiple-word subtraction.
54131914882SAlex Richardson      */
54231914882SAlex Richardson     if (is_double_rettype(rettype)) {
54331914882SAlex Richardson         r2 = -b[2]; carry = (r2 == 0);
54431914882SAlex Richardson         r1 = a[1] + ~b[1] + carry; carry = (r1 < a[1] || (carry && r1 == a[1]));
54531914882SAlex Richardson         r0 = a[0] + ~b[0] + carry;
54631914882SAlex Richardson     } else {
54731914882SAlex Richardson         r2 = -b[1]; carry = (r2 == 0);
54831914882SAlex Richardson         r1 = a[0] + ~b[0] + carry; carry = (r1 < a[0] || (carry && r1 == a[0]));
54931914882SAlex Richardson         r0 = ~0 + carry;
55031914882SAlex Richardson     }
55131914882SAlex Richardson 
55231914882SAlex Richardson     /*
55331914882SAlex Richardson      * Forgive larger errors in specialised cases.
55431914882SAlex Richardson      */
55531914882SAlex Richardson     if (shift > 0) {
55631914882SAlex Richardson         if (shift > 32*3)
55731914882SAlex Richardson             return 0;                  /* all errors are forgiven! */
55831914882SAlex Richardson         while (shift >= 32) {
55931914882SAlex Richardson             r2 = r1;
56031914882SAlex Richardson             r1 = r0;
56131914882SAlex Richardson             r0 = -(r0 >> 31);
56231914882SAlex Richardson             shift -= 32;
56331914882SAlex Richardson         }
56431914882SAlex Richardson 
56531914882SAlex Richardson         if (shift > 0) {
56631914882SAlex Richardson             r2 = (r2 >> shift) | (r1 << (32-shift));
56731914882SAlex Richardson             r1 = (r1 >> shift) | (r0 << (32-shift));
56831914882SAlex Richardson             r0 = (r0 >> shift) | ((-(r0 >> 31)) << (32-shift));
56931914882SAlex Richardson         }
57031914882SAlex Richardson     }
57131914882SAlex Richardson 
57231914882SAlex Richardson     if (r0 & 0x80000000) {
57331914882SAlex Richardson         sign = 1;
57431914882SAlex Richardson         r2 = ~r2; carry = (r2 == 0);
57531914882SAlex Richardson         r1 = 0 + ~r1 + carry; carry = (carry && (r2 == 0));
57631914882SAlex Richardson         r0 = 0 + ~r0 + carry;
57731914882SAlex Richardson     } else {
57831914882SAlex Richardson         sign = 0;
57931914882SAlex Richardson     }
58031914882SAlex Richardson 
58131914882SAlex Richardson     if (r0 >= (1LL<<(31-EXTRABITS)))
58231914882SAlex Richardson         return LLONG_MAX;                /* many ulps out */
58331914882SAlex Richardson 
58431914882SAlex Richardson     result = (r2 >> (32-EXTRABITS)) & (ULPUNIT-1);
58531914882SAlex Richardson     result |= r1 << EXTRABITS;
58631914882SAlex Richardson     result |= (long long)r0 << (32+EXTRABITS);
58731914882SAlex Richardson     if (sign)
58831914882SAlex Richardson         result = -result;
58931914882SAlex Richardson     return result;
59031914882SAlex Richardson }
59131914882SAlex Richardson 
59231914882SAlex Richardson /* special named operands */
59331914882SAlex Richardson 
59431914882SAlex Richardson typedef struct {
59531914882SAlex Richardson     unsigned op1, op2;
59631914882SAlex Richardson     char* name;
59731914882SAlex Richardson } special_op;
59831914882SAlex Richardson 
59931914882SAlex Richardson static special_op special_ops_double[] = {
60031914882SAlex Richardson     {0x00000000,0x00000000,"0"},
60131914882SAlex Richardson     {0x3FF00000,0x00000000,"1"},
60231914882SAlex Richardson     {0x7FF00000,0x00000000,"inf"},
60331914882SAlex Richardson     {0x7FF80000,0x00000001,"qnan"},
60431914882SAlex Richardson     {0x7FF00000,0x00000001,"snan"},
60531914882SAlex Richardson     {0x3ff921fb,0x54442d18,"pi2"},
60631914882SAlex Richardson     {0x400921fb,0x54442d18,"pi"},
60731914882SAlex Richardson     {0x3fe921fb,0x54442d18,"pi4"},
60831914882SAlex Richardson     {0x4002d97c,0x7f3321d2,"3pi4"},
60931914882SAlex Richardson };
61031914882SAlex Richardson 
61131914882SAlex Richardson static special_op special_ops_float[] = {
61231914882SAlex Richardson     {0x00000000,0,"0"},
61331914882SAlex Richardson     {0x3f800000,0,"1"},
61431914882SAlex Richardson     {0x7f800000,0,"inf"},
61531914882SAlex Richardson     {0x7fc00000,0,"qnan"},
61631914882SAlex Richardson     {0x7f800001,0,"snan"},
61731914882SAlex Richardson     {0x3fc90fdb,0,"pi2"},
61831914882SAlex Richardson     {0x40490fdb,0,"pi"},
61931914882SAlex Richardson     {0x3f490fdb,0,"pi4"},
62031914882SAlex Richardson     {0x4016cbe4,0,"3pi4"},
62131914882SAlex Richardson };
62231914882SAlex Richardson 
62331914882SAlex Richardson /*
62431914882SAlex Richardson    This is what is returned by the below functions.
62531914882SAlex Richardson    We need it to handle the sign of the number
62631914882SAlex Richardson */
62731914882SAlex Richardson static special_op tmp_op = {0,0,0};
62831914882SAlex Richardson 
62931914882SAlex Richardson special_op* find_special_op_from_op(unsigned op1, unsigned op2, int is_double) {
63031914882SAlex Richardson     int i;
63131914882SAlex Richardson     special_op* sop;
63231914882SAlex Richardson     if(is_double) {
63331914882SAlex Richardson         sop = special_ops_double;
63431914882SAlex Richardson     } else {
63531914882SAlex Richardson         sop = special_ops_float;
63631914882SAlex Richardson     }
63731914882SAlex Richardson     for(i = 0; i < sizeof(special_ops_double)/sizeof(special_op); i++) {
63831914882SAlex Richardson         if(sop->op1 == (op1&0x7fffffff) && sop->op2 == op2) {
63931914882SAlex Richardson             if(tmp_op.name) free(tmp_op.name);
64031914882SAlex Richardson             tmp_op.name = malloc(strlen(sop->name)+2);
64131914882SAlex Richardson             if(op1>>31) {
64231914882SAlex Richardson                 sprintf(tmp_op.name,"-%s",sop->name);
64331914882SAlex Richardson             } else {
64431914882SAlex Richardson                 strcpy(tmp_op.name,sop->name);
64531914882SAlex Richardson             }
64631914882SAlex Richardson             return &tmp_op;
64731914882SAlex Richardson         }
64831914882SAlex Richardson         sop++;
64931914882SAlex Richardson     }
65031914882SAlex Richardson     return NULL;
65131914882SAlex Richardson }
65231914882SAlex Richardson 
65331914882SAlex Richardson special_op* find_special_op_from_name(const char* name, int is_double) {
65431914882SAlex Richardson     int i, neg=0;
65531914882SAlex Richardson     special_op* sop;
65631914882SAlex Richardson     if(is_double) {
65731914882SAlex Richardson         sop = special_ops_double;
65831914882SAlex Richardson     } else {
65931914882SAlex Richardson         sop = special_ops_float;
66031914882SAlex Richardson     }
66131914882SAlex Richardson     if(*name=='-') {
66231914882SAlex Richardson         neg=1;
66331914882SAlex Richardson         name++;
66431914882SAlex Richardson     } else if(*name=='+') {
66531914882SAlex Richardson         name++;
66631914882SAlex Richardson     }
66731914882SAlex Richardson     for(i = 0; i < sizeof(special_ops_double)/sizeof(special_op); i++) {
66831914882SAlex Richardson         if(0 == strcmp(name,sop->name)) {
66931914882SAlex Richardson             tmp_op.op1 = sop->op1;
67031914882SAlex Richardson             if(neg) {
67131914882SAlex Richardson                 tmp_op.op1 |= 0x80000000;
67231914882SAlex Richardson             }
67331914882SAlex Richardson             tmp_op.op2 = sop->op2;
67431914882SAlex Richardson             return &tmp_op;
67531914882SAlex Richardson         }
67631914882SAlex Richardson         sop++;
67731914882SAlex Richardson     }
67831914882SAlex Richardson     return NULL;
67931914882SAlex Richardson }
68031914882SAlex Richardson 
68131914882SAlex Richardson /*
68231914882SAlex Richardson    helper function for the below
68331914882SAlex Richardson    type=0 for single, 1 for double, 2 for no sop
68431914882SAlex Richardson */
68531914882SAlex Richardson int do_op(char* q, unsigned* op, const char* name, int num, int sop_type) {
68631914882SAlex Richardson     int i;
68731914882SAlex Richardson     int n=num;
68831914882SAlex Richardson     special_op* sop = NULL;
68931914882SAlex Richardson     for(i = 0; i < num; i++) {
69031914882SAlex Richardson         op[i] = 0;
69131914882SAlex Richardson     }
69231914882SAlex Richardson     if(sop_type<2) {
69331914882SAlex Richardson         sop = find_special_op_from_name(q,sop_type);
69431914882SAlex Richardson     }
69531914882SAlex Richardson     if(sop != NULL) {
69631914882SAlex Richardson         op[0] = sop->op1;
69731914882SAlex Richardson         op[1] = sop->op2;
69831914882SAlex Richardson     } else {
69931914882SAlex Richardson         switch(num) {
70031914882SAlex Richardson         case 1: n = sscanf(q, "%x", &op[0]); break;
70131914882SAlex Richardson         case 2: n = sscanf(q, "%x.%x", &op[0], &op[1]); break;
70231914882SAlex Richardson         case 3: n = sscanf(q, "%x.%x.%x", &op[0], &op[1], &op[2]); break;
70331914882SAlex Richardson         default: return -1;
70431914882SAlex Richardson         }
70531914882SAlex Richardson     }
70631914882SAlex Richardson     if (verbose) {
70731914882SAlex Richardson         printf("%s=",name);
70831914882SAlex Richardson         for (i = 0; (i < n); ++i) printf("%x.", op[i]);
70931914882SAlex Richardson         printf(" (n=%d)\n", n);
71031914882SAlex Richardson     }
71131914882SAlex Richardson     return n;
71231914882SAlex Richardson }
71331914882SAlex Richardson 
71431914882SAlex Richardson testdetail parsetest(char *testbuf, testdetail oldtest) {
71531914882SAlex Richardson     char *p; /* Current part of line: Option name */
71631914882SAlex Richardson     char *q; /* Current part of line: Option value */
71731914882SAlex Richardson     testdetail ret; /* What we return */
71831914882SAlex Richardson     int k; /* Function enum from k_* */
71931914882SAlex Richardson     int n; /* Used as returns for scanfs */
72031914882SAlex Richardson     int argtype=2, rettype=2; /* for do_op */
72131914882SAlex Richardson 
72231914882SAlex Richardson     /* clear ret */
72331914882SAlex Richardson     memset(&ret, 0, sizeof(ret));
72431914882SAlex Richardson 
72531914882SAlex Richardson     if (verbose) printf("Parsing line: %s\n", testbuf);
72631914882SAlex Richardson     while (*testbuf && isspace(*testbuf)) testbuf++;
72731914882SAlex Richardson     if (testbuf[0] == ';' || testbuf[0] == '#' || testbuf[0] == '!' ||
72831914882SAlex Richardson         testbuf[0] == '>' || testbuf[0] == '\0') {
72931914882SAlex Richardson         ret.comment = 1;
73031914882SAlex Richardson         if (verbose) printf("Line is a comment\n");
73131914882SAlex Richardson         return ret;
73231914882SAlex Richardson     }
73331914882SAlex Richardson     ret.comment = 0;
73431914882SAlex Richardson 
73531914882SAlex Richardson     if (*testbuf == '+') {
73631914882SAlex Richardson         if (oldtest.valid) {
73731914882SAlex Richardson             ret = oldtest;             /* structure copy */
73831914882SAlex Richardson         } else {
73931914882SAlex Richardson             fprintf(stderr, "copy from invalid: ignored\n");
74031914882SAlex Richardson         }
74131914882SAlex Richardson         testbuf++;
74231914882SAlex Richardson     }
74331914882SAlex Richardson 
74431914882SAlex Richardson     ret.random = randomstate;
74531914882SAlex Richardson 
74631914882SAlex Richardson     ret.in_err = 0;
74731914882SAlex Richardson     ret.in_err_limit = e_number_of_errnos;
74831914882SAlex Richardson 
74931914882SAlex Richardson     p = strtok(testbuf, " \t");
75031914882SAlex Richardson     while (p != NULL) {
75131914882SAlex Richardson         q = strchr(p, '=');
75231914882SAlex Richardson         if (!q)
75331914882SAlex Richardson             goto balderdash;
75431914882SAlex Richardson         *q++ = '\0';
75531914882SAlex Richardson         k = find(p, keywords, sizeof(keywords));
75631914882SAlex Richardson         switch (k) {
75731914882SAlex Richardson         case k_random:
75831914882SAlex Richardson             randomstate = (!strcmp(q, "on"));
75931914882SAlex Richardson             ret.comment = 1;
76031914882SAlex Richardson             return ret;                /* otherwise ignore this line */
76131914882SAlex Richardson         case k_func:
76231914882SAlex Richardson             if (verbose) printf("func=%s ", q);
76331914882SAlex Richardson             //ret.func = find(q, funcs, sizeof(funcs));
76431914882SAlex Richardson             ret.func = find_testfunc(q);
76531914882SAlex Richardson             if (ret.func == NULL)
76631914882SAlex Richardson                 {
76731914882SAlex Richardson                     if (verbose) printf("(id=unknown)\n");
76831914882SAlex Richardson                     goto balderdash;
76931914882SAlex Richardson                 }
77031914882SAlex Richardson             if(is_single_argtype(ret.func->argtype))
77131914882SAlex Richardson                 argtype = 0;
77231914882SAlex Richardson             else if(is_double_argtype(ret.func->argtype))
77331914882SAlex Richardson                 argtype = 1;
77431914882SAlex Richardson             if(is_single_rettype(ret.func->rettype))
77531914882SAlex Richardson                 rettype = 0;
77631914882SAlex Richardson             else if(is_double_rettype(ret.func->rettype))
77731914882SAlex Richardson                 rettype = 1;
77831914882SAlex Richardson             //ret.size = sizes[ret.func];
77931914882SAlex Richardson             if (verbose) printf("(name=%s) (size=%d)\n", ret.func->name, ret.func->argtype);
78031914882SAlex Richardson             break;
78131914882SAlex Richardson         case k_op1:
78231914882SAlex Richardson         case k_op1r:
78331914882SAlex Richardson             n = do_op(q,ret.op1r,"op1r",2,argtype);
78431914882SAlex Richardson             if (n < 1)
78531914882SAlex Richardson                 goto balderdash;
78631914882SAlex Richardson             break;
78731914882SAlex Richardson         case k_op1i:
78831914882SAlex Richardson             n = do_op(q,ret.op1i,"op1i",2,argtype);
78931914882SAlex Richardson             if (n < 1)
79031914882SAlex Richardson                 goto balderdash;
79131914882SAlex Richardson             break;
79231914882SAlex Richardson         case k_op2:
79331914882SAlex Richardson         case k_op2r:
79431914882SAlex Richardson             n = do_op(q,ret.op2r,"op2r",2,argtype);
79531914882SAlex Richardson             if (n < 1)
79631914882SAlex Richardson                 goto balderdash;
79731914882SAlex Richardson             break;
79831914882SAlex Richardson         case k_op2i:
79931914882SAlex Richardson             n = do_op(q,ret.op2i,"op2i",2,argtype);
80031914882SAlex Richardson             if (n < 1)
80131914882SAlex Richardson                 goto balderdash;
80231914882SAlex Richardson             break;
80331914882SAlex Richardson         case k_resultc:
80431914882SAlex Richardson             puts(q);
80531914882SAlex Richardson             if(strncmp(q,"inf",3)==0) {
80631914882SAlex Richardson                 ret.resultc = rc_infinity;
80731914882SAlex Richardson             } else if(strcmp(q,"zero")==0) {
80831914882SAlex Richardson                 ret.resultc = rc_zero;
80931914882SAlex Richardson             } else if(strcmp(q,"nan")==0) {
81031914882SAlex Richardson                 ret.resultc = rc_nan;
81131914882SAlex Richardson             } else if(strcmp(q,"finite")==0) {
81231914882SAlex Richardson                 ret.resultc = rc_finite;
81331914882SAlex Richardson             } else {
81431914882SAlex Richardson                 goto balderdash;
81531914882SAlex Richardson             }
81631914882SAlex Richardson             break;
81731914882SAlex Richardson         case k_result:
81831914882SAlex Richardson         case k_resultr:
81931914882SAlex Richardson             n = (do_op)(q,ret.resultr,"resultr",3,rettype);
82031914882SAlex Richardson             if (n < 1)
82131914882SAlex Richardson                 goto balderdash;
82231914882SAlex Richardson             ret.nresult = n; /* assume real and imaginary have same no. words */
82331914882SAlex Richardson             break;
82431914882SAlex Richardson         case k_resulti:
82531914882SAlex Richardson             n = do_op(q,ret.resulti,"resulti",3,rettype);
82631914882SAlex Richardson             if (n < 1)
82731914882SAlex Richardson                 goto balderdash;
82831914882SAlex Richardson             break;
82931914882SAlex Richardson         case k_res2:
83031914882SAlex Richardson             n = do_op(q,ret.res2,"res2",2,rettype);
83131914882SAlex Richardson             if (n < 1)
83231914882SAlex Richardson                 goto balderdash;
83331914882SAlex Richardson             break;
83431914882SAlex Richardson         case k_status:
83531914882SAlex Richardson             while (*q) {
83631914882SAlex Richardson                 if (*q == 'i') ret.status |= FE_INVALID;
83731914882SAlex Richardson                 if (*q == 'z') ret.status |= FE_DIVBYZERO;
83831914882SAlex Richardson                 if (*q == 'o') ret.status |= FE_OVERFLOW;
83931914882SAlex Richardson                 if (*q == 'u') ret.status |= FE_UNDERFLOW;
84031914882SAlex Richardson                 q++;
84131914882SAlex Richardson             }
84231914882SAlex Richardson             break;
84331914882SAlex Richardson         case k_maybeerror:
84431914882SAlex Richardson             n = find(q, errors, sizeof(errors));
84531914882SAlex Richardson             if (n < 0)
84631914882SAlex Richardson                 goto balderdash;
84731914882SAlex Richardson             if(math_errhandling&MATH_ERREXCEPT) {
84831914882SAlex Richardson                 switch(n) {
84931914882SAlex Richardson                 case e_domain: ret.maybestatus |= FE_INVALID; break;
85031914882SAlex Richardson                 case e_divbyzero: ret.maybestatus |= FE_DIVBYZERO; break;
85131914882SAlex Richardson                 case e_overflow: ret.maybestatus |= FE_OVERFLOW; break;
85231914882SAlex Richardson                 case e_underflow: ret.maybestatus |= FE_UNDERFLOW; break;
85331914882SAlex Richardson                 }
85431914882SAlex Richardson             }
85531914882SAlex Richardson             {
85631914882SAlex Richardson                 switch(n) {
85731914882SAlex Richardson                 case e_domain:
85831914882SAlex Richardson                     ret.maybeerr = e_EDOM; break;
85931914882SAlex Richardson                 case e_divbyzero:
86031914882SAlex Richardson                 case e_overflow:
86131914882SAlex Richardson                 case e_underflow:
86231914882SAlex Richardson                     ret.maybeerr = e_ERANGE; break;
86331914882SAlex Richardson                 }
86431914882SAlex Richardson             }
86531914882SAlex Richardson         case k_maybestatus:
86631914882SAlex Richardson             while (*q) {
86731914882SAlex Richardson                 if (*q == 'i') ret.maybestatus |= FE_INVALID;
86831914882SAlex Richardson                 if (*q == 'z') ret.maybestatus |= FE_DIVBYZERO;
86931914882SAlex Richardson                 if (*q == 'o') ret.maybestatus |= FE_OVERFLOW;
87031914882SAlex Richardson                 if (*q == 'u') ret.maybestatus |= FE_UNDERFLOW;
87131914882SAlex Richardson                 q++;
87231914882SAlex Richardson             }
87331914882SAlex Richardson             break;
87431914882SAlex Richardson         case k_error:
87531914882SAlex Richardson             n = find(q, errors, sizeof(errors));
87631914882SAlex Richardson             if (n < 0)
87731914882SAlex Richardson                 goto balderdash;
87831914882SAlex Richardson             if(math_errhandling&MATH_ERREXCEPT) {
87931914882SAlex Richardson                 switch(n) {
88031914882SAlex Richardson                 case e_domain: ret.status |= FE_INVALID; break;
88131914882SAlex Richardson                 case e_divbyzero: ret.status |= FE_DIVBYZERO; break;
88231914882SAlex Richardson                 case e_overflow: ret.status |= FE_OVERFLOW; break;
88331914882SAlex Richardson                 case e_underflow: ret.status |= FE_UNDERFLOW; break;
88431914882SAlex Richardson                 }
88531914882SAlex Richardson             }
88631914882SAlex Richardson             if(math_errhandling&MATH_ERRNO) {
88731914882SAlex Richardson                 switch(n) {
88831914882SAlex Richardson                 case e_domain:
88931914882SAlex Richardson                     ret.err = e_EDOM; break;
89031914882SAlex Richardson                 case e_divbyzero:
89131914882SAlex Richardson                 case e_overflow:
89231914882SAlex Richardson                 case e_underflow:
89331914882SAlex Richardson                     ret.err = e_ERANGE; break;
89431914882SAlex Richardson                 }
89531914882SAlex Richardson             }
89631914882SAlex Richardson             if(!(math_errhandling&MATH_ERRNO)) {
89731914882SAlex Richardson                 switch(n) {
89831914882SAlex Richardson                 case e_domain:
89931914882SAlex Richardson                     ret.maybeerr = e_EDOM; break;
90031914882SAlex Richardson                 case e_divbyzero:
90131914882SAlex Richardson                 case e_overflow:
90231914882SAlex Richardson                 case e_underflow:
90331914882SAlex Richardson                     ret.maybeerr = e_ERANGE; break;
90431914882SAlex Richardson                 }
90531914882SAlex Richardson             }
90631914882SAlex Richardson             break;
90731914882SAlex Richardson         case k_errno:
90831914882SAlex Richardson             ret.err = find(q, errnos, sizeof(errnos));
90931914882SAlex Richardson             if (ret.err < 0)
91031914882SAlex Richardson                 goto balderdash;
91131914882SAlex Richardson             break;
91231914882SAlex Richardson         case k_errno_in:
91331914882SAlex Richardson             ret.in_err = find(q, errnos, sizeof(errnos));
91431914882SAlex Richardson             if (ret.err < 0)
91531914882SAlex Richardson                 goto balderdash;
91631914882SAlex Richardson             ret.in_err_limit = ret.in_err + 1;
91731914882SAlex Richardson             break;
91831914882SAlex Richardson         case k_wrongresult:
91931914882SAlex Richardson         case k_wrongstatus:
92031914882SAlex Richardson         case k_wrongres2:
92131914882SAlex Richardson         case k_wrongerrno:
92231914882SAlex Richardson             /* quietly ignore these keys */
92331914882SAlex Richardson             break;
92431914882SAlex Richardson         default:
92531914882SAlex Richardson             goto balderdash;
92631914882SAlex Richardson         }
92731914882SAlex Richardson         p = strtok(NULL, " \t");
92831914882SAlex Richardson     }
92931914882SAlex Richardson     ret.valid = 1;
93031914882SAlex Richardson     return ret;
93131914882SAlex Richardson 
93231914882SAlex Richardson     /* come here from almost any error */
93331914882SAlex Richardson  balderdash:
93431914882SAlex Richardson     ret.valid = 0;
93531914882SAlex Richardson     return ret;
93631914882SAlex Richardson }
93731914882SAlex Richardson 
93831914882SAlex Richardson typedef enum {
93931914882SAlex Richardson     test_comment,                      /* deliberately not a test */
94031914882SAlex Richardson     test_invalid,                      /* accidentally not a test */
94131914882SAlex Richardson     test_decline,                      /* was a test, and wasn't run */
94231914882SAlex Richardson     test_fail,                         /* was a test, and failed */
94331914882SAlex Richardson     test_pass                          /* was a test, and passed */
94431914882SAlex Richardson } testresult;
94531914882SAlex Richardson 
94631914882SAlex Richardson char failtext[512];
94731914882SAlex Richardson 
94831914882SAlex Richardson typedef union {
94931914882SAlex Richardson     unsigned i[2];
95031914882SAlex Richardson     double f;
95131914882SAlex Richardson     double da[2];
95231914882SAlex Richardson } dbl;
95331914882SAlex Richardson 
95431914882SAlex Richardson typedef union {
95531914882SAlex Richardson     unsigned i;
95631914882SAlex Richardson     float f;
95731914882SAlex Richardson     float da[2];
95831914882SAlex Richardson } sgl;
95931914882SAlex Richardson 
96031914882SAlex Richardson /* helper function for runtest */
96131914882SAlex Richardson void print_error(int rettype, unsigned *result, char* text, char** failp) {
96231914882SAlex Richardson     special_op *sop;
96331914882SAlex Richardson     char *str;
96431914882SAlex Richardson 
96531914882SAlex Richardson     if(result) {
96631914882SAlex Richardson         *failp += sprintf(*failp," %s=",text);
96731914882SAlex Richardson         sop = find_special_op_from_op(result[0],result[1],is_double_rettype(rettype));
96831914882SAlex Richardson         if(sop) {
96931914882SAlex Richardson             *failp += sprintf(*failp,"%s",sop->name);
97031914882SAlex Richardson         } else {
97131914882SAlex Richardson             if(is_double_rettype(rettype)) {
97231914882SAlex Richardson                 str="%08x.%08x";
97331914882SAlex Richardson             } else {
97431914882SAlex Richardson                 str="%08x";
97531914882SAlex Richardson             }
97631914882SAlex Richardson             *failp += sprintf(*failp,str,result[0],result[1]);
97731914882SAlex Richardson         }
97831914882SAlex Richardson     }
97931914882SAlex Richardson }
98031914882SAlex Richardson 
98131914882SAlex Richardson 
98231914882SAlex Richardson void print_ulps_helper(const char *name, long long ulps, char** failp) {
98331914882SAlex Richardson     if(ulps == LLONG_MAX) {
98431914882SAlex Richardson         *failp += sprintf(*failp, " %s=HUGE", name);
98531914882SAlex Richardson     } else {
98631914882SAlex Richardson         *failp += sprintf(*failp, " %s=%.3f", name, (double)ulps / ULPUNIT);
98731914882SAlex Richardson     }
98831914882SAlex Richardson }
98931914882SAlex Richardson 
99031914882SAlex Richardson /* for complex args make ulpsr or ulpsri = 0 to not print */
99131914882SAlex Richardson void print_ulps(int rettype, long long ulpsr, long long ulpsi, char** failp) {
99231914882SAlex Richardson     if(is_complex_rettype(rettype)) {
99331914882SAlex Richardson         if (ulpsr) print_ulps_helper("ulpsr",ulpsr,failp);
99431914882SAlex Richardson         if (ulpsi) print_ulps_helper("ulpsi",ulpsi,failp);
99531914882SAlex Richardson     } else {
99631914882SAlex Richardson         if (ulpsr) print_ulps_helper("ulps",ulpsr,failp);
99731914882SAlex Richardson     }
99831914882SAlex Richardson }
99931914882SAlex Richardson 
100031914882SAlex Richardson int runtest(testdetail t) {
100131914882SAlex Richardson     int err, status;
100231914882SAlex Richardson 
100331914882SAlex Richardson     dbl d_arg1, d_arg2, d_res, d_res2;
100431914882SAlex Richardson     sgl s_arg1, s_arg2, s_res, s_res2;
100531914882SAlex Richardson 
100631914882SAlex Richardson     int deferred_decline = FALSE;
100731914882SAlex Richardson     char *failp = failtext;
100831914882SAlex Richardson 
100931914882SAlex Richardson     unsigned int intres=0;
101031914882SAlex Richardson 
101131914882SAlex Richardson     int res2_adjust = 0;
101231914882SAlex Richardson 
101331914882SAlex Richardson     if (t.comment)
101431914882SAlex Richardson         return test_comment;
101531914882SAlex Richardson     if (!t.valid)
101631914882SAlex Richardson         return test_invalid;
101731914882SAlex Richardson 
101831914882SAlex Richardson     /* Set IEEE status to mathlib-normal */
101931914882SAlex Richardson     feclearexcept(FE_ALL_EXCEPT);
102031914882SAlex Richardson 
102131914882SAlex Richardson     /* Deal with operands */
102231914882SAlex Richardson #define DO_DOP(arg,op) arg.i[dmsd] = t.op[0]; arg.i[dlsd] = t.op[1]
102331914882SAlex Richardson     DO_DOP(d_arg1,op1r);
102431914882SAlex Richardson     DO_DOP(d_arg2,op2r);
102531914882SAlex Richardson     s_arg1.i = t.op1r[0]; s_arg2.i = t.op2r[0];
10265a02ffc3SAndrew Turner     s_res.i = 0;
102731914882SAlex Richardson 
102831914882SAlex Richardson     /*
102931914882SAlex Richardson      * Detect NaNs, infinities and denormals on input, and set a
103031914882SAlex Richardson      * deferred decline flag if we're in FO mode.
103131914882SAlex Richardson      *
103231914882SAlex Richardson      * (We defer the decline rather than doing it immediately
103331914882SAlex Richardson      * because even in FO mode the operation is not permitted to
103431914882SAlex Richardson      * crash or tight-loop; so we _run_ the test, and then ignore
103531914882SAlex Richardson      * all the results.)
103631914882SAlex Richardson      */
103731914882SAlex Richardson     if (fo) {
103831914882SAlex Richardson         if (is_double_argtype(t.func->argtype) && is_dhard(t.op1r))
103931914882SAlex Richardson             deferred_decline = TRUE;
104031914882SAlex Richardson         if (t.func->argtype==at_d2 && is_dhard(t.op2r))
104131914882SAlex Richardson             deferred_decline = TRUE;
104231914882SAlex Richardson         if (is_single_argtype(t.func->argtype) && is_shard(t.op1r))
104331914882SAlex Richardson             deferred_decline = TRUE;
104431914882SAlex Richardson         if (t.func->argtype==at_s2 && is_shard(t.op2r))
104531914882SAlex Richardson             deferred_decline = TRUE;
104631914882SAlex Richardson         if (is_double_rettype(t.func->rettype) && is_dhard(t.resultr))
104731914882SAlex Richardson             deferred_decline = TRUE;
104831914882SAlex Richardson         if (t.func->rettype==rt_d2 && is_dhard(t.res2))
104931914882SAlex Richardson             deferred_decline = TRUE;
105031914882SAlex Richardson         if (is_single_argtype(t.func->rettype) && is_shard(t.resultr))
105131914882SAlex Richardson             deferred_decline = TRUE;
105231914882SAlex Richardson         if (t.func->rettype==rt_s2 && is_shard(t.res2))
105331914882SAlex Richardson             deferred_decline = TRUE;
105431914882SAlex Richardson         if (t.err == e_ERANGE)
105531914882SAlex Richardson             deferred_decline = TRUE;
105631914882SAlex Richardson     }
105731914882SAlex Richardson 
105831914882SAlex Richardson     /*
105931914882SAlex Richardson      * Perform the operation
106031914882SAlex Richardson      */
106131914882SAlex Richardson 
106231914882SAlex Richardson     errno = t.in_err == e_EDOM ? EDOM : t.in_err == e_ERANGE ? ERANGE : 0;
106331914882SAlex Richardson     if (t.err == e_0)
106431914882SAlex Richardson         t.err = t.in_err;
106531914882SAlex Richardson     if (t.maybeerr == e_0)
106631914882SAlex Richardson         t.maybeerr = t.in_err;
106731914882SAlex Richardson 
106831914882SAlex Richardson     if(t.func->type == t_func) {
106931914882SAlex Richardson         switch(t.func->argtype) {
107031914882SAlex Richardson         case at_d: d_res.f = t.func->func.d_d_ptr(d_arg1.f); break;
107131914882SAlex Richardson         case at_s: s_res.f = t.func->func.s_s_ptr(s_arg1.f); break;
107231914882SAlex Richardson         case at_d2: d_res.f = t.func->func.d2_d_ptr(d_arg1.f, d_arg2.f); break;
107331914882SAlex Richardson         case at_s2: s_res.f = t.func->func.s2_s_ptr(s_arg1.f, s_arg2.f); break;
107431914882SAlex Richardson         case at_di: d_res.f = t.func->func.di_d_ptr(d_arg1.f, d_arg2.i[dmsd]); break;
107531914882SAlex Richardson         case at_si: s_res.f = t.func->func.si_s_ptr(s_arg1.f, s_arg2.i); break;
107631914882SAlex Richardson         case at_dip: d_res.f = t.func->func.dip_d_ptr(d_arg1.f, (int*)&intres); break;
107731914882SAlex Richardson         case at_sip: s_res.f = t.func->func.sip_s_ptr(s_arg1.f, (int*)&intres); break;
107831914882SAlex Richardson         case at_ddp: d_res.f = t.func->func.ddp_d_ptr(d_arg1.f, &d_res2.f); break;
107931914882SAlex Richardson         case at_ssp: s_res.f = t.func->func.ssp_s_ptr(s_arg1.f, &s_res2.f); break;
108031914882SAlex Richardson         default:
108131914882SAlex Richardson             printf("unhandled function: %s\n",t.func->name);
108231914882SAlex Richardson             return test_fail;
108331914882SAlex Richardson         }
108431914882SAlex Richardson     } else {
108531914882SAlex Richardson         /* 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); */
108631914882SAlex Richardson         switch(t.func->macro_name) {
108731914882SAlex Richardson         case m_isfinite: intres = isfinite(d_arg1.f); break;
108831914882SAlex Richardson         case m_isinf: intres = isinf(d_arg1.f); break;
108931914882SAlex Richardson         case m_isnan: intres = isnan(d_arg1.f); break;
109031914882SAlex Richardson         case m_isnormal: intres = isnormal(d_arg1.f); break;
109131914882SAlex Richardson         case m_signbit: intres = signbit(d_arg1.f); break;
109231914882SAlex Richardson         case m_fpclassify: intres = fpclassify(d_arg1.f); break;
109331914882SAlex Richardson         case m_isgreater: intres = isgreater(d_arg1.f, d_arg2.f); break;
109431914882SAlex Richardson         case m_isgreaterequal: intres = isgreaterequal(d_arg1.f, d_arg2.f); break;
109531914882SAlex Richardson         case m_isless: intres = isless(d_arg1.f, d_arg2.f); break;
109631914882SAlex Richardson         case m_islessequal: intres = islessequal(d_arg1.f, d_arg2.f); break;
109731914882SAlex Richardson         case m_islessgreater: intres = islessgreater(d_arg1.f, d_arg2.f); break;
109831914882SAlex Richardson         case m_isunordered: intres = isunordered(d_arg1.f, d_arg2.f); break;
109931914882SAlex Richardson 
110031914882SAlex Richardson         case m_isfinitef: intres = isfinite(s_arg1.f); break;
110131914882SAlex Richardson         case m_isinff: intres = isinf(s_arg1.f); break;
110231914882SAlex Richardson         case m_isnanf: intres = isnan(s_arg1.f); break;
110331914882SAlex Richardson         case m_isnormalf: intres = isnormal(s_arg1.f); break;
110431914882SAlex Richardson         case m_signbitf: intres = signbit(s_arg1.f); break;
110531914882SAlex Richardson         case m_fpclassifyf: intres = fpclassify(s_arg1.f); break;
110631914882SAlex Richardson         case m_isgreaterf: intres = isgreater(s_arg1.f, s_arg2.f); break;
110731914882SAlex Richardson         case m_isgreaterequalf: intres = isgreaterequal(s_arg1.f, s_arg2.f); break;
110831914882SAlex Richardson         case m_islessf: intres = isless(s_arg1.f, s_arg2.f); break;
110931914882SAlex Richardson         case m_islessequalf: intres = islessequal(s_arg1.f, s_arg2.f); break;
111031914882SAlex Richardson         case m_islessgreaterf: intres = islessgreater(s_arg1.f, s_arg2.f); break;
111131914882SAlex Richardson         case m_isunorderedf: intres = isunordered(s_arg1.f, s_arg2.f); break;
111231914882SAlex Richardson 
111331914882SAlex Richardson         default:
111431914882SAlex Richardson             printf("unhandled macro: %s\n",t.func->name);
111531914882SAlex Richardson             return test_fail;
111631914882SAlex Richardson         }
111731914882SAlex Richardson     }
111831914882SAlex Richardson 
111931914882SAlex Richardson     /*
112031914882SAlex Richardson      * Decline the test if the deferred decline flag was set above.
112131914882SAlex Richardson      */
112231914882SAlex Richardson     if (deferred_decline)
112331914882SAlex Richardson         return test_decline;
112431914882SAlex Richardson 
112531914882SAlex Richardson     /* printf("intres=%i\n",intres); */
112631914882SAlex Richardson 
112731914882SAlex Richardson     /* Clear the fail text (indicating a pass unless we change it) */
112831914882SAlex Richardson     failp[0] = '\0';
112931914882SAlex Richardson 
113031914882SAlex Richardson     /* Check the IEEE status bits (except INX, which we disregard).
113131914882SAlex Richardson      * We don't bother with this for complex numbers, because the
113231914882SAlex Richardson      * complex functions are hard to get exactly right and we don't
113331914882SAlex Richardson      * have to anyway (C99 annex G is only informative). */
113431914882SAlex Richardson     if (!(is_complex_argtype(t.func->argtype) || is_complex_rettype(t.func->rettype))) {
113531914882SAlex Richardson         status = fetestexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW);
113631914882SAlex Richardson         if ((status|t.maybestatus|~statusmask) != (t.status|t.maybestatus|~statusmask)) {
113731914882SAlex Richardson             if (quiet) failtext[0]='x';
113831914882SAlex Richardson             else {
113931914882SAlex Richardson                 failp += sprintf(failp,
114031914882SAlex Richardson                                  " wrongstatus=%s%s%s%s%s",
114131914882SAlex Richardson                                  (status & FE_INVALID ? "i" : ""),
114231914882SAlex Richardson                                  (status & FE_DIVBYZERO ? "z" : ""),
114331914882SAlex Richardson                                  (status & FE_OVERFLOW ? "o" : ""),
114431914882SAlex Richardson                                  (status & FE_UNDERFLOW ? "u" : ""),
114531914882SAlex Richardson                                  (status ? "" : "OK"));
114631914882SAlex Richardson             }
114731914882SAlex Richardson         }
114831914882SAlex Richardson     }
114931914882SAlex Richardson 
115031914882SAlex Richardson     /* Check the result */
115131914882SAlex Richardson     {
115231914882SAlex Richardson         unsigned resultr[2], resulti[2];
115331914882SAlex Richardson         unsigned tresultr[3], tresulti[3], wres;
115431914882SAlex Richardson 
115531914882SAlex Richardson         switch(t.func->rettype) {
115631914882SAlex Richardson         case rt_d:
115731914882SAlex Richardson         case rt_d2:
115831914882SAlex Richardson             tresultr[0] = t.resultr[0];
115931914882SAlex Richardson             tresultr[1] = t.resultr[1];
116031914882SAlex Richardson             resultr[0] = d_res.i[dmsd]; resultr[1] = d_res.i[dlsd];
11615a02ffc3SAndrew Turner             resulti[0] = resulti[1] = 0;
116231914882SAlex Richardson             wres = 2;
116331914882SAlex Richardson             break;
116431914882SAlex Richardson         case rt_i:
116531914882SAlex Richardson             tresultr[0] = t.resultr[0];
116631914882SAlex Richardson             resultr[0] = intres;
11675a02ffc3SAndrew Turner             resulti[0] = 0;
116831914882SAlex Richardson             wres = 1;
116931914882SAlex Richardson             break;
117031914882SAlex Richardson         case rt_s:
117131914882SAlex Richardson         case rt_s2:
117231914882SAlex Richardson             tresultr[0] = t.resultr[0];
117331914882SAlex Richardson             resultr[0] = s_res.i;
11745a02ffc3SAndrew Turner             resulti[0] = 0;
117531914882SAlex Richardson             wres = 1;
117631914882SAlex Richardson             break;
117731914882SAlex Richardson         default:
117831914882SAlex Richardson             puts("unhandled rettype in runtest");
11795a02ffc3SAndrew Turner             abort ();
118031914882SAlex Richardson         }
118131914882SAlex Richardson         if(t.resultc != rc_none) {
118231914882SAlex Richardson             int err = 0;
118331914882SAlex Richardson             switch(t.resultc) {
118431914882SAlex Richardson             case rc_zero:
118531914882SAlex Richardson                 if(resultr[0] != 0 || resulti[0] != 0 ||
118631914882SAlex Richardson                    (wres==2 && (resultr[1] != 0 || resulti[1] != 0))) {
118731914882SAlex Richardson                     err = 1;
118831914882SAlex Richardson                 }
118931914882SAlex Richardson                 break;
119031914882SAlex Richardson             case rc_infinity:
119131914882SAlex Richardson                 if(wres==1) {
119231914882SAlex Richardson                     if(!((resultr[0]&0x7fffffff)==0x7f800000 ||
119331914882SAlex Richardson                          (resulti[0]&0x7fffffff)==0x7f800000)) {
119431914882SAlex Richardson                         err = 1;
119531914882SAlex Richardson                     }
119631914882SAlex Richardson                 } else {
119731914882SAlex Richardson                   if(!(((resultr[0]&0x7fffffff)==0x7ff00000 && resultr[1]==0) ||
119831914882SAlex Richardson                        ((resulti[0]&0x7fffffff)==0x7ff00000 && resulti[1]==0))) {
119931914882SAlex Richardson                         err = 1;
120031914882SAlex Richardson                     }
120131914882SAlex Richardson                 }
120231914882SAlex Richardson                 break;
120331914882SAlex Richardson             case rc_nan:
120431914882SAlex Richardson                 if(wres==1) {
120531914882SAlex Richardson                     if(!((resultr[0]&0x7fffffff)>0x7f800000 ||
120631914882SAlex Richardson                          (resulti[0]&0x7fffffff)>0x7f800000)) {
120731914882SAlex Richardson                         err = 1;
120831914882SAlex Richardson                     }
120931914882SAlex Richardson                 } else {
121031914882SAlex Richardson                     canon_dNaN(resultr);
121131914882SAlex Richardson                     canon_dNaN(resulti);
121231914882SAlex Richardson                     if(!(((resultr[0]&0x7fffffff)>0x7ff00000 && resultr[1]==1) ||
121331914882SAlex Richardson                          ((resulti[0]&0x7fffffff)>0x7ff00000 && resulti[1]==1))) {
121431914882SAlex Richardson                         err = 1;
121531914882SAlex Richardson                     }
121631914882SAlex Richardson                 }
121731914882SAlex Richardson                 break;
121831914882SAlex Richardson             case rc_finite:
121931914882SAlex Richardson                 if(wres==1) {
122031914882SAlex Richardson                     if(!((resultr[0]&0x7fffffff)<0x7f800000 ||
122131914882SAlex Richardson                          (resulti[0]&0x7fffffff)<0x7f800000)) {
122231914882SAlex Richardson                         err = 1;
122331914882SAlex Richardson                     }
122431914882SAlex Richardson                 } else {
122531914882SAlex Richardson                     if(!((resultr[0]&0x7fffffff)<0x7ff00000 ||
122631914882SAlex Richardson                          (resulti[0]&0x7fffffff)<0x7ff00000)) {
122731914882SAlex Richardson                         err = 1;
122831914882SAlex Richardson                     }
122931914882SAlex Richardson                 }
123031914882SAlex Richardson                 break;
123131914882SAlex Richardson             default:
123231914882SAlex Richardson                 break;
123331914882SAlex Richardson             }
123431914882SAlex Richardson             if(err) {
123531914882SAlex Richardson                 print_error(t.func->rettype,resultr,"wrongresultr",&failp);
123631914882SAlex Richardson                 print_error(t.func->rettype,resulti,"wrongresulti",&failp);
123731914882SAlex Richardson             }
123831914882SAlex Richardson         } else if (t.nresult > wres) {
123931914882SAlex Richardson             /*
124031914882SAlex Richardson              * The test case data has provided the result to more
124131914882SAlex Richardson              * than double precision. Instead of testing exact
124231914882SAlex Richardson              * equality, we test against our maximum error
124331914882SAlex Richardson              * tolerance.
124431914882SAlex Richardson              */
124531914882SAlex Richardson             int rshift, ishift;
124631914882SAlex Richardson             long long ulpsr, ulpsi, ulptolerance;
124731914882SAlex Richardson 
124831914882SAlex Richardson             tresultr[wres] = t.resultr[wres] << (32-EXTRABITS);
124931914882SAlex Richardson             tresulti[wres] = t.resulti[wres] << (32-EXTRABITS);
125031914882SAlex Richardson             if(strict) {
125131914882SAlex Richardson                 ulptolerance = 4096; /* one ulp */
125231914882SAlex Richardson             } else {
125331914882SAlex Richardson                 ulptolerance = t.func->tolerance;
125431914882SAlex Richardson             }
125531914882SAlex Richardson             rshift = ishift = 0;
125631914882SAlex Richardson             if (ulptolerance & ABSLOWERBOUND) {
125731914882SAlex Richardson                 /*
125831914882SAlex Richardson                  * Hack for the lgamma functions, which have an
125931914882SAlex Richardson                  * error behaviour that can't conveniently be
126031914882SAlex Richardson                  * characterised in pure ULPs. Really, we want to
126131914882SAlex Richardson                  * say that the error in lgamma is "at most N ULPs,
126231914882SAlex Richardson                  * or at most an absolute error of X, whichever is
126331914882SAlex Richardson                  * larger", for appropriately chosen N,X. But since
126431914882SAlex Richardson                  * these two functions are the only cases where it
126531914882SAlex Richardson                  * arises, I haven't bothered to do it in a nice way
126631914882SAlex Richardson                  * in the function table above.
126731914882SAlex Richardson                  *
126831914882SAlex Richardson                  * (The difficult cases arise with negative input
126931914882SAlex Richardson                  * values such that |gamma(x)| is very near to 1; in
127031914882SAlex Richardson                  * this situation implementations tend to separately
127131914882SAlex Richardson                  * compute lgamma(|x|) and the log of the correction
127231914882SAlex Richardson                  * term from the Euler reflection formula, and
127331914882SAlex Richardson                  * subtract - which catastrophically loses
127431914882SAlex Richardson                  * significance.)
127531914882SAlex Richardson                  *
127631914882SAlex Richardson                  * As far as I can tell, nobody cares about this:
127731914882SAlex Richardson                  * GNU libm doesn't get those cases right either,
127831914882SAlex Richardson                  * and OpenCL explicitly doesn't state a ULP error
127931914882SAlex Richardson                  * limit for lgamma. So my guess is that this is
128031914882SAlex Richardson                  * simply considered acceptable error behaviour for
128131914882SAlex Richardson                  * this particular function, and hence I feel free
128231914882SAlex Richardson                  * to allow for it here.
128331914882SAlex Richardson                  */
128431914882SAlex Richardson                 ulptolerance &= ~ABSLOWERBOUND;
128531914882SAlex Richardson                 if (t.op1r[0] & 0x80000000) {
128631914882SAlex Richardson                     if (t.func->rettype == rt_d)
128731914882SAlex Richardson                         rshift = 0x400 - ((tresultr[0] >> 20) & 0x7ff);
128831914882SAlex Richardson                     else if (t.func->rettype == rt_s)
128931914882SAlex Richardson                         rshift = 0x80 - ((tresultr[0] >> 23) & 0xff);
129031914882SAlex Richardson                     if (rshift < 0)
129131914882SAlex Richardson                         rshift = 0;
129231914882SAlex Richardson                 }
129331914882SAlex Richardson             }
129431914882SAlex Richardson             if (ulptolerance & PLUSMINUSPIO2) {
129531914882SAlex Richardson                 ulptolerance &= ~PLUSMINUSPIO2;
129631914882SAlex Richardson                 /*
129731914882SAlex Richardson                  * Hack for range reduction, which can reduce
129831914882SAlex Richardson                  * borderline cases in the wrong direction, i.e.
129931914882SAlex Richardson                  * return a value just outside one end of the interval
130031914882SAlex Richardson                  * [-pi/4,+pi/4] when it could have returned a value
130131914882SAlex Richardson                  * just inside the other end by subtracting an
130231914882SAlex Richardson                  * adjacent multiple of pi/2.
130331914882SAlex Richardson                  *
130431914882SAlex Richardson                  * We tolerate this, up to a point, because the
130531914882SAlex Richardson                  * trigonometric functions making use of the output of
130631914882SAlex Richardson                  * rred can cope and because making the range reducer
130731914882SAlex Richardson                  * do the exactly right thing in every case would be
130831914882SAlex Richardson                  * more expensive.
130931914882SAlex Richardson                  */
131031914882SAlex Richardson                 if (wres == 1) {
131131914882SAlex Richardson                     /* Upper bound of overshoot derived in rredf.h */
131231914882SAlex Richardson                     if ((resultr[0]&0x7FFFFFFF) <= 0x3f494b02 &&
131331914882SAlex Richardson                         (resultr[0]&0x7FFFFFFF) > 0x3f490fda &&
131431914882SAlex Richardson                         (resultr[0]&0x80000000) != (tresultr[0]&0x80000000)) {
131531914882SAlex Richardson                         unsigned long long val;
131631914882SAlex Richardson                         val = tresultr[0];
131731914882SAlex Richardson                         val = (val << 32) | tresultr[1];
131831914882SAlex Richardson                         /*
131931914882SAlex Richardson                          * Compute the alternative permitted result by
132031914882SAlex Richardson                          * subtracting from the sum of the extended
132131914882SAlex Richardson                          * single-precision bit patterns of +pi/4 and
132231914882SAlex Richardson                          * -pi/4. This is a horrible hack which only
132331914882SAlex Richardson                          * works because we can be confident that
132431914882SAlex Richardson                          * numbers in this range all have the same
132531914882SAlex Richardson                          * exponent!
132631914882SAlex Richardson                          */
132731914882SAlex Richardson                         val = 0xfe921fb54442d184ULL - val;
132831914882SAlex Richardson                         tresultr[0] = val >> 32;
132931914882SAlex Richardson                         tresultr[1] = (val >> (32-EXTRABITS)) << (32-EXTRABITS);
133031914882SAlex Richardson                         /*
133131914882SAlex Richardson                          * Also, expect a correspondingly different
133231914882SAlex Richardson                          * value of res2 as a result of this change.
133331914882SAlex Richardson                          * The adjustment depends on whether we just
133431914882SAlex Richardson                          * flipped the result from + to - or vice
133531914882SAlex Richardson                          * versa.
133631914882SAlex Richardson                          */
133731914882SAlex Richardson                         if (resultr[0] & 0x80000000) {
133831914882SAlex Richardson                             res2_adjust = +1;
133931914882SAlex Richardson                         } else {
134031914882SAlex Richardson                             res2_adjust = -1;
134131914882SAlex Richardson                         }
134231914882SAlex Richardson                     }
134331914882SAlex Richardson                 }
134431914882SAlex Richardson             }
134531914882SAlex Richardson             ulpsr = calc_error(resultr, tresultr, rshift, t.func->rettype);
134631914882SAlex Richardson             if(is_complex_rettype(t.func->rettype)) {
134731914882SAlex Richardson                 ulpsi = calc_error(resulti, tresulti, ishift, t.func->rettype);
134831914882SAlex Richardson             } else {
134931914882SAlex Richardson                 ulpsi = 0;
135031914882SAlex Richardson             }
135131914882SAlex Richardson             unsigned *rr = (ulpsr > ulptolerance || ulpsr < -ulptolerance) ? resultr : NULL;
135231914882SAlex Richardson             unsigned *ri = (ulpsi > ulptolerance || ulpsi < -ulptolerance) ? resulti : NULL;
135331914882SAlex Richardson /*             printf("tolerance=%i, ulpsr=%i, ulpsi=%i, rr=%p, ri=%p\n",ulptolerance,ulpsr,ulpsi,rr,ri); */
135431914882SAlex Richardson             if (rr || ri) {
135531914882SAlex Richardson                 if (quiet) failtext[0]='x';
135631914882SAlex Richardson                 else {
135731914882SAlex Richardson                     print_error(t.func->rettype,rr,"wrongresultr",&failp);
135831914882SAlex Richardson                     print_error(t.func->rettype,ri,"wrongresulti",&failp);
135931914882SAlex Richardson                     print_ulps(t.func->rettype,rr ? ulpsr : 0, ri ? ulpsi : 0,&failp);
136031914882SAlex Richardson                 }
136131914882SAlex Richardson             }
136231914882SAlex Richardson         } else {
136331914882SAlex Richardson             if(is_complex_rettype(t.func->rettype))
136431914882SAlex Richardson                 /*
136531914882SAlex Richardson                  * Complex functions are not fully supported,
136631914882SAlex Richardson                  * this is unreachable, but prevents warnings.
136731914882SAlex Richardson                  */
136831914882SAlex Richardson                 abort();
136931914882SAlex Richardson             /*
137031914882SAlex Richardson              * The test case data has provided the result in
137131914882SAlex Richardson              * exactly the output precision. Therefore we must
137231914882SAlex Richardson              * complain about _any_ violation.
137331914882SAlex Richardson              */
137431914882SAlex Richardson             switch(t.func->rettype) {
137531914882SAlex Richardson             case rt_dc:
137631914882SAlex Richardson                 canon_dNaN(tresulti);
137731914882SAlex Richardson                 canon_dNaN(resulti);
137831914882SAlex Richardson                 if (fo) {
137931914882SAlex Richardson                     dnormzero(tresulti);
138031914882SAlex Richardson                     dnormzero(resulti);
138131914882SAlex Richardson                 }
138231914882SAlex Richardson                 /* deliberate fall-through */
138331914882SAlex Richardson             case rt_d:
138431914882SAlex Richardson                 canon_dNaN(tresultr);
138531914882SAlex Richardson                 canon_dNaN(resultr);
138631914882SAlex Richardson                 if (fo) {
138731914882SAlex Richardson                     dnormzero(tresultr);
138831914882SAlex Richardson                     dnormzero(resultr);
138931914882SAlex Richardson                 }
139031914882SAlex Richardson                 break;
139131914882SAlex Richardson             case rt_sc:
139231914882SAlex Richardson                 canon_sNaN(tresulti);
139331914882SAlex Richardson                 canon_sNaN(resulti);
139431914882SAlex Richardson                 if (fo) {
139531914882SAlex Richardson                     snormzero(tresulti);
139631914882SAlex Richardson                     snormzero(resulti);
139731914882SAlex Richardson                 }
139831914882SAlex Richardson                 /* deliberate fall-through */
139931914882SAlex Richardson             case rt_s:
140031914882SAlex Richardson                 canon_sNaN(tresultr);
140131914882SAlex Richardson                 canon_sNaN(resultr);
140231914882SAlex Richardson                 if (fo) {
140331914882SAlex Richardson                     snormzero(tresultr);
140431914882SAlex Richardson                     snormzero(resultr);
140531914882SAlex Richardson                 }
140631914882SAlex Richardson                 break;
140731914882SAlex Richardson             default:
140831914882SAlex Richardson                 break;
140931914882SAlex Richardson             }
141031914882SAlex Richardson             if(is_complex_rettype(t.func->rettype)) {
141131914882SAlex Richardson                 unsigned *rr, *ri;
141231914882SAlex Richardson                 if(resultr[0] != tresultr[0] ||
141331914882SAlex Richardson                    (wres > 1 && resultr[1] != tresultr[1])) {
141431914882SAlex Richardson                     rr = resultr;
141531914882SAlex Richardson                 } else {
141631914882SAlex Richardson                     rr = NULL;
141731914882SAlex Richardson                 }
141831914882SAlex Richardson                 if(resulti[0] != tresulti[0] ||
141931914882SAlex Richardson                    (wres > 1 && resulti[1] != tresulti[1])) {
142031914882SAlex Richardson                     ri = resulti;
142131914882SAlex Richardson                 } else {
142231914882SAlex Richardson                     ri = NULL;
142331914882SAlex Richardson                 }
142431914882SAlex Richardson                 if(rr || ri) {
142531914882SAlex Richardson                     if (quiet) failtext[0]='x';
142631914882SAlex Richardson                     print_error(t.func->rettype,rr,"wrongresultr",&failp);
142731914882SAlex Richardson                     print_error(t.func->rettype,ri,"wrongresulti",&failp);
142831914882SAlex Richardson                 }
142931914882SAlex Richardson             } else if (resultr[0] != tresultr[0] ||
143031914882SAlex Richardson                        (wres > 1 && resultr[1] != tresultr[1])) {
143131914882SAlex Richardson                 if (quiet) failtext[0]='x';
143231914882SAlex Richardson                 print_error(t.func->rettype,resultr,"wrongresult",&failp);
143331914882SAlex Richardson             }
143431914882SAlex Richardson         }
143531914882SAlex Richardson         /*
143631914882SAlex Richardson          * Now test res2, for those functions (frexp, modf, rred)
143731914882SAlex Richardson          * which use it.
143831914882SAlex Richardson          */
143931914882SAlex Richardson         if (t.func->func.ptr == &frexp || t.func->func.ptr == &frexpf ||
144031914882SAlex Richardson             t.func->macro_name == m_rred || t.func->macro_name == m_rredf) {
144131914882SAlex Richardson             unsigned tres2 = t.res2[0];
144231914882SAlex Richardson             if (res2_adjust) {
144331914882SAlex Richardson                 /* Fix for range reduction, propagated from further up */
144431914882SAlex Richardson                 tres2 = (tres2 + res2_adjust) & 3;
144531914882SAlex Richardson             }
144631914882SAlex Richardson             if (tres2 != intres) {
144731914882SAlex Richardson                 if (quiet) failtext[0]='x';
144831914882SAlex Richardson                 else {
144931914882SAlex Richardson                     failp += sprintf(failp,
145031914882SAlex Richardson                                      " wrongres2=%08x", intres);
145131914882SAlex Richardson                 }
145231914882SAlex Richardson             }
145331914882SAlex Richardson         } else if (t.func->func.ptr == &modf || t.func->func.ptr == &modff) {
145431914882SAlex Richardson             tresultr[0] = t.res2[0];
145531914882SAlex Richardson             tresultr[1] = t.res2[1];
145631914882SAlex Richardson             if (is_double_rettype(t.func->rettype)) {
145731914882SAlex Richardson                 canon_dNaN(tresultr);
145831914882SAlex Richardson                 resultr[0] = d_res2.i[dmsd];
145931914882SAlex Richardson                 resultr[1] = d_res2.i[dlsd];
146031914882SAlex Richardson                 canon_dNaN(resultr);
146131914882SAlex Richardson                 if (fo) {
146231914882SAlex Richardson                     dnormzero(tresultr);
146331914882SAlex Richardson                     dnormzero(resultr);
146431914882SAlex Richardson                 }
146531914882SAlex Richardson             } else {
146631914882SAlex Richardson                 canon_sNaN(tresultr);
146731914882SAlex Richardson                 resultr[0] = s_res2.i;
146831914882SAlex Richardson                 resultr[1] = s_res2.i;
146931914882SAlex Richardson                 canon_sNaN(resultr);
147031914882SAlex Richardson                 if (fo) {
147131914882SAlex Richardson                     snormzero(tresultr);
147231914882SAlex Richardson                     snormzero(resultr);
147331914882SAlex Richardson                 }
147431914882SAlex Richardson             }
147531914882SAlex Richardson             if (resultr[0] != tresultr[0] ||
147631914882SAlex Richardson                 (wres > 1 && resultr[1] != tresultr[1])) {
147731914882SAlex Richardson                 if (quiet) failtext[0]='x';
147831914882SAlex Richardson                 else {
147931914882SAlex Richardson                     if (is_double_rettype(t.func->rettype))
148031914882SAlex Richardson                         failp += sprintf(failp, " wrongres2=%08x.%08x",
148131914882SAlex Richardson                                          resultr[0], resultr[1]);
148231914882SAlex Richardson                     else
148331914882SAlex Richardson                         failp += sprintf(failp, " wrongres2=%08x",
148431914882SAlex Richardson                                          resultr[0]);
148531914882SAlex Richardson                 }
148631914882SAlex Richardson             }
148731914882SAlex Richardson         }
148831914882SAlex Richardson     }
148931914882SAlex Richardson 
149031914882SAlex Richardson     /* Check errno */
149131914882SAlex Richardson     err = (errno == EDOM ? e_EDOM : errno == ERANGE ? e_ERANGE : e_0);
149231914882SAlex Richardson     if (err != t.err && err != t.maybeerr) {
149331914882SAlex Richardson         if (quiet) failtext[0]='x';
149431914882SAlex Richardson         else {
149531914882SAlex Richardson             failp += sprintf(failp, " wrongerrno=%s expecterrno=%s ", errnos[err], errnos[t.err]);
149631914882SAlex Richardson         }
149731914882SAlex Richardson     }
149831914882SAlex Richardson 
149931914882SAlex Richardson     return *failtext ? test_fail : test_pass;
150031914882SAlex Richardson }
150131914882SAlex Richardson 
150231914882SAlex Richardson int passed, failed, declined;
150331914882SAlex Richardson 
150431914882SAlex Richardson void runtests(char *name, FILE *fp) {
150531914882SAlex Richardson     char testbuf[512], linebuf[512];
150631914882SAlex Richardson     int lineno = 1;
150731914882SAlex Richardson     testdetail test;
150831914882SAlex Richardson 
150931914882SAlex Richardson     test.valid = 0;
151031914882SAlex Richardson 
151131914882SAlex Richardson     if (verbose) printf("runtests: %s\n", name);
151231914882SAlex Richardson     while (fgets(testbuf, sizeof(testbuf), fp)) {
151331914882SAlex Richardson         int res, print_errno;
151431914882SAlex Richardson         testbuf[strcspn(testbuf, "\r\n")] = '\0';
151531914882SAlex Richardson         strcpy(linebuf, testbuf);
151631914882SAlex Richardson         test = parsetest(testbuf, test);
151731914882SAlex Richardson         print_errno = 0;
151831914882SAlex Richardson         while (test.in_err < test.in_err_limit) {
151931914882SAlex Richardson             res = runtest(test);
152031914882SAlex Richardson             if (res == test_pass) {
152131914882SAlex Richardson                 if (verbose)
152231914882SAlex Richardson                     printf("%s:%d: pass\n", name, lineno);
152331914882SAlex Richardson                 ++passed;
152431914882SAlex Richardson             } else if (res == test_decline) {
152531914882SAlex Richardson                 if (verbose)
152631914882SAlex Richardson                     printf("%s:%d: declined\n", name, lineno);
152731914882SAlex Richardson                 ++declined;
152831914882SAlex Richardson             } else if (res == test_fail) {
152931914882SAlex Richardson                 if (!quiet)
153031914882SAlex Richardson                     printf("%s:%d: FAIL%s: %s%s%s%s\n", name, lineno,
153131914882SAlex Richardson                            test.random ? " (random)" : "",
153231914882SAlex Richardson                            linebuf,
153331914882SAlex Richardson                            print_errno ? " errno_in=" : "",
153431914882SAlex Richardson                            print_errno ? errnos[test.in_err] : "",
153531914882SAlex Richardson                            failtext);
153631914882SAlex Richardson                 ++failed;
153731914882SAlex Richardson             } else if (res == test_invalid) {
153831914882SAlex Richardson                 printf("%s:%d: malformed: %s\n", name, lineno, linebuf);
153931914882SAlex Richardson                 ++failed;
154031914882SAlex Richardson             }
154131914882SAlex Richardson             test.in_err++;
154231914882SAlex Richardson             print_errno = 1;
154331914882SAlex Richardson         }
154431914882SAlex Richardson         lineno++;
154531914882SAlex Richardson     }
154631914882SAlex Richardson }
154731914882SAlex Richardson 
154831914882SAlex Richardson int main(int ac, char **av) {
154931914882SAlex Richardson     char **files;
155031914882SAlex Richardson     int i, nfiles = 0;
155131914882SAlex Richardson     dbl d;
155231914882SAlex Richardson 
155331914882SAlex Richardson #ifdef MICROLIB
155431914882SAlex Richardson     /*
155531914882SAlex Richardson      * Invent argc and argv ourselves.
155631914882SAlex Richardson      */
155731914882SAlex Richardson     char *argv[256];
155831914882SAlex Richardson     char args[256];
155931914882SAlex Richardson     {
156031914882SAlex Richardson         int sargs[2];
156131914882SAlex Richardson         char *p;
156231914882SAlex Richardson 
156331914882SAlex Richardson         ac = 0;
156431914882SAlex Richardson 
156531914882SAlex Richardson         sargs[0]=(int)args;
156631914882SAlex Richardson         sargs[1]=(int)sizeof(args);
156731914882SAlex Richardson         if (!__semihost(0x15, sargs)) {
156831914882SAlex Richardson             args[sizeof(args)-1] = '\0';   /* just in case */
156931914882SAlex Richardson             p = args;
157031914882SAlex Richardson             while (1) {
157131914882SAlex Richardson                 while (*p == ' ' || *p == '\t') p++;
157231914882SAlex Richardson                 if (!*p) break;
157331914882SAlex Richardson                 argv[ac++] = p;
157431914882SAlex Richardson                 while (*p && *p != ' ' && *p != '\t') p++;
157531914882SAlex Richardson                 if (*p) *p++ = '\0';
157631914882SAlex Richardson             }
157731914882SAlex Richardson         }
157831914882SAlex Richardson 
157931914882SAlex Richardson         av = argv;
158031914882SAlex Richardson     }
158131914882SAlex Richardson #endif
158231914882SAlex Richardson 
158331914882SAlex Richardson     /* Sort tfuncs */
158431914882SAlex Richardson     qsort(tfuncs, sizeof(tfuncs)/sizeof(test_func), sizeof(test_func), &compare_tfuncs);
158531914882SAlex Richardson 
158631914882SAlex Richardson     /*
158731914882SAlex Richardson      * Autodetect the `double' endianness.
158831914882SAlex Richardson      */
158931914882SAlex Richardson     dmsd = 0;
159031914882SAlex Richardson     d.f = 1.0;                       /* 0x3ff00000 / 0x00000000 */
159131914882SAlex Richardson     if (d.i[dmsd] == 0) {
159231914882SAlex Richardson         dmsd = 1;
159331914882SAlex Richardson     }
159431914882SAlex Richardson     /*
159531914882SAlex Richardson      * Now dmsd denotes what the compiler thinks we're at. Let's
159631914882SAlex Richardson      * check that it agrees with what the runtime thinks.
159731914882SAlex Richardson      */
159831914882SAlex Richardson     d.i[0] = d.i[1] = 0x11111111;/* a random +ve number */
159931914882SAlex Richardson     d.f /= d.f;                    /* must now be one */
160031914882SAlex Richardson     if (d.i[dmsd] == 0) {
160131914882SAlex Richardson         fprintf(stderr, "YIKES! Compiler and runtime disagree on endianness"
160231914882SAlex Richardson                 " of `double'. Bailing out\n");
160331914882SAlex Richardson         return 1;
160431914882SAlex Richardson     }
160531914882SAlex Richardson     dlsd = !dmsd;
160631914882SAlex Richardson 
160731914882SAlex Richardson     /* default is terse */
160831914882SAlex Richardson     verbose = 0;
160931914882SAlex Richardson     fo = 0;
161031914882SAlex Richardson     strict = 0;
161131914882SAlex Richardson 
161231914882SAlex Richardson     files = (char **)malloc((ac+1) * sizeof(char *));
161331914882SAlex Richardson     if (!files) {
161431914882SAlex Richardson         fprintf(stderr, "initial malloc failed!\n");
161531914882SAlex Richardson         return 1;
161631914882SAlex Richardson     }
161731914882SAlex Richardson #ifdef NOCMDLINE
161831914882SAlex Richardson     files[nfiles++] = "testfile";
161931914882SAlex Richardson #endif
162031914882SAlex Richardson 
162131914882SAlex Richardson     while (--ac) {
162231914882SAlex Richardson         char *p = *++av;
162331914882SAlex Richardson         if (*p == '-') {
162431914882SAlex Richardson             static char *options[] = {
162531914882SAlex Richardson                 "-fo",
162631914882SAlex Richardson #if 0
162731914882SAlex Richardson                 "-noinexact",
162831914882SAlex Richardson                 "-noround",
162931914882SAlex Richardson #endif
163031914882SAlex Richardson                 "-nostatus",
163131914882SAlex Richardson                 "-quiet",
163231914882SAlex Richardson                 "-strict",
163331914882SAlex Richardson                 "-v",
163431914882SAlex Richardson                 "-verbose",
163531914882SAlex Richardson             };
163631914882SAlex Richardson             enum {
163731914882SAlex Richardson                 op_fo,
163831914882SAlex Richardson #if 0
163931914882SAlex Richardson                 op_noinexact,
164031914882SAlex Richardson                 op_noround,
164131914882SAlex Richardson #endif
164231914882SAlex Richardson                 op_nostatus,
164331914882SAlex Richardson                 op_quiet,
164431914882SAlex Richardson                 op_strict,
164531914882SAlex Richardson                 op_v,
164631914882SAlex Richardson                 op_verbose,
164731914882SAlex Richardson             };
164831914882SAlex Richardson             switch (find(p, options, sizeof(options))) {
164931914882SAlex Richardson             case op_quiet:
165031914882SAlex Richardson                 quiet = 1;
165131914882SAlex Richardson                 break;
165231914882SAlex Richardson #if 0
165331914882SAlex Richardson             case op_noinexact:
165431914882SAlex Richardson                 statusmask &= 0x0F;    /* remove bit 4 */
165531914882SAlex Richardson                 break;
165631914882SAlex Richardson             case op_noround:
165731914882SAlex Richardson                 doround = 0;
165831914882SAlex Richardson                 break;
165931914882SAlex Richardson #endif
166031914882SAlex Richardson             case op_nostatus:        /* no status word => noinx,noround */
166131914882SAlex Richardson                 statusmask = 0;
166231914882SAlex Richardson                 doround = 0;
166331914882SAlex Richardson                 break;
166431914882SAlex Richardson             case op_v:
166531914882SAlex Richardson             case op_verbose:
166631914882SAlex Richardson                 verbose = 1;
166731914882SAlex Richardson                 break;
166831914882SAlex Richardson             case op_fo:
166931914882SAlex Richardson                 fo = 1;
167031914882SAlex Richardson                 break;
167131914882SAlex Richardson             case op_strict: /* tolerance is 1 ulp */
167231914882SAlex Richardson                 strict = 1;
167331914882SAlex Richardson                 break;
167431914882SAlex Richardson             default:
167531914882SAlex Richardson                 fprintf(stderr, "unrecognised option: %s\n", p);
167631914882SAlex Richardson                 break;
167731914882SAlex Richardson             }
167831914882SAlex Richardson         } else {
167931914882SAlex Richardson             files[nfiles++] = p;
168031914882SAlex Richardson         }
168131914882SAlex Richardson     }
168231914882SAlex Richardson 
168331914882SAlex Richardson     passed = failed = declined = 0;
168431914882SAlex Richardson 
168531914882SAlex Richardson     if (nfiles) {
168631914882SAlex Richardson         for (i = 0; i < nfiles; i++) {
168731914882SAlex Richardson             FILE *fp = fopen(files[i], "r");
168831914882SAlex Richardson             if (!fp) {
168931914882SAlex Richardson                 fprintf(stderr, "Couldn't open %s\n", files[i]);
169031914882SAlex Richardson             } else
169131914882SAlex Richardson                 runtests(files[i], fp);
169231914882SAlex Richardson         }
169331914882SAlex Richardson     } else
169431914882SAlex Richardson         runtests("(stdin)", stdin);
169531914882SAlex Richardson 
169631914882SAlex Richardson     printf("Completed. Passed %d, failed %d (total %d",
169731914882SAlex Richardson            passed, failed, passed+failed);
169831914882SAlex Richardson     if (declined)
169931914882SAlex Richardson         printf(" plus %d declined", declined);
170031914882SAlex Richardson     printf(")\n");
170131914882SAlex Richardson     if (failed || passed == 0)
170231914882SAlex Richardson         return 1;
170331914882SAlex Richardson     printf("** TEST PASSED OK **\n");
170431914882SAlex Richardson     return 0;
170531914882SAlex Richardson }
170631914882SAlex Richardson 
170731914882SAlex Richardson void undef_func() {
170831914882SAlex Richardson     failed++;
170931914882SAlex Richardson     puts("ERROR: undefined function called");
171031914882SAlex Richardson }
1711*f3087befSAndrew Turner /* clang-format on */
1712