os/ossrv/genericopenlibs/cppstdlib/stl/src/num_put_float.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2  * Copyright (c) 1999
     3  * Silicon Graphics Computer Systems, Inc.
     4  *
     5  * Copyright (c) 1999
     6  * Boris Fomitchev
     7  *
     8  * This material is provided "as is", with absolutely no warranty expressed
     9  * or implied. Any use is at your own risk.
    10  *
    11  * Permission to use or copy this software for any purpose is hereby granted
    12  * without fee, provided the above notices are retained on all copies.
    13  * Permission to modify the code and to distribute modified code is granted,
    14  * provided the above notices are retained, and a notice that the code was
    15  * modified is included with the above copyright notice.
    16  *
    17  */
    18 
    19 #include "stlport_prefix.h"
    20 
    21 #include <cmath>
    22 #include <ios>
    23 #include <locale>
    24 
    25 #if defined (__DECCXX)
    26 #  define NDIG 400
    27 #else
    28 #  define NDIG 82
    29 #endif
    30 
    31 #if defined (_STLP_NO_LONG_DOUBLE)
    32 #  define MAXECVT 17
    33 #  define MAXFCVT 18
    34 typedef double max_double_type;
    35 #else
    36 #  define MAXECVT 35
    37 #  define MAXFCVT 36
    38 typedef long double max_double_type;
    39 #endif
    40 
    41 #define MAXFSIG MAXECVT
    42 #define MAXESIZ 5
    43 
    44 #define todigit(x) ((x)+'0')
    45 
    46 #if defined (_STLP_UNIX)
    47 
    48 #  if defined (__sun)
    49 #    include <floatingpoint.h>
    50 #  endif
    51 
    52 #  if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
    53 // DEC, SGI & Solaris need this
    54 #    include <values.h>
    55 #    include <nan.h>
    56 #  endif
    57 
    58 #  if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \
    59       defined(__FreeBSD__)
    60 #    define USE_SPRINTF_INSTEAD
    61 #  endif
    62 
    63 #  if defined( _AIX ) // JFA 3-Aug-2000
    64 #    include <math.h>
    65 #    include <float.h>
    66 #  endif
    67 
    68 #endif
    69 
    70 #include <cstdio>
    71 #include <cstdlib>
    72 
    73 //#if defined(_CRAY)
    74 //# include <stdlib.h>
    75 //#endif
    76 
    77 #if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \
    78     defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
    79 #  include <float.h>
    80 #endif
    81 
    82 #if defined(__MRC__) || defined(__SC__)  || defined(_CRAY)  //*TY 02/24/2000 - added support for MPW
    83 #  include <fp.h>
    84 #endif
    85 
    86 #if defined (__CYGWIN__)
    87 #  include <ieeefp.h>
    88 #endif
    89 
    90 #if defined (__MSL__)
    91 #  include <cstdlib>  // for atoi
    92 #  include <cstdio>  // for snprintf
    93 #  include <algorithm>
    94 #  include <cassert>
    95 #endif
    96 
    97 #if defined (__ISCPP__)
    98 #  include <cfloat>
    99 #endif
   100 
   101 #include <algorithm>
   102 
   103 #if defined (__DMC__)
   104 #  define snprintf _snprintf
   105 #endif
   106 
   107 #if defined(__hpux) && (!defined(_INCLUDE_HPUX_SOURCE) || defined(__GNUC__))
   108 extern "C" double erf(double);
   109 extern "C" double erfc(double);
   110 extern "C" double gamma(double);                             /* obsolescent */
   111 extern "C" double hypot(double, double);
   112 extern "C" int    isnan(double);
   113 extern "C" double j0(double);
   114 extern "C" double j1(double);
   115 extern "C" double jn(int, double);
   116 extern "C" double lgamma(double);
   117 extern "C" double y0(double);
   118 extern "C" double y1(double);
   119 extern "C" double yn(int, double);
   120 
   121 #  define HUGE_VALF     _SINFINITY
   122 #  define INFINITY      _SINFINITY
   123 #  define NAN           _SQNAN
   124 
   125 #  define isnan(x)              _ISNAN(x)
   126 #  define isinf(x)              _ISINF(x)
   127 #  define signbit(x)            _SIGNBIT(x)
   128 #  define isfinite(x)           _ISFINITE(x)
   129 #  define isnormal(x)           _ISNORMAL(x)
   130 #  define fpclassify(x)         _FPCLASSIFY(x)
   131 #  define isunordered(x,y)      _ISUNORDERED(x,y)
   132 #  define isgreater(x,y)        _ISGREATER(x,y)
   133 #  define isgreaterequal(x,y)   _ISGREATEREQUAL(x,y)
   134 #  define isless(x,y)           _ISLESS(x,y)
   135 #  define islessequal(x,y)      _ISLESSEQUAL(x,y)
   136 #  define islessgreater(x,y)    _ISLESSGREATER(x,y)
   137 
   138 #  define FP_NORMAL     0
   139 #  define FP_ZERO       1
   140 #  define FP_INFINITE   2
   141 #  define FP_SUBNORMAL  3
   142 #  define FP_NAN        4
   143 
   144 #  define DECIMAL_DIG   17
   145 
   146 #  define _IS64(x) (sizeof(x) == sizeof(double))
   147 #  define _IS32(x) (sizeof(x) == sizeof(float))
   148 
   149 extern "C" {
   150   extern double copysign(double, double);
   151   extern const float _SINFINITY;
   152   extern const float _SQNAN;
   153 #  if defined (_PA_RISC)
   154 #    define _ISNAN(x)          (_IS32(x)?_Isnanf(x):(isnan)(x))
   155 #    define _ISINF(x)          (_IS32(x)?_Isinff(x):_Isinf(x))
   156 #    define _SIGNBIT(x)        (_IS32(x)?_Signbitf(x):_Signbit(x))
   157 #    define _ISFINITE(x)       (_IS32(x)?_Isfinitef(x):_Isfinite(x))
   158 #    define _ISNORMAL(x)       (_IS32(x)?_Isnormalf(x):_Isnormal(x))
   159 #    define _FPCLASSIFY(x)     (_IS32(x)?_Fpclassifyf(x)>>1:_Fpclassify(x)>>1)
   160 #    define _ISUNORDERED(x,y)  (_IS32(x)&&_IS32(y)?_Isunorderedf(x,y):_Isunordered(x,y))
   161   extern int _Signbit(double);
   162   extern int _Signbitf(float);
   163   extern int _Isnanf(float);
   164   extern int _Isfinite(double);
   165   extern int _Isfinitef(float);
   166   extern int _Isinf(double);
   167   extern int _Isinff(float);
   168   extern int _Isnormal(double);
   169   extern int _Isnormalf(float);
   170   extern int _Isunordered(double, double);
   171   extern int _Isunorderedf(float, float);
   172   extern int _Fpclassify(double);
   173   extern int _Fpclassifyf(float);
   174 #  else
   175 #    include "math_ia64_internal.h"
   176 #    define _FPCLASSIFY(x)     (_IS32(x)?_Fpclassf(x):_Fpclass(x))
   177   extern int _Fpclass(double);
   178   extern int _Fpclassf(float);
   179 #  endif
   180 }
   181 
   182 #  if !defined (_INCLUDE_XOPEN_SOURCE_EXTENDED)
   183 extern "C" char *fcvt(double, int, int *, int *);
   184 extern "C" char *ecvt(double, int, int *, int *);
   185 #  endif
   186 #  if !defined (_INCLUDE_HPUX_SOURCE)
   187 #    if !defined (_LONG_DOUBLE)
   188 #      define _LONG_DOUBLE
   189 typedef struct {
   190   uint32_t word1, word2, word3, word4;
   191 } long_double;
   192 #    endif /* _LONG_DOUBLE */
   193 extern "C" char *_ldecvt(long_double, int, int *, int *);
   194 extern "C" char *_ldfcvt(long_double, int, int *, int *);
   195 
   196 #  endif
   197 #endif /* __hpux */
   198 
   199 _STLP_BEGIN_NAMESPACE
   200 _STLP_MOVE_TO_PRIV_NAMESPACE
   201 
   202 #if defined (__MWERKS__) || defined(__BEOS__)
   203 #  define USE_SPRINTF_INSTEAD
   204 #endif
   205 
   206 #if defined (_AIX) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
   207 // Some OS'es only provide non-reentrant primitives, so we have to use additional synchronization here
   208 
   209 #  if !defined(_REENTRANT) && !defined(_THREAD_SAFE) && !(defined(_POSIX_THREADS) && defined(__OpenBSD__))
   210 #    define LOCK_CVT
   211 #    define RETURN_CVT(ecvt, x, n, pt, sign, buf) return ecvt(x, n, pt, sign);
   212 #  else
   213 static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER;
   214 #    define LOCK_CVT _STLP_auto_lock lock(__put_float_mutex);
   215 #    define RETURN_CVT(ecvt, x, n, pt, sign, buf) strcpy(buf, ecvt(x, n, pt, sign)); return buf;
   216 #  endif // !_REENTRANT
   217 #endif // _AIX || __FreeBSD__ || __NetBSD__ || __OpenBSD__
   218 
   219 // Tests for infinity and NaN differ on different OSs.  We encapsulate
   220 // these differences here.
   221 
   222 #if !defined (USE_SPRINTF_INSTEAD)
   223 #  if defined (__hpux) || defined (__DJGPP) || (defined (_STLP_USE_GLIBC) && ! defined (__MSL__)) || \
   224       defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
   225 static inline bool _Stl_is_nan_or_inf(double x)
   226 #    if defined (isfinite)
   227 { return !isfinite(x); }
   228 #    else
   229 { return !finite(x); }
   230 #    endif
   231 static inline bool _Stl_is_neg_nan(double x)    { return isnan(x) && ( copysign(1., x) < 0 ); }
   232 static inline bool _Stl_is_inf(double x)        { return isinf(x); }
   233 // inline bool _Stl_is_neg_inf(double x)    { return isinf(x) < 0; }
   234 static inline bool _Stl_is_neg_inf(double x)    { return isinf(x) && x < 0; }
   235 #  elif (defined (__unix) || defined (__unix__)) && \
   236          !defined (__APPLE__) && !defined (__DJGPP) && !defined(__osf__) && \
   237          !defined (_CRAY)
   238 static inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); }
   239 static inline bool _Stl_is_inf(double x)        { return IsNANorINF(x) && IsINF(x); }
   240 static inline bool _Stl_is_neg_inf(double x)    { return (IsINF(x)) && (x < 0.0); }
   241 static inline bool _Stl_is_neg_nan(double x)    { return IsNegNAN(x); }
   242 #  elif defined (__BORLANDC__) && ( __BORLANDC__ < 0x540 )
   243 static inline bool _Stl_is_nan_or_inf(double x) {  return !_finite(x); }
   244 static inline bool _Stl_is_inf(double x)        {  return _Stl_is_nan_or_inf(x) && ! _isnan(x);}
   245 static inline bool _Stl_is_neg_inf(double x)    {  return _Stl_is_inf(x) && x < 0 ; }
   246 static inline bool _Stl_is_neg_nan(double x)    { return _isnan(x) && x < 0 ; }
   247 #  elif defined (_STLP_MSVC_LIB) || (defined (__MINGW32__) && !(defined(__SYMBIAN32__) && defined(__GCCXML__)) ) || defined (__BORLANDC__)
   248 static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
   249 static inline bool _Stl_is_inf(double x)        {
   250   int fclass = _fpclass(x);
   251   return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF;
   252 }
   253 static inline bool _Stl_is_neg_inf(double x)    { return _fpclass(x) == _FPCLASS_NINF; }
   254 static inline bool _Stl_is_neg_nan(double x)    { return _isnan(x) && _copysign(1., x) < 0 ; }
   255 #  elif defined (__MRC__) || defined (__SC__)    //*TY 02/24/2000 - added support for MPW
   256 static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); }
   257 static bool _Stl_is_inf(double x)        { return !isfinite(x); }
   258 static bool _Stl_is_neg_inf(double x)    { return !isfinite(x) && signbit(x); }
   259 static bool _Stl_is_neg_nan(double x)    { return isnan(x) && signbit(x); }
   260 #  elif /* defined(__FreeBSD__) || defined(__OpenBSD__) || */ (defined(__GNUC__) && defined(__APPLE__))
   261 static inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); }
   262 static inline bool _Stl_is_inf(double x)        {   return _Stl_is_nan_or_inf(x) && ! isnan(x); }
   263 static inline bool _Stl_is_neg_inf(double x)    {   return _Stl_is_inf(x) && x < 0 ; }
   264 static inline bool _Stl_is_neg_nan(double x)    { return isnan(x) && copysign(1., x) < 0 ; }
   265 #  elif defined( _AIX ) // JFA 11-Aug-2000
   266 static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); }
   267 static bool _Stl_is_inf(double x)        { return !finite(x); }
   268 // bool _Stl_is_neg_inf(double x)    { return _class(x) == FP_MINUS_INF; }
   269 static bool _Stl_is_neg_inf(double x)    { return _Stl_is_inf(x) && ( copysign(1., x) < 0 );  }
   270 static bool _Stl_is_neg_nan(double x)    { return isnan(x) && ( copysign(1., x) < 0 );  }
   271 #  elif defined (__ISCPP__)
   272 static inline bool _Stl_is_nan_or_inf  (double x) { return _fp_isINF(x) || _fp_isNAN(x); }
   273 static inline bool _Stl_is_inf         (double x) { return _fp_isINF(x); }
   274 static inline bool _Stl_is_neg_inf     (double x) { return _fp_isINF(x) && x < 0; }
   275 static inline bool _Stl_is_neg_nan     (double x) { return _fp_isNAN(x) && x < 0; }
   276 #  elif defined (_CRAY)
   277 #    if defined (_CRAYIEEE)
   278 static inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); }
   279 static inline bool _Stl_is_inf(double x)        { return isinf(x); }
   280 static inline bool _Stl_is_neg_inf(double x)    { return isinf(x) && signbit(x); }
   281 static inline bool _Stl_is_neg_nan(double x)    { return isnan(x) && signbit(x); }
   282 #    else
   283 static inline bool _Stl_is_nan_or_inf(double x) { return false; }
   284 static inline bool _Stl_is_inf(double x)        { return false; }
   285 static inline bool _Stl_is_neg_inf(double x)    { return false; }
   286 static inline bool _Stl_is_neg_nan(double x)    { return false; }
   287 #    endif
   288 #  else // nothing from above
   289 #    define USE_SPRINTF_INSTEAD
   290 #  endif
   291 #endif // !USE_SPRINTF_INSTEAD
   292 
   293 #if !defined (USE_SPRINTF_INSTEAD)
   294 // Reentrant versions of floating-point conversion functions.  The argument
   295 // lists look slightly different on different operating systems, so we're
   296 // encapsulating the differences here.
   297 
   298 #  if defined (__CYGWIN__) || defined(__DJGPP)
   299 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   300 { return ecvtbuf(x, n, pt, sign, buf); }
   301 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   302 { return fcvtbuf(x, n, pt, sign, buf); }
   303 #    if !defined (_STLP_NO_LONG_DOUBLE)
   304 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   305 { return ecvtbuf(x, n, pt, sign, buf); }
   306 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   307 { return fcvtbuf(x, n, pt, sign, buf); }
   308 #    endif
   309 #  elif defined (_STLP_USE_GLIBC)
   310 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   311 { return buf + ecvt_r(x, n, pt, sign, buf, NDIG+2); }
   312 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   313 { return buf + fcvt_r(x, n, pt, sign, buf, NDIG+2); }
   314 #    if !defined (_STLP_NO_LONG_DOUBLE)
   315 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   316 { return buf + qecvt_r(x, n, pt, sign, buf, NDIG+2); }
   317 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   318 { return buf + qfcvt_r(x, n, pt, sign, buf, NDIG+2); }
   319 #    endif
   320 #  elif defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
   321 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   322 { return ecvt(x, n, pt, sign); }
   323 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   324 { return fcvt(x, n, pt, sign); }
   325 #    if !defined (_STLP_NO_LONG_DOUBLE)
   326 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   327 { return ecvtl(x, n, pt, sign); }
   328 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   329 { return fcvtl(x, n, pt, sign); }
   330 #    endif
   331 #  elif defined (__sun)
   332 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   333 { return econvert(x, n, pt, sign, buf); }
   334 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   335 { return fconvert(x, n, pt, sign, buf); }
   336 #    if !defined (_STLP_NO_LONG_DOUBLE)
   337 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   338 { return qeconvert(&x, n, pt, sign, buf); }
   339 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   340 { return qfconvert(&x, n, pt, sign, buf); }
   341 #    endif
   342 #  elif defined (__DECCXX)
   343 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   344 { return (ecvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
   345 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   346 { return (fcvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
   347 #    if !defined (_STLP_NO_LONG_DOUBLE)
   348 // fbp : no "long double" conversions !
   349 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   350 { return (ecvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0) ; }
   351 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   352 { return (fcvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
   353 #    endif
   354 #  elif defined (__hpux)
   355 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   356 { return ecvt(x, n, pt, sign); }
   357 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   358 { return fcvt(x, n, pt, sign); }
   359 #    if !defined (_STLP_NO_LONG_DOUBLE)
   360 
   361 #      if defined( _REENTRANT ) && (defined(_PTHREADS_DRAFT4) || defined(PTHREAD_THREADS_MAX))
   362 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   363 { return (_ldecvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); }
   364 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   365 { return (_ldfcvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); }
   366 #      else
   367 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   368 { return _ldecvt(*(long_double*)&x, n, pt, sign); }
   369 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   370 { return _ldfcvt(*(long_double*)&x, n, pt, sign); }
   371 #      endif
   372 #    endif
   373 #  elif defined (_AIX) || defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
   374 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   375 { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) }
   376 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   377 { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) }
   378 #    if !defined (_STLP_NO_LONG_DOUBLE)
   379 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   380 { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) }
   381 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   382 { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) }
   383 #    endif
   384 #  elif defined (__unix) && !defined (__APPLE__) && !defined (_CRAY)
   385 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   386 { return ecvt_r(x, n, pt, sign, buf); }
   387 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   388 { return fcvt_r(x, n, pt, sign, buf); }
   389 #    if !defined (_STLP_NO_LONG_DOUBLE)
   390 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   391 { return qecvt_r(x, n, pt, sign, buf); }
   392 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   393 { return qfcvt_r(x, n, pt, sign, buf); }
   394 #    endif
   395 #  elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
   396 // those guys claim _cvt functions being reentrant.
   397 #    if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
   398 #      define _STLP_APPEND(a, b) a##b
   399 #      define _STLP_BUF_PARAMS , char* buf, size_t bsize
   400 #      define _STLP_SECURE_FUN(F, X, N, PT, SIGN) _STLP_APPEND(F, _s)(buf, bsize, X, N, PT, SIGN); return buf
   401 #    else
   402 #      define _STLP_CVT_DONT_NEED_BUF
   403 #      define _STLP_BUF_PARAMS
   404 #      define _STLP_SECURE_FUN(F, X, N, PT, SIGN) return F(X, N, PT, SIGN)
   405 #    endif
   406 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
   407 { _STLP_SECURE_FUN(_ecvt, x, n, pt, sign); }
   408 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
   409 { _STLP_SECURE_FUN(_fcvt, x, n, pt, sign); }
   410 #    if !defined (_STLP_NO_LONG_DOUBLE)
   411 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
   412 { _STLP_SECURE_FUN(_ecvt, (double)x, n, pt, sign); }
   413 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
   414 { _STLP_SECURE_FUN(_fcvt, (double)x, n, pt, sign); }
   415 #    endif
   416 #    undef _STLP_SECURE_FUN
   417 #    undef _STLP_BUF_PARAMS
   418 #    undef _STLP_APPEND
   419 #  elif defined (__ISCPP__)
   420 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   421 { return _fp_ecvt( x, n, pt, sign, buf); }
   422 
   423 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   424 { return _fp_fcvt(x, n, pt, sign, buf); }
   425 
   426 #    if !defined (_STLP_NO_LONG_DOUBLE)
   427 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
   428 { return _fp_ecvt( x, n, pt, sign, buf); }
   429 
   430 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
   431 { return _fp_fcvt(x, n, pt, sign, buf); }
   432 #    endif
   433 #  elif defined (__MRC__) || defined (__SC__) || defined (_CRAY)
   434 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* )
   435 { return ecvt( x, n, pt, sign ); }
   436 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* )
   437 { return fcvt(x, n, pt, sign); }
   438 #    if !defined (_STLP_NO_LONG_DOUBLE)
   439 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* )
   440 { return ecvt( x, n, pt, sign ); }
   441 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* )
   442 { return fcvt(x, n, pt, sign); }
   443 #    endif
   444 #  endif
   445 
   446 #  if defined (_STLP_CVT_DONT_NEED_BUF)
   447 #    define _STLP_CVT_BUFFER(B)
   448 #  elif !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
   449 #    define _STLP_CVT_BUFFER(B) , B
   450 #  else
   451 #    define _STLP_CVT_BUFFER(B) , _STLP_ARRAY_AND_SIZE(B)
   452 #  endif
   453 
   454 #  if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
   455 #    define _STLP_BUFFER(B) B
   456 #  else
   457 #    define _STLP_BUFFER(B) _STLP_ARRAY_AND_SIZE(B)
   458 #  endif
   459 
   460 //----------------------------------------------------------------------
   461 // num_put
   462 
   463 // __format_float formats a mantissa and exponent as returned by
   464 // one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r)
   465 // according to the specified precision and format flags.  This is
   466 // based on doprnt but is much simpler since it is concerned only
   467 // with floating point input and does not consider all formats.  It
   468 // also does not deal with blank padding, which is handled by
   469 // __copy_float_and_fill.
   470 
   471 static size_t __format_float_scientific( __iostring& buf, const char *bp,
   472                                          int decpt, int sign, bool is_zero,
   473                                          ios_base::fmtflags flags,
   474                                          int precision, bool /* islong */)
   475 {
   476   // sign if required
   477   if (sign)
   478     buf += '-';
   479   else if (flags & ios_base::showpos)
   480     buf += '+';
   481 
   482   // first digit of mantissa
   483   buf += *bp++;
   484 
   485   // start of grouping position, grouping won't occur in scientific notation
   486   // as it is impossible to have something like 1234.0e04 but we return a correct
   487   // group position for coherency with __format_float_fixed.
   488   size_t __group_pos = buf.size();
   489 
   490   // decimal point if required
   491   if (precision != 0 || flags & ios_base::showpoint) {
   492     buf += '.';
   493   }
   494 
   495   // rest of mantissa
   496   int rz = precision;
   497   while (rz-- > 0 && *bp != 0)
   498     buf += *bp++;
   499 
   500   // exponent
   501   char expbuf[MAXESIZ + 2];
   502   char *suffix = expbuf + MAXESIZ;
   503   *suffix = 0;
   504   if (!is_zero) {
   505     int nn = decpt - 1;
   506     if (nn < 0)
   507       nn = -nn;
   508     for (; nn > 9; nn /= 10)
   509       *--suffix = (char) todigit(nn % 10);
   510     *--suffix = (char) todigit(nn);
   511   }
   512 
   513   // prepend leading zeros to exponent
   514   while (suffix > &expbuf[MAXESIZ - 2])
   515     *--suffix = '0';
   516 
   517   // put in the exponent sign
   518   *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-');
   519 
   520   // put in the e
   521   *--suffix = flags & ios_base::uppercase ? 'E' : 'e';
   522 
   523   // copy the suffix
   524   buf += suffix;
   525   return __group_pos;
   526 }
   527 
   528 static size_t __format_float_fixed( __iostring &buf, const char *bp,
   529                                     int decpt, int sign, bool /* x */,
   530                                     ios_base::fmtflags flags,
   531                                     int precision, bool islong )
   532 {
   533   if ( sign && (decpt > -precision) && (*bp != 0) )
   534     buf += '-';
   535   else if ( flags & ios_base::showpos )
   536     buf += '+';
   537 
   538   int k       = 0;
   539   int maxfsig = islong ? 2*MAXFSIG : MAXFSIG;
   540 
   541   // digits before decimal point
   542   int nnn = decpt;
   543   do {
   544     buf += ((nnn <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++));
   545   } while ( --nnn > 0 );
   546 
   547   // start of grouping position
   548   size_t __group_pos = buf.size();
   549 
   550   // decimal point if needed
   551   if ( flags & ios_base::showpoint || precision > 0 ) {
   552     buf += '.';
   553   }
   554 
   555   // digits after decimal point if any
   556   nnn = (min) (precision, MAXFCVT);
   557 
   558   while ( --nnn >= 0 ) {
   559     buf += (++decpt <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++);
   560   }
   561 
   562   // trailing zeros if needed
   563   if ( precision > MAXFCVT ) {
   564     buf.append( precision - MAXFCVT, '0' );
   565   }
   566 
   567   return __group_pos;
   568 }
   569 
   570 static void __format_nan_or_inf(__iostring& buf, double x, ios_base::fmtflags flags)
   571 {
   572   static const char* inf[2] = { "inf", "Inf" };
   573   static const char* nan[2] = { "nan", "NaN" };
   574   const char** inf_or_nan;
   575   if (_Stl_is_inf(x)) {            // Infinity
   576     inf_or_nan = inf;
   577     if (_Stl_is_neg_inf(x))
   578       buf += '-';
   579     else if (flags & ios_base::showpos)
   580       buf += '+';
   581   } else {                      // NaN
   582     inf_or_nan = nan;
   583     if (_Stl_is_neg_nan(x))
   584       buf += '-';
   585     else if (flags & ios_base::showpos)
   586       buf += '+';
   587   }
   588   buf += inf_or_nan[flags & ios_base::uppercase ? 1 : 0];
   589 }
   590 
   591 template <class max_double_type>
   592 static inline size_t __format_float( __iostring &buf, const char * bp,
   593                                      int decpt, int sign, max_double_type x,
   594                                      ios_base::fmtflags flags,
   595                                      int precision, bool islong)
   596 {
   597   size_t __group_pos = 0;
   598   // Output of infinities and NANs does not depend on the format flags
   599   if (_Stl_is_nan_or_inf((double)x)) {       // Infinity or NaN
   600     __format_nan_or_inf(buf, (double)x, flags);
   601   } else {                        // representable number
   602     switch (flags & ios_base::floatfield) {
   603       case ios_base::scientific:
   604         __group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0.0,
   605                                                  flags, precision, islong);
   606         break;
   607       case ios_base::fixed:
   608         __group_pos = __format_float_fixed( buf, bp, decpt, sign, true,
   609                                             flags, precision, islong);
   610         break;
   611       default: // g format
   612         // establish default precision
   613         if (flags & ios_base::showpoint || precision > 0) {
   614           if (precision == 0) precision = 1;
   615         } else
   616           precision = 6;
   617 
   618         // reset exponent if value is zero
   619         if (x == 0)
   620           decpt = 1;
   621 
   622         int kk = precision;
   623         if (!(flags & ios_base::showpoint)) {
   624           size_t n = strlen(bp);
   625           if (n < (size_t)kk)
   626             kk = (int)n;
   627           while (kk >= 1 && bp[kk-1] == '0')
   628             --kk;
   629         }
   630 
   631         if (decpt < -3 || decpt > precision) {
   632           precision = kk - 1;
   633           __group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0,
   634                                                    flags, precision, islong);
   635         } else {
   636           precision = kk - decpt;
   637           __group_pos = __format_float_fixed( buf, bp, decpt, sign, true,
   638                                               flags, precision, islong);
   639         }
   640         break;
   641     } /* switch */
   642   } /* else is_nan_or_inf */
   643   return __group_pos;
   644 }
   645 
   646 #else /* USE_SPRINTF_INSTEAD */
   647 
   648 struct GroupPos {
   649   bool operator () (char __c) const {
   650     return __c == '.' ||
   651            __c == 'e' || __c == 'E';
   652   }
   653 };
   654 
   655 // Creates a format string for sprintf()
   656 static int __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) {
   657   fmtbuf[0] = '%';
   658   int i = 1;
   659 
   660   if (flags & ios_base::showpos)
   661     fmtbuf[i++] = '+';
   662 
   663   if (flags & ios_base::showpoint)
   664     fmtbuf[i++] = '#';
   665 
   666   fmtbuf[i++] = '.';
   667   fmtbuf[i++] = '*';
   668 
   669   if (long_modifier)
   670     fmtbuf[i++] = long_modifier;
   671 
   672   switch (flags & ios_base::floatfield)
   673     {
   674     case ios_base::scientific:
   675       fmtbuf[i++] = (flags & ios_base::uppercase) ?  'E' : 'e';
   676       break;
   677     case ios_base::fixed:
   678 #  if defined (__FreeBSD__)
   679       fmtbuf[i++] = 'f';
   680 #  else
   681       fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f';
   682 #  endif
   683       break;
   684     default:
   685       fmtbuf[i++] = (flags & ios_base::uppercase) ?  'G' : 'g';
   686       break;
   687     }
   688 
   689   fmtbuf[i] = 0;
   690   return i;
   691 }
   692 
   693 #endif  /* USE_SPRINTF_INSTEAD */
   694 
   695 _STLP_DECLSPEC size_t  _STLP_CALL
   696 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
   697               double x) {
   698 #if defined (USE_SPRINTF_INSTEAD)
   699   /* If we want 'abitrary' precision, we should use 'abitrary' buffer size
   700    * below. - ptr
   701    */
   702   char static_buf[128];
   703   // char *static_buf = new char [128+precision];
   704   char fmtbuf[32];
   705   __fill_fmtbuf(fmtbuf, flags, 0);
   706   // snprintf(static_buf, 128+precision, fmtbuf, precision, x);
   707 #  if !defined (N_PLAT_NLM)
   708   snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
   709 #  else
   710   sprintf(static_buf, fmtbuf, precision, x);
   711 #  endif
   712   buf = static_buf;
   713   // delete [] static_buf;
   714   return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
   715 #else
   716 #  if !defined (_STLP_CVT_DONT_NEED_BUF)
   717   char cvtbuf[NDIG + 2];
   718 #  endif
   719   char * bp;
   720   int decpt, sign;
   721 
   722   switch (flags & ios_base::floatfield) {
   723   case ios_base::fixed:
   724     bp = _Stl_fcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
   725     break;
   726   case ios_base::scientific :
   727     bp = _Stl_ecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
   728     break;
   729   default :
   730     bp = _Stl_ecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
   731     break;
   732   }
   733   return __format_float(buf, bp, decpt, sign, x, flags, precision, false);
   734 #endif
   735 }
   736 
   737 #if !defined (_STLP_NO_LONG_DOUBLE)
   738 _STLP_DECLSPEC size_t _STLP_CALL
   739 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
   740               long double x) {
   741 #  if defined (USE_SPRINTF_INSTEAD)
   742   /* If we want 'abitrary' precision, we should use 'abitrary' buffer size
   743    * below. - ptr
   744    */
   745   char static_buf[128];
   746   // char *static_buf = new char [128+precision];
   747   char fmtbuf[64];
   748   int i = __fill_fmtbuf(fmtbuf, flags, 'L');
   749   // snprintf(static_buf, 128+precision, fmtbuf, precision, x);
   750 #    if !defined (N_PLAT_NLM)
   751   snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
   752 #    else
   753   sprintf(static_buf, fmtbuf, precision, x);
   754 #    endif
   755   // we should be able to return buf + sprintf(), but we do not trust'em...
   756   buf = static_buf;
   757   // delete [] static_buf;
   758   return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
   759 #  else
   760 #    if !defined (_STLP_CVT_DONT_NEED_BUF)
   761   char cvtbuf[NDIG + 2];
   762 #    endif
   763   char * bp;
   764   int decpt, sign;
   765 
   766   switch (flags & ios_base::floatfield) {
   767   case ios_base::fixed:
   768     bp = _Stl_qfcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
   769     break;
   770   case ios_base::scientific:
   771     bp = _Stl_qecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
   772     break;
   773   default :
   774     bp = _Stl_qecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
   775     break;
   776   }
   777   return __format_float(buf, bp, decpt, sign, x, flags, precision, true);
   778 #  endif /* USE_SPRINTF_INSTEAD */
   779 }
   780 #endif /* _STLP_NO_LONG_DOUBLE */
   781 
   782 _STLP_DECLSPEC void _STLP_CALL __get_floor_digits(__iostring &out, _STLP_LONGEST_FLOAT_TYPE __x) {
   783 #if defined (USE_SPRINTF_INSTEAD)
   784   char cvtbuf[128];
   785 #  if !defined (_STLP_NO_LONG_DOUBLE)
   786 #    if !defined (N_PLAT_NLM)
   787   snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%Lf", __x); // check for 1234.56!
   788 #    else
   789   sprintf(cvtbuf, "%Lf", __x); // check for 1234.56!
   790 #    endif
   791 #  else
   792   snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%f", __x);  // check for 1234.56!
   793 #  endif
   794   char *p = strchr( cvtbuf, '.' );
   795   if ( p == 0 ) {
   796     out.append( cvtbuf );
   797   } else {
   798     out.append( cvtbuf, p );
   799   }
   800 #else
   801 #  if !defined (_STLP_CVT_DONT_NEED_BUF)
   802   char cvtbuf[NDIG + 2];
   803 #  endif
   804   char * bp;
   805   int decpt, sign;
   806 #  if !defined (_STLP_NO_LONG_DOUBLE)
   807   bp = _Stl_qfcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
   808 #  else
   809   bp = _Stl_fcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
   810 #  endif
   811 
   812   if (sign) {
   813     out += '-';
   814   }
   815   out.append(bp, bp + decpt);
   816 #endif // USE_PRINTF_INSTEAD
   817 }
   818 
   819 
   820 #if !defined (_STLP_NO_WCHAR_T)
   821 _STLP_DECLSPEC void _STLP_CALL __convert_float_buffer( __iostring const& str, __iowstring &out,
   822                                         const ctype<wchar_t>& ct, wchar_t dot, bool __check_dot)
   823 {
   824   string::const_iterator str_ite(str.begin()), str_end(str.end());
   825 
   826   //First loop, check the dot char
   827   if (__check_dot) {
   828     while (str_ite != str_end) {
   829       if (*str_ite != '.') {
   830         out += ct.widen(*str_ite++);
   831       } else {
   832         out += dot;
   833         break;
   834       }
   835     }
   836   } else {
   837     if (str_ite != str_end) {
   838       out += ct.widen(*str_ite);
   839     }
   840   }
   841 
   842   if (str_ite != str_end) {
   843     //Second loop, dot has been found, no check anymore
   844     while (++str_ite != str_end) {
   845       out += ct.widen(*str_ite);
   846     }
   847   }
   848 }
   849 
   850 #endif
   851 
   852 void _STLP_CALL
   853 __adjust_float_buffer(__iostring &str, char dot) {
   854   if ('.' != dot) {
   855     size_t __dot_pos = str.find('.');
   856     if (__dot_pos != string::npos) {
   857       str[__dot_pos] = dot;
   858     }
   859   }
   860 }
   861 
   862 _STLP_MOVE_TO_STD_NAMESPACE
   863 _STLP_END_NAMESPACE
   864 
   865 // Local Variables:
   866 // mode:C++
   867 // End: