2019-02-06 22:50:52 +01:00
|
|
|
/****************************************************************************
|
2019-02-17 19:16:28 +01:00
|
|
|
* libs/libc/stdio/lib_dtoa_engine.c
|
2019-02-06 22:50:52 +01:00
|
|
|
*
|
|
|
|
* Copyright © 2018, Keith Packard
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-10 23:15:49 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
2019-02-17 19:16:28 +01:00
|
|
|
#include "lib_dtoa_engine.h"
|
2019-02-06 22:50:52 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* A bit of CPP trickery -- construct the floating-point value 10 ** DBL_DIG
|
|
|
|
* by pasting the value of DBL_DIG onto '1e' to
|
|
|
|
*/
|
|
|
|
|
toolchain/ghs: Fix green hills toolchain build Vela MIN_MANT error
"stdio/lib_dtoa_engine.c", line 102: error #166: invalid floating constant
if (x < MIN_MANT)
^
"stdio/lib_dtoa_engine.c", line 102: warning #1626-D: concatenation with "("
in macro "PASTE" does not create a valid token
if (x < MIN_MANT)
^
"stdio/lib_dtoa_engine.c", line 102: error #109: expression preceding
parentheses of apparent call must have (pointer-to-) function type
if (x < MIN_MANT)
^
"stdio/lib_dtoa_engine.c", line 107: error #166: invalid floating constant
if (y < MAX_MANT)
^
"stdio/lib_dtoa_engine.c", line 107: warning #1626-D: concatenation with "("
in macro "PASTE" does not create a valid token
if (y < MAX_MANT)
^
"stdio/lib_dtoa_engine.c", line 107: error #109: expression preceding
parentheses of apparent call must have (pointer-to-) function type
if (y < MAX_MANT)
^
"stdio/lib_dtoa_engine.c", line 119: error #166: invalid floating constant
if (y >= MIN_MANT)
^
"stdio/lib_dtoa_engine.c", line 119: warning #1626-D: concatenation with "("
in macro "PASTE" does not create a valid token
if (y >= MIN_MANT)
^
"stdio/lib_dtoa_engine.c", line 119: error #109: expression preceding
parentheses of apparent call must have (pointer-to-) function type
if (y >= MIN_MANT)
^
"stdio/lib_dtoa_engine.c", line 144: error #166: invalid floating constant
if (x >= MAX_MANT)
^
"stdio/lib_dtoa_engine.c", line 144: warning #1626-D: concatenation with "("
in macro "PASTE" does not create a valid token
if (x >= MAX_MANT)
^
"stdio/lib_dtoa_engine.c", line 144: error #109: expression preceding
parentheses of apparent call must have (pointer-to-) function type
if (x >= MAX_MANT)
^
"stdio/lib_dtoa_engine.c", line 153: error #166: invalid floating constant
uint64_t decimal = MIN_MANT_INT;
^
"stdio/lib_dtoa_engine.c", line 153: warning #1626-D: concatenation with "("
in macro "PASTE" does not create a valid token
uint64_t decimal = MIN_MANT_INT;
^
"stdio/lib_dtoa_engine.c", line 153: error #109: expression preceding
parentheses of apparent call must have (pointer-to-) function type
uint64_t decimal = MIN_MANT_INT;
Signed-off-by: yanghuatao <yanghuatao@xiaomi.com>
2024-04-23 18:59:53 +02:00
|
|
|
/* Green hills #define DBL_DIG (6) or #define DBL_DIG (15)
|
|
|
|
* we need remove "()" here
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if DBL_DIG == 6
|
|
|
|
# undef DBL_DIG
|
|
|
|
# define DBL_DIG 6
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DBL_DIG == 15
|
|
|
|
# undef DBL_DIG
|
|
|
|
# define DBL_DIG 15
|
|
|
|
#endif
|
|
|
|
|
2019-02-06 22:50:52 +01:00
|
|
|
#define PASTE(a) 1e##a
|
|
|
|
#define SUBSTITUTE(a) PASTE(a)
|
|
|
|
#define MIN_MANT (SUBSTITUTE(DBL_DIG))
|
|
|
|
#define MAX_MANT (10.0 * MIN_MANT)
|
|
|
|
#define MIN_MANT_INT ((uint64_t)MIN_MANT)
|
|
|
|
#define MIN_MANT_EXP DBL_DIG
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-25 17:25:06 +02:00
|
|
|
int __dtoa_engine(double x, FAR struct dtoa_s *dtoa, int max_digits,
|
2019-02-06 22:50:52 +01:00
|
|
|
int max_decimals)
|
|
|
|
{
|
|
|
|
int32_t exp = 0;
|
|
|
|
uint8_t flags = 0;
|
|
|
|
int i;
|
|
|
|
|
2024-04-24 06:12:55 +02:00
|
|
|
if (x < 0)
|
2019-02-06 22:50:52 +01:00
|
|
|
{
|
|
|
|
flags |= DTOA_MINUS;
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x == 0)
|
|
|
|
{
|
|
|
|
flags |= DTOA_ZERO;
|
|
|
|
for (i = 0; i < max_digits; i++)
|
|
|
|
dtoa->digits[i] = '0';
|
|
|
|
}
|
|
|
|
else if (isnan(x))
|
|
|
|
{
|
|
|
|
flags |= DTOA_NAN;
|
|
|
|
}
|
|
|
|
else if (isinf(x))
|
|
|
|
{
|
|
|
|
flags |= DTOA_INF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-25 17:25:06 +02:00
|
|
|
double y;
|
2019-02-06 22:50:52 +01:00
|
|
|
|
|
|
|
exp = MIN_MANT_EXP;
|
|
|
|
|
|
|
|
/* Bring x within range MIN_MANT <= x < MAX_MANT while computing
|
|
|
|
* exponent value
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (x < MIN_MANT)
|
|
|
|
{
|
|
|
|
for (i = DTOA_SCALE_UP_NUM - 1; i >= 0; i--)
|
|
|
|
{
|
2019-02-07 00:05:01 +01:00
|
|
|
y = x * g_dtoa_scale_up[i];
|
2019-02-06 22:50:52 +01:00
|
|
|
if (y < MAX_MANT)
|
|
|
|
{
|
|
|
|
x = y;
|
|
|
|
exp -= (1 << i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = DTOA_SCALE_DOWN_NUM - 1; i >= 0; i--)
|
|
|
|
{
|
2019-02-07 00:05:01 +01:00
|
|
|
y = x * g_dtoa_scale_down[i];
|
2019-02-06 22:50:52 +01:00
|
|
|
if (y >= MIN_MANT)
|
|
|
|
{
|
|
|
|
x = y;
|
|
|
|
exp += (1 << i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If limiting decimals, then limit the max digits to no more than the
|
|
|
|
* number of digits left of the decimal plus the number of digits right
|
2023-01-30 12:34:08 +01:00
|
|
|
* of the decimal. If the integer value is 0, there are only values to
|
|
|
|
* the right of the decimal point in dtoa->digits.
|
2019-02-06 22:50:52 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (max_decimals != 0)
|
|
|
|
{
|
2023-01-30 12:34:08 +01:00
|
|
|
max_digits = MIN(max_digits, max_decimals + MAX(exp + 1, 0));
|
2019-02-06 22:50:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Round nearest by adding 1/2 of the last digit before converting to
|
|
|
|
* int. Check for overflow and adjust mantissa and exponent values
|
|
|
|
*/
|
|
|
|
|
2019-02-07 00:05:01 +01:00
|
|
|
x = x + g_dtoa_round[max_digits];
|
2019-02-06 22:50:52 +01:00
|
|
|
|
|
|
|
if (x >= MAX_MANT)
|
|
|
|
{
|
|
|
|
x /= 10.0;
|
|
|
|
exp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now convert mantissa to decimal. */
|
|
|
|
|
2022-11-25 19:24:08 +01:00
|
|
|
uint64_t mant = (uint64_t)x;
|
2019-02-06 22:50:52 +01:00
|
|
|
uint64_t decimal = MIN_MANT_INT;
|
|
|
|
|
|
|
|
/* Compute digits */
|
|
|
|
|
|
|
|
for (i = 0; i < max_digits; i++)
|
|
|
|
{
|
|
|
|
dtoa->digits[i] = mant / decimal + '0';
|
|
|
|
mant %= decimal;
|
|
|
|
decimal /= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dtoa->digits[max_digits] = '\0';
|
|
|
|
dtoa->flags = flags;
|
|
|
|
dtoa->exp = exp;
|
|
|
|
return max_digits;
|
|
|
|
}
|