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