_limits.h

00001 /*
00002  * Copyright (c) 1997
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999 
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted 
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 
00019 /* NOTE: This may be not portable code. Parts of numeric_limits<> are
00020  * inherently machine-dependent.  At present this file is suitable
00021  * for the MIPS, SPARC, Alpha and ia32 architectures.
00022  */
00023 
00024 #ifndef _STLP_INTERNAL_LIMITS_H
00025 # define _STLP_INTERNAL_LIMITS_H
00026 
00027 #ifndef _STLP_CLIMITS
00028 # include <climits>
00029 #endif
00030 
00031 #ifndef _STLP_CFLOAT
00032 # include <cfloat>
00033 #endif
00034 
00035 #if !defined (_STLP_NO_WCHAR_T)
00036 # include <cwchar>
00037 #endif
00038 
00039 _STLP_BEGIN_NAMESPACE
00040 
00041 enum float_round_style {
00042   round_indeterminate       = -1,
00043   round_toward_zero         =  0,
00044   round_to_nearest          =  1,
00045   round_toward_infinity     =  2,
00046   round_toward_neg_infinity =  3
00047 };
00048 
00049 enum float_denorm_style {
00050   denorm_indeterminate = -1,
00051   denorm_absent        =  0,
00052   denorm_present       =  1
00053 };
00054 
00055 // Base class for all specializations of numeric_limits.
00056 
00057 template <class __number>
00058 class _Numeric_limits_base {
00059 public:
00060 
00061   static __number (_STLP_CALL min)() _STLP_NOTHROW { return __number(); }
00062   static __number (_STLP_CALL max)() _STLP_NOTHROW { return __number(); }
00063 
00064 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00065   enum {
00066 # else
00067   static const int 
00068 # endif
00069   
00070   digits = 0,
00071   digits10 = 0,
00072   radix = 0,
00073   min_exponent = 0,
00074   min_exponent10 = 0,
00075   max_exponent = 0,
00076   max_exponent10 = 0
00077 
00078 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00079   ,  
00080   has_denorm = denorm_absent,
00081   round_style = round_toward_zero,
00082 # else
00083   ;
00084   static const float_denorm_style has_denorm =  denorm_absent;
00085   static const float_round_style round_style = round_toward_zero;
00086   static const bool 
00087 # endif
00088 
00089     is_specialized = false,
00090     is_signed  = false,
00091     is_integer = false,
00092     is_exact = false,
00093     has_infinity = false,
00094     has_quiet_NaN = false,
00095     has_signaling_NaN = false,
00096     has_denorm_loss = false,
00097     is_iec559 = false,
00098     is_bounded = false,
00099     is_modulo = false,
00100     traps = false,
00101     tinyness_before = false
00102 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00103   }
00104 # endif
00105   ;
00106     
00107   static __number _STLP_CALL epsilon() _STLP_NOTHROW     { return __number(); }
00108   static __number _STLP_CALL round_error() _STLP_NOTHROW { return __number(); }
00109 
00110   static __number _STLP_CALL infinity() _STLP_NOTHROW      { return __number(); }
00111   static __number _STLP_CALL quiet_NaN() _STLP_NOTHROW     { return __number(); }
00112   static __number _STLP_CALL signaling_NaN() _STLP_NOTHROW { return __number(); }
00113   static __number _STLP_CALL denorm_min() _STLP_NOTHROW    { return __number(); }
00114 
00115 
00116 };
00117 
00118 // Base class for integers.
00119 
00120 # ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
00121 #  ifdef _STLP_LONG_LONG
00122 #   define _STLP_LIMITS_MIN_TYPE _STLP_LONG_LONG
00123 #   define _STLP_LIMITS_MAX_TYPE unsigned _STLP_LONG_LONG
00124 #  else
00125 #   define _STLP_LIMITS_MIN_TYPE long
00126 #   define _STLP_LIMITS_MAX_TYPE unsigned long
00127 #  endif
00128 # else
00129 #   define _STLP_LIMITS_MIN_TYPE _Int
00130 #   define _STLP_LIMITS_MAX_TYPE _Int
00131 # endif /* _STLP_LIMITED_DEFAULT_TEMPLATES */
00132 
00133 template <class _Int,
00134           _STLP_LIMITS_MIN_TYPE __imin,
00135           _STLP_LIMITS_MAX_TYPE __imax,
00136           int __idigits, bool __ismod>
00137 class _Integer_limits : public _Numeric_limits_base<_Int> 
00138 {
00139 public:
00140 
00141   static _Int (_STLP_CALL min) () _STLP_NOTHROW { return (_Int)__imin; }
00142   static _Int (_STLP_CALL max) () _STLP_NOTHROW { return (_Int)__imax; }
00143 
00144 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00145   enum {
00146 # else
00147   static const int 
00148 # endif  
00149   digits = (__idigits < 0) ?
00150   ((int)((sizeof(_Int) * (CHAR_BIT))) - ((__imin == 0) ? 0 : 1))
00151   : (__idigits),
00152   digits10 = (digits * 301UL) / 1000,
00153   radix = 2
00154 # if ! defined ( _STLP_STATIC_CONST_INIT_BUG)
00155   ;
00156   static const bool
00157 # else
00158   ,
00159 # endif
00160   is_specialized = true,
00161   is_signed = (__imin != 0),
00162   is_integer = true,
00163   is_exact = true,
00164   is_bounded = true,
00165   is_modulo = __ismod
00166 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00167   }
00168 # endif
00169   ;
00170 };
00171 
00172 // Base class for floating-point numbers.
00173 template <class __number,
00174          int __Digits, int __Digits10,
00175          int __MinExp, int __MaxExp,
00176          int __MinExp10, int __MaxExp10,
00177          bool __IsIEC559,
00178          float_round_style __RoundStyle>
00179 class _Floating_limits : public _Numeric_limits_base<__number>
00180 {
00181 public:
00182 
00183 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00184   enum {
00185 # else
00186   static const int 
00187 # endif  
00188 
00189   digits = __Digits,
00190   digits10 = __Digits10,
00191 
00192   radix = (  FLT_RADIX /* 2 */ ),
00193   min_exponent = __MinExp, 
00194   max_exponent = __MaxExp,
00195   min_exponent10 = __MinExp10,
00196   max_exponent10 = __MaxExp10
00197 
00198 # if defined (_STLP_STATIC_CONST_INIT_BUG)
00199   ,  
00200   has_denorm = denorm_indeterminate,
00201   round_style = __RoundStyle,
00202 # else
00203   ;
00204   static const float_denorm_style has_denorm = denorm_indeterminate;
00205   static const float_round_style round_style = __RoundStyle;
00206   static const bool 
00207 # endif
00208 
00209   is_specialized = true,
00210   is_signed = true, 
00211   has_infinity     =  true,
00212   has_quiet_NaN    =  true,
00213   has_signaling_NaN=  true,
00214   has_denorm_loss  =  false,
00215   is_iec559      =  __IsIEC559,
00216   is_bounded     =  true,
00217   traps          =  true,
00218   tinyness_before=  false
00219 
00220 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00221   }
00222 # endif
00223   ;
00224 
00225 };
00226 
00227 // Class numeric_limits
00228 
00229 // The unspecialized class.
00230 
00231 template<class _Tp> 
00232 class numeric_limits : public _Numeric_limits_base<_Tp> {};
00233 
00234 // Specializations for all built-in integral types.
00235 
00236 #ifndef _STLP_NO_BOOL
00237 
00238 _STLP_TEMPLATE_NULL
00239 class   numeric_limits<bool>
00240   : public _Integer_limits<bool, false, true, 1, false>
00241 {};
00242 
00243 #endif /* _STLP_NO_BOOL */
00244 
00245 _STLP_TEMPLATE_NULL
00246 class   numeric_limits<char>
00247   : public _Integer_limits<char, CHAR_MIN, CHAR_MAX, -1, true>
00248 {};
00249 
00250 # ifndef _STLP_NO_SIGNED_BUILTINS
00251 _STLP_TEMPLATE_NULL
00252 class   numeric_limits<signed char>
00253   : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX, -1, true>
00254 {};
00255 # endif
00256 
00257 _STLP_TEMPLATE_NULL
00258 class   numeric_limits<unsigned char>
00259   : public _Integer_limits<unsigned char, 0, UCHAR_MAX, -1, true>
00260 {};
00261 
00262 #if !(defined ( _STLP_NO_WCHAR_T ) || defined (_STLP_WCHAR_T_IS_USHORT))
00263 
00264 _STLP_TEMPLATE_NULL
00265 class   numeric_limits<wchar_t>
00266   : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX, -1, true>
00267 {};
00268 
00269 #endif
00270 
00271 _STLP_TEMPLATE_NULL
00272 class   numeric_limits<short>
00273   : public _Integer_limits<short, SHRT_MIN, SHRT_MAX, -1, true>
00274 {};
00275 
00276 _STLP_TEMPLATE_NULL
00277 class   numeric_limits<unsigned short>
00278   : public _Integer_limits<unsigned short, 0, USHRT_MAX, -1, true>
00279 {};
00280 
00281 _STLP_TEMPLATE_NULL
00282 class   numeric_limits<int>
00283   : public _Integer_limits<int, INT_MIN, INT_MAX, -1, true>
00284 {};
00285 
00286 _STLP_TEMPLATE_NULL
00287 class   numeric_limits<unsigned int>
00288   : public _Integer_limits<unsigned int, 0, UINT_MAX, -1, true>
00289 {};
00290 
00291 _STLP_TEMPLATE_NULL
00292 class   numeric_limits<long>
00293   : public _Integer_limits<long, LONG_MIN, LONG_MAX, -1, true>
00294 {};
00295 
00296 _STLP_TEMPLATE_NULL
00297 class   numeric_limits<unsigned long>
00298   : public _Integer_limits<unsigned long, 0, ULONG_MAX, -1, true>
00299 {};
00300 
00301 #ifdef _STLP_LONG_LONG
00302 
00303 # if defined (_STLP_MSVC) || defined (__BORLANDC__)
00304 
00305 #    define LONGLONG_MAX     0x7fffffffffffffffi64
00306 #    define LONGLONG_MIN     (-LONGLONG_MAX-1i64)
00307 #    define ULONGLONG_MAX    0xffffffffffffffffUi64
00308 
00309 # else
00310 
00311 #  ifndef   LONGLONG_MAX
00312 #    define LONGLONG_MAX     0x7fffffffffffffffLL
00313 #  endif
00314 #  ifndef   LONGLONG_MIN
00315 #    define LONGLONG_MIN     (-LONGLONG_MAX-1LL)
00316 #  endif
00317 #  ifndef   ULONGLONG_MAX
00318 #    define ULONGLONG_MAX    0xffffffffffffffffULL
00319 #  endif
00320 
00321 # endif
00322 
00323 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ <= 96)
00324 
00325 _STLP_TEMPLATE_NULL
00326 class   numeric_limits<_STLP_LONG_LONG>
00327   : public _Integer_limits<_STLP_LONG_LONG, LONGLONG_MIN, LONGLONG_MAX, -1, true>
00328 {};
00329 
00330 _STLP_TEMPLATE_NULL
00331 class   numeric_limits<unsigned _STLP_LONG_LONG>
00332   : public _Integer_limits<unsigned _STLP_LONG_LONG, 0, ULONGLONG_MAX, -1, true>
00333 {};
00334 #else /* gcc 2.97 (after 2000-11-01), 2.98, 3.0 */
00335 /*
00336  newest gcc has new mangling scheme, that has problem
00337  with generating name [instantiated] of template specialization like
00338  _Integer_limits<_STLP_LONG_LONG, LONGLONG_MIN, LONGLONG_MAX, -1, true>
00339                                   ~~~~~~~~~~~~  ~~~~~~~~~~~~
00340  Below is code that solve this problem.
00341    - ptr
00342  */
00343 _STLP_TEMPLATE_NULL
00344 class   numeric_limits<_STLP_LONG_LONG>
00345   : public _Numeric_limits_base<_STLP_LONG_LONG> 
00346 {
00347 public:
00348 
00349   static _STLP_LONG_LONG (_STLP_CALL min) () _STLP_NOTHROW { return LONGLONG_MIN; }
00350   static _STLP_LONG_LONG (_STLP_CALL max) () _STLP_NOTHROW { return LONGLONG_MAX; }
00351 
00352 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00353   enum {
00354 # else
00355   static const int 
00356 # endif  
00357   digits = ((int)((sizeof(_STLP_LONG_LONG) * (CHAR_BIT))) - 1),
00358   digits10 = (digits * 301UL) / 1000,
00359   radix = 2
00360 # if ! defined ( _STLP_STATIC_CONST_INIT_BUG)
00361   ;
00362   static const bool
00363 # else
00364   ,
00365 # endif
00366   is_specialized = true,
00367   is_signed = true,
00368   is_integer = true,
00369   is_exact = true,
00370   is_bounded = true,
00371   is_modulo = true
00372 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00373   }
00374 # endif
00375   ;
00376 };
00377 
00378 _STLP_TEMPLATE_NULL
00379 class   numeric_limits<unsigned _STLP_LONG_LONG>
00380   : public _Numeric_limits_base<unsigned _STLP_LONG_LONG> 
00381 {
00382 public:
00383 
00384   static unsigned _STLP_LONG_LONG (_STLP_CALL min) () _STLP_NOTHROW { return 0ULL; }
00385   static unsigned _STLP_LONG_LONG (_STLP_CALL max) () _STLP_NOTHROW { return ULONGLONG_MAX; }
00386 
00387 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00388   enum {
00389 # else
00390   static const int 
00391 # endif  
00392   digits = ((int)((sizeof(unsigned _STLP_LONG_LONG) * (CHAR_BIT)))),
00393   digits10 = (digits * 301UL) / 1000,
00394   radix = 2
00395 # if ! defined ( _STLP_STATIC_CONST_INIT_BUG)
00396   ;
00397   static const bool
00398 # else
00399   ,
00400 # endif
00401   is_specialized = true,
00402   is_signed = false,
00403   is_integer = true,
00404   is_exact = true,
00405   is_bounded = true,
00406   is_modulo = true
00407 # if defined ( _STLP_STATIC_CONST_INIT_BUG)
00408   }
00409 # endif
00410   ;
00411 };
00412 
00413 # endif /* __GNUC__ > 2000-11-01 */
00414 
00415 #endif /* _STLP_LONG_LONG */
00416 
00417 // Specializations for all built-in floating-point types.
00418 
00419 union _F_rep
00420 {
00421   unsigned short rep[2];
00422   float val;
00423 };
00424 union _D_rep
00425 {
00426   unsigned short rep[4];
00427   double val;
00428 };
00429 
00430 # ifndef _STLP_NO_LONG_DOUBLE
00431 union _L_rep
00432 {
00433   unsigned short rep[8];
00434   long double val;
00435 };
00436 # endif
00437 
00438 template <class __dummy>
00439 class _LimG 
00440 {
00441 public:  
00442   static const _F_rep _F_inf;
00443   static const _F_rep _F_qNaN;
00444   static const _F_rep _F_sNaN;
00445   static const _D_rep _D_inf;
00446   static const _D_rep _D_qNaN;
00447   static const _D_rep _D_sNaN;
00448   
00449 # ifndef _STLP_NO_LONG_DOUBLE
00450   static const _L_rep _L_inf;
00451   static const _L_rep _L_qNaN;
00452   static const _L_rep _L_sNaN;
00453 # endif
00454 };
00455 
00456 # if defined (_STLP_USE_TEMPLATE_EXPORT) 
00457 _STLP_EXPORT_TEMPLATE_CLASS _LimG<bool>;
00458 # endif
00459 
00460 _STLP_TEMPLATE_NULL class   numeric_limits<float>
00461   : public _Floating_limits<float, 
00462                             FLT_MANT_DIG,   // Binary digits of precision
00463                             FLT_DIG,        // Decimal digits of precision
00464                             FLT_MIN_EXP,    // Minimum exponent
00465                             FLT_MAX_EXP,    // Maximum exponent
00466                             FLT_MIN_10_EXP, // Minimum base 10 exponent
00467                             FLT_MAX_10_EXP, // Maximum base 10 exponent
00468                             true,           // conforms to iec559
00469                             round_to_nearest>
00470 {
00471 public:
00472   static float (_STLP_CALL min) () _STLP_NOTHROW { return FLT_MIN; }
00473   static float _STLP_CALL denorm_min() _STLP_NOTHROW { return FLT_MIN; }
00474   static float (_STLP_CALL max) () _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return FLT_MAX; }
00475   static float _STLP_CALL epsilon() _STLP_NOTHROW { return FLT_EPSILON; }
00476   static float _STLP_CALL round_error() _STLP_NOTHROW { return 0.5f; } // Units: ulps.
00477   static  float _STLP_CALL infinity() { return _LimG<bool>::_F_inf.val; }
00478   static  float _STLP_CALL quiet_NaN() { return _LimG<bool>::_F_qNaN.val; }
00479   static  float _STLP_CALL signaling_NaN() { return _LimG<bool>::_F_sNaN.val; }
00480 };
00481 
00482 _STLP_TEMPLATE_NULL class   numeric_limits<double>
00483   : public _Floating_limits<double, 
00484                             DBL_MANT_DIG,   // Binary digits of precision
00485                             DBL_DIG,        // Decimal digits of precision
00486                             DBL_MIN_EXP,    // Minimum exponent
00487                             DBL_MAX_EXP,    // Maximum exponent
00488                             DBL_MIN_10_EXP, // Minimum base 10 exponent
00489                             DBL_MAX_10_EXP, // Maximum base 10 exponent
00490                             true,           // conforms to iec559
00491                             round_to_nearest>
00492 {
00493 public:
00494   static double (_STLP_CALL min)() _STLP_NOTHROW { return DBL_MIN; }
00495   static double _STLP_CALL denorm_min() _STLP_NOTHROW { return DBL_MIN; }
00496   static double (_STLP_CALL max)() _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return DBL_MAX; }
00497   static double _STLP_CALL epsilon() _STLP_NOTHROW { return DBL_EPSILON; }
00498   static double _STLP_CALL round_error() _STLP_NOTHROW { return 0.5; } // Units: ulps.
00499   static  double _STLP_CALL infinity() { return _LimG<bool>::_D_inf.val; }
00500   static  double _STLP_CALL quiet_NaN(){ return _LimG<bool>::_D_qNaN.val; }
00501   static  double _STLP_CALL signaling_NaN() { return _LimG<bool>::_D_sNaN.val; }
00502 };
00503 
00504 # ifndef _STLP_NO_LONG_DOUBLE
00505 
00506 _STLP_TEMPLATE_NULL 
00507 class   numeric_limits<long double>
00508   : public _Floating_limits<long double, 
00509                             LDBL_MANT_DIG,  // Binary digits of precision
00510                             LDBL_DIG,       // Decimal digits of precision
00511                             LDBL_MIN_EXP,   // Minimum exponent
00512                             LDBL_MAX_EXP,   // Maximum exponent
00513                             LDBL_MIN_10_EXP,// Minimum base 10 exponent
00514                             LDBL_MAX_10_EXP,// Maximum base 10 exponent
00515                             false,          // Doesn't conform to iec559
00516                             round_to_nearest>
00517 {
00518 public:
00519   static long double (_STLP_CALL min) () _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return LDBL_MIN; }
00520   static long double _STLP_CALL denorm_min() _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return LDBL_MIN; }
00521   static long double (_STLP_CALL max) () _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return LDBL_MAX; }
00522   static long double _STLP_CALL epsilon() _STLP_NOTHROW { return LDBL_EPSILON; }
00523   static long double _STLP_CALL round_error() _STLP_NOTHROW { return 4; } // Units: ulps.
00524   static long double _STLP_CALL infinity() { return _LimG<bool>::_L_inf.val; } 
00525   static long double _STLP_CALL quiet_NaN() { return _LimG<bool>::_L_qNaN.val; }
00526   static long double _STLP_CALL signaling_NaN() { return _LimG<bool>::_L_sNaN.val; }
00527 };
00528 
00529 # endif
00530 
00531 // We write special values (Inf and NaN) as bit patterns and 
00532 // cast the the appropriate floating-point types. 
00533 _STLP_END_NAMESPACE
00534 
00535 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
00536 #  include <stl/_limits.c>
00537 # endif
00538 
00539 #endif
00540 
00541 // Local Variables:
00542 // mode:C++
00543 // End:

Generated on Mon Jun 5 10:20:46 2006 for Intelligence.kdevelop by  doxygen 1.4.6