#define SUBNORMAL
#include <stdio.h>
#include <stdint.h>
#include <mpfr.h>
#include <math.h>
#include <assert.h>
#include <omp.h>
double minerr = 1;
unsigned long count = 0;
typedef union {__bf16 f; uint16_t u;} b16u16_u;
static int
compare (__bf16 x, __bf16 y, int e)
{
mpfr_t X, Y;
mpfr_init2 (X, 8);
mpfr_init2 (Y, 8);
mpfr_set_flt (X, (float) x, MPFR_RNDN);
mpfr_set_flt (Y, (float) y, MPFR_RNDN);
mpfr_pow (X, X, Y, MPFR_RNDZ);
mpfr_set_ui (Y, 1, MPFR_RNDZ);
mpfr_mul_2si (Y, Y, e, MPFR_RNDZ);
int ret = mpfr_cmp (X, Y);
mpfr_clear (X);
mpfr_clear (Y);
return ret;
}
static double
get_err (__bf16 x, __bf16 y)
{
mpfr_t X, Y;
mpfr_init2 (X, 255);
mpfr_init2 (Y, 9);
mpfr_set_flt (X, (float) x, MPFR_RNDN);
mpfr_set_flt (Y, (float) y, MPFR_RNDN);
int inex = mpfr_pow (X, X, Y, MPFR_RNDN);
inex = mpfr_set (Y, X, MPFR_RNDN) | inex; #ifdef IGNORE_ONE
if (mpfr_cmp_ui (Y, 1) == 0) inex = 0;
#endif
mpfr_sub (X, X, Y, MPFR_RNDN);
mpfr_abs (X, X, MPFR_RNDN);
assert (!mpfr_zero_p (Y));
mpfr_exp_t ey = mpfr_get_exp (Y);
#ifdef SUBNORMAL
if (ey <= -133 || -125 <= ey)
inex = 0;
double u = 0x1p-133;
#else
double u = pow (2.0, ey - 8);
#endif
double ret = mpfr_get_d (X, MPFR_RNDN) / u;
mpfr_clear (X);
mpfr_clear (Y);
return (inex == 0) ? 0 : ret;
}
static void
doit (__bf16 y)
{
b16u16_u x0, x1, a, b;
if (fabs (y) <= 1) {
x0.f = 0x1p-133f;
x1.f = 0x1.fep+127f;
} else if (y > 0) { // y > 0: x^y is increasing
// determine x0 by binary search
a.f = 0x1p-133f;
b.f = 0x1.fep+127f;
while (a.u + 1 < b.u) {
b16u16_u c = {.u = (a.u + b.u) / 2};
if (compare (c.f, y, -134) >= 0) // c^y >= 2^-134
b.f = c.f;
else // c^y < 2^-134
a.f = c.f;
}
x0.f = b.f; // for all y > 1, (2^-133)^y < 2^-134
// determine x1 by binary search
a.f = 0x1p-133f;
b.f = 0x1.fep+127f;
while (a.u + 1 < b.u) {
b16u16_u c = {.u = (a.u + b.u) / 2};
if (compare (c.f, y, 128) >= 0) // c^y > 2^128
b.f = c.f;
else // c^y <= 2^128
a.f = c.f;
}
x1.f = a.f;
} else { // case y < 0: x^y is decreasing
// determine x1 by binary search
a.f = 0x1p-133f;
b.f = 0x1.fep+127f;
while (a.u + 1 < b.u) {
b16u16_u c = {.u = (a.u + b.u) / 2};
if (compare (c.f, y, -134) >= 0) // c^y >= 2^-134
a.f = c.f;
else // c^y < 2^-134
b.f = c.f;
}
x1.f = a.f;
// determine x0 by binary search
a.f = 0x1p-133f;
b.f = 0x1.fep+127f;
while (a.u + 1 < b.u) {
b16u16_u c = {.u = (a.u + b.u) / 2};
if (compare (c.f, y, 128) > 0) // c^y > 2^128
a.f = c.f;
else // c^y <= 2^128
b.f = c.f;
}
x0.f = a.f;
}
if (x1.u < x0.u) printf ("y=%a x0=%a x1=%a\n", (float) y, (float) x0.f, (float) x1.f);
assert (x0.u <= x1.u);
#pragma omp atomic update
count += x1.u - x0.u + 1;
#pragma omp parallel for
for (uint16_t ux = x0.u; ux <= x1.u; ux++) {
b16u16_u x = {.u = ux};
double err = get_err (x.f, y);
#pragma omp critical
if (err != 0 && err < minerr)
printf ("x=%a y=%a err=%e\n", (float) x.f, (float) y, minerr = err);
}
}
int
main()
{
for (uint16_t uy = 1; uy < 0x7f80; uy++) {
b16u16_u v = {.u = uy};
__bf16 y = v.f;
doit (y);
doit (-y);
}
printf ("count=%lu\n", count);
}