diff options
author | Björn-Egil Dahlberg <[email protected]> | 2013-01-23 17:03:41 +0100 |
---|---|---|
committer | Björn-Egil Dahlberg <[email protected]> | 2013-01-23 17:03:41 +0100 |
commit | d5bc1c71f756114d3110328d57e4f0545e144577 (patch) | |
tree | 11c95b7af28a060cffda5733d3043ffea707e798 /erts/emulator/sys | |
parent | 4c97cddddbe7c193953dbb58c342069330e3324c (diff) | |
parent | ab27e8699ef2a2bafe574158200993f184de3dc2 (diff) | |
download | otp-d5bc1c71f756114d3110328d57e4f0545e144577.tar.gz otp-d5bc1c71f756114d3110328d57e4f0545e144577.tar.bz2 otp-d5bc1c71f756114d3110328d57e4f0545e144577.zip |
Merge branch 'sal/float_to_list_2/OTP-10752'
* sal/float_to_list_2/OTP-10752:
Text representation of a float formatted using given options.
Diffstat (limited to 'erts/emulator/sys')
-rw-r--r-- | erts/emulator/sys/common/erl_sys_common_misc.c | 151 | ||||
-rw-r--r-- | erts/emulator/sys/unix/sys_float.c | 9 | ||||
-rw-r--r-- | erts/emulator/sys/win32/sys_float.c | 9 |
3 files changed, 161 insertions, 8 deletions
diff --git a/erts/emulator/sys/common/erl_sys_common_misc.c b/erts/emulator/sys/common/erl_sys_common_misc.c index 461e763f03..d22914acea 100644 --- a/erts/emulator/sys/common/erl_sys_common_misc.c +++ b/erts/emulator/sys/common/erl_sys_common_misc.c @@ -105,3 +105,154 @@ int erts_get_native_filename_encoding(void) { return filename_encoding; } + +/* For internal use by sys_double_to_chars_fast() */ +static char* float_first_trailing_zero(char* p) +{ + for (--p; *p == '0' && *(p-1) == '0'; --p); + if (*(p-1) == '.') ++p; + return p; +} + +int +sys_double_to_chars(double fp, char *buffer, size_t buffer_size) +{ + return sys_double_to_chars_ext(fp, buffer, buffer_size, SYS_DEFAULT_FLOAT_DECIMALS); +} + +int +sys_double_to_chars_fast(double f, char *outbuf, int maxlen, int decimals, int compact) +{ + enum { + FRAC_SIZE = 52 + , EXP_SIZE = 11 + , EXP_MASK = (1ll << EXP_SIZE) - 1 + , FRAC_MASK = (1ll << FRAC_SIZE) - 1 + , FRAC_MASK2 = (1ll << (FRAC_SIZE + 1)) - 1 + , MAX_FLOAT = 1ll << (FRAC_SIZE+1) + }; + + long long mantissa, int_part, int_part2, frac_part; + short exp; + int sign, i, n, m, max; + double absf; + union { long long L; double F; } x; + char c, *p = outbuf; + int digit, roundup; + + x.F = f; + + exp = (x.L >> FRAC_SIZE) & EXP_MASK; + mantissa = x.L & FRAC_MASK; + sign = x.L >= 0 ? 1 : -1; + if (exp == EXP_MASK) { + if (mantissa == 0) { + if (sign == -1) + *p++ = '-'; + *p++ = 'i'; + *p++ = 'n'; + *p++ = 'f'; + } else { + *p++ = 'n'; + *p++ = 'a'; + *p++ = 'n'; + } + *p = '\0'; + return p - outbuf; + } + + exp -= EXP_MASK >> 1; + mantissa |= (1ll << FRAC_SIZE); + frac_part = 0; + int_part = 0; + absf = f * sign; + + /* Don't bother with optimizing too large numbers and decimals */ + if (absf > MAX_FLOAT || decimals > maxlen-17) { + int len = erts_snprintf(outbuf, maxlen, "%.*f", decimals, f); + if (len >= maxlen) + return -1; + p = outbuf + len; + /* Delete trailing zeroes */ + if (compact) + p = float_first_trailing_zero(outbuf + len); + *p = '\0'; + return p - outbuf; + } + + if (exp >= FRAC_SIZE) + int_part = mantissa << (exp - FRAC_SIZE); + else if (exp >= 0) { + int_part = mantissa >> (FRAC_SIZE - exp); + frac_part = (mantissa << (exp + 1)) & FRAC_MASK2; + } + else /* if (exp < 0) */ + frac_part = (mantissa & FRAC_MASK2) >> -(exp + 1); + + if (int_part == 0) { + if (sign == -1) + *p++ = '-'; + *p++ = '0'; + } else { + int ret; + while (int_part != 0) { + int_part2 = int_part / 10; + *p++ = (char)(int_part - ((int_part2 << 3) + (int_part2 << 1)) + '0'); + int_part = int_part2; + } + if (sign == -1) + *p++ = '-'; + /* Reverse string */ + ret = p - outbuf; + for (i = 0, n = ret/2; i < n; i++) { + int j = ret - i - 1; + c = outbuf[i]; + outbuf[i] = outbuf[j]; + outbuf[j] = c; + } + } + if (decimals != 0) + *p++ = '.'; + + max = maxlen - (p - outbuf) - 1 /* leave room for trailing '\0' */; + if (max > decimals) + max = decimals; + for (m = 0; m < max; m++) { + /* frac_part *= 10; */ + frac_part = (frac_part << 3) + (frac_part << 1); + + *p++ = (char)((frac_part >> (FRAC_SIZE + 1)) + '0'); + frac_part &= FRAC_MASK2; + } + + roundup = 0; + /* Rounding - look at the next digit */ + frac_part = (frac_part << 3) + (frac_part << 1); + digit = (frac_part >> (FRAC_SIZE + 1)); + if (digit > 5) + roundup = 1; + else if (digit == 5) { + frac_part &= FRAC_MASK2; + if (frac_part != 0) roundup = 1; + } + if (roundup) { + char d; + int pos = p - outbuf - 1; + do { + d = outbuf[pos]; + if (d == '-') break; + if (d == '.') continue; + if (++d != ':') { + outbuf[pos] = d; + break; + } + outbuf[pos] = '0'; + } while (--pos); + } + + /* Delete trailing zeroes */ + if (compact && *(p - 1) == '0') + p = float_first_trailing_zero(--p); + *p = '\0'; + return p - outbuf; +} diff --git a/erts/emulator/sys/unix/sys_float.c b/erts/emulator/sys/unix/sys_float.c index 3fcb4d88dc..6875c17a75 100644 --- a/erts/emulator/sys/unix/sys_float.c +++ b/erts/emulator/sys/unix/sys_float.c @@ -735,7 +735,7 @@ void erts_sys_unblock_fpe(int unmasked) /* ** Convert a double to ascii format 0.dddde[+|-]ddd - ** return number of characters converted + ** return number of characters converted or -1 if error. ** ** These two functions should maybe use localeconv() to pick up ** the current radix character, but since it is uncertain how @@ -745,11 +745,12 @@ void erts_sys_unblock_fpe(int unmasked) */ int -sys_double_to_chars(double fp, char *buffer, size_t buffer_size) +sys_double_to_chars_ext(double fp, char *buffer, size_t buffer_size, size_t decimals) { char *s = buffer; - - (void) erts_snprintf(buffer, buffer_size, "%.20e", fp); + + if (erts_snprintf(buffer, buffer_size, "%.*e", decimals, fp) >= buffer_size) + return -1; /* Search upto decimal point */ if (*s == '+' || *s == '-') s++; while (ISDIGIT(*s)) s++; diff --git a/erts/emulator/sys/win32/sys_float.c b/erts/emulator/sys/win32/sys_float.c index 09dad89140..960edaa7a5 100644 --- a/erts/emulator/sys/win32/sys_float.c +++ b/erts/emulator/sys/win32/sys_float.c @@ -114,15 +114,16 @@ sys_chars_to_double(char *buf, double *fp) /* ** Convert a double to ascii format 0.dddde[+|-]ddd -** return number of characters converted +** return number of characters converted or -1 if error. */ int -sys_double_to_chars(double fp, char *buffer, size_t buffer_size) +sys_double_to_chars_ext(double fp, char *buffer, size_t buffer_size, size_t decimals) { char *s = buffer; - - (void) erts_snprintf(buffer, buffer_size, "%.20e", fp); + + if (erts_snprintf(buffer, buffer_size, "%.*e", decimals, fp) >= buffer_size) + return -1; /* Search upto decimal point */ if (*s == '+' || *s == '-') s++; while (isdigit(*s)) s++; |