#include <stdio.h>
#include <stdint.h>
#include <mpfr.h>
#include <fenv.h>
#include <math.h>
#include <float.h>
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#include <omp.h>
#endif
#include <stdbool.h>
#include <assert.h>
#ifndef MODE
#error "You should define MODE (0, 1, or 2)"
#endif
typedef union {long double f; struct {uint64_t m; uint16_t e;};} b80u80_t;
#define POWL_DPRINTF(x,y,z)
#include "qint.h"
#include "powl_tables.h"
static int
is_possible (int i1, int i2)
{
assert (0 <= i1 && i1 < 128);
double xmin = 1.0 + (double) i1 * 0x1p-7;
double xmax = 1.0 + (double) (i1 + 1) * 0x1p-7;
double r1 = coarse[i1].r;
xmin = r1 * xmin;
xmax = r1 * xmax;
assert ((0 <= i2 && i2 < 32) || (32 <= i2 && i2 < 128));
double ymin, ymax;
if (i2 < 32)
{
ymin = 1.0 + (double) i2 * 0x1p-12;
ymax = 1.0 + (double) (i2 + 1) * 0x1p-12;
}
else
{
ymin = 1.0 - 0x1p-7 + (double) (i2 - 64) * 0x1p-13;
ymax = 1.0 - 0x1p-7 + (double) (i2 + 1 - 64) * 0x1p-13;
}
return (xmax <= ymin || ymax <= xmin) ? 0 : 1;
}
static inline
void fast_two_sum(double* rh, double* rl, double a, double b) {
*rh = a + b;
double e = *rh - a;
*rl = b - e;
}
static inline
void high_sum(double* rh, double* rl, double a, double bh, double bl) {
double e;
fast_two_sum(rh, &e, a, bh);
*rl = bl + e;
}
static double
compute_error (int extra_int, int i1, int i2, double mlogr12h, double mlogr12l)
{
mpfr_t x, y;
mpfr_init2 (x, 127);
mpfr_init2 (y, 127);
mpfr_set_si (x, extra_int, MPFR_RNDN);
mpfr_set_d (y, coarse[i1].r, MPFR_RNDN);
if (coarse[i1].z)
mpfr_mul_2ui (y, y, 1, MPFR_RNDN);
mpfr_log2 (y, y, MPFR_RNDN);
mpfr_sub (x, x, y, MPFR_RNDN);
mpfr_set_d (y, fine[i2].r, MPFR_RNDN);
mpfr_log2 (y, y, MPFR_RNDN);
mpfr_sub (x, x, y, MPFR_RNDN);
mpfr_set_d (y, mlogr12h, MPFR_RNDN);
mpfr_add_d (y, y, mlogr12l, MPFR_RNDN);
mpfr_sub (x, x, y, MPFR_RNDN);
mpfr_abs (x, x, MPFR_RNDN);
double err = mpfr_get_d (x, MPFR_RNDN);
mpfr_clear (x);
mpfr_clear (y);
return err / mlogr12h;
}
static double get_ratio (double a, double b)
{
a = fabs (a);
b = fabs (b);
int r = fegetround ();
fesetround (FE_UPWARD);
double ratio = a / b;
fesetround (r);
return ratio;
}
static void
analyze_second_sum (void)
{
double maxerr = 0, maxratio1 = 0, maxratio2 = 0, maxratio3 = 0;
double min_mlogr12h = DBL_MAX;
static int R[4] = {FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD, FE_DOWNWARD};
for (int r = 0; r < 4; r++)
{
fesetround (R[r]);
#if MODE==1
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for schedule(static,1)
#endif
#endif
for (int extra_int = -16382; extra_int < 16384; extra_int ++)
for (int i1 = 0; i1 < 128; i1++)
{
double mlogr1h = coarse[i1].mlogrh;
double mlogr1l = coarse[i1].mlogrl;
double mlogrh, mlogrl;
high_sum(&mlogrh, &mlogrl, (double) extra_int, mlogr1h, mlogr1l);
for (int i2 = 0; i2 < 128; i2++)
{
if (32 <= i2 && i2 < 64)
continue;
if (!is_possible (i1, i2))
continue;
double mlogr2h = fine[i2].mlogrh;
double mlogr2l = fine[i2].mlogrl;
double mlogr12h, mlogr12l;
high_sum(&mlogr12h, &mlogr12l, mlogrh, mlogr2h, mlogr2l);
mlogr12l += mlogrl;
#if MODE==0
double ratio1 = get_ratio (mlogrh, mlogr12h);
double ratio2 = get_ratio (mlogr2h, mlogr12h);
double ratio3 = get_ratio (mlogr1h, mlogr12h);
if (ratio1 > maxratio1)
printf ("r=%d extra_int=%d i1=%d i2=%d |mlogrh/mlogr12h|=%la\n",
r, extra_int, i1, i2, maxratio1 = ratio1);
if (ratio2 > maxratio2)
printf ("r=%d extra_int=%d i1=%d i2=%d |mlogr2h/mlogr12h|=%la\n",
r, extra_int, i1, i2, maxratio2 = ratio2);
if (ratio3 > maxratio3)
printf ("r=%d extra_int=%d i1=%d i2=%d |mlogr1h/mlogr12h|=%la\n",
r, extra_int, i1, i2, maxratio3 = ratio3);
fflush (stdout);
#elif MODE==1
double err = compute_error (extra_int, i1, i2, mlogr12h, mlogr12l);
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp critical
#endif
if (err > maxerr)
{
printf ("r=%d extra_int=%d i1=%d i2=%d err=%e\n",
r, extra_int, i1, i2, maxerr = err);
}
#elif MODE==2
double ratio1 = fabs (mlogr12l / mlogr12h);
if (ratio1 > maxratio1)
printf ("r=%d extra_int=%d i1=%d i2=%d |mlogr12l/mlogr12h|=%.16e\n",
r, extra_int, i1, i2, maxratio1 = ratio1);
#elif MODE==3
fast_two_sum(&mlogr12h, &mlogr12l, mlogr12h, mlogr12l);
if (mlogr12h != 0 && fabs (mlogr12h) < min_mlogr12h)
printf ("r=%d extra_int=%d i1=%d i2=%d |mlogr12h|=%la\n",
r, extra_int, i1, i2, min_mlogr12h = fabs (mlogr12h));
#else
#error "Invalid value of MODE"
#endif
}
}
}
}
static void
count_possible (void)
{
int possible = 0;
for (int i1 = 0; i1 < 128; i1++)
for (int i2 = 0; i2 < 128; i2++)
if (i2 < 32 || 64 <= i2)
possible += is_possible (i1, i2);
printf ("Number of possible (i1,i2) combinations: %d\n", possible);
}
int
main ()
{
count_possible ();
analyze_second_sum ();
return 0;
}