1*418b791dSBob Badour /* 2*418b791dSBob Badour * Copyright (c) 2019, The Linux Foundation. All rights reserved. 3*418b791dSBob Badour * 4*418b791dSBob Badour * Redistribution and use in source and binary forms, with or without 5*418b791dSBob Badour * modification, are permitted provided that the following conditions are 6*418b791dSBob Badour * met: 7*418b791dSBob Badour * * Redistributions of source code must retain the above copyright 8*418b791dSBob Badour * notice, this list of conditions and the following disclaimer. 9*418b791dSBob Badour * * Redistributions in binary form must reproduce the above 10*418b791dSBob Badour * copyright notice, this list of conditions and the following 11*418b791dSBob Badour * disclaimer in the documentation and/or other materials provided 12*418b791dSBob Badour * with the distribution. 13*418b791dSBob Badour * * Neither the name of The Linux Foundation nor the names of its 14*418b791dSBob Badour * contributors may be used to endorse or promote products derived 15*418b791dSBob Badour * from this software without specific prior written permission. 16*418b791dSBob Badour * 17*418b791dSBob Badour * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18*418b791dSBob Badour * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19*418b791dSBob Badour * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20*418b791dSBob Badour * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21*418b791dSBob Badour * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22*418b791dSBob Badour * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23*418b791dSBob Badour * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24*418b791dSBob Badour * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25*418b791dSBob Badour * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26*418b791dSBob Badour * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27*418b791dSBob Badour * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*418b791dSBob Badour */ 29*418b791dSBob Badour 30*418b791dSBob Badour #ifndef STD_DTOA_H 31*418b791dSBob Badour #define STD_DTOA_H 32*418b791dSBob Badour 33*418b791dSBob Badour // 34*418b791dSBob Badour // Constant Definitions 35*418b791dSBob Badour // 36*418b791dSBob Badour 37*418b791dSBob Badour // For floating point numbers, the range of a double precision number is 38*418b791dSBob Badour // approximately +/- 10 ^ 308.25 as per the IEEE Standard 754. 39*418b791dSBob Badour // As such, the maximum size of the integer portion of the 40*418b791dSBob Badour // string is assumed to be 311 (309 + sign + \0). The maximum 41*418b791dSBob Badour // size of the fractional part is assumed to be 100. Thus, the 42*418b791dSBob Badour // maximum size of the string that would contain the number 43*418b791dSBob Badour // after conversion is safely assumed to be 420 (including any 44*418b791dSBob Badour // prefix, the null character and exponent specifiers 'e'). 45*418b791dSBob Badour // 46*418b791dSBob Badour // The buffers that contain the converted integer and the fraction parts of 47*418b791dSBob Badour // the float are safely assumed to be of size 310. 48*418b791dSBob Badour #define STD_DTOA_FORMAT_FLOAT_SIZE 420 49*418b791dSBob Badour #define STD_DTOA_FORMAT_INTEGER_SIZE 311 50*418b791dSBob Badour #define STD_DTOA_FORMAT_FRACTION_SIZE 100 51*418b791dSBob Badour 52*418b791dSBob Badour // Constants for operations on the IEEE 754 representation of double 53*418b791dSBob Badour // precision floating point numbers. 54*418b791dSBob Badour #define STD_DTOA_DP_SIGN_SHIFT_COUNT 63 55*418b791dSBob Badour #define STD_DTOA_DP_EXPONENT_SHIFT_COUNT 52 56*418b791dSBob Badour #define STD_DTOA_DP_EXPONENT_MASK 0x7ff 57*418b791dSBob Badour #define STD_DTOA_DP_EXPONENT_BIAS 1023 58*418b791dSBob Badour #define STD_DTOA_DP_MANTISSA_MASK ( ( (uint64)1 << 52 ) - 1 ) 59*418b791dSBob Badour #define STD_DTOA_DP_INFINITY_EXPONENT_ID 0x7FF 60*418b791dSBob Badour #define STD_DTOA_DP_MAX_EXPONENT 1023 61*418b791dSBob Badour #define STD_DTOA_DP_MIN_EXPONENT_NORM -1022 62*418b791dSBob Badour #define STD_DTOA_DP_MIN_EXPONENT_DENORM -1074 63*418b791dSBob Badour #define STD_DTOA_DP_MAX_EXPONENT_DEC 308 64*418b791dSBob Badour #define STD_DTOA_DP_MIN_EXPONENT_DEC_DENORM -323 65*418b791dSBob Badour 66*418b791dSBob Badour #define STD_DTOA_PRECISION_ROUNDING_VALUE 4 67*418b791dSBob Badour #define STD_DTOA_DEFAULT_FLOAT_PRECISION 6 68*418b791dSBob Badour 69*418b791dSBob Badour #define STD_DTOA_NEGATIVE_INF_UPPER_CASE "-INF" 70*418b791dSBob Badour #define STD_DTOA_NEGATIVE_INF_LOWER_CASE "-inf" 71*418b791dSBob Badour #define STD_DTOA_POSITIVE_INF_UPPER_CASE "INF" 72*418b791dSBob Badour #define STD_DTOA_POSITIVE_INF_LOWER_CASE "inf" 73*418b791dSBob Badour #define STD_DTOA_NAN_UPPER_CASE "NAN" 74*418b791dSBob Badour #define STD_DTOA_NAN_LOWER_CASE "nan" 75*418b791dSBob Badour #define STD_DTOA_FP_POSITIVE_INF 0x7FF0000000000000uLL 76*418b791dSBob Badour #define STD_DTOA_FP_NEGATIVE_INF 0xFFF0000000000000uLL 77*418b791dSBob Badour #define STD_DTOA_FP_SNAN 0xFFF0000000000001uLL 78*418b791dSBob Badour #define STD_DTOA_FP_QNAN 0xFFFFFFFFFFFFFFFFuLL 79*418b791dSBob Badour 80*418b791dSBob Badour // 81*418b791dSBob Badour // Useful Macros 82*418b791dSBob Badour // 83*418b791dSBob Badour 84*418b791dSBob Badour #define MY_ISDIGIT(c) ( ( (c) >= '0' ) && ( (c) <= '9' ) ) 85*418b791dSBob Badour #define FP_EXPONENT(u) ( ( ( (u) >> STD_DTOA_DP_EXPONENT_SHIFT_COUNT ) \ 86*418b791dSBob Badour & STD_DTOA_DP_EXPONENT_MASK ) - STD_DTOA_DP_EXPONENT_BIAS ) 87*418b791dSBob Badour #define FP_EXPONENT_BIASED(u) ( ( (u) >> STD_DTOA_DP_EXPONENT_SHIFT_COUNT ) \ 88*418b791dSBob Badour & STD_DTOA_DP_EXPONENT_MASK ) 89*418b791dSBob Badour #define FP_MANTISSA_NORM(u) ( ( (u) & STD_DTOA_DP_MANTISSA_MASK ) | \ 90*418b791dSBob Badour ( (uint64)1 << STD_DTOA_DP_EXPONENT_SHIFT_COUNT ) ) 91*418b791dSBob Badour #define FP_MANTISSA_DENORM(u) ( (u) & STD_DTOA_DP_MANTISSA_MASK ) 92*418b791dSBob Badour #define FP_MANTISSA(u) ( FP_EXPONENT_BIASED(u) ? FP_MANTISSA_NORM(u) : \ 93*418b791dSBob Badour FP_MANTISSA_DENORM(u) ) 94*418b791dSBob Badour #define FP_SIGN(u) ( (u) >> STD_DTOA_DP_SIGN_SHIFT_COUNT ) 95*418b791dSBob Badour #define DOUBLE_TO_UINT64(d) ( *( (uint64*) &(d) ) ) 96*418b791dSBob Badour #define DOUBLE_TO_INT64(d) ( *( (int64*) &(d) ) ) 97*418b791dSBob Badour #define UINT64_TO_DOUBLE(u) ( *( (double*) &(u) ) ) 98*418b791dSBob Badour 99*418b791dSBob Badour // 100*418b791dSBob Badour // Type Definitions 101*418b791dSBob Badour // 102*418b791dSBob Badour 103*418b791dSBob Badour typedef enum 104*418b791dSBob Badour { 105*418b791dSBob Badour FP_TYPE_UNKOWN = 0, 106*418b791dSBob Badour FP_TYPE_NEGATIVE_INF, 107*418b791dSBob Badour FP_TYPE_POSITIVE_INF, 108*418b791dSBob Badour FP_TYPE_NAN, 109*418b791dSBob Badour FP_TYPE_GENERAL, 110*418b791dSBob Badour } FloatingPointType; 111*418b791dSBob Badour 112*418b791dSBob Badour // 113*418b791dSBob Badour // Function Declarations 114*418b791dSBob Badour // 115*418b791dSBob Badour 116*418b791dSBob Badour #ifdef __cplusplus 117*418b791dSBob Badour extern "C" { 118*418b791dSBob Badour #endif // #ifdef __cplusplus 119*418b791dSBob Badour 120*418b791dSBob Badour double fp_pow_10( int nPow ); 121*418b791dSBob Badour double fp_round( double dNumber, int nPrecision ); 122*418b791dSBob Badour int fp_log_10( double dNumber ); 123*418b791dSBob Badour int fp_check_special_cases( double dNumber, FloatingPointType* pNumberType ); 124*418b791dSBob Badour int std_dtoa_decimal( double dNumber, int nPrecision, 125*418b791dSBob Badour char acIntegerPart[ STD_DTOA_FORMAT_INTEGER_SIZE ], 126*418b791dSBob Badour char acFractionPart[ STD_DTOA_FORMAT_FRACTION_SIZE ] ); 127*418b791dSBob Badour int std_dtoa_hex( double dNumber, int nPrecision, char cFormat, 128*418b791dSBob Badour char acIntegerPart[ STD_DTOA_FORMAT_INTEGER_SIZE ], 129*418b791dSBob Badour char acFractionPart[ STD_DTOA_FORMAT_FRACTION_SIZE ], 130*418b791dSBob Badour int* pnExponent ); 131*418b791dSBob Badour 132*418b791dSBob Badour #ifdef __cplusplus 133*418b791dSBob Badour } 134*418b791dSBob Badour #endif // #ifdef __cplusplus 135*418b791dSBob Badour 136*418b791dSBob Badour #endif // STD_DTOA_H 137*418b791dSBob Badour 138