diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-05-19 10:41:02 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-05-19 10:41:02 +0100 |
commit | 77f54e95b38197dfac36864cd1ddd05fa25d5b7d (patch) | |
tree | b8630d43ccd6b9cef0ab98d9f347ef3ad2aea7e3 /contrib/fpconv/fpconv.c | |
parent | 2dc49d136538276625a38d0944d221a1f0580d54 (diff) | |
download | rspamd-77f54e95b38197dfac36864cd1ddd05fa25d5b7d.tar.gz rspamd-77f54e95b38197dfac36864cd1ddd05fa25d5b7d.zip |
[Minor] Reformat file to fix tabs/spaces mess
Diffstat (limited to 'contrib/fpconv/fpconv.c')
-rw-r--r-- | contrib/fpconv/fpconv.c | 499 |
1 files changed, 249 insertions, 250 deletions
diff --git a/contrib/fpconv/fpconv.c b/contrib/fpconv/fpconv.c index 3b96af270..71818828f 100644 --- a/contrib/fpconv/fpconv.c +++ b/contrib/fpconv/fpconv.c @@ -14,290 +14,283 @@ #define minv(a, b) ((a) < (b) ? (a) : (b)) static uint64_t tens[] = { - 10000000000000000000U, 1000000000000000000U, 100000000000000000U, - 10000000000000000U, 1000000000000000U, 100000000000000U, - 10000000000000U, 1000000000000U, 100000000000U, - 10000000000U, 1000000000U, 100000000U, - 10000000U, 1000000U, 100000U, - 10000U, 1000U, 100U, - 10U, 1U + 10000000000000000000U, 1000000000000000000U, 100000000000000000U, + 10000000000000000U, 1000000000000000U, 100000000000000U, + 10000000000000U, 1000000000000U, 100000000000U, + 10000000000U, 1000000000U, 100000000U, + 10000000U, 1000000U, 100000U, + 10000U, 1000U, 100U, + 10U, 1U }; -static inline uint64_t get_dbits(double d) -{ - union { - double dbl; - uint64_t i; - } dbl_bits = { d }; +static inline uint64_t get_dbits (double d) { + union { + double dbl; + uint64_t i; + } dbl_bits = {d}; - return dbl_bits.i; + return dbl_bits.i; } -static Fp build_fp(double d) -{ - uint64_t bits = get_dbits(d); +static Fp build_fp (double d) { + uint64_t bits = get_dbits (d); - Fp fp; - fp.frac = bits & fracmask; - fp.exp = (bits & expmask) >> 52; + Fp fp; + fp.frac = bits & fracmask; + fp.exp = (bits & expmask) >> 52u; - if(fp.exp) { - fp.frac += hiddenbit; - fp.exp -= expbias; + if (fp.exp) { + fp.frac += hiddenbit; + fp.exp -= expbias; - } else { - fp.exp = -expbias + 1; - } + } + else { + fp.exp = -expbias + 1; + } - return fp; + return fp; } -static void normalize(Fp* fp) -{ - while ((fp->frac & hiddenbit) == 0) { - fp->frac <<= 1; - fp->exp--; - } +static void normalize (Fp *fp) { + while ((fp->frac & hiddenbit) == 0) { + fp->frac <<= 1u; + fp->exp--; + } - int shift = 64 - 52 - 1; - fp->frac <<= shift; - fp->exp -= shift; + const unsigned int shift = 64 - 52 - 1; + fp->frac <<= shift; + fp->exp -= shift; } -static void get_normalized_boundaries(Fp* fp, Fp* lower, Fp* upper) -{ - upper->frac = (fp->frac << 1) + 1; - upper->exp = fp->exp - 1; +static void get_normalized_boundaries (Fp *fp, Fp *lower, Fp *upper) { + upper->frac = (fp->frac << 1u) + 1u; + upper->exp = fp->exp - 1u; - while ((upper->frac & (hiddenbit << 1)) == 0) { - upper->frac <<= 1; - upper->exp--; - } + while ((upper->frac & (hiddenbit << 1u)) == 0) { + upper->frac <<= 1u; + upper->exp--; + } - int u_shift = 64 - 52 - 2; + const unsigned int u_shift = 64 - 52 - 2; - upper->frac <<= u_shift; - upper->exp = upper->exp - u_shift; + upper->frac <<= u_shift; + upper->exp = upper->exp - u_shift; - int l_shift = fp->frac == hiddenbit ? 2 : 1; + unsigned int l_shift = fp->frac == hiddenbit ? 2u : 1u; - lower->frac = (fp->frac << l_shift) - 1; - lower->exp = fp->exp - l_shift; + lower->frac = (fp->frac << l_shift) - 1; + lower->exp = fp->exp - l_shift; - lower->frac <<= lower->exp - upper->exp; - lower->exp = upper->exp; + lower->frac <<= lower->exp - upper->exp; + lower->exp = upper->exp; } -static Fp multiply(Fp* a, Fp* b) -{ - const uint64_t lomask = 0x00000000FFFFFFFF; +static Fp multiply (Fp *a, Fp *b) { + const uint64_t lomask = 0x00000000FFFFFFFFu; - uint64_t ah_bl = (a->frac >> 32) * (b->frac & lomask); - uint64_t al_bh = (a->frac & lomask) * (b->frac >> 32); - uint64_t al_bl = (a->frac & lomask) * (b->frac & lomask); - uint64_t ah_bh = (a->frac >> 32) * (b->frac >> 32); + uint64_t ah_bl = (a->frac >> 32u) * (b->frac & lomask); + uint64_t al_bh = (a->frac & lomask) * (b->frac >> 32u); + uint64_t al_bl = (a->frac & lomask) * (b->frac & lomask); + uint64_t ah_bh = (a->frac >> 32u) * (b->frac >> 32u); - uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32); - /* round up */ - tmp += 1U << 31; + uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32u); + /* round up */ + tmp += 1U << 31u; - Fp fp = { - ah_bh + (ah_bl >> 32) + (al_bh >> 32) + (tmp >> 32), - a->exp + b->exp + 64 - }; + Fp fp = { + ah_bh + (ah_bl >> 32u) + (al_bh >> 32u) + (tmp >> 32u), + a->exp + b->exp + 64u + }; - return fp; + return fp; } -static void round_digit(char* digits, int ndigits, uint64_t delta, uint64_t rem, uint64_t kappa, uint64_t frac) -{ - while (rem < frac && delta - rem >= kappa && - (rem + kappa < frac || frac - rem > rem + kappa - frac)) { +static void round_digit (char *digits, int ndigits, uint64_t delta, uint64_t rem, uint64_t kappa, uint64_t frac) { + while (rem < frac && delta - rem >= kappa && + (rem + kappa < frac || frac - rem > rem + kappa - frac)) { - digits[ndigits - 1]--; - rem += kappa; - } + digits[ndigits - 1]--; + rem += kappa; + } } -static int generate_digits(Fp* fp, Fp* upper, Fp* lower, char* digits, int* K) -{ - uint64_t wfrac = upper->frac - fp->frac; - uint64_t delta = upper->frac - lower->frac; +static int generate_digits (Fp *fp, Fp *upper, Fp *lower, char *digits, int *K) { + uint64_t wfrac = upper->frac - fp->frac; + uint64_t delta = upper->frac - lower->frac; - Fp one; - one.frac = 1ULL << -upper->exp; - one.exp = upper->exp; + Fp one; + one.frac = 1ULL << -upper->exp; + one.exp = upper->exp; - uint64_t part1 = upper->frac >> -one.exp; - uint64_t part2 = upper->frac & (one.frac - 1); + uint64_t part1 = upper->frac >> -one.exp; + uint64_t part2 = upper->frac & (one.frac - 1); - int idx = 0, kappa = 10; - uint64_t* divp; - /* 1000000000 */ - for(divp = tens + 10; kappa > 0; divp++) { + int idx = 0, kappa = 10; + uint64_t *divp; + /* 1000000000 */ + for (divp = tens + 10; kappa > 0; divp++) { - uint64_t div = *divp; - unsigned digit = part1 / div; + uint64_t div = *divp; + unsigned digit = part1 / div; - if (digit || idx) { - digits[idx++] = digit + '0'; - } + if (digit || idx) { + digits[idx++] = digit + '0'; + } - part1 -= digit * div; - kappa--; + part1 -= digit * div; + kappa--; - uint64_t tmp = (part1 <<-one.exp) + part2; - if (tmp <= delta) { - *K += kappa; - round_digit(digits, idx, delta, tmp, div << -one.exp, wfrac); + uint64_t tmp = (part1 << -one.exp) + part2; + if (tmp <= delta) { + *K += kappa; + round_digit (digits, idx, delta, tmp, div << -one.exp, wfrac); - return idx; - } - } + return idx; + } + } - /* 10 */ - uint64_t* unit = tens + 18; + /* 10 */ + uint64_t *unit = tens + 18; - while(true) { - part2 *= 10; - delta *= 10; - kappa--; + while (true) { + part2 *= 10; + delta *= 10; + kappa--; - unsigned digit = part2 >> -one.exp; - if (digit || idx) { - digits[idx++] = digit + '0'; - } + unsigned digit = part2 >> -one.exp; + if (digit || idx) { + digits[idx++] = digit + '0'; + } - part2 &= one.frac - 1; - if (part2 < delta) { - *K += kappa; - round_digit(digits, idx, delta, part2, one.frac, wfrac * *unit); + part2 &= one.frac - 1; + if (part2 < delta) { + *K += kappa; + round_digit (digits, idx, delta, part2, one.frac, wfrac * *unit); - return idx; - } + return idx; + } - unit--; - } + unit--; + } } -static int grisu2(double d, char* digits, int* K) -{ - Fp w = build_fp(d); +static int grisu2 (double d, char *digits, int *K) { + Fp w = build_fp (d); - Fp lower, upper; - get_normalized_boundaries(&w, &lower, &upper); + Fp lower, upper; + get_normalized_boundaries (&w, &lower, &upper); - normalize(&w); + normalize (&w); - int k; - Fp cp = find_cachedpow10(upper.exp, &k); + int k; + Fp cp = find_cachedpow10 (upper.exp, &k); - w = multiply(&w, &cp); - upper = multiply(&upper, &cp); - lower = multiply(&lower, &cp); + w = multiply (&w, &cp); + upper = multiply (&upper, &cp); + lower = multiply (&lower, &cp); - lower.frac++; - upper.frac--; + lower.frac++; + upper.frac--; - *K = -k; + *K = -k; - return generate_digits(&w, &upper, &lower, digits, K); + return generate_digits (&w, &upper, &lower, digits, K); } -static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg, - bool scientific) -{ - int exp = absv(K + ndigits - 1); - - /* write plain integer */ - if(K >= 0 && (exp < (ndigits + 7))) { - memcpy(dest, digits, ndigits); - memset(dest + ndigits, '0', K); - - return ndigits + K; - } - - /* write decimal w/o scientific notation */ - if(!scientific || (K < 0 && (K > -7 || exp < 4))) { - int offset = ndigits - absv(K); - /* fp < 1.0 -> write leading zero */ - if(offset <= 0) { - offset = -offset; - dest[0] = '0'; - dest[1] = '.'; - - /* We have up to 21 characters in output available */ - if (offset + ndigits <= 21) { - memset(dest + 2, '0', offset); - memcpy(dest + offset + 2, digits, ndigits); - - return ndigits + 2 + offset; - } - else { - goto scientific_fallback; - } - - /* fp > 1.0 */ - } else { - /* Overflow check */ - if (ndigits <= 23) { - memcpy(dest, digits, offset); - dest[offset] = '.'; - memcpy(dest + offset + 1, digits + offset, ndigits - offset); - return ndigits + 1; - } - - goto scientific_fallback; - } - } - -scientific_fallback: - /* write decimal w/ scientific notation */ - ndigits = minv(ndigits, 18 - neg); - - int idx = 0; - dest[idx++] = digits[0]; - - if(ndigits > 1) { - dest[idx++] = '.'; - memcpy(dest + idx, digits + 1, ndigits - 1); - idx += ndigits - 1; - } - - dest[idx++] = 'e'; - - char sign = K + ndigits - 1 < 0 ? '-' : '+'; - dest[idx++] = sign; - - int cent = 0; - - if(exp > 99) { - cent = exp / 100; - dest[idx++] = cent + '0'; - exp -= cent * 100; - } - if(exp > 9) { - int dec = exp / 10; - dest[idx++] = dec + '0'; - exp -= dec * 10; - - } else if(cent) { - dest[idx++] = '0'; - } - - dest[idx++] = exp % 10 + '0'; - - return idx; +static int emit_digits (char *digits, int ndigits, char *dest, int K, bool neg, + bool scientific) { + int exp = absv(K + ndigits - 1); + + /* write plain integer */ + if (K >= 0 && (exp < (ndigits + 7))) { + memcpy(dest, digits, ndigits); + memset(dest + ndigits, '0', K); + + return ndigits + K; + } + + /* write decimal w/o scientific notation */ + if (!scientific || (K < 0 && (K > -7 || exp < 4))) { + int offset = ndigits - absv(K); + /* fp < 1.0 -> write leading zero */ + if (offset <= 0) { + offset = -offset; + dest[0] = '0'; + dest[1] = '.'; + + /* We have up to 21 characters in output available */ + if (offset + ndigits <= 21) { + memset(dest + 2, '0', offset); + memcpy(dest + offset + 2, digits, ndigits); + + return ndigits + 2 + offset; + } + else { + goto scientific_fallback; + } + + /* fp > 1.0 */ + } + else { + /* Overflow check */ + if (ndigits <= 23) { + memcpy(dest, digits, offset); + dest[offset] = '.'; + memcpy(dest + offset + 1, digits + offset, ndigits - offset); + return ndigits + 1; + } + + goto scientific_fallback; + } + } + + scientific_fallback: + /* write decimal w/ scientific notation */ + ndigits = minv(ndigits, 18 - neg); + + int idx = 0; + dest[idx++] = digits[0]; + + if (ndigits > 1) { + dest[idx++] = '.'; + memcpy(dest + idx, digits + 1, ndigits - 1); + idx += ndigits - 1; + } + + dest[idx++] = 'e'; + + char sign = K + ndigits - 1 < 0 ? '-' : '+'; + dest[idx++] = sign; + + int cent = 0; + + if (exp > 99) { + cent = exp / 100; + dest[idx++] = cent + '0'; + exp -= cent * 100; + } + if (exp > 9) { + int dec = exp / 10; + dest[idx++] = dec + '0'; + exp -= dec * 10; + + } + else if (cent) { + dest[idx++] = '0'; + } + + dest[idx++] = exp % 10 + '0'; + + return idx; } -static int filter_special(double fp, char* dest) -{ +static int filter_special (double fp, char *dest) { int nchars = 3; - if(fp == 0.0) { - if(get_dbits(fp) & signmask) { + if (fp == 0.0) { + if (get_dbits (fp) & signmask) { dest[0] = '-'; dest[1] = '0'; return 2; @@ -306,56 +299,62 @@ static int filter_special(double fp, char* dest) dest[0] = '0'; return 1; } - } + } - uint64_t bits = get_dbits(fp); + uint64_t bits = get_dbits (fp); - bool nan = (bits & expmask) == expmask; + bool nan = (bits & expmask) == expmask; - if(!nan) { - return 0; - } + if (!nan) { + return 0; + } - if(bits & fracmask) { - dest[0] = 'n'; dest[1] = 'a'; dest[2] = 'n'; - } else { - if(get_dbits(fp) & signmask) { + if (bits & fracmask) { + dest[0] = 'n'; + dest[1] = 'a'; + dest[2] = 'n'; + } + else { + if (get_dbits (fp) & signmask) { dest[0] = '-'; - dest[1] = 'i'; dest[2] = 'n'; dest[3] = 'f'; + dest[1] = 'i'; + dest[2] = 'n'; + dest[3] = 'f'; nchars = 4; } else { - dest[0] = 'i'; dest[1] = 'n'; dest[2] = 'f'; + dest[0] = 'i'; + dest[1] = 'n'; + dest[2] = 'f'; } - } + } - return nchars; + return nchars; } -int fpconv_dtoa(double d, char dest[24], bool scientific) -{ - char digits[18]; +int fpconv_dtoa (double d, char dest[24], bool scientific) { + char digits[18]; - int str_len = 0; - bool neg = false; + int str_len = 0; + bool neg = false; - int spec = filter_special(d, dest + str_len); + int spec = filter_special (d, dest + str_len); - if(spec) { - return str_len + spec; - } + if (spec) { + return str_len + spec; + } - if(get_dbits(d) & signmask) { + if (get_dbits (d) & signmask) { dest[0] = '-'; str_len++; neg = true; } - int K = 0; - int ndigits = grisu2(d, digits, &K); + int K = 0; + int ndigits = grisu2 (d, digits, &K); - str_len += emit_digits(digits, ndigits, dest + str_len, K, neg, scientific); + str_len += emit_digits (digits, ndigits, dest + str_len, K, neg, scientific); - return str_len; + return str_len; } |