Path: blob/main/contrib/arm-optimized-routines/math/test/rtest/dotest.c
48375 views
/*1* dotest.c - actually generate mathlib test cases2*3* Copyright (c) 1999-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include <stdio.h>8#include <string.h>9#include <stdlib.h>10#include <stdint.h>11#include <assert.h>12#include <limits.h>1314#include "semi.h"15#include "intern.h"16#include "random.h"1718#define MPFR_PREC 96 /* good enough for float or double + a few extra bits */1920#if MPFR_VERSION < MPFR_VERSION_NUM(4, 2, 0)21int22mpfr_tanpi (mpfr_t ret, const mpfr_t arg, mpfr_rnd_t rnd)23{24MPFR_DECL_INIT (frd, MPFR_PREC);25mpfr_const_pi (frd, GMP_RNDN);26mpfr_mul (frd, frd, arg, GMP_RNDN);27return mpfr_tan (ret, frd, GMP_RNDN);28}2930int31mpfr_sinpi (mpfr_t ret, const mpfr_t arg, mpfr_rnd_t rnd)32{33MPFR_DECL_INIT (frd, MPFR_PREC);34mpfr_const_pi (frd, GMP_RNDN);35mpfr_mul (frd, frd, arg, GMP_RNDN);36return mpfr_sin (ret, frd, GMP_RNDN);37}3839int40mpfr_cospi (mpfr_t ret, const mpfr_t arg, mpfr_rnd_t rnd)41{42MPFR_DECL_INIT (frd, MPFR_PREC);43mpfr_const_pi (frd, GMP_RNDN);44mpfr_mul (frd, frd, arg, GMP_RNDN);45return mpfr_cos (ret, frd, GMP_RNDN);46}47#endif4849extern int lib_fo, lib_no_arith, ntests;5051/*52* Prototypes.53*/54static void cases_biased(uint32 *, uint32, uint32);55static void cases_biased_positive(uint32 *, uint32, uint32);56static void cases_biased_float(uint32 *, uint32, uint32);57static void cases_uniform(uint32 *, uint32, uint32);58static void cases_uniform_positive(uint32 *, uint32, uint32);59static void cases_uniform_float(uint32 *, uint32, uint32);60static void cases_uniform_float_positive(uint32 *, uint32, uint32);61static void log_cases(uint32 *, uint32, uint32);62static void log_cases_float(uint32 *, uint32, uint32);63static void log1p_cases(uint32 *, uint32, uint32);64static void log1p_cases_float(uint32 *, uint32, uint32);65static void minmax_cases(uint32 *, uint32, uint32);66static void minmax_cases_float(uint32 *, uint32, uint32);67static void atan2_cases(uint32 *, uint32, uint32);68static void atan2_cases_float(uint32 *, uint32, uint32);69static void pow_cases(uint32 *, uint32, uint32);70static void pow_cases_float(uint32 *, uint32, uint32);71static void rred_cases(uint32 *, uint32, uint32);72static void rred_cases_float(uint32 *, uint32, uint32);73static void cases_semi1(uint32 *, uint32, uint32);74static void cases_semi1_float(uint32 *, uint32, uint32);75static void cases_semi2(uint32 *, uint32, uint32);76static void cases_semi2_float(uint32 *, uint32, uint32);77static void cases_ldexp(uint32 *, uint32, uint32);78static void cases_ldexp_float(uint32 *, uint32, uint32);7980static void complex_cases_uniform(uint32 *, uint32, uint32);81static void complex_cases_uniform_float(uint32 *, uint32, uint32);82static void complex_cases_biased(uint32 *, uint32, uint32);83static void complex_cases_biased_float(uint32 *, uint32, uint32);84static void complex_log_cases(uint32 *, uint32, uint32);85static void complex_log_cases_float(uint32 *, uint32, uint32);86static void complex_pow_cases(uint32 *, uint32, uint32);87static void complex_pow_cases_float(uint32 *, uint32, uint32);88static void complex_arithmetic_cases(uint32 *, uint32, uint32);89static void complex_arithmetic_cases_float(uint32 *, uint32, uint32);9091static uint32 doubletop(int x, int scale);92static uint32 floatval(int x, int scale);9394/*95* Convert back and forth between IEEE bit patterns and the96* mpfr_t/mpc_t types.97*/98static void set_mpfr_d(mpfr_t x, uint32 h, uint32 l)99{100uint64_t hl = ((uint64_t)h << 32) | l;101uint32 exp = (hl >> 52) & 0x7ff;102int64_t mantissa = hl & (((uint64_t)1 << 52) - 1);103int sign = (hl >> 63) ? -1 : +1;104if (exp == 0x7ff) {105if (mantissa == 0)106mpfr_set_inf(x, sign);107else108mpfr_set_nan(x);109} else if (exp == 0 && mantissa == 0) {110mpfr_set_ui(x, 0, GMP_RNDN);111mpfr_setsign(x, x, sign < 0, GMP_RNDN);112} else {113if (exp != 0)114mantissa |= ((uint64_t)1 << 52);115else116exp++;117mpfr_set_sj_2exp(x, mantissa * sign, (int)exp - 0x3ff - 52, GMP_RNDN);118}119}120static void set_mpfr_f(mpfr_t x, uint32 f)121{122uint32 exp = (f >> 23) & 0xff;123int32 mantissa = f & ((1 << 23) - 1);124int sign = (f >> 31) ? -1 : +1;125if (exp == 0xff) {126if (mantissa == 0)127mpfr_set_inf(x, sign);128else129mpfr_set_nan(x);130} else if (exp == 0 && mantissa == 0) {131mpfr_set_ui(x, 0, GMP_RNDN);132mpfr_setsign(x, x, sign < 0, GMP_RNDN);133} else {134if (exp != 0)135mantissa |= (1 << 23);136else137exp++;138mpfr_set_sj_2exp(x, mantissa * sign, (int)exp - 0x7f - 23, GMP_RNDN);139}140}141static void set_mpc_d(mpc_t z, uint32 rh, uint32 rl, uint32 ih, uint32 il)142{143mpfr_t x, y;144mpfr_init2(x, MPFR_PREC);145mpfr_init2(y, MPFR_PREC);146set_mpfr_d(x, rh, rl);147set_mpfr_d(y, ih, il);148mpc_set_fr_fr(z, x, y, MPC_RNDNN);149mpfr_clear(x);150mpfr_clear(y);151}152static void set_mpc_f(mpc_t z, uint32 r, uint32 i)153{154mpfr_t x, y;155mpfr_init2(x, MPFR_PREC);156mpfr_init2(y, MPFR_PREC);157set_mpfr_f(x, r);158set_mpfr_f(y, i);159mpc_set_fr_fr(z, x, y, MPC_RNDNN);160mpfr_clear(x);161mpfr_clear(y);162}163static void get_mpfr_d(const mpfr_t x, uint32 *h, uint32 *l, uint32 *extra)164{165uint32_t sign, expfield, mantfield;166mpfr_t significand;167int exp;168169if (mpfr_nan_p(x)) {170*h = 0x7ff80000;171*l = 0;172*extra = 0;173return;174}175176sign = mpfr_signbit(x) ? 0x80000000U : 0;177178if (mpfr_inf_p(x)) {179*h = 0x7ff00000 | sign;180*l = 0;181*extra = 0;182return;183}184185if (mpfr_zero_p(x)) {186*h = 0x00000000 | sign;187*l = 0;188*extra = 0;189return;190}191192mpfr_init2(significand, MPFR_PREC);193mpfr_set(significand, x, GMP_RNDN);194exp = mpfr_get_exp(significand);195mpfr_set_exp(significand, 0);196197/* Now significand is in [1/2,1), and significand * 2^exp == x.198* So the IEEE exponent corresponding to exp==0 is 0x3fe. */199if (exp > 0x400) {200/* overflow to infinity anyway */201*h = 0x7ff00000 | sign;202*l = 0;203*extra = 0;204mpfr_clear(significand);205return;206}207208if (exp <= -0x3fe || mpfr_zero_p(x))209exp = -0x3fd; /* denormalise */210expfield = exp + 0x3fd; /* offset to cancel leading mantissa bit */211212mpfr_div_2si(significand, x, exp - 21, GMP_RNDN);213mpfr_abs(significand, significand, GMP_RNDN);214mantfield = mpfr_get_ui(significand, GMP_RNDZ);215*h = sign + ((uint64_t)expfield << 20) + mantfield;216mpfr_sub_ui(significand, significand, mantfield, GMP_RNDN);217mpfr_mul_2ui(significand, significand, 32, GMP_RNDN);218mantfield = mpfr_get_ui(significand, GMP_RNDZ);219*l = mantfield;220mpfr_sub_ui(significand, significand, mantfield, GMP_RNDN);221mpfr_mul_2ui(significand, significand, 32, GMP_RNDN);222mantfield = mpfr_get_ui(significand, GMP_RNDZ);223*extra = mantfield;224225mpfr_clear(significand);226}227static void get_mpfr_f(const mpfr_t x, uint32 *f, uint32 *extra)228{229uint32_t sign, expfield, mantfield;230mpfr_t significand;231int exp;232233if (mpfr_nan_p(x)) {234*f = 0x7fc00000;235*extra = 0;236return;237}238239sign = mpfr_signbit(x) ? 0x80000000U : 0;240241if (mpfr_inf_p(x)) {242*f = 0x7f800000 | sign;243*extra = 0;244return;245}246247if (mpfr_zero_p(x)) {248*f = 0x00000000 | sign;249*extra = 0;250return;251}252253mpfr_init2(significand, MPFR_PREC);254mpfr_set(significand, x, GMP_RNDN);255exp = mpfr_get_exp(significand);256mpfr_set_exp(significand, 0);257258/* Now significand is in [1/2,1), and significand * 2^exp == x.259* So the IEEE exponent corresponding to exp==0 is 0x7e. */260if (exp > 0x80) {261/* overflow to infinity anyway */262*f = 0x7f800000 | sign;263*extra = 0;264mpfr_clear(significand);265return;266}267268if (exp <= -0x7e || mpfr_zero_p(x))269exp = -0x7d; /* denormalise */270expfield = exp + 0x7d; /* offset to cancel leading mantissa bit */271272mpfr_div_2si(significand, x, exp - 24, GMP_RNDN);273mpfr_abs(significand, significand, GMP_RNDN);274mantfield = mpfr_get_ui(significand, GMP_RNDZ);275*f = sign + ((uint64_t)expfield << 23) + mantfield;276mpfr_sub_ui(significand, significand, mantfield, GMP_RNDN);277mpfr_mul_2ui(significand, significand, 32, GMP_RNDN);278mantfield = mpfr_get_ui(significand, GMP_RNDZ);279*extra = mantfield;280281mpfr_clear(significand);282}283static void get_mpc_d(const mpc_t z,284uint32 *rh, uint32 *rl, uint32 *rextra,285uint32 *ih, uint32 *il, uint32 *iextra)286{287mpfr_t x, y;288mpfr_init2(x, MPFR_PREC);289mpfr_init2(y, MPFR_PREC);290mpc_real(x, z, GMP_RNDN);291mpc_imag(y, z, GMP_RNDN);292get_mpfr_d(x, rh, rl, rextra);293get_mpfr_d(y, ih, il, iextra);294mpfr_clear(x);295mpfr_clear(y);296}297static void get_mpc_f(const mpc_t z,298uint32 *r, uint32 *rextra,299uint32 *i, uint32 *iextra)300{301mpfr_t x, y;302mpfr_init2(x, MPFR_PREC);303mpfr_init2(y, MPFR_PREC);304mpc_real(x, z, GMP_RNDN);305mpc_imag(y, z, GMP_RNDN);306get_mpfr_f(x, r, rextra);307get_mpfr_f(y, i, iextra);308mpfr_clear(x);309mpfr_clear(y);310}311312/*313* Implementation of mathlib functions that aren't trivially314* implementable using an existing mpfr or mpc function.315*/316int test_rred(mpfr_t ret, const mpfr_t x, int *quadrant)317{318mpfr_t halfpi;319long quo;320int status;321322/*323* In the worst case of range reduction, we get an input of size324* around 2^1024, and must find its remainder mod pi, which means325* we need 1024 bits of pi at least. Plus, the remainder might326* happen to come out very very small if we're unlucky. How327* unlucky can we be? Well, conveniently, I once went through and328* actually worked that out using Paxson's modular minimisation329* algorithm, and it turns out that the smallest exponent you can330* get out of a nontrivial[1] double precision range reduction is331* 0x3c2, i.e. of the order of 2^-61. So we need 1024 bits of pi332* to get us down to the units digit, another 61 or so bits (say333* 64) to get down to the highest set bit of the output, and then334* some bits to make the actual mantissa big enough.335*336* [1] of course the output of range reduction can have an337* arbitrarily small exponent in the trivial case, where the338* input is so small that it's the identity function. That339* doesn't count.340*/341mpfr_init2(halfpi, MPFR_PREC + 1024 + 64);342mpfr_const_pi(halfpi, GMP_RNDN);343mpfr_div_ui(halfpi, halfpi, 2, GMP_RNDN);344345status = mpfr_remquo(ret, &quo, x, halfpi, GMP_RNDN);346*quadrant = quo & 3;347348mpfr_clear(halfpi);349350return status;351}352int test_lgamma(mpfr_t ret, const mpfr_t x, mpfr_rnd_t rnd)353{354/*355* mpfr_lgamma takes an extra int * parameter to hold the output356* sign. We don't bother testing that, so this wrapper throws away357* the sign and hence fits into the same function prototype as all358* the other real->real mpfr functions.359*360* There is also mpfr_lngamma which has no sign output and hence361* has the right prototype already, but unfortunately it returns362* NaN in cases where gamma(x) < 0, so it's no use to us.363*/364int sign;365return mpfr_lgamma(ret, &sign, x, rnd);366}367int test_cpow(mpc_t ret, const mpc_t x, const mpc_t y, mpc_rnd_t rnd)368{369/*370* For complex pow, we must bump up the precision by a huge amount371* if we want it to get the really difficult cases right. (Not372* that we expect the library under test to be getting those cases373* right itself, but we'd at least like the test suite to report374* them as wrong for the _right reason_.)375*376* This works around a bug in mpc_pow(), fixed by r1455 in the MPC377* svn repository (2014-10-14) and expected to be in any MPC378* release after 1.0.2 (which was the latest release already made379* at the time of the fix). So as and when we update to an MPC380* with the fix in it, we could remove this workaround.381*382* For the reasons for choosing this amount of extra precision,383* see analysis in complex/cpownotes.txt for the rationale for the384* amount.385*/386mpc_t xbig, ybig, retbig;387int status;388389mpc_init2(xbig, 1034 + 53 + 60 + MPFR_PREC);390mpc_init2(ybig, 1034 + 53 + 60 + MPFR_PREC);391mpc_init2(retbig, 1034 + 53 + 60 + MPFR_PREC);392393mpc_set(xbig, x, MPC_RNDNN);394mpc_set(ybig, y, MPC_RNDNN);395status = mpc_pow(retbig, xbig, ybig, rnd);396mpc_set(ret, retbig, rnd);397398mpc_clear(xbig);399mpc_clear(ybig);400mpc_clear(retbig);401402return status;403}404405/*406* Identify 'hard' values (NaN, Inf, nonzero denormal) for deciding407* whether microlib will decline to run a test.408*/409#define is_shard(in) ( \410(((in)[0] & 0x7F800000) == 0x7F800000 || \411(((in)[0] & 0x7F800000) == 0 && ((in)[0]&0x7FFFFFFF) != 0)))412413#define is_dhard(in) ( \414(((in)[0] & 0x7FF00000) == 0x7FF00000 || \415(((in)[0] & 0x7FF00000) == 0 && (((in)[0] & 0xFFFFF) | (in)[1]) != 0)))416417/*418* Identify integers.419*/420int is_dinteger(uint32 *in)421{422uint32 out[3];423if ((0x7FF00000 & ~in[0]) == 0)424return 0; /* not finite, hence not integer */425test_ceil(in, out);426return in[0] == out[0] && in[1] == out[1];427}428int is_sinteger(uint32 *in)429{430uint32 out[3];431if ((0x7F800000 & ~in[0]) == 0)432return 0; /* not finite, hence not integer */433test_ceilf(in, out);434return in[0] == out[0];435}436437/*438* Identify signalling NaNs.439*/440int is_dsnan(const uint32 *in)441{442if ((in[0] & 0x7FF00000) != 0x7FF00000)443return 0; /* not the inf/nan exponent */444if ((in[0] << 12) == 0 && in[1] == 0)445return 0; /* inf */446if (in[0] & 0x00080000)447return 0; /* qnan */448return 1;449}450int is_ssnan(const uint32 *in)451{452if ((in[0] & 0x7F800000) != 0x7F800000)453return 0; /* not the inf/nan exponent */454if ((in[0] << 9) == 0)455return 0; /* inf */456if (in[0] & 0x00400000)457return 0; /* qnan */458return 1;459}460int is_snan(const uint32 *in, int size)461{462return size == 2 ? is_dsnan(in) : is_ssnan(in);463}464465/*466* Wrapper functions called to fix up unusual results after the main467* test function has run.468*/469void universal_wrapper(wrapperctx *ctx)470{471/*472* Any SNaN input gives rise to a QNaN output.473*/474int op;475for (op = 0; op < wrapper_get_nops(ctx); op++) {476int size = wrapper_get_size(ctx, op);477478if (!wrapper_is_complex(ctx, op) &&479is_snan(wrapper_get_ieee(ctx, op), size)) {480wrapper_set_nan(ctx);481}482}483}484485/* clang-format off */486Testable functions[] = {487/*488* Trig functions: sin, cos, tan. We test the core function489* between -16 and +16: we assume that range reduction exists490* and will be used for larger arguments, and we'll test that491* separately. Also we only go down to 2^-27 in magnitude,492* because below that sin(x)=tan(x)=x and cos(x)=1 as far as493* double precision can tell, which is boring.494*/495{"sin", (funcptr)mpfr_sin, args1, {NULL},496cases_uniform, 0x3e400000, 0x40300000},497{"sinf", (funcptr)mpfr_sin, args1f, {NULL},498cases_uniform_float, 0x39800000, 0x41800000},499{"cos", (funcptr)mpfr_cos, args1, {NULL},500cases_uniform, 0x3e400000, 0x40300000},501{"cosf", (funcptr)mpfr_cos, args1f, {NULL},502cases_uniform_float, 0x39800000, 0x41800000},503{"tan", (funcptr)mpfr_tan, args1, {NULL},504cases_uniform, 0x3e400000, 0x40300000},505{"tanf", (funcptr)mpfr_tan, args1f, {NULL},506cases_uniform_float, 0x39800000, 0x41800000},507{"sincosf_sinf", (funcptr)mpfr_sin, args1f, {NULL},508cases_uniform_float, 0x39800000, 0x41800000},509{"sincosf_cosf", (funcptr)mpfr_cos, args1f, {NULL},510cases_uniform_float, 0x39800000, 0x41800000},511{"sinpi", (funcptr)mpfr_sinpi, args1, {NULL},512cases_uniform, 0x3e400000, 0x40300000},513{"sinpif", (funcptr)mpfr_sinpi, args1f, {NULL},514cases_uniform_float, 0x39800000, 0x41800000},515{"cospi", (funcptr)mpfr_cospi, args1, {NULL},516cases_uniform, 0x3e400000, 0x40300000},517{"cospif", (funcptr)mpfr_cospi, args1f, {NULL},518cases_uniform_float, 0x39800000, 0x41800000},519{"tanpi", (funcptr)mpfr_tanpi, args1, {NULL},520cases_uniform, 0x3e400000, 0x40300000},521{"tanpif", (funcptr)mpfr_tanpi, args1f, {NULL},522cases_uniform_float, 0x39800000, 0x41800000},523/*524* Inverse trig: asin, acos. Between 1 and -1, of course. acos525* goes down to 2^-54, asin to 2^-27.526*/527{"asin", (funcptr)mpfr_asin, args1, {NULL},528cases_uniform, 0x3e400000, 0x3fefffff},529{"asinf", (funcptr)mpfr_asin, args1f, {NULL},530cases_uniform_float, 0x39800000, 0x3f7fffff},531{"acos", (funcptr)mpfr_acos, args1, {NULL},532cases_uniform, 0x3c900000, 0x3fefffff},533{"acosf", (funcptr)mpfr_acos, args1f, {NULL},534cases_uniform_float, 0x33800000, 0x3f7fffff},535/*536* Inverse trig: atan. atan is stable (in double prec) with537* argument magnitude past 2^53, so we'll test up to there.538* atan(x) is boringly just x below 2^-27.539*/540{"atan", (funcptr)mpfr_atan, args1, {NULL},541cases_uniform, 0x3e400000, 0x43400000},542{"atanf", (funcptr)mpfr_atan, args1f, {NULL},543cases_uniform_float, 0x39800000, 0x4b800000},544/*545* atan2. Interesting cases arise when the exponents of the546* arguments differ by at most about 50.547*/548{"atan2", (funcptr)mpfr_atan2, args2, {NULL},549atan2_cases, 0},550{"atan2f", (funcptr)mpfr_atan2, args2f, {NULL},551atan2_cases_float, 0},552/*553* The exponentials: exp, sinh, cosh. They overflow at around554* 710. exp and sinh are boring below 2^-54, cosh below 2^-27.555*/556{"exp", (funcptr)mpfr_exp, args1, {NULL},557cases_uniform, 0x3c900000, 0x40878000},558{"expf", (funcptr)mpfr_exp, args1f, {NULL},559cases_uniform_float, 0x33800000, 0x42dc0000},560{"sinh", (funcptr)mpfr_sinh, args1, {NULL},561cases_uniform, 0x3c900000, 0x40878000},562{"sinhf", (funcptr)mpfr_sinh, args1f, {NULL},563cases_uniform_float, 0x33800000, 0x42dc0000},564{"cosh", (funcptr)mpfr_cosh, args1, {NULL},565cases_uniform, 0x3e400000, 0x40878000},566{"coshf", (funcptr)mpfr_cosh, args1f, {NULL},567cases_uniform_float, 0x39800000, 0x42dc0000},568/*569* tanh is stable past around 20. It's boring below 2^-27.570*/571{"tanh", (funcptr)mpfr_tanh, args1, {NULL},572cases_uniform, 0x3e400000, 0x40340000},573{"tanhf", (funcptr)mpfr_tanh, args1f, {NULL},574cases_uniform, 0x39800000, 0x41100000},575/*576* log must be tested only on positive numbers, but can cover577* the whole range of positive nonzero finite numbers. It never578* gets boring.579*/580{"log", (funcptr)mpfr_log, args1, {NULL}, log_cases, 0},581{"logf", (funcptr)mpfr_log, args1f, {NULL}, log_cases_float, 0},582{"log10", (funcptr)mpfr_log10, args1, {NULL}, log_cases, 0},583{"log10f", (funcptr)mpfr_log10, args1f, {NULL}, log_cases_float, 0},584/*585* pow.586*/587{"pow", (funcptr)mpfr_pow, args2, {NULL}, pow_cases, 0},588{"powf", (funcptr)mpfr_pow, args2f, {NULL}, pow_cases_float, 0},589/*590* Trig range reduction. We are able to test this for all591* finite values, but will only bother for things between 2^-3592* and 2^+52.593*/594{"rred", (funcptr)test_rred, rred, {NULL}, rred_cases, 0},595{"rredf", (funcptr)test_rred, rredf, {NULL}, rred_cases_float, 0},596/*597* Square and cube root.598*/599{"sqrt", (funcptr)mpfr_sqrt, args1, {NULL}, log_cases, 0},600{"sqrtf", (funcptr)mpfr_sqrt, args1f, {NULL}, log_cases_float, 0},601{"cbrt", (funcptr)mpfr_cbrt, args1, {NULL}, log_cases, 0},602{"cbrtf", (funcptr)mpfr_cbrt, args1f, {NULL}, log_cases_float, 0},603{"hypot", (funcptr)mpfr_hypot, args2, {NULL}, atan2_cases, 0},604{"hypotf", (funcptr)mpfr_hypot, args2f, {NULL}, atan2_cases_float, 0},605/*606* Seminumerical functions.607*/608{"ceil", (funcptr)test_ceil, semi1, {NULL}, cases_semi1},609{"ceilf", (funcptr)test_ceilf, semi1f, {NULL}, cases_semi1_float},610{"floor", (funcptr)test_floor, semi1, {NULL}, cases_semi1},611{"floorf", (funcptr)test_floorf, semi1f, {NULL}, cases_semi1_float},612{"fmod", (funcptr)test_fmod, semi2, {NULL}, cases_semi2},613{"fmodf", (funcptr)test_fmodf, semi2f, {NULL}, cases_semi2_float},614{"ldexp", (funcptr)test_ldexp, t_ldexp, {NULL}, cases_ldexp},615{"ldexpf", (funcptr)test_ldexpf, t_ldexpf, {NULL}, cases_ldexp_float},616{"frexp", (funcptr)test_frexp, t_frexp, {NULL}, cases_semi1},617{"frexpf", (funcptr)test_frexpf, t_frexpf, {NULL}, cases_semi1_float},618{"modf", (funcptr)test_modf, t_modf, {NULL}, cases_semi1},619{"modff", (funcptr)test_modff, t_modff, {NULL}, cases_semi1_float},620621/*622* Classification and more semi-numericals623*/624{"copysign", (funcptr)test_copysign, semi2, {NULL}, cases_semi2},625{"copysignf", (funcptr)test_copysignf, semi2f, {NULL}, cases_semi2_float},626{"isfinite", (funcptr)test_isfinite, classify, {NULL}, cases_uniform, 0, 0x7fffffff},627{"isfinitef", (funcptr)test_isfinitef, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},628{"isinf", (funcptr)test_isinf, classify, {NULL}, cases_uniform, 0, 0x7fffffff},629{"isinff", (funcptr)test_isinff, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},630{"isnan", (funcptr)test_isnan, classify, {NULL}, cases_uniform, 0, 0x7fffffff},631{"isnanf", (funcptr)test_isnanf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},632{"isnormal", (funcptr)test_isnormal, classify, {NULL}, cases_uniform, 0, 0x7fffffff},633{"isnormalf", (funcptr)test_isnormalf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},634{"signbit", (funcptr)test_signbit, classify, {NULL}, cases_uniform, 0, 0x7fffffff},635{"signbitf", (funcptr)test_signbitf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},636{"fpclassify", (funcptr)test_fpclassify, classify, {NULL}, cases_uniform, 0, 0x7fffffff},637{"fpclassifyf", (funcptr)test_fpclassifyf, classifyf, {NULL}, cases_uniform_float, 0, 0x7fffffff},638/*639* Comparisons640*/641{"isgreater", (funcptr)test_isgreater, compare, {NULL}, cases_uniform, 0, 0x7fffffff},642{"isgreaterequal", (funcptr)test_isgreaterequal, compare, {NULL}, cases_uniform, 0, 0x7fffffff},643{"isless", (funcptr)test_isless, compare, {NULL}, cases_uniform, 0, 0x7fffffff},644{"islessequal", (funcptr)test_islessequal, compare, {NULL}, cases_uniform, 0, 0x7fffffff},645{"islessgreater", (funcptr)test_islessgreater, compare, {NULL}, cases_uniform, 0, 0x7fffffff},646{"isunordered", (funcptr)test_isunordered, compare, {NULL}, cases_uniform, 0, 0x7fffffff},647648{"isgreaterf", (funcptr)test_isgreaterf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},649{"isgreaterequalf", (funcptr)test_isgreaterequalf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},650{"islessf", (funcptr)test_islessf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},651{"islessequalf", (funcptr)test_islessequalf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},652{"islessgreaterf", (funcptr)test_islessgreaterf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},653{"isunorderedf", (funcptr)test_isunorderedf, comparef, {NULL}, cases_uniform_float, 0, 0x7fffffff},654655/*656* Inverse Hyperbolic functions657*/658{"atanh", (funcptr)mpfr_atanh, args1, {NULL}, cases_uniform, 0x3e400000, 0x3fefffff},659{"asinh", (funcptr)mpfr_asinh, args1, {NULL}, cases_uniform, 0x3e400000, 0x3fefffff},660{"acosh", (funcptr)mpfr_acosh, args1, {NULL}, cases_uniform_positive, 0x3ff00000, 0x7fefffff},661662{"atanhf", (funcptr)mpfr_atanh, args1f, {NULL}, cases_uniform_float, 0x32000000, 0x3f7fffff},663{"asinhf", (funcptr)mpfr_asinh, args1f, {NULL}, cases_uniform_float, 0x32000000, 0x3f7fffff},664{"acoshf", (funcptr)mpfr_acosh, args1f, {NULL}, cases_uniform_float_positive, 0x3f800000, 0x7f800000},665666/*667* Everything else (sitting in a section down here at the bottom668* because historically they were not tested because we didn't669* have reference implementations for them)670*/671{"csin", (funcptr)mpc_sin, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},672{"csinf", (funcptr)mpc_sin, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},673{"ccos", (funcptr)mpc_cos, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},674{"ccosf", (funcptr)mpc_cos, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},675{"ctan", (funcptr)mpc_tan, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},676{"ctanf", (funcptr)mpc_tan, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},677678{"casin", (funcptr)mpc_asin, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},679{"casinf", (funcptr)mpc_asin, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},680{"cacos", (funcptr)mpc_acos, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},681{"cacosf", (funcptr)mpc_acos, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},682{"catan", (funcptr)mpc_atan, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},683{"catanf", (funcptr)mpc_atan, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},684685{"csinh", (funcptr)mpc_sinh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},686{"csinhf", (funcptr)mpc_sinh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},687{"ccosh", (funcptr)mpc_cosh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},688{"ccoshf", (funcptr)mpc_cosh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},689{"ctanh", (funcptr)mpc_tanh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},690{"ctanhf", (funcptr)mpc_tanh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},691692{"casinh", (funcptr)mpc_asinh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},693{"casinhf", (funcptr)mpc_asinh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},694{"cacosh", (funcptr)mpc_acosh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},695{"cacoshf", (funcptr)mpc_acosh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},696{"catanh", (funcptr)mpc_atanh, args1c, {NULL}, complex_cases_uniform, 0x3f000000, 0x40300000},697{"catanhf", (funcptr)mpc_atanh, args1fc, {NULL}, complex_cases_uniform_float, 0x38000000, 0x41800000},698699{"cexp", (funcptr)mpc_exp, args1c, {NULL}, complex_cases_uniform, 0x3c900000, 0x40862000},700{"cpow", (funcptr)test_cpow, args2c, {NULL}, complex_pow_cases, 0x3fc00000, 0x40000000},701{"clog", (funcptr)mpc_log, args1c, {NULL}, complex_log_cases, 0, 0},702{"csqrt", (funcptr)mpc_sqrt, args1c, {NULL}, complex_log_cases, 0, 0},703704{"cexpf", (funcptr)mpc_exp, args1fc, {NULL}, complex_cases_uniform_float, 0x24800000, 0x42b00000},705{"cpowf", (funcptr)test_cpow, args2fc, {NULL}, complex_pow_cases_float, 0x3e000000, 0x41000000},706{"clogf", (funcptr)mpc_log, args1fc, {NULL}, complex_log_cases_float, 0, 0},707{"csqrtf", (funcptr)mpc_sqrt, args1fc, {NULL}, complex_log_cases_float, 0, 0},708709{"cdiv", (funcptr)mpc_div, args2c, {NULL}, complex_arithmetic_cases, 0, 0},710{"cmul", (funcptr)mpc_mul, args2c, {NULL}, complex_arithmetic_cases, 0, 0},711{"cadd", (funcptr)mpc_add, args2c, {NULL}, complex_arithmetic_cases, 0, 0},712{"csub", (funcptr)mpc_sub, args2c, {NULL}, complex_arithmetic_cases, 0, 0},713714{"cdivf", (funcptr)mpc_div, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},715{"cmulf", (funcptr)mpc_mul, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},716{"caddf", (funcptr)mpc_add, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},717{"csubf", (funcptr)mpc_sub, args2fc, {NULL}, complex_arithmetic_cases_float, 0, 0},718719{"cabsf", (funcptr)mpc_abs, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},720{"cabs", (funcptr)mpc_abs, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},721{"cargf", (funcptr)mpc_arg, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},722{"carg", (funcptr)mpc_arg, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},723{"cimagf", (funcptr)mpc_imag, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},724{"cimag", (funcptr)mpc_imag, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},725{"conjf", (funcptr)mpc_conj, args1fc, {NULL}, complex_arithmetic_cases_float, 0, 0},726{"conj", (funcptr)mpc_conj, args1c, {NULL}, complex_arithmetic_cases, 0, 0},727{"cprojf", (funcptr)mpc_proj, args1fc, {NULL}, complex_arithmetic_cases_float, 0, 0},728{"cproj", (funcptr)mpc_proj, args1c, {NULL}, complex_arithmetic_cases, 0, 0},729{"crealf", (funcptr)mpc_real, args1fcr, {NULL}, complex_arithmetic_cases_float, 0, 0},730{"creal", (funcptr)mpc_real, args1cr, {NULL}, complex_arithmetic_cases, 0, 0},731{"erfcf", (funcptr)mpfr_erfc, args1f, {NULL}, cases_biased_float, 0x1e800000, 0x41000000},732{"erfc", (funcptr)mpfr_erfc, args1, {NULL}, cases_biased, 0x3bd00000, 0x403c0000},733{"erff", (funcptr)mpfr_erf, args1f, {NULL}, cases_biased_float, 0x03800000, 0x40700000},734{"erf", (funcptr)mpfr_erf, args1, {NULL}, cases_biased, 0x00800000, 0x40200000},735{"exp2f", (funcptr)mpfr_exp2, args1f, {NULL}, cases_uniform_float, 0x33800000, 0x43c00000},736{"exp2", (funcptr)mpfr_exp2, args1, {NULL}, cases_uniform, 0x3ca00000, 0x40a00000},737{"expm1f", (funcptr)mpfr_expm1, args1f, {NULL}, cases_uniform_float, 0x33000000, 0x43800000},738{"expm1", (funcptr)mpfr_expm1, args1, {NULL}, cases_uniform, 0x3c900000, 0x409c0000},739{"fmaxf", (funcptr)mpfr_max, args2f, {NULL}, minmax_cases_float, 0, 0x7f7fffff},740{"fmax", (funcptr)mpfr_max, args2, {NULL}, minmax_cases, 0, 0x7fefffff},741{"fminf", (funcptr)mpfr_min, args2f, {NULL}, minmax_cases_float, 0, 0x7f7fffff},742{"fmin", (funcptr)mpfr_min, args2, {NULL}, minmax_cases, 0, 0x7fefffff},743{"lgammaf", (funcptr)test_lgamma, args1f, {NULL}, cases_uniform_float, 0x01800000, 0x7f800000},744{"lgamma", (funcptr)test_lgamma, args1, {NULL}, cases_uniform, 0x00100000, 0x7ff00000},745{"log1pf", (funcptr)mpfr_log1p, args1f, {NULL}, log1p_cases_float, 0, 0},746{"log1p", (funcptr)mpfr_log1p, args1, {NULL}, log1p_cases, 0, 0},747{"log2f", (funcptr)mpfr_log2, args1f, {NULL}, log_cases_float, 0, 0},748{"log2", (funcptr)mpfr_log2, args1, {NULL}, log_cases, 0, 0},749{"tgammaf", (funcptr)mpfr_gamma, args1f, {NULL}, cases_uniform_float, 0x2f800000, 0x43000000},750{"tgamma", (funcptr)mpfr_gamma, args1, {NULL}, cases_uniform, 0x3c000000, 0x40800000},751};752/* clang-format on */753754const int nfunctions = ( sizeof(functions)/sizeof(*functions) );755756#define random_sign ( random_upto(1) ? 0x80000000 : 0 )757758static int iszero(uint32 *x) {759return !((x[0] & 0x7FFFFFFF) || x[1]);760}761762763static void complex_log_cases(uint32 *out, uint32 param1,764uint32 param2) {765cases_uniform(out,0x00100000,0x7fefffff);766cases_uniform(out+2,0x00100000,0x7fefffff);767}768769770static void complex_log_cases_float(uint32 *out, uint32 param1,771uint32 param2) {772cases_uniform_float(out,0x00800000,0x7f7fffff);773cases_uniform_float(out+2,0x00800000,0x7f7fffff);774}775776static void complex_cases_biased(uint32 *out, uint32 lowbound,777uint32 highbound) {778cases_biased(out,lowbound,highbound);779cases_biased(out+2,lowbound,highbound);780}781782static void complex_cases_biased_float(uint32 *out, uint32 lowbound,783uint32 highbound) {784cases_biased_float(out,lowbound,highbound);785cases_biased_float(out+2,lowbound,highbound);786}787788static void complex_cases_uniform(uint32 *out, uint32 lowbound,789uint32 highbound) {790cases_uniform(out,lowbound,highbound);791cases_uniform(out+2,lowbound,highbound);792}793794static void complex_cases_uniform_float(uint32 *out, uint32 lowbound,795uint32 highbound) {796cases_uniform_float(out,lowbound,highbound);797cases_uniform(out+2,lowbound,highbound);798}799800static void complex_pow_cases(uint32 *out, uint32 lowbound,801uint32 highbound) {802/*803* Generating non-overflowing cases for complex pow:804*805* Our base has both parts within the range [1/2,2], and hence806* its magnitude is within [1/2,2*sqrt(2)]. The magnitude of its807* logarithm in base 2 is therefore at most the magnitude of808* (log2(2*sqrt(2)) + i*pi/log(2)), or in other words809* hypot(3/2,pi/log(2)) = 4.77. So the magnitude of the exponent810* input must be at most our output magnitude limit (as a power811* of two) divided by that.812*813* I also set the output magnitude limit a bit low, because we814* don't guarantee (and neither does glibc) to prevent internal815* overflow in cases where the output _magnitude_ overflows but816* scaling it back down by cos and sin of the argument brings it817* back in range.818*/819cases_uniform(out,0x3fe00000, 0x40000000);820cases_uniform(out+2,0x3fe00000, 0x40000000);821cases_uniform(out+4,0x3f800000, 0x40600000);822cases_uniform(out+6,0x3f800000, 0x40600000);823}824825static void complex_pow_cases_float(uint32 *out, uint32 lowbound,826uint32 highbound) {827/*828* Reasoning as above, though of course the detailed numbers are829* all different.830*/831cases_uniform_float(out,0x3f000000, 0x40000000);832cases_uniform_float(out+2,0x3f000000, 0x40000000);833cases_uniform_float(out+4,0x3d600000, 0x41900000);834cases_uniform_float(out+6,0x3d600000, 0x41900000);835}836837static void complex_arithmetic_cases(uint32 *out, uint32 lowbound,838uint32 highbound) {839cases_uniform(out,0,0x7fefffff);840cases_uniform(out+2,0,0x7fefffff);841cases_uniform(out+4,0,0x7fefffff);842cases_uniform(out+6,0,0x7fefffff);843}844845static void complex_arithmetic_cases_float(uint32 *out, uint32 lowbound,846uint32 highbound) {847cases_uniform_float(out,0,0x7f7fffff);848cases_uniform_float(out+2,0,0x7f7fffff);849cases_uniform_float(out+4,0,0x7f7fffff);850cases_uniform_float(out+6,0,0x7f7fffff);851}852853/*854* Included from fplib test suite, in a compact self-contained855* form.856*/857858void float32_case(uint32 *ret) {859int n, bits;860uint32 f;861static int premax, preptr;862static uint32 *specifics = NULL;863864if (!ret) {865if (specifics)866free(specifics);867specifics = NULL;868premax = preptr = 0;869return;870}871872if (!specifics) {873int exps[] = {874-127, -126, -125, -24, -4, -3, -2, -1, 0, 1, 2, 3, 4,87524, 29, 30, 31, 32, 61, 62, 63, 64, 126, 127, 128876};877int sign, eptr;878uint32 se, j;879/*880* We want a cross product of:881* - each of two sign bits (2)882* - each of the above (unbiased) exponents (25)883* - the following list of fraction parts:884* * zero (1)885* * all bits (1)886* * one-bit-set (23)887* * one-bit-clear (23)888* * one-bit-and-above (20: 3 are duplicates)889* * one-bit-and-below (20: 3 are duplicates)890* (total 88)891* (total 4400)892*/893specifics = malloc(4400 * sizeof(*specifics));894preptr = 0;895for (sign = 0; sign <= 1; sign++) {896for (eptr = 0; eptr < sizeof(exps)/sizeof(*exps); eptr++) {897se = (sign ? 0x80000000 : 0) | ((exps[eptr]+127) << 23);898/*899* Zero.900*/901specifics[preptr++] = se | 0;902/*903* All bits.904*/905specifics[preptr++] = se | 0x7FFFFF;906/*907* One-bit-set.908*/909for (j = 1; j && j <= 0x400000; j <<= 1)910specifics[preptr++] = se | j;911/*912* One-bit-clear.913*/914for (j = 1; j && j <= 0x400000; j <<= 1)915specifics[preptr++] = se | (0x7FFFFF ^ j);916/*917* One-bit-and-everything-below.918*/919for (j = 2; j && j <= 0x100000; j <<= 1)920specifics[preptr++] = se | (2*j-1);921/*922* One-bit-and-everything-above.923*/924for (j = 4; j && j <= 0x200000; j <<= 1)925specifics[preptr++] = se | (0x7FFFFF ^ (j-1));926/*927* Done.928*/929}930}931assert(preptr == 4400);932premax = preptr;933}934935/*936* Decide whether to return a pre or a random case.937*/938n = random32() % (premax+1);939if (n < preptr) {940/*941* Return pre[n].942*/943uint32 t;944t = specifics[n];945specifics[n] = specifics[preptr-1];946specifics[preptr-1] = t; /* (not really needed) */947preptr--;948*ret = t;949} else {950/*951* Random case.952* Sign and exponent:953* - FIXME954* Significand:955* - with prob 1/5, a totally random bit pattern956* - with prob 1/5, all 1s down to some point and then random957* - with prob 1/5, all 1s up to some point and then random958* - with prob 1/5, all 0s down to some point and then random959* - with prob 1/5, all 0s up to some point and then random960*/961n = random32() % 5;962f = random32(); /* some random bits */963bits = random32() % 22 + 1; /* 1-22 */964switch (n) {965case 0:966break; /* leave f alone */967case 1:968f |= (1<<bits)-1;969break;970case 2:971f &= ~((1<<bits)-1);972break;973case 3:974f |= ~((1<<bits)-1);975break;976case 4:977f &= (1<<bits)-1;978break;979}980f &= 0x7FFFFF;981f |= (random32() & 0xFF800000);/* FIXME - do better */982*ret = f;983}984}985static void float64_case(uint32 *ret) {986int n, bits;987uint32 f, g;988static int premax, preptr;989static uint32 (*specifics)[2] = NULL;990991if (!ret) {992if (specifics)993free(specifics);994specifics = NULL;995premax = preptr = 0;996return;997}998999if (!specifics) {1000int exps[] = {1001-1023, -1022, -1021, -129, -128, -127, -126, -53, -4, -3, -2,1002-1, 0, 1, 2, 3, 4, 29, 30, 31, 32, 53, 61, 62, 63, 64, 127,1003128, 129, 1022, 1023, 10241004};1005int sign, eptr;1006uint32 se, j;1007/*1008* We want a cross product of:1009* - each of two sign bits (2)1010* - each of the above (unbiased) exponents (32)1011* - the following list of fraction parts:1012* * zero (1)1013* * all bits (1)1014* * one-bit-set (52)1015* * one-bit-clear (52)1016* * one-bit-and-above (49: 3 are duplicates)1017* * one-bit-and-below (49: 3 are duplicates)1018* (total 204)1019* (total 13056)1020*/1021specifics = malloc(13056 * sizeof(*specifics));1022preptr = 0;1023for (sign = 0; sign <= 1; sign++) {1024for (eptr = 0; eptr < sizeof(exps)/sizeof(*exps); eptr++) {1025se = (sign ? 0x80000000 : 0) | ((exps[eptr]+1023) << 20);1026/*1027* Zero.1028*/1029specifics[preptr][0] = 0;1030specifics[preptr][1] = 0;1031specifics[preptr++][0] |= se;1032/*1033* All bits.1034*/1035specifics[preptr][0] = 0xFFFFF;1036specifics[preptr][1] = ~0;1037specifics[preptr++][0] |= se;1038/*1039* One-bit-set.1040*/1041for (j = 1; j && j <= 0x80000000; j <<= 1) {1042specifics[preptr][0] = 0;1043specifics[preptr][1] = j;1044specifics[preptr++][0] |= se;1045if (j & 0xFFFFF) {1046specifics[preptr][0] = j;1047specifics[preptr][1] = 0;1048specifics[preptr++][0] |= se;1049}1050}1051/*1052* One-bit-clear.1053*/1054for (j = 1; j && j <= 0x80000000; j <<= 1) {1055specifics[preptr][0] = 0xFFFFF;1056specifics[preptr][1] = ~j;1057specifics[preptr++][0] |= se;1058if (j & 0xFFFFF) {1059specifics[preptr][0] = 0xFFFFF ^ j;1060specifics[preptr][1] = ~0;1061specifics[preptr++][0] |= se;1062}1063}1064/*1065* One-bit-and-everything-below.1066*/1067for (j = 2; j && j <= 0x80000000; j <<= 1) {1068specifics[preptr][0] = 0;1069specifics[preptr][1] = 2*j-1;1070specifics[preptr++][0] |= se;1071}1072for (j = 1; j && j <= 0x20000; j <<= 1) {1073specifics[preptr][0] = 2*j-1;1074specifics[preptr][1] = ~0;1075specifics[preptr++][0] |= se;1076}1077/*1078* One-bit-and-everything-above.1079*/1080for (j = 4; j && j <= 0x80000000; j <<= 1) {1081specifics[preptr][0] = 0xFFFFF;1082specifics[preptr][1] = ~(j-1);1083specifics[preptr++][0] |= se;1084}1085for (j = 1; j && j <= 0x40000; j <<= 1) {1086specifics[preptr][0] = 0xFFFFF ^ (j-1);1087specifics[preptr][1] = 0;1088specifics[preptr++][0] |= se;1089}1090/*1091* Done.1092*/1093}1094}1095assert(preptr == 13056);1096premax = preptr;1097}10981099/*1100* Decide whether to return a pre or a random case.1101*/1102n = (uint32) random32() % (uint32) (premax+1);1103if (n < preptr) {1104/*1105* Return pre[n].1106*/1107uint32 t;1108t = specifics[n][0];1109specifics[n][0] = specifics[preptr-1][0];1110specifics[preptr-1][0] = t; /* (not really needed) */1111ret[0] = t;1112t = specifics[n][1];1113specifics[n][1] = specifics[preptr-1][1];1114specifics[preptr-1][1] = t; /* (not really needed) */1115ret[1] = t;1116preptr--;1117} else {1118/*1119* Random case.1120* Sign and exponent:1121* - FIXME1122* Significand:1123* - with prob 1/5, a totally random bit pattern1124* - with prob 1/5, all 1s down to some point and then random1125* - with prob 1/5, all 1s up to some point and then random1126* - with prob 1/5, all 0s down to some point and then random1127* - with prob 1/5, all 0s up to some point and then random1128*/1129n = random32() % 5;1130f = random32(); /* some random bits */1131g = random32(); /* some random bits */1132bits = random32() % 51 + 1; /* 1-51 */1133switch (n) {1134case 0:1135break; /* leave f alone */1136case 1:1137if (bits <= 32)1138f |= (1<<bits)-1;1139else {1140bits -= 32;1141g |= (1<<bits)-1;1142f = ~0;1143}1144break;1145case 2:1146if (bits <= 32)1147f &= ~((1<<bits)-1);1148else {1149bits -= 32;1150g &= ~((1<<bits)-1);1151f = 0;1152}1153break;1154case 3:1155if (bits <= 32)1156g &= (1<<bits)-1;1157else {1158bits -= 32;1159f &= (1<<bits)-1;1160g = 0;1161}1162break;1163case 4:1164if (bits <= 32)1165g |= ~((1<<bits)-1);1166else {1167bits -= 32;1168f |= ~((1<<bits)-1);1169g = ~0;1170}1171break;1172}1173g &= 0xFFFFF;1174g |= (random32() & 0xFFF00000);/* FIXME - do better */1175ret[0] = g;1176ret[1] = f;1177}1178}11791180static void cases_biased(uint32 *out, uint32 lowbound,1181uint32 highbound) {1182do {1183out[0] = highbound - random_upto_biased(highbound-lowbound, 8);1184out[1] = random_upto(0xFFFFFFFF);1185out[0] |= random_sign;1186} while (iszero(out)); /* rule out zero */1187}11881189static void cases_biased_positive(uint32 *out, uint32 lowbound,1190uint32 highbound) {1191do {1192out[0] = highbound - random_upto_biased(highbound-lowbound, 8);1193out[1] = random_upto(0xFFFFFFFF);1194} while (iszero(out)); /* rule out zero */1195}11961197static void cases_biased_float(uint32 *out, uint32 lowbound,1198uint32 highbound) {1199do {1200out[0] = highbound - random_upto_biased(highbound-lowbound, 8);1201out[1] = 0;1202out[0] |= random_sign;1203} while (iszero(out)); /* rule out zero */1204}12051206static void cases_semi1(uint32 *out, uint32 param1,1207uint32 param2) {1208float64_case(out);1209}12101211static void cases_semi1_float(uint32 *out, uint32 param1,1212uint32 param2) {1213float32_case(out);1214}12151216static void cases_semi2(uint32 *out, uint32 param1,1217uint32 param2) {1218float64_case(out);1219float64_case(out+2);1220}12211222static void cases_semi2_float(uint32 *out, uint32 param1,1223uint32 param2) {1224float32_case(out);1225float32_case(out+2);1226}12271228static void cases_ldexp(uint32 *out, uint32 param1,1229uint32 param2) {1230float64_case(out);1231out[2] = random_upto(2048)-1024;1232}12331234static void cases_ldexp_float(uint32 *out, uint32 param1,1235uint32 param2) {1236float32_case(out);1237out[2] = random_upto(256)-128;1238}12391240static void cases_uniform(uint32 *out, uint32 lowbound,1241uint32 highbound) {1242do {1243out[0] = highbound - random_upto(highbound-lowbound);1244out[1] = random_upto(0xFFFFFFFF);1245out[0] |= random_sign;1246} while (iszero(out)); /* rule out zero */1247}1248static void cases_uniform_float(uint32 *out, uint32 lowbound,1249uint32 highbound) {1250do {1251out[0] = highbound - random_upto(highbound-lowbound);1252out[1] = 0;1253out[0] |= random_sign;1254} while (iszero(out)); /* rule out zero */1255}12561257static void cases_uniform_positive(uint32 *out, uint32 lowbound,1258uint32 highbound) {1259do {1260out[0] = highbound - random_upto(highbound-lowbound);1261out[1] = random_upto(0xFFFFFFFF);1262} while (iszero(out)); /* rule out zero */1263}1264static void cases_uniform_float_positive(uint32 *out, uint32 lowbound,1265uint32 highbound) {1266do {1267out[0] = highbound - random_upto(highbound-lowbound);1268out[1] = 0;1269} while (iszero(out)); /* rule out zero */1270}127112721273static void log_cases(uint32 *out, uint32 param1,1274uint32 param2) {1275do {1276out[0] = random_upto(0x7FEFFFFF);1277out[1] = random_upto(0xFFFFFFFF);1278} while (iszero(out)); /* rule out zero */1279}12801281static void log_cases_float(uint32 *out, uint32 param1,1282uint32 param2) {1283do {1284out[0] = random_upto(0x7F7FFFFF);1285out[1] = 0;1286} while (iszero(out)); /* rule out zero */1287}12881289static void log1p_cases(uint32 *out, uint32 param1, uint32 param2)1290{1291uint32 sign = random_sign;1292if (sign == 0) {1293cases_uniform_positive(out, 0x3c700000, 0x43400000);1294} else {1295cases_uniform_positive(out, 0x3c000000, 0x3ff00000);1296}1297out[0] |= sign;1298}12991300static void log1p_cases_float(uint32 *out, uint32 param1, uint32 param2)1301{1302uint32 sign = random_sign;1303if (sign == 0) {1304cases_uniform_float_positive(out, 0x32000000, 0x4c000000);1305} else {1306cases_uniform_float_positive(out, 0x30000000, 0x3f800000);1307}1308out[0] |= sign;1309}13101311static void minmax_cases(uint32 *out, uint32 param1, uint32 param2)1312{1313do {1314out[0] = random_upto(0x7FEFFFFF);1315out[1] = random_upto(0xFFFFFFFF);1316out[0] |= random_sign;1317out[2] = random_upto(0x7FEFFFFF);1318out[3] = random_upto(0xFFFFFFFF);1319out[2] |= random_sign;1320} while (iszero(out)); /* rule out zero */1321}13221323static void minmax_cases_float(uint32 *out, uint32 param1, uint32 param2)1324{1325do {1326out[0] = random_upto(0x7F7FFFFF);1327out[1] = 0;1328out[0] |= random_sign;1329out[2] = random_upto(0x7F7FFFFF);1330out[3] = 0;1331out[2] |= random_sign;1332} while (iszero(out)); /* rule out zero */1333}13341335static void rred_cases(uint32 *out, uint32 param1,1336uint32 param2) {1337do {1338out[0] = ((0x3fc00000 + random_upto(0x036fffff)) |1339(random_upto(1) << 31));1340out[1] = random_upto(0xFFFFFFFF);1341} while (iszero(out)); /* rule out zero */1342}13431344static void rred_cases_float(uint32 *out, uint32 param1,1345uint32 param2) {1346do {1347out[0] = ((0x3e000000 + random_upto(0x0cffffff)) |1348(random_upto(1) << 31));1349out[1] = 0; /* for iszero */1350} while (iszero(out)); /* rule out zero */1351}13521353static void atan2_cases(uint32 *out, uint32 param1,1354uint32 param2) {1355do {1356int expdiff = random_upto(101)-51;1357int swap;1358if (expdiff < 0) {1359expdiff = -expdiff;1360swap = 2;1361} else1362swap = 0;1363out[swap ^ 0] = random_upto(0x7FEFFFFF-((expdiff+1)<<20));1364out[swap ^ 2] = random_upto(((expdiff+1)<<20)-1) + out[swap ^ 0];1365out[1] = random_upto(0xFFFFFFFF);1366out[3] = random_upto(0xFFFFFFFF);1367out[0] |= random_sign;1368out[2] |= random_sign;1369} while (iszero(out) || iszero(out+2));/* rule out zero */1370}13711372static void atan2_cases_float(uint32 *out, uint32 param1,1373uint32 param2) {1374do {1375int expdiff = random_upto(44)-22;1376int swap;1377if (expdiff < 0) {1378expdiff = -expdiff;1379swap = 2;1380} else1381swap = 0;1382out[swap ^ 0] = random_upto(0x7F7FFFFF-((expdiff+1)<<23));1383out[swap ^ 2] = random_upto(((expdiff+1)<<23)-1) + out[swap ^ 0];1384out[0] |= random_sign;1385out[2] |= random_sign;1386out[1] = out[3] = 0; /* for iszero */1387} while (iszero(out) || iszero(out+2));/* rule out zero */1388}13891390static void pow_cases(uint32 *out, uint32 param1,1391uint32 param2) {1392/*1393* Pick an exponent e (-0x33 to +0x7FE) for x, and here's the1394* range of numbers we can use as y:1395*1396* For e < 0x3FE, the range is [-0x400/(0x3FE-e),+0x432/(0x3FE-e)]1397* For e > 0x3FF, the range is [-0x432/(e-0x3FF),+0x400/(e-0x3FF)]1398*1399* For e == 0x3FE or e == 0x3FF, the range gets infinite at one1400* end or the other, so we have to be cleverer: pick a number n1401* of useful bits in the mantissa (1 thru 52, so 1 must imply1402* 0x3ff00000.00000001 whereas 52 is anything at least as big1403* as 0x3ff80000.00000000; for e == 0x3fe, 1 necessarily means1404* 0x3fefffff.ffffffff and 52 is anything at most as big as1405* 0x3fe80000.00000000). Then, as it happens, a sensible1406* maximum power is 2^(63-n) for e == 0x3fe, and 2^(62-n) for1407* e == 0x3ff.1408*1409* We inevitably get some overflows in approximating the log1410* curves by these nasty step functions, but that's all right -1411* we do want _some_ overflows to be tested.1412*1413* Having got that, then, it's just a matter of inventing a1414* probability distribution for all of this.1415*/1416int e, n;1417uint32 dmin, dmax;1418const uint32 pmin = 0x3e100000;14191420/*1421* Generate exponents in a slightly biased fashion.1422*/1423e = (random_upto(1) ? /* is exponent small or big? */14240x3FE - random_upto_biased(0x431,2) : /* small */14250x3FF + random_upto_biased(0x3FF,2)); /* big */14261427/*1428* Now split into cases.1429*/1430if (e < 0x3FE || e > 0x3FF) {1431uint32 imin, imax;1432if (e < 0x3FE)1433imin = 0x40000 / (0x3FE - e), imax = 0x43200 / (0x3FE - e);1434else1435imin = 0x43200 / (e - 0x3FF), imax = 0x40000 / (e - 0x3FF);1436/* Power range runs from -imin to imax. Now convert to doubles */1437dmin = doubletop(imin, -8);1438dmax = doubletop(imax, -8);1439/* Compute the number of mantissa bits. */1440n = (e > 0 ? 53 : 52+e);1441} else {1442/* Critical exponents. Generate a top bit index. */1443n = 52 - random_upto_biased(51, 4);1444if (e == 0x3FE)1445dmax = 63 - n;1446else1447dmax = 62 - n;1448dmax = (dmax << 20) + 0x3FF00000;1449dmin = dmax;1450}1451/* Generate a mantissa. */1452if (n <= 32) {1453out[0] = 0;1454out[1] = random_upto((1 << (n-1)) - 1) + (1 << (n-1));1455} else if (n == 33) {1456out[0] = 1;1457out[1] = random_upto(0xFFFFFFFF);1458} else if (n > 33) {1459out[0] = random_upto((1 << (n-33)) - 1) + (1 << (n-33));1460out[1] = random_upto(0xFFFFFFFF);1461}1462/* Negate the mantissa if e == 0x3FE. */1463if (e == 0x3FE) {1464out[1] = -out[1];1465out[0] = -out[0];1466if (out[1]) out[0]--;1467}1468/* Put the exponent on. */1469out[0] &= 0xFFFFF;1470out[0] |= ((e > 0 ? e : 0) << 20);1471/* Generate a power. Powers don't go below 2^-30. */1472if (random_upto(1)) {1473/* Positive power */1474out[2] = dmax - random_upto_biased(dmax-pmin, 10);1475} else {1476/* Negative power */1477out[2] = (dmin - random_upto_biased(dmin-pmin, 10)) | 0x80000000;1478}1479out[3] = random_upto(0xFFFFFFFF);1480}1481static void pow_cases_float(uint32 *out, uint32 param1,1482uint32 param2) {1483/*1484* Pick an exponent e (-0x16 to +0xFE) for x, and here's the1485* range of numbers we can use as y:1486*1487* For e < 0x7E, the range is [-0x80/(0x7E-e),+0x95/(0x7E-e)]1488* For e > 0x7F, the range is [-0x95/(e-0x7F),+0x80/(e-0x7F)]1489*1490* For e == 0x7E or e == 0x7F, the range gets infinite at one1491* end or the other, so we have to be cleverer: pick a number n1492* of useful bits in the mantissa (1 thru 23, so 1 must imply1493* 0x3f800001 whereas 23 is anything at least as big as1494* 0x3fc00000; for e == 0x7e, 1 necessarily means 0x3f7fffff1495* and 23 is anything at most as big as 0x3f400000). Then, as1496* it happens, a sensible maximum power is 2^(31-n) for e ==1497* 0x7e, and 2^(30-n) for e == 0x7f.1498*1499* We inevitably get some overflows in approximating the log1500* curves by these nasty step functions, but that's all right -1501* we do want _some_ overflows to be tested.1502*1503* Having got that, then, it's just a matter of inventing a1504* probability distribution for all of this.1505*/1506int e, n;1507uint32 dmin, dmax;1508const uint32 pmin = 0x38000000;15091510/*1511* Generate exponents in a slightly biased fashion.1512*/1513e = (random_upto(1) ? /* is exponent small or big? */15140x7E - random_upto_biased(0x94,2) : /* small */15150x7F + random_upto_biased(0x7f,2)); /* big */15161517/*1518* Now split into cases.1519*/1520if (e < 0x7E || e > 0x7F) {1521uint32 imin, imax;1522if (e < 0x7E)1523imin = 0x8000 / (0x7e - e), imax = 0x9500 / (0x7e - e);1524else1525imin = 0x9500 / (e - 0x7f), imax = 0x8000 / (e - 0x7f);1526/* Power range runs from -imin to imax. Now convert to doubles */1527dmin = floatval(imin, -8);1528dmax = floatval(imax, -8);1529/* Compute the number of mantissa bits. */1530n = (e > 0 ? 24 : 23+e);1531} else {1532/* Critical exponents. Generate a top bit index. */1533n = 23 - random_upto_biased(22, 4);1534if (e == 0x7E)1535dmax = 31 - n;1536else1537dmax = 30 - n;1538dmax = (dmax << 23) + 0x3F800000;1539dmin = dmax;1540}1541/* Generate a mantissa. */1542out[0] = random_upto((1 << (n-1)) - 1) + (1 << (n-1));1543out[1] = 0;1544/* Negate the mantissa if e == 0x7E. */1545if (e == 0x7E) {1546out[0] = -out[0];1547}1548/* Put the exponent on. */1549out[0] &= 0x7FFFFF;1550out[0] |= ((e > 0 ? e : 0) << 23);1551/* Generate a power. Powers don't go below 2^-15. */1552if (random_upto(1)) {1553/* Positive power */1554out[2] = dmax - random_upto_biased(dmax-pmin, 10);1555} else {1556/* Negative power */1557out[2] = (dmin - random_upto_biased(dmin-pmin, 10)) | 0x80000000;1558}1559out[3] = 0;1560}15611562void vet_for_decline(Testable *fn, uint32 *args, uint32 *result, int got_errno_in) {1563int declined = 0;15641565switch (fn->type) {1566case args1:1567case rred:1568case semi1:1569case t_frexp:1570case t_modf:1571case classify:1572case t_ldexp:1573declined |= lib_fo && is_dhard(args+0);1574break;1575case args1f:1576case rredf:1577case semi1f:1578case t_frexpf:1579case t_modff:1580case classifyf:1581declined |= lib_fo && is_shard(args+0);1582break;1583case args2:1584case semi2:1585case args1c:1586case args1cr:1587case compare:1588declined |= lib_fo && is_dhard(args+0);1589declined |= lib_fo && is_dhard(args+2);1590break;1591case args2f:1592case semi2f:1593case t_ldexpf:1594case comparef:1595case args1fc:1596case args1fcr:1597declined |= lib_fo && is_shard(args+0);1598declined |= lib_fo && is_shard(args+2);1599break;1600case args2c:1601declined |= lib_fo && is_dhard(args+0);1602declined |= lib_fo && is_dhard(args+2);1603declined |= lib_fo && is_dhard(args+4);1604declined |= lib_fo && is_dhard(args+6);1605break;1606case args2fc:1607declined |= lib_fo && is_shard(args+0);1608declined |= lib_fo && is_shard(args+2);1609declined |= lib_fo && is_shard(args+4);1610declined |= lib_fo && is_shard(args+6);1611break;1612}16131614switch (fn->type) {1615case args1: /* return an extra-precise result */1616case args2:1617case rred:1618case semi1: /* return a double result */1619case semi2:1620case t_ldexp:1621case t_frexp: /* return double * int */1622case args1cr:1623declined |= lib_fo && is_dhard(result);1624break;1625case args1f:1626case args2f:1627case rredf:1628case semi1f:1629case semi2f:1630case t_ldexpf:1631case args1fcr:1632declined |= lib_fo && is_shard(result);1633break;1634case t_modf: /* return double * double */1635declined |= lib_fo && is_dhard(result+0);1636declined |= lib_fo && is_dhard(result+2);1637break;1638case t_modff: /* return float * float */1639declined |= lib_fo && is_shard(result+2);1640/* fall through */1641case t_frexpf: /* return float * int */1642declined |= lib_fo && is_shard(result+0);1643break;1644case args1c:1645case args2c:1646declined |= lib_fo && is_dhard(result+0);1647declined |= lib_fo && is_dhard(result+4);1648break;1649case args1fc:1650case args2fc:1651declined |= lib_fo && is_shard(result+0);1652declined |= lib_fo && is_shard(result+4);1653break;1654}16551656/* Expect basic arithmetic tests to be declined if the command1657* line said that would happen */1658declined |= (lib_no_arith && (fn->func == (funcptr)mpc_add ||1659fn->func == (funcptr)mpc_sub ||1660fn->func == (funcptr)mpc_mul ||1661fn->func == (funcptr)mpc_div));16621663if (!declined) {1664if (got_errno_in)1665ntests++;1666else1667ntests += 3;1668}1669}16701671void docase(Testable *fn, uint32 *args) {1672uint32 result[8]; /* real part in first 4, imaginary part in last 4 */1673char *errstr = NULL;1674mpfr_t a, b, r;1675mpc_t ac, bc, rc;1676int rejected, printextra;1677wrapperctx ctx;16781679mpfr_init2(a, MPFR_PREC);1680mpfr_init2(b, MPFR_PREC);1681mpfr_init2(r, MPFR_PREC);1682mpc_init2(ac, MPFR_PREC);1683mpc_init2(bc, MPFR_PREC);1684mpc_init2(rc, MPFR_PREC);16851686printf("func=%s", fn->name);16871688rejected = 0; /* FIXME */16891690switch (fn->type) {1691case args1:1692case rred:1693case semi1:1694case t_frexp:1695case t_modf:1696case classify:1697printf(" op1=%08x.%08x", args[0], args[1]);1698break;1699case args1f:1700case rredf:1701case semi1f:1702case t_frexpf:1703case t_modff:1704case classifyf:1705printf(" op1=%08x", args[0]);1706break;1707case args2:1708case semi2:1709case compare:1710printf(" op1=%08x.%08x", args[0], args[1]);1711printf(" op2=%08x.%08x", args[2], args[3]);1712break;1713case args2f:1714case semi2f:1715case t_ldexpf:1716case comparef:1717printf(" op1=%08x", args[0]);1718printf(" op2=%08x", args[2]);1719break;1720case t_ldexp:1721printf(" op1=%08x.%08x", args[0], args[1]);1722printf(" op2=%08x", args[2]);1723break;1724case args1c:1725case args1cr:1726printf(" op1r=%08x.%08x", args[0], args[1]);1727printf(" op1i=%08x.%08x", args[2], args[3]);1728break;1729case args2c:1730printf(" op1r=%08x.%08x", args[0], args[1]);1731printf(" op1i=%08x.%08x", args[2], args[3]);1732printf(" op2r=%08x.%08x", args[4], args[5]);1733printf(" op2i=%08x.%08x", args[6], args[7]);1734break;1735case args1fc:1736case args1fcr:1737printf(" op1r=%08x", args[0]);1738printf(" op1i=%08x", args[2]);1739break;1740case args2fc:1741printf(" op1r=%08x", args[0]);1742printf(" op1i=%08x", args[2]);1743printf(" op2r=%08x", args[4]);1744printf(" op2i=%08x", args[6]);1745break;1746default:1747fprintf(stderr, "internal inconsistency?!\n");1748abort();1749}17501751if (rejected == 2) {1752printf(" - test case rejected\n");1753goto cleanup;1754}17551756wrapper_init(&ctx);17571758if (rejected == 0) {1759switch (fn->type) {1760case args1:1761set_mpfr_d(a, args[0], args[1]);1762wrapper_op_real(&ctx, a, 2, args);1763((testfunc1)(fn->func))(r, a, GMP_RNDN);1764get_mpfr_d(r, &result[0], &result[1], &result[2]);1765wrapper_result_real(&ctx, r, 2, result);1766if (wrapper_run(&ctx, fn->wrappers))1767get_mpfr_d(r, &result[0], &result[1], &result[2]);1768break;1769case args1cr:1770set_mpc_d(ac, args[0], args[1], args[2], args[3]);1771wrapper_op_complex(&ctx, ac, 2, args);1772((testfunc1cr)(fn->func))(r, ac, GMP_RNDN);1773get_mpfr_d(r, &result[0], &result[1], &result[2]);1774wrapper_result_real(&ctx, r, 2, result);1775if (wrapper_run(&ctx, fn->wrappers))1776get_mpfr_d(r, &result[0], &result[1], &result[2]);1777break;1778case args1f:1779set_mpfr_f(a, args[0]);1780wrapper_op_real(&ctx, a, 1, args);1781((testfunc1)(fn->func))(r, a, GMP_RNDN);1782get_mpfr_f(r, &result[0], &result[1]);1783wrapper_result_real(&ctx, r, 1, result);1784if (wrapper_run(&ctx, fn->wrappers))1785get_mpfr_f(r, &result[0], &result[1]);1786break;1787case args1fcr:1788set_mpc_f(ac, args[0], args[2]);1789wrapper_op_complex(&ctx, ac, 1, args);1790((testfunc1cr)(fn->func))(r, ac, GMP_RNDN);1791get_mpfr_f(r, &result[0], &result[1]);1792wrapper_result_real(&ctx, r, 1, result);1793if (wrapper_run(&ctx, fn->wrappers))1794get_mpfr_f(r, &result[0], &result[1]);1795break;1796case args2:1797set_mpfr_d(a, args[0], args[1]);1798wrapper_op_real(&ctx, a, 2, args);1799set_mpfr_d(b, args[2], args[3]);1800wrapper_op_real(&ctx, b, 2, args+2);1801((testfunc2)(fn->func))(r, a, b, GMP_RNDN);1802get_mpfr_d(r, &result[0], &result[1], &result[2]);1803wrapper_result_real(&ctx, r, 2, result);1804if (wrapper_run(&ctx, fn->wrappers))1805get_mpfr_d(r, &result[0], &result[1], &result[2]);1806break;1807case args2f:1808set_mpfr_f(a, args[0]);1809wrapper_op_real(&ctx, a, 1, args);1810set_mpfr_f(b, args[2]);1811wrapper_op_real(&ctx, b, 1, args+2);1812((testfunc2)(fn->func))(r, a, b, GMP_RNDN);1813get_mpfr_f(r, &result[0], &result[1]);1814wrapper_result_real(&ctx, r, 1, result);1815if (wrapper_run(&ctx, fn->wrappers))1816get_mpfr_f(r, &result[0], &result[1]);1817break;1818case rred:1819set_mpfr_d(a, args[0], args[1]);1820wrapper_op_real(&ctx, a, 2, args);1821((testrred)(fn->func))(r, a, (int *)&result[3]);1822get_mpfr_d(r, &result[0], &result[1], &result[2]);1823wrapper_result_real(&ctx, r, 2, result);1824/* We never need to mess about with the integer auxiliary1825* output. */1826if (wrapper_run(&ctx, fn->wrappers))1827get_mpfr_d(r, &result[0], &result[1], &result[2]);1828break;1829case rredf:1830set_mpfr_f(a, args[0]);1831wrapper_op_real(&ctx, a, 1, args);1832((testrred)(fn->func))(r, a, (int *)&result[3]);1833get_mpfr_f(r, &result[0], &result[1]);1834wrapper_result_real(&ctx, r, 1, result);1835/* We never need to mess about with the integer auxiliary1836* output. */1837if (wrapper_run(&ctx, fn->wrappers))1838get_mpfr_f(r, &result[0], &result[1]);1839break;1840case semi1:1841case semi1f:1842errstr = ((testsemi1)(fn->func))(args, result);1843break;1844case semi2:1845case compare:1846errstr = ((testsemi2)(fn->func))(args, args+2, result);1847break;1848case semi2f:1849case comparef:1850case t_ldexpf:1851errstr = ((testsemi2f)(fn->func))(args, args+2, result);1852break;1853case t_ldexp:1854errstr = ((testldexp)(fn->func))(args, args+2, result);1855break;1856case t_frexp:1857errstr = ((testfrexp)(fn->func))(args, result, result+2);1858break;1859case t_frexpf:1860errstr = ((testfrexp)(fn->func))(args, result, result+2);1861break;1862case t_modf:1863errstr = ((testmodf)(fn->func))(args, result, result+2);1864break;1865case t_modff:1866errstr = ((testmodf)(fn->func))(args, result, result+2);1867break;1868case classify:1869errstr = ((testclassify)(fn->func))(args, &result[0]);1870break;1871case classifyf:1872errstr = ((testclassifyf)(fn->func))(args, &result[0]);1873break;1874case args1c:1875set_mpc_d(ac, args[0], args[1], args[2], args[3]);1876wrapper_op_complex(&ctx, ac, 2, args);1877((testfunc1c)(fn->func))(rc, ac, MPC_RNDNN);1878get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);1879wrapper_result_complex(&ctx, rc, 2, result);1880if (wrapper_run(&ctx, fn->wrappers))1881get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);1882break;1883case args2c:1884set_mpc_d(ac, args[0], args[1], args[2], args[3]);1885wrapper_op_complex(&ctx, ac, 2, args);1886set_mpc_d(bc, args[4], args[5], args[6], args[7]);1887wrapper_op_complex(&ctx, bc, 2, args+4);1888((testfunc2c)(fn->func))(rc, ac, bc, MPC_RNDNN);1889get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);1890wrapper_result_complex(&ctx, rc, 2, result);1891if (wrapper_run(&ctx, fn->wrappers))1892get_mpc_d(rc, &result[0], &result[1], &result[2], &result[4], &result[5], &result[6]);1893break;1894case args1fc:1895set_mpc_f(ac, args[0], args[2]);1896wrapper_op_complex(&ctx, ac, 1, args);1897((testfunc1c)(fn->func))(rc, ac, MPC_RNDNN);1898get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);1899wrapper_result_complex(&ctx, rc, 1, result);1900if (wrapper_run(&ctx, fn->wrappers))1901get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);1902break;1903case args2fc:1904set_mpc_f(ac, args[0], args[2]);1905wrapper_op_complex(&ctx, ac, 1, args);1906set_mpc_f(bc, args[4], args[6]);1907wrapper_op_complex(&ctx, bc, 1, args+4);1908((testfunc2c)(fn->func))(rc, ac, bc, MPC_RNDNN);1909get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);1910wrapper_result_complex(&ctx, rc, 1, result);1911if (wrapper_run(&ctx, fn->wrappers))1912get_mpc_f(rc, &result[0], &result[1], &result[4], &result[5]);1913break;1914default:1915fprintf(stderr, "internal inconsistency?!\n");1916abort();1917}1918}19191920switch (fn->type) {1921case args1: /* return an extra-precise result */1922case args2:1923case args1cr:1924case rred:1925printextra = 1;1926if (rejected == 0) {1927errstr = NULL;1928if (!mpfr_zero_p(a)) {1929if ((result[0] & 0x7FFFFFFF) == 0 && result[1] == 0) {1930/*1931* If the output is +0 or -0 apart from the extra1932* precision in result[2], then there's a tricky1933* judgment call about what we require in the1934* output. If we output the extra bits and set1935* errstr="?underflow" then mathtest will tolerate1936* the function under test rounding down to zero1937* _or_ up to the minimum denormal; whereas if we1938* suppress the extra bits and set1939* errstr="underflow", then mathtest will enforce1940* that the function really does underflow to zero.1941*1942* But where to draw the line? It seems clear to1943* me that numbers along the lines of1944* 00000000.00000000.7ff should be treated1945* similarly to 00000000.00000000.801, but on the1946* other hand, we must surely be prepared to1947* enforce a genuine underflow-to-zero in _some_1948* case where the true mathematical output is1949* nonzero but absurdly tiny.1950*1951* I think a reasonable place to draw the1952* distinction is at 00000000.00000000.400, i.e.1953* one quarter of the minimum positive denormal.1954* If a value less than that rounds up to the1955* minimum denormal, that must mean the function1956* under test has managed to make an error of an1957* entire factor of two, and that's something we1958* should fix. Above that, you can misround within1959* the limits of your accuracy bound if you have1960* to.1961*/1962if (result[2] < 0x40000000) {1963/* Total underflow (ERANGE + UFL) is required,1964* and we suppress the extra bits to make1965* mathtest enforce that the output is really1966* zero. */1967errstr = "underflow";1968printextra = 0;1969} else {1970/* Total underflow is not required, but if the1971* function rounds down to zero anyway, then1972* we should be prepared to tolerate it. */1973errstr = "?underflow";1974}1975} else if (!(result[0] & 0x7ff00000)) {1976/*1977* If the output is denormal, we usually expect a1978* UFL exception, warning the user of partial1979* underflow. The exception is if the denormal1980* being returned is just one of the input values,1981* unchanged even in principle. I bodgily handle1982* this by just special-casing the functions in1983* question below.1984*/1985if (!strcmp(fn->name, "fmax") ||1986!strcmp(fn->name, "fmin") ||1987!strcmp(fn->name, "creal") ||1988!strcmp(fn->name, "cimag")) {1989/* no error expected */1990} else {1991errstr = "u";1992}1993} else if ((result[0] & 0x7FFFFFFF) > 0x7FEFFFFF) {1994/*1995* Infinite results are usually due to overflow,1996* but one exception is lgamma of a negative1997* integer.1998*/1999if (!strcmp(fn->name, "lgamma") &&2000(args[0] & 0x80000000) != 0 && /* negative */2001is_dinteger(args)) {2002errstr = "ERANGE status=z";2003} else {2004errstr = "overflow";2005}2006printextra = 0;2007}2008} else {2009/* lgamma(0) is also a pole. */2010if (!strcmp(fn->name, "lgamma")) {2011errstr = "ERANGE status=z";2012printextra = 0;2013}2014}2015}20162017if (!printextra || (rejected && !(rejected==1 && result[2]!=0))) {2018printf(" result=%08x.%08x",2019result[0], result[1]);2020} else {2021printf(" result=%08x.%08x.%03x",2022result[0], result[1], (result[2] >> 20) & 0xFFF);2023}2024if (fn->type == rred) {2025printf(" res2=%08x", result[3]);2026}2027break;2028case args1f:2029case args2f:2030case args1fcr:2031case rredf:2032printextra = 1;2033if (rejected == 0) {2034errstr = NULL;2035if (!mpfr_zero_p(a)) {2036if ((result[0] & 0x7FFFFFFF) == 0) {2037/*2038* Decide whether to print the extra bits based on2039* just how close to zero the number is. See the2040* big comment in the double-precision case for2041* discussion.2042*/2043if (result[1] < 0x40000000) {2044errstr = "underflow";2045printextra = 0;2046} else {2047errstr = "?underflow";2048}2049} else if (!(result[0] & 0x7f800000)) {2050/*2051* Functions which do not report partial overflow2052* are listed here as special cases. (See the2053* corresponding double case above for a fuller2054* comment.)2055*/2056if (!strcmp(fn->name, "fmaxf") ||2057!strcmp(fn->name, "fminf") ||2058!strcmp(fn->name, "crealf") ||2059!strcmp(fn->name, "cimagf")) {2060/* no error expected */2061} else {2062errstr = "u";2063}2064} else if ((result[0] & 0x7FFFFFFF) > 0x7F7FFFFF) {2065/*2066* Infinite results are usually due to overflow,2067* but one exception is lgamma of a negative2068* integer.2069*/2070if (!strcmp(fn->name, "lgammaf") &&2071(args[0] & 0x80000000) != 0 && /* negative */2072is_sinteger(args)) {2073errstr = "ERANGE status=z";2074} else {2075errstr = "overflow";2076}2077printextra = 0;2078}2079} else {2080/* lgamma(0) is also a pole. */2081if (!strcmp(fn->name, "lgammaf")) {2082errstr = "ERANGE status=z";2083printextra = 0;2084}2085}2086}20872088if (!printextra || (rejected && !(rejected==1 && result[1]!=0))) {2089printf(" result=%08x",2090result[0]);2091} else {2092printf(" result=%08x.%03x",2093result[0], (result[1] >> 20) & 0xFFF);2094}2095if (fn->type == rredf) {2096printf(" res2=%08x", result[3]);2097}2098break;2099case semi1: /* return a double result */2100case semi2:2101case t_ldexp:2102printf(" result=%08x.%08x", result[0], result[1]);2103break;2104case semi1f:2105case semi2f:2106case t_ldexpf:2107printf(" result=%08x", result[0]);2108break;2109case t_frexp: /* return double * int */2110printf(" result=%08x.%08x res2=%08x", result[0], result[1],2111result[2]);2112break;2113case t_modf: /* return double * double */2114printf(" result=%08x.%08x res2=%08x.%08x",2115result[0], result[1], result[2], result[3]);2116break;2117case t_modff: /* return float * float */2118/* fall through */2119case t_frexpf: /* return float * int */2120printf(" result=%08x res2=%08x", result[0], result[2]);2121break;2122case classify:2123case classifyf:2124case compare:2125case comparef:2126printf(" result=%x", result[0]);2127break;2128case args1c:2129case args2c:2130if (0/* errstr */) {2131printf(" resultr=%08x.%08x", result[0], result[1]);2132printf(" resulti=%08x.%08x", result[4], result[5]);2133} else {2134printf(" resultr=%08x.%08x.%03x",2135result[0], result[1], (result[2] >> 20) & 0xFFF);2136printf(" resulti=%08x.%08x.%03x",2137result[4], result[5], (result[6] >> 20) & 0xFFF);2138}2139/* Underflow behaviour doesn't seem to be specified for complex arithmetic */2140errstr = "?underflow";2141break;2142case args1fc:2143case args2fc:2144if (0/* errstr */) {2145printf(" resultr=%08x", result[0]);2146printf(" resulti=%08x", result[4]);2147} else {2148printf(" resultr=%08x.%03x",2149result[0], (result[1] >> 20) & 0xFFF);2150printf(" resulti=%08x.%03x",2151result[4], (result[5] >> 20) & 0xFFF);2152}2153/* Underflow behaviour doesn't seem to be specified for complex arithmetic */2154errstr = "?underflow";2155break;2156}21572158if (errstr && *(errstr+1) == '\0') {2159printf(" errno=0 status=%c",*errstr);2160} else if (errstr && *errstr == '?') {2161printf(" maybeerror=%s", errstr+1);2162} else if (errstr && errstr[0] == 'E') {2163printf(" errno=%s", errstr);2164} else {2165printf(" error=%s", errstr && *errstr ? errstr : "0");2166}21672168printf("\n");21692170vet_for_decline(fn, args, result, 0);21712172cleanup:2173mpfr_clear(a);2174mpfr_clear(b);2175mpfr_clear(r);2176mpc_clear(ac);2177mpc_clear(bc);2178mpc_clear(rc);2179}21802181void gencases(Testable *fn, int number) {2182int i;2183uint32 args[8];21842185float32_case(NULL);2186float64_case(NULL);21872188printf("random=on\n"); /* signal to runtests.pl that the following tests are randomly generated */2189for (i = 0; i < number; i++) {2190/* generate test point */2191fn->cases(args, fn->caseparam1, fn->caseparam2);2192docase(fn, args);2193}2194printf("random=off\n");2195}21962197static uint32 doubletop(int x, int scale) {2198int e = 0x412 + scale;2199while (!(x & 0x100000))2200x <<= 1, e--;2201return (e << 20) + x;2202}22032204static uint32 floatval(int x, int scale) {2205int e = 0x95 + scale;2206while (!(x & 0x800000))2207x <<= 1, e--;2208return (e << 23) + x;2209}221022112212