#include #include #include typedef long unsigned int uL; void pL(char* n, uL x){printf("%016lX = %s\n", x, n);} void va(uL a){assert(a>=(1L<<63) && !(a&((1<<11)-1)));} typedef struct{uL q; uL r;} res; void pLL(char* n, res r){printf("%016lX.%016lX = %s:\n", r.q, r.r, n);} res divq(res, uL); res mulq(uL, uL); res addq(res, res); int min(int a, int b){return a>64-n):0),v.r<=nd?quo+1:quo;} b = min(b, qb); // crude and safe estimate int splitpoint = 52+rs-b; if(p) printf("splitpoint=%d\n", splitpoint); int td = nnp>>splitpoint; uL nx = nnp - ((uL)td<=den; // not used soon; part of result if(num==den) return 1L<<52; b = bi; int N = tab[(den>>(64-(lts+1)))-ts]; Ng=N; if(p) printf("N=%d=0x%03x\n", N, N); assert(54+(rs-1) <= 64); nn = N*(num>>(64-53)); nd = N*(den>>(64-53)); delta = (1L<<(53+rs-1))-nd; if(pz) {pL("nn", nn); pL("nd", nd); pL("delta", delta);} assert(nn < (1L<<(53+rs))); {uL tq = divq(llls((res){0, num}, 53-big), den).q, rq = divq(llls((res){0, nn}, 53-big), nd).q; if(pz) {pL("tq", tq); pL("rq", rq);} assert(tq==rq); uL xq = rd(nn, 0, 53-big); if(pz) pL("xq", xq); assert(xq==tq); return xq;}} uL dvg(uL const num, uL const den){ {va(num); va(den);} if(num>11), 53), den>>11).q; if(den>11), 52), den>>11).q; return 1L<<52;} void tsd(uL n, uL d){if(pz) {pL("num", n); pL("den", d);} uL qg= dvg(n, d); if(pz) pL("qg", qg); uL qt = dv(n, d); if(pz) pL("qt", qt); uL qr = (divq((res){n>>1, 0}, d).q>>10)>>(n>=d); if(pz) pL("qr", qr); assert(qt == qr); assert(qt == qg);} // This code is to compute the quotient to 53 bits without rounding // but since position of leftmost 1 bit depends, by 1, onwhether num