#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fenv.h>
#include <mpfr.h>
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#include <omp.h>
#endif
#include <unistd.h>
#include <math.h>
#include <assert.h>
#ifndef CORE_MATH_TESTS
#define CORE_MATH_TESTS 1000000000UL
#endif
void doloop (int, int);
extern long double cr_atan2l (long double, long double);
extern int ref_fesetround (int);
extern void ref_init (void);
extern mpfr_rnd_t rnd2[];
extern long double ref_atan2l (long double, long double);
int rnd1[] = { FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD, FE_DOWNWARD };
int rnd;
int verbose = 0;
typedef union {long double f; struct {uint64_t m; uint16_t e;};} b80u80_t;
#define MAX_THREADS 192
static unsigned int Seed[MAX_THREADS];
static long double
get_random (int tid)
{
b80u80_t v;
v.m = rand_r (Seed + tid);
v.m |= (uint64_t) rand_r (Seed + tid) << 31;
v.m |= (uint64_t) (rand_r (Seed + tid) & 1) << 62;
v.e = rand_r (Seed + tid) & 0xffff;
uint64_t t = (v.e & 0x7fff) != 0;
v.m |= t << 63;
return v.f;
}
static inline int
is_nan (long double x)
{
b80u80_t v = {.f = x};
return ((v.e == 0x7fff || v.e == 0xffff) && (v.m != (1ull << 63)));
}
static inline int
is_equal (long double x, long double y)
{
if (is_nan (x))
return is_nan (y);
if (is_nan (y))
return is_nan (x);
b80u80_t v = {.f = x}, w = {.f = y};
return v.e == w.e && v.m == w.m;
}
static int
check_aux (long double x, long double y)
{
long double z, t;
mpfr_t X, Y, Z;
ref_init();
ref_fesetround(rnd);
mpfr_init2 (X, 64);
mpfr_init2 (Y, 64);
mpfr_init2 (Z, 64);
mpfr_set_ld (X, x, MPFR_RNDN);
mpfr_set_ld (Y, y, MPFR_RNDN);
mpfr_flags_clear (MPFR_FLAGS_INEXACT);
t = ref_atan2l (x, y);
mpfr_flags_t inex1 = mpfr_flags_test (MPFR_FLAGS_INEXACT);
fesetround(rnd1[rnd]);
feclearexcept (FE_INEXACT);
z = cr_atan2l (x, y);
int inex2 = fetestexcept (FE_INEXACT);
mpfr_clear (X);
mpfr_clear (Y);
mpfr_clear (Z);
if (!is_equal (z, t))
{
printf("FAIL x,y=%La,%La ref=%La z=%La\n", x,y,t,z);
#ifdef DO_NOT_ABORT
return 1;
#else
exit(1);
#endif
}
if ((inex1 == 0) && (inex2 != 0))
{
printf ("Spurious inexact exception for x,y=%La,%La (z=%La)\n", x, y, z);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit(1);
#endif
}
if ((inex1 != 0) && (inex2 == 0))
{
printf ("Missing inexact exception for x,y=%La,%La (z=%La)\n", x, y, z);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit(1);
#endif
}
return 0;
}
static int
check (long double x, long double y)
{
int ret = check_aux (x, y);
if (y == (long double) (int64_t) y)
ret += check_aux (-x, y);
return ret;
}
static void
check_random (void)
{
#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);
fesetround(rnd1[rnd]);
int tid;
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
tid = omp_get_thread_num ();
#else
tid = 0;
#endif
long double x = get_random(tid), y = get_random(tid);
check(x, y);
}
}
static void
check_subnormal (void)
{
#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);
fesetround(rnd1[rnd]);
int tid;
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
tid = omp_get_thread_num ();
#else
tid = 0;
#endif
long double x = get_random(tid), y = get_random(tid);
int e = -16382 - (rand_r (Seed + tid) % 64); b80u80_t v;
v.f = x;
v.m |= 1ul << 63;
v.e = (v.e & 0x8000) | ((e/2) + 0x3fff);
x = v.f;
v.f = y;
v.m |= 1ul << 63;
v.e = (v.e & 0x8000) | (((e/2)-e) + 0x3fff);
y = v.f;
check(x, y);
}
}
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);
fesetround(rnd1[rnd]);
unsigned int seed = getpid ();
for (int i = 0; i < MAX_THREADS; i++)
Seed[i] = seed + i;
printf ("Checking random subnormal values\n");
check_subnormal ();
printf ("Checking random values\n");
check_random ();
return 0;
}