0.2.0-dev Download ZIP
119 lines ·4.3 KB Raw
| 1 | diff --git a/thirdparty/grisu2/grisu2.h b/thirdparty/grisu2/grisu2.h |
| 2 | index f140e1a192..316a727ce0 100644 |
| 3 | --- a/thirdparty/grisu2/grisu2.h |
| 4 | +++ b/thirdparty/grisu2/grisu2.h |
| 5 | @@ -1,15 +1,11 @@ |
| 6 | -#ifndef SIMDJSON_SRC_TO_CHARS_CPP |
| 7 | -#define SIMDJSON_SRC_TO_CHARS_CPP |
| 8 | - |
| 9 | -#include <base.h> |
| 10 | +#pragma once |
| 11 | |
| 12 | #include <cstring> |
| 13 | #include <cstdint> |
| 14 | #include <array> |
| 15 | #include <cmath> |
| 16 | |
| 17 | -namespace simdjson { |
| 18 | -namespace internal { |
| 19 | +namespace grisu2 { |
| 20 | /*! |
| 21 | implements the Grisu2 algorithm for binary to decimal floating-point |
| 22 | conversion. |
| 23 | @@ -26,7 +22,6 @@ PLDI 2010 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and |
| 24 | Accurately", Proceedings of the ACM SIGPLAN 1996 Conference on Programming |
| 25 | Language Design and Implementation, PLDI 1996 |
| 26 | */ |
| 27 | -namespace dtoa_impl { |
| 28 | |
| 29 | template <typename Target, typename Source> |
| 30 | Target reinterpret_bits(const Source source) { |
| 31 | @@ -718,7 +713,7 @@ v = buf * 10^decimal_exponent |
| 32 | len is the length of the buffer (number of decimal digits) |
| 33 | The buffer must be large enough, i.e. >= max_digits10. |
| 34 | */ |
| 35 | -inline void grisu2(char *buf, int &len, int &decimal_exponent, diyfp m_minus, |
| 36 | +inline void grisu2_core(char *buf, int &len, int &decimal_exponent, diyfp m_minus, |
| 37 | diyfp v, diyfp m_plus) { |
| 38 | |
| 39 | // --------(-----------------------+-----------------------)-------- (A) |
| 40 | @@ -775,7 +770,7 @@ len is the length of the buffer (number of decimal digits) |
| 41 | The buffer must be large enough, i.e. >= max_digits10. |
| 42 | */ |
| 43 | template <typename FloatType> |
| 44 | -void grisu2(char *buf, int &len, int &decimal_exponent, FloatType value) { |
| 45 | +void grisu2_wrap(char *buf, int &len, int &decimal_exponent, FloatType value) { |
| 46 | static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3, |
| 47 | "internal error: not enough precision"); |
| 48 | |
| 49 | @@ -804,7 +799,7 @@ void grisu2(char *buf, int &len, int &decimal_exponent, FloatType value) { |
| 50 | const boundaries w = compute_boundaries(value); |
| 51 | #endif |
| 52 | |
| 53 | - grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus); |
| 54 | + grisu2_core(buf, len, decimal_exponent, w.minus, w.w, w.plus); |
| 55 | } |
| 56 | |
| 57 | /*! |
| 58 | @@ -858,10 +853,7 @@ inline char *format_buffer(char *buf, int len, int decimal_exponent, |
| 59 | // len <= max_exp + 2 |
| 60 | |
| 61 | std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k)); |
| 62 | - // Make it look like a floating-point number (#362, #378) |
| 63 | - buf[n + 0] = '.'; |
| 64 | - buf[n + 1] = '0'; |
| 65 | - return buf + (static_cast<size_t>(n)) + 2; |
| 66 | + return buf + (static_cast<size_t>(n)); |
| 67 | } |
| 68 | |
| 69 | if (0 < n && n <= max_exp) { |
| 70 | @@ -903,8 +895,6 @@ inline char *format_buffer(char *buf, int len, int decimal_exponent, |
| 71 | return append_exponent(buf, n - 1); |
| 72 | } |
| 73 | |
| 74 | -} // namespace dtoa_impl |
| 75 | - |
| 76 | /*! |
| 77 | The format of the resulting decimal representation is similar to printf's %g |
| 78 | format. Returns an iterator pointing past-the-end of the decimal representation. |
| 79 | @@ -912,8 +902,8 @@ format. Returns an iterator pointing past-the-end of the decimal representation. |
| 80 | @note The buffer must be large enough. |
| 81 | @note The result is NOT null-terminated. |
| 82 | */ |
| 83 | -char *to_chars(char *first, const char *last, double value) { |
| 84 | - static_cast<void>(last); // maybe unused - fix warning |
| 85 | +template <typename FloatType> |
| 86 | +char *to_chars(char *first, FloatType value) { |
| 87 | bool negative = std::signbit(value); |
| 88 | if (negative) { |
| 89 | value = -value; |
| 90 | @@ -922,9 +912,6 @@ char *to_chars(char *first, const char *last, double value) { |
| 91 | |
| 92 | if (value == 0) // +-0 |
| 93 | { |
| 94 | - *first++ = '0'; |
| 95 | - // Make it look like a floating-point number (#362, #378) |
| 96 | - *first++ = '.'; |
| 97 | *first++ = '0'; |
| 98 | return first; |
| 99 | } |
| 100 | @@ -934,15 +921,11 @@ char *to_chars(char *first, const char *last, double value) { |
| 101 | // len is the length of the buffer, i.e. the number of decimal digits. |
| 102 | int len = 0; |
| 103 | int decimal_exponent = 0; |
| 104 | - dtoa_impl::grisu2(first, len, decimal_exponent, value); |
| 105 | + grisu2_wrap(first, len, decimal_exponent, value); |
| 106 | // Format the buffer like printf("%.*g", prec, value) |
| 107 | constexpr int kMinExp = -4; |
| 108 | constexpr int kMaxExp = std::numeric_limits<double>::digits10; |
| 109 | |
| 110 | - return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, |
| 111 | - kMaxExp); |
| 112 | + return format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp); |
| 113 | } |
| 114 | -} // namespace internal |
| 115 | -} // namespace simdjson |
| 116 | - |
| 117 | -#endif // SIMDJSON_SRC_TO_CHARS_CPP |
| 118 | \ No newline at end of file |
| 119 | +} // namespace grisu2 |