📄 num_get_float.cpp
字号:
/*
* Copyright (c) 1999
* Silicon Graphics Computer Systems, Inc.
*
* Copyright (c) 1999
* Boris Fomitchev
*
* This material is provided "as is", with absolutely no warranty expressed
* or implied. Any use is at your own risk.
*
* Permission to use or copy this software for any purpose is hereby granted
* without fee, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*/
#include "stlport_prefix.h"
#include <limits>
#include <locale>
#include <istream>
#if defined (__GNUC__) && !defined (__sun) || \
defined (__DMC__)
# include <stdint.h>
#endif
#if defined (__linux__)
# include <ieee754.h>
union _ll {
uint64_t i64;
struct {
# if defined (_STLP_BIG_ENDIAN)
uint32_t hi;
uint32_t lo;
# elif defined (_STLP_LITTLE_ENDIAN)
uint32_t lo;
uint32_t hi;
# else
# error Unknown endianess
# endif
} i32;
};
#endif
#if defined (N_PLAT_NLM)
# include <nlm/nwintxx.h>
# if defined (INT64)
typedef unsigned INT64 uint64_t;
# else
// #error "Can't find INT64"
// 64-bit int really not defined in headers
// (_INTEGRAL_MAX_BITS < 64 in any case?), but compiler indeed know __int64
// - ptr, 2005-05-06
typedef unsigned __int64 uint64_t;
# endif
# if defined (INT32)
typedef unsigned INT32 uint32_t;
# else
# error Can not find INT32
# endif
union _ll {
uint64_t i64;
struct {
uint32_t lo;
uint32_t hi;
} i32;
};
#endif
_STLP_BEGIN_NAMESPACE
_STLP_MOVE_TO_PRIV_NAMESPACE
//----------------------------------------------------------------------
// num_get
// Helper functions for _M_do_get_float.
#if !defined (_STLP_NO_WCHAR_T)
void _STLP_CALL
_Initialize_get_float( const ctype<wchar_t>& ct,
wchar_t& Plus, wchar_t& Minus,
wchar_t& pow_e, wchar_t& pow_E,
wchar_t* digits) {
char ndigits[11] = "0123456789";
Plus = ct.widen('+');
Minus = ct.widen('-');
pow_e = ct.widen('e');
pow_E = ct.widen('E');
ct.widen(ndigits + 0, ndigits + 10, digits);
}
#endif /* WCHAR_T */
/*
* __string_to_double is just lifted from atof, the difference being
* that we just use '.' for the decimal point, rather than let it
* be taken from the current C locale, which of course is not accessible
* to us.
*/
#if defined (_STLP_MSVC) || defined (__BORLANDC__) || defined (__ICL)
typedef unsigned long uint32;
typedef unsigned __int64 uint64;
# define ULL(x) x##Ui64
#elif defined (__MRC__) || defined (__SC__)
typedef unsigned long uint32;
# include "uint64.h" //*TY 03/25/2000 - added 64bit math type definition
#elif defined (__unix) || defined (__MINGW32__) || defined (N_PLAT_NLM) || \
(defined (__DMC__) && (__LONGLONG))
typedef uint32_t uint32;
typedef uint64_t uint64;
# define ULL(x) x##ULL
#else
# error There should be some unsigned 64-bit integer on the system!
#endif
// Multiplication of two 64-bit integers, giving a 128-bit result.
// Taken from Algorithm M in Knuth section 4.3.1, with the loop
// hand-unrolled.
static void _Stl_mult64(const uint64 u, const uint64 v,
uint64& high, uint64& low) {
const uint64 low_mask = ULL(0xffffffff);
const uint64 u0 = u & low_mask;
const uint64 u1 = u >> 32;
const uint64 v0 = v & low_mask;
const uint64 v1 = v >> 32;
uint64 t = u0 * v0;
low = t & low_mask;
t = u1 * v0 + (t >> 32);
uint64 w1 = t & low_mask;
uint64 w2 = t >> 32;
uint64 x = u0 * v1 + w1;
low += (x & low_mask) << 32;
high = u1 * v1 + w2 + (x >> 32);
}
#define bit11 ULL(0x7ff)
#define exponent_mask (bit11 << 52)
#if !defined (__GNUC__) || (__GNUC__ != 3) || (__GNUC_MINOR__ != 4) || \
(!defined (__CYGWIN__) && !defined (__MINGW32__))
//Generate bad code when compiled with -O2 option.
inline
#endif
void _Stl_set_exponent(uint64 &val, uint64 exp)
{ val = (val & ~exponent_mask) | ((exp & bit11) << 52); }
/* Power of ten fractions for tenscale*/
/* The constants are factored so that at most two constants
* and two multiplies are needed. Furthermore, one of the constants
* is represented exactly - 10**n where 1<= n <= 27.
*/
#if !defined (__SC__) //*TY 03/25/2000 - no native 64bit integer under SCpp
static const uint64 _Stl_tenpow[80] = {
ULL(0xa000000000000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
ULL(0xc800000000000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
ULL(0xfa00000000000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
ULL(0x9c40000000000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
ULL(0xc350000000000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
ULL(0xf424000000000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
ULL(0x9896800000000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
ULL(0xbebc200000000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
ULL(0xee6b280000000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
ULL(0x9502f90000000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
ULL(0xba43b74000000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
ULL(0xe8d4a51000000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
ULL(0x9184e72a00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
ULL(0xb5e620f480000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
ULL(0xe35fa931a0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
ULL(0x8e1bc9bf04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
ULL(0xb1a2bc2ec5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
ULL(0xde0b6b3a76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
ULL(0x8ac7230489e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
ULL(0xad78ebc5ac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
ULL(0xd8d726b7177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
ULL(0x878678326eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
ULL(0xa968163f0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
ULL(0xd3c21bcecceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
ULL(0x84595161401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
ULL(0xa56fa5b99019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
ULL(0xcecb8f27f4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
ULL(0xd0cf4b50cfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
ULL(0xd2d80db02aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
ULL(0xd4e5e2cdc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
ULL(0xd6f8d7509292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
ULL(0xd910f7ff28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
ULL(0xdb2e51bfe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
ULL(0xdd50f1996b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
ULL(0xdf78e4b2bd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
ULL(0xe1a63853bbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
ULL(0xe3d8f9e563a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
ULL(0xfd87b5f28300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
ULL(0xfb158592be068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
ULL(0xf8a95fcf88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
ULL(0xf64335bcf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
ULL(0xf3e2f893dec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
ULL(0xf18899b1bc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
ULL(0xef340a98172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
ULL(0xece53cec4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
ULL(0xea9c227723ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */
ULL(0xe858ad248f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
ULL(0xe61acf033d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */
ULL(0xe3e27a444d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
ULL(0xe1afa13afbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
#else //*TY 03/20/2000 - added support for SCpp which lacks native 64bit integer type
static const UnsignedWide _Stl_tenpow[80] = {
ULL2(0xa0000000,0x00000000), /* _Stl_tenpow[0]=(10**1)/(2**4) */
ULL2(0xc8000000,0x00000000), /* _Stl_tenpow[1]=(10**2)/(2**7) */
ULL2(0xfa000000,0x00000000), /* _Stl_tenpow[2]=(10**3)/(2**10) */
ULL2(0x9c400000,0x00000000), /* _Stl_tenpow[3]=(10**4)/(2**14) */
ULL2(0xc3500000,0x00000000), /* _Stl_tenpow[4]=(10**5)/(2**17) */
ULL2(0xf4240000,0x00000000), /* _Stl_tenpow[5]=(10**6)/(2**20) */
ULL2(0x98968000,0x00000000), /* _Stl_tenpow[6]=(10**7)/(2**24) */
ULL2(0xbebc2000,0x00000000), /* _Stl_tenpow[7]=(10**8)/(2**27) */
ULL2(0xee6b2800,0x00000000), /* _Stl_tenpow[8]=(10**9)/(2**30) */
ULL2(0x9502f900,0x00000000), /* _Stl_tenpow[9]=(10**10)/(2**34) */
ULL2(0xba43b740,0x00000000), /* _Stl_tenpow[10]=(10**11)/(2**37) */
ULL2(0xe8d4a510,0x00000000), /* _Stl_tenpow[11]=(10**12)/(2**40) */
ULL2(0x9184e72a,0x00000000), /* _Stl_tenpow[12]=(10**13)/(2**44) */
ULL2(0xb5e620f4,0x80000000), /* _Stl_tenpow[13]=(10**14)/(2**47) */
ULL2(0xe35fa931,0xa0000000), /* _Stl_tenpow[14]=(10**15)/(2**50) */
ULL2(0x8e1bc9bf,0x04000000), /* _Stl_tenpow[15]=(10**16)/(2**54) */
ULL2(0xb1a2bc2e,0xc5000000), /* _Stl_tenpow[16]=(10**17)/(2**57) */
ULL2(0xde0b6b3a,0x76400000), /* _Stl_tenpow[17]=(10**18)/(2**60) */
ULL2(0x8ac72304,0x89e80000), /* _Stl_tenpow[18]=(10**19)/(2**64) */
ULL2(0xad78ebc5,0xac620000), /* _Stl_tenpow[19]=(10**20)/(2**67) */
ULL2(0xd8d726b7,0x177a8000), /* _Stl_tenpow[20]=(10**21)/(2**70) */
ULL2(0x87867832,0x6eac9000), /* _Stl_tenpow[21]=(10**22)/(2**74) */
ULL2(0xa968163f,0x0a57b400), /* _Stl_tenpow[22]=(10**23)/(2**77) */
ULL2(0xd3c21bce,0xcceda100), /* _Stl_tenpow[23]=(10**24)/(2**80) */
ULL2(0x84595161,0x401484a0), /* _Stl_tenpow[24]=(10**25)/(2**84) */
ULL2(0xa56fa5b9,0x9019a5c8), /* _Stl_tenpow[25]=(10**26)/(2**87) */
ULL2(0xcecb8f27,0xf4200f3a), /* _Stl_tenpow[26]=(10**27)/(2**90) */
ULL2(0xd0cf4b50,0xcfe20766), /* _Stl_tenpow[27]=(10**55)/(2**183) */
ULL2(0xd2d80db0,0x2aabd62c), /* _Stl_tenpow[28]=(10**83)/(2**276) */
ULL2(0xd4e5e2cd,0xc1d1ea96), /* _Stl_tenpow[29]=(10**111)/(2**369) */
ULL2(0xd6f8d750,0x9292d603), /* _Stl_tenpow[30]=(10**139)/(2**462) */
ULL2(0xd910f7ff,0x28069da4), /* _Stl_tenpow[31]=(10**167)/(2**555) */
ULL2(0xdb2e51bf,0xe9d0696a), /* _Stl_tenpow[32]=(10**195)/(2**648) */
ULL2(0xdd50f199,0x6b947519), /* _Stl_tenpow[33]=(10**223)/(2**741) */
ULL2(0xdf78e4b2,0xbd342cf7), /* _Stl_tenpow[34]=(10**251)/(2**834) */
ULL2(0xe1a63853,0xbbd26451), /* _Stl_tenpow[35]=(10**279)/(2**927) */
ULL2(0xe3d8f9e5,0x63a198e5), /* _Stl_tenpow[36]=(10**307)/(2**1020) */
ULL2(0xfd87b5f2,0x8300ca0e), /* _Stl_tenpow[37]=(10**-28)/(2**-93) */
ULL2(0xfb158592,0xbe068d2f), /* _Stl_tenpow[38]=(10**-56)/(2**-186) */
ULL2(0xf8a95fcf,0x88747d94), /* _Stl_tenpow[39]=(10**-84)/(2**-279) */
ULL2(0xf64335bc,0xf065d37d), /* _Stl_tenpow[40]=(10**-112)/(2**-372) */
ULL2(0xf3e2f893,0xdec3f126), /* _Stl_tenpow[41]=(10**-140)/(2**-465) */
ULL2(0xf18899b1,0xbc3f8ca2), /* _Stl_tenpow[42]=(10**-168)/(2**-558) */
ULL2(0xef340a98,0x172aace5), /* _Stl_tenpow[43]=(10**-196)/(2**-651) */
ULL2(0xece53cec,0x4a314ebe), /* _Stl_tenpow[44]=(10**-224)/(2**-744) */
ULL2(0xea9c2277,0x23ee8bcb), /* _Stl_tenpow[45]=(10**-252)/(2**-837) */
ULL2(0xe858ad24,0x8f5c22ca), /* _Stl_tenpow[46]=(10**-280)/(2**-930) */
ULL2(0xe61acf03,0x3d1a45df), /* _Stl_tenpow[47]=(10**-308)/(2**-1023) */
ULL2(0xe3e27a44,0x4d8d98b8), /* _Stl_tenpow[48]=(10**-336)/(2**-1116) */
ULL2(0xe1afa13a,0xfbd14d6e) /* _Stl_tenpow[49]=(10**-364)/(2**-1209) */
#endif
};
static const short _Stl_twoexp[80] = {
4,7,10,14,17,20,24,27,30,34,37,40,44,47,50,54,57,60,64,67,70,74,77,80,84,87,90,
183,276,369,462,555,648,741,834,927,1020,
-93,-186,-279,-372,-465,-558,-651,-744,-837,-930,-1023,-1116,-1209
};
#define TEN_1 0 /* offset to 10 ** 1 */
#define TEN_27 26 /* offset to 10 ** 27 */
#define TEN_M28 37 /* offset to 10 ** -28 */
#define NUM_HI_P 11
#define NUM_HI_N 13
#define _Stl_HIBITULL (ULL(1) << 63)
static void _Stl_norm_and_round(uint64& p, int& norm, uint64 prodhi, uint64 prodlo) {
norm = 0;
if ((prodhi & _Stl_HIBITULL) == 0) {
/* leading bit is a zero
* may have to normalize
*/
if ((prodhi == ~_Stl_HIBITULL) &&
((prodlo >> 62) == 0x3)) { /* normalization followed by round
* would cause carry to create
* extra bit, so don't normalize
*/
p = _Stl_HIBITULL;
return;
}
p = (prodhi << 1) | (prodlo >> 63); /* normalize */
norm = 1;
prodlo <<= 1;
}
else {
p = prodhi;
}
if ((prodlo & _Stl_HIBITULL) != 0) { /* first guard bit a one */ //*TY 03/25/2000 - added explicit comparison to zero to avoid reliance to the implicit conversion from uint64 to bool
#if !defined (__SC__) //*TY 03/25/2000 -
if (((p & 0x1) != 0) ||
prodlo != _Stl_HIBITULL ) { /* not borderline for round to even */
#else //*TY 03/25/2000 - added workaround for SCpp compiler
bool b1 = ((p & 0x1) != 0);
if (b1 || prodlo != _Stl_HIBITULL) { //*TY 03/25/2000 - SCpp confuses on this particular original boolean expression
#endif //*TY 03/25/2000 -
/* round */
++p;
if (p == 0)
++p;
}
}
return;
}
// Convert a 64-bitb fraction * 10^exp to a 64-bit fraction * 2^bexp.
// p: 64-bit fraction
// exp: base-10 exponent
// bexp: base-2 exponent (output parameter)
static void _Stl_tenscale(uint64& p, int exp, int& bexp) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -