#include <stdint.h>
#include <fenv.h>
#include <stdbool.h>
#ifdef CORE_MATH_SUPPORT_ERRNO
#include <errno.h>
#endif
#ifndef CORE_MATH_FAIL_QUIET
#include <stdio.h>
#include <stdlib.h>
#endif
#ifdef __x86_64__
#include <x86intrin.h>
#endif
#if defined(__x86_64__)
#define FLAG_T uint32_t
#else
#define FLAG_T fexcept_t
#endif
static FLAG_T
get_flag (void)
{
#if defined(__x86_64__)
return _mm_getcsr ();
#else
fexcept_t flag;
fegetexceptflag (&flag, FE_ALL_EXCEPT);
return flag;
#endif
}
static void
set_flag (FLAG_T flag)
{
#if defined(__x86_64__)
_mm_setcsr (flag);
#else
fesetexceptflag (&flag, FE_ALL_EXCEPT);
#endif
}
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif
#pragma STDC FENV_ACCESS ON
typedef union {long double f; struct {uint64_t m; uint16_t e;};} b80u80_t;
typedef union {
double f;
struct __attribute__((packed)) {uint64_t m:52;uint32_t e:11;uint32_t s:1;};
uint64_t u;
} b64u64_t;
#include "qint.h"
static inline int get_rounding_mode (void)
{
#if defined(__x86_64__)
#if defined(__WIN32__) || defined(__WIN64__)
#if FE_UPWARD == 0x0200
return _MM_GET_ROUNDING_MODE()>>5;
#elif FE_UPWARD == 0x0100
static const unsigned lut[4] = {FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO};
return lut[_MM_GET_ROUNDING_MODE()>>13];
#else
#warning The floating point rounding constants have an unknown value. A slower path will be taken.
return fegetround();
#endif
#else
return _MM_GET_ROUNDING_MODE()>>3;
#endif
#else
return fegetround ();
#endif
}
static inline
void split(double* rh, double* rl, long double x) {
b80u80_t cvt_x; cvt_x.f = x;
b64u64_t cvt_w;
cvt_w.u = (0x3f4ull << (64 - 12)) | (cvt_x.m & ((1ull << 31) - 1ull));
*rl = cvt_w.f - 0x1p-11;
cvt_w.u = (0x413ull << (64 - 12)) | (cvt_x.m >> 31);
*rh = cvt_w.f - 0x1p20;
}
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 two_sum(double* rh, double* rl, double a, double b) {
*rh = a + b;
double ap = *rh - b;
double bp = *rh - ap;
*rl = (a - ap) + (b - bp);
}
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 inline
void a_mul(double* rh, double* rl, double a, double b) {
*rh = a*b;
*rl = __builtin_fma(a,b,-*rh);
}
static inline
void d_mul(double* rh, double* rl, double ah, double al,
double bh, double bl) {
double p;
a_mul(rh, &p, ah, bh); double q = __builtin_fma(al, bh, p);
*rl = __builtin_fma(ah, bl, q);
}
#include "powl_tables.h"
static inline
void polyeval(double* rh, double* rl, double xh, double xl) {
static const double ln2invh = 0x1.71547652b82fep+0,
ln2invl = 0x1.777d0ffda0d24p-56;
double scaleh, scalel;
d_mul(&scaleh, &scalel, ln2invh, ln2invl, xh, xl);
double ord01h, ord01l;
ord01h = -xh/2; ord01l = -xl/2; high_sum(&ord01h, &ord01l, 1.0, ord01h, ord01l);
double ord23h, ord23l;
ord23h = -xh/4;
ord23l = __builtin_fma(-xl, 1./4, 0x1.55555a5b705aap-56);
high_sum(&ord23h, &ord23l, 0x1.5555555555555p-2, ord23h, ord23l);
double xsqh, xsql;
d_mul(&xsqh, &xsql, xh, xl, xh, xl);
d_mul(&ord23h, &ord23l, ord23h, ord23l, xsqh, xsql);
double x4 = xsqh*xsqh;
double acc = __builtin_fma(xh, -0x1.555555555554dp-3,
0x1.999999999998ap-3);
double bcc = __builtin_fma(xh, -0x1.0000014f8ec21p-3,
0x1.24924ad7557bep-3);
acc = __builtin_fma(xsqh, bcc, acc);
ord01l = __builtin_fma(x4, acc, ord01l);
high_sum(&ord23h, &ord23l, ord01h, ord23h, ord23l);
ord23l += ord01l;
d_mul(rh, rl, scaleh, scalel, ord23h, ord23l);
}
static inline
void compute_log2pow(double* rh, double* rl, long double x, long double y) {
b80u80_t cvt_x = {.f = x};
int extra_int = (cvt_x.e&0x7fff) - 16383;
cvt_x.e = 16383; x = cvt_x.f;
double xh, xl; split(&xh, &xl, x);
lut_t l = coarse[cvt_x.m>>56 & 0x7f];
double r1 = l.r;
double mlogr1h = l.mlogrh;
double mlogr1l = l.mlogrl;
extra_int += l.z;
xh *= r1; xl *= r1;
b64u64_t cvt_xh = {.f = xh};
lut_t l2 = fine[(cvt_xh.u>>40) & 0x7f]; double r2 = l2.r;
double mlogr2h = l2.mlogrh;
double mlogr2l = l2.mlogrl;
double mlogrh, mlogrl;
high_sum(&mlogrh, &mlogrl, extra_int, mlogr1h, mlogr1l);
double mlogr12h, mlogr12l;
high_sum(&mlogr12h, &mlogr12l, mlogrh, mlogr2h, mlogr2l);
mlogr12l += mlogrl;
fast_two_sum(&mlogr12h, &mlogr12l, mlogr12h, mlogr12l);
xh = __builtin_fma(r2, xh, -1); xl *= r2;
two_sum(&xh, &xl, xh, xl);
polyeval(rh, rl, xh, xl);
high_sum(rh, rl, mlogr12h, *rh, *rl);
*rl += mlogr12l;
b80u80_t cvt_y; cvt_y.f = y;
b64u64_t cvt_w, cvt_aux;
cvt_w.u = ((cvt_y.e&0x8000ull) << (63 - 15)) | (((cvt_y.e&0x7fffull) + (1023ull - 16383ull)) << (64 - 12)) |
((cvt_y.m >> 11) & ~(1ull << 52)); double yh = cvt_w.f;
cvt_w.u = ((cvt_y.e&0x8000ull) << (63 - 15)) | (((cvt_y.e&0x7fffull) + (1023ull - 16383ull - 11)) << (64 - 12)) |
(cvt_y.m & ((1ull << 11) - 1ull));
cvt_aux.u = ((cvt_y.e&0x8000ull) << (63 - 15)) | (((cvt_y.e&0x7fffull) + (1023ull - 16383ull - 11)) << (64 - 12));
double yl = cvt_w.f - cvt_aux.f;
d_mul(rh, rl, yh, yl, *rh, *rl);
}
static inline
int exp2d(double* resh, double* resl, double xh, double xl) {
b64u64_t cvt = {.f = xh};
bool do_red = cvt.e >= -20 + 0x3ff;
static const double C = 0x1.8p+32; b64u64_t y = {.f = xh + C};
uint64_t fracpart = y.u;
int16_t extra_exponent = y.u>>20;
if(__builtin_expect(do_red, 1)) {
double rem = xh - (y.f - C); fast_two_sum (&xh,&xl,rem,xl);
}
int i0 = fracpart & 0x1f;
int i1 = (fracpart >> 5) & 0x1f;
int i2 = (fracpart >> 10) & 0x1f;
int i3 = (fracpart >> 15) & 0x1f;
double frcp_acc0_l, frcp_acc0_h, frcp_acc2_h, frcp_acc2_l;
double xs_pow2_h, xs_pow2_l;
d_mul(&frcp_acc0_h, &frcp_acc0_l,
t0[i0][0], t0[i0][1], t1[i1][0], t1[i1][1]); d_mul(&frcp_acc2_h, &frcp_acc2_l,
t2[i2][0], t2[i2][1], t3[i3][0], t3[i3][1]); d_mul(&xs_pow2_h, &xs_pow2_l, frcp_acc0_h, frcp_acc0_l,
frcp_acc2_h, frcp_acc2_l);
double xsq = xh * xh;
double orders23 = xsq * __builtin_fma(xh,0x1.c6b08d704a1cdp-5,
0x1.ebfbdff82c696p-3);
double order1h, order1l;
static const double coeff1h = 0x1.62e42fefa39efp-1;
static const double coeff1l = 0x1.abc9e3b369936p-56;
d_mul(&order1h, &order1l, coeff1h, coeff1l, xh, xl);
double finalh, finall;
fast_two_sum(&finalh, &finall, 1, orders23);
double tmp;
fast_two_sum(&finalh, &tmp, finalh, order1h);
finall = tmp + (finall + order1l);
if(__builtin_expect(do_red,1)) {
d_mul(&finalh, &finall, finalh, finall, xs_pow2_h, xs_pow2_l);
} else {
extra_exponent = 0;
}
*resh = finalh;
*resl = finall;
return extra_exponent;
}
static
long double fastpath_roundtest(double rh, double rl, int extra_exp,
bool invert, bool* fail) {
unsigned rm = get_rounding_mode();
fast_two_sum(&rh, &rl, rh, rl);
b64u64_t th = {.f = rh}, tl = {.f = rl};
long eh = th.u>>52, el = (tl.u>>52)&0x3ff, de = eh - el;
long ml = (tl.u&~(0xfffull<<52))|1ll<<52, sgnl = -(tl.u>>63);
ml = (ml^sgnl) - sgnl;
int64_t mlt;
long sh = de-11;
if(__builtin_expect(sh>63,0)){
mlt = sgnl;
if(__builtin_expect(sh-64>63,0)) ml = sgnl;
else ml >>= sh-64;
} else {
mlt = ml>>sh;
ml = (uint64_t)ml << (64-sh);
}
uint64_t mh = ((th.u<<11)|1ull<<63);
mh += mlt;
if(__builtin_expect(!(mh>>63),0)){ mh = mh<<1 | (uint64_t)ml>>63;
ml = (uint64_t)ml<<1;
extra_exp--;
}
int64_t eps = (mh >> (83 - 64));
int wanted_exponent = extra_exp + 0x3c00 + eh;
if(__builtin_expect(wanted_exponent <= 0, 0)) { int shiftby = 1 - wanted_exponent;
if(__builtin_expect(shiftby >= 64, 0)) {
ml = mh >> (shiftby - 64); mh = 0; eps = 1;
if(__builtin_expect(shiftby > 65, 0)) {
*fail = false; return invert ? -0x1p-16445L*.25L : 0x1p-16445L*0.25L;
}
// This overestimates epsilon, which is safe
} else {
ml = (uint64_t)ml >> shiftby;
ml |= mh << (64 - shiftby);
mh >>= shiftby;
eps >>= shiftby;
eps += 1;
}
wanted_exponent = 0;
}
uint64_t oldmh = mh; // For overflow detection
if(rm==FE_TONEAREST){ // round to nearest
mh += (uint64_t)ml>>63; // add the round bit
ml = (uint64_t)ml<<1;
eps <<= 1;
/* Multiplying ml by 2 we discard the round bit,
thus the rounding test will fail even if we are
near an exact value. The reason is that when x^y
is exact, we should reset the inexact flag. Since
testing whether x^y is exact is expensive, we delegate
this case to the accurate path. Thus we sacrifice
x^y exact to make the average case faster. */
} else if((rm==FE_UPWARD && !invert) || (rm==FE_DOWNWARD && invert)) {
mh += 1;
// This is as if ml had a trailing 1.
// We are not precise up to an LSB of ml anyway.
}
// This branch can only be taken if wanted_exponent != 0
// Else we simply cannot have an overflow
if(__builtin_expect(mh < oldmh, 0)) {
ml = ml/2; // Signed semantics matter
eps >>= 1;
mh = (uint64_t) 1 << 63;
wanted_exponent++;
}
// We had a denormal but rounding made it into the smallest normal
if(__builtin_expect((mh>>63) && !wanted_exponent, 0)) {
wanted_exponent = 1;
}
b80u80_t v;
v.m = mh; // mantissa
v.e = wanted_exponent; // exponent
if(__builtin_expect(invert, 0)) {v.e += (1<<15);}
bool b1 = (uint64_t)ml + eps <= (uint64_t)(2*eps);
*fail = b1;
// Denormals *inside* the computation don't seem to be a problem
// given the error analysis (we used absolute bounds mostly)
// Infinity output case, when we know we won't take the accurate path
if(__builtin_expect(wanted_exponent >= 32767 && !*fail, 0)) {
return (invert ? -0x1p16383L - 0x1p16383L : 0x1p16383L + 0x1p16383L);
}
// inexact underflow case
if (__builtin_expect (wanted_exponent == 0 && !*fail, 0)) {
feraiseexcept (FE_UNDERFLOW);
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE; // underflow
#endif
}
return v.f;
}
/* Given |x| < 2^-11.999 fitting in 128 bits,
computes an approximation of log2(1 + x).
Relative error at most 2^-249.997 (see analyze_logpoly() in
accurate_analysis.sage)
*/
inline static
void q_logpoly(qint64_t* r, const qint64_t* x) {
/* We use a naïve Horner scheme as a placeholder,
can most definitely be improved.
Coefficients for log2(1 + x)/x
Minimax polynomial of degree 18 from accurate_log2.sollya,
with maximal relative error < 2^-250.299.
*/
static const qint64_t P[19] = {
{.hh = 0x9b81e344cc4acd3f, .hl = 0x0, .lh = 0x0, .ll = 0x0, .ex = -4, .sgn = 0x0}, /* degree 18 */
{.hh = 0xa4258caa93661016, .hl = 0x0, .lh = 0x0, .ll = 0x0, .ex = -4, .sgn = 0x1}, /* degree 17 */
{.hh = 0xadcd64dba1f4c039, .hl = 0x2cdde110238be8d8, .lh = 0x0, .ll = 0x0, .ex = -4, .sgn = 0x0}, /* degree 16 */
{.hh = 0xb8aa3b295c127161, .hl = 0xfba1b1615d052d58, .lh = 0x0, .ll = 0x0, .ex = -4, .sgn = 0x1}, /* degree 15 */
{.hh = 0xc4f9d8b4a67fefb7, .hl = 0x3425410828a24674, .lh = 0x0, .ll = 0x0, .ex = -4, .sgn = 0x0}, /* degree 14 */
{.hh = 0xd30bb153d6f6c9fb, .hl = 0x28e9fdc71f70e43e, .lh = 0x0, .ll = 0x0, .ex = -4, .sgn = 0x1}, /* degree 13 */
{.hh = 0xe347ab4698bb00e7, .hl = 0x11e274b19fcf82ca, .lh = 0x0, .ll = 0x0, .ex = -4, .sgn = 0x0}, /* degree 12 */
{.hh = 0xf6384ee1d01feba4, .hl = 0xfe0aa911f62b593b, .lh = 0x0, .ll = 0x0, .ex = -4, .sgn = 0x1}, /* degree 11 */
{.hh = 0x864d424ca0116942, .hl = 0xb91d166906a0e72e, .lh = 0xfbc828ff26a1984a, .ll = 0x4e591874ecaec24e, .ex = -3, .sgn = 0x0}, /* degree 10 */
{.hh = 0x93bb62877cdff3c9, .hl = 0x653998a6ba7dcc66, .lh = 0x16bdfd507a428b04, .ll = 0x41d10daaa8adc41c, .ex = -3, .sgn = 0x1}, /* degree 9 */
{.hh = 0xa42589ebe01547c3, .hl = 0x54071b63eba83796, .lh = 0x2686f8891574e5f5, .ll = 0x4a2c567591c942f9, .ex = -3, .sgn = 0x0}, /* degree 8 */
{.hh = 0xb8aa3b295c17f0bb, .hl = 0xbe87fed0691d3e88, .lh = 0xeb574d0da8d33458, .ll = 0x9fa51eba24fafe8, .ex = -3, .sgn = 0x1}, /* degree 7 */
{.hh = 0xd30bb153d6f6c9fb, .hl = 0x22e490ee2efcd9c1, .lh = 0xcf6430a218437c7, .ll = 0x94f364a4987e6e8, .ex = -3, .sgn = 0x0}, /* degree 6 */
{.hh = 0xf6384ee1d01feba4, .hl = 0xfe0aa915e17c5361, .lh = 0x39c9f8e1273cd88d, .ll = 0x96a06e6ed417ed99, .ex = -3, .sgn = 0x1}, /* degree 5 */
{.hh = 0x93bb62877cdff3c9, .hl = 0x653998a6ba7dcba0, .lh = 0xbc45fbba4abaaead, .ll = 0x3dee003a9576c9a2, .ex = -2, .sgn = 0x0}, /* degree 4 */
{.hh = 0xb8aa3b295c17f0bb, .hl = 0xbe87fed0691d3e88, .lh = 0xeb577aa8dd695a58, .ll = 0x8afb83221b9653eb, .ex = -2, .sgn = 0x1}, /* degree 3 */
{.hh = 0xf6384ee1d01feba4, .hl = 0xfe0aa915e17c5361, .lh = 0x39c9f8e127372320, .ll = 0xb986c890fa1e97e5, .ex = -2, .sgn = 0x0}, /* degree 2 */
{.hh = 0xb8aa3b295c17f0bb, .hl = 0xbe87fed0691d3e88, .lh = 0xeb577aa8dd695a58, .ll = 0x8b25166cd1fa0378, .ex = -1, .sgn = 0x1}, /* degree 1 */
{.hh = 0xb8aa3b295c17f0bb, .hl = 0xbe87fed0691d3e88, .lh = 0xeb577aa8dd695a58, .ll = 0x8b25166cd1a13248, .ex = 0, .sgn = 0x0}, /* degree 0 */
};
mul_qint_11(r, x, &P[0]); // Relative error ~2^-64 here is fine
/* mul_qint_11 is exact when its inputs have only one limb, but here x might have
up to 75 bits, thus the relative error is < 2^-63 */
for(int i = 1; i <= 7; i++) {
add_qint_22(r, &P[i], r);
mul_qint_22(r, r, x);
}
for(int i = 8; i <= 18; i++) {
add_qint(r, &P[i], r);
mul_qint(r, r, x);
}
}
// put in r an 256-bit approximation of y*log2(x)
// assume x is normal, not 0, 2^-80 <= |y| < 2^78
// if x is negative, y should be an integer
// ensure r = y*log2(x) * (1 + eps) with |eps| < 2^-249.064
inline static
void q_log2pow(qint64_t* r, long double x, long double y) {
b80u80_t cvt_x = {.f = x};
int extra_int = (cvt_x.e&0x7fff) - 16383;
int shiftamnt = __builtin_clzl(cvt_x.m); // We know x is not 0 so not UB
extra_int -= shiftamnt ? (shiftamnt - 1) : 0;
cvt_x.m <<= shiftamnt; // Handle denormals
cvt_x.e = 16383; // new wanted exponent
x = cvt_x.f; // original x = 2^extra_int * x
/* Really, this is the same as in the fastpath. Try to merge to reduce
code cache footprint ?
*/
double xh, xl; // 33 and 31 bits
split(&xh, &xl, x);
// x = xh + xl with 1 <= |xh| <= 2 and |xl| < 2^-32
// since 1 <= |x| < 2, xl is multiple of 2^-63
// Uses the high 7 bits of x's mantissa.
int i1 = cvt_x.m>>56 & 0x7f; // index in the coarse[] table
lut_t l = coarse[i1];
extra_int += l.z;
xh *= l.r; xl *= l.r; // exact (see compute_log2pow)
b64u64_t cvt_xh = {.f = xh};
int i2 = (cvt_xh.u>>40) & 0x7f; // index in the fine[] table
lut_t l2 = fine[i2];
// bit 52 goes to 6+5 = 11. Bits 11 - 8
xh = __builtin_fma(l2.r, xh, -1); xl *= l2.r;
/* The above operations are exact (see the analysis in compute_log2pow). */
two_sum(&xh, &xl, xh, xl);
qint64_t reducted[1];
qint_fromdd(reducted, xh, xl);
/* From the fastpath we know that we should have |reducted| <= 2^-11.999.
Since the original xl was multiple of 2^-63, r1 is multiple of 2^-9
and r2 is multiple of 2^13, xl is a multiple of 2^-63*2^-9*2^-13 = 2^-85.
Furthermore, since the original xl satisfied |xl| < 2^-32, r1 <= 1 and r2 <= 2,
xl is at most 2*2^-32 = 2^-31. On the other hand, xh is a multiple of
2^-32*2^-9*2^-13 = 2^-54, and is at most 2^-12.
This ensures that when xh != 0 and xl != 0, the exponent difference
between xh and xl is at most 85 - 12 = 73, so that qint_fromdd's
precondition applies; computing reducted is exact.
Also, reducted must be a multiple of 2^-85
which is less than 2^-11.999 therefore reducted fits in 128 bits.
*/
qint64_t eint[1];
qint_fromsi(eint, extra_int); // eint = extra_int
qint64_t mlogr[1];
add_qint(mlogr, &acc_coarsetbl[i1], eint);
// mlogr approximates extra_int - log2(2^z*r1)
qint64_t mlogr12[1];
add_qint(mlogr12, &acc_finetbl[i2], mlogr);
// mlogr12 approximates extra_int - log2(2^z*r1) - log2(r2)
/* We have log2(x)= reduction + log2(1 + xr) */
/* Since the accurate tables merely extend the precision of the fast
tables, the ratios computed by high_sum.c (-DMODE=0) stay valid.
By analogy to the fastpath, let us call mlogr1 and mlogr2 the values
looked up from the coarse and fine table respectively.
We then know that
|mlogr/mlogr12| < 1.951
|mlogr2/mlogr12| <= 1
Furthermore, since |mlogr1| < .5 it is straightforward to see that
|mlogr| >= |mlogr1| (if eint=0, then mlogr=mlogr1).
The total relative error computing mlogr is 2^-254 due to rounding
errors and (at most) 2^-256 relative error due mlogr1's error:
this yields a relative error < 2^-254+2^-256 < 2^-253.7.
The total relative error of mlogr2 is also <= 2^-256.
This gives a relative error bound on mlogr12 as follows :
2^-254 + 2^-256*|mlogr2/mlogr12| + 2^-253.7*|mlogr/mlogr12|
<= 2^-254 + 2^-256*1 + 2^-253.7*1.951
<= 2^-252.131
We thus have an error on mlogr12 which is <= 2^-252.131 |mlogr12|.
*/
qint64_t q_y[1];
qint_fromld(q_y, y); // exact
q_logpoly(r, reducted);
/* As mentioned in q_logpoly(), the relative error on the result is
at most 2^-249.997:
r = log2(1+reducted) * (1 + eps) with |eps| <= 2^-249.997
*/
add_qint(r, mlogr12, r);
/* Let us call r_in/r_out the values of r as input/output.
From max_rh_over_rh_prime_all() in powl.sage, we have here
|mlogr12/r_out| <= 2.002 and |r_in/r_out| <= 1.002.
The relative errors 2^-252.131 on mlogr12 and 2^-249.997 on r,
together with the sum's rounding error, yield an error bound of
|r_out|*(2^-254 + 2.002*2^-252.131 + 1.002*2^-249.997)
<= |r_out|*2^-249.392
The total relative error computing log2(x) is therefore at most
2^-249.392:
r = log2(x) * (1 + eps) with |eps| <= 2^-249.392.
*/
mul_qint_41(r, r, q_y);
/* The product imparts an additional relative rounding error of 2^-254.
The total relative error computing y log2(x) is thus at most
(1 + 2^-249.392) * (1 + 2^-254) - 1 < 2^-249.334 */
}
/* Let f be the low 20 bits of fracpart. Computes r and corr
such that 2^(f/2^20)*2^corr = r*(1 + e) with |e| < 2^-251.192,
and |corr| < 2^-103.
*/
inline static
void q_exp2xs(qint64_t* r, uint64_t fracpart, qint64_t* corr) {
int i0 = fracpart & 0x1f;
int i1 = (fracpart >> 5) & 0x1f;
int i2 = (fracpart >> 10) & 0x1f;
int i3 = (fracpart >> 15) & 0x1f;
qint64_t tmp0[1], tmp1[1], tmp2[1], tmp3[1];
qint_fromdd(tmp0, t0[i0][0], t0[i0][1]); corr_t corr0 = t0_corr[i0];
qint_fromdd(tmp1, t1[i1][0], t1[i1][1]); corr_t corr1 = t1_corr[i1];
qint_fromdd(tmp2, t2[i2][0], t2[i2][1]); corr_t corr2 = t2_corr[i2];
qint_fromdd(tmp3, t3[i3][0], t3[i3][1]); corr_t corr3 = t3_corr[i3];
int exponent = -104;
int64_t corr_h = corr0.h + corr1.h + corr2.h + corr3.h;
unsigned __int128 corr_l = corr0.l + corr1.l + corr2.l + corr3.l;
corr_h += (unsigned) (corr_l >> 126); corr_l <<= 2;
int sgn = corr_h < 0;
if(sgn) {
corr_h = -corr_h - 1;
corr_l = -corr_l;
}
if(__builtin_expect(corr_h == 0, 0)) {
cp_qint(corr, &ZERO_Q);
} else {
int shift = __builtin_clzl(corr_h);
corr_h = (uint64_t)corr_h<<shift;
corr_h |= shift ? (corr_l >> (128 - shift)) : 0;
corr_l <<= shift;
corr->hh = corr_h;
corr->hl = corr_l >> 64;
corr->lh = (uint64_t) corr_l;
corr->ll = 0;
corr->sgn = sgn;
corr->ex = exponent - shift;
}
qint64_t acc0[1], acc1[1];
mul_qint_22(acc0, tmp0, tmp1);
mul_qint_22(acc1, tmp2, tmp3);
mul_qint(r, acc0, acc1);
}
inline static
void q_exp2poly(qint64_t* r, const qint64_t* x) {
static const qint64_t Q[11] = {
{.hh = 0xf267a8ac5c749bda, .hl = 0x0, .lh = 0x0, .ll = 0x0, .ex = -28, .sgn = 0x0},
{.hh = 0xda929e9caf40bee9, .hl = 0x28ba755cfbeb15af, .lh = 0x0, .ll = 0x0, .ex = -24, .sgn = 0x0},
{.hh = 0xb160111d2e411fec, .hl = 0x7ff30374d01cdca8, .lh = 0x0, .ll = 0x0, .ex = -20, .sgn = 0x0},
{.hh = 0xffe5fe2c45863435, .hl = 0x8a8e64398706e1c3, .lh = 0x0, .ll = 0x0, .ex = -17, .sgn = 0x0},
{.hh = 0xa184897c363c3b7a, .hl = 0x58544c3591a0f9f6, .lh = 0x62916db41ee8676d, .ll = 0x9a0f49f2428577c7, .ex = -13, .sgn = 0x0},
{.hh = 0xaec3ff3c53398883, .hl = 0x8bfb4d28a5f61982, .lh = 0xbb69ccdf430a035d, .ll = 0xc86feff9c6e5d36f, .ex = -10, .sgn = 0x0},
{.hh = 0x9d955b7dd273b94e, .hl = 0x65df05a9f7562839, .lh = 0x23c7529f31c88ec9, .ll = 0x10b5dbd81066669c, .ex = -7, .sgn = 0x0},
{.hh = 0xe35846b82505fc59, .hl = 0x9d3b15d995e96f74, .lh = 0xf5c47444da0110e2, .ll = 0x5a29dd80d795db82, .ex = -5, .sgn = 0x0},
{.hh = 0xf5fdeffc162c7543, .hl = 0x78b583764b9afe55, .lh = 0x1d13a8e186734ea6, .ll = 0x15f49ddd89a9bc73, .ex = -3, .sgn = 0x0},
{.hh = 0xb17217f7d1cf79ab, .hl = 0xc9e3b39803f2f6af, .lh = 0x40f343267298b62d, .ll = 0x8a0d175b8bb03a5a, .ex = -1, .sgn = 0x0},
{.hh = 0x8000000000000000, .hl = 0x0, .lh = 0x0, .ll = 0x0, .ex = 0, .sgn = 0x0},
};
mul_qint_11(r, &Q[0], x);
for(int i = 1; i <= 4; ++i) {
add_qint_22(r, &Q[i], r);
mul_qint_22(r, r, x);
}
for(int i = 5; i <= 9; i++) {
add_qint(r, &Q[i], r);
mul_qint(r, r, x);
}
add_qint(r, &Q[10], r);
}
inline static
void q_exp2(qint64_t* r, const qint64_t* x) {
uint64_t xs = x->ex >= -20 ? (x->hh >> (43 - x->ex)) : 0;
qint64_t reducted[1];
reducted->hh = x->ex >= -20 ? (xs << (43 - x->ex)) : 0;
reducted->hl = reducted->lh = reducted->ll = 0;
reducted->sgn = x->sgn ^ 1;
reducted->ex = x->ex;
add_qint(reducted, x, reducted);
int extra_exp;
uint64_t fracpart;
if(x->sgn) {
fracpart = (1 << 20) - xs; if(__builtin_expect(xs & 0xfffff, 1)) {
extra_exp = -(xs>>20) - 1;
} else {
extra_exp = -(xs>>20); }
} else {
extra_exp = xs>>20;
fracpart = xs;
}
qint64_t corr[1], exp2frac[1];
q_exp2xs(exp2frac, fracpart, corr);
corr->sgn ^= 1;
add_qint(reducted, corr, reducted);
qint64_t exp2red[1];
q_exp2poly(exp2red, reducted);
mul_qint(r, exp2red, exp2frac);
r->ex += extra_exp;
}
#include "powl_exact.h"
inline static
bool is_integer(long double x) {
const b80u80_t cvt = {.f = x};
int e = (cvt.e & 0x7fff) - 16383; if (e >= 63) return true; else {
if (e <= -1) return cvt.m == 0; else {
uint64_t u = cvt.m << (e + 1);
return u == 0;
}
}
}
inline static
bool is_odd_integer(long double x) {
const b80u80_t cvt = {.f = x};
int e = (cvt.e&0x7fff) - 16383; if (e >= 64) return false; else {
if (e <= -1) return false; else {
uint64_t u = cvt.m << e;
return u == (uint64_t) 1 << 63;
}
}
}
inline static
int _isnan(long double x) {
const b80u80_t v = {.f = x};
return ((v.e&0x7fff) == 0x7fff && (v.m<<1));
}
inline static
int _issnan(long double x) {
const b80u80_t v = {.f = x};
return _isnan(x) && ((int64_t)(v.m<<1) > 0);
}
__attribute__((cold))
static
long double accurate_path(long double x, long double y, FLAG_T inex, bool invert) {
qint64_t q_r[1]; q_log2pow(q_r, x, y);
if(q_r->ex >= 15) {
if(q_r->sgn) { return (invert ? -0x1p-16445L : 0x1p-16445L)*.25L;
} else { // y*log2|x| > 32767: overflow
return (invert ? -0x1p16383L : 0x1p16383L) * 2L;
}
}
q_exp2(q_r, q_r);
unsigned rm = get_rounding_mode();
qint64_t subqr[1];
if(q_r->ex <= -16448) {
return (invert ? -0x1p-16445L : 0x1p-16445L) * .25L;
}
uint64_t extralow[1] = {0};
// Extra-low limb to avoid loss of precision
// when the final result is denormal.
qint_subnormalize(subqr, extralow, q_r);
// In corner cases, qint_subnormalize creates a relative error 2^-255. This
// implies that the total relative error is at most
// 2^-234.862 + 2^-255 + 2^-255 * 2^-234.862 <= 2^-234.861
bool exact_if_hard = __builtin_expect(check_rb(x,y,q_r),0);
bool hard = false;
long double r = qint_told(subqr, *extralow, rm, invert, &hard);
b80u80_t cvt_r = {.f = r};
/* Assume that (x,y) is a hard case in the sense that the accurate path
rounding test fails. Assume further that (x,y) is potentially exact or
midpoint, i.e. is in the set S defined in reference [2] for double
precision. For extended double the inputs (x,y) such that x^y is
exact or midpoint satisfy necessarily one of the following:
(a) x=2^k and y integer
(b) y integer, 2 <= y <= 41 (2 <= y <= 40 if x^y is exact)
(c) x=m*2^E and y=n*2^F with -5 <= F <= -1 and 3 <= n <= 41,
n odd (with n <= 40 if x^y is exact)
Let us note z the approximation we computed. We know that for some rounding
boundary r, |z - r| <= (2^-234 + 2^-254)|z| because the rounding test
failed. We also know that |z - x^y| <= 2^-234.861|z|. Thus we have
x^y = z * (1 + eps) with |eps| <= 2^-234.861, or |x^y/z - 1| <= 2^-234.861.
This implies that |x^y - r| <= (2^-234.861 + 2^-234 + 2^-254)|z|, or
|x^y - r| <= (2^-234.861+2^-234+2^-254)/(1-2^-234.861) |x^y|
<= 2^-233.367 |x^y|.
If we list with BaCSeL all cases where |x^y - r| <= 2^-233.367 |x^y|,
we may be able to certify that in all these cases, x^y is exact. When
run with parameter m, BaCSeL will output all pairs (x,y) in S at distance
less than 2^-m ulp(r') from a rounding boundary r'.
Since |x^y - r| <= 2^-233.367 |x^y| implies |x^y - r| <= 2^-169.367 ulp(r),
taking m=169 ensures we miss no hard case in S.
(It is obvious that r = r' whenever the discussed situation arises.
The argument above applies even when x^y is subnormal. Indeed, subnormal
rounding boundaries are a subset of the boundaries considered by BaCSeL.)
*/
if(hard && exact_if_hard) {
/* If we are here, then either the rounding mode is not FE_TONEAREST
and we are on an exact number, or rm == FE_TONEAREST and we are near a
rounding boundary.
*/
exactify(q_r);
// Makes an exact rounding boundary out of q_r on unbounded exponent range
qint_subnormalize(subqr,extralow, q_r);
bool restore_flags = ((cvt_r.e&0x7fff) != 0x7fff) && !subqr->hl && !subqr->lh;
// restore the inexact flag
if( restore_flags ) {
set_flag (inex);
} else {
feraiseexcept(FE_INEXACT);
}
/* If the result overflows, even if it would be exact with an unbounded
exponent range, the inexact flag must not be cleared. The mantissa checks
are there to check if the result is exact even accounting for
subnormalization.
*/
b80u80_t cvt = {.f = qint_told(subqr,*extralow, rm, invert, &hard)};
// We return a non-exact subnormal
if((cvt.e&0x7fff) == 0 && !restore_flags) {
feraiseexcept(FE_UNDERFLOW);
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE;
#endif
}
return cvt.f;
}
//FIXME Do we have a standard macro switch ?
#ifndef CORE_MATH_FAIL_QUIET
else if(hard) {
printf ("Unexpected worst-case found.\n");
printf ("Please report to core-math@inria.fr:\n");
printf ("Worst-case of powl found: x,y=%La,%La\n", x, y);
exit(1);
}
#endif
else {
b80u80_t cvt = {.f = r};
feraiseexcept(FE_INEXACT);
if((cvt.e&0x7fff) == 0) { // We return a non-exact subnormal
feraiseexcept(FE_UNDERFLOW);
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE;
#endif
}
return r;
}
}
long double cr_powl(long double x, long double y) {
const b80u80_t cvt_x = {.f = x}, cvt_y = {.f = y};
int x_exp = (cvt_x.e&0x7fff) - 16383;
int y_exp = (cvt_y.e&0x7fff) - 16383;
bool invert = false; // true iff x^y < 0
long double sign = 1.0L; // sign of x^y
/* Both these variables hold the same information, but
invert is more convenient for conditionals (in the rounding tests)
whereas sign is more convenient during special value handling.
*/
static const long double inf = __builtin_infl();
bool lt1 = (x_exp < 0) ^ (cvt_y.e>>15);
// If lt1 is set, then |x|^y < 1.
// If lt1 is not set, then either |x|^y > 1 or |x|==1
/* x is negative */
if(__builtin_expect(cvt_x.e>>15, 0)) {
if(__builtin_expect(_isnan(y)
|| (!is_integer(y) && cvt_x.m && x_exp != 0x7fff-16383)
,0)) {
feraiseexcept(FE_INVALID);
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = EDOM;
#endif
return __builtin_nanl("");
}
if(is_odd_integer(y)) {
invert = true;
sign = -1.0L;
}
}
// x is either (s)NaN or +-infty
if(__builtin_expect(x_exp == 0x7fff - 16383, 0)) {
if(_issnan(x)) {
feraiseexcept(FE_INVALID);
return __builtin_nanl("");
}
if(!cvt_y.m) {return 1L;}
if(_isnan(x) || _isnan(y)) {
feraiseexcept(FE_INVALID);
return __builtin_nanl("");
}
// Here x == +-infty and y != 0
return lt1 ? sign*0L : sign*inf;
} else
// From now on, x is normal
if(__builtin_expect(!cvt_x.m, 0)) { // x == +-0
if(_isnan(y)) {feraiseexcept(FE_INVALID); return __builtin_nanl("");}
if(!cvt_y.m) {return 1L;} // y == 0
if(cvt_y.e>>15 && cvt_y.e != 0xffff) {
feraiseexcept(FE_DIVBYZERO);
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE; // pole error
#endif
}
return lt1 ? sign*0L : sign*inf;
}
/* For |y| >= 2^78, and |x| <> 1, the smallest value of
|y * log2(x)| is attained for x = 1 - 2^-64, and is > 23637,
thus |x^y| is smaller than the smallest positive subnormal
2^-16445, or largest than MAX_LDBL = 2^16384*(1-2^-64).
Here y is either +-inf, (s)NaN, or very large.
*/
else if(__builtin_expect(y_exp >= 78, 0)) {
if(_issnan(y)) {feraiseexcept(FE_INVALID); return __builtin_nanl("");}
if(x == 1.L) {return 1.L;}
if(_isnan(y)) {feraiseexcept(FE_INVALID); return __builtin_nanl("");}
if(x == -1.L) {return sign;}
// y == +-infty
if(y_exp == 0x7fff - 16383) {
return lt1 ? sign*0L : sign*inf;
} else {
#ifdef CORE_MATH_SUPPORT_ERRNO
errno = ERANGE; // overflow
#endif
return lt1 ? (sign*0x1p-16445L)*.25L : (sign*0x1p16383L)*2L;
}
} else if(__builtin_expect(y_exp <= -81, 0)) {
/* For y_exp <= -81, we have |y| < 2^-80,
thus since |log2(x)| <= 16445, we have |y*log2(x)| < 0x1.00f4p-66.
Since for |t| <= 0x1.71547652b82fe176p-65, 2^t rounds to 1
to nearest, we can deduce the correct rounding.
if(!cvt_y.m || x == 1.L) {
return 1.L;
} else { return lt1 ? 1. - 0x1p-16445L : 1. + 0x1p-16445L;
}
}
const FLAG_T inex = get_flag();
// now -80 <= y_exp <= 77 thus 2^-80 <= |y| < 2^78
#ifndef ACCURATE_ONLY
// Automatic giveup if x subnormal
if(__builtin_expect(cvt_x.m >> 63, 1)) {
long double r;
double rh, rl;
compute_log2pow(&rh, &rl, x, y);
// rh + rl = y * log2(x) * (1 + eps1) with
// |eps1| <= 2^-97.286 |rh| and |rl| <= 2^-48.262 |rh|
double rhsq = rh*rh;
/* This implies |rh| <= 1p-66;
Since |rl| <= 2^-48.262 |rh|,
|y * log2(x)| < 2^-66 * (1+2^-48.262) * (1+2^-97.286)
< 2^-65, and exp(+/-2^-65) rounds to 1 to nearest,
thus by monotonicity of rounding x^y rounds to 1
to nearest.
if(__builtin_expect(rhsq <= 0x1p-132, 0)) {
return sign * 1.L + sign * rh;
} else if(__builtin_expect(rhsq >= (16447*16447), 0)) {
if(rh > 0) {return sign*0x1p16383L + sign*0x1p16383L;}
else {return (sign*0x1p-16445L)*.25L;}
} else {
// rh + rl approximates y*log2(x)
double resh, resl;
int extra_exponent = exp2d(&resh, &resl, rh, rl);
/* 2^extra_exponent * (resh + resl)
= 2^(rh + rl) * (1 + eps2) with |eps2| <= 2^-85.010,
0.499 < |resh| < 2.004, |resl| <= 2^-47.638.
Since rh + rl = y * log2(x) * (1 + eps1) with
|eps1| <= 2^-97.286,
and |rh+rl| < 16446.1 * (1 + 2^-48.262),
we deduce |y * log2(x)| < 16446.2, thus
rh + rl = y * log2(x) + eps1_abs
with |eps1_abs| < 16446.2 * 2^-97.286 < 2^-83.280.
It follows:
2^extra_exponent * (resh + resl)
= x^y * 2^eps1_abs * (1 + eps2)
= x^y * (1 + eps)
with |eps| < 2^eps1_abs * (1 + eps2) - 1 < 2^-83.287
*/
bool fail = false;
r = fastpath_roundtest(resh, resl, extra_exponent, invert, &fail);
if(__builtin_expect(!fail, 1)) {
return r;
}
}
} // Fastpath failed or x was subnormal
#endif
return accurate_path(x, y, inex, invert);
}