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