xref: /aosp_15_r20/external/arm-optimized-routines/math/test/rtest/dotest.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * dotest.c - actually generate mathlib test cases
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 1999-2019, Arm Limited.
5*412f47f9SXin Li  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li  */
7*412f47f9SXin Li 
8*412f47f9SXin Li #include <stdio.h>
9*412f47f9SXin Li #include <string.h>
10*412f47f9SXin Li #include <stdlib.h>
11*412f47f9SXin Li #include <stdint.h>
12*412f47f9SXin Li #include <assert.h>
13*412f47f9SXin Li #include <limits.h>
14*412f47f9SXin Li 
15*412f47f9SXin Li #include "semi.h"
16*412f47f9SXin Li #include "intern.h"
17*412f47f9SXin Li #include "random.h"
18*412f47f9SXin Li 
19*412f47f9SXin Li #define MPFR_PREC 96 /* good enough for float or double + a few extra bits */
20*412f47f9SXin Li 
21*412f47f9SXin Li extern int lib_fo, lib_no_arith, ntests;
22*412f47f9SXin Li 
23*412f47f9SXin Li /*
24*412f47f9SXin Li  * Prototypes.
25*412f47f9SXin Li  */
26*412f47f9SXin Li static void cases_biased(uint32 *, uint32, uint32);
27*412f47f9SXin Li static void cases_biased_positive(uint32 *, uint32, uint32);
28*412f47f9SXin Li static void cases_biased_float(uint32 *, uint32, uint32);
29*412f47f9SXin Li static void cases_uniform(uint32 *, uint32, uint32);
30*412f47f9SXin Li static void cases_uniform_positive(uint32 *, uint32, uint32);
31*412f47f9SXin Li static void cases_uniform_float(uint32 *, uint32, uint32);
32*412f47f9SXin Li static void cases_uniform_float_positive(uint32 *, uint32, uint32);
33*412f47f9SXin Li static void log_cases(uint32 *, uint32, uint32);
34*412f47f9SXin Li static void log_cases_float(uint32 *, uint32, uint32);
35*412f47f9SXin Li static void log1p_cases(uint32 *, uint32, uint32);
36*412f47f9SXin Li static void log1p_cases_float(uint32 *, uint32, uint32);
37*412f47f9SXin Li static void minmax_cases(uint32 *, uint32, uint32);
38*412f47f9SXin Li static void minmax_cases_float(uint32 *, uint32, uint32);
39*412f47f9SXin Li static void atan2_cases(uint32 *, uint32, uint32);
40*412f47f9SXin Li static void atan2_cases_float(uint32 *, uint32, uint32);
41*412f47f9SXin Li static void pow_cases(uint32 *, uint32, uint32);
42*412f47f9SXin Li static void pow_cases_float(uint32 *, uint32, uint32);
43*412f47f9SXin Li static void rred_cases(uint32 *, uint32, uint32);
44*412f47f9SXin Li static void rred_cases_float(uint32 *, uint32, uint32);
45*412f47f9SXin Li static void cases_semi1(uint32 *, uint32, uint32);
46*412f47f9SXin Li static void cases_semi1_float(uint32 *, uint32, uint32);
47*412f47f9SXin Li static void cases_semi2(uint32 *, uint32, uint32);
48*412f47f9SXin Li static void cases_semi2_float(uint32 *, uint32, uint32);
49*412f47f9SXin Li static void cases_ldexp(uint32 *, uint32, uint32);
50*412f47f9SXin Li static void cases_ldexp_float(uint32 *, uint32, uint32);
51*412f47f9SXin Li 
52*412f47f9SXin Li static void complex_cases_uniform(uint32 *, uint32, uint32);
53*412f47f9SXin Li static void complex_cases_uniform_float(uint32 *, uint32, uint32);
54*412f47f9SXin Li static void complex_cases_biased(uint32 *, uint32, uint32);
55*412f47f9SXin Li static void complex_cases_biased_float(uint32 *, uint32, uint32);
56*412f47f9SXin Li static void complex_log_cases(uint32 *, uint32, uint32);
57*412f47f9SXin Li static void complex_log_cases_float(uint32 *, uint32, uint32);
58*412f47f9SXin Li static void complex_pow_cases(uint32 *, uint32, uint32);
59*412f47f9SXin Li static void complex_pow_cases_float(uint32 *, uint32, uint32);
60*412f47f9SXin Li static void complex_arithmetic_cases(uint32 *, uint32, uint32);
61*412f47f9SXin Li static void complex_arithmetic_cases_float(uint32 *, uint32, uint32);
62*412f47f9SXin Li 
63*412f47f9SXin Li static uint32 doubletop(int x, int scale);
64*412f47f9SXin Li static uint32 floatval(int x, int scale);
65*412f47f9SXin Li 
66*412f47f9SXin Li /*
67*412f47f9SXin Li  * Convert back and forth between IEEE bit patterns and the
68*412f47f9SXin Li  * mpfr_t/mpc_t types.
69*412f47f9SXin Li  */
set_mpfr_d(mpfr_t x,uint32 h,uint32 l)70*412f47f9SXin Li static void set_mpfr_d(mpfr_t x, uint32 h, uint32 l)
71*412f47f9SXin Li {
72*412f47f9SXin Li     uint64_t hl = ((uint64_t)h << 32) | l;
73*412f47f9SXin Li     uint32 exp = (hl >> 52) & 0x7ff;
74*412f47f9SXin Li     int64_t mantissa = hl & (((uint64_t)1 << 52) - 1);
75*412f47f9SXin Li     int sign = (hl >> 63) ? -1 : +1;
76*412f47f9SXin Li     if (exp == 0x7ff) {
77*412f47f9SXin Li         if (mantissa == 0)
78*412f47f9SXin Li             mpfr_set_inf(x, sign);
79*412f47f9SXin Li         else
80*412f47f9SXin Li             mpfr_set_nan(x);
81*412f47f9SXin Li     } else if (exp == 0 && mantissa == 0) {
82*412f47f9SXin Li         mpfr_set_ui(x, 0, GMP_RNDN);
83*412f47f9SXin Li         mpfr_setsign(x, x, sign < 0, GMP_RNDN);
84*412f47f9SXin Li     } else {
85*412f47f9SXin Li         if (exp != 0)
86*412f47f9SXin Li             mantissa |= ((uint64_t)1 << 52);
87*412f47f9SXin Li         else
88*412f47f9SXin Li             exp++;
89*412f47f9SXin Li         mpfr_set_sj_2exp(x, mantissa * sign, (int)exp - 0x3ff - 52, GMP_RNDN);
90*412f47f9SXin Li     }
91*412f47f9SXin Li }
set_mpfr_f(mpfr_t x,uint32 f)92*412f47f9SXin Li static void set_mpfr_f(mpfr_t x, uint32 f)
93*412f47f9SXin Li {
94*412f47f9SXin Li     uint32 exp = (f >> 23) & 0xff;
95*412f47f9SXin Li     int32 mantissa = f & ((1 << 23) - 1);
96*412f47f9SXin Li     int sign = (f >> 31) ? -1 : +1;
97*412f47f9SXin Li     if (exp == 0xff) {
98*412f47f9SXin Li         if (mantissa == 0)
99*412f47f9SXin Li             mpfr_set_inf(x, sign);
100*412f47f9SXin Li         else
101*412f47f9SXin Li             mpfr_set_nan(x);
102*412f47f9SXin Li     } else if (exp == 0 && mantissa == 0) {
103*412f47f9SXin Li         mpfr_set_ui(x, 0, GMP_RNDN);
104*412f47f9SXin Li         mpfr_setsign(x, x, sign < 0, GMP_RNDN);
105*412f47f9SXin Li     } else {
106*412f47f9SXin Li         if (exp != 0)
107*412f47f9SXin Li             mantissa |= (1 << 23);
108*412f47f9SXin Li         else
109*412f47f9SXin Li             exp++;
110*412f47f9SXin Li         mpfr_set_sj_2exp(x, mantissa * sign, (int)exp - 0x7f - 23, GMP_RNDN);
111*412f47f9SXin Li     }
112*412f47f9SXin Li }
set_mpc_d(mpc_t z,uint32 rh,uint32 rl,uint32 ih,uint32 il)113*412f47f9SXin Li static void set_mpc_d(mpc_t z, uint32 rh, uint32 rl, uint32 ih, uint32 il)
114*412f47f9SXin Li {
115*412f47f9SXin Li     mpfr_t x, y;
116*412f47f9SXin Li     mpfr_init2(x, MPFR_PREC);
117*412f47f9SXin Li     mpfr_init2(y, MPFR_PREC);
118*412f47f9SXin Li     set_mpfr_d(x, rh, rl);
119*412f47f9SXin Li     set_mpfr_d(y, ih, il);
120*412f47f9SXin Li     mpc_set_fr_fr(z, x, y, MPC_RNDNN);
121*412f47f9SXin Li     mpfr_clear(x);
122*412f47f9SXin Li     mpfr_clear(y);
123*412f47f9SXin Li }
set_mpc_f(mpc_t z,uint32 r,uint32 i)124*412f47f9SXin Li static void set_mpc_f(mpc_t z, uint32 r, uint32 i)
125*412f47f9SXin Li {
126*412f47f9SXin Li     mpfr_t x, y;
127*412f47f9SXin Li     mpfr_init2(x, MPFR_PREC);
128*412f47f9SXin Li     mpfr_init2(y, MPFR_PREC);
129*412f47f9SXin Li     set_mpfr_f(x, r);
130*412f47f9SXin Li     set_mpfr_f(y, i);
131*412f47f9SXin Li     mpc_set_fr_fr(z, x, y, MPC_RNDNN);
132*412f47f9SXin Li     mpfr_clear(x);
133*412f47f9SXin Li     mpfr_clear(y);
134*412f47f9SXin Li }
get_mpfr_d(const mpfr_t x,uint32 * h,uint32 * l,uint32 * extra)135*412f47f9SXin Li static void get_mpfr_d(const mpfr_t x, uint32 *h, uint32 *l, uint32 *extra)
136*412f47f9SXin Li {
137*412f47f9SXin Li     uint32_t sign, expfield, mantfield;
138*412f47f9SXin Li     mpfr_t significand;
139*412f47f9SXin Li     int exp;
140*412f47f9SXin Li 
141*412f47f9SXin Li     if (mpfr_nan_p(x)) {
142*412f47f9SXin Li         *h = 0x7ff80000;
143*412f47f9SXin Li         *l = 0;
144*412f47f9SXin Li         *extra = 0;
145*412f47f9SXin Li         return;
146*412f47f9SXin Li     }
147*412f47f9SXin Li 
148*412f47f9SXin Li     sign = mpfr_signbit(x) ? 0x80000000U : 0;
149*412f47f9SXin Li 
150*412f47f9SXin Li     if (mpfr_inf_p(x)) {
151*412f47f9SXin Li         *h = 0x7ff00000 | sign;
152*412f47f9SXin Li         *l = 0;
153*412f47f9SXin Li         *extra = 0;
154*412f47f9SXin Li         return;
155*412f47f9SXin Li     }
156*412f47f9SXin Li 
157*412f47f9SXin Li     if (mpfr_zero_p(x)) {
158*412f47f9SXin Li         *h = 0x00000000 | sign;
159*412f47f9SXin Li         *l = 0;
160*412f47f9SXin Li         *extra = 0;
161*412f47f9SXin Li         return;
162*412f47f9SXin Li     }
163*412f47f9SXin Li 
164*412f47f9SXin Li     mpfr_init2(significand, MPFR_PREC);
165*412f47f9SXin Li     mpfr_set(significand, x, GMP_RNDN);
166*412f47f9SXin Li     exp = mpfr_get_exp(significand);
167*412f47f9SXin Li     mpfr_set_exp(significand, 0);
168*412f47f9SXin Li 
169*412f47f9SXin Li     /* Now significand is in [1/2,1), and significand * 2^exp == x.
170*412f47f9SXin Li      * So the IEEE exponent corresponding to exp==0 is 0x3fe. */
171*412f47f9SXin Li     if (exp > 0x400) {
172*412f47f9SXin Li         /* overflow to infinity anyway */
173*412f47f9SXin Li         *h = 0x7ff00000 | sign;
174*412f47f9SXin Li         *l = 0;
175*412f47f9SXin Li         *extra = 0;
176*412f47f9SXin Li         mpfr_clear(significand);
177*412f47f9SXin Li         return;
178*412f47f9SXin Li     }
179*412f47f9SXin Li 
180*412f47f9SXin Li     if (exp <= -0x3fe || mpfr_zero_p(x))
181*412f47f9SXin Li         exp = -0x3fd;       /* denormalise */
182*412f47f9SXin Li     expfield = exp + 0x3fd; /* offset to cancel leading mantissa bit */
183*412f47f9SXin Li 
184*412f47f9SXin Li     mpfr_div_2si(significand, x, exp - 21, GMP_RNDN);
185*412f47f9SXin Li     mpfr_abs(significand, significand, GMP_RNDN);
186*412f47f9SXin Li     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
187*412f47f9SXin Li     *h = sign + ((uint64_t)expfield << 20) + mantfield;
188*412f47f9SXin Li     mpfr_sub_ui(significand, significand, mantfield, GMP_RNDN);
189*412f47f9SXin Li     mpfr_mul_2ui(significand, significand, 32, GMP_RNDN);
190*412f47f9SXin Li     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
191*412f47f9SXin Li     *l = mantfield;
192*412f47f9SXin Li     mpfr_sub_ui(significand, significand, mantfield, GMP_RNDN);
193*412f47f9SXin Li     mpfr_mul_2ui(significand, significand, 32, GMP_RNDN);
194*412f47f9SXin Li     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
195*412f47f9SXin Li     *extra = mantfield;
196*412f47f9SXin Li 
197*412f47f9SXin Li     mpfr_clear(significand);
198*412f47f9SXin Li }
get_mpfr_f(const mpfr_t x,uint32 * f,uint32 * extra)199*412f47f9SXin Li static void get_mpfr_f(const mpfr_t x, uint32 *f, uint32 *extra)
200*412f47f9SXin Li {
201*412f47f9SXin Li     uint32_t sign, expfield, mantfield;
202*412f47f9SXin Li     mpfr_t significand;
203*412f47f9SXin Li     int exp;
204*412f47f9SXin Li 
205*412f47f9SXin Li     if (mpfr_nan_p(x)) {
206*412f47f9SXin Li         *f = 0x7fc00000;
207*412f47f9SXin Li         *extra = 0;
208*412f47f9SXin Li         return;
209*412f47f9SXin Li     }
210*412f47f9SXin Li 
211*412f47f9SXin Li     sign = mpfr_signbit(x) ? 0x80000000U : 0;
212*412f47f9SXin Li 
213*412f47f9SXin Li     if (mpfr_inf_p(x)) {
214*412f47f9SXin Li         *f = 0x7f800000 | sign;
215*412f47f9SXin Li         *extra = 0;
216*412f47f9SXin Li         return;
217*412f47f9SXin Li     }
218*412f47f9SXin Li 
219*412f47f9SXin Li     if (mpfr_zero_p(x)) {
220*412f47f9SXin Li         *f = 0x00000000 | sign;
221*412f47f9SXin Li         *extra = 0;
222*412f47f9SXin Li         return;
223*412f47f9SXin Li     }
224*412f47f9SXin Li 
225*412f47f9SXin Li     mpfr_init2(significand, MPFR_PREC);
226*412f47f9SXin Li     mpfr_set(significand, x, GMP_RNDN);
227*412f47f9SXin Li     exp = mpfr_get_exp(significand);
228*412f47f9SXin Li     mpfr_set_exp(significand, 0);
229*412f47f9SXin Li 
230*412f47f9SXin Li     /* Now significand is in [1/2,1), and significand * 2^exp == x.
231*412f47f9SXin Li      * So the IEEE exponent corresponding to exp==0 is 0x7e. */
232*412f47f9SXin Li     if (exp > 0x80) {
233*412f47f9SXin Li         /* overflow to infinity anyway */
234*412f47f9SXin Li         *f = 0x7f800000 | sign;
235*412f47f9SXin Li         *extra = 0;
236*412f47f9SXin Li         mpfr_clear(significand);
237*412f47f9SXin Li         return;
238*412f47f9SXin Li     }
239*412f47f9SXin Li 
240*412f47f9SXin Li     if (exp <= -0x7e || mpfr_zero_p(x))
241*412f47f9SXin Li         exp = -0x7d;                   /* denormalise */
242*412f47f9SXin Li     expfield = exp + 0x7d; /* offset to cancel leading mantissa bit */
243*412f47f9SXin Li 
244*412f47f9SXin Li     mpfr_div_2si(significand, x, exp - 24, GMP_RNDN);
245*412f47f9SXin Li     mpfr_abs(significand, significand, GMP_RNDN);
246*412f47f9SXin Li     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
247*412f47f9SXin Li     *f = sign + ((uint64_t)expfield << 23) + mantfield;
248*412f47f9SXin Li     mpfr_sub_ui(significand, significand, mantfield, GMP_RNDN);
249*412f47f9SXin Li     mpfr_mul_2ui(significand, significand, 32, GMP_RNDN);
250*412f47f9SXin Li     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
251*412f47f9SXin Li     *extra = mantfield;
252*412f47f9SXin Li 
253*412f47f9SXin Li     mpfr_clear(significand);
254*412f47f9SXin Li }
get_mpc_d(const mpc_t z,uint32 * rh,uint32 * rl,uint32 * rextra,uint32 * ih,uint32 * il,uint32 * iextra)255*412f47f9SXin Li static void get_mpc_d(const mpc_t z,
256*412f47f9SXin Li                       uint32 *rh, uint32 *rl, uint32 *rextra,
257*412f47f9SXin Li                       uint32 *ih, uint32 *il, uint32 *iextra)
258*412f47f9SXin Li {
259*412f47f9SXin Li     mpfr_t x, y;
260*412f47f9SXin Li     mpfr_init2(x, MPFR_PREC);
261*412f47f9SXin Li     mpfr_init2(y, MPFR_PREC);
262*412f47f9SXin Li     mpc_real(x, z, GMP_RNDN);
263*412f47f9SXin Li     mpc_imag(y, z, GMP_RNDN);
264*412f47f9SXin Li     get_mpfr_d(x, rh, rl, rextra);
265*412f47f9SXin Li     get_mpfr_d(y, ih, il, iextra);
266*412f47f9SXin Li     mpfr_clear(x);
267*412f47f9SXin Li     mpfr_clear(y);
268*412f47f9SXin Li }
get_mpc_f(const mpc_t z,uint32 * r,uint32 * rextra,uint32 * i,uint32 * iextra)269*412f47f9SXin Li static void get_mpc_f(const mpc_t z,
270*412f47f9SXin Li                       uint32 *r, uint32 *rextra,
271*412f47f9SXin Li                       uint32 *i, uint32 *iextra)
272*412f47f9SXin Li {
273*412f47f9SXin Li     mpfr_t x, y;
274*412f47f9SXin Li     mpfr_init2(x, MPFR_PREC);
275*412f47f9SXin Li     mpfr_init2(y, MPFR_PREC);
276*412f47f9SXin Li     mpc_real(x, z, GMP_RNDN);
277*412f47f9SXin Li     mpc_imag(y, z, GMP_RNDN);
278*412f47f9SXin Li     get_mpfr_f(x, r, rextra);
279*412f47f9SXin Li     get_mpfr_f(y, i, iextra);
280*412f47f9SXin Li     mpfr_clear(x);
281*412f47f9SXin Li     mpfr_clear(y);
282*412f47f9SXin Li }
283*412f47f9SXin Li 
284*412f47f9SXin Li /*
285*412f47f9SXin Li  * Implementation of mathlib functions that aren't trivially
286*412f47f9SXin Li  * implementable using an existing mpfr or mpc function.
287*412f47f9SXin Li  */
test_rred(mpfr_t ret,const mpfr_t x,int * quadrant)288*412f47f9SXin Li int test_rred(mpfr_t ret, const mpfr_t x, int *quadrant)
289*412f47f9SXin Li {
290*412f47f9SXin Li     mpfr_t halfpi;
291*412f47f9SXin Li     long quo;
292*412f47f9SXin Li     int status;
293*412f47f9SXin Li 
294*412f47f9SXin Li     /*
295*412f47f9SXin Li      * In the worst case of range reduction, we get an input of size
296*412f47f9SXin Li      * around 2^1024, and must find its remainder mod pi, which means
297*412f47f9SXin Li      * we need 1024 bits of pi at least. Plus, the remainder might
298*412f47f9SXin Li      * happen to come out very very small if we're unlucky. How
299*412f47f9SXin Li      * unlucky can we be? Well, conveniently, I once went through and
300*412f47f9SXin Li      * actually worked that out using Paxson's modular minimisation
301*412f47f9SXin Li      * algorithm, and it turns out that the smallest exponent you can
302*412f47f9SXin Li      * get out of a nontrivial[1] double precision range reduction is
303*412f47f9SXin Li      * 0x3c2, i.e. of the order of 2^-61. So we need 1024 bits of pi
304*412f47f9SXin Li      * to get us down to the units digit, another 61 or so bits (say
305*412f47f9SXin Li      * 64) to get down to the highest set bit of the output, and then
306*412f47f9SXin Li      * some bits to make the actual mantissa big enough.
307*412f47f9SXin Li      *
308*412f47f9SXin Li      *   [1] of course the output of range reduction can have an
309*412f47f9SXin Li      *   arbitrarily small exponent in the trivial case, where the
310*412f47f9SXin Li      *   input is so small that it's the identity function. That
311*412f47f9SXin Li      *   doesn't count.
312*412f47f9SXin Li      */
313*412f47f9SXin Li     mpfr_init2(halfpi, MPFR_PREC + 1024 + 64);
314*412f47f9SXin Li     mpfr_const_pi(halfpi, GMP_RNDN);
315*412f47f9SXin Li     mpfr_div_ui(halfpi, halfpi, 2, GMP_RNDN);
316*412f47f9SXin Li 
317*412f47f9SXin Li     status = mpfr_remquo(ret, &quo, x, halfpi, GMP_RNDN);
318*412f47f9SXin Li     *quadrant = quo & 3;
319*412f47f9SXin Li 
320*412f47f9SXin Li     mpfr_clear(halfpi);
321*412f47f9SXin Li 
322*412f47f9SXin Li     return status;
323*412f47f9SXin Li }
test_lgamma(mpfr_t ret,const mpfr_t x,mpfr_rnd_t rnd)324*412f47f9SXin Li int test_lgamma(mpfr_t ret, const mpfr_t x, mpfr_rnd_t rnd)
325*412f47f9SXin Li {
326*412f47f9SXin Li     /*
327*412f47f9SXin Li      * mpfr_lgamma takes an extra int * parameter to hold the output
328*412f47f9SXin Li      * sign. We don't bother testing that, so this wrapper throws away
329*412f47f9SXin Li      * the sign and hence fits into the same function prototype as all
330*412f47f9SXin Li      * the other real->real mpfr functions.
331*412f47f9SXin Li      *
332*412f47f9SXin Li      * There is also mpfr_lngamma which has no sign output and hence
333*412f47f9SXin Li      * has the right prototype already, but unfortunately it returns
334*412f47f9SXin Li      * NaN in cases where gamma(x) < 0, so it's no use to us.
335*412f47f9SXin Li      */
336*412f47f9SXin Li     int sign;
337*412f47f9SXin Li     return mpfr_lgamma(ret, &sign, x, rnd);
338*412f47f9SXin Li }
test_cpow(mpc_t ret,const mpc_t x,const mpc_t y,mpc_rnd_t rnd)339*412f47f9SXin Li int test_cpow(mpc_t ret, const mpc_t x, const mpc_t y, mpc_rnd_t rnd)
340*412f47f9SXin Li {
341*412f47f9SXin Li     /*
342*412f47f9SXin Li      * For complex pow, we must bump up the precision by a huge amount
343*412f47f9SXin Li      * if we want it to get the really difficult cases right. (Not
344*412f47f9SXin Li      * that we expect the library under test to be getting those cases
345*412f47f9SXin Li      * right itself, but we'd at least like the test suite to report
346*412f47f9SXin Li      * them as wrong for the _right reason_.)
347*412f47f9SXin Li      *
348*412f47f9SXin Li      * This works around a bug in mpc_pow(), fixed by r1455 in the MPC
349*412f47f9SXin Li      * svn repository (2014-10-14) and expected to be in any MPC
350*412f47f9SXin Li      * release after 1.0.2 (which was the latest release already made
351*412f47f9SXin Li      * at the time of the fix). So as and when we update to an MPC
352*412f47f9SXin Li      * with the fix in it, we could remove this workaround.
353*412f47f9SXin Li      *
354*412f47f9SXin Li      * For the reasons for choosing this amount of extra precision,
355*412f47f9SXin Li      * see analysis in complex/cpownotes.txt for the rationale for the
356*412f47f9SXin Li      * amount.
357*412f47f9SXin Li      */
358*412f47f9SXin Li     mpc_t xbig, ybig, retbig;
359*412f47f9SXin Li     int status;
360*412f47f9SXin Li 
361*412f47f9SXin Li     mpc_init2(xbig, 1034 + 53 + 60 + MPFR_PREC);
362*412f47f9SXin Li     mpc_init2(ybig, 1034 + 53 + 60 + MPFR_PREC);
363*412f47f9SXin Li     mpc_init2(retbig, 1034 + 53 + 60 + MPFR_PREC);
364*412f47f9SXin Li 
365*412f47f9SXin Li     mpc_set(xbig, x, MPC_RNDNN);
366*412f47f9SXin Li     mpc_set(ybig, y, MPC_RNDNN);
367*412f47f9SXin Li     status = mpc_pow(retbig, xbig, ybig, rnd);
368*412f47f9SXin Li     mpc_set(ret, retbig, rnd);
369*412f47f9SXin Li 
370*412f47f9SXin Li     mpc_clear(xbig);
371*412f47f9SXin Li     mpc_clear(ybig);
372*412f47f9SXin Li     mpc_clear(retbig);
373*412f47f9SXin Li 
374*412f47f9SXin Li     return status;
375*412f47f9SXin Li }
376*412f47f9SXin Li 
377*412f47f9SXin Li /*
378*412f47f9SXin Li  * Identify 'hard' values (NaN, Inf, nonzero denormal) for deciding
379*412f47f9SXin Li  * whether microlib will decline to run a test.
380*412f47f9SXin Li  */
381*412f47f9SXin Li #define is_shard(in) ( \
382*412f47f9SXin Li     (((in)[0] & 0x7F800000) == 0x7F800000 || \
383*412f47f9SXin Li      (((in)[0] & 0x7F800000) == 0 && ((in)[0]&0x7FFFFFFF) != 0)))
384*412f47f9SXin Li 
385*412f47f9SXin Li #define is_dhard(in) ( \
386*412f47f9SXin Li     (((in)[0] & 0x7FF00000) == 0x7FF00000 || \
387*412f47f9SXin Li      (((in)[0] & 0x7FF00000) == 0 && (((in)[0] & 0xFFFFF) | (in)[1]) != 0)))
388*412f47f9SXin Li 
389*412f47f9SXin Li /*
390*412f47f9SXin Li  * Identify integers.
391*412f47f9SXin Li  */
is_dinteger(uint32 * in)392*412f47f9SXin Li int is_dinteger(uint32 *in)
393*412f47f9SXin Li {
394*412f47f9SXin Li     uint32 out[3];
395*412f47f9SXin Li     if ((0x7FF00000 & ~in[0]) == 0)
396*412f47f9SXin Li         return 0;                      /* not finite, hence not integer */
397*412f47f9SXin Li     test_ceil(in, out);
398*412f47f9SXin Li     return in[0] == out[0] && in[1] == out[1];
399*412f47f9SXin Li }
is_sinteger(uint32 * in)400*412f47f9SXin Li int is_sinteger(uint32 *in)
401*412f47f9SXin Li {
402*412f47f9SXin Li     uint32 out[3];
403*412f47f9SXin Li     if ((0x7F800000 & ~in[0]) == 0)
404*412f47f9SXin Li         return 0;                      /* not finite, hence not integer */
405*412f47f9SXin Li     test_ceilf(in, out);
406*412f47f9SXin Li     return in[0] == out[0];
407*412f47f9SXin Li }
408*412f47f9SXin Li 
409*412f47f9SXin Li /*
410*412f47f9SXin Li  * Identify signalling NaNs.
411*412f47f9SXin Li  */
is_dsnan(const uint32 * in)412*412f47f9SXin Li int is_dsnan(const uint32 *in)
413*412f47f9SXin Li {
414*412f47f9SXin Li     if ((in[0] & 0x7FF00000) != 0x7FF00000)
415*412f47f9SXin Li         return 0;                      /* not the inf/nan exponent */
416*412f47f9SXin Li     if ((in[0] << 12) == 0 && in[1] == 0)
417*412f47f9SXin Li         return 0;                      /* inf */
418*412f47f9SXin Li     if (in[0] & 0x00080000)
419*412f47f9SXin Li         return 0;                      /* qnan */
420*412f47f9SXin Li     return 1;
421*412f47f9SXin Li }
is_ssnan(const uint32 * in)422*412f47f9SXin Li int is_ssnan(const uint32 *in)
423*412f47f9SXin Li {
424*412f47f9SXin Li     if ((in[0] & 0x7F800000) != 0x7F800000)
425*412f47f9SXin Li         return 0;                      /* not the inf/nan exponent */
426*412f47f9SXin Li     if ((in[0] << 9) == 0)
427*412f47f9SXin Li         return 0;                      /* inf */
428*412f47f9SXin Li     if (in[0] & 0x00400000)
429*412f47f9SXin Li         return 0;                      /* qnan */
430*412f47f9SXin Li     return 1;
431*412f47f9SXin Li }
is_snan(const uint32 * in,int size)432*412f47f9SXin Li int is_snan(const uint32 *in, int size)
433*412f47f9SXin Li {
434*412f47f9SXin Li     return size == 2 ? is_dsnan(in) : is_ssnan(in);
435*412f47f9SXin Li }
436*412f47f9SXin Li 
437*412f47f9SXin Li /*
438*412f47f9SXin Li  * Wrapper functions called to fix up unusual results after the main
439*412f47f9SXin Li  * test function has run.
440*412f47f9SXin Li  */
universal_wrapper(wrapperctx * ctx)441*412f47f9SXin Li void universal_wrapper(wrapperctx *ctx)
442*412f47f9SXin Li {
443*412f47f9SXin Li     /*
444*412f47f9SXin Li      * Any SNaN input gives rise to a QNaN output.
445*412f47f9SXin Li      */
446*412f47f9SXin Li     int op;
447*412f47f9SXin Li     for (op = 0; op < wrapper_get_nops(ctx); op++) {
448*412f47f9SXin Li         int size = wrapper_get_size(ctx, op);
449*412f47f9SXin Li 
450*412f47f9SXin Li         if (!wrapper_is_complex(ctx, op) &&
451*412f47f9SXin Li             is_snan(wrapper_get_ieee(ctx, op), size)) {
452*412f47f9SXin Li             wrapper_set_nan(ctx);
453*412f47f9SXin Li         }
454*412f47f9SXin Li     }
455*412f47f9SXin Li }
456*412f47f9SXin Li 
457*412f47f9SXin Li Testable functions[] = {
458*412f47f9SXin Li     /*
459*412f47f9SXin Li      * Trig functions: sin, cos, tan. We test the core function
460*412f47f9SXin Li      * between -16 and +16: we assume that range reduction exists
461*412f47f9SXin Li      * and will be used for larger arguments, and we'll test that
462*412f47f9SXin Li      * separately. Also we only go down to 2^-27 in magnitude,
463*412f47f9SXin Li      * because below that sin(x)=tan(x)=x and cos(x)=1 as far as
464*412f47f9SXin Li      * double precision can tell, which is boring.
465*412f47f9SXin Li      */
466*412f47f9SXin Li     {"sin", (funcptr)mpfr_sin, args1, {NULL},
467*412f47f9SXin Li         cases_uniform, 0x3e400000, 0x40300000},
468*412f47f9SXin Li     {"sinf", (funcptr)mpfr_sin, args1f, {NULL},
469*412f47f9SXin Li         cases_uniform_float, 0x39800000, 0x41800000},
470*412f47f9SXin Li     {"cos", (funcptr)mpfr_cos, args1, {NULL},
471*412f47f9SXin Li         cases_uniform, 0x3e400000, 0x40300000},
472*412f47f9SXin Li     {"cosf", (funcptr)mpfr_cos, args1f, {NULL},
473*412f47f9SXin Li         cases_uniform_float, 0x39800000, 0x41800000},
474*412f47f9SXin Li     {"tan", (funcptr)mpfr_tan, args1, {NULL},
475*412f47f9SXin Li         cases_uniform, 0x3e400000, 0x40300000},
476*412f47f9SXin Li     {"tanf", (funcptr)mpfr_tan, args1f, {NULL},
477*412f47f9SXin Li         cases_uniform_float, 0x39800000, 0x41800000},
478*412f47f9SXin Li     {"sincosf_sinf", (funcptr)mpfr_sin, args1f, {NULL},
479*412f47f9SXin Li         cases_uniform_float, 0x39800000, 0x41800000},
480*412f47f9SXin Li     {"sincosf_cosf", (funcptr)mpfr_cos, args1f, {NULL},
481*412f47f9SXin Li         cases_uniform_float, 0x39800000, 0x41800000},
482*412f47f9SXin Li     /*
483*412f47f9SXin Li      * Inverse trig: asin, acos. Between 1 and -1, of course. acos
484*412f47f9SXin Li      * goes down to 2^-54, asin to 2^-27.
485*412f47f9SXin Li      */
486*412f47f9SXin Li     {"asin", (funcptr)mpfr_asin, args1, {NULL},
487*412f47f9SXin Li         cases_uniform, 0x3e400000, 0x3fefffff},
488*412f47f9SXin Li     {"asinf", (funcptr)mpfr_asin, args1f, {NULL},
489*412f47f9SXin Li         cases_uniform_float, 0x39800000, 0x3f7fffff},
490*412f47f9SXin Li     {"acos", (funcptr)mpfr_acos, args1, {NULL},
491*412f47f9SXin Li         cases_uniform, 0x3c900000, 0x3fefffff},
492*412f47f9SXin Li     {"acosf", (funcptr)mpfr_acos, args1f, {NULL},
493*412f47f9SXin Li         cases_uniform_float, 0x33800000, 0x3f7fffff},
494*412f47f9SXin Li     /*
495*412f47f9SXin Li      * Inverse trig: atan. atan is stable (in double prec) with
496*412f47f9SXin Li      * argument magnitude past 2^53, so we'll test up to there.
497*412f47f9SXin Li      * atan(x) is boringly just x below 2^-27.
498*412f47f9SXin Li      */
499*412f47f9SXin Li     {"atan", (funcptr)mpfr_atan, args1, {NULL},
500*412f47f9SXin Li         cases_uniform, 0x3e400000, 0x43400000},
501*412f47f9SXin Li     {"atanf", (funcptr)mpfr_atan, args1f, {NULL},
502*412f47f9SXin Li         cases_uniform_float, 0x39800000, 0x4b800000},
503*412f47f9SXin Li     /*
504*412f47f9SXin Li      * atan2. Interesting cases arise when the exponents of the
505*412f47f9SXin Li      * arguments differ by at most about 50.
506*412f47f9SXin Li      */
507*412f47f9SXin Li     {"atan2", (funcptr)mpfr_atan2, args2, {NULL},
508*412f47f9SXin Li         atan2_cases, 0},
509*412f47f9SXin Li     {"atan2f", (funcptr)mpfr_atan2, args2f, {NULL},
510*412f47f9SXin Li         atan2_cases_float, 0},
511*412f47f9SXin Li     /*
512*412f47f9SXin Li      * The exponentials: exp, sinh, cosh. They overflow at around
513*412f47f9SXin Li      * 710. exp and sinh are boring below 2^-54, cosh below 2^-27.
514*412f47f9SXin Li      */
515*412f47f9SXin Li     {"exp", (funcptr)mpfr_exp, args1, {NULL},
516*412f47f9SXin Li         cases_uniform, 0x3c900000, 0x40878000},
517*412f47f9SXin Li     {"expf", (funcptr)mpfr_exp, args1f, {NULL},
518*412f47f9SXin Li         cases_uniform_float, 0x33800000, 0x42dc0000},
519*412f47f9SXin Li     {"sinh", (funcptr)mpfr_sinh, args1, {NULL},
520*412f47f9SXin Li         cases_uniform, 0x3c900000, 0x40878000},
521*412f47f9SXin Li     {"sinhf", (funcptr)mpfr_sinh, args1f, {NULL},
522*412f47f9SXin Li         cases_uniform_float, 0x33800000, 0x42dc0000},
523*412f47f9SXin Li     {"cosh", (funcptr)mpfr_cosh, args1, {NULL},
524*412f47f9SXin Li         cases_uniform, 0x3e400000, 0x40878000},
525*412f47f9SXin Li     {"coshf", (funcptr)mpfr_cosh, args1f, {NULL},
526*412f47f9SXin Li         cases_uniform_float, 0x39800000, 0x42dc0000},
527*412f47f9SXin Li     /*
528*412f47f9SXin Li      * tanh is stable past around 20. It's boring below 2^-27.
529*412f47f9SXin Li      */
530*412f47f9SXin Li     {"tanh", (funcptr)mpfr_tanh, args1, {NULL},
531*412f47f9SXin Li         cases_uniform, 0x3e400000, 0x40340000},
532*412f47f9SXin Li     {"tanhf", (funcptr)mpfr_tanh, args1f, {NULL},
533*412f47f9SXin Li         cases_uniform, 0x39800000, 0x41100000},
534*412f47f9SXin Li     /*
535*412f47f9SXin Li      * log must be tested only on positive numbers, but can cover
536*412f47f9SXin Li      * the whole range of positive nonzero finite numbers. It never
537*412f47f9SXin Li      * gets boring.
538*412f47f9SXin Li      */
539*412f47f9SXin Li     {"log", (funcptr)mpfr_log, args1, {NULL}, log_cases, 0},
540*412f47f9SXin Li     {"logf", (funcptr)mpfr_log, args1f, {NULL}, log_cases_float, 0},
541*412f47f9SXin Li     {"log10", (funcptr)mpfr_log10, args1, {NULL}, log_cases, 0},
542*412f47f9SXin Li     {"log10f", (funcptr)mpfr_log10, args1f, {NULL}, log_cases_float, 0},
543*412f47f9SXin Li     /*
544*412f47f9SXin Li      * pow.
545*412f47f9SXin Li      */
546*412f47f9SXin Li     {"pow", (funcptr)mpfr_pow, args2, {NULL}, pow_cases, 0},
547*412f47f9SXin Li     {"powf", (funcptr)mpfr_pow, args2f, {NULL}, pow_cases_float, 0},
548*412f47f9SXin Li     /*
549*412f47f9SXin Li      * Trig range reduction. We are able to test this for all
550*412f47f9SXin Li      * finite values, but will only bother for things between 2^-3
551*412f47f9SXin Li      * and 2^+52.
552*412f47f9SXin Li      */
553*412f47f9SXin Li     {"rred", (funcptr)test_rred, rred, {NULL}, rred_cases, 0},
554*412f47f9SXin Li     {"rredf", (funcptr)test_rred, rredf, {NULL}, rred_cases_float, 0},
555*412f47f9SXin Li     /*
556*412f47f9SXin Li      * Square and cube root.
557*412f47f9SXin Li      */
558*412f47f9SXin Li     {"sqrt", (funcptr)mpfr_sqrt, args1, {NULL}, log_cases, 0},
559*412f47f9SXin Li     {"sqrtf", (funcptr)mpfr_sqrt, args1f, {NULL}, log_cases_float, 0},
560*412f47f9SXin Li     {"cbrt", (funcptr)mpfr_cbrt, args1, {NULL}, log_cases, 0},
561*412f47f9SXin Li     {"cbrtf", (funcptr)mpfr_cbrt, args1f, {NULL}, log_cases_float, 0},
562*412f47f9SXin Li     {"hypot", (funcptr)mpfr_hypot, args2, {NULL}, atan2_cases, 0},
563*412f47f9SXin Li     {"hypotf", (funcptr)mpfr_hypot, args2f, {NULL}, atan2_cases_float, 0},
564*412f47f9SXin Li     /*
565*412f47f9SXin Li      * Seminumerical functions.
566*412f47f9SXin Li      */
567*412f47f9SXin Li     {"ceil", (funcptr)test_ceil, semi1, {NULL}, cases_semi1},
568*412f47f9SXin Li     {"ceilf", (funcptr)test_ceilf, semi1f, {NULL}, cases_semi1_float},
569*412f47f9SXin Li     {"floor", (funcptr)test_floor, semi1, {NULL}, cases_semi1},
570*412f47f9SXin Li     {"floorf", (funcptr)test_floorf, semi1f, {NULL}, cases_semi1_float},
571*412f47f9SXin Li     {"fmod", (funcptr)test_fmod, semi2, {NULL}, cases_semi2},
572*412f47f9SXin Li     {"fmodf", (funcptr)test_fmodf, semi2f, {NULL}, cases_semi2_float},
573*412f47f9SXin Li     {"ldexp", (funcptr)test_ldexp, t_ldexp, {NULL}, cases_ldexp},
574*412f47f9SXin Li     {"ldexpf", (funcptr)test_ldexpf, t_ldexpf, {NULL}, cases_ldexp_float},
575*412f47f9SXin Li     {"frexp", (funcptr)test_frexp, t_frexp, {NULL}, cases_semi1},
576*412f47f9SXin Li     {"frexpf", (funcptr)test_frexpf, t_frexpf, {NULL}, cases_semi1_float},
577*412f47f9SXin Li     {"modf", (funcptr)test_modf, t_modf, {NULL}, cases_semi1},
578*412f47f9SXin Li     {"modff", (funcptr)test_modff, t_modff, {NULL}, cases_semi1_float},
579*412f47f9SXin Li 
580*412f47f9SXin Li     /*
581*412f47f9SXin Li      * Classification and more semi-numericals
582*412f47f9SXin Li      */
583*412f47f9SXin Li     {"copysign", (funcptr)test_copysign, semi2, {NULL}, cases_semi2},
584*412f47f9SXin Li     {"copysignf", (funcptr)test_copysignf, semi2f, {NULL}, cases_semi2_float},
585*412f47f9SXin Li     {"isfinite", (funcptr)test_isfinite, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
586*412f47f9SXin Li     {"isfinitef", (funcptr)test_isfinitef, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
587*412f47f9SXin Li     {"isinf", (funcptr)test_isinf, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
588*412f47f9SXin Li     {"isinff", (funcptr)test_isinff, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
589*412f47f9SXin Li     {"isnan", (funcptr)test_isnan, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
590*412f47f9SXin Li     {"isnanf", (funcptr)test_isnanf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
591*412f47f9SXin Li     {"isnormal", (funcptr)test_isnormal, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
592*412f47f9SXin Li     {"isnormalf", (funcptr)test_isnormalf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
593*412f47f9SXin Li     {"signbit", (funcptr)test_signbit, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
594*412f47f9SXin Li     {"signbitf", (funcptr)test_signbitf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
595*412f47f9SXin Li     {"fpclassify", (funcptr)test_fpclassify, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
596*412f47f9SXin Li     {"fpclassifyf", (funcptr)test_fpclassifyf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
597*412f47f9SXin Li     /*
598*412f47f9SXin Li      * Comparisons
599*412f47f9SXin Li      */
600*412f47f9SXin Li     {"isgreater", (funcptr)test_isgreater, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
601*412f47f9SXin Li     {"isgreaterequal", (funcptr)test_isgreaterequal, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
602*412f47f9SXin Li     {"isless", (funcptr)test_isless, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
603*412f47f9SXin Li     {"islessequal", (funcptr)test_islessequal, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
604*412f47f9SXin Li     {"islessgreater", (funcptr)test_islessgreater, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
605*412f47f9SXin Li     {"isunordered", (funcptr)test_isunordered, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
606*412f47f9SXin Li 
607*412f47f9SXin Li     {"isgreaterf", (funcptr)test_isgreaterf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
608*412f47f9SXin Li     {"isgreaterequalf", (funcptr)test_isgreaterequalf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
609*412f47f9SXin Li     {"islessf", (funcptr)test_islessf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
610*412f47f9SXin Li     {"islessequalf", (funcptr)test_islessequalf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
611*412f47f9SXin Li     {"islessgreaterf", (funcptr)test_islessgreaterf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
612*412f47f9SXin Li     {"isunorderedf", (funcptr)test_isunorderedf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
613*412f47f9SXin Li 
614*412f47f9SXin Li     /*
615*412f47f9SXin Li      * Inverse Hyperbolic functions
616*412f47f9SXin Li      */
617*412f47f9SXin Li     {"atanh", (funcptr)mpfr_atanh, args1, {NULL}, cases_uniform, 0x3e400000, 0x3fefffff},
618*412f47f9SXin Li     {"asinh", (funcptr)mpfr_asinh, args1, {NULL}, cases_uniform, 0x3e400000, 0x3fefffff},
619*412f47f9SXin Li     {"acosh", (funcptr)mpfr_acosh, args1, {NULL}, cases_uniform_positive, 0x3ff00000, 0x7fefffff},
620*412f47f9SXin Li 
621*412f47f9SXin Li     {"atanhf", (funcptr)mpfr_atanh, args1f, {NULL}, cases_uniform_float, 0x32000000, 0x3f7fffff},
622*412f47f9SXin Li     {"asinhf", (funcptr)mpfr_asinh, args1f, {NULL}, cases_uniform_float, 0x32000000, 0x3f7fffff},
623*412f47f9SXin Li     {"acoshf", (funcptr)mpfr_acosh, args1f, {NULL}, cases_uniform_float_positive, 0x3f800000, 0x7f800000},
624*412f47f9SXin Li 
625*412f47f9SXin Li     /*
626*412f47f9SXin Li      * Everything else (sitting in a section down here at the bottom
627*412f47f9SXin Li      * because historically they were not tested because we didn't
628*412f47f9SXin Li      * have reference implementations for them)
629*412f47f9SXin Li      */
630*412f47f9SXin Li     {"csin", (funcptr)mpc_sin, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
631*412f47f9SXin Li     {"csinf", (funcptr)mpc_sin, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
632*412f47f9SXin Li     {"ccos", (funcptr)mpc_cos, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
633*412f47f9SXin Li     {"ccosf", (funcptr)mpc_cos, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
634*412f47f9SXin Li     {"ctan", (funcptr)mpc_tan, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
635*412f47f9SXin Li     {"ctanf", (funcptr)mpc_tan, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
636*412f47f9SXin Li 
637*412f47f9SXin Li     {"casin", (funcptr)mpc_asin, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
638*412f47f9SXin Li     {"casinf", (funcptr)mpc_asin, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
639*412f47f9SXin Li     {"cacos", (funcptr)mpc_acos, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
640*412f47f9SXin Li     {"cacosf", (funcptr)mpc_acos, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
641*412f47f9SXin Li     {"catan", (funcptr)mpc_atan, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
642*412f47f9SXin Li     {"catanf", (funcptr)mpc_atan, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
643*412f47f9SXin Li 
644*412f47f9SXin Li     {"csinh", (funcptr)mpc_sinh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
645*412f47f9SXin Li     {"csinhf", (funcptr)mpc_sinh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
646*412f47f9SXin Li     {"ccosh", (funcptr)mpc_cosh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
647*412f47f9SXin Li     {"ccoshf", (funcptr)mpc_cosh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
648*412f47f9SXin Li     {"ctanh", (funcptr)mpc_tanh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
649*412f47f9SXin Li     {"ctanhf", (funcptr)mpc_tanh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
650*412f47f9SXin Li 
651*412f47f9SXin Li     {"casinh", (funcptr)mpc_asinh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
652*412f47f9SXin Li     {"casinhf", (funcptr)mpc_asinh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
653*412f47f9SXin Li     {"cacosh", (funcptr)mpc_acosh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
654*412f47f9SXin Li     {"cacoshf", (funcptr)mpc_acosh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
655*412f47f9SXin Li     {"catanh", (funcptr)mpc_atanh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
656*412f47f9SXin Li     {"catanhf", (funcptr)mpc_atanh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
657*412f47f9SXin Li 
658*412f47f9SXin Li     {"cexp", (funcptr)mpc_exp, args1c, {NULL}, complex_cases_uniform, 0x3c900000, 0x40862000},
659*412f47f9SXin Li     {"cpow", (funcptr)test_cpow, args2c, {NULL}, complex_pow_cases, 0x3fc00000, 0x40000000},
660*412f47f9SXin Li     {"clog", (funcptr)mpc_log, args1c, {NULL}, complex_log_cases, 0, 0},
661*412f47f9SXin Li     {"csqrt", (funcptr)mpc_sqrt, args1c, {NULL}, complex_log_cases, 0, 0},
662*412f47f9SXin Li 
663*412f47f9SXin Li     {"cexpf", (funcptr)mpc_exp, args1fc, {NULL}, complex_cases_uniform_float, 0x24800000, 0x42b00000},
664*412f47f9SXin Li     {"cpowf", (funcptr)test_cpow, args2fc, {NULL}, complex_pow_cases_float, 0x3e000000, 0x41000000},
665*412f47f9SXin Li     {"clogf", (funcptr)mpc_log, args1fc, {NULL}, complex_log_cases_float, 0, 0},
666*412f47f9SXin Li     {"csqrtf", (funcptr)mpc_sqrt, args1fc, {NULL}, complex_log_cases_float, 0, 0},
667*412f47f9SXin Li 
668*412f47f9SXin Li     {"cdiv", (funcptr)mpc_div, args2c, {NULL}, complex_arithmetic_cases, 0, 0},
669*412f47f9SXin Li     {"cmul", (funcptr)mpc_mul, args2c, {NULL}, complex_arithmetic_cases, 0, 0},
670*412f47f9SXin Li     {"cadd", (funcptr)mpc_add, args2c, {NULL}, complex_arithmetic_cases, 0, 0},
671*412f47f9SXin Li     {"csub", (funcptr)mpc_sub, args2c, {NULL}, complex_arithmetic_cases, 0, 0},
672*412f47f9SXin Li 
673*412f47f9SXin Li     {"cdivf", (funcptr)mpc_div, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
674*412f47f9SXin Li     {"cmulf", (funcptr)mpc_mul, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
675*412f47f9SXin Li     {"caddf", (funcptr)mpc_add, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
676*412f47f9SXin Li     {"csubf", (funcptr)mpc_sub, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
677*412f47f9SXin Li 
678*412f47f9SXin Li     {"cabsf", (funcptr)mpc_abs, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},
679*412f47f9SXin Li     {"cabs", (funcptr)mpc_abs, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},
680*412f47f9SXin Li     {"cargf", (funcptr)mpc_arg, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},
681*412f47f9SXin Li     {"carg", (funcptr)mpc_arg, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},
682*412f47f9SXin Li     {"cimagf", (funcptr)mpc_imag, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},
683*412f47f9SXin Li     {"cimag", (funcptr)mpc_imag, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},
684*412f47f9SXin Li     {"conjf", (funcptr)mpc_conj, args1fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
685*412f47f9SXin Li     {"conj", (funcptr)mpc_conj, args1c, {NULL}, complex_arithmetic_cases, 0, 0},
686*412f47f9SXin Li     {"cprojf", (funcptr)mpc_proj, args1fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
687*412f47f9SXin Li     {"cproj", (funcptr)mpc_proj, args1c, {NULL}, complex_arithmetic_cases, 0, 0},
688*412f47f9SXin Li     {"crealf", (funcptr)mpc_real, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},
689*412f47f9SXin Li     {"creal", (funcptr)mpc_real, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},
690*412f47f9SXin Li     {"erfcf", (funcptr)mpfr_erfc, args1f, {NULL}, cases_biased_float, 0x1e800000, 0x41000000},
691*412f47f9SXin Li     {"erfc", (funcptr)mpfr_erfc, args1, {NULL}, cases_biased, 0x3bd00000, 0x403c0000},
692*412f47f9SXin Li     {"erff", (funcptr)mpfr_erf, args1f, {NULL}, cases_biased_float, 0x03800000, 0x40700000},
693*412f47f9SXin Li     {"erf", (funcptr)mpfr_erf, args1, {NULL}, cases_biased, 0x00800000, 0x40200000},
694*412f47f9SXin Li     {"exp2f", (funcptr)mpfr_exp2, args1f, {NULL}, cases_uniform_float, 0x33800000, 0x43c00000},
695*412f47f9SXin Li     {"exp2", (funcptr)mpfr_exp2, args1, {NULL}, cases_uniform, 0x3ca00000, 0x40a00000},
696*412f47f9SXin Li     {"expm1f", (funcptr)mpfr_expm1, args1f, {NULL}, cases_uniform_float, 0x33000000, 0x43800000},
697*412f47f9SXin Li     {"expm1", (funcptr)mpfr_expm1, args1, {NULL}, cases_uniform, 0x3c900000, 0x409c0000},
698*412f47f9SXin Li     {"fmaxf", (funcptr)mpfr_max, args2f, {NULL}, minmax_cases_float, 0, 0x7f7fffff},
699*412f47f9SXin Li     {"fmax", (funcptr)mpfr_max, args2, {NULL}, minmax_cases, 0, 0x7fefffff},
700*412f47f9SXin Li     {"fminf", (funcptr)mpfr_min, args2f, {NULL}, minmax_cases_float, 0, 0x7f7fffff},
701*412f47f9SXin Li     {"fmin", (funcptr)mpfr_min, args2, {NULL}, minmax_cases, 0, 0x7fefffff},
702*412f47f9SXin Li     {"lgammaf", (funcptr)test_lgamma, args1f, {NULL}, cases_uniform_float, 0x01800000, 0x7f800000},
703*412f47f9SXin Li     {"lgamma", (funcptr)test_lgamma, args1, {NULL}, cases_uniform, 0x00100000, 0x7ff00000},
704*412f47f9SXin Li     {"log1pf", (funcptr)mpfr_log1p, args1f, {NULL}, log1p_cases_float, 0, 0},
705*412f47f9SXin Li     {"log1p", (funcptr)mpfr_log1p, args1, {NULL}, log1p_cases, 0, 0},
706*412f47f9SXin Li     {"log2f", (funcptr)mpfr_log2, args1f, {NULL}, log_cases_float, 0, 0},
707*412f47f9SXin Li     {"log2", (funcptr)mpfr_log2, args1, {NULL}, log_cases, 0, 0},
708*412f47f9SXin Li     {"tgammaf", (funcptr)mpfr_gamma, args1f, {NULL}, cases_uniform_float, 0x2f800000, 0x43000000},
709*412f47f9SXin Li     {"tgamma", (funcptr)mpfr_gamma, args1, {NULL}, cases_uniform, 0x3c000000, 0x40800000},
710*412f47f9SXin Li };
711*412f47f9SXin Li 
712*412f47f9SXin Li const int nfunctions = ( sizeof(functions)/sizeof(*functions) );
713*412f47f9SXin Li 
714*412f47f9SXin Li #define random_sign ( random_upto(1) ? 0x80000000 : 0 )
715*412f47f9SXin Li 
iszero(uint32 * x)716*412f47f9SXin Li static int iszero(uint32 *x) {
717*412f47f9SXin Li     return !((x[0] & 0x7FFFFFFF) || x[1]);
718*412f47f9SXin Li }
719*412f47f9SXin Li 
720*412f47f9SXin Li 
complex_log_cases(uint32 * out,uint32 param1,uint32 param2)721*412f47f9SXin Li static void complex_log_cases(uint32 *out, uint32 param1,
722*412f47f9SXin Li                               uint32 param2) {
723*412f47f9SXin Li     cases_uniform(out,0x00100000,0x7fefffff);
724*412f47f9SXin Li     cases_uniform(out+2,0x00100000,0x7fefffff);
725*412f47f9SXin Li }
726*412f47f9SXin Li 
727*412f47f9SXin Li 
complex_log_cases_float(uint32 * out,uint32 param1,uint32 param2)728*412f47f9SXin Li static void complex_log_cases_float(uint32 *out, uint32 param1,
729*412f47f9SXin Li                                     uint32 param2) {
730*412f47f9SXin Li     cases_uniform_float(out,0x00800000,0x7f7fffff);
731*412f47f9SXin Li     cases_uniform_float(out+2,0x00800000,0x7f7fffff);
732*412f47f9SXin Li }
733*412f47f9SXin Li 
complex_cases_biased(uint32 * out,uint32 lowbound,uint32 highbound)734*412f47f9SXin Li static void complex_cases_biased(uint32 *out, uint32 lowbound,
735*412f47f9SXin Li                                  uint32 highbound) {
736*412f47f9SXin Li     cases_biased(out,lowbound,highbound);
737*412f47f9SXin Li     cases_biased(out+2,lowbound,highbound);
738*412f47f9SXin Li }
739*412f47f9SXin Li 
complex_cases_biased_float(uint32 * out,uint32 lowbound,uint32 highbound)740*412f47f9SXin Li static void complex_cases_biased_float(uint32 *out, uint32 lowbound,
741*412f47f9SXin Li                                        uint32 highbound) {
742*412f47f9SXin Li     cases_biased_float(out,lowbound,highbound);
743*412f47f9SXin Li     cases_biased_float(out+2,lowbound,highbound);
744*412f47f9SXin Li }
745*412f47f9SXin Li 
complex_cases_uniform(uint32 * out,uint32 lowbound,uint32 highbound)746*412f47f9SXin Li static void complex_cases_uniform(uint32 *out, uint32 lowbound,
747*412f47f9SXin Li                                  uint32 highbound) {
748*412f47f9SXin Li     cases_uniform(out,lowbound,highbound);
749*412f47f9SXin Li     cases_uniform(out+2,lowbound,highbound);
750*412f47f9SXin Li }
751*412f47f9SXin Li 
complex_cases_uniform_float(uint32 * out,uint32 lowbound,uint32 highbound)752*412f47f9SXin Li static void complex_cases_uniform_float(uint32 *out, uint32 lowbound,
753*412f47f9SXin Li                                        uint32 highbound) {
754*412f47f9SXin Li     cases_uniform_float(out,lowbound,highbound);
755*412f47f9SXin Li     cases_uniform(out+2,lowbound,highbound);
756*412f47f9SXin Li }
757*412f47f9SXin Li 
complex_pow_cases(uint32 * out,uint32 lowbound,uint32 highbound)758*412f47f9SXin Li static void complex_pow_cases(uint32 *out, uint32 lowbound,
759*412f47f9SXin Li                               uint32 highbound) {
760*412f47f9SXin Li     /*
761*412f47f9SXin Li      * Generating non-overflowing cases for complex pow:
762*412f47f9SXin Li      *
763*412f47f9SXin Li      * Our base has both parts within the range [1/2,2], and hence
764*412f47f9SXin Li      * its magnitude is within [1/2,2*sqrt(2)]. The magnitude of its
765*412f47f9SXin Li      * logarithm in base 2 is therefore at most the magnitude of
766*412f47f9SXin Li      * (log2(2*sqrt(2)) + i*pi/log(2)), or in other words
767*412f47f9SXin Li      * hypot(3/2,pi/log(2)) = 4.77. So the magnitude of the exponent
768*412f47f9SXin Li      * input must be at most our output magnitude limit (as a power
769*412f47f9SXin Li      * of two) divided by that.
770*412f47f9SXin Li      *
771*412f47f9SXin Li      * I also set the output magnitude limit a bit low, because we
772*412f47f9SXin Li      * don't guarantee (and neither does glibc) to prevent internal
773*412f47f9SXin Li      * overflow in cases where the output _magnitude_ overflows but
774*412f47f9SXin Li      * scaling it back down by cos and sin of the argument brings it
775*412f47f9SXin Li      * back in range.
776*412f47f9SXin Li      */
777*412f47f9SXin Li     cases_uniform(out,0x3fe00000, 0x40000000);
778*412f47f9SXin Li     cases_uniform(out+2,0x3fe00000, 0x40000000);
779*412f47f9SXin Li     cases_uniform(out+4,0x3f800000, 0x40600000);
780*412f47f9SXin Li     cases_uniform(out+6,0x3f800000, 0x40600000);
781*412f47f9SXin Li }
782*412f47f9SXin Li 
complex_pow_cases_float(uint32 * out,uint32 lowbound,uint32 highbound)783*412f47f9SXin Li static void complex_pow_cases_float(uint32 *out, uint32 lowbound,
784*412f47f9SXin Li                                     uint32 highbound) {
785*412f47f9SXin Li     /*
786*412f47f9SXin Li      * Reasoning as above, though of course the detailed numbers are
787*412f47f9SXin Li      * all different.
788*412f47f9SXin Li      */
789*412f47f9SXin Li     cases_uniform_float(out,0x3f000000, 0x40000000);
790*412f47f9SXin Li     cases_uniform_float(out+2,0x3f000000, 0x40000000);
791*412f47f9SXin Li     cases_uniform_float(out+4,0x3d600000, 0x41900000);
792*412f47f9SXin Li     cases_uniform_float(out+6,0x3d600000, 0x41900000);
793*412f47f9SXin Li }
794*412f47f9SXin Li 
complex_arithmetic_cases(uint32 * out,uint32 lowbound,uint32 highbound)795*412f47f9SXin Li static void complex_arithmetic_cases(uint32 *out, uint32 lowbound,
796*412f47f9SXin Li                                      uint32 highbound) {
797*412f47f9SXin Li     cases_uniform(out,0,0x7fefffff);
798*412f47f9SXin Li     cases_uniform(out+2,0,0x7fefffff);
799*412f47f9SXin Li     cases_uniform(out+4,0,0x7fefffff);
800*412f47f9SXin Li     cases_uniform(out+6,0,0x7fefffff);
801*412f47f9SXin Li }
802*412f47f9SXin Li 
complex_arithmetic_cases_float(uint32 * out,uint32 lowbound,uint32 highbound)803*412f47f9SXin Li static void complex_arithmetic_cases_float(uint32 *out, uint32 lowbound,
804*412f47f9SXin Li                                            uint32 highbound) {
805*412f47f9SXin Li     cases_uniform_float(out,0,0x7f7fffff);
806*412f47f9SXin Li     cases_uniform_float(out+2,0,0x7f7fffff);
807*412f47f9SXin Li     cases_uniform_float(out+4,0,0x7f7fffff);
808*412f47f9SXin Li     cases_uniform_float(out+6,0,0x7f7fffff);
809*412f47f9SXin Li }
810*412f47f9SXin Li 
811*412f47f9SXin Li /*
812*412f47f9SXin Li  * Included from fplib test suite, in a compact self-contained
813*412f47f9SXin Li  * form.
814*412f47f9SXin Li  */
815*412f47f9SXin Li 
float32_case(uint32 * ret)816*412f47f9SXin Li void float32_case(uint32 *ret) {
817*412f47f9SXin Li     int n, bits;
818*412f47f9SXin Li     uint32 f;
819*412f47f9SXin Li     static int premax, preptr;
820*412f47f9SXin Li     static uint32 *specifics = NULL;
821*412f47f9SXin Li 
822*412f47f9SXin Li     if (!ret) {
823*412f47f9SXin Li         if (specifics)
824*412f47f9SXin Li             free(specifics);
825*412f47f9SXin Li         specifics = NULL;
826*412f47f9SXin Li         premax = preptr = 0;
827*412f47f9SXin Li         return;
828*412f47f9SXin Li     }
829*412f47f9SXin Li 
830*412f47f9SXin Li     if (!specifics) {
831*412f47f9SXin Li         int exps[] = {
832*412f47f9SXin Li             -127, -126, -125, -24, -4, -3, -2, -1, 0, 1, 2, 3, 4,
833*412f47f9SXin Li                 24, 29, 30, 31, 32, 61, 62, 63, 64, 126, 127, 128
834*412f47f9SXin Li         };
835*412f47f9SXin Li         int sign, eptr;
836*412f47f9SXin Li         uint32 se, j;
837*412f47f9SXin Li         /*
838*412f47f9SXin Li          * We want a cross product of:
839*412f47f9SXin Li          *  - each of two sign bits (2)
840*412f47f9SXin Li          *  - each of the above (unbiased) exponents (25)
841*412f47f9SXin Li          *  - the following list of fraction parts:
842*412f47f9SXin Li          *    * zero (1)
843*412f47f9SXin Li          *    * all bits (1)
844*412f47f9SXin Li          *    * one-bit-set (23)
845*412f47f9SXin Li          *    * one-bit-clear (23)
846*412f47f9SXin Li          *    * one-bit-and-above (20: 3 are duplicates)
847*412f47f9SXin Li          *    * one-bit-and-below (20: 3 are duplicates)
848*412f47f9SXin Li          *    (total 88)
849*412f47f9SXin Li          *  (total 4400)
850*412f47f9SXin Li          */
851*412f47f9SXin Li         specifics = malloc(4400 * sizeof(*specifics));
852*412f47f9SXin Li         preptr = 0;
853*412f47f9SXin Li         for (sign = 0; sign <= 1; sign++) {
854*412f47f9SXin Li             for (eptr = 0; eptr < sizeof(exps)/sizeof(*exps); eptr++) {
855*412f47f9SXin Li                 se = (sign ? 0x80000000 : 0) | ((exps[eptr]+127) << 23);
856*412f47f9SXin Li                 /*
857*412f47f9SXin Li                  * Zero.
858*412f47f9SXin Li                  */
859*412f47f9SXin Li                 specifics[preptr++] = se | 0;
860*412f47f9SXin Li                 /*
861*412f47f9SXin Li                  * All bits.
862*412f47f9SXin Li                  */
863*412f47f9SXin Li                 specifics[preptr++] = se | 0x7FFFFF;
864*412f47f9SXin Li                 /*
865*412f47f9SXin Li                  * One-bit-set.
866*412f47f9SXin Li                  */
867*412f47f9SXin Li                 for (j = 1; j && j <= 0x400000; j <<= 1)
868*412f47f9SXin Li                     specifics[preptr++] = se | j;
869*412f47f9SXin Li                 /*
870*412f47f9SXin Li                  * One-bit-clear.
871*412f47f9SXin Li                  */
872*412f47f9SXin Li                 for (j = 1; j && j <= 0x400000; j <<= 1)
873*412f47f9SXin Li                     specifics[preptr++] = se | (0x7FFFFF ^ j);
874*412f47f9SXin Li                 /*
875*412f47f9SXin Li                  * One-bit-and-everything-below.
876*412f47f9SXin Li                  */
877*412f47f9SXin Li                 for (j = 2; j && j <= 0x100000; j <<= 1)
878*412f47f9SXin Li                     specifics[preptr++] = se | (2*j-1);
879*412f47f9SXin Li                 /*
880*412f47f9SXin Li                  * One-bit-and-everything-above.
881*412f47f9SXin Li                  */
882*412f47f9SXin Li                 for (j = 4; j && j <= 0x200000; j <<= 1)
883*412f47f9SXin Li                     specifics[preptr++] = se | (0x7FFFFF ^ (j-1));
884*412f47f9SXin Li                 /*
885*412f47f9SXin Li                  * Done.
886*412f47f9SXin Li                  */
887*412f47f9SXin Li             }
888*412f47f9SXin Li         }
889*412f47f9SXin Li         assert(preptr == 4400);
890*412f47f9SXin Li         premax = preptr;
891*412f47f9SXin Li     }
892*412f47f9SXin Li 
893*412f47f9SXin Li     /*
894*412f47f9SXin Li      * Decide whether to return a pre or a random case.
895*412f47f9SXin Li      */
896*412f47f9SXin Li     n = random32() % (premax+1);
897*412f47f9SXin Li     if (n < preptr) {
898*412f47f9SXin Li         /*
899*412f47f9SXin Li          * Return pre[n].
900*412f47f9SXin Li          */
901*412f47f9SXin Li         uint32 t;
902*412f47f9SXin Li         t = specifics[n];
903*412f47f9SXin Li         specifics[n] = specifics[preptr-1];
904*412f47f9SXin Li         specifics[preptr-1] = t;        /* (not really needed) */
905*412f47f9SXin Li         preptr--;
906*412f47f9SXin Li         *ret = t;
907*412f47f9SXin Li     } else {
908*412f47f9SXin Li         /*
909*412f47f9SXin Li          * Random case.
910*412f47f9SXin Li          * Sign and exponent:
911*412f47f9SXin Li          *  - FIXME
912*412f47f9SXin Li          * Significand:
913*412f47f9SXin Li          *  - with prob 1/5, a totally random bit pattern
914*412f47f9SXin Li          *  - with prob 1/5, all 1s down to some point and then random
915*412f47f9SXin Li          *  - with prob 1/5, all 1s up to some point and then random
916*412f47f9SXin Li          *  - with prob 1/5, all 0s down to some point and then random
917*412f47f9SXin Li          *  - with prob 1/5, all 0s up to some point and then random
918*412f47f9SXin Li          */
919*412f47f9SXin Li         n = random32() % 5;
920*412f47f9SXin Li         f = random32();                /* some random bits */
921*412f47f9SXin Li         bits = random32() % 22 + 1;    /* 1-22 */
922*412f47f9SXin Li         switch (n) {
923*412f47f9SXin Li           case 0:
924*412f47f9SXin Li             break;                     /* leave f alone */
925*412f47f9SXin Li           case 1:
926*412f47f9SXin Li             f |= (1<<bits)-1;
927*412f47f9SXin Li             break;
928*412f47f9SXin Li           case 2:
929*412f47f9SXin Li             f &= ~((1<<bits)-1);
930*412f47f9SXin Li             break;
931*412f47f9SXin Li           case 3:
932*412f47f9SXin Li             f |= ~((1<<bits)-1);
933*412f47f9SXin Li             break;
934*412f47f9SXin Li           case 4:
935*412f47f9SXin Li             f &= (1<<bits)-1;
936*412f47f9SXin Li             break;
937*412f47f9SXin Li         }
938*412f47f9SXin Li         f &= 0x7FFFFF;
939*412f47f9SXin Li         f |= (random32() & 0xFF800000);/* FIXME - do better */
940*412f47f9SXin Li         *ret = f;
941*412f47f9SXin Li     }
942*412f47f9SXin Li }
float64_case(uint32 * ret)943*412f47f9SXin Li static void float64_case(uint32 *ret) {
944*412f47f9SXin Li     int n, bits;
945*412f47f9SXin Li     uint32 f, g;
946*412f47f9SXin Li     static int premax, preptr;
947*412f47f9SXin Li     static uint32 (*specifics)[2] = NULL;
948*412f47f9SXin Li 
949*412f47f9SXin Li     if (!ret) {
950*412f47f9SXin Li         if (specifics)
951*412f47f9SXin Li             free(specifics);
952*412f47f9SXin Li         specifics = NULL;
953*412f47f9SXin Li         premax = preptr = 0;
954*412f47f9SXin Li         return;
955*412f47f9SXin Li     }
956*412f47f9SXin Li 
957*412f47f9SXin Li     if (!specifics) {
958*412f47f9SXin Li         int exps[] = {
959*412f47f9SXin Li             -1023, -1022, -1021, -129, -128, -127, -126, -53, -4, -3, -2,
960*412f47f9SXin Li             -1, 0, 1, 2, 3, 4, 29, 30, 31, 32, 53, 61, 62, 63, 64, 127,
961*412f47f9SXin Li             128, 129, 1022, 1023, 1024
962*412f47f9SXin Li         };
963*412f47f9SXin Li         int sign, eptr;
964*412f47f9SXin Li         uint32 se, j;
965*412f47f9SXin Li         /*
966*412f47f9SXin Li          * We want a cross product of:
967*412f47f9SXin Li          *  - each of two sign bits (2)
968*412f47f9SXin Li          *  - each of the above (unbiased) exponents (32)
969*412f47f9SXin Li          *  - the following list of fraction parts:
970*412f47f9SXin Li          *    * zero (1)
971*412f47f9SXin Li          *    * all bits (1)
972*412f47f9SXin Li          *    * one-bit-set (52)
973*412f47f9SXin Li          *    * one-bit-clear (52)
974*412f47f9SXin Li          *    * one-bit-and-above (49: 3 are duplicates)
975*412f47f9SXin Li          *    * one-bit-and-below (49: 3 are duplicates)
976*412f47f9SXin Li          *    (total 204)
977*412f47f9SXin Li          *  (total 13056)
978*412f47f9SXin Li          */
979*412f47f9SXin Li         specifics = malloc(13056 * sizeof(*specifics));
980*412f47f9SXin Li         preptr = 0;
981*412f47f9SXin Li         for (sign = 0; sign <= 1; sign++) {
982*412f47f9SXin Li             for (eptr = 0; eptr < sizeof(exps)/sizeof(*exps); eptr++) {
983*412f47f9SXin Li                 se = (sign ? 0x80000000 : 0) | ((exps[eptr]+1023) << 20);
984*412f47f9SXin Li                 /*
985*412f47f9SXin Li                  * Zero.
986*412f47f9SXin Li                  */
987*412f47f9SXin Li                 specifics[preptr][0] = 0;
988*412f47f9SXin Li                 specifics[preptr][1] = 0;
989*412f47f9SXin Li                 specifics[preptr++][0] |= se;
990*412f47f9SXin Li                 /*
991*412f47f9SXin Li                  * All bits.
992*412f47f9SXin Li                  */
993*412f47f9SXin Li                 specifics[preptr][0] = 0xFFFFF;
994*412f47f9SXin Li                 specifics[preptr][1] = ~0;
995*412f47f9SXin Li                 specifics[preptr++][0] |= se;
996*412f47f9SXin Li                 /*
997*412f47f9SXin Li                  * One-bit-set.
998*412f47f9SXin Li                  */
999*412f47f9SXin Li                 for (j = 1; j && j <= 0x80000000; j <<= 1) {
1000*412f47f9SXin Li                     specifics[preptr][0] = 0;
1001*412f47f9SXin Li                     specifics[preptr][1] = j;
1002*412f47f9SXin Li                     specifics[preptr++][0] |= se;
1003*412f47f9SXin Li                     if (j & 0xFFFFF) {
1004*412f47f9SXin Li                         specifics[preptr][0] = j;
1005*412f47f9SXin Li                         specifics[preptr][1] = 0;
1006*412f47f9SXin Li                         specifics[preptr++][0] |= se;
1007*412f47f9SXin Li                     }
1008*412f47f9SXin Li                 }
1009*412f47f9SXin Li                 /*
1010*412f47f9SXin Li                  * One-bit-clear.
1011*412f47f9SXin Li                  */
1012*412f47f9SXin Li                 for (j = 1; j && j <= 0x80000000; j <<= 1) {
1013*412f47f9SXin Li                     specifics[preptr][0] = 0xFFFFF;
1014*412f47f9SXin Li                     specifics[preptr][1] = ~j;
1015*412f47f9SXin Li                     specifics[preptr++][0] |= se;
1016*412f47f9SXin Li                     if (j & 0xFFFFF) {
1017*412f47f9SXin Li                         specifics[preptr][0] = 0xFFFFF ^ j;
1018*412f47f9SXin Li                         specifics[preptr][1] = ~0;
1019*412f47f9SXin Li                         specifics[preptr++][0] |= se;
1020*412f47f9SXin Li                     }
1021*412f47f9SXin Li                 }
1022*412f47f9SXin Li                 /*
1023*412f47f9SXin Li                  * One-bit-and-everything-below.
1024*412f47f9SXin Li                  */
1025*412f47f9SXin Li                 for (j = 2; j && j <= 0x80000000; j <<= 1) {
1026*412f47f9SXin Li                     specifics[preptr][0] = 0;
1027*412f47f9SXin Li                     specifics[preptr][1] = 2*j-1;
1028*412f47f9SXin Li                     specifics[preptr++][0] |= se;
1029*412f47f9SXin Li                 }
1030*412f47f9SXin Li                 for (j = 1; j && j <= 0x20000; j <<= 1) {
1031*412f47f9SXin Li                     specifics[preptr][0] = 2*j-1;
1032*412f47f9SXin Li                     specifics[preptr][1] = ~0;
1033*412f47f9SXin Li                     specifics[preptr++][0] |= se;
1034*412f47f9SXin Li                 }
1035*412f47f9SXin Li                 /*
1036*412f47f9SXin Li                  * One-bit-and-everything-above.
1037*412f47f9SXin Li                  */
1038*412f47f9SXin Li                 for (j = 4; j && j <= 0x80000000; j <<= 1) {
1039*412f47f9SXin Li                     specifics[preptr][0] = 0xFFFFF;
1040*412f47f9SXin Li                     specifics[preptr][1] = ~(j-1);
1041*412f47f9SXin Li                     specifics[preptr++][0] |= se;
1042*412f47f9SXin Li                 }
1043*412f47f9SXin Li                 for (j = 1; j && j <= 0x40000; j <<= 1) {
1044*412f47f9SXin Li                     specifics[preptr][0] = 0xFFFFF ^ (j-1);
1045*412f47f9SXin Li                     specifics[preptr][1] = 0;
1046*412f47f9SXin Li                     specifics[preptr++][0] |= se;
1047*412f47f9SXin Li                 }
1048*412f47f9SXin Li                 /*
1049*412f47f9SXin Li                  * Done.
1050*412f47f9SXin Li                  */
1051*412f47f9SXin Li             }
1052*412f47f9SXin Li         }
1053*412f47f9SXin Li         assert(preptr == 13056);
1054*412f47f9SXin Li         premax = preptr;
1055*412f47f9SXin Li     }
1056*412f47f9SXin Li 
1057*412f47f9SXin Li     /*
1058*412f47f9SXin Li      * Decide whether to return a pre or a random case.
1059*412f47f9SXin Li      */
1060*412f47f9SXin Li     n = (uint32) random32() % (uint32) (premax+1);
1061*412f47f9SXin Li     if (n < preptr) {
1062*412f47f9SXin Li         /*
1063*412f47f9SXin Li          * Return pre[n].
1064*412f47f9SXin Li          */
1065*412f47f9SXin Li         uint32 t;
1066*412f47f9SXin Li         t = specifics[n][0];
1067*412f47f9SXin Li         specifics[n][0] = specifics[preptr-1][0];
1068*412f47f9SXin Li         specifics[preptr-1][0] = t;     /* (not really needed) */
1069*412f47f9SXin Li         ret[0] = t;
1070*412f47f9SXin Li         t = specifics[n][1];
1071*412f47f9SXin Li         specifics[n][1] = specifics[preptr-1][1];
1072*412f47f9SXin Li         specifics[preptr-1][1] = t;     /* (not really needed) */
1073*412f47f9SXin Li         ret[1] = t;
1074*412f47f9SXin Li         preptr--;
1075*412f47f9SXin Li     } else {
1076*412f47f9SXin Li         /*
1077*412f47f9SXin Li          * Random case.
1078*412f47f9SXin Li          * Sign and exponent:
1079*412f47f9SXin Li          *  - FIXME
1080*412f47f9SXin Li          * Significand:
1081*412f47f9SXin Li          *  - with prob 1/5, a totally random bit pattern
1082*412f47f9SXin Li          *  - with prob 1/5, all 1s down to some point and then random
1083*412f47f9SXin Li          *  - with prob 1/5, all 1s up to some point and then random
1084*412f47f9SXin Li          *  - with prob 1/5, all 0s down to some point and then random
1085*412f47f9SXin Li          *  - with prob 1/5, all 0s up to some point and then random
1086*412f47f9SXin Li          */
1087*412f47f9SXin Li         n = random32() % 5;
1088*412f47f9SXin Li         f = random32();                /* some random bits */
1089*412f47f9SXin Li         g = random32();                /* some random bits */
1090*412f47f9SXin Li         bits = random32() % 51 + 1;    /* 1-51 */
1091*412f47f9SXin Li         switch (n) {
1092*412f47f9SXin Li           case 0:
1093*412f47f9SXin Li             break;                     /* leave f alone */
1094*412f47f9SXin Li           case 1:
1095*412f47f9SXin Li             if (bits <= 32)
1096*412f47f9SXin Li                 f |= (1<<bits)-1;
1097*412f47f9SXin Li             else {
1098*412f47f9SXin Li                 bits -= 32;
1099*412f47f9SXin Li                 g |= (1<<bits)-1;
1100*412f47f9SXin Li                 f = ~0;
1101*412f47f9SXin Li             }
1102*412f47f9SXin Li             break;
1103*412f47f9SXin Li           case 2:
1104*412f47f9SXin Li             if (bits <= 32)
1105*412f47f9SXin Li                 f &= ~((1<<bits)-1);
1106*412f47f9SXin Li             else {
1107*412f47f9SXin Li                 bits -= 32;
1108*412f47f9SXin Li                 g &= ~((1<<bits)-1);
1109*412f47f9SXin Li                 f = 0;
1110*412f47f9SXin Li             }
1111*412f47f9SXin Li             break;
1112*412f47f9SXin Li           case 3:
1113*412f47f9SXin Li             if (bits <= 32)
1114*412f47f9SXin Li                 g &= (1<<bits)-1;
1115*412f47f9SXin Li             else {
1116*412f47f9SXin Li                 bits -= 32;
1117*412f47f9SXin Li                 f &= (1<<bits)-1;
1118*412f47f9SXin Li                 g = 0;
1119*412f47f9SXin Li             }
1120*412f47f9SXin Li             break;
1121*412f47f9SXin Li           case 4:
1122*412f47f9SXin Li             if (bits <= 32)
1123*412f47f9SXin Li                 g |= ~((1<<bits)-1);
1124*412f47f9SXin Li             else {
1125*412f47f9SXin Li                 bits -= 32;
1126*412f47f9SXin Li                 f |= ~((1<<bits)-1);
1127*412f47f9SXin Li                 g = ~0;
1128*412f47f9SXin Li             }
1129*412f47f9SXin Li             break;
1130*412f47f9SXin Li         }
1131*412f47f9SXin Li         g &= 0xFFFFF;
1132*412f47f9SXin Li         g |= (random32() & 0xFFF00000);/* FIXME - do better */
1133*412f47f9SXin Li         ret[0] = g;
1134*412f47f9SXin Li         ret[1] = f;
1135*412f47f9SXin Li     }
1136*412f47f9SXin Li }
1137*412f47f9SXin Li 
cases_biased(uint32 * out,uint32 lowbound,uint32 highbound)1138*412f47f9SXin Li static void cases_biased(uint32 *out, uint32 lowbound,
1139*412f47f9SXin Li                           uint32 highbound) {
1140*412f47f9SXin Li     do {
1141*412f47f9SXin Li         out[0] = highbound - random_upto_biased(highbound-lowbound, 8);
1142*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1143*412f47f9SXin Li         out[0] |= random_sign;
1144*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1145*412f47f9SXin Li }
1146*412f47f9SXin Li 
cases_biased_positive(uint32 * out,uint32 lowbound,uint32 highbound)1147*412f47f9SXin Li static void cases_biased_positive(uint32 *out, uint32 lowbound,
1148*412f47f9SXin Li                                   uint32 highbound) {
1149*412f47f9SXin Li     do {
1150*412f47f9SXin Li         out[0] = highbound - random_upto_biased(highbound-lowbound, 8);
1151*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1152*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1153*412f47f9SXin Li }
1154*412f47f9SXin Li 
cases_biased_float(uint32 * out,uint32 lowbound,uint32 highbound)1155*412f47f9SXin Li static void cases_biased_float(uint32 *out, uint32 lowbound,
1156*412f47f9SXin Li                                uint32 highbound) {
1157*412f47f9SXin Li     do {
1158*412f47f9SXin Li         out[0] = highbound - random_upto_biased(highbound-lowbound, 8);
1159*412f47f9SXin Li         out[1] = 0;
1160*412f47f9SXin Li         out[0] |= random_sign;
1161*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1162*412f47f9SXin Li }
1163*412f47f9SXin Li 
cases_semi1(uint32 * out,uint32 param1,uint32 param2)1164*412f47f9SXin Li static void cases_semi1(uint32 *out, uint32 param1,
1165*412f47f9SXin Li                         uint32 param2) {
1166*412f47f9SXin Li     float64_case(out);
1167*412f47f9SXin Li }
1168*412f47f9SXin Li 
cases_semi1_float(uint32 * out,uint32 param1,uint32 param2)1169*412f47f9SXin Li static void cases_semi1_float(uint32 *out, uint32 param1,
1170*412f47f9SXin Li                               uint32 param2) {
1171*412f47f9SXin Li     float32_case(out);
1172*412f47f9SXin Li }
1173*412f47f9SXin Li 
cases_semi2(uint32 * out,uint32 param1,uint32 param2)1174*412f47f9SXin Li static void cases_semi2(uint32 *out, uint32 param1,
1175*412f47f9SXin Li                         uint32 param2) {
1176*412f47f9SXin Li     float64_case(out);
1177*412f47f9SXin Li     float64_case(out+2);
1178*412f47f9SXin Li }
1179*412f47f9SXin Li 
cases_semi2_float(uint32 * out,uint32 param1,uint32 param2)1180*412f47f9SXin Li static void cases_semi2_float(uint32 *out, uint32 param1,
1181*412f47f9SXin Li                         uint32 param2) {
1182*412f47f9SXin Li     float32_case(out);
1183*412f47f9SXin Li     float32_case(out+2);
1184*412f47f9SXin Li }
1185*412f47f9SXin Li 
cases_ldexp(uint32 * out,uint32 param1,uint32 param2)1186*412f47f9SXin Li static void cases_ldexp(uint32 *out, uint32 param1,
1187*412f47f9SXin Li                         uint32 param2) {
1188*412f47f9SXin Li     float64_case(out);
1189*412f47f9SXin Li     out[2] = random_upto(2048)-1024;
1190*412f47f9SXin Li }
1191*412f47f9SXin Li 
cases_ldexp_float(uint32 * out,uint32 param1,uint32 param2)1192*412f47f9SXin Li static void cases_ldexp_float(uint32 *out, uint32 param1,
1193*412f47f9SXin Li                               uint32 param2) {
1194*412f47f9SXin Li     float32_case(out);
1195*412f47f9SXin Li     out[2] = random_upto(256)-128;
1196*412f47f9SXin Li }
1197*412f47f9SXin Li 
cases_uniform(uint32 * out,uint32 lowbound,uint32 highbound)1198*412f47f9SXin Li static void cases_uniform(uint32 *out, uint32 lowbound,
1199*412f47f9SXin Li                           uint32 highbound) {
1200*412f47f9SXin Li     do {
1201*412f47f9SXin Li         out[0] = highbound - random_upto(highbound-lowbound);
1202*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1203*412f47f9SXin Li         out[0] |= random_sign;
1204*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1205*412f47f9SXin Li }
cases_uniform_float(uint32 * out,uint32 lowbound,uint32 highbound)1206*412f47f9SXin Li static void cases_uniform_float(uint32 *out, uint32 lowbound,
1207*412f47f9SXin Li                                 uint32 highbound) {
1208*412f47f9SXin Li     do {
1209*412f47f9SXin Li         out[0] = highbound - random_upto(highbound-lowbound);
1210*412f47f9SXin Li         out[1] = 0;
1211*412f47f9SXin Li         out[0] |= random_sign;
1212*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1213*412f47f9SXin Li }
1214*412f47f9SXin Li 
cases_uniform_positive(uint32 * out,uint32 lowbound,uint32 highbound)1215*412f47f9SXin Li static void cases_uniform_positive(uint32 *out, uint32 lowbound,
1216*412f47f9SXin Li                                    uint32 highbound) {
1217*412f47f9SXin Li     do {
1218*412f47f9SXin Li         out[0] = highbound - random_upto(highbound-lowbound);
1219*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1220*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1221*412f47f9SXin Li }
cases_uniform_float_positive(uint32 * out,uint32 lowbound,uint32 highbound)1222*412f47f9SXin Li static void cases_uniform_float_positive(uint32 *out, uint32 lowbound,
1223*412f47f9SXin Li                                          uint32 highbound) {
1224*412f47f9SXin Li     do {
1225*412f47f9SXin Li         out[0] = highbound - random_upto(highbound-lowbound);
1226*412f47f9SXin Li         out[1] = 0;
1227*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1228*412f47f9SXin Li }
1229*412f47f9SXin Li 
1230*412f47f9SXin Li 
log_cases(uint32 * out,uint32 param1,uint32 param2)1231*412f47f9SXin Li static void log_cases(uint32 *out, uint32 param1,
1232*412f47f9SXin Li                       uint32 param2) {
1233*412f47f9SXin Li     do {
1234*412f47f9SXin Li         out[0] = random_upto(0x7FEFFFFF);
1235*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1236*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1237*412f47f9SXin Li }
1238*412f47f9SXin Li 
log_cases_float(uint32 * out,uint32 param1,uint32 param2)1239*412f47f9SXin Li static void log_cases_float(uint32 *out, uint32 param1,
1240*412f47f9SXin Li                             uint32 param2) {
1241*412f47f9SXin Li     do {
1242*412f47f9SXin Li         out[0] = random_upto(0x7F7FFFFF);
1243*412f47f9SXin Li         out[1] = 0;
1244*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1245*412f47f9SXin Li }
1246*412f47f9SXin Li 
log1p_cases(uint32 * out,uint32 param1,uint32 param2)1247*412f47f9SXin Li static void log1p_cases(uint32 *out, uint32 param1, uint32 param2)
1248*412f47f9SXin Li {
1249*412f47f9SXin Li     uint32 sign = random_sign;
1250*412f47f9SXin Li     if (sign == 0) {
1251*412f47f9SXin Li         cases_uniform_positive(out, 0x3c700000, 0x43400000);
1252*412f47f9SXin Li     } else {
1253*412f47f9SXin Li         cases_uniform_positive(out, 0x3c000000, 0x3ff00000);
1254*412f47f9SXin Li     }
1255*412f47f9SXin Li     out[0] |= sign;
1256*412f47f9SXin Li }
1257*412f47f9SXin Li 
log1p_cases_float(uint32 * out,uint32 param1,uint32 param2)1258*412f47f9SXin Li static void log1p_cases_float(uint32 *out, uint32 param1, uint32 param2)
1259*412f47f9SXin Li {
1260*412f47f9SXin Li     uint32 sign = random_sign;
1261*412f47f9SXin Li     if (sign == 0) {
1262*412f47f9SXin Li         cases_uniform_float_positive(out, 0x32000000, 0x4c000000);
1263*412f47f9SXin Li     } else {
1264*412f47f9SXin Li         cases_uniform_float_positive(out, 0x30000000, 0x3f800000);
1265*412f47f9SXin Li     }
1266*412f47f9SXin Li     out[0] |= sign;
1267*412f47f9SXin Li }
1268*412f47f9SXin Li 
minmax_cases(uint32 * out,uint32 param1,uint32 param2)1269*412f47f9SXin Li static void minmax_cases(uint32 *out, uint32 param1, uint32 param2)
1270*412f47f9SXin Li {
1271*412f47f9SXin Li     do {
1272*412f47f9SXin Li         out[0] = random_upto(0x7FEFFFFF);
1273*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1274*412f47f9SXin Li         out[0] |= random_sign;
1275*412f47f9SXin Li         out[2] = random_upto(0x7FEFFFFF);
1276*412f47f9SXin Li         out[3] = random_upto(0xFFFFFFFF);
1277*412f47f9SXin Li         out[2] |= random_sign;
1278*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1279*412f47f9SXin Li }
1280*412f47f9SXin Li 
minmax_cases_float(uint32 * out,uint32 param1,uint32 param2)1281*412f47f9SXin Li static void minmax_cases_float(uint32 *out, uint32 param1, uint32 param2)
1282*412f47f9SXin Li {
1283*412f47f9SXin Li     do {
1284*412f47f9SXin Li         out[0] = random_upto(0x7F7FFFFF);
1285*412f47f9SXin Li         out[1] = 0;
1286*412f47f9SXin Li         out[0] |= random_sign;
1287*412f47f9SXin Li         out[2] = random_upto(0x7F7FFFFF);
1288*412f47f9SXin Li         out[3] = 0;
1289*412f47f9SXin Li         out[2] |= random_sign;
1290*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1291*412f47f9SXin Li }
1292*412f47f9SXin Li 
rred_cases(uint32 * out,uint32 param1,uint32 param2)1293*412f47f9SXin Li static void rred_cases(uint32 *out, uint32 param1,
1294*412f47f9SXin Li                        uint32 param2) {
1295*412f47f9SXin Li     do {
1296*412f47f9SXin Li         out[0] = ((0x3fc00000 + random_upto(0x036fffff)) |
1297*412f47f9SXin Li                   (random_upto(1) << 31));
1298*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1299*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1300*412f47f9SXin Li }
1301*412f47f9SXin Li 
rred_cases_float(uint32 * out,uint32 param1,uint32 param2)1302*412f47f9SXin Li static void rred_cases_float(uint32 *out, uint32 param1,
1303*412f47f9SXin Li                              uint32 param2) {
1304*412f47f9SXin Li     do {
1305*412f47f9SXin Li         out[0] = ((0x3e000000 + random_upto(0x0cffffff)) |
1306*412f47f9SXin Li                   (random_upto(1) << 31));
1307*412f47f9SXin Li         out[1] = 0;                    /* for iszero */
1308*412f47f9SXin Li     } while (iszero(out));             /* rule out zero */
1309*412f47f9SXin Li }
1310*412f47f9SXin Li 
atan2_cases(uint32 * out,uint32 param1,uint32 param2)1311*412f47f9SXin Li static void atan2_cases(uint32 *out, uint32 param1,
1312*412f47f9SXin Li                         uint32 param2) {
1313*412f47f9SXin Li     do {
1314*412f47f9SXin Li         int expdiff = random_upto(101)-51;
1315*412f47f9SXin Li         int swap;
1316*412f47f9SXin Li         if (expdiff < 0) {
1317*412f47f9SXin Li             expdiff = -expdiff;
1318*412f47f9SXin Li             swap = 2;
1319*412f47f9SXin Li         } else
1320*412f47f9SXin Li             swap = 0;
1321*412f47f9SXin Li         out[swap ^ 0] = random_upto(0x7FEFFFFF-((expdiff+1)<<20));
1322*412f47f9SXin Li         out[swap ^ 2] = random_upto(((expdiff+1)<<20)-1) + out[swap ^ 0];
1323*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1324*412f47f9SXin Li         out[3] = random_upto(0xFFFFFFFF);
1325*412f47f9SXin Li         out[0] |= random_sign;
1326*412f47f9SXin Li         out[2] |= random_sign;
1327*412f47f9SXin Li     } while (iszero(out) || iszero(out+2));/* rule out zero */
1328*412f47f9SXin Li }
1329*412f47f9SXin Li 
atan2_cases_float(uint32 * out,uint32 param1,uint32 param2)1330*412f47f9SXin Li static void atan2_cases_float(uint32 *out, uint32 param1,
1331*412f47f9SXin Li                               uint32 param2) {
1332*412f47f9SXin Li     do {
1333*412f47f9SXin Li         int expdiff = random_upto(44)-22;
1334*412f47f9SXin Li         int swap;
1335*412f47f9SXin Li         if (expdiff < 0) {
1336*412f47f9SXin Li             expdiff = -expdiff;
1337*412f47f9SXin Li             swap = 2;
1338*412f47f9SXin Li         } else
1339*412f47f9SXin Li             swap = 0;
1340*412f47f9SXin Li         out[swap ^ 0] = random_upto(0x7F7FFFFF-((expdiff+1)<<23));
1341*412f47f9SXin Li         out[swap ^ 2] = random_upto(((expdiff+1)<<23)-1) + out[swap ^ 0];
1342*412f47f9SXin Li         out[0] |= random_sign;
1343*412f47f9SXin Li         out[2] |= random_sign;
1344*412f47f9SXin Li         out[1] = out[3] = 0;           /* for iszero */
1345*412f47f9SXin Li     } while (iszero(out) || iszero(out+2));/* rule out zero */
1346*412f47f9SXin Li }
1347*412f47f9SXin Li 
pow_cases(uint32 * out,uint32 param1,uint32 param2)1348*412f47f9SXin Li static void pow_cases(uint32 *out, uint32 param1,
1349*412f47f9SXin Li                       uint32 param2) {
1350*412f47f9SXin Li     /*
1351*412f47f9SXin Li      * Pick an exponent e (-0x33 to +0x7FE) for x, and here's the
1352*412f47f9SXin Li      * range of numbers we can use as y:
1353*412f47f9SXin Li      *
1354*412f47f9SXin Li      * For e < 0x3FE, the range is [-0x400/(0x3FE-e),+0x432/(0x3FE-e)]
1355*412f47f9SXin Li      * For e > 0x3FF, the range is [-0x432/(e-0x3FF),+0x400/(e-0x3FF)]
1356*412f47f9SXin Li      *
1357*412f47f9SXin Li      * For e == 0x3FE or e == 0x3FF, the range gets infinite at one
1358*412f47f9SXin Li      * end or the other, so we have to be cleverer: pick a number n
1359*412f47f9SXin Li      * of useful bits in the mantissa (1 thru 52, so 1 must imply
1360*412f47f9SXin Li      * 0x3ff00000.00000001 whereas 52 is anything at least as big
1361*412f47f9SXin Li      * as 0x3ff80000.00000000; for e == 0x3fe, 1 necessarily means
1362*412f47f9SXin Li      * 0x3fefffff.ffffffff and 52 is anything at most as big as
1363*412f47f9SXin Li      * 0x3fe80000.00000000). Then, as it happens, a sensible
1364*412f47f9SXin Li      * maximum power is 2^(63-n) for e == 0x3fe, and 2^(62-n) for
1365*412f47f9SXin Li      * e == 0x3ff.
1366*412f47f9SXin Li      *
1367*412f47f9SXin Li      * We inevitably get some overflows in approximating the log
1368*412f47f9SXin Li      * curves by these nasty step functions, but that's all right -
1369*412f47f9SXin Li      * we do want _some_ overflows to be tested.
1370*412f47f9SXin Li      *
1371*412f47f9SXin Li      * Having got that, then, it's just a matter of inventing a
1372*412f47f9SXin Li      * probability distribution for all of this.
1373*412f47f9SXin Li      */
1374*412f47f9SXin Li     int e, n;
1375*412f47f9SXin Li     uint32 dmin, dmax;
1376*412f47f9SXin Li     const uint32 pmin = 0x3e100000;
1377*412f47f9SXin Li 
1378*412f47f9SXin Li     /*
1379*412f47f9SXin Li      * Generate exponents in a slightly biased fashion.
1380*412f47f9SXin Li      */
1381*412f47f9SXin Li     e = (random_upto(1) ?              /* is exponent small or big? */
1382*412f47f9SXin Li          0x3FE - random_upto_biased(0x431,2) :   /* small */
1383*412f47f9SXin Li          0x3FF + random_upto_biased(0x3FF,2));   /* big */
1384*412f47f9SXin Li 
1385*412f47f9SXin Li     /*
1386*412f47f9SXin Li      * Now split into cases.
1387*412f47f9SXin Li      */
1388*412f47f9SXin Li     if (e < 0x3FE || e > 0x3FF) {
1389*412f47f9SXin Li         uint32 imin, imax;
1390*412f47f9SXin Li         if (e < 0x3FE)
1391*412f47f9SXin Li             imin = 0x40000 / (0x3FE - e), imax = 0x43200 / (0x3FE - e);
1392*412f47f9SXin Li         else
1393*412f47f9SXin Li             imin = 0x43200 / (e - 0x3FF), imax = 0x40000 / (e - 0x3FF);
1394*412f47f9SXin Li         /* Power range runs from -imin to imax. Now convert to doubles */
1395*412f47f9SXin Li         dmin = doubletop(imin, -8);
1396*412f47f9SXin Li         dmax = doubletop(imax, -8);
1397*412f47f9SXin Li         /* Compute the number of mantissa bits. */
1398*412f47f9SXin Li         n = (e > 0 ? 53 : 52+e);
1399*412f47f9SXin Li     } else {
1400*412f47f9SXin Li         /* Critical exponents. Generate a top bit index. */
1401*412f47f9SXin Li         n = 52 - random_upto_biased(51, 4);
1402*412f47f9SXin Li         if (e == 0x3FE)
1403*412f47f9SXin Li             dmax = 63 - n;
1404*412f47f9SXin Li         else
1405*412f47f9SXin Li             dmax = 62 - n;
1406*412f47f9SXin Li         dmax = (dmax << 20) + 0x3FF00000;
1407*412f47f9SXin Li         dmin = dmax;
1408*412f47f9SXin Li     }
1409*412f47f9SXin Li     /* Generate a mantissa. */
1410*412f47f9SXin Li     if (n <= 32) {
1411*412f47f9SXin Li         out[0] = 0;
1412*412f47f9SXin Li         out[1] = random_upto((1 << (n-1)) - 1) + (1 << (n-1));
1413*412f47f9SXin Li     } else if (n == 33) {
1414*412f47f9SXin Li         out[0] = 1;
1415*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1416*412f47f9SXin Li     } else if (n > 33) {
1417*412f47f9SXin Li         out[0] = random_upto((1 << (n-33)) - 1) + (1 << (n-33));
1418*412f47f9SXin Li         out[1] = random_upto(0xFFFFFFFF);
1419*412f47f9SXin Li     }
1420*412f47f9SXin Li     /* Negate the mantissa if e == 0x3FE. */
1421*412f47f9SXin Li     if (e == 0x3FE) {
1422*412f47f9SXin Li         out[1] = -out[1];
1423*412f47f9SXin Li         out[0] = -out[0];
1424*412f47f9SXin Li         if (out[1]) out[0]--;
1425*412f47f9SXin Li     }
1426*412f47f9SXin Li     /* Put the exponent on. */
1427*412f47f9SXin Li     out[0] &= 0xFFFFF;
1428*412f47f9SXin Li     out[0] |= ((e > 0 ? e : 0) << 20);
1429*412f47f9SXin Li     /* Generate a power. Powers don't go below 2^-30. */
1430*412f47f9SXin Li     if (random_upto(1)) {
1431*412f47f9SXin Li         /* Positive power */
1432*412f47f9SXin Li         out[2] = dmax - random_upto_biased(dmax-pmin, 10);
1433*412f47f9SXin Li     } else {
1434*412f47f9SXin Li         /* Negative power */
1435*412f47f9SXin Li         out[2] = (dmin - random_upto_biased(dmin-pmin, 10)) | 0x80000000;
1436*412f47f9SXin Li     }
1437*412f47f9SXin Li     out[3] = random_upto(0xFFFFFFFF);
1438*412f47f9SXin Li }
pow_cases_float(uint32 * out,uint32 param1,uint32 param2)1439*412f47f9SXin Li static void pow_cases_float(uint32 *out, uint32 param1,
1440*412f47f9SXin Li                             uint32 param2) {
1441*412f47f9SXin Li     /*
1442*412f47f9SXin Li      * Pick an exponent e (-0x16 to +0xFE) for x, and here's the
1443*412f47f9SXin Li      * range of numbers we can use as y:
1444*412f47f9SXin Li      *
1445*412f47f9SXin Li      * For e < 0x7E, the range is [-0x80/(0x7E-e),+0x95/(0x7E-e)]
1446*412f47f9SXin Li      * For e > 0x7F, the range is [-0x95/(e-0x7F),+0x80/(e-0x7F)]
1447*412f47f9SXin Li      *
1448*412f47f9SXin Li      * For e == 0x7E or e == 0x7F, the range gets infinite at one
1449*412f47f9SXin Li      * end or the other, so we have to be cleverer: pick a number n
1450*412f47f9SXin Li      * of useful bits in the mantissa (1 thru 23, so 1 must imply
1451*412f47f9SXin Li      * 0x3f800001 whereas 23 is anything at least as big as
1452*412f47f9SXin Li      * 0x3fc00000; for e == 0x7e, 1 necessarily means 0x3f7fffff
1453*412f47f9SXin Li      * and 23 is anything at most as big as 0x3f400000). Then, as
1454*412f47f9SXin Li      * it happens, a sensible maximum power is 2^(31-n) for e ==
1455*412f47f9SXin Li      * 0x7e, and 2^(30-n) for e == 0x7f.
1456*412f47f9SXin Li      *
1457*412f47f9SXin Li      * We inevitably get some overflows in approximating the log
1458*412f47f9SXin Li      * curves by these nasty step functions, but that's all right -
1459*412f47f9SXin Li      * we do want _some_ overflows to be tested.
1460*412f47f9SXin Li      *
1461*412f47f9SXin Li      * Having got that, then, it's just a matter of inventing a
1462*412f47f9SXin Li      * probability distribution for all of this.
1463*412f47f9SXin Li      */
1464*412f47f9SXin Li     int e, n;
1465*412f47f9SXin Li     uint32 dmin, dmax;
1466*412f47f9SXin Li     const uint32 pmin = 0x38000000;
1467*412f47f9SXin Li 
1468*412f47f9SXin Li     /*
1469*412f47f9SXin Li      * Generate exponents in a slightly biased fashion.
1470*412f47f9SXin Li      */
1471*412f47f9SXin Li     e = (random_upto(1) ?              /* is exponent small or big? */
1472*412f47f9SXin Li          0x7E - random_upto_biased(0x94,2) :   /* small */
1473*412f47f9SXin Li          0x7F + random_upto_biased(0x7f,2));   /* big */
1474*412f47f9SXin Li 
1475*412f47f9SXin Li     /*
1476*412f47f9SXin Li      * Now split into cases.
1477*412f47f9SXin Li      */
1478*412f47f9SXin Li     if (e < 0x7E || e > 0x7F) {
1479*412f47f9SXin Li         uint32 imin, imax;
1480*412f47f9SXin Li         if (e < 0x7E)
1481*412f47f9SXin Li             imin = 0x8000 / (0x7e - e), imax = 0x9500 / (0x7e - e);
1482*412f47f9SXin Li         else
1483*412f47f9SXin Li             imin = 0x9500 / (e - 0x7f), imax = 0x8000 / (e - 0x7f);
1484*412f47f9SXin Li         /* Power range runs from -imin to imax. Now convert to doubles */
1485*412f47f9SXin Li         dmin = floatval(imin, -8);
1486*412f47f9SXin Li         dmax = floatval(imax, -8);
1487*412f47f9SXin Li         /* Compute the number of mantissa bits. */
1488*412f47f9SXin Li         n = (e > 0 ? 24 : 23+e);
1489*412f47f9SXin Li     } else {
1490*412f47f9SXin Li         /* Critical exponents. Generate a top bit index. */
1491*412f47f9SXin Li         n = 23 - random_upto_biased(22, 4);
1492*412f47f9SXin Li         if (e == 0x7E)
1493*412f47f9SXin Li             dmax = 31 - n;
1494*412f47f9SXin Li         else
1495*412f47f9SXin Li             dmax = 30 - n;
1496*412f47f9SXin Li         dmax = (dmax << 23) + 0x3F800000;
1497*412f47f9SXin Li         dmin = dmax;
1498*412f47f9SXin Li     }
1499*412f47f9SXin Li     /* Generate a mantissa. */
1500*412f47f9SXin Li     out[0] = random_upto((1 << (n-1)) - 1) + (1 << (n-1));
1501*412f47f9SXin Li     out[1] = 0;
1502*412f47f9SXin Li     /* Negate the mantissa if e == 0x7E. */
1503*412f47f9SXin Li     if (e == 0x7E) {
1504*412f47f9SXin Li         out[0] = -out[0];
1505*412f47f9SXin Li     }
1506*412f47f9SXin Li     /* Put the exponent on. */
1507*412f47f9SXin Li     out[0] &= 0x7FFFFF;
1508*412f47f9SXin Li     out[0] |= ((e > 0 ? e : 0) << 23);
1509*412f47f9SXin Li     /* Generate a power. Powers don't go below 2^-15. */
1510*412f47f9SXin Li     if (random_upto(1)) {
1511*412f47f9SXin Li         /* Positive power */
1512*412f47f9SXin Li         out[2] = dmax - random_upto_biased(dmax-pmin, 10);
1513*412f47f9SXin Li     } else {
1514*412f47f9SXin Li         /* Negative power */
1515*412f47f9SXin Li         out[2] = (dmin - random_upto_biased(dmin-pmin, 10)) | 0x80000000;
1516*412f47f9SXin Li     }
1517*412f47f9SXin Li     out[3] = 0;
1518*412f47f9SXin Li }
1519*412f47f9SXin Li 
vet_for_decline(Testable * fn,uint32 * args,uint32 * result,int got_errno_in)1520*412f47f9SXin Li void vet_for_decline(Testable *fn, uint32 *args, uint32 *result, int got_errno_in) {
1521*412f47f9SXin Li     int declined = 0;
1522*412f47f9SXin Li 
1523*412f47f9SXin Li     switch (fn->type) {
1524*412f47f9SXin Li       case args1:
1525*412f47f9SXin Li       case rred:
1526*412f47f9SXin Li       case semi1:
1527*412f47f9SXin Li       case t_frexp:
1528*412f47f9SXin Li       case t_modf:
1529*412f47f9SXin Li       case classify:
1530*412f47f9SXin Li       case t_ldexp:
1531*412f47f9SXin Li         declined |= lib_fo && is_dhard(args+0);
1532*412f47f9SXin Li         break;
1533*412f47f9SXin Li       case args1f:
1534*412f47f9SXin Li       case rredf:
1535*412f47f9SXin Li       case semi1f:
1536*412f47f9SXin Li       case t_frexpf:
1537*412f47f9SXin Li       case t_modff:
1538*412f47f9SXin Li       case classifyf:
1539*412f47f9SXin Li         declined |= lib_fo && is_shard(args+0);
1540*412f47f9SXin Li         break;
1541*412f47f9SXin Li       case args2:
1542*412f47f9SXin Li       case semi2:
1543*412f47f9SXin Li       case args1c:
1544*412f47f9SXin Li       case args1cr:
1545*412f47f9SXin Li       case compare:
1546*412f47f9SXin Li         declined |= lib_fo && is_dhard(args+0);
1547*412f47f9SXin Li         declined |= lib_fo && is_dhard(args+2);
1548*412f47f9SXin Li         break;
1549*412f47f9SXin Li       case args2f:
1550*412f47f9SXin Li       case semi2f:
1551*412f47f9SXin Li       case t_ldexpf:
1552*412f47f9SXin Li       case comparef:
1553*412f47f9SXin Li       case args1fc:
1554*412f47f9SXin Li       case args1fcr:
1555*412f47f9SXin Li         declined |= lib_fo && is_shard(args+0);
1556*412f47f9SXin Li         declined |= lib_fo && is_shard(args+2);
1557*412f47f9SXin Li         break;
1558*412f47f9SXin Li       case args2c:
1559*412f47f9SXin Li         declined |= lib_fo && is_dhard(args+0);
1560*412f47f9SXin Li         declined |= lib_fo && is_dhard(args+2);
1561*412f47f9SXin Li         declined |= lib_fo && is_dhard(args+4);
1562*412f47f9SXin Li         declined |= lib_fo && is_dhard(args+6);
1563*412f47f9SXin Li         break;
1564*412f47f9SXin Li       case args2fc:
1565*412f47f9SXin Li         declined |= lib_fo && is_shard(args+0);
1566*412f47f9SXin Li         declined |= lib_fo && is_shard(args+2);
1567*412f47f9SXin Li         declined |= lib_fo && is_shard(args+4);
1568*412f47f9SXin Li         declined |= lib_fo && is_shard(args+6);
1569*412f47f9SXin Li         break;
1570*412f47f9SXin Li     }
1571*412f47f9SXin Li 
1572*412f47f9SXin Li     switch (fn->type) {
1573*412f47f9SXin Li       case args1:              /* return an extra-precise result */
1574*412f47f9SXin Li       case args2:
1575*412f47f9SXin Li       case rred:
1576*412f47f9SXin Li       case semi1:              /* return a double result */
1577*412f47f9SXin Li       case semi2:
1578*412f47f9SXin Li       case t_ldexp:
1579*412f47f9SXin Li       case t_frexp:            /* return double * int */
1580*412f47f9SXin Li       case args1cr:
1581*412f47f9SXin Li         declined |= lib_fo && is_dhard(result);
1582*412f47f9SXin Li         break;
1583*412f47f9SXin Li       case args1f:
1584*412f47f9SXin Li       case args2f:
1585*412f47f9SXin Li       case rredf:
1586*412f47f9SXin Li       case semi1f:
1587*412f47f9SXin Li       case semi2f:
1588*412f47f9SXin Li       case t_ldexpf:
1589*412f47f9SXin Li       case args1fcr:
1590*412f47f9SXin Li         declined |= lib_fo && is_shard(result);
1591*412f47f9SXin Li         break;
1592*412f47f9SXin Li       case t_modf:             /* return double * double */
1593*412f47f9SXin Li         declined |= lib_fo && is_dhard(result+0);
1594*412f47f9SXin Li         declined |= lib_fo && is_dhard(result+2);
1595*412f47f9SXin Li         break;
1596*412f47f9SXin Li       case t_modff:                    /* return float * float */
1597*412f47f9SXin Li         declined |= lib_fo && is_shard(result+2);
1598*412f47f9SXin Li         /* fall through */
1599*412f47f9SXin Li       case t_frexpf:                   /* return float * int */
1600*412f47f9SXin Li         declined |= lib_fo && is_shard(result+0);
1601*412f47f9SXin Li         break;
1602*412f47f9SXin Li       case args1c:
1603*412f47f9SXin Li       case args2c:
1604*412f47f9SXin Li         declined |= lib_fo && is_dhard(result+0);
1605*412f47f9SXin Li         declined |= lib_fo && is_dhard(result+4);
1606*412f47f9SXin Li         break;
1607*412f47f9SXin Li       case args1fc:
1608*412f47f9SXin Li       case args2fc:
1609*412f47f9SXin Li         declined |= lib_fo && is_shard(result+0);
1610*412f47f9SXin Li         declined |= lib_fo && is_shard(result+4);
1611*412f47f9SXin Li         break;
1612*412f47f9SXin Li     }
1613*412f47f9SXin Li 
1614*412f47f9SXin Li     /* Expect basic arithmetic tests to be declined if the command
1615*412f47f9SXin Li      * line said that would happen */
1616*412f47f9SXin Li     declined |= (lib_no_arith && (fn->func == (funcptr)mpc_add ||
1617*412f47f9SXin Li                                   fn->func == (funcptr)mpc_sub ||
1618*412f47f9SXin Li                                   fn->func == (funcptr)mpc_mul ||
1619*412f47f9SXin Li                                   fn->func == (funcptr)mpc_div));
1620*412f47f9SXin Li 
1621*412f47f9SXin Li     if (!declined) {
1622*412f47f9SXin Li         if (got_errno_in)
1623*412f47f9SXin Li             ntests++;
1624*412f47f9SXin Li         else
1625*412f47f9SXin Li             ntests += 3;
1626*412f47f9SXin Li     }
1627*412f47f9SXin Li }
1628*412f47f9SXin Li 
docase(Testable * fn,uint32 * args)1629*412f47f9SXin Li void docase(Testable *fn, uint32 *args) {
1630*412f47f9SXin Li     uint32 result[8];  /* real part in first 4, imaginary part in last 4 */
1631*412f47f9SXin Li     char *errstr = NULL;
1632*412f47f9SXin Li     mpfr_t a, b, r;
1633*412f47f9SXin Li     mpc_t ac, bc, rc;
1634*412f47f9SXin Li     int rejected, printextra;
1635*412f47f9SXin Li     wrapperctx ctx;
1636*412f47f9SXin Li 
1637*412f47f9SXin Li     mpfr_init2(a, MPFR_PREC);
1638*412f47f9SXin Li     mpfr_init2(b, MPFR_PREC);
1639*412f47f9SXin Li     mpfr_init2(r, MPFR_PREC);
1640*412f47f9SXin Li     mpc_init2(ac, MPFR_PREC);
1641*412f47f9SXin Li     mpc_init2(bc, MPFR_PREC);
1642*412f47f9SXin Li     mpc_init2(rc, MPFR_PREC);
1643*412f47f9SXin Li 
1644*412f47f9SXin Li     printf("func=%s", fn->name);
1645*412f47f9SXin Li 
1646*412f47f9SXin Li     rejected = 0; /* FIXME */
1647*412f47f9SXin Li 
1648*412f47f9SXin Li     switch (fn->type) {
1649*412f47f9SXin Li       case args1:
1650*412f47f9SXin Li       case rred:
1651*412f47f9SXin Li       case semi1:
1652*412f47f9SXin Li       case t_frexp:
1653*412f47f9SXin Li       case t_modf:
1654*412f47f9SXin Li       case classify:
1655*412f47f9SXin Li         printf(" op1=%08x.%08x", args[0], args[1]);
1656*412f47f9SXin Li         break;
1657*412f47f9SXin Li       case args1f:
1658*412f47f9SXin Li       case rredf:
1659*412f47f9SXin Li       case semi1f:
1660*412f47f9SXin Li       case t_frexpf:
1661*412f47f9SXin Li       case t_modff:
1662*412f47f9SXin Li       case classifyf:
1663*412f47f9SXin Li         printf(" op1=%08x", args[0]);
1664*412f47f9SXin Li         break;
1665*412f47f9SXin Li       case args2:
1666*412f47f9SXin Li       case semi2:
1667*412f47f9SXin Li       case compare:
1668*412f47f9SXin Li         printf(" op1=%08x.%08x", args[0], args[1]);
1669*412f47f9SXin Li         printf(" op2=%08x.%08x", args[2], args[3]);
1670*412f47f9SXin Li         break;
1671*412f47f9SXin Li       case args2f:
1672*412f47f9SXin Li       case semi2f:
1673*412f47f9SXin Li       case t_ldexpf:
1674*412f47f9SXin Li       case comparef:
1675*412f47f9SXin Li         printf(" op1=%08x", args[0]);
1676*412f47f9SXin Li         printf(" op2=%08x", args[2]);
1677*412f47f9SXin Li         break;
1678*412f47f9SXin Li       case t_ldexp:
1679*412f47f9SXin Li         printf(" op1=%08x.%08x", args[0], args[1]);
1680*412f47f9SXin Li         printf(" op2=%08x", args[2]);
1681*412f47f9SXin Li         break;
1682*412f47f9SXin Li       case args1c:
1683*412f47f9SXin Li       case args1cr:
1684*412f47f9SXin Li         printf(" op1r=%08x.%08x", args[0], args[1]);
1685*412f47f9SXin Li         printf(" op1i=%08x.%08x", args[2], args[3]);
1686*412f47f9SXin Li         break;
1687*412f47f9SXin Li       case args2c:
1688*412f47f9SXin Li         printf(" op1r=%08x.%08x", args[0], args[1]);
1689*412f47f9SXin Li         printf(" op1i=%08x.%08x", args[2], args[3]);
1690*412f47f9SXin Li         printf(" op2r=%08x.%08x", args[4], args[5]);
1691*412f47f9SXin Li         printf(" op2i=%08x.%08x", args[6], args[7]);
1692*412f47f9SXin Li         break;
1693*412f47f9SXin Li       case args1fc:
1694*412f47f9SXin Li       case args1fcr:
1695*412f47f9SXin Li         printf(" op1r=%08x", args[0]);
1696*412f47f9SXin Li         printf(" op1i=%08x", args[2]);
1697*412f47f9SXin Li         break;
1698*412f47f9SXin Li       case args2fc:
1699*412f47f9SXin Li         printf(" op1r=%08x", args[0]);
1700*412f47f9SXin Li         printf(" op1i=%08x", args[2]);
1701*412f47f9SXin Li         printf(" op2r=%08x", args[4]);
1702*412f47f9SXin Li         printf(" op2i=%08x", args[6]);
1703*412f47f9SXin Li         break;
1704*412f47f9SXin Li       default:
1705*412f47f9SXin Li         fprintf(stderr, "internal inconsistency?!\n");
1706*412f47f9SXin Li         abort();
1707*412f47f9SXin Li     }
1708*412f47f9SXin Li 
1709*412f47f9SXin Li     if (rejected == 2) {
1710*412f47f9SXin Li         printf(" - test case rejected\n");
1711*412f47f9SXin Li         goto cleanup;
1712*412f47f9SXin Li     }
1713*412f47f9SXin Li 
1714*412f47f9SXin Li     wrapper_init(&ctx);
1715*412f47f9SXin Li 
1716*412f47f9SXin Li     if (rejected == 0) {
1717*412f47f9SXin Li         switch (fn->type) {
1718*412f47f9SXin Li           case args1:
1719*412f47f9SXin Li             set_mpfr_d(a, args[0], args[1]);
1720*412f47f9SXin Li             wrapper_op_real(&ctx, a, 2, args);
1721*412f47f9SXin Li             ((testfunc1)(fn->func))(r, a, GMP_RNDN);
1722*412f47f9SXin Li             get_mpfr_d(r, &result[0], &result[1], &result[2]);
1723*412f47f9SXin Li             wrapper_result_real(&ctx, r, 2, result);
1724*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1725*412f47f9SXin Li                 get_mpfr_d(r, &result[0], &result[1], &result[2]);
1726*412f47f9SXin Li             break;
1727*412f47f9SXin Li           case args1cr:
1728*412f47f9SXin Li             set_mpc_d(ac, args[0], args[1], args[2], args[3]);
1729*412f47f9SXin Li             wrapper_op_complex(&ctx, ac, 2, args);
1730*412f47f9SXin Li             ((testfunc1cr)(fn->func))(r, ac, GMP_RNDN);
1731*412f47f9SXin Li             get_mpfr_d(r, &result[0], &result[1], &result[2]);
1732*412f47f9SXin Li             wrapper_result_real(&ctx, r, 2, result);
1733*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1734*412f47f9SXin Li                 get_mpfr_d(r, &result[0], &result[1], &result[2]);
1735*412f47f9SXin Li             break;
1736*412f47f9SXin Li           case args1f:
1737*412f47f9SXin Li             set_mpfr_f(a, args[0]);
1738*412f47f9SXin Li             wrapper_op_real(&ctx, a, 1, args);
1739*412f47f9SXin Li             ((testfunc1)(fn->func))(r, a, GMP_RNDN);
1740*412f47f9SXin Li             get_mpfr_f(r, &result[0], &result[1]);
1741*412f47f9SXin Li             wrapper_result_real(&ctx, r, 1, result);
1742*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1743*412f47f9SXin Li                 get_mpfr_f(r, &result[0], &result[1]);
1744*412f47f9SXin Li             break;
1745*412f47f9SXin Li           case args1fcr:
1746*412f47f9SXin Li             set_mpc_f(ac, args[0], args[2]);
1747*412f47f9SXin Li             wrapper_op_complex(&ctx, ac, 1, args);
1748*412f47f9SXin Li             ((testfunc1cr)(fn->func))(r, ac, GMP_RNDN);
1749*412f47f9SXin Li             get_mpfr_f(r, &result[0], &result[1]);
1750*412f47f9SXin Li             wrapper_result_real(&ctx, r, 1, result);
1751*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1752*412f47f9SXin Li                 get_mpfr_f(r, &result[0], &result[1]);
1753*412f47f9SXin Li             break;
1754*412f47f9SXin Li           case args2:
1755*412f47f9SXin Li             set_mpfr_d(a, args[0], args[1]);
1756*412f47f9SXin Li             wrapper_op_real(&ctx, a, 2, args);
1757*412f47f9SXin Li             set_mpfr_d(b, args[2], args[3]);
1758*412f47f9SXin Li             wrapper_op_real(&ctx, b, 2, args+2);
1759*412f47f9SXin Li             ((testfunc2)(fn->func))(r, a, b, GMP_RNDN);
1760*412f47f9SXin Li             get_mpfr_d(r, &result[0], &result[1], &result[2]);
1761*412f47f9SXin Li             wrapper_result_real(&ctx, r, 2, result);
1762*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1763*412f47f9SXin Li                 get_mpfr_d(r, &result[0], &result[1], &result[2]);
1764*412f47f9SXin Li             break;
1765*412f47f9SXin Li           case args2f:
1766*412f47f9SXin Li             set_mpfr_f(a, args[0]);
1767*412f47f9SXin Li             wrapper_op_real(&ctx, a, 1, args);
1768*412f47f9SXin Li             set_mpfr_f(b, args[2]);
1769*412f47f9SXin Li             wrapper_op_real(&ctx, b, 1, args+2);
1770*412f47f9SXin Li             ((testfunc2)(fn->func))(r, a, b, GMP_RNDN);
1771*412f47f9SXin Li             get_mpfr_f(r, &result[0], &result[1]);
1772*412f47f9SXin Li             wrapper_result_real(&ctx, r, 1, result);
1773*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1774*412f47f9SXin Li                 get_mpfr_f(r, &result[0], &result[1]);
1775*412f47f9SXin Li             break;
1776*412f47f9SXin Li           case rred:
1777*412f47f9SXin Li             set_mpfr_d(a, args[0], args[1]);
1778*412f47f9SXin Li             wrapper_op_real(&ctx, a, 2, args);
1779*412f47f9SXin Li             ((testrred)(fn->func))(r, a, (int *)&result[3]);
1780*412f47f9SXin Li             get_mpfr_d(r, &result[0], &result[1], &result[2]);
1781*412f47f9SXin Li             wrapper_result_real(&ctx, r, 2, result);
1782*412f47f9SXin Li             /* We never need to mess about with the integer auxiliary
1783*412f47f9SXin Li              * output. */
1784*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1785*412f47f9SXin Li                 get_mpfr_d(r, &result[0], &result[1], &result[2]);
1786*412f47f9SXin Li             break;
1787*412f47f9SXin Li           case rredf:
1788*412f47f9SXin Li             set_mpfr_f(a, args[0]);
1789*412f47f9SXin Li             wrapper_op_real(&ctx, a, 1, args);
1790*412f47f9SXin Li             ((testrred)(fn->func))(r, a, (int *)&result[3]);
1791*412f47f9SXin Li             get_mpfr_f(r, &result[0], &result[1]);
1792*412f47f9SXin Li             wrapper_result_real(&ctx, r, 1, result);
1793*412f47f9SXin Li             /* We never need to mess about with the integer auxiliary
1794*412f47f9SXin Li              * output. */
1795*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1796*412f47f9SXin Li                 get_mpfr_f(r, &result[0], &result[1]);
1797*412f47f9SXin Li             break;
1798*412f47f9SXin Li           case semi1:
1799*412f47f9SXin Li           case semi1f:
1800*412f47f9SXin Li             errstr = ((testsemi1)(fn->func))(args, result);
1801*412f47f9SXin Li             break;
1802*412f47f9SXin Li           case semi2:
1803*412f47f9SXin Li           case compare:
1804*412f47f9SXin Li             errstr = ((testsemi2)(fn->func))(args, args+2, result);
1805*412f47f9SXin Li             break;
1806*412f47f9SXin Li           case semi2f:
1807*412f47f9SXin Li           case comparef:
1808*412f47f9SXin Li           case t_ldexpf:
1809*412f47f9SXin Li             errstr = ((testsemi2f)(fn->func))(args, args+2, result);
1810*412f47f9SXin Li             break;
1811*412f47f9SXin Li           case t_ldexp:
1812*412f47f9SXin Li             errstr = ((testldexp)(fn->func))(args, args+2, result);
1813*412f47f9SXin Li             break;
1814*412f47f9SXin Li           case t_frexp:
1815*412f47f9SXin Li             errstr = ((testfrexp)(fn->func))(args, result, result+2);
1816*412f47f9SXin Li             break;
1817*412f47f9SXin Li           case t_frexpf:
1818*412f47f9SXin Li             errstr = ((testfrexp)(fn->func))(args, result, result+2);
1819*412f47f9SXin Li             break;
1820*412f47f9SXin Li           case t_modf:
1821*412f47f9SXin Li             errstr = ((testmodf)(fn->func))(args, result, result+2);
1822*412f47f9SXin Li             break;
1823*412f47f9SXin Li           case t_modff:
1824*412f47f9SXin Li             errstr = ((testmodf)(fn->func))(args, result, result+2);
1825*412f47f9SXin Li             break;
1826*412f47f9SXin Li           case classify:
1827*412f47f9SXin Li             errstr = ((testclassify)(fn->func))(args, &result[0]);
1828*412f47f9SXin Li             break;
1829*412f47f9SXin Li           case classifyf:
1830*412f47f9SXin Li             errstr = ((testclassifyf)(fn->func))(args, &result[0]);
1831*412f47f9SXin Li             break;
1832*412f47f9SXin Li           case args1c:
1833*412f47f9SXin Li             set_mpc_d(ac, args[0], args[1], args[2], args[3]);
1834*412f47f9SXin Li             wrapper_op_complex(&ctx, ac, 2, args);
1835*412f47f9SXin Li             ((testfunc1c)(fn->func))(rc, ac, MPC_RNDNN);
1836*412f47f9SXin Li             get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);
1837*412f47f9SXin Li             wrapper_result_complex(&ctx, rc, 2, result);
1838*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1839*412f47f9SXin Li                 get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);
1840*412f47f9SXin Li             break;
1841*412f47f9SXin Li           case args2c:
1842*412f47f9SXin Li             set_mpc_d(ac, args[0], args[1], args[2], args[3]);
1843*412f47f9SXin Li             wrapper_op_complex(&ctx, ac, 2, args);
1844*412f47f9SXin Li             set_mpc_d(bc, args[4], args[5], args[6], args[7]);
1845*412f47f9SXin Li             wrapper_op_complex(&ctx, bc, 2, args+4);
1846*412f47f9SXin Li             ((testfunc2c)(fn->func))(rc, ac, bc, MPC_RNDNN);
1847*412f47f9SXin Li             get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);
1848*412f47f9SXin Li             wrapper_result_complex(&ctx, rc, 2, result);
1849*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1850*412f47f9SXin Li                 get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);
1851*412f47f9SXin Li             break;
1852*412f47f9SXin Li           case args1fc:
1853*412f47f9SXin Li             set_mpc_f(ac, args[0], args[2]);
1854*412f47f9SXin Li             wrapper_op_complex(&ctx, ac, 1, args);
1855*412f47f9SXin Li             ((testfunc1c)(fn->func))(rc, ac, MPC_RNDNN);
1856*412f47f9SXin Li             get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);
1857*412f47f9SXin Li             wrapper_result_complex(&ctx, rc, 1, result);
1858*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1859*412f47f9SXin Li                 get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);
1860*412f47f9SXin Li             break;
1861*412f47f9SXin Li           case args2fc:
1862*412f47f9SXin Li             set_mpc_f(ac, args[0], args[2]);
1863*412f47f9SXin Li             wrapper_op_complex(&ctx, ac, 1, args);
1864*412f47f9SXin Li             set_mpc_f(bc, args[4], args[6]);
1865*412f47f9SXin Li             wrapper_op_complex(&ctx, bc, 1, args+4);
1866*412f47f9SXin Li             ((testfunc2c)(fn->func))(rc, ac, bc, MPC_RNDNN);
1867*412f47f9SXin Li             get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);
1868*412f47f9SXin Li             wrapper_result_complex(&ctx, rc, 1, result);
1869*412f47f9SXin Li             if (wrapper_run(&ctx, fn->wrappers))
1870*412f47f9SXin Li                 get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);
1871*412f47f9SXin Li             break;
1872*412f47f9SXin Li           default:
1873*412f47f9SXin Li             fprintf(stderr, "internal inconsistency?!\n");
1874*412f47f9SXin Li             abort();
1875*412f47f9SXin Li         }
1876*412f47f9SXin Li     }
1877*412f47f9SXin Li 
1878*412f47f9SXin Li     switch (fn->type) {
1879*412f47f9SXin Li       case args1:              /* return an extra-precise result */
1880*412f47f9SXin Li       case args2:
1881*412f47f9SXin Li       case args1cr:
1882*412f47f9SXin Li       case rred:
1883*412f47f9SXin Li         printextra = 1;
1884*412f47f9SXin Li         if (rejected == 0) {
1885*412f47f9SXin Li             errstr = NULL;
1886*412f47f9SXin Li             if (!mpfr_zero_p(a)) {
1887*412f47f9SXin Li                 if ((result[0] & 0x7FFFFFFF) == 0 && result[1] == 0) {
1888*412f47f9SXin Li                     /*
1889*412f47f9SXin Li                      * If the output is +0 or -0 apart from the extra
1890*412f47f9SXin Li                      * precision in result[2], then there's a tricky
1891*412f47f9SXin Li                      * judgment call about what we require in the
1892*412f47f9SXin Li                      * output. If we output the extra bits and set
1893*412f47f9SXin Li                      * errstr="?underflow" then mathtest will tolerate
1894*412f47f9SXin Li                      * the function under test rounding down to zero
1895*412f47f9SXin Li                      * _or_ up to the minimum denormal; whereas if we
1896*412f47f9SXin Li                      * suppress the extra bits and set
1897*412f47f9SXin Li                      * errstr="underflow", then mathtest will enforce
1898*412f47f9SXin Li                      * that the function really does underflow to zero.
1899*412f47f9SXin Li                      *
1900*412f47f9SXin Li                      * But where to draw the line? It seems clear to
1901*412f47f9SXin Li                      * me that numbers along the lines of
1902*412f47f9SXin Li                      * 00000000.00000000.7ff should be treated
1903*412f47f9SXin Li                      * similarly to 00000000.00000000.801, but on the
1904*412f47f9SXin Li                      * other hand, we must surely be prepared to
1905*412f47f9SXin Li                      * enforce a genuine underflow-to-zero in _some_
1906*412f47f9SXin Li                      * case where the true mathematical output is
1907*412f47f9SXin Li                      * nonzero but absurdly tiny.
1908*412f47f9SXin Li                      *
1909*412f47f9SXin Li                      * I think a reasonable place to draw the
1910*412f47f9SXin Li                      * distinction is at 00000000.00000000.400, i.e.
1911*412f47f9SXin Li                      * one quarter of the minimum positive denormal.
1912*412f47f9SXin Li                      * If a value less than that rounds up to the
1913*412f47f9SXin Li                      * minimum denormal, that must mean the function
1914*412f47f9SXin Li                      * under test has managed to make an error of an
1915*412f47f9SXin Li                      * entire factor of two, and that's something we
1916*412f47f9SXin Li                      * should fix. Above that, you can misround within
1917*412f47f9SXin Li                      * the limits of your accuracy bound if you have
1918*412f47f9SXin Li                      * to.
1919*412f47f9SXin Li                      */
1920*412f47f9SXin Li                     if (result[2] < 0x40000000) {
1921*412f47f9SXin Li                         /* Total underflow (ERANGE + UFL) is required,
1922*412f47f9SXin Li                          * and we suppress the extra bits to make
1923*412f47f9SXin Li                          * mathtest enforce that the output is really
1924*412f47f9SXin Li                          * zero. */
1925*412f47f9SXin Li                         errstr = "underflow";
1926*412f47f9SXin Li                         printextra = 0;
1927*412f47f9SXin Li                     } else {
1928*412f47f9SXin Li                         /* Total underflow is not required, but if the
1929*412f47f9SXin Li                          * function rounds down to zero anyway, then
1930*412f47f9SXin Li                          * we should be prepared to tolerate it. */
1931*412f47f9SXin Li                         errstr = "?underflow";
1932*412f47f9SXin Li                     }
1933*412f47f9SXin Li                 } else if (!(result[0] & 0x7ff00000)) {
1934*412f47f9SXin Li                     /*
1935*412f47f9SXin Li                      * If the output is denormal, we usually expect a
1936*412f47f9SXin Li                      * UFL exception, warning the user of partial
1937*412f47f9SXin Li                      * underflow. The exception is if the denormal
1938*412f47f9SXin Li                      * being returned is just one of the input values,
1939*412f47f9SXin Li                      * unchanged even in principle. I bodgily handle
1940*412f47f9SXin Li                      * this by just special-casing the functions in
1941*412f47f9SXin Li                      * question below.
1942*412f47f9SXin Li                      */
1943*412f47f9SXin Li                     if (!strcmp(fn->name, "fmax") ||
1944*412f47f9SXin Li                         !strcmp(fn->name, "fmin") ||
1945*412f47f9SXin Li                         !strcmp(fn->name, "creal") ||
1946*412f47f9SXin Li                         !strcmp(fn->name, "cimag")) {
1947*412f47f9SXin Li                         /* no error expected */
1948*412f47f9SXin Li                     } else {
1949*412f47f9SXin Li                         errstr = "u";
1950*412f47f9SXin Li                     }
1951*412f47f9SXin Li                 } else if ((result[0] & 0x7FFFFFFF) > 0x7FEFFFFF) {
1952*412f47f9SXin Li                     /*
1953*412f47f9SXin Li                      * Infinite results are usually due to overflow,
1954*412f47f9SXin Li                      * but one exception is lgamma of a negative
1955*412f47f9SXin Li                      * integer.
1956*412f47f9SXin Li                      */
1957*412f47f9SXin Li                     if (!strcmp(fn->name, "lgamma") &&
1958*412f47f9SXin Li                         (args[0] & 0x80000000) != 0 && /* negative */
1959*412f47f9SXin Li                         is_dinteger(args)) {
1960*412f47f9SXin Li                         errstr = "ERANGE status=z";
1961*412f47f9SXin Li                     } else {
1962*412f47f9SXin Li                         errstr = "overflow";
1963*412f47f9SXin Li                     }
1964*412f47f9SXin Li                     printextra = 0;
1965*412f47f9SXin Li                 }
1966*412f47f9SXin Li             } else {
1967*412f47f9SXin Li                 /* lgamma(0) is also a pole. */
1968*412f47f9SXin Li                 if (!strcmp(fn->name, "lgamma")) {
1969*412f47f9SXin Li                     errstr = "ERANGE status=z";
1970*412f47f9SXin Li                     printextra = 0;
1971*412f47f9SXin Li                 }
1972*412f47f9SXin Li             }
1973*412f47f9SXin Li         }
1974*412f47f9SXin Li 
1975*412f47f9SXin Li         if (!printextra || (rejected && !(rejected==1 && result[2]!=0))) {
1976*412f47f9SXin Li             printf(" result=%08x.%08x",
1977*412f47f9SXin Li                    result[0], result[1]);
1978*412f47f9SXin Li         } else {
1979*412f47f9SXin Li             printf(" result=%08x.%08x.%03x",
1980*412f47f9SXin Li                    result[0], result[1], (result[2] >> 20) & 0xFFF);
1981*412f47f9SXin Li         }
1982*412f47f9SXin Li         if (fn->type == rred) {
1983*412f47f9SXin Li             printf(" res2=%08x", result[3]);
1984*412f47f9SXin Li         }
1985*412f47f9SXin Li         break;
1986*412f47f9SXin Li       case args1f:
1987*412f47f9SXin Li       case args2f:
1988*412f47f9SXin Li       case args1fcr:
1989*412f47f9SXin Li       case rredf:
1990*412f47f9SXin Li         printextra = 1;
1991*412f47f9SXin Li         if (rejected == 0) {
1992*412f47f9SXin Li             errstr = NULL;
1993*412f47f9SXin Li             if (!mpfr_zero_p(a)) {
1994*412f47f9SXin Li                 if ((result[0] & 0x7FFFFFFF) == 0) {
1995*412f47f9SXin Li                     /*
1996*412f47f9SXin Li                      * Decide whether to print the extra bits based on
1997*412f47f9SXin Li                      * just how close to zero the number is. See the
1998*412f47f9SXin Li                      * big comment in the double-precision case for
1999*412f47f9SXin Li                      * discussion.
2000*412f47f9SXin Li                      */
2001*412f47f9SXin Li                     if (result[1] < 0x40000000) {
2002*412f47f9SXin Li                         errstr = "underflow";
2003*412f47f9SXin Li                         printextra = 0;
2004*412f47f9SXin Li                     } else {
2005*412f47f9SXin Li                         errstr = "?underflow";
2006*412f47f9SXin Li                     }
2007*412f47f9SXin Li                 } else if (!(result[0] & 0x7f800000)) {
2008*412f47f9SXin Li                     /*
2009*412f47f9SXin Li                      * Functions which do not report partial overflow
2010*412f47f9SXin Li                      * are listed here as special cases. (See the
2011*412f47f9SXin Li                      * corresponding double case above for a fuller
2012*412f47f9SXin Li                      * comment.)
2013*412f47f9SXin Li                      */
2014*412f47f9SXin Li                     if (!strcmp(fn->name, "fmaxf") ||
2015*412f47f9SXin Li                         !strcmp(fn->name, "fminf") ||
2016*412f47f9SXin Li                         !strcmp(fn->name, "crealf") ||
2017*412f47f9SXin Li                         !strcmp(fn->name, "cimagf")) {
2018*412f47f9SXin Li                         /* no error expected */
2019*412f47f9SXin Li                     } else {
2020*412f47f9SXin Li                         errstr = "u";
2021*412f47f9SXin Li                     }
2022*412f47f9SXin Li                 } else if ((result[0] & 0x7FFFFFFF) > 0x7F7FFFFF) {
2023*412f47f9SXin Li                     /*
2024*412f47f9SXin Li                      * Infinite results are usually due to overflow,
2025*412f47f9SXin Li                      * but one exception is lgamma of a negative
2026*412f47f9SXin Li                      * integer.
2027*412f47f9SXin Li                      */
2028*412f47f9SXin Li                     if (!strcmp(fn->name, "lgammaf") &&
2029*412f47f9SXin Li                         (args[0] & 0x80000000) != 0 && /* negative */
2030*412f47f9SXin Li                         is_sinteger(args)) {
2031*412f47f9SXin Li                         errstr = "ERANGE status=z";
2032*412f47f9SXin Li                     } else {
2033*412f47f9SXin Li                         errstr = "overflow";
2034*412f47f9SXin Li                     }
2035*412f47f9SXin Li                     printextra = 0;
2036*412f47f9SXin Li                 }
2037*412f47f9SXin Li             } else {
2038*412f47f9SXin Li                 /* lgamma(0) is also a pole. */
2039*412f47f9SXin Li                 if (!strcmp(fn->name, "lgammaf")) {
2040*412f47f9SXin Li                     errstr = "ERANGE status=z";
2041*412f47f9SXin Li                     printextra = 0;
2042*412f47f9SXin Li                 }
2043*412f47f9SXin Li             }
2044*412f47f9SXin Li         }
2045*412f47f9SXin Li 
2046*412f47f9SXin Li         if (!printextra || (rejected && !(rejected==1 && result[1]!=0))) {
2047*412f47f9SXin Li             printf(" result=%08x",
2048*412f47f9SXin Li                    result[0]);
2049*412f47f9SXin Li         } else {
2050*412f47f9SXin Li             printf(" result=%08x.%03x",
2051*412f47f9SXin Li                    result[0], (result[1] >> 20) & 0xFFF);
2052*412f47f9SXin Li         }
2053*412f47f9SXin Li         if (fn->type == rredf) {
2054*412f47f9SXin Li             printf(" res2=%08x", result[3]);
2055*412f47f9SXin Li         }
2056*412f47f9SXin Li         break;
2057*412f47f9SXin Li       case semi1:              /* return a double result */
2058*412f47f9SXin Li       case semi2:
2059*412f47f9SXin Li       case t_ldexp:
2060*412f47f9SXin Li         printf(" result=%08x.%08x", result[0], result[1]);
2061*412f47f9SXin Li         break;
2062*412f47f9SXin Li       case semi1f:
2063*412f47f9SXin Li       case semi2f:
2064*412f47f9SXin Li       case t_ldexpf:
2065*412f47f9SXin Li         printf(" result=%08x", result[0]);
2066*412f47f9SXin Li         break;
2067*412f47f9SXin Li       case t_frexp:            /* return double * int */
2068*412f47f9SXin Li         printf(" result=%08x.%08x res2=%08x", result[0], result[1],
2069*412f47f9SXin Li                result[2]);
2070*412f47f9SXin Li         break;
2071*412f47f9SXin Li       case t_modf:             /* return double * double */
2072*412f47f9SXin Li         printf(" result=%08x.%08x res2=%08x.%08x",
2073*412f47f9SXin Li                result[0], result[1], result[2], result[3]);
2074*412f47f9SXin Li         break;
2075*412f47f9SXin Li       case t_modff:                    /* return float * float */
2076*412f47f9SXin Li         /* fall through */
2077*412f47f9SXin Li       case t_frexpf:                   /* return float * int */
2078*412f47f9SXin Li         printf(" result=%08x res2=%08x", result[0], result[2]);
2079*412f47f9SXin Li         break;
2080*412f47f9SXin Li       case classify:
2081*412f47f9SXin Li       case classifyf:
2082*412f47f9SXin Li       case compare:
2083*412f47f9SXin Li       case comparef:
2084*412f47f9SXin Li         printf(" result=%x", result[0]);
2085*412f47f9SXin Li         break;
2086*412f47f9SXin Li       case args1c:
2087*412f47f9SXin Li       case args2c:
2088*412f47f9SXin Li         if (0/* errstr */) {
2089*412f47f9SXin Li             printf(" resultr=%08x.%08x", result[0], result[1]);
2090*412f47f9SXin Li             printf(" resulti=%08x.%08x", result[4], result[5]);
2091*412f47f9SXin Li         } else {
2092*412f47f9SXin Li             printf(" resultr=%08x.%08x.%03x",
2093*412f47f9SXin Li                    result[0], result[1], (result[2] >> 20) & 0xFFF);
2094*412f47f9SXin Li             printf(" resulti=%08x.%08x.%03x",
2095*412f47f9SXin Li                    result[4], result[5], (result[6] >> 20) & 0xFFF);
2096*412f47f9SXin Li         }
2097*412f47f9SXin Li         /* Underflow behaviour doesn't seem to be specified for complex arithmetic */
2098*412f47f9SXin Li         errstr = "?underflow";
2099*412f47f9SXin Li         break;
2100*412f47f9SXin Li       case args1fc:
2101*412f47f9SXin Li       case args2fc:
2102*412f47f9SXin Li         if (0/* errstr */) {
2103*412f47f9SXin Li             printf(" resultr=%08x", result[0]);
2104*412f47f9SXin Li             printf(" resulti=%08x", result[4]);
2105*412f47f9SXin Li         } else {
2106*412f47f9SXin Li             printf(" resultr=%08x.%03x",
2107*412f47f9SXin Li                    result[0], (result[1] >> 20) & 0xFFF);
2108*412f47f9SXin Li             printf(" resulti=%08x.%03x",
2109*412f47f9SXin Li                    result[4], (result[5] >> 20) & 0xFFF);
2110*412f47f9SXin Li         }
2111*412f47f9SXin Li         /* Underflow behaviour doesn't seem to be specified for complex arithmetic */
2112*412f47f9SXin Li         errstr = "?underflow";
2113*412f47f9SXin Li         break;
2114*412f47f9SXin Li     }
2115*412f47f9SXin Li 
2116*412f47f9SXin Li     if (errstr && *(errstr+1) == '\0') {
2117*412f47f9SXin Li         printf(" errno=0 status=%c",*errstr);
2118*412f47f9SXin Li     } else if (errstr && *errstr == '?') {
2119*412f47f9SXin Li         printf(" maybeerror=%s", errstr+1);
2120*412f47f9SXin Li     } else if (errstr && errstr[0] == 'E') {
2121*412f47f9SXin Li         printf(" errno=%s", errstr);
2122*412f47f9SXin Li     } else {
2123*412f47f9SXin Li         printf(" error=%s", errstr && *errstr ? errstr : "0");
2124*412f47f9SXin Li     }
2125*412f47f9SXin Li 
2126*412f47f9SXin Li     printf("\n");
2127*412f47f9SXin Li 
2128*412f47f9SXin Li     vet_for_decline(fn, args, result, 0);
2129*412f47f9SXin Li 
2130*412f47f9SXin Li   cleanup:
2131*412f47f9SXin Li     mpfr_clear(a);
2132*412f47f9SXin Li     mpfr_clear(b);
2133*412f47f9SXin Li     mpfr_clear(r);
2134*412f47f9SXin Li     mpc_clear(ac);
2135*412f47f9SXin Li     mpc_clear(bc);
2136*412f47f9SXin Li     mpc_clear(rc);
2137*412f47f9SXin Li }
2138*412f47f9SXin Li 
gencases(Testable * fn,int number)2139*412f47f9SXin Li void gencases(Testable *fn, int number) {
2140*412f47f9SXin Li     int i;
2141*412f47f9SXin Li     uint32 args[8];
2142*412f47f9SXin Li 
2143*412f47f9SXin Li     float32_case(NULL);
2144*412f47f9SXin Li     float64_case(NULL);
2145*412f47f9SXin Li 
2146*412f47f9SXin Li     printf("random=on\n"); /* signal to runtests.pl that the following tests are randomly generated */
2147*412f47f9SXin Li     for (i = 0; i < number; i++) {
2148*412f47f9SXin Li         /* generate test point */
2149*412f47f9SXin Li         fn->cases(args, fn->caseparam1, fn->caseparam2);
2150*412f47f9SXin Li         docase(fn, args);
2151*412f47f9SXin Li     }
2152*412f47f9SXin Li     printf("random=off\n");
2153*412f47f9SXin Li }
2154*412f47f9SXin Li 
doubletop(int x,int scale)2155*412f47f9SXin Li static uint32 doubletop(int x, int scale) {
2156*412f47f9SXin Li     int e = 0x412 + scale;
2157*412f47f9SXin Li     while (!(x & 0x100000))
2158*412f47f9SXin Li         x <<= 1, e--;
2159*412f47f9SXin Li     return (e << 20) + x;
2160*412f47f9SXin Li }
2161*412f47f9SXin Li 
floatval(int x,int scale)2162*412f47f9SXin Li static uint32 floatval(int x, int scale) {
2163*412f47f9SXin Li     int e = 0x95 + scale;
2164*412f47f9SXin Li     while (!(x & 0x800000))
2165*412f47f9SXin Li         x <<= 1, e--;
2166*412f47f9SXin Li     return (e << 23) + x;
2167*412f47f9SXin Li }
2168