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