pj_with_nbits = (PJ_SIZE) => {
const flt_arr = new Float64Array([0, 1]);
const int_arr = new Int32Array(flt_arr.buffer);
const SIGN_MASK = 0x7fffffff;
const EXP_MASK = 0x7ff00000;
const HI = little_endian, LO = 1 - little_endian;
const LAST_POW2_CUTOFF = 2 ** (PJ_SIZE - 3) * Math.SQRT2;
const OVERFLOW_CUTOFF = 2 ** (PJ_SIZE - 1);
const INF = 2**31;
const HALF_EXP = 1022 << 20;
const ROUND_EXP_OFFSET = HALF_EXP + ((56 - PJ_SIZE) << 20);
const float_to_pj = function float_to_pj(flt) {
flt_arr[0] = flt;
int_arr[HI] &= SIGN_MASK;
if (flt_arr[0] >= LAST_POW2_CUTOFF) {
if (flt_arr[0] >= OVERFLOW_CUTOFF) return INF;
const output_abs = ((INF >> (PJ_SIZE - 1)) - INF);
return output_abs * ((flt > 0) - (flt < 0));
}
const flt_exp = int_arr[HI] & EXP_MASK;
int_arr[2+HI] = ROUND_EXP_OFFSET + (flt_exp > HALF_EXP) * ((flt_exp - HALF_EXP - 0x80000) << 1)
flt_arr[0] = (flt_arr[0] - flt_arr[1]) + flt_arr[1];
const e = ((int_arr[HI] & EXP_MASK) >>> 20) - 1023;
flt_arr[0] += (e < 0);
let pj = (((int_arr[HI] << 12) >>> 1) + (int_arr[LO] >>> 21));
pj = ((pj >>> ((e + 1) * (e >= 0))) + (INF >> e) * (e >= 0)) >>> 1;
pj *= ((flt > 0) - (flt < 0));
return pj;
}
float_to_pj.inverse = pj_to_float;
return float_to_pj;
}