BitUtil: Delta, ZigZag, NumBits, Floating Point,...

This commit is contained in:
x
2018-03-17 09:01:17 +01:00
parent 260363f738
commit 41dfc01bf2

View File

@ -443,26 +443,24 @@ unsigned bitfm64(uint64_t *in, unsigned n, uint64_t *pmin) { uint64_t mi,mx; BIT
//----------- Lossy floating point conversion: pad the trailing mantissa bits with zeros according to the error e (ex. 0.00001) ----------------------------------- //----------- Lossy floating point conversion: pad the trailing mantissa bits with zeros according to the error e (ex. 0.00001) -----------------------------------
static inline double efloat64(double d, double e, int lg2e) { static inline double efloat64(double d, double e, int lg2e) {
int v;
uint64_t u, du = ctou64(&d); uint64_t u, du = ctou64(&d);
frexp(d, &v); int v = (du>>52 & 0x7ff)-0x3fe;
if((v = 54 - v - lg2e) <= 0) return d; if((v = 54 - v - lg2e) <= 0) return d;
v = v > 52?52:v; v = v > 52?52:v;
do u = du & (~((1ull<<(--v))-1)); while(fabs((ctof64(&u) - d)/d) > e); do u = du & (~((1ull<<(--v))-1)); while(fabs((ctof64(&u) - d)/d) > e);
return ctof64(&u); return ctof64(&u);
} }
void padfloat64(double *in, size_t n, double *out, double e) { int lg2e = -log(e)/log(2.0); double *ip; for(ip = in; ip < in+n; ip++) *out++ = efloat64(*ip, e, lg2e); } void padfloat64(double *in, size_t n, double *out, double e) { int lg2e = -log(e)/log(2.0); double *ip; for(ip = in; ip < in+n; ip++,out++) *out = efloat64(*ip, e, lg2e); }
static inline float efloat32(float d, float e, int lg2e) { static inline float efloat32(float d, float e, int lg2e) {
int v;
uint32_t u, du = ctou32(&d); uint32_t u, du = ctou32(&d);
frexpf(d, &v); int v = (du>>23 & 0xff)-0x7e;
if((v = 25 - v - lg2e) <= 0) return d; if((v = 25 - v - lg2e) <= 0) return d;
v = v > 23?23:v; v = v > 23?23:v;
do u = du & (~((1u<<(--v))-1)); while(fabsf((ctof32(&u) - d)/d) > e); do u = du & (~((1u<<(--v))-1)); while(fabsf((ctof32(&u) - d)/d) > e);
return ctof32(&u); return ctof32(&u);
} }
void padfloat32(float *in, size_t n, float *out, float e) { int lg2e = -log(e)/log(2.0); float *ip; for(ip = in; ip < in+n; ip++) *out++ = efloat32(*ip, e, lg2e); } void padfloat32(float *in, size_t n, float *out, float e) { int lg2e = -log(e)/log(2.0); float *ip; for(ip = in; ip < in+n; ip++,out++) *out = efloat32(*ip, e, lg2e); }