LCOV - code coverage report
Current view: top level - include/flatcc/portable - grisu3_math.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 40 67.5 %
Date: 2016-11-30 13:12:14 Functions: 1 3 33.3 %
Branches: 12 28 42.9 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2016 Mikkel F. Jørgensen, dvide.com
       3                 :            :  * Copyright author of MathGeoLib (https://github.com/juj)
       4                 :            :  *
       5                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       6                 :            :  * you may not use this file except in compliance with the License.
       7                 :            :  * You may obtain a copy of the License at
       8                 :            :  *
       9                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
      10                 :            :  *
      11                 :            :  * Unless required by applicable law or agreed to in writing, software
      12                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      13                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14                 :            :  * See the License for the specific language governing permissions and
      15                 :            :  * limitations under the License. http://www.apache.org/licenses/LICENSE-2.0
      16                 :            :  */
      17                 :            : 
      18                 :            : /* 2016-02-02: Updated by mikkelfj
      19                 :            :  *
      20                 :            :  * Extracted from MatGeoLib grisu3.c, Apache 2.0 license, and extended.
      21                 :            :  *
      22                 :            :  * This file is usually include via grisu3_print.h or grisu3_parse.h.
      23                 :            :  *
      24                 :            :  * The original MatGeoLib dtoa_grisu3 implementation is largely
      25                 :            :  * unchanged except for the uint64 to double cast. The remaining changes
      26                 :            :  * are file structure, name changes, and new additions for parsing:
      27                 :            :  *
      28                 :            :  * - Split into header files only:
      29                 :            :  *   grisu3_math.h, grisu3_print.h, (added grisu3_parse.h)
      30                 :            :  *
      31                 :            :  * - names prefixed with grisu3_, grisu3_diy_fp_, GRISU3_.
      32                 :            :  * - added static to all functions.
      33                 :            :  * - disabled clang unused function warnings.
      34                 :            :  * - guarded <stdint.h> to allow for alternative impl.
      35                 :            :  * - added extra numeric constants needed for parsing.
      36                 :            :  * - added dec_pow, cast_double_from_diy_fp.
      37                 :            :  * - changed some function names for consistency.
      38                 :            :  * - moved printing specific grisu3 functions to grisu3_print.h.
      39                 :            :  * - changed double to uint64 cast to avoid aliasing.
      40                 :            :  * - added new grisu3_parse.h for parsing doubles.
      41                 :            :  * - grisu3_print_double (dtoa_grisu3) format .1 as 0.1 needed for valid JSON output
      42                 :            :  *   and grisu3_parse_double wouldn't consume it.
      43                 :            :  * - grsu3_print_double changed formatting to prefer 0.012 over 1.2e-2.
      44                 :            :  *
      45                 :            :  * These changes make it possible to include the files as headers only
      46                 :            :  * in other software libraries without risking name conflicts, and to
      47                 :            :  * extend the implementation with a port of Googles Double Conversion
      48                 :            :  * strtod functionality for parsing doubles.
      49                 :            :  *
      50                 :            :  * Extracted from: rev. 915501a / Dec 22, 2015
      51                 :            :  * <https://github.com/juj/MathGeoLib/blob/master/src/Math/grisu3.c>
      52                 :            :  * MathGeoLib License: http://www.apache.org/licenses/LICENSE-2.0.html
      53                 :            :  */
      54                 :            : 
      55                 :            : #ifndef GRISU3_MATH_H
      56                 :            : #define GRISU3_MATH_H
      57                 :            : 
      58                 :            : /* Guarded to allow inclusion of pstdint.h first, if stdint.h is not supported. */
      59                 :            : #ifndef UINT8_MAX
      60                 :            : #include <stdint.h> // uint64_t etc.
      61                 :            : #endif
      62                 :            : #include <assert.h> // assert
      63                 :            : 
      64                 :            : #ifdef _MSC_VER
      65                 :            : #pragma warning(disable : 4204) // nonstandard extension used : non-constant aggregate initializer
      66                 :            : #endif
      67                 :            : 
      68                 :            : #define GRISU3_D64_SIGN             0x8000000000000000ULL
      69                 :            : #define GRISU3_D64_EXP_MASK         0x7FF0000000000000ULL
      70                 :            : #define GRISU3_D64_FRACT_MASK       0x000FFFFFFFFFFFFFULL
      71                 :            : #define GRISU3_D64_IMPLICIT_ONE     0x0010000000000000ULL
      72                 :            : #define GRISU3_D64_EXP_POS          52
      73                 :            : #define GRISU3_D64_EXP_BIAS         1075
      74                 :            : #define GRISU3_D64_DENORM_EXP       (-GRISU3_D64_EXP_BIAS + 1)
      75                 :            : #define GRISU3_DIY_FP_FRACT_SIZE    64
      76                 :            : #define GRISU3_D_1_LOG2_10          0.30102999566398114 // 1 / lg(10)
      77                 :            : #define GRISU3_MIN_TARGET_EXP       -60
      78                 :            : #define GRISU3_MASK32               0xFFFFFFFFULL
      79                 :            : #define GRISU3_MIN_CACHED_EXP       -348
      80                 :            : #define GRISU3_MAX_CACHED_EXP       340
      81                 :            : #define GRISU3_CACHED_EXP_STEP      8
      82                 :            : #define GRISU3_D64_MAX_DEC_EXP      309
      83                 :            : #define GRISU3_D64_MIN_DEC_EXP      -324
      84                 :            : #define GRISU3_D64_INF              GRISU3_D64_EXP_MASK
      85                 :            : 
      86                 :            : #define GRISU3_MIN(x,y) ((x) <= (y) ? (x) : (y))
      87                 :            : #define GRISU3_MAX(x,y) ((x) >= (y) ? (x) : (y))
      88                 :            : 
      89                 :            : 
      90                 :            : typedef struct grisu3_diy_fp
      91                 :            : {
      92                 :            :     uint64_t f;
      93                 :            :     int e;
      94                 :            : } grisu3_diy_fp_t;
      95                 :            : 
      96                 :            : typedef struct grisu3_diy_fp_power
      97                 :            : {
      98                 :            :     uint64_t fract;
      99                 :            :     int16_t b_exp, d_exp;
     100                 :            : } grisu3_diy_fp_power_t;
     101                 :            : 
     102                 :            : typedef union {
     103                 :            :     uint64_t u64;
     104                 :            :     double d64;
     105                 :            : } grisu3_cast_double_t;
     106                 :            : 
     107                 :            : static uint64_t grisu3_cast_uint64_from_double(double d)
     108                 :            : {
     109                 :            :     grisu3_cast_double_t cd;
     110                 :            :     cd.d64 = d;
     111                 :          8 :     return cd.u64;
     112                 :            : }
     113                 :            : 
     114                 :            : static double grisu3_cast_double_from_uint64(uint64_t u)
     115                 :            : {
     116                 :            :     grisu3_cast_double_t cd;
     117                 :          0 :     cd.u64 = u;
     118                 :            :     return cd.d64;
     119                 :            : }
     120                 :            : 
     121                 :            : #define grisu3_double_infinity grisu3_cast_double_from_uint64(GRISU3_D64_INF)
     122                 :            : #define grisu3_double_nan grisu3_cast_double_from_uint64(GRISU3_D64_INF + 1)
     123                 :            : 
     124                 :            : static const grisu3_diy_fp_power_t grisu3_diy_fp_pow_cache[] =
     125                 :            : {
     126                 :            :     { 0xfa8fd5a0081c0288ULL, -1220, -348 },
     127                 :            :     { 0xbaaee17fa23ebf76ULL, -1193, -340 },
     128                 :            :     { 0x8b16fb203055ac76ULL, -1166, -332 },
     129                 :            :     { 0xcf42894a5dce35eaULL, -1140, -324 },
     130                 :            :     { 0x9a6bb0aa55653b2dULL, -1113, -316 },
     131                 :            :     { 0xe61acf033d1a45dfULL, -1087, -308 },
     132                 :            :     { 0xab70fe17c79ac6caULL, -1060, -300 },
     133                 :            :     { 0xff77b1fcbebcdc4fULL, -1034, -292 },
     134                 :            :     { 0xbe5691ef416bd60cULL, -1007, -284 },
     135                 :            :     { 0x8dd01fad907ffc3cULL,  -980, -276 },
     136                 :            :     { 0xd3515c2831559a83ULL,  -954, -268 },
     137                 :            :     { 0x9d71ac8fada6c9b5ULL,  -927, -260 },
     138                 :            :     { 0xea9c227723ee8bcbULL,  -901, -252 },
     139                 :            :     { 0xaecc49914078536dULL,  -874, -244 },
     140                 :            :     { 0x823c12795db6ce57ULL,  -847, -236 },
     141                 :            :     { 0xc21094364dfb5637ULL,  -821, -228 },
     142                 :            :     { 0x9096ea6f3848984fULL,  -794, -220 },
     143                 :            :     { 0xd77485cb25823ac7ULL,  -768, -212 },
     144                 :            :     { 0xa086cfcd97bf97f4ULL,  -741, -204 },
     145                 :            :     { 0xef340a98172aace5ULL,  -715, -196 },
     146                 :            :     { 0xb23867fb2a35b28eULL,  -688, -188 },
     147                 :            :     { 0x84c8d4dfd2c63f3bULL,  -661, -180 },
     148                 :            :     { 0xc5dd44271ad3cdbaULL,  -635, -172 },
     149                 :            :     { 0x936b9fcebb25c996ULL,  -608, -164 },
     150                 :            :     { 0xdbac6c247d62a584ULL,  -582, -156 },
     151                 :            :     { 0xa3ab66580d5fdaf6ULL,  -555, -148 },
     152                 :            :     { 0xf3e2f893dec3f126ULL,  -529, -140 },
     153                 :            :     { 0xb5b5ada8aaff80b8ULL,  -502, -132 },
     154                 :            :     { 0x87625f056c7c4a8bULL,  -475, -124 },
     155                 :            :     { 0xc9bcff6034c13053ULL,  -449, -116 },
     156                 :            :     { 0x964e858c91ba2655ULL,  -422, -108 },
     157                 :            :     { 0xdff9772470297ebdULL,  -396, -100 },
     158                 :            :     { 0xa6dfbd9fb8e5b88fULL,  -369,  -92 },
     159                 :            :     { 0xf8a95fcf88747d94ULL,  -343,  -84 },
     160                 :            :     { 0xb94470938fa89bcfULL,  -316,  -76 },
     161                 :            :     { 0x8a08f0f8bf0f156bULL,  -289,  -68 },
     162                 :            :     { 0xcdb02555653131b6ULL,  -263,  -60 },
     163                 :            :     { 0x993fe2c6d07b7facULL,  -236,  -52 },
     164                 :            :     { 0xe45c10c42a2b3b06ULL,  -210,  -44 },
     165                 :            :     { 0xaa242499697392d3ULL,  -183,  -36 },
     166                 :            :     { 0xfd87b5f28300ca0eULL,  -157,  -28 },
     167                 :            :     { 0xbce5086492111aebULL,  -130,  -20 },
     168                 :            :     { 0x8cbccc096f5088ccULL,  -103,  -12 },
     169                 :            :     { 0xd1b71758e219652cULL,   -77,   -4 },
     170                 :            :     { 0x9c40000000000000ULL,   -50,    4 },
     171                 :            :     { 0xe8d4a51000000000ULL,   -24,   12 },
     172                 :            :     { 0xad78ebc5ac620000ULL,     3,   20 },
     173                 :            :     { 0x813f3978f8940984ULL,    30,   28 },
     174                 :            :     { 0xc097ce7bc90715b3ULL,    56,   36 },
     175                 :            :     { 0x8f7e32ce7bea5c70ULL,    83,   44 },
     176                 :            :     { 0xd5d238a4abe98068ULL,   109,   52 },
     177                 :            :     { 0x9f4f2726179a2245ULL,   136,   60 },
     178                 :            :     { 0xed63a231d4c4fb27ULL,   162,   68 },
     179                 :            :     { 0xb0de65388cc8ada8ULL,   189,   76 },
     180                 :            :     { 0x83c7088e1aab65dbULL,   216,   84 },
     181                 :            :     { 0xc45d1df942711d9aULL,   242,   92 },
     182                 :            :     { 0x924d692ca61be758ULL,   269,  100 },
     183                 :            :     { 0xda01ee641a708deaULL,   295,  108 },
     184                 :            :     { 0xa26da3999aef774aULL,   322,  116 },
     185                 :            :     { 0xf209787bb47d6b85ULL,   348,  124 },
     186                 :            :     { 0xb454e4a179dd1877ULL,   375,  132 },
     187                 :            :     { 0x865b86925b9bc5c2ULL,   402,  140 },
     188                 :            :     { 0xc83553c5c8965d3dULL,   428,  148 },
     189                 :            :     { 0x952ab45cfa97a0b3ULL,   455,  156 },
     190                 :            :     { 0xde469fbd99a05fe3ULL,   481,  164 },
     191                 :            :     { 0xa59bc234db398c25ULL,   508,  172 },
     192                 :            :     { 0xf6c69a72a3989f5cULL,   534,  180 },
     193                 :            :     { 0xb7dcbf5354e9beceULL,   561,  188 },
     194                 :            :     { 0x88fcf317f22241e2ULL,   588,  196 },
     195                 :            :     { 0xcc20ce9bd35c78a5ULL,   614,  204 },
     196                 :            :     { 0x98165af37b2153dfULL,   641,  212 },
     197                 :            :     { 0xe2a0b5dc971f303aULL,   667,  220 },
     198                 :            :     { 0xa8d9d1535ce3b396ULL,   694,  228 },
     199                 :            :     { 0xfb9b7cd9a4a7443cULL,   720,  236 },
     200                 :            :     { 0xbb764c4ca7a44410ULL,   747,  244 },
     201                 :            :     { 0x8bab8eefb6409c1aULL,   774,  252 },
     202                 :            :     { 0xd01fef10a657842cULL,   800,  260 },
     203                 :            :     { 0x9b10a4e5e9913129ULL,   827,  268 },
     204                 :            :     { 0xe7109bfba19c0c9dULL,   853,  276 },
     205                 :            :     { 0xac2820d9623bf429ULL,   880,  284 },
     206                 :            :     { 0x80444b5e7aa7cf85ULL,   907,  292 },
     207                 :            :     { 0xbf21e44003acdd2dULL,   933,  300 },
     208                 :            :     { 0x8e679c2f5e44ff8fULL,   960,  308 },
     209                 :            :     { 0xd433179d9c8cb841ULL,   986,  316 },
     210                 :            :     { 0x9e19db92b4e31ba9ULL,  1013,  324 },
     211                 :            :     { 0xeb96bf6ebadf77d9ULL,  1039,  332 },
     212                 :            :     { 0xaf87023b9bf0ee6bULL,  1066,  340 }
     213                 :            : };
     214                 :            : 
     215                 :            : /* Avoid dependence on lib math to get (int)ceil(v) */
     216                 :            : static int grisu3_iceil(double v)
     217                 :            : {
     218                 :          4 :     int k = (int)v;
     219         [ +  - ]:          4 :     if (v < 0) return k;
     220         [ +  - ]:          4 :     return v - k == 0 ? k : k + 1;
     221                 :            : }
     222                 :            : 
     223                 :            : static int grisu3_diy_fp_cached_pow(int exp, grisu3_diy_fp_t *p)
     224                 :            : {
     225                 :          4 :     int k = grisu3_iceil((exp+GRISU3_DIY_FP_FRACT_SIZE-1) * GRISU3_D_1_LOG2_10);
     226                 :          4 :     int i = (k-GRISU3_MIN_CACHED_EXP-1) / GRISU3_CACHED_EXP_STEP + 1;
     227                 :          4 :     p->f = grisu3_diy_fp_pow_cache[i].fract;
     228                 :          4 :     p->e = grisu3_diy_fp_pow_cache[i].b_exp;
     229                 :          4 :     return grisu3_diy_fp_pow_cache[i].d_exp;
     230                 :            : }
     231                 :            : 
     232                 :            : static grisu3_diy_fp_t grisu3_diy_fp_minus(grisu3_diy_fp_t x, grisu3_diy_fp_t y)
     233                 :            : {
     234                 :          8 :     grisu3_diy_fp_t d; d.f = x.f - y.f; d.e = x.e;
     235                 :            :     assert(x.e == y.e && x.f >= y.f);
     236                 :            :     return d;
     237                 :            : }
     238                 :            : 
     239                 :         24 : static grisu3_diy_fp_t grisu3_diy_fp_multiply(grisu3_diy_fp_t x, grisu3_diy_fp_t y)
     240                 :            : {
     241                 :            :     uint64_t a, b, c, d, ac, bc, ad, bd, tmp;
     242                 :            :     grisu3_diy_fp_t r;
     243                 :         12 :     a = x.f >> 32; b = x.f & GRISU3_MASK32;
     244                 :         12 :     c = y.f >> 32; d = y.f & GRISU3_MASK32;
     245                 :         12 :     ac = a*c; bc = b*c;
     246                 :         12 :     ad = a*d; bd = b*d;
     247                 :         12 :     tmp = (bd >> 32) + (ad & GRISU3_MASK32) + (bc & GRISU3_MASK32);
     248                 :         12 :     tmp += 1U << 31; // round
     249                 :         12 :     r.f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
     250                 :         12 :     r.e = x.e + y.e + 64;
     251                 :         12 :     return r;
     252                 :            : }
     253                 :            : 
     254                 :            : static grisu3_diy_fp_t grisu3_diy_fp_normalize(grisu3_diy_fp_t n)
     255                 :            : {
     256                 :            :     assert(n.f != 0);
     257 [ +  + ][ +  + ]:         16 :     while(!(n.f & 0xFFC0000000000000ULL)) { n.f <<= 10; n.e -= 10; }
     258 [ +  + ][ -  + ]:         12 :     while(!(n.f & GRISU3_D64_SIGN)) { n.f <<= 1; --n.e; }
     259                 :            :     return n;
     260                 :            : }
     261                 :            : 
     262                 :            : static grisu3_diy_fp_t grisu3_cast_diy_fp_from_double(double d)
     263                 :            : {
     264                 :            :     grisu3_diy_fp_t fp;
     265                 :            :     uint64_t u64 = grisu3_cast_uint64_from_double(d);
     266         [ -  + ]:          4 :     if (!(u64 & GRISU3_D64_EXP_MASK)) { fp.f = u64 & GRISU3_D64_FRACT_MASK; fp.e = 1 - GRISU3_D64_EXP_BIAS; }
     267                 :          4 :     else { fp.f = (u64 & GRISU3_D64_FRACT_MASK) + GRISU3_D64_IMPLICIT_ONE; fp.e = (int)((u64 & GRISU3_D64_EXP_MASK) >> GRISU3_D64_EXP_POS) - GRISU3_D64_EXP_BIAS; }
     268                 :            :     return fp;
     269                 :            : }
     270                 :            : 
     271                 :          0 : static double grisu3_cast_double_from_diy_fp(grisu3_diy_fp_t n)
     272                 :            : {
     273                 :            :     const uint64_t hidden_bit = GRISU3_D64_IMPLICIT_ONE;
     274                 :            :     const uint64_t frac_mask = GRISU3_D64_FRACT_MASK;
     275                 :            :     const int denorm_exp = GRISU3_D64_DENORM_EXP;
     276                 :            :     const int exp_bias = GRISU3_D64_EXP_BIAS;
     277                 :            :     const int exp_pos = GRISU3_D64_EXP_POS;
     278                 :            : 
     279                 :          0 :     grisu3_diy_fp_t v = n;
     280                 :            :     uint64_t e_biased;
     281                 :            : 
     282         [ #  # ]:          0 :     while (v.f > hidden_bit + frac_mask) {
     283                 :          0 :         v.f >>= 1;
     284                 :          0 :         ++v.e;
     285                 :            :     }
     286         [ #  # ]:          0 :     if (v.e < denorm_exp) {
     287                 :            :         return 0.0;
     288                 :            :     }
     289 [ #  # ][ #  # ]:          0 :     while (v.e > denorm_exp && (v.f & hidden_bit) == 0) {
     290                 :          0 :         v.f <<= 1;
     291                 :          0 :         --v.e;
     292                 :            :     }
     293 [ #  # ][ #  # ]:          0 :     if (v.e == denorm_exp && (v.f & hidden_bit) == 0) {
     294                 :            :         e_biased = 0;
     295                 :            :     } else {
     296                 :          0 :         e_biased = (uint64_t)(v.e + exp_bias);
     297                 :            :     }
     298                 :          0 :     return grisu3_cast_double_from_uint64((v.f & frac_mask) | (e_biased << exp_pos));
     299                 :            : }
     300                 :            : 
     301                 :            : // pow10_cache[i] = 10^(i-1)
     302                 :            : static const unsigned int grisu3_pow10_cache[] = { 0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
     303                 :            : 
     304                 :            : static int grisu3_largest_pow10(uint32_t n, int n_bits, uint32_t *power)
     305                 :            : {
     306                 :          4 :     int guess = ((n_bits + 1) * 1233 >> 12) + 1/*skip first entry*/;
     307         [ +  + ]:          4 :     if (n < grisu3_pow10_cache[guess]) --guess; // We don't have any guarantees that 2^n_bits <= n.
     308                 :          4 :     *power = grisu3_pow10_cache[guess];
     309                 :            :     return guess;
     310                 :            : }
     311                 :            : 
     312                 :            : #endif /* GRISU3_MATH_H */

Generated by: LCOV version 1.12