#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fenv.h>
#include <math.h>
#include <gmp.h>
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#include <omp.h>
#endif
#include <assert.h>
int ref_fesetround (int);
void ref_init (void);
extern void doloop (int, int);
extern void check (float, float);
extern uint64_t gcd (uint64_t, uint64_t);
float cr_hypotf (float, float);
extern void doit_subnormal_above (uint32_t);
int rnd1[] = { FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD, FE_DOWNWARD };
int rnd = 0;
int verbose = 0;
static void
check_triples_subnormal (void)
{
uint64_t r, s, x, y, z;
for (r = 1; r <= 2896; r += 2)
for (s = 2; s < r; s += 2)
{
if (gcd (r, s) == 1)
{
x = r * r - s * s;
y = 2 * r * s;
z = r * r + s * s;
if (z > 0x7fffff)
break;
for (int n = 1; ; n++)
{
uint64_t nn = n * n;
uint64_t xx = x * nn, yy = y * nn, zz = z * nn;
if (zz > 0x7fffff)
break;
check (ldexpf (xx, -149), ldexpf (yy, -149));
}
}
}
for (r = 2; r <= 2896; r += 2)
for (s = 1; s < r; s += 2)
{
if (gcd (r, s) == 1)
{
x = r * r - s * s;
y = 2 * r * s;
z = r * r + s * s;
if (z > 0x7fffff)
break;
for (int n = 1; ; n++)
{
uint64_t nn = n * n;
uint64_t xx = x * nn, yy = y * nn, zz = z * nn;
if (zz > 0x7fffff)
break;
check (ldexpf (xx, -149), ldexpf (yy, -149));
}
}
}
}
static void
check_triples_subnormal_above (void)
{
doit_subnormal_above (8388608);
}
typedef union {float f; uint32_t u;} b32u32_u;
static float
get_random (void)
{
b32u32_u v;
int64_t l;
l = rand ();
v.u = l;
l = rand ();
v.u |= (uint32_t) l << 31;
return v.f;
}
#ifndef CORE_MATH_TESTS
#define CORE_MATH_TESTS 1000000000ul
#endif
static void
check_random (int seed, int nthreads)
{
ref_init ();
ref_fesetround (rnd);
fesetround(rnd1[rnd]);
float x, y;
srand (seed);
for (uint64_t n = 0; n < CORE_MATH_TESTS; n += nthreads)
{
x = get_random ();
y = get_random ();
check (x, y);
}
}
static void
check_random_all (void)
{
int nthreads = 1;
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel
nthreads = omp_get_num_threads ();
#pragma omp parallel for
#endif
for (int i = 0; i < nthreads; i++)
check_random (getpid () + i, nthreads);
}
static int
is_near_square (uint64_t t)
{
t ++;
uint64_t k = (uint64_t) sqrt ((double) t);
uint64_t r;
if (k * k > t)
{
k--;
assert (k * k <= t);
r = t - k * k;
}
else
r = t - k * k;
assert (r <= 2 * k);
return r <= 2;
}
#define LIMIT 0x1000000ull
static void
check_near_exact (void)
{
uint64_t nx = CORE_MATH_TESTS / LIMIT; if (nx == 0)
nx = 1;
nx = nx * 20000; uint64_t skip = (nx > LIMIT) ? 1 : LIMIT / nx;
uint64_t x0 = 2 + (getpid () % skip);
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for schedule(dynamic,1)
#endif
for (uint64_t x = x0; x < LIMIT; x += skip)
{
for (uint64_t y = 2; y <= x; y++)
{
uint64_t t = x * x + y * y;
if (is_near_square (t))
{
check ((float) x, (float) y);
check (ldexpf ((float) x, -149), ldexpf ((float) y, -149));
}
}
}
}
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);
}
}
printf ("Checking near-exact values\n");
fflush (stdout);
check_near_exact ();
printf ("Checking random values\n");
fflush (stdout);
check_random_all ();
printf ("Checking near-exact subnormal values\n");
fflush (stdout);
check_triples_subnormal_above ();
printf ("Checking exact subnormal values\n");
fflush (stdout);
check_triples_subnormal ();
printf ("Checking Pythagorean triples\n");
fflush (stdout);
doloop(0, 12);
return 0;
}