LCOV - code coverage report
Current view: top level - src - double.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 32 32 100.0 %
Date: 2026-07-14 21:28:07 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 18 18 100.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2019-2026 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #include "big_decimal.h"
       5                 :            : #include "char_utils.h"
       6                 :            : #include "decimal_to_double.h"
       7                 :            : #include "double_to_decimal.h"
       8                 :            : #include "number_utils.h"
       9                 :            : #include "parse_decimal.h"
      10                 :            : #include "read_utils.h"
      11                 :            : #include "result.h"
      12                 :            : #include "scientific.h"
      13                 :            : #include "write_utils.h"
      14                 :            : 
      15                 :            : #include <exess/exess.h>
      16                 :            : 
      17                 :            : #include <math.h>
      18                 :            : #include <stdint.h>
      19                 :            : #include <string.h>
      20                 :            : 
      21                 :            : #ifndef DBL_DECIMAL_DIG
      22                 :            : #  define DBL_DECIMAL_DIG 17U
      23                 :            : #endif
      24                 :            : 
      25                 :            : typedef struct {
      26                 :            :   BigDecimalKind kind;
      27                 :            :   unsigned       length;
      28                 :            :   const char*    string;
      29                 :            : } SpecialCase;
      30                 :            : 
      31                 :            : static const SpecialCase special_cases[] = {
      32                 :            :   {EXESS_NAN, 3U, "NaN"},
      33                 :            :   {EXESS_NEGATIVE_INFINITY, 4U, "-INF"},
      34                 :            :   {EXESS_POSITIVE_INFINITY, 3U, "INF"},
      35                 :            :   {EXESS_POSITIVE_INFINITY, 4U, "+INF"},
      36                 :            :   {EXESS_NAN, 0U, ""},
      37                 :            : };
      38                 :            : 
      39                 :            : static EXESS_NONBLOCKING ExessResult
      40                 :      27565 : parse_double(const char* const str,
      41                 :            :              BigDecimal* const out,
      42                 :            :              const size_t      digits_size,
      43                 :            :              char* const       digits)
      44                 :            : {
      45                 :            :   // Handle non-numeric special cases
      46         [ +  + ]:     135858 :   for (const SpecialCase* c = special_cases; c->length; ++c) {
      47         [ +  + ]:     109270 :     if (!strcmp(str, c->string)) {
      48                 :        977 :       out->kind = c->kind;
      49                 :        977 :       return RESULT(EXESS_SUCCESS, c->length);
      50                 :            :     }
      51                 :            :   }
      52                 :            : 
      53                 :            :   // Read mantissa as a decimal
      54                 :      26588 :   const ExessResult r = parse_decimal(str, out, digits_size, digits);
      55         [ +  + ]:      26588 :   if (r.status > EXESS_LOSS) {
      56                 :         12 :     return r;
      57                 :            :   }
      58                 :            : 
      59                 :      26576 :   size_t i = r.count;
      60                 :            : 
      61                 :            :   // Read exponent
      62                 :      26576 :   int abs_expt  = 0;
      63                 :      26576 :   int expt_sign = 1;
      64   [ +  +  +  + ]:      26576 :   if (str[i] == 'e' || str[i] == 'E') {
      65                 :      26543 :     ++i;
      66                 :            : 
      67   [ +  +  +  + ]:      26543 :     if (!is_sign(str[i]) && !is_digit(str[i])) {
      68                 :          6 :       return RESULT(EXESS_EXPECTED_DIGIT, i);
      69                 :            :     }
      70                 :            : 
      71                 :      26537 :     i += read_sign(&expt_sign, &str[i]);
      72         [ +  + ]:      81216 :     while (is_digit(str[i])) {
      73                 :      54679 :       abs_expt = (abs_expt * 10) + (str[i++] - '0');
      74                 :            :     }
      75                 :            :   }
      76                 :            : 
      77                 :            :   // Calculate final output exponent
      78                 :      26570 :   out->expt = (int16_t)(out->expt + (expt_sign * abs_expt));
      79                 :            : 
      80                 :      26570 :   return RESULT(r.status, i);
      81                 :            : }
      82                 :            : 
      83                 :            : EXESS_NONBLOCKING ExessResult
      84                 :      27565 : exess_read_double(const char* const str, double* const out)
      85                 :            : {
      86                 :      27565 :   char digits[DBL_DECIMAL_DIG + 1U] = {0};
      87                 :            : 
      88                 :      27565 :   const size_t      i  = skip_whitespace(str);
      89                 :      27565 :   BigDecimal        in = {EXESS_NAN, 0U, 0};
      90                 :      27565 :   const ExessResult r  = parse_double(str + i, &in, sizeof(digits), digits);
      91                 :            : 
      92         [ +  + ]:      27565 :   *out = (r.status > EXESS_LOSS) ? (double)NAN : decimal_to_double(in, digits);
      93                 :      27565 :   return RESULT(r.status, i + r.count);
      94                 :            : }
      95                 :            : 
      96                 :            : EXESS_NONBLOCKING ExessResult
      97                 :      14489 : exess_write_double(const double value, const size_t buf_size, char* const buf)
      98                 :            : {
      99                 :      14489 :   char digits[DBL_DECIMAL_DIG + 1U] = {0};
     100                 :            : 
     101                 :      14489 :   const BigDecimal  decimal = double_to_decimal(value, DBL_DECIMAL_DIG, digits);
     102                 :      14489 :   const ExessResult r       = write_scientific(decimal, digits, buf_size, buf);
     103                 :      14489 :   return end_write(r.status, buf_size, buf, r.count);
     104                 :            : }

Generated by: LCOV version 1.16