#pragma once
static inline
void q_extract(int64_t* e, uint64_t* m, long double x) {
const b80u80_t cvt_x = {.f = x};
int shift = __builtin_ctzl(cvt_x.m);
int exponent = (cvt_x.e&0x7fff) - 16383;
if(exponent == -16383) {exponent++;}
*e = exponent - (63 - shift);
*m = cvt_x.m >> shift;
}
static inline
void q_extract65(int64_t* e, const qint64_t* z) {
uint64_t l = (z->hl >> 62) | (z->hh << 2), h = z->hh >> 62;
if(l & 1) {l+=1; if(l == 0) {h++;}}
if(l == 0) { *e = z->ex + __builtin_ctzll(h) - 1;
} else {
*e = z->ex + __builtin_ctzll(l) - 65;
}
}
static
bool check_rb(long double x, long double y, const qint64_t* z) {
uint64_t m;
int64_t E;
q_extract(&E, &m, x);
uint64_t n;
int64_t F;
q_extract(&F, &n, y);
if (m == 1) { if(F >= 0) return true;
if(F <= -31) return false;
return !(E & (((int64_t)1 << (-F)) - 1)); }
if(y < 0) return false;
if(F > 5 || n > 41 || (F >= 0 && (n * (1 << F) > 41)))
return false;
if (F < -5) return false;
if(F < 0) {
if(E & (((int64_t)1 << (-F)) - 1)) return false;
int64_t off = n*(E >> (-F)); int64_t G;
q_extract65(&G, z);
if(G != off) return false;
}
return true;
}
static inline
void exactify(qint64_t* a) {
if((a->hl>>62) & 1) {
uint64_t oldhl = a->hl;
a->hl += (1ull << 63);
if(a->hl < oldhl) {
a->hh++;
}
if(__builtin_expect(!a->hh, 0)) {
a->hh = 1ull << 63;
a->ex++;
}
}
a->hl &= 1ull<<63; a->lh = a->ll = 0;
}