xref: /freebsd-src/contrib/arm-optimized-routines/math/test/rtest/dotest.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
131914882SAlex Richardson /*
231914882SAlex Richardson  * dotest.c - actually generate mathlib test cases
331914882SAlex Richardson  *
4*f3087befSAndrew Turner  * Copyright (c) 1999-2024, Arm Limited.
5072a4ba8SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson  */
731914882SAlex Richardson 
831914882SAlex Richardson #include <stdio.h>
931914882SAlex Richardson #include <string.h>
1031914882SAlex Richardson #include <stdlib.h>
1131914882SAlex Richardson #include <stdint.h>
1231914882SAlex Richardson #include <assert.h>
1331914882SAlex Richardson #include <limits.h>
1431914882SAlex Richardson 
1531914882SAlex Richardson #include "semi.h"
1631914882SAlex Richardson #include "intern.h"
1731914882SAlex Richardson #include "random.h"
1831914882SAlex Richardson 
1931914882SAlex Richardson #define MPFR_PREC 96 /* good enough for float or double + a few extra bits */
2031914882SAlex Richardson 
21*f3087befSAndrew Turner #if MPFR_VERSION < MPFR_VERSION_NUM(4, 2, 0)
22*f3087befSAndrew Turner int
23*f3087befSAndrew Turner mpfr_tanpi (mpfr_t ret, const mpfr_t arg, mpfr_rnd_t rnd)
24*f3087befSAndrew Turner {
25*f3087befSAndrew Turner   MPFR_DECL_INIT (frd, MPFR_PREC);
26*f3087befSAndrew Turner   mpfr_const_pi (frd, GMP_RNDN);
27*f3087befSAndrew Turner   mpfr_mul (frd, frd, arg, GMP_RNDN);
28*f3087befSAndrew Turner   return mpfr_tan (ret, frd, GMP_RNDN);
29*f3087befSAndrew Turner }
30*f3087befSAndrew Turner 
31*f3087befSAndrew Turner int
32*f3087befSAndrew Turner mpfr_sinpi (mpfr_t ret, const mpfr_t arg, mpfr_rnd_t rnd)
33*f3087befSAndrew Turner {
34*f3087befSAndrew Turner   MPFR_DECL_INIT (frd, MPFR_PREC);
35*f3087befSAndrew Turner   mpfr_const_pi (frd, GMP_RNDN);
36*f3087befSAndrew Turner   mpfr_mul (frd, frd, arg, GMP_RNDN);
37*f3087befSAndrew Turner   return mpfr_sin (ret, frd, GMP_RNDN);
38*f3087befSAndrew Turner }
39*f3087befSAndrew Turner 
40*f3087befSAndrew Turner int
41*f3087befSAndrew Turner mpfr_cospi (mpfr_t ret, const mpfr_t arg, mpfr_rnd_t rnd)
42*f3087befSAndrew Turner {
43*f3087befSAndrew Turner   MPFR_DECL_INIT (frd, MPFR_PREC);
44*f3087befSAndrew Turner   mpfr_const_pi (frd, GMP_RNDN);
45*f3087befSAndrew Turner   mpfr_mul (frd, frd, arg, GMP_RNDN);
46*f3087befSAndrew Turner   return mpfr_cos (ret, frd, GMP_RNDN);
47*f3087befSAndrew Turner }
48*f3087befSAndrew Turner #endif
49*f3087befSAndrew Turner 
5031914882SAlex Richardson extern int lib_fo, lib_no_arith, ntests;
5131914882SAlex Richardson 
5231914882SAlex Richardson /*
5331914882SAlex Richardson  * Prototypes.
5431914882SAlex Richardson  */
5531914882SAlex Richardson static void cases_biased(uint32 *, uint32, uint32);
5631914882SAlex Richardson static void cases_biased_positive(uint32 *, uint32, uint32);
5731914882SAlex Richardson static void cases_biased_float(uint32 *, uint32, uint32);
5831914882SAlex Richardson static void cases_uniform(uint32 *, uint32, uint32);
5931914882SAlex Richardson static void cases_uniform_positive(uint32 *, uint32, uint32);
6031914882SAlex Richardson static void cases_uniform_float(uint32 *, uint32, uint32);
6131914882SAlex Richardson static void cases_uniform_float_positive(uint32 *, uint32, uint32);
6231914882SAlex Richardson static void log_cases(uint32 *, uint32, uint32);
6331914882SAlex Richardson static void log_cases_float(uint32 *, uint32, uint32);
6431914882SAlex Richardson static void log1p_cases(uint32 *, uint32, uint32);
6531914882SAlex Richardson static void log1p_cases_float(uint32 *, uint32, uint32);
6631914882SAlex Richardson static void minmax_cases(uint32 *, uint32, uint32);
6731914882SAlex Richardson static void minmax_cases_float(uint32 *, uint32, uint32);
6831914882SAlex Richardson static void atan2_cases(uint32 *, uint32, uint32);
6931914882SAlex Richardson static void atan2_cases_float(uint32 *, uint32, uint32);
7031914882SAlex Richardson static void pow_cases(uint32 *, uint32, uint32);
7131914882SAlex Richardson static void pow_cases_float(uint32 *, uint32, uint32);
7231914882SAlex Richardson static void rred_cases(uint32 *, uint32, uint32);
7331914882SAlex Richardson static void rred_cases_float(uint32 *, uint32, uint32);
7431914882SAlex Richardson static void cases_semi1(uint32 *, uint32, uint32);
7531914882SAlex Richardson static void cases_semi1_float(uint32 *, uint32, uint32);
7631914882SAlex Richardson static void cases_semi2(uint32 *, uint32, uint32);
7731914882SAlex Richardson static void cases_semi2_float(uint32 *, uint32, uint32);
7831914882SAlex Richardson static void cases_ldexp(uint32 *, uint32, uint32);
7931914882SAlex Richardson static void cases_ldexp_float(uint32 *, uint32, uint32);
8031914882SAlex Richardson 
8131914882SAlex Richardson static void complex_cases_uniform(uint32 *, uint32, uint32);
8231914882SAlex Richardson static void complex_cases_uniform_float(uint32 *, uint32, uint32);
8331914882SAlex Richardson static void complex_cases_biased(uint32 *, uint32, uint32);
8431914882SAlex Richardson static void complex_cases_biased_float(uint32 *, uint32, uint32);
8531914882SAlex Richardson static void complex_log_cases(uint32 *, uint32, uint32);
8631914882SAlex Richardson static void complex_log_cases_float(uint32 *, uint32, uint32);
8731914882SAlex Richardson static void complex_pow_cases(uint32 *, uint32, uint32);
8831914882SAlex Richardson static void complex_pow_cases_float(uint32 *, uint32, uint32);
8931914882SAlex Richardson static void complex_arithmetic_cases(uint32 *, uint32, uint32);
9031914882SAlex Richardson static void complex_arithmetic_cases_float(uint32 *, uint32, uint32);
9131914882SAlex Richardson 
9231914882SAlex Richardson static uint32 doubletop(int x, int scale);
9331914882SAlex Richardson static uint32 floatval(int x, int scale);
9431914882SAlex Richardson 
9531914882SAlex Richardson /*
9631914882SAlex Richardson  * Convert back and forth between IEEE bit patterns and the
9731914882SAlex Richardson  * mpfr_t/mpc_t types.
9831914882SAlex Richardson  */
9931914882SAlex Richardson static void set_mpfr_d(mpfr_t x, uint32 h, uint32 l)
10031914882SAlex Richardson {
10131914882SAlex Richardson     uint64_t hl = ((uint64_t)h << 32) | l;
10231914882SAlex Richardson     uint32 exp = (hl >> 52) & 0x7ff;
10331914882SAlex Richardson     int64_t mantissa = hl & (((uint64_t)1 << 52) - 1);
10431914882SAlex Richardson     int sign = (hl >> 63) ? -1 : +1;
10531914882SAlex Richardson     if (exp == 0x7ff) {
10631914882SAlex Richardson         if (mantissa == 0)
10731914882SAlex Richardson             mpfr_set_inf(x, sign);
10831914882SAlex Richardson         else
10931914882SAlex Richardson             mpfr_set_nan(x);
11031914882SAlex Richardson     } else if (exp == 0 && mantissa == 0) {
11131914882SAlex Richardson         mpfr_set_ui(x, 0, GMP_RNDN);
11231914882SAlex Richardson         mpfr_setsign(x, x, sign < 0, GMP_RNDN);
11331914882SAlex Richardson     } else {
11431914882SAlex Richardson         if (exp != 0)
11531914882SAlex Richardson             mantissa |= ((uint64_t)1 << 52);
11631914882SAlex Richardson         else
11731914882SAlex Richardson             exp++;
11831914882SAlex Richardson         mpfr_set_sj_2exp(x, mantissa * sign, (int)exp - 0x3ff - 52, GMP_RNDN);
11931914882SAlex Richardson     }
12031914882SAlex Richardson }
12131914882SAlex Richardson static void set_mpfr_f(mpfr_t x, uint32 f)
12231914882SAlex Richardson {
12331914882SAlex Richardson     uint32 exp = (f >> 23) & 0xff;
12431914882SAlex Richardson     int32 mantissa = f & ((1 << 23) - 1);
12531914882SAlex Richardson     int sign = (f >> 31) ? -1 : +1;
12631914882SAlex Richardson     if (exp == 0xff) {
12731914882SAlex Richardson         if (mantissa == 0)
12831914882SAlex Richardson             mpfr_set_inf(x, sign);
12931914882SAlex Richardson         else
13031914882SAlex Richardson             mpfr_set_nan(x);
13131914882SAlex Richardson     } else if (exp == 0 && mantissa == 0) {
13231914882SAlex Richardson         mpfr_set_ui(x, 0, GMP_RNDN);
13331914882SAlex Richardson         mpfr_setsign(x, x, sign < 0, GMP_RNDN);
13431914882SAlex Richardson     } else {
13531914882SAlex Richardson         if (exp != 0)
13631914882SAlex Richardson             mantissa |= (1 << 23);
13731914882SAlex Richardson         else
13831914882SAlex Richardson             exp++;
13931914882SAlex Richardson         mpfr_set_sj_2exp(x, mantissa * sign, (int)exp - 0x7f - 23, GMP_RNDN);
14031914882SAlex Richardson     }
14131914882SAlex Richardson }
14231914882SAlex Richardson static void set_mpc_d(mpc_t z, uint32 rh, uint32 rl, uint32 ih, uint32 il)
14331914882SAlex Richardson {
14431914882SAlex Richardson     mpfr_t x, y;
14531914882SAlex Richardson     mpfr_init2(x, MPFR_PREC);
14631914882SAlex Richardson     mpfr_init2(y, MPFR_PREC);
14731914882SAlex Richardson     set_mpfr_d(x, rh, rl);
14831914882SAlex Richardson     set_mpfr_d(y, ih, il);
14931914882SAlex Richardson     mpc_set_fr_fr(z, x, y, MPC_RNDNN);
15031914882SAlex Richardson     mpfr_clear(x);
15131914882SAlex Richardson     mpfr_clear(y);
15231914882SAlex Richardson }
15331914882SAlex Richardson static void set_mpc_f(mpc_t z, uint32 r, uint32 i)
15431914882SAlex Richardson {
15531914882SAlex Richardson     mpfr_t x, y;
15631914882SAlex Richardson     mpfr_init2(x, MPFR_PREC);
15731914882SAlex Richardson     mpfr_init2(y, MPFR_PREC);
15831914882SAlex Richardson     set_mpfr_f(x, r);
15931914882SAlex Richardson     set_mpfr_f(y, i);
16031914882SAlex Richardson     mpc_set_fr_fr(z, x, y, MPC_RNDNN);
16131914882SAlex Richardson     mpfr_clear(x);
16231914882SAlex Richardson     mpfr_clear(y);
16331914882SAlex Richardson }
16431914882SAlex Richardson static void get_mpfr_d(const mpfr_t x, uint32 *h, uint32 *l, uint32 *extra)
16531914882SAlex Richardson {
16631914882SAlex Richardson     uint32_t sign, expfield, mantfield;
16731914882SAlex Richardson     mpfr_t significand;
16831914882SAlex Richardson     int exp;
16931914882SAlex Richardson 
17031914882SAlex Richardson     if (mpfr_nan_p(x)) {
17131914882SAlex Richardson         *h = 0x7ff80000;
17231914882SAlex Richardson         *l = 0;
17331914882SAlex Richardson         *extra = 0;
17431914882SAlex Richardson         return;
17531914882SAlex Richardson     }
17631914882SAlex Richardson 
17731914882SAlex Richardson     sign = mpfr_signbit(x) ? 0x80000000U : 0;
17831914882SAlex Richardson 
17931914882SAlex Richardson     if (mpfr_inf_p(x)) {
18031914882SAlex Richardson         *h = 0x7ff00000 | sign;
18131914882SAlex Richardson         *l = 0;
18231914882SAlex Richardson         *extra = 0;
18331914882SAlex Richardson         return;
18431914882SAlex Richardson     }
18531914882SAlex Richardson 
18631914882SAlex Richardson     if (mpfr_zero_p(x)) {
18731914882SAlex Richardson         *h = 0x00000000 | sign;
18831914882SAlex Richardson         *l = 0;
18931914882SAlex Richardson         *extra = 0;
19031914882SAlex Richardson         return;
19131914882SAlex Richardson     }
19231914882SAlex Richardson 
19331914882SAlex Richardson     mpfr_init2(significand, MPFR_PREC);
19431914882SAlex Richardson     mpfr_set(significand, x, GMP_RNDN);
19531914882SAlex Richardson     exp = mpfr_get_exp(significand);
19631914882SAlex Richardson     mpfr_set_exp(significand, 0);
19731914882SAlex Richardson 
19831914882SAlex Richardson     /* Now significand is in [1/2,1), and significand * 2^exp == x.
19931914882SAlex Richardson      * So the IEEE exponent corresponding to exp==0 is 0x3fe. */
20031914882SAlex Richardson     if (exp > 0x400) {
20131914882SAlex Richardson         /* overflow to infinity anyway */
20231914882SAlex Richardson         *h = 0x7ff00000 | sign;
20331914882SAlex Richardson         *l = 0;
20431914882SAlex Richardson         *extra = 0;
20531914882SAlex Richardson         mpfr_clear(significand);
20631914882SAlex Richardson         return;
20731914882SAlex Richardson     }
20831914882SAlex Richardson 
20931914882SAlex Richardson     if (exp <= -0x3fe || mpfr_zero_p(x))
21031914882SAlex Richardson         exp = -0x3fd;       /* denormalise */
21131914882SAlex Richardson     expfield = exp + 0x3fd; /* offset to cancel leading mantissa bit */
21231914882SAlex Richardson 
21331914882SAlex Richardson     mpfr_div_2si(significand, x, exp - 21, GMP_RNDN);
21431914882SAlex Richardson     mpfr_abs(significand, significand, GMP_RNDN);
21531914882SAlex Richardson     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
21631914882SAlex Richardson     *h = sign + ((uint64_t)expfield << 20) + mantfield;
21731914882SAlex Richardson     mpfr_sub_ui(significand, significand, mantfield, GMP_RNDN);
21831914882SAlex Richardson     mpfr_mul_2ui(significand, significand, 32, GMP_RNDN);
21931914882SAlex Richardson     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
22031914882SAlex Richardson     *l = mantfield;
22131914882SAlex Richardson     mpfr_sub_ui(significand, significand, mantfield, GMP_RNDN);
22231914882SAlex Richardson     mpfr_mul_2ui(significand, significand, 32, GMP_RNDN);
22331914882SAlex Richardson     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
22431914882SAlex Richardson     *extra = mantfield;
22531914882SAlex Richardson 
22631914882SAlex Richardson     mpfr_clear(significand);
22731914882SAlex Richardson }
22831914882SAlex Richardson static void get_mpfr_f(const mpfr_t x, uint32 *f, uint32 *extra)
22931914882SAlex Richardson {
23031914882SAlex Richardson     uint32_t sign, expfield, mantfield;
23131914882SAlex Richardson     mpfr_t significand;
23231914882SAlex Richardson     int exp;
23331914882SAlex Richardson 
23431914882SAlex Richardson     if (mpfr_nan_p(x)) {
23531914882SAlex Richardson         *f = 0x7fc00000;
23631914882SAlex Richardson         *extra = 0;
23731914882SAlex Richardson         return;
23831914882SAlex Richardson     }
23931914882SAlex Richardson 
24031914882SAlex Richardson     sign = mpfr_signbit(x) ? 0x80000000U : 0;
24131914882SAlex Richardson 
24231914882SAlex Richardson     if (mpfr_inf_p(x)) {
24331914882SAlex Richardson         *f = 0x7f800000 | sign;
24431914882SAlex Richardson         *extra = 0;
24531914882SAlex Richardson         return;
24631914882SAlex Richardson     }
24731914882SAlex Richardson 
24831914882SAlex Richardson     if (mpfr_zero_p(x)) {
24931914882SAlex Richardson         *f = 0x00000000 | sign;
25031914882SAlex Richardson         *extra = 0;
25131914882SAlex Richardson         return;
25231914882SAlex Richardson     }
25331914882SAlex Richardson 
25431914882SAlex Richardson     mpfr_init2(significand, MPFR_PREC);
25531914882SAlex Richardson     mpfr_set(significand, x, GMP_RNDN);
25631914882SAlex Richardson     exp = mpfr_get_exp(significand);
25731914882SAlex Richardson     mpfr_set_exp(significand, 0);
25831914882SAlex Richardson 
25931914882SAlex Richardson     /* Now significand is in [1/2,1), and significand * 2^exp == x.
26031914882SAlex Richardson      * So the IEEE exponent corresponding to exp==0 is 0x7e. */
26131914882SAlex Richardson     if (exp > 0x80) {
26231914882SAlex Richardson         /* overflow to infinity anyway */
26331914882SAlex Richardson         *f = 0x7f800000 | sign;
26431914882SAlex Richardson         *extra = 0;
26531914882SAlex Richardson         mpfr_clear(significand);
26631914882SAlex Richardson         return;
26731914882SAlex Richardson     }
26831914882SAlex Richardson 
26931914882SAlex Richardson     if (exp <= -0x7e || mpfr_zero_p(x))
27031914882SAlex Richardson         exp = -0x7d;                   /* denormalise */
27131914882SAlex Richardson     expfield = exp + 0x7d; /* offset to cancel leading mantissa bit */
27231914882SAlex Richardson 
27331914882SAlex Richardson     mpfr_div_2si(significand, x, exp - 24, GMP_RNDN);
27431914882SAlex Richardson     mpfr_abs(significand, significand, GMP_RNDN);
27531914882SAlex Richardson     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
27631914882SAlex Richardson     *f = sign + ((uint64_t)expfield << 23) + mantfield;
27731914882SAlex Richardson     mpfr_sub_ui(significand, significand, mantfield, GMP_RNDN);
27831914882SAlex Richardson     mpfr_mul_2ui(significand, significand, 32, GMP_RNDN);
27931914882SAlex Richardson     mantfield = mpfr_get_ui(significand, GMP_RNDZ);
28031914882SAlex Richardson     *extra = mantfield;
28131914882SAlex Richardson 
28231914882SAlex Richardson     mpfr_clear(significand);
28331914882SAlex Richardson }
28431914882SAlex Richardson static void get_mpc_d(const mpc_t z,
28531914882SAlex Richardson                       uint32 *rh, uint32 *rl, uint32 *rextra,
28631914882SAlex Richardson                       uint32 *ih, uint32 *il, uint32 *iextra)
28731914882SAlex Richardson {
28831914882SAlex Richardson     mpfr_t x, y;
28931914882SAlex Richardson     mpfr_init2(x, MPFR_PREC);
29031914882SAlex Richardson     mpfr_init2(y, MPFR_PREC);
29131914882SAlex Richardson     mpc_real(x, z, GMP_RNDN);
29231914882SAlex Richardson     mpc_imag(y, z, GMP_RNDN);
29331914882SAlex Richardson     get_mpfr_d(x, rh, rl, rextra);
29431914882SAlex Richardson     get_mpfr_d(y, ih, il, iextra);
29531914882SAlex Richardson     mpfr_clear(x);
29631914882SAlex Richardson     mpfr_clear(y);
29731914882SAlex Richardson }
29831914882SAlex Richardson static void get_mpc_f(const mpc_t z,
29931914882SAlex Richardson                       uint32 *r, uint32 *rextra,
30031914882SAlex Richardson                       uint32 *i, uint32 *iextra)
30131914882SAlex Richardson {
30231914882SAlex Richardson     mpfr_t x, y;
30331914882SAlex Richardson     mpfr_init2(x, MPFR_PREC);
30431914882SAlex Richardson     mpfr_init2(y, MPFR_PREC);
30531914882SAlex Richardson     mpc_real(x, z, GMP_RNDN);
30631914882SAlex Richardson     mpc_imag(y, z, GMP_RNDN);
30731914882SAlex Richardson     get_mpfr_f(x, r, rextra);
30831914882SAlex Richardson     get_mpfr_f(y, i, iextra);
30931914882SAlex Richardson     mpfr_clear(x);
31031914882SAlex Richardson     mpfr_clear(y);
31131914882SAlex Richardson }
31231914882SAlex Richardson 
31331914882SAlex Richardson /*
31431914882SAlex Richardson  * Implementation of mathlib functions that aren't trivially
31531914882SAlex Richardson  * implementable using an existing mpfr or mpc function.
31631914882SAlex Richardson  */
31731914882SAlex Richardson int test_rred(mpfr_t ret, const mpfr_t x, int *quadrant)
31831914882SAlex Richardson {
31931914882SAlex Richardson     mpfr_t halfpi;
32031914882SAlex Richardson     long quo;
32131914882SAlex Richardson     int status;
32231914882SAlex Richardson 
32331914882SAlex Richardson     /*
32431914882SAlex Richardson      * In the worst case of range reduction, we get an input of size
32531914882SAlex Richardson      * around 2^1024, and must find its remainder mod pi, which means
32631914882SAlex Richardson      * we need 1024 bits of pi at least. Plus, the remainder might
32731914882SAlex Richardson      * happen to come out very very small if we're unlucky. How
32831914882SAlex Richardson      * unlucky can we be? Well, conveniently, I once went through and
32931914882SAlex Richardson      * actually worked that out using Paxson's modular minimisation
33031914882SAlex Richardson      * algorithm, and it turns out that the smallest exponent you can
33131914882SAlex Richardson      * get out of a nontrivial[1] double precision range reduction is
33231914882SAlex Richardson      * 0x3c2, i.e. of the order of 2^-61. So we need 1024 bits of pi
33331914882SAlex Richardson      * to get us down to the units digit, another 61 or so bits (say
33431914882SAlex Richardson      * 64) to get down to the highest set bit of the output, and then
33531914882SAlex Richardson      * some bits to make the actual mantissa big enough.
33631914882SAlex Richardson      *
33731914882SAlex Richardson      *   [1] of course the output of range reduction can have an
33831914882SAlex Richardson      *   arbitrarily small exponent in the trivial case, where the
33931914882SAlex Richardson      *   input is so small that it's the identity function. That
34031914882SAlex Richardson      *   doesn't count.
34131914882SAlex Richardson      */
34231914882SAlex Richardson     mpfr_init2(halfpi, MPFR_PREC + 1024 + 64);
34331914882SAlex Richardson     mpfr_const_pi(halfpi, GMP_RNDN);
34431914882SAlex Richardson     mpfr_div_ui(halfpi, halfpi, 2, GMP_RNDN);
34531914882SAlex Richardson 
34631914882SAlex Richardson     status = mpfr_remquo(ret, &quo, x, halfpi, GMP_RNDN);
34731914882SAlex Richardson     *quadrant = quo & 3;
34831914882SAlex Richardson 
34931914882SAlex Richardson     mpfr_clear(halfpi);
35031914882SAlex Richardson 
35131914882SAlex Richardson     return status;
35231914882SAlex Richardson }
35331914882SAlex Richardson int test_lgamma(mpfr_t ret, const mpfr_t x, mpfr_rnd_t rnd)
35431914882SAlex Richardson {
35531914882SAlex Richardson     /*
35631914882SAlex Richardson      * mpfr_lgamma takes an extra int * parameter to hold the output
35731914882SAlex Richardson      * sign. We don't bother testing that, so this wrapper throws away
35831914882SAlex Richardson      * the sign and hence fits into the same function prototype as all
35931914882SAlex Richardson      * the other real->real mpfr functions.
36031914882SAlex Richardson      *
36131914882SAlex Richardson      * There is also mpfr_lngamma which has no sign output and hence
36231914882SAlex Richardson      * has the right prototype already, but unfortunately it returns
36331914882SAlex Richardson      * NaN in cases where gamma(x) < 0, so it's no use to us.
36431914882SAlex Richardson      */
36531914882SAlex Richardson     int sign;
36631914882SAlex Richardson     return mpfr_lgamma(ret, &sign, x, rnd);
36731914882SAlex Richardson }
36831914882SAlex Richardson int test_cpow(mpc_t ret, const mpc_t x, const mpc_t y, mpc_rnd_t rnd)
36931914882SAlex Richardson {
37031914882SAlex Richardson     /*
37131914882SAlex Richardson      * For complex pow, we must bump up the precision by a huge amount
37231914882SAlex Richardson      * if we want it to get the really difficult cases right. (Not
37331914882SAlex Richardson      * that we expect the library under test to be getting those cases
37431914882SAlex Richardson      * right itself, but we'd at least like the test suite to report
37531914882SAlex Richardson      * them as wrong for the _right reason_.)
37631914882SAlex Richardson      *
37731914882SAlex Richardson      * This works around a bug in mpc_pow(), fixed by r1455 in the MPC
37831914882SAlex Richardson      * svn repository (2014-10-14) and expected to be in any MPC
37931914882SAlex Richardson      * release after 1.0.2 (which was the latest release already made
38031914882SAlex Richardson      * at the time of the fix). So as and when we update to an MPC
38131914882SAlex Richardson      * with the fix in it, we could remove this workaround.
38231914882SAlex Richardson      *
38331914882SAlex Richardson      * For the reasons for choosing this amount of extra precision,
38431914882SAlex Richardson      * see analysis in complex/cpownotes.txt for the rationale for the
38531914882SAlex Richardson      * amount.
38631914882SAlex Richardson      */
38731914882SAlex Richardson     mpc_t xbig, ybig, retbig;
38831914882SAlex Richardson     int status;
38931914882SAlex Richardson 
39031914882SAlex Richardson     mpc_init2(xbig, 1034 + 53 + 60 + MPFR_PREC);
39131914882SAlex Richardson     mpc_init2(ybig, 1034 + 53 + 60 + MPFR_PREC);
39231914882SAlex Richardson     mpc_init2(retbig, 1034 + 53 + 60 + MPFR_PREC);
39331914882SAlex Richardson 
39431914882SAlex Richardson     mpc_set(xbig, x, MPC_RNDNN);
39531914882SAlex Richardson     mpc_set(ybig, y, MPC_RNDNN);
39631914882SAlex Richardson     status = mpc_pow(retbig, xbig, ybig, rnd);
39731914882SAlex Richardson     mpc_set(ret, retbig, rnd);
39831914882SAlex Richardson 
39931914882SAlex Richardson     mpc_clear(xbig);
40031914882SAlex Richardson     mpc_clear(ybig);
40131914882SAlex Richardson     mpc_clear(retbig);
40231914882SAlex Richardson 
40331914882SAlex Richardson     return status;
40431914882SAlex Richardson }
40531914882SAlex Richardson 
40631914882SAlex Richardson /*
40731914882SAlex Richardson  * Identify 'hard' values (NaN, Inf, nonzero denormal) for deciding
40831914882SAlex Richardson  * whether microlib will decline to run a test.
40931914882SAlex Richardson  */
41031914882SAlex Richardson #define is_shard(in) ( \
41131914882SAlex Richardson     (((in)[0] & 0x7F800000) == 0x7F800000 || \
41231914882SAlex Richardson      (((in)[0] & 0x7F800000) == 0 && ((in)[0]&0x7FFFFFFF) != 0)))
41331914882SAlex Richardson 
41431914882SAlex Richardson #define is_dhard(in) ( \
41531914882SAlex Richardson     (((in)[0] & 0x7FF00000) == 0x7FF00000 || \
41631914882SAlex Richardson      (((in)[0] & 0x7FF00000) == 0 && (((in)[0] & 0xFFFFF) | (in)[1]) != 0)))
41731914882SAlex Richardson 
41831914882SAlex Richardson /*
41931914882SAlex Richardson  * Identify integers.
42031914882SAlex Richardson  */
42131914882SAlex Richardson int is_dinteger(uint32 *in)
42231914882SAlex Richardson {
42331914882SAlex Richardson     uint32 out[3];
42431914882SAlex Richardson     if ((0x7FF00000 & ~in[0]) == 0)
42531914882SAlex Richardson         return 0;                      /* not finite, hence not integer */
42631914882SAlex Richardson     test_ceil(in, out);
42731914882SAlex Richardson     return in[0] == out[0] && in[1] == out[1];
42831914882SAlex Richardson }
42931914882SAlex Richardson int is_sinteger(uint32 *in)
43031914882SAlex Richardson {
43131914882SAlex Richardson     uint32 out[3];
43231914882SAlex Richardson     if ((0x7F800000 & ~in[0]) == 0)
43331914882SAlex Richardson         return 0;                      /* not finite, hence not integer */
43431914882SAlex Richardson     test_ceilf(in, out);
43531914882SAlex Richardson     return in[0] == out[0];
43631914882SAlex Richardson }
43731914882SAlex Richardson 
43831914882SAlex Richardson /*
43931914882SAlex Richardson  * Identify signalling NaNs.
44031914882SAlex Richardson  */
44131914882SAlex Richardson int is_dsnan(const uint32 *in)
44231914882SAlex Richardson {
44331914882SAlex Richardson     if ((in[0] & 0x7FF00000) != 0x7FF00000)
44431914882SAlex Richardson         return 0;                      /* not the inf/nan exponent */
44531914882SAlex Richardson     if ((in[0] << 12) == 0 && in[1] == 0)
44631914882SAlex Richardson         return 0;                      /* inf */
44731914882SAlex Richardson     if (in[0] & 0x00080000)
44831914882SAlex Richardson         return 0;                      /* qnan */
44931914882SAlex Richardson     return 1;
45031914882SAlex Richardson }
45131914882SAlex Richardson int is_ssnan(const uint32 *in)
45231914882SAlex Richardson {
45331914882SAlex Richardson     if ((in[0] & 0x7F800000) != 0x7F800000)
45431914882SAlex Richardson         return 0;                      /* not the inf/nan exponent */
45531914882SAlex Richardson     if ((in[0] << 9) == 0)
45631914882SAlex Richardson         return 0;                      /* inf */
45731914882SAlex Richardson     if (in[0] & 0x00400000)
45831914882SAlex Richardson         return 0;                      /* qnan */
45931914882SAlex Richardson     return 1;
46031914882SAlex Richardson }
46131914882SAlex Richardson int is_snan(const uint32 *in, int size)
46231914882SAlex Richardson {
46331914882SAlex Richardson     return size == 2 ? is_dsnan(in) : is_ssnan(in);
46431914882SAlex Richardson }
46531914882SAlex Richardson 
46631914882SAlex Richardson /*
46731914882SAlex Richardson  * Wrapper functions called to fix up unusual results after the main
46831914882SAlex Richardson  * test function has run.
46931914882SAlex Richardson  */
47031914882SAlex Richardson void universal_wrapper(wrapperctx *ctx)
47131914882SAlex Richardson {
47231914882SAlex Richardson     /*
47331914882SAlex Richardson      * Any SNaN input gives rise to a QNaN output.
47431914882SAlex Richardson      */
47531914882SAlex Richardson     int op;
47631914882SAlex Richardson     for (op = 0; op < wrapper_get_nops(ctx); op++) {
47731914882SAlex Richardson         int size = wrapper_get_size(ctx, op);
47831914882SAlex Richardson 
47931914882SAlex Richardson         if (!wrapper_is_complex(ctx, op) &&
48031914882SAlex Richardson             is_snan(wrapper_get_ieee(ctx, op), size)) {
48131914882SAlex Richardson             wrapper_set_nan(ctx);
48231914882SAlex Richardson         }
48331914882SAlex Richardson     }
48431914882SAlex Richardson }
48531914882SAlex Richardson 
486*f3087befSAndrew Turner /* clang-format off */
48731914882SAlex Richardson Testable functions[] = {
48831914882SAlex Richardson     /*
48931914882SAlex Richardson      * Trig functions: sin, cos, tan. We test the core function
49031914882SAlex Richardson      * between -16 and +16: we assume that range reduction exists
49131914882SAlex Richardson      * and will be used for larger arguments, and we'll test that
49231914882SAlex Richardson      * separately. Also we only go down to 2^-27 in magnitude,
49331914882SAlex Richardson      * because below that sin(x)=tan(x)=x and cos(x)=1 as far as
49431914882SAlex Richardson      * double precision can tell, which is boring.
49531914882SAlex Richardson      */
49631914882SAlex Richardson     {"sin", (funcptr)mpfr_sin, args1, {NULL},
49731914882SAlex Richardson         cases_uniform, 0x3e400000, 0x40300000},
49831914882SAlex Richardson     {"sinf", (funcptr)mpfr_sin, args1f, {NULL},
49931914882SAlex Richardson         cases_uniform_float, 0x39800000, 0x41800000},
50031914882SAlex Richardson     {"cos", (funcptr)mpfr_cos, args1, {NULL},
50131914882SAlex Richardson         cases_uniform, 0x3e400000, 0x40300000},
50231914882SAlex Richardson     {"cosf", (funcptr)mpfr_cos, args1f, {NULL},
50331914882SAlex Richardson         cases_uniform_float, 0x39800000, 0x41800000},
50431914882SAlex Richardson     {"tan", (funcptr)mpfr_tan, args1, {NULL},
50531914882SAlex Richardson         cases_uniform, 0x3e400000, 0x40300000},
50631914882SAlex Richardson     {"tanf", (funcptr)mpfr_tan, args1f, {NULL},
50731914882SAlex Richardson         cases_uniform_float, 0x39800000, 0x41800000},
50831914882SAlex Richardson     {"sincosf_sinf", (funcptr)mpfr_sin, args1f, {NULL},
50931914882SAlex Richardson         cases_uniform_float, 0x39800000, 0x41800000},
51031914882SAlex Richardson     {"sincosf_cosf", (funcptr)mpfr_cos, args1f, {NULL},
51131914882SAlex Richardson         cases_uniform_float, 0x39800000, 0x41800000},
512*f3087befSAndrew Turner     {"sinpi", (funcptr)mpfr_sinpi, args1, {NULL},
513*f3087befSAndrew Turner         cases_uniform, 0x3e400000, 0x40300000},
514*f3087befSAndrew Turner     {"sinpif", (funcptr)mpfr_sinpi, args1f, {NULL},
515*f3087befSAndrew Turner         cases_uniform_float, 0x39800000, 0x41800000},
516*f3087befSAndrew Turner     {"cospi", (funcptr)mpfr_cospi, args1, {NULL},
517*f3087befSAndrew Turner         cases_uniform, 0x3e400000, 0x40300000},
518*f3087befSAndrew Turner     {"cospif", (funcptr)mpfr_cospi, args1f, {NULL},
519*f3087befSAndrew Turner         cases_uniform_float, 0x39800000, 0x41800000},
520*f3087befSAndrew Turner     {"tanpi", (funcptr)mpfr_tanpi, args1, {NULL},
521*f3087befSAndrew Turner         cases_uniform, 0x3e400000, 0x40300000},
522*f3087befSAndrew Turner     {"tanpif", (funcptr)mpfr_tanpi, args1f, {NULL},
523*f3087befSAndrew Turner         cases_uniform_float, 0x39800000, 0x41800000},
52431914882SAlex Richardson     /*
52531914882SAlex Richardson      * Inverse trig: asin, acos. Between 1 and -1, of course. acos
52631914882SAlex Richardson      * goes down to 2^-54, asin to 2^-27.
52731914882SAlex Richardson      */
52831914882SAlex Richardson     {"asin", (funcptr)mpfr_asin, args1, {NULL},
52931914882SAlex Richardson         cases_uniform, 0x3e400000, 0x3fefffff},
53031914882SAlex Richardson     {"asinf", (funcptr)mpfr_asin, args1f, {NULL},
53131914882SAlex Richardson         cases_uniform_float, 0x39800000, 0x3f7fffff},
53231914882SAlex Richardson     {"acos", (funcptr)mpfr_acos, args1, {NULL},
53331914882SAlex Richardson         cases_uniform, 0x3c900000, 0x3fefffff},
53431914882SAlex Richardson     {"acosf", (funcptr)mpfr_acos, args1f, {NULL},
53531914882SAlex Richardson         cases_uniform_float, 0x33800000, 0x3f7fffff},
53631914882SAlex Richardson     /*
53731914882SAlex Richardson      * Inverse trig: atan. atan is stable (in double prec) with
53831914882SAlex Richardson      * argument magnitude past 2^53, so we'll test up to there.
53931914882SAlex Richardson      * atan(x) is boringly just x below 2^-27.
54031914882SAlex Richardson      */
54131914882SAlex Richardson     {"atan", (funcptr)mpfr_atan, args1, {NULL},
54231914882SAlex Richardson         cases_uniform, 0x3e400000, 0x43400000},
54331914882SAlex Richardson     {"atanf", (funcptr)mpfr_atan, args1f, {NULL},
54431914882SAlex Richardson         cases_uniform_float, 0x39800000, 0x4b800000},
54531914882SAlex Richardson     /*
54631914882SAlex Richardson      * atan2. Interesting cases arise when the exponents of the
54731914882SAlex Richardson      * arguments differ by at most about 50.
54831914882SAlex Richardson      */
54931914882SAlex Richardson     {"atan2", (funcptr)mpfr_atan2, args2, {NULL},
55031914882SAlex Richardson         atan2_cases, 0},
55131914882SAlex Richardson     {"atan2f", (funcptr)mpfr_atan2, args2f, {NULL},
55231914882SAlex Richardson         atan2_cases_float, 0},
55331914882SAlex Richardson     /*
55431914882SAlex Richardson      * The exponentials: exp, sinh, cosh. They overflow at around
55531914882SAlex Richardson      * 710. exp and sinh are boring below 2^-54, cosh below 2^-27.
55631914882SAlex Richardson      */
55731914882SAlex Richardson     {"exp", (funcptr)mpfr_exp, args1, {NULL},
55831914882SAlex Richardson         cases_uniform, 0x3c900000, 0x40878000},
55931914882SAlex Richardson     {"expf", (funcptr)mpfr_exp, args1f, {NULL},
56031914882SAlex Richardson         cases_uniform_float, 0x33800000, 0x42dc0000},
56131914882SAlex Richardson     {"sinh", (funcptr)mpfr_sinh, args1, {NULL},
56231914882SAlex Richardson         cases_uniform, 0x3c900000, 0x40878000},
56331914882SAlex Richardson     {"sinhf", (funcptr)mpfr_sinh, args1f, {NULL},
56431914882SAlex Richardson         cases_uniform_float, 0x33800000, 0x42dc0000},
56531914882SAlex Richardson     {"cosh", (funcptr)mpfr_cosh, args1, {NULL},
56631914882SAlex Richardson         cases_uniform, 0x3e400000, 0x40878000},
56731914882SAlex Richardson     {"coshf", (funcptr)mpfr_cosh, args1f, {NULL},
56831914882SAlex Richardson         cases_uniform_float, 0x39800000, 0x42dc0000},
56931914882SAlex Richardson     /*
57031914882SAlex Richardson      * tanh is stable past around 20. It's boring below 2^-27.
57131914882SAlex Richardson      */
57231914882SAlex Richardson     {"tanh", (funcptr)mpfr_tanh, args1, {NULL},
57331914882SAlex Richardson         cases_uniform, 0x3e400000, 0x40340000},
57431914882SAlex Richardson     {"tanhf", (funcptr)mpfr_tanh, args1f, {NULL},
57531914882SAlex Richardson         cases_uniform, 0x39800000, 0x41100000},
57631914882SAlex Richardson     /*
57731914882SAlex Richardson      * log must be tested only on positive numbers, but can cover
57831914882SAlex Richardson      * the whole range of positive nonzero finite numbers. It never
57931914882SAlex Richardson      * gets boring.
58031914882SAlex Richardson      */
58131914882SAlex Richardson     {"log", (funcptr)mpfr_log, args1, {NULL}, log_cases, 0},
58231914882SAlex Richardson     {"logf", (funcptr)mpfr_log, args1f, {NULL}, log_cases_float, 0},
58331914882SAlex Richardson     {"log10", (funcptr)mpfr_log10, args1, {NULL}, log_cases, 0},
58431914882SAlex Richardson     {"log10f", (funcptr)mpfr_log10, args1f, {NULL}, log_cases_float, 0},
58531914882SAlex Richardson     /*
58631914882SAlex Richardson      * pow.
58731914882SAlex Richardson      */
58831914882SAlex Richardson     {"pow", (funcptr)mpfr_pow, args2, {NULL}, pow_cases, 0},
58931914882SAlex Richardson     {"powf", (funcptr)mpfr_pow, args2f, {NULL}, pow_cases_float, 0},
59031914882SAlex Richardson     /*
59131914882SAlex Richardson      * Trig range reduction. We are able to test this for all
59231914882SAlex Richardson      * finite values, but will only bother for things between 2^-3
59331914882SAlex Richardson      * and 2^+52.
59431914882SAlex Richardson      */
59531914882SAlex Richardson     {"rred", (funcptr)test_rred, rred, {NULL}, rred_cases, 0},
59631914882SAlex Richardson     {"rredf", (funcptr)test_rred, rredf, {NULL}, rred_cases_float, 0},
59731914882SAlex Richardson     /*
59831914882SAlex Richardson      * Square and cube root.
59931914882SAlex Richardson      */
60031914882SAlex Richardson     {"sqrt", (funcptr)mpfr_sqrt, args1, {NULL}, log_cases, 0},
60131914882SAlex Richardson     {"sqrtf", (funcptr)mpfr_sqrt, args1f, {NULL}, log_cases_float, 0},
60231914882SAlex Richardson     {"cbrt", (funcptr)mpfr_cbrt, args1, {NULL}, log_cases, 0},
60331914882SAlex Richardson     {"cbrtf", (funcptr)mpfr_cbrt, args1f, {NULL}, log_cases_float, 0},
60431914882SAlex Richardson     {"hypot", (funcptr)mpfr_hypot, args2, {NULL}, atan2_cases, 0},
60531914882SAlex Richardson     {"hypotf", (funcptr)mpfr_hypot, args2f, {NULL}, atan2_cases_float, 0},
60631914882SAlex Richardson     /*
60731914882SAlex Richardson      * Seminumerical functions.
60831914882SAlex Richardson      */
60931914882SAlex Richardson     {"ceil", (funcptr)test_ceil, semi1, {NULL}, cases_semi1},
61031914882SAlex Richardson     {"ceilf", (funcptr)test_ceilf, semi1f, {NULL}, cases_semi1_float},
61131914882SAlex Richardson     {"floor", (funcptr)test_floor, semi1, {NULL}, cases_semi1},
61231914882SAlex Richardson     {"floorf", (funcptr)test_floorf, semi1f, {NULL}, cases_semi1_float},
61331914882SAlex Richardson     {"fmod", (funcptr)test_fmod, semi2, {NULL}, cases_semi2},
61431914882SAlex Richardson     {"fmodf", (funcptr)test_fmodf, semi2f, {NULL}, cases_semi2_float},
61531914882SAlex Richardson     {"ldexp", (funcptr)test_ldexp, t_ldexp, {NULL}, cases_ldexp},
61631914882SAlex Richardson     {"ldexpf", (funcptr)test_ldexpf, t_ldexpf, {NULL}, cases_ldexp_float},
61731914882SAlex Richardson     {"frexp", (funcptr)test_frexp, t_frexp, {NULL}, cases_semi1},
61831914882SAlex Richardson     {"frexpf", (funcptr)test_frexpf, t_frexpf, {NULL}, cases_semi1_float},
61931914882SAlex Richardson     {"modf", (funcptr)test_modf, t_modf, {NULL}, cases_semi1},
62031914882SAlex Richardson     {"modff", (funcptr)test_modff, t_modff, {NULL}, cases_semi1_float},
62131914882SAlex Richardson 
62231914882SAlex Richardson     /*
62331914882SAlex Richardson      * Classification and more semi-numericals
62431914882SAlex Richardson      */
62531914882SAlex Richardson     {"copysign", (funcptr)test_copysign, semi2, {NULL}, cases_semi2},
62631914882SAlex Richardson     {"copysignf", (funcptr)test_copysignf, semi2f, {NULL}, cases_semi2_float},
62731914882SAlex Richardson     {"isfinite", (funcptr)test_isfinite, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
62831914882SAlex Richardson     {"isfinitef", (funcptr)test_isfinitef, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
62931914882SAlex Richardson     {"isinf", (funcptr)test_isinf, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
63031914882SAlex Richardson     {"isinff", (funcptr)test_isinff, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
63131914882SAlex Richardson     {"isnan", (funcptr)test_isnan, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
63231914882SAlex Richardson     {"isnanf", (funcptr)test_isnanf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
63331914882SAlex Richardson     {"isnormal", (funcptr)test_isnormal, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
63431914882SAlex Richardson     {"isnormalf", (funcptr)test_isnormalf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
63531914882SAlex Richardson     {"signbit", (funcptr)test_signbit, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
63631914882SAlex Richardson     {"signbitf", (funcptr)test_signbitf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
63731914882SAlex Richardson     {"fpclassify", (funcptr)test_fpclassify, classify, {NULL}, cases_uniform, 0, 0x7fffffff},
63831914882SAlex Richardson     {"fpclassifyf", (funcptr)test_fpclassifyf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},
63931914882SAlex Richardson     /*
64031914882SAlex Richardson      * Comparisons
64131914882SAlex Richardson      */
64231914882SAlex Richardson     {"isgreater", (funcptr)test_isgreater, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
64331914882SAlex Richardson     {"isgreaterequal", (funcptr)test_isgreaterequal, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
64431914882SAlex Richardson     {"isless", (funcptr)test_isless, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
64531914882SAlex Richardson     {"islessequal", (funcptr)test_islessequal, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
64631914882SAlex Richardson     {"islessgreater", (funcptr)test_islessgreater, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
64731914882SAlex Richardson     {"isunordered", (funcptr)test_isunordered, compare, {NULL}, cases_uniform, 0, 0x7fffffff},
64831914882SAlex Richardson 
64931914882SAlex Richardson     {"isgreaterf", (funcptr)test_isgreaterf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
65031914882SAlex Richardson     {"isgreaterequalf", (funcptr)test_isgreaterequalf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
65131914882SAlex Richardson     {"islessf", (funcptr)test_islessf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
65231914882SAlex Richardson     {"islessequalf", (funcptr)test_islessequalf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
65331914882SAlex Richardson     {"islessgreaterf", (funcptr)test_islessgreaterf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
65431914882SAlex Richardson     {"isunorderedf", (funcptr)test_isunorderedf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},
65531914882SAlex Richardson 
65631914882SAlex Richardson     /*
65731914882SAlex Richardson      * Inverse Hyperbolic functions
65831914882SAlex Richardson      */
65931914882SAlex Richardson     {"atanh", (funcptr)mpfr_atanh, args1, {NULL}, cases_uniform, 0x3e400000, 0x3fefffff},
66031914882SAlex Richardson     {"asinh", (funcptr)mpfr_asinh, args1, {NULL}, cases_uniform, 0x3e400000, 0x3fefffff},
66131914882SAlex Richardson     {"acosh", (funcptr)mpfr_acosh, args1, {NULL}, cases_uniform_positive, 0x3ff00000, 0x7fefffff},
66231914882SAlex Richardson 
66331914882SAlex Richardson     {"atanhf", (funcptr)mpfr_atanh, args1f, {NULL}, cases_uniform_float, 0x32000000, 0x3f7fffff},
66431914882SAlex Richardson     {"asinhf", (funcptr)mpfr_asinh, args1f, {NULL}, cases_uniform_float, 0x32000000, 0x3f7fffff},
66531914882SAlex Richardson     {"acoshf", (funcptr)mpfr_acosh, args1f, {NULL}, cases_uniform_float_positive, 0x3f800000, 0x7f800000},
66631914882SAlex Richardson 
66731914882SAlex Richardson     /*
66831914882SAlex Richardson      * Everything else (sitting in a section down here at the bottom
66931914882SAlex Richardson      * because historically they were not tested because we didn't
67031914882SAlex Richardson      * have reference implementations for them)
67131914882SAlex Richardson      */
67231914882SAlex Richardson     {"csin", (funcptr)mpc_sin, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
67331914882SAlex Richardson     {"csinf", (funcptr)mpc_sin, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
67431914882SAlex Richardson     {"ccos", (funcptr)mpc_cos, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
67531914882SAlex Richardson     {"ccosf", (funcptr)mpc_cos, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
67631914882SAlex Richardson     {"ctan", (funcptr)mpc_tan, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
67731914882SAlex Richardson     {"ctanf", (funcptr)mpc_tan, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
67831914882SAlex Richardson 
67931914882SAlex Richardson     {"casin", (funcptr)mpc_asin, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
68031914882SAlex Richardson     {"casinf", (funcptr)mpc_asin, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
68131914882SAlex Richardson     {"cacos", (funcptr)mpc_acos, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
68231914882SAlex Richardson     {"cacosf", (funcptr)mpc_acos, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
68331914882SAlex Richardson     {"catan", (funcptr)mpc_atan, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
68431914882SAlex Richardson     {"catanf", (funcptr)mpc_atan, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
68531914882SAlex Richardson 
68631914882SAlex Richardson     {"csinh", (funcptr)mpc_sinh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
68731914882SAlex Richardson     {"csinhf", (funcptr)mpc_sinh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
68831914882SAlex Richardson     {"ccosh", (funcptr)mpc_cosh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
68931914882SAlex Richardson     {"ccoshf", (funcptr)mpc_cosh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
69031914882SAlex Richardson     {"ctanh", (funcptr)mpc_tanh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
69131914882SAlex Richardson     {"ctanhf", (funcptr)mpc_tanh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
69231914882SAlex Richardson 
69331914882SAlex Richardson     {"casinh", (funcptr)mpc_asinh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
69431914882SAlex Richardson     {"casinhf", (funcptr)mpc_asinh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
69531914882SAlex Richardson     {"cacosh", (funcptr)mpc_acosh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
69631914882SAlex Richardson     {"cacoshf", (funcptr)mpc_acosh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
69731914882SAlex Richardson     {"catanh", (funcptr)mpc_atanh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},
69831914882SAlex Richardson     {"catanhf", (funcptr)mpc_atanh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},
69931914882SAlex Richardson 
70031914882SAlex Richardson     {"cexp", (funcptr)mpc_exp, args1c, {NULL}, complex_cases_uniform, 0x3c900000, 0x40862000},
70131914882SAlex Richardson     {"cpow", (funcptr)test_cpow, args2c, {NULL}, complex_pow_cases, 0x3fc00000, 0x40000000},
70231914882SAlex Richardson     {"clog", (funcptr)mpc_log, args1c, {NULL}, complex_log_cases, 0, 0},
70331914882SAlex Richardson     {"csqrt", (funcptr)mpc_sqrt, args1c, {NULL}, complex_log_cases, 0, 0},
70431914882SAlex Richardson 
70531914882SAlex Richardson     {"cexpf", (funcptr)mpc_exp, args1fc, {NULL}, complex_cases_uniform_float, 0x24800000, 0x42b00000},
70631914882SAlex Richardson     {"cpowf", (funcptr)test_cpow, args2fc, {NULL}, complex_pow_cases_float, 0x3e000000, 0x41000000},
70731914882SAlex Richardson     {"clogf", (funcptr)mpc_log, args1fc, {NULL}, complex_log_cases_float, 0, 0},
70831914882SAlex Richardson     {"csqrtf", (funcptr)mpc_sqrt, args1fc, {NULL}, complex_log_cases_float, 0, 0},
70931914882SAlex Richardson 
71031914882SAlex Richardson     {"cdiv", (funcptr)mpc_div, args2c, {NULL}, complex_arithmetic_cases, 0, 0},
71131914882SAlex Richardson     {"cmul", (funcptr)mpc_mul, args2c, {NULL}, complex_arithmetic_cases, 0, 0},
71231914882SAlex Richardson     {"cadd", (funcptr)mpc_add, args2c, {NULL}, complex_arithmetic_cases, 0, 0},
71331914882SAlex Richardson     {"csub", (funcptr)mpc_sub, args2c, {NULL}, complex_arithmetic_cases, 0, 0},
71431914882SAlex Richardson 
71531914882SAlex Richardson     {"cdivf", (funcptr)mpc_div, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
71631914882SAlex Richardson     {"cmulf", (funcptr)mpc_mul, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
71731914882SAlex Richardson     {"caddf", (funcptr)mpc_add, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
71831914882SAlex Richardson     {"csubf", (funcptr)mpc_sub, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
71931914882SAlex Richardson 
72031914882SAlex Richardson     {"cabsf", (funcptr)mpc_abs, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},
72131914882SAlex Richardson     {"cabs", (funcptr)mpc_abs, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},
72231914882SAlex Richardson     {"cargf", (funcptr)mpc_arg, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},
72331914882SAlex Richardson     {"carg", (funcptr)mpc_arg, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},
72431914882SAlex Richardson     {"cimagf", (funcptr)mpc_imag, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},
72531914882SAlex Richardson     {"cimag", (funcptr)mpc_imag, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},
72631914882SAlex Richardson     {"conjf", (funcptr)mpc_conj, args1fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
72731914882SAlex Richardson     {"conj", (funcptr)mpc_conj, args1c, {NULL}, complex_arithmetic_cases, 0, 0},
72831914882SAlex Richardson     {"cprojf", (funcptr)mpc_proj, args1fc, {NULL}, complex_arithmetic_cases_float, 0, 0},
72931914882SAlex Richardson     {"cproj", (funcptr)mpc_proj, args1c, {NULL}, complex_arithmetic_cases, 0, 0},
73031914882SAlex Richardson     {"crealf", (funcptr)mpc_real, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},
73131914882SAlex Richardson     {"creal", (funcptr)mpc_real, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},
73231914882SAlex Richardson     {"erfcf", (funcptr)mpfr_erfc, args1f, {NULL}, cases_biased_float, 0x1e800000, 0x41000000},
73331914882SAlex Richardson     {"erfc", (funcptr)mpfr_erfc, args1, {NULL}, cases_biased, 0x3bd00000, 0x403c0000},
73431914882SAlex Richardson     {"erff", (funcptr)mpfr_erf, args1f, {NULL}, cases_biased_float, 0x03800000, 0x40700000},
73531914882SAlex Richardson     {"erf", (funcptr)mpfr_erf, args1, {NULL}, cases_biased, 0x00800000, 0x40200000},
73631914882SAlex Richardson     {"exp2f", (funcptr)mpfr_exp2, args1f, {NULL}, cases_uniform_float, 0x33800000, 0x43c00000},
73731914882SAlex Richardson     {"exp2", (funcptr)mpfr_exp2, args1, {NULL}, cases_uniform, 0x3ca00000, 0x40a00000},
73831914882SAlex Richardson     {"expm1f", (funcptr)mpfr_expm1, args1f, {NULL}, cases_uniform_float, 0x33000000, 0x43800000},
73931914882SAlex Richardson     {"expm1", (funcptr)mpfr_expm1, args1, {NULL}, cases_uniform, 0x3c900000, 0x409c0000},
74031914882SAlex Richardson     {"fmaxf", (funcptr)mpfr_max, args2f, {NULL}, minmax_cases_float, 0, 0x7f7fffff},
74131914882SAlex Richardson     {"fmax", (funcptr)mpfr_max, args2, {NULL}, minmax_cases, 0, 0x7fefffff},
74231914882SAlex Richardson     {"fminf", (funcptr)mpfr_min, args2f, {NULL}, minmax_cases_float, 0, 0x7f7fffff},
74331914882SAlex Richardson     {"fmin", (funcptr)mpfr_min, args2, {NULL}, minmax_cases, 0, 0x7fefffff},
74431914882SAlex Richardson     {"lgammaf", (funcptr)test_lgamma, args1f, {NULL}, cases_uniform_float, 0x01800000, 0x7f800000},
74531914882SAlex Richardson     {"lgamma", (funcptr)test_lgamma, args1, {NULL}, cases_uniform, 0x00100000, 0x7ff00000},
74631914882SAlex Richardson     {"log1pf", (funcptr)mpfr_log1p, args1f, {NULL}, log1p_cases_float, 0, 0},
74731914882SAlex Richardson     {"log1p", (funcptr)mpfr_log1p, args1, {NULL}, log1p_cases, 0, 0},
74831914882SAlex Richardson     {"log2f", (funcptr)mpfr_log2, args1f, {NULL}, log_cases_float, 0, 0},
74931914882SAlex Richardson     {"log2", (funcptr)mpfr_log2, args1, {NULL}, log_cases, 0, 0},
75031914882SAlex Richardson     {"tgammaf", (funcptr)mpfr_gamma, args1f, {NULL}, cases_uniform_float, 0x2f800000, 0x43000000},
75131914882SAlex Richardson     {"tgamma", (funcptr)mpfr_gamma, args1, {NULL}, cases_uniform, 0x3c000000, 0x40800000},
75231914882SAlex Richardson };
753*f3087befSAndrew Turner /* clang-format on */
75431914882SAlex Richardson 
75531914882SAlex Richardson const int nfunctions = ( sizeof(functions)/sizeof(*functions) );
75631914882SAlex Richardson 
75731914882SAlex Richardson #define random_sign ( random_upto(1) ? 0x80000000 : 0 )
75831914882SAlex Richardson 
75931914882SAlex Richardson static int iszero(uint32 *x) {
76031914882SAlex Richardson     return !((x[0] & 0x7FFFFFFF) || x[1]);
76131914882SAlex Richardson }
76231914882SAlex Richardson 
76331914882SAlex Richardson 
76431914882SAlex Richardson static void complex_log_cases(uint32 *out, uint32 param1,
76531914882SAlex Richardson                               uint32 param2) {
76631914882SAlex Richardson     cases_uniform(out,0x00100000,0x7fefffff);
76731914882SAlex Richardson     cases_uniform(out+2,0x00100000,0x7fefffff);
76831914882SAlex Richardson }
76931914882SAlex Richardson 
77031914882SAlex Richardson 
77131914882SAlex Richardson static void complex_log_cases_float(uint32 *out, uint32 param1,
77231914882SAlex Richardson                                     uint32 param2) {
77331914882SAlex Richardson     cases_uniform_float(out,0x00800000,0x7f7fffff);
77431914882SAlex Richardson     cases_uniform_float(out+2,0x00800000,0x7f7fffff);
77531914882SAlex Richardson }
77631914882SAlex Richardson 
77731914882SAlex Richardson static void complex_cases_biased(uint32 *out, uint32 lowbound,
77831914882SAlex Richardson                                  uint32 highbound) {
77931914882SAlex Richardson     cases_biased(out,lowbound,highbound);
78031914882SAlex Richardson     cases_biased(out+2,lowbound,highbound);
78131914882SAlex Richardson }
78231914882SAlex Richardson 
78331914882SAlex Richardson static void complex_cases_biased_float(uint32 *out, uint32 lowbound,
78431914882SAlex Richardson                                        uint32 highbound) {
78531914882SAlex Richardson     cases_biased_float(out,lowbound,highbound);
78631914882SAlex Richardson     cases_biased_float(out+2,lowbound,highbound);
78731914882SAlex Richardson }
78831914882SAlex Richardson 
78931914882SAlex Richardson static void complex_cases_uniform(uint32 *out, uint32 lowbound,
79031914882SAlex Richardson                                  uint32 highbound) {
79131914882SAlex Richardson     cases_uniform(out,lowbound,highbound);
79231914882SAlex Richardson     cases_uniform(out+2,lowbound,highbound);
79331914882SAlex Richardson }
79431914882SAlex Richardson 
79531914882SAlex Richardson static void complex_cases_uniform_float(uint32 *out, uint32 lowbound,
79631914882SAlex Richardson                                        uint32 highbound) {
79731914882SAlex Richardson     cases_uniform_float(out,lowbound,highbound);
79831914882SAlex Richardson     cases_uniform(out+2,lowbound,highbound);
79931914882SAlex Richardson }
80031914882SAlex Richardson 
80131914882SAlex Richardson static void complex_pow_cases(uint32 *out, uint32 lowbound,
80231914882SAlex Richardson                               uint32 highbound) {
80331914882SAlex Richardson     /*
80431914882SAlex Richardson      * Generating non-overflowing cases for complex pow:
80531914882SAlex Richardson      *
80631914882SAlex Richardson      * Our base has both parts within the range [1/2,2], and hence
80731914882SAlex Richardson      * its magnitude is within [1/2,2*sqrt(2)]. The magnitude of its
80831914882SAlex Richardson      * logarithm in base 2 is therefore at most the magnitude of
80931914882SAlex Richardson      * (log2(2*sqrt(2)) + i*pi/log(2)), or in other words
81031914882SAlex Richardson      * hypot(3/2,pi/log(2)) = 4.77. So the magnitude of the exponent
81131914882SAlex Richardson      * input must be at most our output magnitude limit (as a power
81231914882SAlex Richardson      * of two) divided by that.
81331914882SAlex Richardson      *
81431914882SAlex Richardson      * I also set the output magnitude limit a bit low, because we
81531914882SAlex Richardson      * don't guarantee (and neither does glibc) to prevent internal
81631914882SAlex Richardson      * overflow in cases where the output _magnitude_ overflows but
81731914882SAlex Richardson      * scaling it back down by cos and sin of the argument brings it
81831914882SAlex Richardson      * back in range.
81931914882SAlex Richardson      */
82031914882SAlex Richardson     cases_uniform(out,0x3fe00000, 0x40000000);
82131914882SAlex Richardson     cases_uniform(out+2,0x3fe00000, 0x40000000);
82231914882SAlex Richardson     cases_uniform(out+4,0x3f800000, 0x40600000);
82331914882SAlex Richardson     cases_uniform(out+6,0x3f800000, 0x40600000);
82431914882SAlex Richardson }
82531914882SAlex Richardson 
82631914882SAlex Richardson static void complex_pow_cases_float(uint32 *out, uint32 lowbound,
82731914882SAlex Richardson                                     uint32 highbound) {
82831914882SAlex Richardson     /*
82931914882SAlex Richardson      * Reasoning as above, though of course the detailed numbers are
83031914882SAlex Richardson      * all different.
83131914882SAlex Richardson      */
83231914882SAlex Richardson     cases_uniform_float(out,0x3f000000, 0x40000000);
83331914882SAlex Richardson     cases_uniform_float(out+2,0x3f000000, 0x40000000);
83431914882SAlex Richardson     cases_uniform_float(out+4,0x3d600000, 0x41900000);
83531914882SAlex Richardson     cases_uniform_float(out+6,0x3d600000, 0x41900000);
83631914882SAlex Richardson }
83731914882SAlex Richardson 
83831914882SAlex Richardson static void complex_arithmetic_cases(uint32 *out, uint32 lowbound,
83931914882SAlex Richardson                                      uint32 highbound) {
84031914882SAlex Richardson     cases_uniform(out,0,0x7fefffff);
84131914882SAlex Richardson     cases_uniform(out+2,0,0x7fefffff);
84231914882SAlex Richardson     cases_uniform(out+4,0,0x7fefffff);
84331914882SAlex Richardson     cases_uniform(out+6,0,0x7fefffff);
84431914882SAlex Richardson }
84531914882SAlex Richardson 
84631914882SAlex Richardson static void complex_arithmetic_cases_float(uint32 *out, uint32 lowbound,
84731914882SAlex Richardson                                            uint32 highbound) {
84831914882SAlex Richardson     cases_uniform_float(out,0,0x7f7fffff);
84931914882SAlex Richardson     cases_uniform_float(out+2,0,0x7f7fffff);
85031914882SAlex Richardson     cases_uniform_float(out+4,0,0x7f7fffff);
85131914882SAlex Richardson     cases_uniform_float(out+6,0,0x7f7fffff);
85231914882SAlex Richardson }
85331914882SAlex Richardson 
85431914882SAlex Richardson /*
85531914882SAlex Richardson  * Included from fplib test suite, in a compact self-contained
85631914882SAlex Richardson  * form.
85731914882SAlex Richardson  */
85831914882SAlex Richardson 
85931914882SAlex Richardson void float32_case(uint32 *ret) {
86031914882SAlex Richardson     int n, bits;
86131914882SAlex Richardson     uint32 f;
86231914882SAlex Richardson     static int premax, preptr;
86331914882SAlex Richardson     static uint32 *specifics = NULL;
86431914882SAlex Richardson 
86531914882SAlex Richardson     if (!ret) {
86631914882SAlex Richardson         if (specifics)
86731914882SAlex Richardson             free(specifics);
86831914882SAlex Richardson         specifics = NULL;
86931914882SAlex Richardson         premax = preptr = 0;
87031914882SAlex Richardson         return;
87131914882SAlex Richardson     }
87231914882SAlex Richardson 
87331914882SAlex Richardson     if (!specifics) {
87431914882SAlex Richardson         int exps[] = {
87531914882SAlex Richardson             -127, -126, -125, -24, -4, -3, -2, -1, 0, 1, 2, 3, 4,
87631914882SAlex Richardson                 24, 29, 30, 31, 32, 61, 62, 63, 64, 126, 127, 128
87731914882SAlex Richardson         };
87831914882SAlex Richardson         int sign, eptr;
87931914882SAlex Richardson         uint32 se, j;
88031914882SAlex Richardson         /*
88131914882SAlex Richardson          * We want a cross product of:
88231914882SAlex Richardson          *  - each of two sign bits (2)
88331914882SAlex Richardson          *  - each of the above (unbiased) exponents (25)
88431914882SAlex Richardson          *  - the following list of fraction parts:
88531914882SAlex Richardson          *    * zero (1)
88631914882SAlex Richardson          *    * all bits (1)
88731914882SAlex Richardson          *    * one-bit-set (23)
88831914882SAlex Richardson          *    * one-bit-clear (23)
88931914882SAlex Richardson          *    * one-bit-and-above (20: 3 are duplicates)
89031914882SAlex Richardson          *    * one-bit-and-below (20: 3 are duplicates)
89131914882SAlex Richardson          *    (total 88)
89231914882SAlex Richardson          *  (total 4400)
89331914882SAlex Richardson          */
89431914882SAlex Richardson         specifics = malloc(4400 * sizeof(*specifics));
89531914882SAlex Richardson         preptr = 0;
89631914882SAlex Richardson         for (sign = 0; sign <= 1; sign++) {
89731914882SAlex Richardson             for (eptr = 0; eptr < sizeof(exps)/sizeof(*exps); eptr++) {
89831914882SAlex Richardson                 se = (sign ? 0x80000000 : 0) | ((exps[eptr]+127) << 23);
89931914882SAlex Richardson                 /*
90031914882SAlex Richardson                  * Zero.
90131914882SAlex Richardson                  */
90231914882SAlex Richardson                 specifics[preptr++] = se | 0;
90331914882SAlex Richardson                 /*
90431914882SAlex Richardson                  * All bits.
90531914882SAlex Richardson                  */
90631914882SAlex Richardson                 specifics[preptr++] = se | 0x7FFFFF;
90731914882SAlex Richardson                 /*
90831914882SAlex Richardson                  * One-bit-set.
90931914882SAlex Richardson                  */
91031914882SAlex Richardson                 for (j = 1; j && j <= 0x400000; j <<= 1)
91131914882SAlex Richardson                     specifics[preptr++] = se | j;
91231914882SAlex Richardson                 /*
91331914882SAlex Richardson                  * One-bit-clear.
91431914882SAlex Richardson                  */
91531914882SAlex Richardson                 for (j = 1; j && j <= 0x400000; j <<= 1)
91631914882SAlex Richardson                     specifics[preptr++] = se | (0x7FFFFF ^ j);
91731914882SAlex Richardson                 /*
91831914882SAlex Richardson                  * One-bit-and-everything-below.
91931914882SAlex Richardson                  */
92031914882SAlex Richardson                 for (j = 2; j && j <= 0x100000; j <<= 1)
92131914882SAlex Richardson                     specifics[preptr++] = se | (2*j-1);
92231914882SAlex Richardson                 /*
92331914882SAlex Richardson                  * One-bit-and-everything-above.
92431914882SAlex Richardson                  */
92531914882SAlex Richardson                 for (j = 4; j && j <= 0x200000; j <<= 1)
92631914882SAlex Richardson                     specifics[preptr++] = se | (0x7FFFFF ^ (j-1));
92731914882SAlex Richardson                 /*
92831914882SAlex Richardson                  * Done.
92931914882SAlex Richardson                  */
93031914882SAlex Richardson             }
93131914882SAlex Richardson         }
93231914882SAlex Richardson         assert(preptr == 4400);
93331914882SAlex Richardson         premax = preptr;
93431914882SAlex Richardson     }
93531914882SAlex Richardson 
93631914882SAlex Richardson     /*
93731914882SAlex Richardson      * Decide whether to return a pre or a random case.
93831914882SAlex Richardson      */
93931914882SAlex Richardson     n = random32() % (premax+1);
94031914882SAlex Richardson     if (n < preptr) {
94131914882SAlex Richardson         /*
94231914882SAlex Richardson          * Return pre[n].
94331914882SAlex Richardson          */
94431914882SAlex Richardson         uint32 t;
94531914882SAlex Richardson         t = specifics[n];
94631914882SAlex Richardson         specifics[n] = specifics[preptr-1];
94731914882SAlex Richardson         specifics[preptr-1] = t;        /* (not really needed) */
94831914882SAlex Richardson         preptr--;
94931914882SAlex Richardson         *ret = t;
95031914882SAlex Richardson     } else {
95131914882SAlex Richardson         /*
95231914882SAlex Richardson          * Random case.
95331914882SAlex Richardson          * Sign and exponent:
95431914882SAlex Richardson          *  - FIXME
95531914882SAlex Richardson          * Significand:
95631914882SAlex Richardson          *  - with prob 1/5, a totally random bit pattern
95731914882SAlex Richardson          *  - with prob 1/5, all 1s down to some point and then random
95831914882SAlex Richardson          *  - with prob 1/5, all 1s up to some point and then random
95931914882SAlex Richardson          *  - with prob 1/5, all 0s down to some point and then random
96031914882SAlex Richardson          *  - with prob 1/5, all 0s up to some point and then random
96131914882SAlex Richardson          */
96231914882SAlex Richardson         n = random32() % 5;
96331914882SAlex Richardson         f = random32();                /* some random bits */
96431914882SAlex Richardson         bits = random32() % 22 + 1;    /* 1-22 */
96531914882SAlex Richardson         switch (n) {
96631914882SAlex Richardson           case 0:
96731914882SAlex Richardson             break;                     /* leave f alone */
96831914882SAlex Richardson           case 1:
96931914882SAlex Richardson             f |= (1<<bits)-1;
97031914882SAlex Richardson             break;
97131914882SAlex Richardson           case 2:
97231914882SAlex Richardson             f &= ~((1<<bits)-1);
97331914882SAlex Richardson             break;
97431914882SAlex Richardson           case 3:
97531914882SAlex Richardson             f |= ~((1<<bits)-1);
97631914882SAlex Richardson             break;
97731914882SAlex Richardson           case 4:
97831914882SAlex Richardson             f &= (1<<bits)-1;
97931914882SAlex Richardson             break;
98031914882SAlex Richardson         }
98131914882SAlex Richardson         f &= 0x7FFFFF;
98231914882SAlex Richardson         f |= (random32() & 0xFF800000);/* FIXME - do better */
98331914882SAlex Richardson         *ret = f;
98431914882SAlex Richardson     }
98531914882SAlex Richardson }
98631914882SAlex Richardson static void float64_case(uint32 *ret) {
98731914882SAlex Richardson     int n, bits;
98831914882SAlex Richardson     uint32 f, g;
98931914882SAlex Richardson     static int premax, preptr;
99031914882SAlex Richardson     static uint32 (*specifics)[2] = NULL;
99131914882SAlex Richardson 
99231914882SAlex Richardson     if (!ret) {
99331914882SAlex Richardson         if (specifics)
99431914882SAlex Richardson             free(specifics);
99531914882SAlex Richardson         specifics = NULL;
99631914882SAlex Richardson         premax = preptr = 0;
99731914882SAlex Richardson         return;
99831914882SAlex Richardson     }
99931914882SAlex Richardson 
100031914882SAlex Richardson     if (!specifics) {
100131914882SAlex Richardson         int exps[] = {
100231914882SAlex Richardson             -1023, -1022, -1021, -129, -128, -127, -126, -53, -4, -3, -2,
100331914882SAlex Richardson             -1, 0, 1, 2, 3, 4, 29, 30, 31, 32, 53, 61, 62, 63, 64, 127,
100431914882SAlex Richardson             128, 129, 1022, 1023, 1024
100531914882SAlex Richardson         };
100631914882SAlex Richardson         int sign, eptr;
100731914882SAlex Richardson         uint32 se, j;
100831914882SAlex Richardson         /*
100931914882SAlex Richardson          * We want a cross product of:
101031914882SAlex Richardson          *  - each of two sign bits (2)
101131914882SAlex Richardson          *  - each of the above (unbiased) exponents (32)
101231914882SAlex Richardson          *  - the following list of fraction parts:
101331914882SAlex Richardson          *    * zero (1)
101431914882SAlex Richardson          *    * all bits (1)
101531914882SAlex Richardson          *    * one-bit-set (52)
101631914882SAlex Richardson          *    * one-bit-clear (52)
101731914882SAlex Richardson          *    * one-bit-and-above (49: 3 are duplicates)
101831914882SAlex Richardson          *    * one-bit-and-below (49: 3 are duplicates)
101931914882SAlex Richardson          *    (total 204)
102031914882SAlex Richardson          *  (total 13056)
102131914882SAlex Richardson          */
102231914882SAlex Richardson         specifics = malloc(13056 * sizeof(*specifics));
102331914882SAlex Richardson         preptr = 0;
102431914882SAlex Richardson         for (sign = 0; sign <= 1; sign++) {
102531914882SAlex Richardson             for (eptr = 0; eptr < sizeof(exps)/sizeof(*exps); eptr++) {
102631914882SAlex Richardson                 se = (sign ? 0x80000000 : 0) | ((exps[eptr]+1023) << 20);
102731914882SAlex Richardson                 /*
102831914882SAlex Richardson                  * Zero.
102931914882SAlex Richardson                  */
103031914882SAlex Richardson                 specifics[preptr][0] = 0;
103131914882SAlex Richardson                 specifics[preptr][1] = 0;
103231914882SAlex Richardson                 specifics[preptr++][0] |= se;
103331914882SAlex Richardson                 /*
103431914882SAlex Richardson                  * All bits.
103531914882SAlex Richardson                  */
103631914882SAlex Richardson                 specifics[preptr][0] = 0xFFFFF;
103731914882SAlex Richardson                 specifics[preptr][1] = ~0;
103831914882SAlex Richardson                 specifics[preptr++][0] |= se;
103931914882SAlex Richardson                 /*
104031914882SAlex Richardson                  * One-bit-set.
104131914882SAlex Richardson                  */
104231914882SAlex Richardson                 for (j = 1; j && j <= 0x80000000; j <<= 1) {
104331914882SAlex Richardson                     specifics[preptr][0] = 0;
104431914882SAlex Richardson                     specifics[preptr][1] = j;
104531914882SAlex Richardson                     specifics[preptr++][0] |= se;
104631914882SAlex Richardson                     if (j & 0xFFFFF) {
104731914882SAlex Richardson                         specifics[preptr][0] = j;
104831914882SAlex Richardson                         specifics[preptr][1] = 0;
104931914882SAlex Richardson                         specifics[preptr++][0] |= se;
105031914882SAlex Richardson                     }
105131914882SAlex Richardson                 }
105231914882SAlex Richardson                 /*
105331914882SAlex Richardson                  * One-bit-clear.
105431914882SAlex Richardson                  */
105531914882SAlex Richardson                 for (j = 1; j && j <= 0x80000000; j <<= 1) {
105631914882SAlex Richardson                     specifics[preptr][0] = 0xFFFFF;
105731914882SAlex Richardson                     specifics[preptr][1] = ~j;
105831914882SAlex Richardson                     specifics[preptr++][0] |= se;
105931914882SAlex Richardson                     if (j & 0xFFFFF) {
106031914882SAlex Richardson                         specifics[preptr][0] = 0xFFFFF ^ j;
106131914882SAlex Richardson                         specifics[preptr][1] = ~0;
106231914882SAlex Richardson                         specifics[preptr++][0] |= se;
106331914882SAlex Richardson                     }
106431914882SAlex Richardson                 }
106531914882SAlex Richardson                 /*
106631914882SAlex Richardson                  * One-bit-and-everything-below.
106731914882SAlex Richardson                  */
106831914882SAlex Richardson                 for (j = 2; j && j <= 0x80000000; j <<= 1) {
106931914882SAlex Richardson                     specifics[preptr][0] = 0;
107031914882SAlex Richardson                     specifics[preptr][1] = 2*j-1;
107131914882SAlex Richardson                     specifics[preptr++][0] |= se;
107231914882SAlex Richardson                 }
107331914882SAlex Richardson                 for (j = 1; j && j <= 0x20000; j <<= 1) {
107431914882SAlex Richardson                     specifics[preptr][0] = 2*j-1;
107531914882SAlex Richardson                     specifics[preptr][1] = ~0;
107631914882SAlex Richardson                     specifics[preptr++][0] |= se;
107731914882SAlex Richardson                 }
107831914882SAlex Richardson                 /*
107931914882SAlex Richardson                  * One-bit-and-everything-above.
108031914882SAlex Richardson                  */
108131914882SAlex Richardson                 for (j = 4; j && j <= 0x80000000; j <<= 1) {
108231914882SAlex Richardson                     specifics[preptr][0] = 0xFFFFF;
108331914882SAlex Richardson                     specifics[preptr][1] = ~(j-1);
108431914882SAlex Richardson                     specifics[preptr++][0] |= se;
108531914882SAlex Richardson                 }
108631914882SAlex Richardson                 for (j = 1; j && j <= 0x40000; j <<= 1) {
108731914882SAlex Richardson                     specifics[preptr][0] = 0xFFFFF ^ (j-1);
108831914882SAlex Richardson                     specifics[preptr][1] = 0;
108931914882SAlex Richardson                     specifics[preptr++][0] |= se;
109031914882SAlex Richardson                 }
109131914882SAlex Richardson                 /*
109231914882SAlex Richardson                  * Done.
109331914882SAlex Richardson                  */
109431914882SAlex Richardson             }
109531914882SAlex Richardson         }
109631914882SAlex Richardson         assert(preptr == 13056);
109731914882SAlex Richardson         premax = preptr;
109831914882SAlex Richardson     }
109931914882SAlex Richardson 
110031914882SAlex Richardson     /*
110131914882SAlex Richardson      * Decide whether to return a pre or a random case.
110231914882SAlex Richardson      */
110331914882SAlex Richardson     n = (uint32) random32() % (uint32) (premax+1);
110431914882SAlex Richardson     if (n < preptr) {
110531914882SAlex Richardson         /*
110631914882SAlex Richardson          * Return pre[n].
110731914882SAlex Richardson          */
110831914882SAlex Richardson         uint32 t;
110931914882SAlex Richardson         t = specifics[n][0];
111031914882SAlex Richardson         specifics[n][0] = specifics[preptr-1][0];
111131914882SAlex Richardson         specifics[preptr-1][0] = t;     /* (not really needed) */
111231914882SAlex Richardson         ret[0] = t;
111331914882SAlex Richardson         t = specifics[n][1];
111431914882SAlex Richardson         specifics[n][1] = specifics[preptr-1][1];
111531914882SAlex Richardson         specifics[preptr-1][1] = t;     /* (not really needed) */
111631914882SAlex Richardson         ret[1] = t;
111731914882SAlex Richardson         preptr--;
111831914882SAlex Richardson     } else {
111931914882SAlex Richardson         /*
112031914882SAlex Richardson          * Random case.
112131914882SAlex Richardson          * Sign and exponent:
112231914882SAlex Richardson          *  - FIXME
112331914882SAlex Richardson          * Significand:
112431914882SAlex Richardson          *  - with prob 1/5, a totally random bit pattern
112531914882SAlex Richardson          *  - with prob 1/5, all 1s down to some point and then random
112631914882SAlex Richardson          *  - with prob 1/5, all 1s up to some point and then random
112731914882SAlex Richardson          *  - with prob 1/5, all 0s down to some point and then random
112831914882SAlex Richardson          *  - with prob 1/5, all 0s up to some point and then random
112931914882SAlex Richardson          */
113031914882SAlex Richardson         n = random32() % 5;
113131914882SAlex Richardson         f = random32();                /* some random bits */
113231914882SAlex Richardson         g = random32();                /* some random bits */
113331914882SAlex Richardson         bits = random32() % 51 + 1;    /* 1-51 */
113431914882SAlex Richardson         switch (n) {
113531914882SAlex Richardson           case 0:
113631914882SAlex Richardson             break;                     /* leave f alone */
113731914882SAlex Richardson           case 1:
113831914882SAlex Richardson             if (bits <= 32)
113931914882SAlex Richardson                 f |= (1<<bits)-1;
114031914882SAlex Richardson             else {
114131914882SAlex Richardson                 bits -= 32;
114231914882SAlex Richardson                 g |= (1<<bits)-1;
114331914882SAlex Richardson                 f = ~0;
114431914882SAlex Richardson             }
114531914882SAlex Richardson             break;
114631914882SAlex Richardson           case 2:
114731914882SAlex Richardson             if (bits <= 32)
114831914882SAlex Richardson                 f &= ~((1<<bits)-1);
114931914882SAlex Richardson             else {
115031914882SAlex Richardson                 bits -= 32;
115131914882SAlex Richardson                 g &= ~((1<<bits)-1);
115231914882SAlex Richardson                 f = 0;
115331914882SAlex Richardson             }
115431914882SAlex Richardson             break;
115531914882SAlex Richardson           case 3:
115631914882SAlex Richardson             if (bits <= 32)
115731914882SAlex Richardson                 g &= (1<<bits)-1;
115831914882SAlex Richardson             else {
115931914882SAlex Richardson                 bits -= 32;
116031914882SAlex Richardson                 f &= (1<<bits)-1;
116131914882SAlex Richardson                 g = 0;
116231914882SAlex Richardson             }
116331914882SAlex Richardson             break;
116431914882SAlex Richardson           case 4:
116531914882SAlex Richardson             if (bits <= 32)
116631914882SAlex Richardson                 g |= ~((1<<bits)-1);
116731914882SAlex Richardson             else {
116831914882SAlex Richardson                 bits -= 32;
116931914882SAlex Richardson                 f |= ~((1<<bits)-1);
117031914882SAlex Richardson                 g = ~0;
117131914882SAlex Richardson             }
117231914882SAlex Richardson             break;
117331914882SAlex Richardson         }
117431914882SAlex Richardson         g &= 0xFFFFF;
117531914882SAlex Richardson         g |= (random32() & 0xFFF00000);/* FIXME - do better */
117631914882SAlex Richardson         ret[0] = g;
117731914882SAlex Richardson         ret[1] = f;
117831914882SAlex Richardson     }
117931914882SAlex Richardson }
118031914882SAlex Richardson 
118131914882SAlex Richardson static void cases_biased(uint32 *out, uint32 lowbound,
118231914882SAlex Richardson                           uint32 highbound) {
118331914882SAlex Richardson     do {
118431914882SAlex Richardson         out[0] = highbound - random_upto_biased(highbound-lowbound, 8);
118531914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
118631914882SAlex Richardson         out[0] |= random_sign;
118731914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
118831914882SAlex Richardson }
118931914882SAlex Richardson 
119031914882SAlex Richardson static void cases_biased_positive(uint32 *out, uint32 lowbound,
119131914882SAlex Richardson                                   uint32 highbound) {
119231914882SAlex Richardson     do {
119331914882SAlex Richardson         out[0] = highbound - random_upto_biased(highbound-lowbound, 8);
119431914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
119531914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
119631914882SAlex Richardson }
119731914882SAlex Richardson 
119831914882SAlex Richardson static void cases_biased_float(uint32 *out, uint32 lowbound,
119931914882SAlex Richardson                                uint32 highbound) {
120031914882SAlex Richardson     do {
120131914882SAlex Richardson         out[0] = highbound - random_upto_biased(highbound-lowbound, 8);
120231914882SAlex Richardson         out[1] = 0;
120331914882SAlex Richardson         out[0] |= random_sign;
120431914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
120531914882SAlex Richardson }
120631914882SAlex Richardson 
120731914882SAlex Richardson static void cases_semi1(uint32 *out, uint32 param1,
120831914882SAlex Richardson                         uint32 param2) {
120931914882SAlex Richardson     float64_case(out);
121031914882SAlex Richardson }
121131914882SAlex Richardson 
121231914882SAlex Richardson static void cases_semi1_float(uint32 *out, uint32 param1,
121331914882SAlex Richardson                               uint32 param2) {
121431914882SAlex Richardson     float32_case(out);
121531914882SAlex Richardson }
121631914882SAlex Richardson 
121731914882SAlex Richardson static void cases_semi2(uint32 *out, uint32 param1,
121831914882SAlex Richardson                         uint32 param2) {
121931914882SAlex Richardson     float64_case(out);
122031914882SAlex Richardson     float64_case(out+2);
122131914882SAlex Richardson }
122231914882SAlex Richardson 
122331914882SAlex Richardson static void cases_semi2_float(uint32 *out, uint32 param1,
122431914882SAlex Richardson                         uint32 param2) {
122531914882SAlex Richardson     float32_case(out);
122631914882SAlex Richardson     float32_case(out+2);
122731914882SAlex Richardson }
122831914882SAlex Richardson 
122931914882SAlex Richardson static void cases_ldexp(uint32 *out, uint32 param1,
123031914882SAlex Richardson                         uint32 param2) {
123131914882SAlex Richardson     float64_case(out);
123231914882SAlex Richardson     out[2] = random_upto(2048)-1024;
123331914882SAlex Richardson }
123431914882SAlex Richardson 
123531914882SAlex Richardson static void cases_ldexp_float(uint32 *out, uint32 param1,
123631914882SAlex Richardson                               uint32 param2) {
123731914882SAlex Richardson     float32_case(out);
123831914882SAlex Richardson     out[2] = random_upto(256)-128;
123931914882SAlex Richardson }
124031914882SAlex Richardson 
124131914882SAlex Richardson static void cases_uniform(uint32 *out, uint32 lowbound,
124231914882SAlex Richardson                           uint32 highbound) {
124331914882SAlex Richardson     do {
124431914882SAlex Richardson         out[0] = highbound - random_upto(highbound-lowbound);
124531914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
124631914882SAlex Richardson         out[0] |= random_sign;
124731914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
124831914882SAlex Richardson }
124931914882SAlex Richardson static void cases_uniform_float(uint32 *out, uint32 lowbound,
125031914882SAlex Richardson                                 uint32 highbound) {
125131914882SAlex Richardson     do {
125231914882SAlex Richardson         out[0] = highbound - random_upto(highbound-lowbound);
125331914882SAlex Richardson         out[1] = 0;
125431914882SAlex Richardson         out[0] |= random_sign;
125531914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
125631914882SAlex Richardson }
125731914882SAlex Richardson 
125831914882SAlex Richardson static void cases_uniform_positive(uint32 *out, uint32 lowbound,
125931914882SAlex Richardson                                    uint32 highbound) {
126031914882SAlex Richardson     do {
126131914882SAlex Richardson         out[0] = highbound - random_upto(highbound-lowbound);
126231914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
126331914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
126431914882SAlex Richardson }
126531914882SAlex Richardson static void cases_uniform_float_positive(uint32 *out, uint32 lowbound,
126631914882SAlex Richardson                                          uint32 highbound) {
126731914882SAlex Richardson     do {
126831914882SAlex Richardson         out[0] = highbound - random_upto(highbound-lowbound);
126931914882SAlex Richardson         out[1] = 0;
127031914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
127131914882SAlex Richardson }
127231914882SAlex Richardson 
127331914882SAlex Richardson 
127431914882SAlex Richardson static void log_cases(uint32 *out, uint32 param1,
127531914882SAlex Richardson                       uint32 param2) {
127631914882SAlex Richardson     do {
127731914882SAlex Richardson         out[0] = random_upto(0x7FEFFFFF);
127831914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
127931914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
128031914882SAlex Richardson }
128131914882SAlex Richardson 
128231914882SAlex Richardson static void log_cases_float(uint32 *out, uint32 param1,
128331914882SAlex Richardson                             uint32 param2) {
128431914882SAlex Richardson     do {
128531914882SAlex Richardson         out[0] = random_upto(0x7F7FFFFF);
128631914882SAlex Richardson         out[1] = 0;
128731914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
128831914882SAlex Richardson }
128931914882SAlex Richardson 
129031914882SAlex Richardson static void log1p_cases(uint32 *out, uint32 param1, uint32 param2)
129131914882SAlex Richardson {
129231914882SAlex Richardson     uint32 sign = random_sign;
129331914882SAlex Richardson     if (sign == 0) {
129431914882SAlex Richardson         cases_uniform_positive(out, 0x3c700000, 0x43400000);
129531914882SAlex Richardson     } else {
129631914882SAlex Richardson         cases_uniform_positive(out, 0x3c000000, 0x3ff00000);
129731914882SAlex Richardson     }
129831914882SAlex Richardson     out[0] |= sign;
129931914882SAlex Richardson }
130031914882SAlex Richardson 
130131914882SAlex Richardson static void log1p_cases_float(uint32 *out, uint32 param1, uint32 param2)
130231914882SAlex Richardson {
130331914882SAlex Richardson     uint32 sign = random_sign;
130431914882SAlex Richardson     if (sign == 0) {
130531914882SAlex Richardson         cases_uniform_float_positive(out, 0x32000000, 0x4c000000);
130631914882SAlex Richardson     } else {
130731914882SAlex Richardson         cases_uniform_float_positive(out, 0x30000000, 0x3f800000);
130831914882SAlex Richardson     }
130931914882SAlex Richardson     out[0] |= sign;
131031914882SAlex Richardson }
131131914882SAlex Richardson 
131231914882SAlex Richardson static void minmax_cases(uint32 *out, uint32 param1, uint32 param2)
131331914882SAlex Richardson {
131431914882SAlex Richardson     do {
131531914882SAlex Richardson         out[0] = random_upto(0x7FEFFFFF);
131631914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
131731914882SAlex Richardson         out[0] |= random_sign;
131831914882SAlex Richardson         out[2] = random_upto(0x7FEFFFFF);
131931914882SAlex Richardson         out[3] = random_upto(0xFFFFFFFF);
132031914882SAlex Richardson         out[2] |= random_sign;
132131914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
132231914882SAlex Richardson }
132331914882SAlex Richardson 
132431914882SAlex Richardson static void minmax_cases_float(uint32 *out, uint32 param1, uint32 param2)
132531914882SAlex Richardson {
132631914882SAlex Richardson     do {
132731914882SAlex Richardson         out[0] = random_upto(0x7F7FFFFF);
132831914882SAlex Richardson         out[1] = 0;
132931914882SAlex Richardson         out[0] |= random_sign;
133031914882SAlex Richardson         out[2] = random_upto(0x7F7FFFFF);
133131914882SAlex Richardson         out[3] = 0;
133231914882SAlex Richardson         out[2] |= random_sign;
133331914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
133431914882SAlex Richardson }
133531914882SAlex Richardson 
133631914882SAlex Richardson static void rred_cases(uint32 *out, uint32 param1,
133731914882SAlex Richardson                        uint32 param2) {
133831914882SAlex Richardson     do {
133931914882SAlex Richardson         out[0] = ((0x3fc00000 + random_upto(0x036fffff)) |
134031914882SAlex Richardson                   (random_upto(1) << 31));
134131914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
134231914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
134331914882SAlex Richardson }
134431914882SAlex Richardson 
134531914882SAlex Richardson static void rred_cases_float(uint32 *out, uint32 param1,
134631914882SAlex Richardson                              uint32 param2) {
134731914882SAlex Richardson     do {
134831914882SAlex Richardson         out[0] = ((0x3e000000 + random_upto(0x0cffffff)) |
134931914882SAlex Richardson                   (random_upto(1) << 31));
135031914882SAlex Richardson         out[1] = 0;                    /* for iszero */
135131914882SAlex Richardson     } while (iszero(out));             /* rule out zero */
135231914882SAlex Richardson }
135331914882SAlex Richardson 
135431914882SAlex Richardson static void atan2_cases(uint32 *out, uint32 param1,
135531914882SAlex Richardson                         uint32 param2) {
135631914882SAlex Richardson     do {
135731914882SAlex Richardson         int expdiff = random_upto(101)-51;
135831914882SAlex Richardson         int swap;
135931914882SAlex Richardson         if (expdiff < 0) {
136031914882SAlex Richardson             expdiff = -expdiff;
136131914882SAlex Richardson             swap = 2;
136231914882SAlex Richardson         } else
136331914882SAlex Richardson             swap = 0;
136431914882SAlex Richardson         out[swap ^ 0] = random_upto(0x7FEFFFFF-((expdiff+1)<<20));
136531914882SAlex Richardson         out[swap ^ 2] = random_upto(((expdiff+1)<<20)-1) + out[swap ^ 0];
136631914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
136731914882SAlex Richardson         out[3] = random_upto(0xFFFFFFFF);
136831914882SAlex Richardson         out[0] |= random_sign;
136931914882SAlex Richardson         out[2] |= random_sign;
137031914882SAlex Richardson     } while (iszero(out) || iszero(out+2));/* rule out zero */
137131914882SAlex Richardson }
137231914882SAlex Richardson 
137331914882SAlex Richardson static void atan2_cases_float(uint32 *out, uint32 param1,
137431914882SAlex Richardson                               uint32 param2) {
137531914882SAlex Richardson     do {
137631914882SAlex Richardson         int expdiff = random_upto(44)-22;
137731914882SAlex Richardson         int swap;
137831914882SAlex Richardson         if (expdiff < 0) {
137931914882SAlex Richardson             expdiff = -expdiff;
138031914882SAlex Richardson             swap = 2;
138131914882SAlex Richardson         } else
138231914882SAlex Richardson             swap = 0;
138331914882SAlex Richardson         out[swap ^ 0] = random_upto(0x7F7FFFFF-((expdiff+1)<<23));
138431914882SAlex Richardson         out[swap ^ 2] = random_upto(((expdiff+1)<<23)-1) + out[swap ^ 0];
138531914882SAlex Richardson         out[0] |= random_sign;
138631914882SAlex Richardson         out[2] |= random_sign;
138731914882SAlex Richardson         out[1] = out[3] = 0;           /* for iszero */
138831914882SAlex Richardson     } while (iszero(out) || iszero(out+2));/* rule out zero */
138931914882SAlex Richardson }
139031914882SAlex Richardson 
139131914882SAlex Richardson static void pow_cases(uint32 *out, uint32 param1,
139231914882SAlex Richardson                       uint32 param2) {
139331914882SAlex Richardson     /*
139431914882SAlex Richardson      * Pick an exponent e (-0x33 to +0x7FE) for x, and here's the
139531914882SAlex Richardson      * range of numbers we can use as y:
139631914882SAlex Richardson      *
139731914882SAlex Richardson      * For e < 0x3FE, the range is [-0x400/(0x3FE-e),+0x432/(0x3FE-e)]
139831914882SAlex Richardson      * For e > 0x3FF, the range is [-0x432/(e-0x3FF),+0x400/(e-0x3FF)]
139931914882SAlex Richardson      *
140031914882SAlex Richardson      * For e == 0x3FE or e == 0x3FF, the range gets infinite at one
140131914882SAlex Richardson      * end or the other, so we have to be cleverer: pick a number n
140231914882SAlex Richardson      * of useful bits in the mantissa (1 thru 52, so 1 must imply
140331914882SAlex Richardson      * 0x3ff00000.00000001 whereas 52 is anything at least as big
140431914882SAlex Richardson      * as 0x3ff80000.00000000; for e == 0x3fe, 1 necessarily means
140531914882SAlex Richardson      * 0x3fefffff.ffffffff and 52 is anything at most as big as
140631914882SAlex Richardson      * 0x3fe80000.00000000). Then, as it happens, a sensible
140731914882SAlex Richardson      * maximum power is 2^(63-n) for e == 0x3fe, and 2^(62-n) for
140831914882SAlex Richardson      * e == 0x3ff.
140931914882SAlex Richardson      *
141031914882SAlex Richardson      * We inevitably get some overflows in approximating the log
141131914882SAlex Richardson      * curves by these nasty step functions, but that's all right -
141231914882SAlex Richardson      * we do want _some_ overflows to be tested.
141331914882SAlex Richardson      *
141431914882SAlex Richardson      * Having got that, then, it's just a matter of inventing a
141531914882SAlex Richardson      * probability distribution for all of this.
141631914882SAlex Richardson      */
141731914882SAlex Richardson     int e, n;
141831914882SAlex Richardson     uint32 dmin, dmax;
141931914882SAlex Richardson     const uint32 pmin = 0x3e100000;
142031914882SAlex Richardson 
142131914882SAlex Richardson     /*
142231914882SAlex Richardson      * Generate exponents in a slightly biased fashion.
142331914882SAlex Richardson      */
142431914882SAlex Richardson     e = (random_upto(1) ?              /* is exponent small or big? */
142531914882SAlex Richardson          0x3FE - random_upto_biased(0x431,2) :   /* small */
142631914882SAlex Richardson          0x3FF + random_upto_biased(0x3FF,2));   /* big */
142731914882SAlex Richardson 
142831914882SAlex Richardson     /*
142931914882SAlex Richardson      * Now split into cases.
143031914882SAlex Richardson      */
143131914882SAlex Richardson     if (e < 0x3FE || e > 0x3FF) {
143231914882SAlex Richardson         uint32 imin, imax;
143331914882SAlex Richardson         if (e < 0x3FE)
143431914882SAlex Richardson             imin = 0x40000 / (0x3FE - e), imax = 0x43200 / (0x3FE - e);
143531914882SAlex Richardson         else
143631914882SAlex Richardson             imin = 0x43200 / (e - 0x3FF), imax = 0x40000 / (e - 0x3FF);
143731914882SAlex Richardson         /* Power range runs from -imin to imax. Now convert to doubles */
143831914882SAlex Richardson         dmin = doubletop(imin, -8);
143931914882SAlex Richardson         dmax = doubletop(imax, -8);
144031914882SAlex Richardson         /* Compute the number of mantissa bits. */
144131914882SAlex Richardson         n = (e > 0 ? 53 : 52+e);
144231914882SAlex Richardson     } else {
144331914882SAlex Richardson         /* Critical exponents. Generate a top bit index. */
144431914882SAlex Richardson         n = 52 - random_upto_biased(51, 4);
144531914882SAlex Richardson         if (e == 0x3FE)
144631914882SAlex Richardson             dmax = 63 - n;
144731914882SAlex Richardson         else
144831914882SAlex Richardson             dmax = 62 - n;
144931914882SAlex Richardson         dmax = (dmax << 20) + 0x3FF00000;
145031914882SAlex Richardson         dmin = dmax;
145131914882SAlex Richardson     }
145231914882SAlex Richardson     /* Generate a mantissa. */
145331914882SAlex Richardson     if (n <= 32) {
145431914882SAlex Richardson         out[0] = 0;
145531914882SAlex Richardson         out[1] = random_upto((1 << (n-1)) - 1) + (1 << (n-1));
145631914882SAlex Richardson     } else if (n == 33) {
145731914882SAlex Richardson         out[0] = 1;
145831914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
145931914882SAlex Richardson     } else if (n > 33) {
146031914882SAlex Richardson         out[0] = random_upto((1 << (n-33)) - 1) + (1 << (n-33));
146131914882SAlex Richardson         out[1] = random_upto(0xFFFFFFFF);
146231914882SAlex Richardson     }
146331914882SAlex Richardson     /* Negate the mantissa if e == 0x3FE. */
146431914882SAlex Richardson     if (e == 0x3FE) {
146531914882SAlex Richardson         out[1] = -out[1];
146631914882SAlex Richardson         out[0] = -out[0];
146731914882SAlex Richardson         if (out[1]) out[0]--;
146831914882SAlex Richardson     }
146931914882SAlex Richardson     /* Put the exponent on. */
147031914882SAlex Richardson     out[0] &= 0xFFFFF;
147131914882SAlex Richardson     out[0] |= ((e > 0 ? e : 0) << 20);
147231914882SAlex Richardson     /* Generate a power. Powers don't go below 2^-30. */
147331914882SAlex Richardson     if (random_upto(1)) {
147431914882SAlex Richardson         /* Positive power */
147531914882SAlex Richardson         out[2] = dmax - random_upto_biased(dmax-pmin, 10);
147631914882SAlex Richardson     } else {
147731914882SAlex Richardson         /* Negative power */
147831914882SAlex Richardson         out[2] = (dmin - random_upto_biased(dmin-pmin, 10)) | 0x80000000;
147931914882SAlex Richardson     }
148031914882SAlex Richardson     out[3] = random_upto(0xFFFFFFFF);
148131914882SAlex Richardson }
148231914882SAlex Richardson static void pow_cases_float(uint32 *out, uint32 param1,
148331914882SAlex Richardson                             uint32 param2) {
148431914882SAlex Richardson     /*
148531914882SAlex Richardson      * Pick an exponent e (-0x16 to +0xFE) for x, and here's the
148631914882SAlex Richardson      * range of numbers we can use as y:
148731914882SAlex Richardson      *
148831914882SAlex Richardson      * For e < 0x7E, the range is [-0x80/(0x7E-e),+0x95/(0x7E-e)]
148931914882SAlex Richardson      * For e > 0x7F, the range is [-0x95/(e-0x7F),+0x80/(e-0x7F)]
149031914882SAlex Richardson      *
149131914882SAlex Richardson      * For e == 0x7E or e == 0x7F, the range gets infinite at one
149231914882SAlex Richardson      * end or the other, so we have to be cleverer: pick a number n
149331914882SAlex Richardson      * of useful bits in the mantissa (1 thru 23, so 1 must imply
149431914882SAlex Richardson      * 0x3f800001 whereas 23 is anything at least as big as
149531914882SAlex Richardson      * 0x3fc00000; for e == 0x7e, 1 necessarily means 0x3f7fffff
149631914882SAlex Richardson      * and 23 is anything at most as big as 0x3f400000). Then, as
149731914882SAlex Richardson      * it happens, a sensible maximum power is 2^(31-n) for e ==
149831914882SAlex Richardson      * 0x7e, and 2^(30-n) for e == 0x7f.
149931914882SAlex Richardson      *
150031914882SAlex Richardson      * We inevitably get some overflows in approximating the log
150131914882SAlex Richardson      * curves by these nasty step functions, but that's all right -
150231914882SAlex Richardson      * we do want _some_ overflows to be tested.
150331914882SAlex Richardson      *
150431914882SAlex Richardson      * Having got that, then, it's just a matter of inventing a
150531914882SAlex Richardson      * probability distribution for all of this.
150631914882SAlex Richardson      */
150731914882SAlex Richardson     int e, n;
150831914882SAlex Richardson     uint32 dmin, dmax;
150931914882SAlex Richardson     const uint32 pmin = 0x38000000;
151031914882SAlex Richardson 
151131914882SAlex Richardson     /*
151231914882SAlex Richardson      * Generate exponents in a slightly biased fashion.
151331914882SAlex Richardson      */
151431914882SAlex Richardson     e = (random_upto(1) ?              /* is exponent small or big? */
151531914882SAlex Richardson          0x7E - random_upto_biased(0x94,2) :   /* small */
151631914882SAlex Richardson          0x7F + random_upto_biased(0x7f,2));   /* big */
151731914882SAlex Richardson 
151831914882SAlex Richardson     /*
151931914882SAlex Richardson      * Now split into cases.
152031914882SAlex Richardson      */
152131914882SAlex Richardson     if (e < 0x7E || e > 0x7F) {
152231914882SAlex Richardson         uint32 imin, imax;
152331914882SAlex Richardson         if (e < 0x7E)
152431914882SAlex Richardson             imin = 0x8000 / (0x7e - e), imax = 0x9500 / (0x7e - e);
152531914882SAlex Richardson         else
152631914882SAlex Richardson             imin = 0x9500 / (e - 0x7f), imax = 0x8000 / (e - 0x7f);
152731914882SAlex Richardson         /* Power range runs from -imin to imax. Now convert to doubles */
152831914882SAlex Richardson         dmin = floatval(imin, -8);
152931914882SAlex Richardson         dmax = floatval(imax, -8);
153031914882SAlex Richardson         /* Compute the number of mantissa bits. */
153131914882SAlex Richardson         n = (e > 0 ? 24 : 23+e);
153231914882SAlex Richardson     } else {
153331914882SAlex Richardson         /* Critical exponents. Generate a top bit index. */
153431914882SAlex Richardson         n = 23 - random_upto_biased(22, 4);
153531914882SAlex Richardson         if (e == 0x7E)
153631914882SAlex Richardson             dmax = 31 - n;
153731914882SAlex Richardson         else
153831914882SAlex Richardson             dmax = 30 - n;
153931914882SAlex Richardson         dmax = (dmax << 23) + 0x3F800000;
154031914882SAlex Richardson         dmin = dmax;
154131914882SAlex Richardson     }
154231914882SAlex Richardson     /* Generate a mantissa. */
154331914882SAlex Richardson     out[0] = random_upto((1 << (n-1)) - 1) + (1 << (n-1));
154431914882SAlex Richardson     out[1] = 0;
154531914882SAlex Richardson     /* Negate the mantissa if e == 0x7E. */
154631914882SAlex Richardson     if (e == 0x7E) {
154731914882SAlex Richardson         out[0] = -out[0];
154831914882SAlex Richardson     }
154931914882SAlex Richardson     /* Put the exponent on. */
155031914882SAlex Richardson     out[0] &= 0x7FFFFF;
155131914882SAlex Richardson     out[0] |= ((e > 0 ? e : 0) << 23);
155231914882SAlex Richardson     /* Generate a power. Powers don't go below 2^-15. */
155331914882SAlex Richardson     if (random_upto(1)) {
155431914882SAlex Richardson         /* Positive power */
155531914882SAlex Richardson         out[2] = dmax - random_upto_biased(dmax-pmin, 10);
155631914882SAlex Richardson     } else {
155731914882SAlex Richardson         /* Negative power */
155831914882SAlex Richardson         out[2] = (dmin - random_upto_biased(dmin-pmin, 10)) | 0x80000000;
155931914882SAlex Richardson     }
156031914882SAlex Richardson     out[3] = 0;
156131914882SAlex Richardson }
156231914882SAlex Richardson 
156331914882SAlex Richardson void vet_for_decline(Testable *fn, uint32 *args, uint32 *result, int got_errno_in) {
156431914882SAlex Richardson     int declined = 0;
156531914882SAlex Richardson 
156631914882SAlex Richardson     switch (fn->type) {
156731914882SAlex Richardson       case args1:
156831914882SAlex Richardson       case rred:
156931914882SAlex Richardson       case semi1:
157031914882SAlex Richardson       case t_frexp:
157131914882SAlex Richardson       case t_modf:
157231914882SAlex Richardson       case classify:
157331914882SAlex Richardson       case t_ldexp:
157431914882SAlex Richardson         declined |= lib_fo && is_dhard(args+0);
157531914882SAlex Richardson         break;
157631914882SAlex Richardson       case args1f:
157731914882SAlex Richardson       case rredf:
157831914882SAlex Richardson       case semi1f:
157931914882SAlex Richardson       case t_frexpf:
158031914882SAlex Richardson       case t_modff:
158131914882SAlex Richardson       case classifyf:
158231914882SAlex Richardson         declined |= lib_fo && is_shard(args+0);
158331914882SAlex Richardson         break;
158431914882SAlex Richardson       case args2:
158531914882SAlex Richardson       case semi2:
158631914882SAlex Richardson       case args1c:
158731914882SAlex Richardson       case args1cr:
158831914882SAlex Richardson       case compare:
158931914882SAlex Richardson         declined |= lib_fo && is_dhard(args+0);
159031914882SAlex Richardson         declined |= lib_fo && is_dhard(args+2);
159131914882SAlex Richardson         break;
159231914882SAlex Richardson       case args2f:
159331914882SAlex Richardson       case semi2f:
159431914882SAlex Richardson       case t_ldexpf:
159531914882SAlex Richardson       case comparef:
159631914882SAlex Richardson       case args1fc:
159731914882SAlex Richardson       case args1fcr:
159831914882SAlex Richardson         declined |= lib_fo && is_shard(args+0);
159931914882SAlex Richardson         declined |= lib_fo && is_shard(args+2);
160031914882SAlex Richardson         break;
160131914882SAlex Richardson       case args2c:
160231914882SAlex Richardson         declined |= lib_fo && is_dhard(args+0);
160331914882SAlex Richardson         declined |= lib_fo && is_dhard(args+2);
160431914882SAlex Richardson         declined |= lib_fo && is_dhard(args+4);
160531914882SAlex Richardson         declined |= lib_fo && is_dhard(args+6);
160631914882SAlex Richardson         break;
160731914882SAlex Richardson       case args2fc:
160831914882SAlex Richardson         declined |= lib_fo && is_shard(args+0);
160931914882SAlex Richardson         declined |= lib_fo && is_shard(args+2);
161031914882SAlex Richardson         declined |= lib_fo && is_shard(args+4);
161131914882SAlex Richardson         declined |= lib_fo && is_shard(args+6);
161231914882SAlex Richardson         break;
161331914882SAlex Richardson     }
161431914882SAlex Richardson 
161531914882SAlex Richardson     switch (fn->type) {
161631914882SAlex Richardson       case args1:              /* return an extra-precise result */
161731914882SAlex Richardson       case args2:
161831914882SAlex Richardson       case rred:
161931914882SAlex Richardson       case semi1:              /* return a double result */
162031914882SAlex Richardson       case semi2:
162131914882SAlex Richardson       case t_ldexp:
162231914882SAlex Richardson       case t_frexp:            /* return double * int */
162331914882SAlex Richardson       case args1cr:
162431914882SAlex Richardson         declined |= lib_fo && is_dhard(result);
162531914882SAlex Richardson         break;
162631914882SAlex Richardson       case args1f:
162731914882SAlex Richardson       case args2f:
162831914882SAlex Richardson       case rredf:
162931914882SAlex Richardson       case semi1f:
163031914882SAlex Richardson       case semi2f:
163131914882SAlex Richardson       case t_ldexpf:
163231914882SAlex Richardson       case args1fcr:
163331914882SAlex Richardson         declined |= lib_fo && is_shard(result);
163431914882SAlex Richardson         break;
163531914882SAlex Richardson       case t_modf:             /* return double * double */
163631914882SAlex Richardson         declined |= lib_fo && is_dhard(result+0);
163731914882SAlex Richardson         declined |= lib_fo && is_dhard(result+2);
163831914882SAlex Richardson         break;
163931914882SAlex Richardson       case t_modff:                    /* return float * float */
164031914882SAlex Richardson         declined |= lib_fo && is_shard(result+2);
164131914882SAlex Richardson         /* fall through */
164231914882SAlex Richardson       case t_frexpf:                   /* return float * int */
164331914882SAlex Richardson         declined |= lib_fo && is_shard(result+0);
164431914882SAlex Richardson         break;
164531914882SAlex Richardson       case args1c:
164631914882SAlex Richardson       case args2c:
164731914882SAlex Richardson         declined |= lib_fo && is_dhard(result+0);
164831914882SAlex Richardson         declined |= lib_fo && is_dhard(result+4);
164931914882SAlex Richardson         break;
165031914882SAlex Richardson       case args1fc:
165131914882SAlex Richardson       case args2fc:
165231914882SAlex Richardson         declined |= lib_fo && is_shard(result+0);
165331914882SAlex Richardson         declined |= lib_fo && is_shard(result+4);
165431914882SAlex Richardson         break;
165531914882SAlex Richardson     }
165631914882SAlex Richardson 
165731914882SAlex Richardson     /* Expect basic arithmetic tests to be declined if the command
165831914882SAlex Richardson      * line said that would happen */
165931914882SAlex Richardson     declined |= (lib_no_arith && (fn->func == (funcptr)mpc_add ||
166031914882SAlex Richardson                                   fn->func == (funcptr)mpc_sub ||
166131914882SAlex Richardson                                   fn->func == (funcptr)mpc_mul ||
166231914882SAlex Richardson                                   fn->func == (funcptr)mpc_div));
166331914882SAlex Richardson 
166431914882SAlex Richardson     if (!declined) {
166531914882SAlex Richardson         if (got_errno_in)
166631914882SAlex Richardson             ntests++;
166731914882SAlex Richardson         else
166831914882SAlex Richardson             ntests += 3;
166931914882SAlex Richardson     }
167031914882SAlex Richardson }
167131914882SAlex Richardson 
167231914882SAlex Richardson void docase(Testable *fn, uint32 *args) {
167331914882SAlex Richardson     uint32 result[8];  /* real part in first 4, imaginary part in last 4 */
167431914882SAlex Richardson     char *errstr = NULL;
167531914882SAlex Richardson     mpfr_t a, b, r;
167631914882SAlex Richardson     mpc_t ac, bc, rc;
167731914882SAlex Richardson     int rejected, printextra;
167831914882SAlex Richardson     wrapperctx ctx;
167931914882SAlex Richardson 
168031914882SAlex Richardson     mpfr_init2(a, MPFR_PREC);
168131914882SAlex Richardson     mpfr_init2(b, MPFR_PREC);
168231914882SAlex Richardson     mpfr_init2(r, MPFR_PREC);
168331914882SAlex Richardson     mpc_init2(ac, MPFR_PREC);
168431914882SAlex Richardson     mpc_init2(bc, MPFR_PREC);
168531914882SAlex Richardson     mpc_init2(rc, MPFR_PREC);
168631914882SAlex Richardson 
168731914882SAlex Richardson     printf("func=%s", fn->name);
168831914882SAlex Richardson 
168931914882SAlex Richardson     rejected = 0; /* FIXME */
169031914882SAlex Richardson 
169131914882SAlex Richardson     switch (fn->type) {
169231914882SAlex Richardson       case args1:
169331914882SAlex Richardson       case rred:
169431914882SAlex Richardson       case semi1:
169531914882SAlex Richardson       case t_frexp:
169631914882SAlex Richardson       case t_modf:
169731914882SAlex Richardson       case classify:
169831914882SAlex Richardson         printf(" op1=%08x.%08x", args[0], args[1]);
169931914882SAlex Richardson         break;
170031914882SAlex Richardson       case args1f:
170131914882SAlex Richardson       case rredf:
170231914882SAlex Richardson       case semi1f:
170331914882SAlex Richardson       case t_frexpf:
170431914882SAlex Richardson       case t_modff:
170531914882SAlex Richardson       case classifyf:
170631914882SAlex Richardson         printf(" op1=%08x", args[0]);
170731914882SAlex Richardson         break;
170831914882SAlex Richardson       case args2:
170931914882SAlex Richardson       case semi2:
171031914882SAlex Richardson       case compare:
171131914882SAlex Richardson         printf(" op1=%08x.%08x", args[0], args[1]);
171231914882SAlex Richardson         printf(" op2=%08x.%08x", args[2], args[3]);
171331914882SAlex Richardson         break;
171431914882SAlex Richardson       case args2f:
171531914882SAlex Richardson       case semi2f:
171631914882SAlex Richardson       case t_ldexpf:
171731914882SAlex Richardson       case comparef:
171831914882SAlex Richardson         printf(" op1=%08x", args[0]);
171931914882SAlex Richardson         printf(" op2=%08x", args[2]);
172031914882SAlex Richardson         break;
172131914882SAlex Richardson       case t_ldexp:
172231914882SAlex Richardson         printf(" op1=%08x.%08x", args[0], args[1]);
172331914882SAlex Richardson         printf(" op2=%08x", args[2]);
172431914882SAlex Richardson         break;
172531914882SAlex Richardson       case args1c:
172631914882SAlex Richardson       case args1cr:
172731914882SAlex Richardson         printf(" op1r=%08x.%08x", args[0], args[1]);
172831914882SAlex Richardson         printf(" op1i=%08x.%08x", args[2], args[3]);
172931914882SAlex Richardson         break;
173031914882SAlex Richardson       case args2c:
173131914882SAlex Richardson         printf(" op1r=%08x.%08x", args[0], args[1]);
173231914882SAlex Richardson         printf(" op1i=%08x.%08x", args[2], args[3]);
173331914882SAlex Richardson         printf(" op2r=%08x.%08x", args[4], args[5]);
173431914882SAlex Richardson         printf(" op2i=%08x.%08x", args[6], args[7]);
173531914882SAlex Richardson         break;
173631914882SAlex Richardson       case args1fc:
173731914882SAlex Richardson       case args1fcr:
173831914882SAlex Richardson         printf(" op1r=%08x", args[0]);
173931914882SAlex Richardson         printf(" op1i=%08x", args[2]);
174031914882SAlex Richardson         break;
174131914882SAlex Richardson       case args2fc:
174231914882SAlex Richardson         printf(" op1r=%08x", args[0]);
174331914882SAlex Richardson         printf(" op1i=%08x", args[2]);
174431914882SAlex Richardson         printf(" op2r=%08x", args[4]);
174531914882SAlex Richardson         printf(" op2i=%08x", args[6]);
174631914882SAlex Richardson         break;
174731914882SAlex Richardson       default:
174831914882SAlex Richardson         fprintf(stderr, "internal inconsistency?!\n");
174931914882SAlex Richardson         abort();
175031914882SAlex Richardson     }
175131914882SAlex Richardson 
175231914882SAlex Richardson     if (rejected == 2) {
175331914882SAlex Richardson         printf(" - test case rejected\n");
175431914882SAlex Richardson         goto cleanup;
175531914882SAlex Richardson     }
175631914882SAlex Richardson 
175731914882SAlex Richardson     wrapper_init(&ctx);
175831914882SAlex Richardson 
175931914882SAlex Richardson     if (rejected == 0) {
176031914882SAlex Richardson         switch (fn->type) {
176131914882SAlex Richardson           case args1:
176231914882SAlex Richardson             set_mpfr_d(a, args[0], args[1]);
176331914882SAlex Richardson             wrapper_op_real(&ctx, a, 2, args);
176431914882SAlex Richardson             ((testfunc1)(fn->func))(r, a, GMP_RNDN);
176531914882SAlex Richardson             get_mpfr_d(r, &result[0], &result[1], &result[2]);
176631914882SAlex Richardson             wrapper_result_real(&ctx, r, 2, result);
176731914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
176831914882SAlex Richardson                 get_mpfr_d(r, &result[0], &result[1], &result[2]);
176931914882SAlex Richardson             break;
177031914882SAlex Richardson           case args1cr:
177131914882SAlex Richardson             set_mpc_d(ac, args[0], args[1], args[2], args[3]);
177231914882SAlex Richardson             wrapper_op_complex(&ctx, ac, 2, args);
177331914882SAlex Richardson             ((testfunc1cr)(fn->func))(r, ac, GMP_RNDN);
177431914882SAlex Richardson             get_mpfr_d(r, &result[0], &result[1], &result[2]);
177531914882SAlex Richardson             wrapper_result_real(&ctx, r, 2, result);
177631914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
177731914882SAlex Richardson                 get_mpfr_d(r, &result[0], &result[1], &result[2]);
177831914882SAlex Richardson             break;
177931914882SAlex Richardson           case args1f:
178031914882SAlex Richardson             set_mpfr_f(a, args[0]);
178131914882SAlex Richardson             wrapper_op_real(&ctx, a, 1, args);
178231914882SAlex Richardson             ((testfunc1)(fn->func))(r, a, GMP_RNDN);
178331914882SAlex Richardson             get_mpfr_f(r, &result[0], &result[1]);
178431914882SAlex Richardson             wrapper_result_real(&ctx, r, 1, result);
178531914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
178631914882SAlex Richardson                 get_mpfr_f(r, &result[0], &result[1]);
178731914882SAlex Richardson             break;
178831914882SAlex Richardson           case args1fcr:
178931914882SAlex Richardson             set_mpc_f(ac, args[0], args[2]);
179031914882SAlex Richardson             wrapper_op_complex(&ctx, ac, 1, args);
179131914882SAlex Richardson             ((testfunc1cr)(fn->func))(r, ac, GMP_RNDN);
179231914882SAlex Richardson             get_mpfr_f(r, &result[0], &result[1]);
179331914882SAlex Richardson             wrapper_result_real(&ctx, r, 1, result);
179431914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
179531914882SAlex Richardson                 get_mpfr_f(r, &result[0], &result[1]);
179631914882SAlex Richardson             break;
179731914882SAlex Richardson           case args2:
179831914882SAlex Richardson             set_mpfr_d(a, args[0], args[1]);
179931914882SAlex Richardson             wrapper_op_real(&ctx, a, 2, args);
180031914882SAlex Richardson             set_mpfr_d(b, args[2], args[3]);
180131914882SAlex Richardson             wrapper_op_real(&ctx, b, 2, args+2);
180231914882SAlex Richardson             ((testfunc2)(fn->func))(r, a, b, GMP_RNDN);
180331914882SAlex Richardson             get_mpfr_d(r, &result[0], &result[1], &result[2]);
180431914882SAlex Richardson             wrapper_result_real(&ctx, r, 2, result);
180531914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
180631914882SAlex Richardson                 get_mpfr_d(r, &result[0], &result[1], &result[2]);
180731914882SAlex Richardson             break;
180831914882SAlex Richardson           case args2f:
180931914882SAlex Richardson             set_mpfr_f(a, args[0]);
181031914882SAlex Richardson             wrapper_op_real(&ctx, a, 1, args);
181131914882SAlex Richardson             set_mpfr_f(b, args[2]);
181231914882SAlex Richardson             wrapper_op_real(&ctx, b, 1, args+2);
181331914882SAlex Richardson             ((testfunc2)(fn->func))(r, a, b, GMP_RNDN);
181431914882SAlex Richardson             get_mpfr_f(r, &result[0], &result[1]);
181531914882SAlex Richardson             wrapper_result_real(&ctx, r, 1, result);
181631914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
181731914882SAlex Richardson                 get_mpfr_f(r, &result[0], &result[1]);
181831914882SAlex Richardson             break;
181931914882SAlex Richardson           case rred:
182031914882SAlex Richardson             set_mpfr_d(a, args[0], args[1]);
182131914882SAlex Richardson             wrapper_op_real(&ctx, a, 2, args);
182231914882SAlex Richardson             ((testrred)(fn->func))(r, a, (int *)&result[3]);
182331914882SAlex Richardson             get_mpfr_d(r, &result[0], &result[1], &result[2]);
182431914882SAlex Richardson             wrapper_result_real(&ctx, r, 2, result);
182531914882SAlex Richardson             /* We never need to mess about with the integer auxiliary
182631914882SAlex Richardson              * output. */
182731914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
182831914882SAlex Richardson                 get_mpfr_d(r, &result[0], &result[1], &result[2]);
182931914882SAlex Richardson             break;
183031914882SAlex Richardson           case rredf:
183131914882SAlex Richardson             set_mpfr_f(a, args[0]);
183231914882SAlex Richardson             wrapper_op_real(&ctx, a, 1, args);
183331914882SAlex Richardson             ((testrred)(fn->func))(r, a, (int *)&result[3]);
183431914882SAlex Richardson             get_mpfr_f(r, &result[0], &result[1]);
183531914882SAlex Richardson             wrapper_result_real(&ctx, r, 1, result);
183631914882SAlex Richardson             /* We never need to mess about with the integer auxiliary
183731914882SAlex Richardson              * output. */
183831914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
183931914882SAlex Richardson                 get_mpfr_f(r, &result[0], &result[1]);
184031914882SAlex Richardson             break;
184131914882SAlex Richardson           case semi1:
184231914882SAlex Richardson           case semi1f:
184331914882SAlex Richardson             errstr = ((testsemi1)(fn->func))(args, result);
184431914882SAlex Richardson             break;
184531914882SAlex Richardson           case semi2:
184631914882SAlex Richardson           case compare:
184731914882SAlex Richardson             errstr = ((testsemi2)(fn->func))(args, args+2, result);
184831914882SAlex Richardson             break;
184931914882SAlex Richardson           case semi2f:
185031914882SAlex Richardson           case comparef:
185131914882SAlex Richardson           case t_ldexpf:
185231914882SAlex Richardson             errstr = ((testsemi2f)(fn->func))(args, args+2, result);
185331914882SAlex Richardson             break;
185431914882SAlex Richardson           case t_ldexp:
185531914882SAlex Richardson             errstr = ((testldexp)(fn->func))(args, args+2, result);
185631914882SAlex Richardson             break;
185731914882SAlex Richardson           case t_frexp:
185831914882SAlex Richardson             errstr = ((testfrexp)(fn->func))(args, result, result+2);
185931914882SAlex Richardson             break;
186031914882SAlex Richardson           case t_frexpf:
186131914882SAlex Richardson             errstr = ((testfrexp)(fn->func))(args, result, result+2);
186231914882SAlex Richardson             break;
186331914882SAlex Richardson           case t_modf:
186431914882SAlex Richardson             errstr = ((testmodf)(fn->func))(args, result, result+2);
186531914882SAlex Richardson             break;
186631914882SAlex Richardson           case t_modff:
186731914882SAlex Richardson             errstr = ((testmodf)(fn->func))(args, result, result+2);
186831914882SAlex Richardson             break;
186931914882SAlex Richardson           case classify:
187031914882SAlex Richardson             errstr = ((testclassify)(fn->func))(args, &result[0]);
187131914882SAlex Richardson             break;
187231914882SAlex Richardson           case classifyf:
187331914882SAlex Richardson             errstr = ((testclassifyf)(fn->func))(args, &result[0]);
187431914882SAlex Richardson             break;
187531914882SAlex Richardson           case args1c:
187631914882SAlex Richardson             set_mpc_d(ac, args[0], args[1], args[2], args[3]);
187731914882SAlex Richardson             wrapper_op_complex(&ctx, ac, 2, args);
187831914882SAlex Richardson             ((testfunc1c)(fn->func))(rc, ac, MPC_RNDNN);
187931914882SAlex Richardson             get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);
188031914882SAlex Richardson             wrapper_result_complex(&ctx, rc, 2, result);
188131914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
188231914882SAlex Richardson                 get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);
188331914882SAlex Richardson             break;
188431914882SAlex Richardson           case args2c:
188531914882SAlex Richardson             set_mpc_d(ac, args[0], args[1], args[2], args[3]);
188631914882SAlex Richardson             wrapper_op_complex(&ctx, ac, 2, args);
188731914882SAlex Richardson             set_mpc_d(bc, args[4], args[5], args[6], args[7]);
188831914882SAlex Richardson             wrapper_op_complex(&ctx, bc, 2, args+4);
188931914882SAlex Richardson             ((testfunc2c)(fn->func))(rc, ac, bc, MPC_RNDNN);
189031914882SAlex Richardson             get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);
189131914882SAlex Richardson             wrapper_result_complex(&ctx, rc, 2, result);
189231914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
189331914882SAlex Richardson                 get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);
189431914882SAlex Richardson             break;
189531914882SAlex Richardson           case args1fc:
189631914882SAlex Richardson             set_mpc_f(ac, args[0], args[2]);
189731914882SAlex Richardson             wrapper_op_complex(&ctx, ac, 1, args);
189831914882SAlex Richardson             ((testfunc1c)(fn->func))(rc, ac, MPC_RNDNN);
189931914882SAlex Richardson             get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);
190031914882SAlex Richardson             wrapper_result_complex(&ctx, rc, 1, result);
190131914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
190231914882SAlex Richardson                 get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);
190331914882SAlex Richardson             break;
190431914882SAlex Richardson           case args2fc:
190531914882SAlex Richardson             set_mpc_f(ac, args[0], args[2]);
190631914882SAlex Richardson             wrapper_op_complex(&ctx, ac, 1, args);
190731914882SAlex Richardson             set_mpc_f(bc, args[4], args[6]);
190831914882SAlex Richardson             wrapper_op_complex(&ctx, bc, 1, args+4);
190931914882SAlex Richardson             ((testfunc2c)(fn->func))(rc, ac, bc, MPC_RNDNN);
191031914882SAlex Richardson             get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);
191131914882SAlex Richardson             wrapper_result_complex(&ctx, rc, 1, result);
191231914882SAlex Richardson             if (wrapper_run(&ctx, fn->wrappers))
191331914882SAlex Richardson                 get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);
191431914882SAlex Richardson             break;
191531914882SAlex Richardson           default:
191631914882SAlex Richardson             fprintf(stderr, "internal inconsistency?!\n");
191731914882SAlex Richardson             abort();
191831914882SAlex Richardson         }
191931914882SAlex Richardson     }
192031914882SAlex Richardson 
192131914882SAlex Richardson     switch (fn->type) {
192231914882SAlex Richardson       case args1:              /* return an extra-precise result */
192331914882SAlex Richardson       case args2:
192431914882SAlex Richardson       case args1cr:
192531914882SAlex Richardson       case rred:
192631914882SAlex Richardson         printextra = 1;
192731914882SAlex Richardson         if (rejected == 0) {
192831914882SAlex Richardson             errstr = NULL;
192931914882SAlex Richardson             if (!mpfr_zero_p(a)) {
193031914882SAlex Richardson                 if ((result[0] & 0x7FFFFFFF) == 0 && result[1] == 0) {
193131914882SAlex Richardson                     /*
193231914882SAlex Richardson                      * If the output is +0 or -0 apart from the extra
193331914882SAlex Richardson                      * precision in result[2], then there's a tricky
193431914882SAlex Richardson                      * judgment call about what we require in the
193531914882SAlex Richardson                      * output. If we output the extra bits and set
193631914882SAlex Richardson                      * errstr="?underflow" then mathtest will tolerate
193731914882SAlex Richardson                      * the function under test rounding down to zero
193831914882SAlex Richardson                      * _or_ up to the minimum denormal; whereas if we
193931914882SAlex Richardson                      * suppress the extra bits and set
194031914882SAlex Richardson                      * errstr="underflow", then mathtest will enforce
194131914882SAlex Richardson                      * that the function really does underflow to zero.
194231914882SAlex Richardson                      *
194331914882SAlex Richardson                      * But where to draw the line? It seems clear to
194431914882SAlex Richardson                      * me that numbers along the lines of
194531914882SAlex Richardson                      * 00000000.00000000.7ff should be treated
194631914882SAlex Richardson                      * similarly to 00000000.00000000.801, but on the
194731914882SAlex Richardson                      * other hand, we must surely be prepared to
194831914882SAlex Richardson                      * enforce a genuine underflow-to-zero in _some_
194931914882SAlex Richardson                      * case where the true mathematical output is
195031914882SAlex Richardson                      * nonzero but absurdly tiny.
195131914882SAlex Richardson                      *
195231914882SAlex Richardson                      * I think a reasonable place to draw the
195331914882SAlex Richardson                      * distinction is at 00000000.00000000.400, i.e.
195431914882SAlex Richardson                      * one quarter of the minimum positive denormal.
195531914882SAlex Richardson                      * If a value less than that rounds up to the
195631914882SAlex Richardson                      * minimum denormal, that must mean the function
195731914882SAlex Richardson                      * under test has managed to make an error of an
195831914882SAlex Richardson                      * entire factor of two, and that's something we
195931914882SAlex Richardson                      * should fix. Above that, you can misround within
196031914882SAlex Richardson                      * the limits of your accuracy bound if you have
196131914882SAlex Richardson                      * to.
196231914882SAlex Richardson                      */
196331914882SAlex Richardson                     if (result[2] < 0x40000000) {
196431914882SAlex Richardson                         /* Total underflow (ERANGE + UFL) is required,
196531914882SAlex Richardson                          * and we suppress the extra bits to make
196631914882SAlex Richardson                          * mathtest enforce that the output is really
196731914882SAlex Richardson                          * zero. */
196831914882SAlex Richardson                         errstr = "underflow";
196931914882SAlex Richardson                         printextra = 0;
197031914882SAlex Richardson                     } else {
197131914882SAlex Richardson                         /* Total underflow is not required, but if the
197231914882SAlex Richardson                          * function rounds down to zero anyway, then
197331914882SAlex Richardson                          * we should be prepared to tolerate it. */
197431914882SAlex Richardson                         errstr = "?underflow";
197531914882SAlex Richardson                     }
197631914882SAlex Richardson                 } else if (!(result[0] & 0x7ff00000)) {
197731914882SAlex Richardson                     /*
197831914882SAlex Richardson                      * If the output is denormal, we usually expect a
197931914882SAlex Richardson                      * UFL exception, warning the user of partial
198031914882SAlex Richardson                      * underflow. The exception is if the denormal
198131914882SAlex Richardson                      * being returned is just one of the input values,
198231914882SAlex Richardson                      * unchanged even in principle. I bodgily handle
198331914882SAlex Richardson                      * this by just special-casing the functions in
198431914882SAlex Richardson                      * question below.
198531914882SAlex Richardson                      */
198631914882SAlex Richardson                     if (!strcmp(fn->name, "fmax") ||
198731914882SAlex Richardson                         !strcmp(fn->name, "fmin") ||
198831914882SAlex Richardson                         !strcmp(fn->name, "creal") ||
198931914882SAlex Richardson                         !strcmp(fn->name, "cimag")) {
199031914882SAlex Richardson                         /* no error expected */
199131914882SAlex Richardson                     } else {
199231914882SAlex Richardson                         errstr = "u";
199331914882SAlex Richardson                     }
199431914882SAlex Richardson                 } else if ((result[0] & 0x7FFFFFFF) > 0x7FEFFFFF) {
199531914882SAlex Richardson                     /*
199631914882SAlex Richardson                      * Infinite results are usually due to overflow,
199731914882SAlex Richardson                      * but one exception is lgamma of a negative
199831914882SAlex Richardson                      * integer.
199931914882SAlex Richardson                      */
200031914882SAlex Richardson                     if (!strcmp(fn->name, "lgamma") &&
200131914882SAlex Richardson                         (args[0] & 0x80000000) != 0 && /* negative */
200231914882SAlex Richardson                         is_dinteger(args)) {
200331914882SAlex Richardson                         errstr = "ERANGE status=z";
200431914882SAlex Richardson                     } else {
200531914882SAlex Richardson                         errstr = "overflow";
200631914882SAlex Richardson                     }
200731914882SAlex Richardson                     printextra = 0;
200831914882SAlex Richardson                 }
200931914882SAlex Richardson             } else {
201031914882SAlex Richardson                 /* lgamma(0) is also a pole. */
201131914882SAlex Richardson                 if (!strcmp(fn->name, "lgamma")) {
201231914882SAlex Richardson                     errstr = "ERANGE status=z";
201331914882SAlex Richardson                     printextra = 0;
201431914882SAlex Richardson                 }
201531914882SAlex Richardson             }
201631914882SAlex Richardson         }
201731914882SAlex Richardson 
201831914882SAlex Richardson         if (!printextra || (rejected && !(rejected==1 && result[2]!=0))) {
201931914882SAlex Richardson             printf(" result=%08x.%08x",
202031914882SAlex Richardson                    result[0], result[1]);
202131914882SAlex Richardson         } else {
202231914882SAlex Richardson             printf(" result=%08x.%08x.%03x",
202331914882SAlex Richardson                    result[0], result[1], (result[2] >> 20) & 0xFFF);
202431914882SAlex Richardson         }
202531914882SAlex Richardson         if (fn->type == rred) {
202631914882SAlex Richardson             printf(" res2=%08x", result[3]);
202731914882SAlex Richardson         }
202831914882SAlex Richardson         break;
202931914882SAlex Richardson       case args1f:
203031914882SAlex Richardson       case args2f:
203131914882SAlex Richardson       case args1fcr:
203231914882SAlex Richardson       case rredf:
203331914882SAlex Richardson         printextra = 1;
203431914882SAlex Richardson         if (rejected == 0) {
203531914882SAlex Richardson             errstr = NULL;
203631914882SAlex Richardson             if (!mpfr_zero_p(a)) {
203731914882SAlex Richardson                 if ((result[0] & 0x7FFFFFFF) == 0) {
203831914882SAlex Richardson                     /*
203931914882SAlex Richardson                      * Decide whether to print the extra bits based on
204031914882SAlex Richardson                      * just how close to zero the number is. See the
204131914882SAlex Richardson                      * big comment in the double-precision case for
204231914882SAlex Richardson                      * discussion.
204331914882SAlex Richardson                      */
204431914882SAlex Richardson                     if (result[1] < 0x40000000) {
204531914882SAlex Richardson                         errstr = "underflow";
204631914882SAlex Richardson                         printextra = 0;
204731914882SAlex Richardson                     } else {
204831914882SAlex Richardson                         errstr = "?underflow";
204931914882SAlex Richardson                     }
205031914882SAlex Richardson                 } else if (!(result[0] & 0x7f800000)) {
205131914882SAlex Richardson                     /*
205231914882SAlex Richardson                      * Functions which do not report partial overflow
205331914882SAlex Richardson                      * are listed here as special cases. (See the
205431914882SAlex Richardson                      * corresponding double case above for a fuller
205531914882SAlex Richardson                      * comment.)
205631914882SAlex Richardson                      */
205731914882SAlex Richardson                     if (!strcmp(fn->name, "fmaxf") ||
205831914882SAlex Richardson                         !strcmp(fn->name, "fminf") ||
205931914882SAlex Richardson                         !strcmp(fn->name, "crealf") ||
206031914882SAlex Richardson                         !strcmp(fn->name, "cimagf")) {
206131914882SAlex Richardson                         /* no error expected */
206231914882SAlex Richardson                     } else {
206331914882SAlex Richardson                         errstr = "u";
206431914882SAlex Richardson                     }
206531914882SAlex Richardson                 } else if ((result[0] & 0x7FFFFFFF) > 0x7F7FFFFF) {
206631914882SAlex Richardson                     /*
206731914882SAlex Richardson                      * Infinite results are usually due to overflow,
206831914882SAlex Richardson                      * but one exception is lgamma of a negative
206931914882SAlex Richardson                      * integer.
207031914882SAlex Richardson                      */
207131914882SAlex Richardson                     if (!strcmp(fn->name, "lgammaf") &&
207231914882SAlex Richardson                         (args[0] & 0x80000000) != 0 && /* negative */
207331914882SAlex Richardson                         is_sinteger(args)) {
207431914882SAlex Richardson                         errstr = "ERANGE status=z";
207531914882SAlex Richardson                     } else {
207631914882SAlex Richardson                         errstr = "overflow";
207731914882SAlex Richardson                     }
207831914882SAlex Richardson                     printextra = 0;
207931914882SAlex Richardson                 }
208031914882SAlex Richardson             } else {
208131914882SAlex Richardson                 /* lgamma(0) is also a pole. */
208231914882SAlex Richardson                 if (!strcmp(fn->name, "lgammaf")) {
208331914882SAlex Richardson                     errstr = "ERANGE status=z";
208431914882SAlex Richardson                     printextra = 0;
208531914882SAlex Richardson                 }
208631914882SAlex Richardson             }
208731914882SAlex Richardson         }
208831914882SAlex Richardson 
208931914882SAlex Richardson         if (!printextra || (rejected && !(rejected==1 && result[1]!=0))) {
209031914882SAlex Richardson             printf(" result=%08x",
209131914882SAlex Richardson                    result[0]);
209231914882SAlex Richardson         } else {
209331914882SAlex Richardson             printf(" result=%08x.%03x",
209431914882SAlex Richardson                    result[0], (result[1] >> 20) & 0xFFF);
209531914882SAlex Richardson         }
209631914882SAlex Richardson         if (fn->type == rredf) {
209731914882SAlex Richardson             printf(" res2=%08x", result[3]);
209831914882SAlex Richardson         }
209931914882SAlex Richardson         break;
210031914882SAlex Richardson       case semi1:              /* return a double result */
210131914882SAlex Richardson       case semi2:
210231914882SAlex Richardson       case t_ldexp:
210331914882SAlex Richardson         printf(" result=%08x.%08x", result[0], result[1]);
210431914882SAlex Richardson         break;
210531914882SAlex Richardson       case semi1f:
210631914882SAlex Richardson       case semi2f:
210731914882SAlex Richardson       case t_ldexpf:
210831914882SAlex Richardson         printf(" result=%08x", result[0]);
210931914882SAlex Richardson         break;
211031914882SAlex Richardson       case t_frexp:            /* return double * int */
211131914882SAlex Richardson         printf(" result=%08x.%08x res2=%08x", result[0], result[1],
211231914882SAlex Richardson                result[2]);
211331914882SAlex Richardson         break;
211431914882SAlex Richardson       case t_modf:             /* return double * double */
211531914882SAlex Richardson         printf(" result=%08x.%08x res2=%08x.%08x",
211631914882SAlex Richardson                result[0], result[1], result[2], result[3]);
211731914882SAlex Richardson         break;
211831914882SAlex Richardson       case t_modff:                    /* return float * float */
211931914882SAlex Richardson         /* fall through */
212031914882SAlex Richardson       case t_frexpf:                   /* return float * int */
212131914882SAlex Richardson         printf(" result=%08x res2=%08x", result[0], result[2]);
212231914882SAlex Richardson         break;
212331914882SAlex Richardson       case classify:
212431914882SAlex Richardson       case classifyf:
212531914882SAlex Richardson       case compare:
212631914882SAlex Richardson       case comparef:
212731914882SAlex Richardson         printf(" result=%x", result[0]);
212831914882SAlex Richardson         break;
212931914882SAlex Richardson       case args1c:
213031914882SAlex Richardson       case args2c:
213131914882SAlex Richardson         if (0/* errstr */) {
213231914882SAlex Richardson             printf(" resultr=%08x.%08x", result[0], result[1]);
213331914882SAlex Richardson             printf(" resulti=%08x.%08x", result[4], result[5]);
213431914882SAlex Richardson         } else {
213531914882SAlex Richardson             printf(" resultr=%08x.%08x.%03x",
213631914882SAlex Richardson                    result[0], result[1], (result[2] >> 20) & 0xFFF);
213731914882SAlex Richardson             printf(" resulti=%08x.%08x.%03x",
213831914882SAlex Richardson                    result[4], result[5], (result[6] >> 20) & 0xFFF);
213931914882SAlex Richardson         }
214031914882SAlex Richardson         /* Underflow behaviour doesn't seem to be specified for complex arithmetic */
214131914882SAlex Richardson         errstr = "?underflow";
214231914882SAlex Richardson         break;
214331914882SAlex Richardson       case args1fc:
214431914882SAlex Richardson       case args2fc:
214531914882SAlex Richardson         if (0/* errstr */) {
214631914882SAlex Richardson             printf(" resultr=%08x", result[0]);
214731914882SAlex Richardson             printf(" resulti=%08x", result[4]);
214831914882SAlex Richardson         } else {
214931914882SAlex Richardson             printf(" resultr=%08x.%03x",
215031914882SAlex Richardson                    result[0], (result[1] >> 20) & 0xFFF);
215131914882SAlex Richardson             printf(" resulti=%08x.%03x",
215231914882SAlex Richardson                    result[4], (result[5] >> 20) & 0xFFF);
215331914882SAlex Richardson         }
215431914882SAlex Richardson         /* Underflow behaviour doesn't seem to be specified for complex arithmetic */
215531914882SAlex Richardson         errstr = "?underflow";
215631914882SAlex Richardson         break;
215731914882SAlex Richardson     }
215831914882SAlex Richardson 
215931914882SAlex Richardson     if (errstr && *(errstr+1) == '\0') {
216031914882SAlex Richardson         printf(" errno=0 status=%c",*errstr);
216131914882SAlex Richardson     } else if (errstr && *errstr == '?') {
216231914882SAlex Richardson         printf(" maybeerror=%s", errstr+1);
216331914882SAlex Richardson     } else if (errstr && errstr[0] == 'E') {
216431914882SAlex Richardson         printf(" errno=%s", errstr);
216531914882SAlex Richardson     } else {
216631914882SAlex Richardson         printf(" error=%s", errstr && *errstr ? errstr : "0");
216731914882SAlex Richardson     }
216831914882SAlex Richardson 
216931914882SAlex Richardson     printf("\n");
217031914882SAlex Richardson 
217131914882SAlex Richardson     vet_for_decline(fn, args, result, 0);
217231914882SAlex Richardson 
217331914882SAlex Richardson   cleanup:
217431914882SAlex Richardson     mpfr_clear(a);
217531914882SAlex Richardson     mpfr_clear(b);
217631914882SAlex Richardson     mpfr_clear(r);
217731914882SAlex Richardson     mpc_clear(ac);
217831914882SAlex Richardson     mpc_clear(bc);
217931914882SAlex Richardson     mpc_clear(rc);
218031914882SAlex Richardson }
218131914882SAlex Richardson 
218231914882SAlex Richardson void gencases(Testable *fn, int number) {
218331914882SAlex Richardson     int i;
218431914882SAlex Richardson     uint32 args[8];
218531914882SAlex Richardson 
218631914882SAlex Richardson     float32_case(NULL);
218731914882SAlex Richardson     float64_case(NULL);
218831914882SAlex Richardson 
218931914882SAlex Richardson     printf("random=on\n"); /* signal to runtests.pl that the following tests are randomly generated */
219031914882SAlex Richardson     for (i = 0; i < number; i++) {
219131914882SAlex Richardson         /* generate test point */
219231914882SAlex Richardson         fn->cases(args, fn->caseparam1, fn->caseparam2);
219331914882SAlex Richardson         docase(fn, args);
219431914882SAlex Richardson     }
219531914882SAlex Richardson     printf("random=off\n");
219631914882SAlex Richardson }
219731914882SAlex Richardson 
219831914882SAlex Richardson static uint32 doubletop(int x, int scale) {
219931914882SAlex Richardson     int e = 0x412 + scale;
220031914882SAlex Richardson     while (!(x & 0x100000))
220131914882SAlex Richardson         x <<= 1, e--;
220231914882SAlex Richardson     return (e << 20) + x;
220331914882SAlex Richardson }
220431914882SAlex Richardson 
220531914882SAlex Richardson static uint32 floatval(int x, int scale) {
220631914882SAlex Richardson     int e = 0x95 + scale;
220731914882SAlex Richardson     while (!(x & 0x800000))
220831914882SAlex Richardson         x <<= 1, e--;
220931914882SAlex Richardson     return (e << 23) + x;
221031914882SAlex Richardson }
2211