Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-12 09:59:55

0001 // Tencent is pleased to support the open source community by making RapidJSON available.
0002 // 
0003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
0004 //
0005 // Licensed under the MIT License (the "License"); you may not use this file except
0006 // in compliance with the License. You may obtain a copy of the License at
0007 //
0008 // http://opensource.org/licenses/MIT
0009 //
0010 // Unless required by applicable law or agreed to in writing, software distributed 
0011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
0012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
0013 // specific language governing permissions and limitations under the License.
0014 
0015 // This is a C++ header-only implementation of Grisu2 algorithm from the publication:
0016 // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with
0017 // integers." ACM Sigplan Notices 45.6 (2010): 233-243.
0018 
0019 #ifndef RAPIDJSON_DTOA_
0020 #define RAPIDJSON_DTOA_
0021 
0022 #include "itoa.h" // GetDigitsLut()
0023 #include "diyfp.h"
0024 #include "ieee754.h"
0025 
0026 RAPIDJSON_NAMESPACE_BEGIN
0027 namespace internal {
0028 
0029 #ifdef __GNUC__
0030 RAPIDJSON_DIAG_PUSH
0031 RAPIDJSON_DIAG_OFF(effc++)
0032 RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124
0033 #endif
0034 
0035 inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
0036     while (rest < wp_w && delta - rest >= ten_kappa &&
0037            (rest + ten_kappa < wp_w ||  /// closer
0038             wp_w - rest > rest + ten_kappa - wp_w)) {
0039         buffer[len - 1]--;
0040         rest += ten_kappa;
0041     }
0042 }
0043 
0044 inline int CountDecimalDigit32(uint32_t n) {
0045     // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
0046     if (n < 10) return 1;
0047     if (n < 100) return 2;
0048     if (n < 1000) return 3;
0049     if (n < 10000) return 4;
0050     if (n < 100000) return 5;
0051     if (n < 1000000) return 6;
0052     if (n < 10000000) return 7;
0053     if (n < 100000000) return 8;
0054     // Will not reach 10 digits in DigitGen()
0055     //if (n < 1000000000) return 9;
0056     //return 10;
0057     return 9;
0058 }
0059 
0060 inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) {
0061     static const uint64_t kPow10[] = { 1U, 10U, 100U, 1000U, 10000U, 100000U, 1000000U, 10000000U, 100000000U,
0062                                        1000000000U, 10000000000U, 100000000000U, 1000000000000U,
0063                                        10000000000000U, 100000000000000U, 1000000000000000U,
0064                                        10000000000000000U, 100000000000000000U, 1000000000000000000U,
0065                                        10000000000000000000U };
0066     const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
0067     const DiyFp wp_w = Mp - W;
0068     uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
0069     uint64_t p2 = Mp.f & (one.f - 1);
0070     int kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
0071     *len = 0;
0072 
0073     while (kappa > 0) {
0074         uint32_t d = 0;
0075         switch (kappa) {
0076             case  9: d = p1 /  100000000; p1 %=  100000000; break;
0077             case  8: d = p1 /   10000000; p1 %=   10000000; break;
0078             case  7: d = p1 /    1000000; p1 %=    1000000; break;
0079             case  6: d = p1 /     100000; p1 %=     100000; break;
0080             case  5: d = p1 /      10000; p1 %=      10000; break;
0081             case  4: d = p1 /       1000; p1 %=       1000; break;
0082             case  3: d = p1 /        100; p1 %=        100; break;
0083             case  2: d = p1 /         10; p1 %=         10; break;
0084             case  1: d = p1;              p1 =           0; break;
0085             default:;
0086         }
0087         if (d || *len)
0088             buffer[(*len)++] = static_cast<char>('0' + static_cast<char>(d));
0089         kappa--;
0090         uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
0091         if (tmp <= delta) {
0092             *K += kappa;
0093             GrisuRound(buffer, *len, delta, tmp, kPow10[kappa] << -one.e, wp_w.f);
0094             return;
0095         }
0096     }
0097 
0098     // kappa = 0
0099     for (;;) {
0100         p2 *= 10;
0101         delta *= 10;
0102         char d = static_cast<char>(p2 >> -one.e);
0103         if (d || *len)
0104             buffer[(*len)++] = static_cast<char>('0' + d);
0105         p2 &= one.f - 1;
0106         kappa--;
0107         if (p2 < delta) {
0108             *K += kappa;
0109             int index = -kappa;
0110             GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 20 ? kPow10[index] : 0));
0111             return;
0112         }
0113     }
0114 }
0115 
0116 inline void Grisu2(double value, char* buffer, int* length, int* K) {
0117     const DiyFp v(value);
0118     DiyFp w_m, w_p;
0119     v.NormalizedBoundaries(&w_m, &w_p);
0120 
0121     const DiyFp c_mk = GetCachedPower(w_p.e, K);
0122     const DiyFp W = v.Normalize() * c_mk;
0123     DiyFp Wp = w_p * c_mk;
0124     DiyFp Wm = w_m * c_mk;
0125     Wm.f++;
0126     Wp.f--;
0127     DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
0128 }
0129 
0130 inline char* WriteExponent(int K, char* buffer) {
0131     if (K < 0) {
0132         *buffer++ = '-';
0133         K = -K;
0134     }
0135 
0136     if (K >= 100) {
0137         *buffer++ = static_cast<char>('0' + static_cast<char>(K / 100));
0138         K %= 100;
0139         const char* d = GetDigitsLut() + K * 2;
0140         *buffer++ = d[0];
0141         *buffer++ = d[1];
0142     }
0143     else if (K >= 10) {
0144         const char* d = GetDigitsLut() + K * 2;
0145         *buffer++ = d[0];
0146         *buffer++ = d[1];
0147     }
0148     else
0149         *buffer++ = static_cast<char>('0' + static_cast<char>(K));
0150 
0151     return buffer;
0152 }
0153 
0154 inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
0155     const int kk = length + k;  // 10^(kk-1) <= v < 10^kk
0156 
0157     if (0 <= k && kk <= 21) {
0158         // 1234e7 -> 12340000000
0159         for (int i = length; i < kk; i++)
0160             buffer[i] = '0';
0161         buffer[kk] = '.';
0162         buffer[kk + 1] = '0';
0163         return &buffer[kk + 2];
0164     }
0165     else if (0 < kk && kk <= 21) {
0166         // 1234e-2 -> 12.34
0167         std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk));
0168         buffer[kk] = '.';
0169         if (0 > k + maxDecimalPlaces) {
0170             // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1
0171             // Remove extra trailing zeros (at least one) after truncation.
0172             for (int i = kk + maxDecimalPlaces; i > kk + 1; i--)
0173                 if (buffer[i] != '0')
0174                     return &buffer[i + 1];
0175             return &buffer[kk + 2]; // Reserve one zero
0176         }
0177         else
0178             return &buffer[length + 1];
0179     }
0180     else if (-6 < kk && kk <= 0) {
0181         // 1234e-6 -> 0.001234
0182         const int offset = 2 - kk;
0183         std::memmove(&buffer[offset], &buffer[0], static_cast<size_t>(length));
0184         buffer[0] = '0';
0185         buffer[1] = '.';
0186         for (int i = 2; i < offset; i++)
0187             buffer[i] = '0';
0188         if (length - kk > maxDecimalPlaces) {
0189             // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1
0190             // Remove extra trailing zeros (at least one) after truncation.
0191             for (int i = maxDecimalPlaces + 1; i > 2; i--)
0192                 if (buffer[i] != '0')
0193                     return &buffer[i + 1];
0194             return &buffer[3]; // Reserve one zero
0195         }
0196         else
0197             return &buffer[length + offset];
0198     }
0199     else if (kk < -maxDecimalPlaces) {
0200         // Truncate to zero
0201         buffer[0] = '0';
0202         buffer[1] = '.';
0203         buffer[2] = '0';
0204         return &buffer[3];
0205     }
0206     else if (length == 1) {
0207         // 1e30
0208         buffer[1] = 'e';
0209         return WriteExponent(kk - 1, &buffer[2]);
0210     }
0211     else {
0212         // 1234e30 -> 1.234e33
0213         std::memmove(&buffer[2], &buffer[1], static_cast<size_t>(length - 1));
0214         buffer[1] = '.';
0215         buffer[length + 1] = 'e';
0216         return WriteExponent(kk - 1, &buffer[0 + length + 2]);
0217     }
0218 }
0219 
0220 inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) {
0221     RAPIDJSON_ASSERT(maxDecimalPlaces >= 1);
0222     Double d(value);
0223     if (d.IsZero()) {
0224         if (d.Sign())
0225             *buffer++ = '-';     // -0.0, Issue #289
0226         buffer[0] = '0';
0227         buffer[1] = '.';
0228         buffer[2] = '0';
0229         return &buffer[3];
0230     }
0231     else {
0232         if (value < 0) {
0233             *buffer++ = '-';
0234             value = -value;
0235         }
0236         int length, K;
0237         Grisu2(value, buffer, &length, &K);
0238         return Prettify(buffer, length, K, maxDecimalPlaces);
0239     }
0240 }
0241 
0242 #ifdef __GNUC__
0243 RAPIDJSON_DIAG_POP
0244 #endif
0245 
0246 } // namespace internal
0247 RAPIDJSON_NAMESPACE_END
0248 
0249 #endif // RAPIDJSON_DTOA_