#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fenv.h>
#include <math.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef CORE_MATH_SUPPORT_ERRNO
#include <errno.h>
#endif
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#include <omp.h>
#endif
int ref_init (void);
int ref_fesetround (int);
void cr_sincos (double, double*, double*);
void ref_sincos (double, double*, 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;
}
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 inline double
asfloat64 (uint64_t i)
{
b64u64_u u = {.u = i};
return u.f;
}
#ifdef CORE_MATH_SUPPORT_ERRNO
static inline int
is_nan (double x)
{
uint64_t u = asuint64 (x);
uint64_t e = u >> 52;
return (e == 0x7ff || e == 0xfff) && (u << 12) != 0;
}
#endif
static void
check (double x)
{
int bug;
double s1, c1, s2, c2;
ref_init ();
ref_fesetround (rnd);
ref_sincos (x, &s1, &c1);
fesetround (rnd1[rnd]);
cr_sincos (x, &s2, &c2);
if (isnan (s1))
bug = !isnan (s2);
else if (isnan (s2))
bug = !isnan (s1);
else
bug = asuint64 (s1) != asuint64 (s2);
if (bug)
{
printf ("FAIL x=%la sin: ref=%la z=%la\n", x, s1, s2);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit (1);
#endif
}
if (isnan (c1))
bug = !isnan (c2);
else if (isnan (c2))
bug = !isnan (c1);
else
bug = asuint64 (c1) != asuint64 (c2);
if (bug)
{
printf ("FAIL x=%la cos: ref=%la z=%la\n", x, c1, c2);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit (1);
#endif
}
}
static void
check_invalid (uint64_t seed)
{
double inf = asfloat64 (0x7ff0000000000000ull);
double minInf = asfloat64 (0xfff0000000000000ull);
double sNan = asfloat64 (0x7ff0000000000000ull + seed);
double minsNan = asfloat64 (0xfff0000000000000ull + seed);
double qNan = asfloat64 (0x7ff8000000000000ull + seed);
double minqNan = asfloat64 (0xfff8000000000000ull + seed);
double s, c;
feclearexcept (FE_INVALID);
cr_sincos (inf, &s, &c);
int flag = fetestexcept (FE_INVALID);
if (!flag)
{
printf ("Missing invalid exception for x=Inf\n");
#ifndef DO_NOT_ABORT
exit (1);
#endif
}
feclearexcept (FE_INVALID);
cr_sincos (minInf, &s, &c);
flag = fetestexcept (FE_INVALID);
if (!flag)
{
printf ("Missing invalid exception for x=-Inf\n");
#ifndef DO_NOT_ABORT
exit (1);
#endif
}
feclearexcept (FE_INVALID);
cr_sincos (sNan, &s, &c);
flag = fetestexcept (FE_INVALID);
if (!flag)
{
printf ("Missing invalid exception for x=sNaN[%lx]\n", asuint64 (sNan));
#ifndef DO_NOT_ABORT
exit (1);
#endif
}
#ifdef CORE_MATH_SUPPORT_ERRNO
int expected_edom = !is_nan (sNan) && (is_nan (s) || is_nan (c));
if (expected_edom && errno != EDOM)
{
printf ("Missing errno=EDOM for x=%la (s=%la) (c=%la)\n", sNan, s, c);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit(1);
#endif
}
#endif
feclearexcept (FE_INVALID);
cr_sincos (minsNan, &s, &c);
flag = fetestexcept (FE_INVALID);
if (!flag)
{
printf ("Missing invalid exception for x=-sNaN\n");
#ifndef DO_NOT_ABORT
exit (1);
#endif
}
#ifdef CORE_MATH_SUPPORT_ERRNO
expected_edom = !is_nan (minsNan) && (is_nan (s) || is_nan (c));
if (expected_edom && errno != EDOM)
{
printf ("Missing errno=EDOM for x=%la (s=%la) (c=%la)\n", minsNan, s, c);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit(1);
#endif
}
#endif
feclearexcept (FE_INVALID);
cr_sincos (qNan, &s, &c);
flag = fetestexcept (FE_INVALID);
if (flag)
{
printf ("Spurious invalid exception for x=qNaN[%lx]\n", asuint64 (qNan));
#ifndef DO_NOT_ABORT
exit (1);
#endif
}
#ifdef CORE_MATH_SUPPORT_ERRNO
expected_edom = !is_nan (qNan) && (is_nan (s) || is_nan (c));
if (expected_edom && errno != EDOM)
{
printf ("Missing errno=EDOM for x=%la (s=%la) (c=%la)\n", qNan, s, c);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit(1);
#endif
}
#endif
feclearexcept (FE_INVALID);
cr_sincos (minqNan, &s, &c);
flag = fetestexcept (FE_INVALID);
if (flag)
{
printf ("Missing invalid exception for x=-qNaN\n");
#ifndef DO_NOT_ABORT
exit (1);
#endif
}
#ifdef CORE_MATH_SUPPORT_ERRNO
expected_edom = !is_nan (minqNan) && (is_nan (s) || is_nan (c));
if (expected_edom && errno != EDOM)
{
printf ("Missing errno=EDOM for x=%la (s=%la) (c=%la)\n", minqNan, s, c);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit(1);
#endif
}
#endif
}
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);
check_invalid (0);
check_invalid (1);
check_invalid (0x7ffffffffffffull);
#define N 1000000000UL
unsigned int seed = getpid ();
srand (seed);
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for
#endif
for (uint64_t n = 0; n < N; n++)
{
double x;
int tid;
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
tid = omp_get_thread_num ();
#else
tid = 0;
#endif
x = get_random (tid);
check (x);
}
return 0;
}