#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <math.h>
#include <fenv.h>
#include <assert.h>
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#include <omp.h>
#endif
#include <mpfr.h>
float cr_hypotf (float, float);
float ref_hypot (float, float);
int ref_fesetround (int);
void ref_init (void);
extern int rnd1[];
extern int rnd;
extern int verbose;
static inline uint32_t
asuint (float f)
{
union
{
float f;
uint32_t i;
} u = {f};
return u.i;
}
static inline int
is_nan (double x)
{
uint32_t u = asuint (x);
int e = u >> 23;
return (e == 0xff || e == 0x1ff) && (u << 9) != 0;
}
static inline int
is_equal (float x, float y)
{
if (is_nan (x))
return is_nan (y);
if (is_nan (y))
return is_nan (x);
return asuint (x) == asuint (y);
}
static void
check_aux (float x, float y)
{
float z1, z2;
ref_init();
ref_fesetround(rnd);
mpfr_flags_clear (MPFR_FLAGS_INEXACT);
z1 = ref_hypot(x, y);
#ifdef CORE_MATH_CHECK_INEXACT
mpfr_flags_t inex1 = mpfr_flags_test (MPFR_FLAGS_INEXACT);
#endif
fesetround(rnd1[rnd]);
feclearexcept (FE_INEXACT);
z2 = cr_hypotf(x, y);
#ifdef CORE_MATH_CHECK_INEXACT
int inex2 = fetestexcept (FE_INEXACT);
#endif
if (!is_equal (z1, z2)) {
printf("FAIL x=%a y=%a ref=%a z=%a\n", x, y, z1, z2);
fflush(stdout);
exit(1);
}
#ifdef CORE_MATH_CHECK_INEXACT
if ((inex1 == 0) && (inex2 != 0))
{
printf ("Spurious inexact exception for x=%a y=%a\n", x, y);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit(1);
#endif
}
if ((inex1 != 0) && (inex2 == 0))
{
printf ("Missing inexact exception for x=%a y=%a\n", x, y);
fflush (stdout);
#ifndef DO_NOT_ABORT
exit(1);
#endif
}
#endif
}
void
check (float x, float y)
{
check_aux (x, y);
check_aux (x, -y);
check_aux (-x, y);
check_aux (-x, -y);
check_aux (y, x);
check_aux (y, -x);
check_aux (-y, x);
check_aux (-y, -x);
}
static int
valid (uint64_t x, uint64_t y, uint64_t z, int k)
{
uint64_t m = x >> k;
if (x != (m << k))
return 0;
if (m < 0x800000 || 0x1000000 <= m)
return 0;
if (y < 0x800000 || 0x1000000 <= y)
return 0;
assert (z > 0);
int e = __builtin_ctz (z);
z = z >> e;
int ret = z < 0x2000000;
return ret;
}
uint64_t
gcd (uint64_t a, uint64_t b)
{
while (b != 0)
{
uint64_t r = a % b;
a = b;
b = r;
}
return a;
}
static uint64_t
generate1 (uint64_t p, uint64_t q, int k)
{
if (gcd (p, q) != 1)
return 0;
uint64_t count = 0;
assert (p > q);
uint64_t x = p * p - q * q;
uint64_t y = 2 * p * q;
uint64_t z = p * p + q * q;
uint64_t xmax = 0xffffffull << k;
uint64_t ymax = 0xffffffull;
for (uint64_t j = 1; ; j++)
{
uint64_t xj = j * x;
uint64_t yj = j * y;
uint64_t zj = j * z;
if (xj > xmax)
break;
if (yj > ymax)
break;
if (valid (xj, yj, zj, k))
{
check (xj, yj);
count ++;
}
}
return count;
}
static uint64_t
generate2 (uint64_t p, uint64_t q, int k)
{
if (gcd (p, q) != 1)
return 0;
uint64_t count = 0;
assert (p > q);
uint64_t x = 2 * p * q;
uint64_t y = p * p - q * q;
uint64_t z = p * p + q * q;
uint64_t xmax = 0xffffffull << k;
uint64_t ymax = 0xffffffull;
for (uint64_t j = 1; ; j++)
{
uint64_t xj = j * x;
uint64_t yj = j * y;
uint64_t zj = j * z;
if (xj > xmax)
break;
if (yj > ymax)
break;
if (valid (xj, yj, zj, k))
{
check (xj, yj);
count ++;
}
}
return count;
}
#ifndef CORE_MATH_TESTS
#define CORE_MATH_TESTS 1000000000ul
#endif
static void
check_pythagorean_triples (int k)
{
uint64_t p, q;
uint64_t count1 = 0, count2 = 0;
if (verbose)
fprintf (stderr, "# k=%d\n", k);
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for reduction(+: count1)
#endif
for (q = 1; q <= 2895; q++)
for (p = q + 1; 2 * p * q < 0x1000000ull; p+=2)
{
if (count1 < CORE_MATH_TESTS / 2)
count1 += generate1 (p, q, k);
}
if (verbose)
fprintf (stderr, "# Type 1: %"PRIu64"\n", count1);
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for reduction(+: count2)
#endif
for (p = 2; p <= 0x800000; p++)
{
uint64_t qmin = 1;
if (p * p < 0x1000000ull)
q = 0;
else
{
q = sqrt (p * p - 0x1000000ull);
while (q * q <= p * p - 0x1000000ull)
q ++;
}
if (qmin < q)
qmin = q;
if ((p + qmin) % 2 == 0)
qmin ++;
for (q = qmin; q < p; q += 2)
if (count2 < CORE_MATH_TESTS / 2)
count2 += generate2 (p, q, k);
}
if (verbose) {
fprintf (stderr, "# Type 2: %"PRIu64"\n", count2);
fprintf (stderr, "# Total: %"PRIu64"\n", count1 + count2);
}
}
void
doloop (int k0, int k1)
{
ref_init ();
ref_fesetround (rnd);
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for
#endif
for (int k = k0; k <= k1; k++)
check_pythagorean_triples (k);
}