#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fenv.h>
#include <math.h>
#include <unistd.h>
#include <mpfr.h>
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#include <omp.h>
#endif
int ref_fesetround (int);
void ref_init (void);
double cr_exp2 (double);
double ref_exp2 (double);
int rnd1[] = { FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD, FE_DOWNWARD };
int rnd = 0;
int verbose = 0;
#define MAX_THREADS 192
static unsigned int Seed[MAX_THREADS];
static inline uint64_t
asuint64 (double f)
{
union
{
double f;
uint64_t i;
} u = {f};
return u.i;
}
static inline int
is_nan (double x)
{
uint64_t u = asuint64 (x);
int e = u >> 52;
return (e == 0x7ff || e == 0xfff) && (u << 12) != 0;
}
static inline int
is_equal (double x, double y)
{
if (is_nan (x))
return is_nan (y);
if (is_nan (y))
return is_nan (x);
return asuint64 (x) == asuint64 (y);
}
static void
check (double x)
{
mpfr_flags_clear (MPFR_FLAGS_INEXACT);
double y1 = ref_exp2 (x);
#ifdef CORE_MATH_CHECK_INEXACT
mpfr_flags_t inex1 = mpfr_flags_test (MPFR_FLAGS_INEXACT);
#endif
fesetround (rnd1[rnd]);
feclearexcept (FE_INEXACT);
double y2 = cr_exp2 (x);
#ifdef CORE_MATH_CHECK_INEXACT
int inex2 = fetestexcept (FE_INEXACT);
#endif
if (! is_equal (y1, y2))
{
printf ("FAIL x=%la ref=%la z=%la\n", x, y1, y2);
fflush (stdout);
exit (1);
}
#ifdef CORE_MATH_CHECK_INEXACT
if ((inex1 == 0) && (inex2 != 0))
{
printf ("Spurious inexact exception for x=%la (y=%la)\n", x, y1);
fflush (stdout);
#ifdef DO_NOT_ABORT
return 1;
#else
exit(1);
#endif
}
if ((inex1 != 0) && (inex2 == 0))
{
printf ("Missing inexact exception for x=%la (y=%la)\n", x, y1);
fflush (stdout);
#ifdef DO_NOT_ABORT
return 1;
#else
exit(1);
#endif
}
#endif
}
typedef union {double f; uint64_t u;} b64u64_u;
static double
get_random (int tid)
{
b64u64_u v;
v.u = rand_r (Seed + tid);
v.u |= (uint64_t) rand_r (Seed + tid) << 31;
v.u |= (uint64_t) rand_r (Seed + tid) << 62;
return v.f;
}
static void
check_exact (void)
{
for (int i = -1074; i <= 1023; i++)
check ((double) i);
}
int
main (int argc, char *argv[])
{
while (argc >= 2)
{
if (strcmp (argv[1], "--rndn") == 0)
{
rnd = 0;
argc --;
argv ++;
}
else if (strcmp (argv[1], "--rndz") == 0)
{
rnd = 1;
argc --;
argv ++;
}
else if (strcmp (argv[1], "--rndu") == 0)
{
rnd = 2;
argc --;
argv ++;
}
else if (strcmp (argv[1], "--rndd") == 0)
{
rnd = 3;
argc --;
argv ++;
}
else if (strcmp (argv[1], "--verbose") == 0)
{
verbose = 1;
argc --;
argv ++;
}
else
{
fprintf (stderr, "Error, unknown option %s\n", argv[1]);
exit (1);
}
}
ref_init();
ref_fesetround (rnd);
printf ("Checking exact results\n");
check_exact ();
#ifndef CORE_MATH_TESTS
#define CORE_MATH_TESTS 1000000000UL
#endif
printf ("Checking results in subnormal range\n");
double x0 = -1075;
double x1 = -1024;
int64_t n0 = ldexp (x0, 42);
int64_t n1 = ldexp (x1, 42);
int64_t skip = (n1 - n0) / CORE_MATH_TESTS + 1;
skip = 10 * skip + 1; n0 += getpid () % skip;
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for
#endif
for (int64_t n = n0; n < n1; n += skip)
check (ldexp ((double) n, -42));
double x2 = -1022;
n1 = ldexp (x1, 43);
int64_t n2 = ldexp (x2, 43);
skip = (n2 - n1) / CORE_MATH_TESTS + 1;
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for
#endif
for (int64_t n = n1; n < n2; n += skip)
check (ldexp ((double) n, -43));
printf ("Checking random values\n");
unsigned int seed = getpid ();
for (int i = 0; i < MAX_THREADS; i++)
Seed[i] = seed + i;
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for
#endif
for (uint64_t n = 0; n < CORE_MATH_TESTS; n++)
{
ref_init ();
ref_fesetround (rnd);
int tid;
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
tid = omp_get_thread_num ();
#else
tid = 0;
#endif
double x = get_random (tid);
check (x);
}
return 0;
}