sl@0: /* sl@0: * Copyright (c) 1999 sl@0: * Silicon Graphics Computer Systems, Inc. sl@0: * sl@0: * Copyright (c) 1999 sl@0: * Boris Fomitchev sl@0: * sl@0: * This material is provided "as is", with absolutely no warranty expressed sl@0: * or implied. Any use is at your own risk. sl@0: * sl@0: * Permission to use or copy this software for any purpose is hereby granted sl@0: * without fee, provided the above notices are retained on all copies. sl@0: * Permission to modify the code and to distribute modified code is granted, sl@0: * provided the above notices are retained, and a notice that the code was sl@0: * modified is included with the above copyright notice. sl@0: * sl@0: */ sl@0: sl@0: #include "stlport_prefix.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #if defined (__DECCXX) sl@0: # define NDIG 400 sl@0: #else sl@0: # define NDIG 82 sl@0: #endif sl@0: sl@0: #if defined (_STLP_NO_LONG_DOUBLE) sl@0: # define MAXECVT 17 sl@0: # define MAXFCVT 18 sl@0: typedef double max_double_type; sl@0: #else sl@0: # define MAXECVT 35 sl@0: # define MAXFCVT 36 sl@0: typedef long double max_double_type; sl@0: #endif sl@0: sl@0: #define MAXFSIG MAXECVT sl@0: #define MAXESIZ 5 sl@0: sl@0: #define todigit(x) ((x)+'0') sl@0: sl@0: #if defined (_STLP_UNIX) sl@0: sl@0: # if defined (__sun) sl@0: # include sl@0: # endif sl@0: sl@0: # if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) sl@0: // DEC, SGI & Solaris need this sl@0: # include sl@0: # include sl@0: # endif sl@0: sl@0: # if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \ sl@0: defined(__FreeBSD__) sl@0: # define USE_SPRINTF_INSTEAD sl@0: # endif sl@0: sl@0: # if defined( _AIX ) // JFA 3-Aug-2000 sl@0: # include sl@0: # include sl@0: # endif sl@0: sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: sl@0: //#if defined(_CRAY) sl@0: //# include sl@0: //#endif sl@0: sl@0: #if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \ sl@0: defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) sl@0: # include sl@0: #endif sl@0: sl@0: #if defined(__MRC__) || defined(__SC__) || defined(_CRAY) //*TY 02/24/2000 - added support for MPW sl@0: # include sl@0: #endif sl@0: sl@0: #if defined (__CYGWIN__) sl@0: # include sl@0: #endif sl@0: sl@0: #if defined (__MSL__) sl@0: # include // for atoi sl@0: # include // for snprintf sl@0: # include sl@0: # include sl@0: #endif sl@0: sl@0: #if defined (__ISCPP__) sl@0: # include sl@0: #endif sl@0: sl@0: #include sl@0: sl@0: #if defined (__DMC__) sl@0: # define snprintf _snprintf sl@0: #endif sl@0: sl@0: #if defined(__hpux) && (!defined(_INCLUDE_HPUX_SOURCE) || defined(__GNUC__)) sl@0: extern "C" double erf(double); sl@0: extern "C" double erfc(double); sl@0: extern "C" double gamma(double); /* obsolescent */ sl@0: extern "C" double hypot(double, double); sl@0: extern "C" int isnan(double); sl@0: extern "C" double j0(double); sl@0: extern "C" double j1(double); sl@0: extern "C" double jn(int, double); sl@0: extern "C" double lgamma(double); sl@0: extern "C" double y0(double); sl@0: extern "C" double y1(double); sl@0: extern "C" double yn(int, double); sl@0: sl@0: # define HUGE_VALF _SINFINITY sl@0: # define INFINITY _SINFINITY sl@0: # define NAN _SQNAN sl@0: sl@0: # define isnan(x) _ISNAN(x) sl@0: # define isinf(x) _ISINF(x) sl@0: # define signbit(x) _SIGNBIT(x) sl@0: # define isfinite(x) _ISFINITE(x) sl@0: # define isnormal(x) _ISNORMAL(x) sl@0: # define fpclassify(x) _FPCLASSIFY(x) sl@0: # define isunordered(x,y) _ISUNORDERED(x,y) sl@0: # define isgreater(x,y) _ISGREATER(x,y) sl@0: # define isgreaterequal(x,y) _ISGREATEREQUAL(x,y) sl@0: # define isless(x,y) _ISLESS(x,y) sl@0: # define islessequal(x,y) _ISLESSEQUAL(x,y) sl@0: # define islessgreater(x,y) _ISLESSGREATER(x,y) sl@0: sl@0: # define FP_NORMAL 0 sl@0: # define FP_ZERO 1 sl@0: # define FP_INFINITE 2 sl@0: # define FP_SUBNORMAL 3 sl@0: # define FP_NAN 4 sl@0: sl@0: # define DECIMAL_DIG 17 sl@0: sl@0: # define _IS64(x) (sizeof(x) == sizeof(double)) sl@0: # define _IS32(x) (sizeof(x) == sizeof(float)) sl@0: sl@0: extern "C" { sl@0: extern double copysign(double, double); sl@0: extern const float _SINFINITY; sl@0: extern const float _SQNAN; sl@0: # if defined (_PA_RISC) sl@0: # define _ISNAN(x) (_IS32(x)?_Isnanf(x):(isnan)(x)) sl@0: # define _ISINF(x) (_IS32(x)?_Isinff(x):_Isinf(x)) sl@0: # define _SIGNBIT(x) (_IS32(x)?_Signbitf(x):_Signbit(x)) sl@0: # define _ISFINITE(x) (_IS32(x)?_Isfinitef(x):_Isfinite(x)) sl@0: # define _ISNORMAL(x) (_IS32(x)?_Isnormalf(x):_Isnormal(x)) sl@0: # define _FPCLASSIFY(x) (_IS32(x)?_Fpclassifyf(x)>>1:_Fpclassify(x)>>1) sl@0: # define _ISUNORDERED(x,y) (_IS32(x)&&_IS32(y)?_Isunorderedf(x,y):_Isunordered(x,y)) sl@0: extern int _Signbit(double); sl@0: extern int _Signbitf(float); sl@0: extern int _Isnanf(float); sl@0: extern int _Isfinite(double); sl@0: extern int _Isfinitef(float); sl@0: extern int _Isinf(double); sl@0: extern int _Isinff(float); sl@0: extern int _Isnormal(double); sl@0: extern int _Isnormalf(float); sl@0: extern int _Isunordered(double, double); sl@0: extern int _Isunorderedf(float, float); sl@0: extern int _Fpclassify(double); sl@0: extern int _Fpclassifyf(float); sl@0: # else sl@0: # include "math_ia64_internal.h" sl@0: # define _FPCLASSIFY(x) (_IS32(x)?_Fpclassf(x):_Fpclass(x)) sl@0: extern int _Fpclass(double); sl@0: extern int _Fpclassf(float); sl@0: # endif sl@0: } sl@0: sl@0: # if !defined (_INCLUDE_XOPEN_SOURCE_EXTENDED) sl@0: extern "C" char *fcvt(double, int, int *, int *); sl@0: extern "C" char *ecvt(double, int, int *, int *); sl@0: # endif sl@0: # if !defined (_INCLUDE_HPUX_SOURCE) sl@0: # if !defined (_LONG_DOUBLE) sl@0: # define _LONG_DOUBLE sl@0: typedef struct { sl@0: uint32_t word1, word2, word3, word4; sl@0: } long_double; sl@0: # endif /* _LONG_DOUBLE */ sl@0: extern "C" char *_ldecvt(long_double, int, int *, int *); sl@0: extern "C" char *_ldfcvt(long_double, int, int *, int *); sl@0: sl@0: # endif sl@0: #endif /* __hpux */ sl@0: sl@0: _STLP_BEGIN_NAMESPACE sl@0: _STLP_MOVE_TO_PRIV_NAMESPACE sl@0: sl@0: #if defined (__MWERKS__) || defined(__BEOS__) sl@0: # define USE_SPRINTF_INSTEAD sl@0: #endif sl@0: sl@0: #if defined (_AIX) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) sl@0: // Some OS'es only provide non-reentrant primitives, so we have to use additional synchronization here sl@0: sl@0: # if !defined(_REENTRANT) && !defined(_THREAD_SAFE) && !(defined(_POSIX_THREADS) && defined(__OpenBSD__)) sl@0: # define LOCK_CVT sl@0: # define RETURN_CVT(ecvt, x, n, pt, sign, buf) return ecvt(x, n, pt, sign); sl@0: # else sl@0: static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER; sl@0: # define LOCK_CVT _STLP_auto_lock lock(__put_float_mutex); sl@0: # define RETURN_CVT(ecvt, x, n, pt, sign, buf) strcpy(buf, ecvt(x, n, pt, sign)); return buf; sl@0: # endif // !_REENTRANT sl@0: #endif // _AIX || __FreeBSD__ || __NetBSD__ || __OpenBSD__ sl@0: sl@0: // Tests for infinity and NaN differ on different OSs. We encapsulate sl@0: // these differences here. sl@0: sl@0: #if !defined (USE_SPRINTF_INSTEAD) sl@0: # if defined (__hpux) || defined (__DJGPP) || (defined (_STLP_USE_GLIBC) && ! defined (__MSL__)) || \ sl@0: defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) sl@0: static inline bool _Stl_is_nan_or_inf(double x) sl@0: # if defined (isfinite) sl@0: { return !isfinite(x); } sl@0: # else sl@0: { return !finite(x); } sl@0: # endif sl@0: static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); } sl@0: static inline bool _Stl_is_inf(double x) { return isinf(x); } sl@0: // inline bool _Stl_is_neg_inf(double x) { return isinf(x) < 0; } sl@0: static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && x < 0; } sl@0: # elif (defined (__unix) || defined (__unix__)) && \ sl@0: !defined (__APPLE__) && !defined (__DJGPP) && !defined(__osf__) && \ sl@0: !defined (_CRAY) sl@0: static inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); } sl@0: static inline bool _Stl_is_inf(double x) { return IsNANorINF(x) && IsINF(x); } sl@0: static inline bool _Stl_is_neg_inf(double x) { return (IsINF(x)) && (x < 0.0); } sl@0: static inline bool _Stl_is_neg_nan(double x) { return IsNegNAN(x); } sl@0: # elif defined (__BORLANDC__) && ( __BORLANDC__ < 0x540 ) sl@0: static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); } sl@0: static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! _isnan(x);} sl@0: static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; } sl@0: static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && x < 0 ; } sl@0: # elif defined (_STLP_MSVC_LIB) || (defined (__MINGW32__) && !(defined(__SYMBIAN32__) && defined(__GCCXML__)) ) || defined (__BORLANDC__) sl@0: static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); } sl@0: static inline bool _Stl_is_inf(double x) { sl@0: int fclass = _fpclass(x); sl@0: return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF; sl@0: } sl@0: static inline bool _Stl_is_neg_inf(double x) { return _fpclass(x) == _FPCLASS_NINF; } sl@0: static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && _copysign(1., x) < 0 ; } sl@0: # elif defined (__MRC__) || defined (__SC__) //*TY 02/24/2000 - added support for MPW sl@0: static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); } sl@0: static bool _Stl_is_inf(double x) { return !isfinite(x); } sl@0: static bool _Stl_is_neg_inf(double x) { return !isfinite(x) && signbit(x); } sl@0: static bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); } sl@0: # elif /* defined(__FreeBSD__) || defined(__OpenBSD__) || */ (defined(__GNUC__) && defined(__APPLE__)) sl@0: static inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); } sl@0: static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! isnan(x); } sl@0: static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; } sl@0: static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && copysign(1., x) < 0 ; } sl@0: # elif defined( _AIX ) // JFA 11-Aug-2000 sl@0: static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); } sl@0: static bool _Stl_is_inf(double x) { return !finite(x); } sl@0: // bool _Stl_is_neg_inf(double x) { return _class(x) == FP_MINUS_INF; } sl@0: static bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && ( copysign(1., x) < 0 ); } sl@0: static bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); } sl@0: # elif defined (__ISCPP__) sl@0: static inline bool _Stl_is_nan_or_inf (double x) { return _fp_isINF(x) || _fp_isNAN(x); } sl@0: static inline bool _Stl_is_inf (double x) { return _fp_isINF(x); } sl@0: static inline bool _Stl_is_neg_inf (double x) { return _fp_isINF(x) && x < 0; } sl@0: static inline bool _Stl_is_neg_nan (double x) { return _fp_isNAN(x) && x < 0; } sl@0: # elif defined (_CRAY) sl@0: # if defined (_CRAYIEEE) sl@0: static inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); } sl@0: static inline bool _Stl_is_inf(double x) { return isinf(x); } sl@0: static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && signbit(x); } sl@0: static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); } sl@0: # else sl@0: static inline bool _Stl_is_nan_or_inf(double x) { return false; } sl@0: static inline bool _Stl_is_inf(double x) { return false; } sl@0: static inline bool _Stl_is_neg_inf(double x) { return false; } sl@0: static inline bool _Stl_is_neg_nan(double x) { return false; } sl@0: # endif sl@0: # else // nothing from above sl@0: # define USE_SPRINTF_INSTEAD sl@0: # endif sl@0: #endif // !USE_SPRINTF_INSTEAD sl@0: sl@0: #if !defined (USE_SPRINTF_INSTEAD) sl@0: // Reentrant versions of floating-point conversion functions. The argument sl@0: // lists look slightly different on different operating systems, so we're sl@0: // encapsulating the differences here. sl@0: sl@0: # if defined (__CYGWIN__) || defined(__DJGPP) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvtbuf(x, n, pt, sign, buf); } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvtbuf(x, n, pt, sign, buf); } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvtbuf(x, n, pt, sign, buf); } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvtbuf(x, n, pt, sign, buf); } sl@0: # endif sl@0: # elif defined (_STLP_USE_GLIBC) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return buf + ecvt_r(x, n, pt, sign, buf, NDIG+2); } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return buf + fcvt_r(x, n, pt, sign, buf, NDIG+2); } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return buf + qecvt_r(x, n, pt, sign, buf, NDIG+2); } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return buf + qfcvt_r(x, n, pt, sign, buf, NDIG+2); } sl@0: # endif sl@0: # elif defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvt(x, n, pt, sign); } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvt(x, n, pt, sign); } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvtl(x, n, pt, sign); } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvtl(x, n, pt, sign); } sl@0: # endif sl@0: # elif defined (__sun) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return econvert(x, n, pt, sign, buf); } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fconvert(x, n, pt, sign, buf); } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return qeconvert(&x, n, pt, sign, buf); } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return qfconvert(&x, n, pt, sign, buf); } sl@0: # endif sl@0: # elif defined (__DECCXX) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return (ecvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return (fcvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: // fbp : no "long double" conversions ! sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return (ecvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0) ; } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return (fcvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0); } sl@0: # endif sl@0: # elif defined (__hpux) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvt(x, n, pt, sign); } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvt(x, n, pt, sign); } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: sl@0: # if defined( _REENTRANT ) && (defined(_PTHREADS_DRAFT4) || defined(PTHREAD_THREADS_MAX)) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return (_ldecvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return (_ldfcvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); } sl@0: # else sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return _ldecvt(*(long_double*)&x, n, pt, sign); } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return _ldfcvt(*(long_double*)&x, n, pt, sign); } sl@0: # endif sl@0: # endif sl@0: # elif defined (_AIX) || defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) } sl@0: # endif sl@0: # elif defined (__unix) && !defined (__APPLE__) && !defined (_CRAY) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return ecvt_r(x, n, pt, sign, buf); } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return fcvt_r(x, n, pt, sign, buf); } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return qecvt_r(x, n, pt, sign, buf); } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return qfcvt_r(x, n, pt, sign, buf); } sl@0: # endif sl@0: # elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) sl@0: // those guys claim _cvt functions being reentrant. sl@0: # if defined (_STLP_USE_SAFE_STRING_FUNCTIONS) sl@0: # define _STLP_APPEND(a, b) a##b sl@0: # define _STLP_BUF_PARAMS , char* buf, size_t bsize sl@0: # define _STLP_SECURE_FUN(F, X, N, PT, SIGN) _STLP_APPEND(F, _s)(buf, bsize, X, N, PT, SIGN); return buf sl@0: # else sl@0: # define _STLP_CVT_DONT_NEED_BUF sl@0: # define _STLP_BUF_PARAMS sl@0: # define _STLP_SECURE_FUN(F, X, N, PT, SIGN) return F(X, N, PT, SIGN) sl@0: # endif sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS) sl@0: { _STLP_SECURE_FUN(_ecvt, x, n, pt, sign); } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS) sl@0: { _STLP_SECURE_FUN(_fcvt, x, n, pt, sign); } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS) sl@0: { _STLP_SECURE_FUN(_ecvt, (double)x, n, pt, sign); } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS) sl@0: { _STLP_SECURE_FUN(_fcvt, (double)x, n, pt, sign); } sl@0: # endif sl@0: # undef _STLP_SECURE_FUN sl@0: # undef _STLP_BUF_PARAMS sl@0: # undef _STLP_APPEND sl@0: # elif defined (__ISCPP__) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return _fp_ecvt( x, n, pt, sign, buf); } sl@0: sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) sl@0: { return _fp_fcvt(x, n, pt, sign, buf); } sl@0: sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return _fp_ecvt( x, n, pt, sign, buf); } sl@0: sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) sl@0: { return _fp_fcvt(x, n, pt, sign, buf); } sl@0: # endif sl@0: # elif defined (__MRC__) || defined (__SC__) || defined (_CRAY) sl@0: static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* ) sl@0: { return ecvt( x, n, pt, sign ); } sl@0: static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* ) sl@0: { return fcvt(x, n, pt, sign); } sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* ) sl@0: { return ecvt( x, n, pt, sign ); } sl@0: static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* ) sl@0: { return fcvt(x, n, pt, sign); } sl@0: # endif sl@0: # endif sl@0: sl@0: # if defined (_STLP_CVT_DONT_NEED_BUF) sl@0: # define _STLP_CVT_BUFFER(B) sl@0: # elif !defined (_STLP_USE_SAFE_STRING_FUNCTIONS) sl@0: # define _STLP_CVT_BUFFER(B) , B sl@0: # else sl@0: # define _STLP_CVT_BUFFER(B) , _STLP_ARRAY_AND_SIZE(B) sl@0: # endif sl@0: sl@0: # if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS) sl@0: # define _STLP_BUFFER(B) B sl@0: # else sl@0: # define _STLP_BUFFER(B) _STLP_ARRAY_AND_SIZE(B) sl@0: # endif sl@0: sl@0: //---------------------------------------------------------------------- sl@0: // num_put sl@0: sl@0: // __format_float formats a mantissa and exponent as returned by sl@0: // one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r) sl@0: // according to the specified precision and format flags. This is sl@0: // based on doprnt but is much simpler since it is concerned only sl@0: // with floating point input and does not consider all formats. It sl@0: // also does not deal with blank padding, which is handled by sl@0: // __copy_float_and_fill. sl@0: sl@0: static size_t __format_float_scientific( __iostring& buf, const char *bp, sl@0: int decpt, int sign, bool is_zero, sl@0: ios_base::fmtflags flags, sl@0: int precision, bool /* islong */) sl@0: { sl@0: // sign if required sl@0: if (sign) sl@0: buf += '-'; sl@0: else if (flags & ios_base::showpos) sl@0: buf += '+'; sl@0: sl@0: // first digit of mantissa sl@0: buf += *bp++; sl@0: sl@0: // start of grouping position, grouping won't occur in scientific notation sl@0: // as it is impossible to have something like 1234.0e04 but we return a correct sl@0: // group position for coherency with __format_float_fixed. sl@0: size_t __group_pos = buf.size(); sl@0: sl@0: // decimal point if required sl@0: if (precision != 0 || flags & ios_base::showpoint) { sl@0: buf += '.'; sl@0: } sl@0: sl@0: // rest of mantissa sl@0: int rz = precision; sl@0: while (rz-- > 0 && *bp != 0) sl@0: buf += *bp++; sl@0: sl@0: // exponent sl@0: char expbuf[MAXESIZ + 2]; sl@0: char *suffix = expbuf + MAXESIZ; sl@0: *suffix = 0; sl@0: if (!is_zero) { sl@0: int nn = decpt - 1; sl@0: if (nn < 0) sl@0: nn = -nn; sl@0: for (; nn > 9; nn /= 10) sl@0: *--suffix = (char) todigit(nn % 10); sl@0: *--suffix = (char) todigit(nn); sl@0: } sl@0: sl@0: // prepend leading zeros to exponent sl@0: while (suffix > &expbuf[MAXESIZ - 2]) sl@0: *--suffix = '0'; sl@0: sl@0: // put in the exponent sign sl@0: *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-'); sl@0: sl@0: // put in the e sl@0: *--suffix = flags & ios_base::uppercase ? 'E' : 'e'; sl@0: sl@0: // copy the suffix sl@0: buf += suffix; sl@0: return __group_pos; sl@0: } sl@0: sl@0: static size_t __format_float_fixed( __iostring &buf, const char *bp, sl@0: int decpt, int sign, bool /* x */, sl@0: ios_base::fmtflags flags, sl@0: int precision, bool islong ) sl@0: { sl@0: if ( sign && (decpt > -precision) && (*bp != 0) ) sl@0: buf += '-'; sl@0: else if ( flags & ios_base::showpos ) sl@0: buf += '+'; sl@0: sl@0: int k = 0; sl@0: int maxfsig = islong ? 2*MAXFSIG : MAXFSIG; sl@0: sl@0: // digits before decimal point sl@0: int nnn = decpt; sl@0: do { sl@0: buf += ((nnn <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++)); sl@0: } while ( --nnn > 0 ); sl@0: sl@0: // start of grouping position sl@0: size_t __group_pos = buf.size(); sl@0: sl@0: // decimal point if needed sl@0: if ( flags & ios_base::showpoint || precision > 0 ) { sl@0: buf += '.'; sl@0: } sl@0: sl@0: // digits after decimal point if any sl@0: nnn = (min) (precision, MAXFCVT); sl@0: sl@0: while ( --nnn >= 0 ) { sl@0: buf += (++decpt <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++); sl@0: } sl@0: sl@0: // trailing zeros if needed sl@0: if ( precision > MAXFCVT ) { sl@0: buf.append( precision - MAXFCVT, '0' ); sl@0: } sl@0: sl@0: return __group_pos; sl@0: } sl@0: sl@0: static void __format_nan_or_inf(__iostring& buf, double x, ios_base::fmtflags flags) sl@0: { sl@0: static const char* inf[2] = { "inf", "Inf" }; sl@0: static const char* nan[2] = { "nan", "NaN" }; sl@0: const char** inf_or_nan; sl@0: if (_Stl_is_inf(x)) { // Infinity sl@0: inf_or_nan = inf; sl@0: if (_Stl_is_neg_inf(x)) sl@0: buf += '-'; sl@0: else if (flags & ios_base::showpos) sl@0: buf += '+'; sl@0: } else { // NaN sl@0: inf_or_nan = nan; sl@0: if (_Stl_is_neg_nan(x)) sl@0: buf += '-'; sl@0: else if (flags & ios_base::showpos) sl@0: buf += '+'; sl@0: } sl@0: buf += inf_or_nan[flags & ios_base::uppercase ? 1 : 0]; sl@0: } sl@0: sl@0: template sl@0: static inline size_t __format_float( __iostring &buf, const char * bp, sl@0: int decpt, int sign, max_double_type x, sl@0: ios_base::fmtflags flags, sl@0: int precision, bool islong) sl@0: { sl@0: size_t __group_pos = 0; sl@0: // Output of infinities and NANs does not depend on the format flags sl@0: if (_Stl_is_nan_or_inf((double)x)) { // Infinity or NaN sl@0: __format_nan_or_inf(buf, (double)x, flags); sl@0: } else { // representable number sl@0: switch (flags & ios_base::floatfield) { sl@0: case ios_base::scientific: sl@0: __group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0.0, sl@0: flags, precision, islong); sl@0: break; sl@0: case ios_base::fixed: sl@0: __group_pos = __format_float_fixed( buf, bp, decpt, sign, true, sl@0: flags, precision, islong); sl@0: break; sl@0: default: // g format sl@0: // establish default precision sl@0: if (flags & ios_base::showpoint || precision > 0) { sl@0: if (precision == 0) precision = 1; sl@0: } else sl@0: precision = 6; sl@0: sl@0: // reset exponent if value is zero sl@0: if (x == 0) sl@0: decpt = 1; sl@0: sl@0: int kk = precision; sl@0: if (!(flags & ios_base::showpoint)) { sl@0: size_t n = strlen(bp); sl@0: if (n < (size_t)kk) sl@0: kk = (int)n; sl@0: while (kk >= 1 && bp[kk-1] == '0') sl@0: --kk; sl@0: } sl@0: sl@0: if (decpt < -3 || decpt > precision) { sl@0: precision = kk - 1; sl@0: __group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0, sl@0: flags, precision, islong); sl@0: } else { sl@0: precision = kk - decpt; sl@0: __group_pos = __format_float_fixed( buf, bp, decpt, sign, true, sl@0: flags, precision, islong); sl@0: } sl@0: break; sl@0: } /* switch */ sl@0: } /* else is_nan_or_inf */ sl@0: return __group_pos; sl@0: } sl@0: sl@0: #else /* USE_SPRINTF_INSTEAD */ sl@0: sl@0: struct GroupPos { sl@0: bool operator () (char __c) const { sl@0: return __c == '.' || sl@0: __c == 'e' || __c == 'E'; sl@0: } sl@0: }; sl@0: sl@0: // Creates a format string for sprintf() sl@0: static int __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) { sl@0: fmtbuf[0] = '%'; sl@0: int i = 1; sl@0: sl@0: if (flags & ios_base::showpos) sl@0: fmtbuf[i++] = '+'; sl@0: sl@0: if (flags & ios_base::showpoint) sl@0: fmtbuf[i++] = '#'; sl@0: sl@0: fmtbuf[i++] = '.'; sl@0: fmtbuf[i++] = '*'; sl@0: sl@0: if (long_modifier) sl@0: fmtbuf[i++] = long_modifier; sl@0: sl@0: switch (flags & ios_base::floatfield) sl@0: { sl@0: case ios_base::scientific: sl@0: fmtbuf[i++] = (flags & ios_base::uppercase) ? 'E' : 'e'; sl@0: break; sl@0: case ios_base::fixed: sl@0: # if defined (__FreeBSD__) sl@0: fmtbuf[i++] = 'f'; sl@0: # else sl@0: fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f'; sl@0: # endif sl@0: break; sl@0: default: sl@0: fmtbuf[i++] = (flags & ios_base::uppercase) ? 'G' : 'g'; sl@0: break; sl@0: } sl@0: sl@0: fmtbuf[i] = 0; sl@0: return i; sl@0: } sl@0: sl@0: #endif /* USE_SPRINTF_INSTEAD */ sl@0: sl@0: _STLP_DECLSPEC size_t _STLP_CALL sl@0: __write_float(__iostring &buf, ios_base::fmtflags flags, int precision, sl@0: double x) { sl@0: #if defined (USE_SPRINTF_INSTEAD) sl@0: /* If we want 'abitrary' precision, we should use 'abitrary' buffer size sl@0: * below. - ptr sl@0: */ sl@0: char static_buf[128]; sl@0: // char *static_buf = new char [128+precision]; sl@0: char fmtbuf[32]; sl@0: __fill_fmtbuf(fmtbuf, flags, 0); sl@0: // snprintf(static_buf, 128+precision, fmtbuf, precision, x); sl@0: # if !defined (N_PLAT_NLM) sl@0: snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x); sl@0: # else sl@0: sprintf(static_buf, fmtbuf, precision, x); sl@0: # endif sl@0: buf = static_buf; sl@0: // delete [] static_buf; sl@0: return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin(); sl@0: #else sl@0: # if !defined (_STLP_CVT_DONT_NEED_BUF) sl@0: char cvtbuf[NDIG + 2]; sl@0: # endif sl@0: char * bp; sl@0: int decpt, sign; sl@0: sl@0: switch (flags & ios_base::floatfield) { sl@0: case ios_base::fixed: sl@0: bp = _Stl_fcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf)); sl@0: break; sl@0: case ios_base::scientific : sl@0: bp = _Stl_ecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf)); sl@0: break; sl@0: default : sl@0: bp = _Stl_ecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf)); sl@0: break; sl@0: } sl@0: return __format_float(buf, bp, decpt, sign, x, flags, precision, false); sl@0: #endif sl@0: } sl@0: sl@0: #if !defined (_STLP_NO_LONG_DOUBLE) sl@0: _STLP_DECLSPEC size_t _STLP_CALL sl@0: __write_float(__iostring &buf, ios_base::fmtflags flags, int precision, sl@0: long double x) { sl@0: # if defined (USE_SPRINTF_INSTEAD) sl@0: /* If we want 'abitrary' precision, we should use 'abitrary' buffer size sl@0: * below. - ptr sl@0: */ sl@0: char static_buf[128]; sl@0: // char *static_buf = new char [128+precision]; sl@0: char fmtbuf[64]; sl@0: int i = __fill_fmtbuf(fmtbuf, flags, 'L'); sl@0: // snprintf(static_buf, 128+precision, fmtbuf, precision, x); sl@0: # if !defined (N_PLAT_NLM) sl@0: snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x); sl@0: # else sl@0: sprintf(static_buf, fmtbuf, precision, x); sl@0: # endif sl@0: // we should be able to return buf + sprintf(), but we do not trust'em... sl@0: buf = static_buf; sl@0: // delete [] static_buf; sl@0: return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin(); sl@0: # else sl@0: # if !defined (_STLP_CVT_DONT_NEED_BUF) sl@0: char cvtbuf[NDIG + 2]; sl@0: # endif sl@0: char * bp; sl@0: int decpt, sign; sl@0: sl@0: switch (flags & ios_base::floatfield) { sl@0: case ios_base::fixed: sl@0: bp = _Stl_qfcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf)); sl@0: break; sl@0: case ios_base::scientific: sl@0: bp = _Stl_qecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf)); sl@0: break; sl@0: default : sl@0: bp = _Stl_qecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf)); sl@0: break; sl@0: } sl@0: return __format_float(buf, bp, decpt, sign, x, flags, precision, true); sl@0: # endif /* USE_SPRINTF_INSTEAD */ sl@0: } sl@0: #endif /* _STLP_NO_LONG_DOUBLE */ sl@0: sl@0: _STLP_DECLSPEC void _STLP_CALL __get_floor_digits(__iostring &out, _STLP_LONGEST_FLOAT_TYPE __x) { sl@0: #if defined (USE_SPRINTF_INSTEAD) sl@0: char cvtbuf[128]; sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: # if !defined (N_PLAT_NLM) sl@0: snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%Lf", __x); // check for 1234.56! sl@0: # else sl@0: sprintf(cvtbuf, "%Lf", __x); // check for 1234.56! sl@0: # endif sl@0: # else sl@0: snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%f", __x); // check for 1234.56! sl@0: # endif sl@0: char *p = strchr( cvtbuf, '.' ); sl@0: if ( p == 0 ) { sl@0: out.append( cvtbuf ); sl@0: } else { sl@0: out.append( cvtbuf, p ); sl@0: } sl@0: #else sl@0: # if !defined (_STLP_CVT_DONT_NEED_BUF) sl@0: char cvtbuf[NDIG + 2]; sl@0: # endif sl@0: char * bp; sl@0: int decpt, sign; sl@0: # if !defined (_STLP_NO_LONG_DOUBLE) sl@0: bp = _Stl_qfcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf)); sl@0: # else sl@0: bp = _Stl_fcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf)); sl@0: # endif sl@0: sl@0: if (sign) { sl@0: out += '-'; sl@0: } sl@0: out.append(bp, bp + decpt); sl@0: #endif // USE_PRINTF_INSTEAD sl@0: } sl@0: sl@0: sl@0: #if !defined (_STLP_NO_WCHAR_T) sl@0: _STLP_DECLSPEC void _STLP_CALL __convert_float_buffer( __iostring const& str, __iowstring &out, sl@0: const ctype& ct, wchar_t dot, bool __check_dot) sl@0: { sl@0: string::const_iterator str_ite(str.begin()), str_end(str.end()); sl@0: sl@0: //First loop, check the dot char sl@0: if (__check_dot) { sl@0: while (str_ite != str_end) { sl@0: if (*str_ite != '.') { sl@0: out += ct.widen(*str_ite++); sl@0: } else { sl@0: out += dot; sl@0: break; sl@0: } sl@0: } sl@0: } else { sl@0: if (str_ite != str_end) { sl@0: out += ct.widen(*str_ite); sl@0: } sl@0: } sl@0: sl@0: if (str_ite != str_end) { sl@0: //Second loop, dot has been found, no check anymore sl@0: while (++str_ite != str_end) { sl@0: out += ct.widen(*str_ite); sl@0: } sl@0: } sl@0: } sl@0: sl@0: #endif sl@0: sl@0: void _STLP_CALL sl@0: __adjust_float_buffer(__iostring &str, char dot) { sl@0: if ('.' != dot) { sl@0: size_t __dot_pos = str.find('.'); sl@0: if (__dot_pos != string::npos) { sl@0: str[__dot_pos] = dot; sl@0: } sl@0: } sl@0: } sl@0: sl@0: _STLP_MOVE_TO_STD_NAMESPACE sl@0: _STLP_END_NAMESPACE sl@0: sl@0: // Local Variables: sl@0: // mode:C++ sl@0: // End: