limits.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1997
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Permission to use, copy, modify, distribute and sell this software
00006  * and its documentation for any purpose is hereby granted without fee,
00007  * provided that the above copyright notice appear in all copies and
00008  * that both that copyright notice and this permission notice appear
00009  * in supporting documentation.  Silicon Graphics makes no
00010  * representations about the suitability of this software for any
00011  * purpose.  It is provided "as is" without express or implied warranty.
00012  */
00013 
00014 /* NOTE: This is not portable code.  Parts of numeric_limits<> are
00015  * inherently machine-dependent, and this file is written for the MIPS
00016  * architecture and the SGI MIPSpro C++ compiler.  Parts of it (in
00017  * particular, some of the characteristics of floating-point types)
00018  * are almost certainly incorrect for any other platform.
00019  */
00020 
00021 /* The above comment is almost certainly out of date. This file works
00022  * on systems other than SGI MIPSpro C++ now.
00023  */
00024 
00025 /*
00026  * Revision history:
00027  * 21 Sep 2001:
00028  *    Only include <cwchar> if BOOST_NO_CWCHAR is defined. (Darin Adler)
00029  * 10 Aug 2001:
00030  *    Added MIPS (big endian) to the big endian family. (Jens Maurer)
00031  * 13 Apr 2001:
00032  *    Added powerpc to the big endian family. (Jeremy Siek)
00033  * 5 Apr 2001:
00034  *    Added sparc (big endian) processor support (John Maddock).
00035  * Initial sub:
00036  *      Modified by Jens Maurer for gcc 2.95 on x86.
00037  */
00038 
00039 #ifndef BOOST_SGI_CPP_LIMITS
00040 #define BOOST_SGI_CPP_LIMITS
00041 
00042 #include <climits>
00043 #include <cfloat>
00044 #include <sysc/packages/boost/config.hpp>
00045 #include <sysc/packages/boost/detail/endian.hpp>
00046 
00047 #ifndef BOOST_NO_CWCHAR
00048 #include <cwchar> // for WCHAR_MIN and WCHAR_MAX
00049 #endif
00050 
00051 namespace std {
00052 
00053 enum float_round_style {
00054   round_indeterminate       = -1,
00055   round_toward_zero         =  0,
00056   round_to_nearest          =  1,
00057   round_toward_infinity     =  2,
00058   round_toward_neg_infinity =  3
00059 };
00060 
00061 enum float_denorm_style {
00062   denorm_indeterminate = -1,
00063   denorm_absent        =  0,
00064   denorm_present       =  1
00065 };
00066 
00067 // The C++ standard (section 18.2.1) requires that some of the members of
00068 // numeric_limits be static const data members that are given constant-
00069 // initializers within the class declaration.  On compilers where the
00070 // BOOST_NO_INCLASS_MEMBER_INITIALIZATION macro is defined, it is impossible to write
00071 // a standard-conforming numeric_limits class.
00072 //
00073 // There are two possible workarounds: either initialize the data
00074 // members outside the class, or change them from data members to
00075 // enums.  Neither workaround is satisfactory: the former makes it
00076 // impossible to use the data members in constant-expressions, and the
00077 // latter means they have the wrong type and that it is impossible to
00078 // take their addresses.  We choose the former workaround.
00079 
00080 #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
00081 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
00082   enum { __mem_name = __mem_value }
00083 #else /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
00084 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
00085   static const __mem_type __mem_name = __mem_value
00086 #endif /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
00087 
00088 // Base class for all specializations of numeric_limits.
00089 template <class __number>
00090 class _Numeric_limits_base {
00091 public:
00092   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
00093 
00094   static __number min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
00095   static __number max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
00096 
00097   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   0);
00098   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
00099 
00100   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  false);
00101   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
00102   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   false);
00103 
00104   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
00105 
00106   static __number epsilon() throw()     { return __number(); }
00107   static __number round_error() throw() { return __number(); }
00108 
00109   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   0);
00110   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
00111   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   0);
00112   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
00113 
00114   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      false);
00115   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN,     false);
00116   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
00117   BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
00118                               has_denorm,
00119                               denorm_absent);
00120   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
00121 
00122   static __number infinity() throw()      { return __number(); }
00123   static __number quiet_NaN() throw()     { return __number(); }
00124   static __number signaling_NaN() throw() { return __number(); }
00125   static __number denorm_min() throw()    { return __number(); }
00126 
00127   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559,  false);
00128   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
00129   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo,  false);
00130 
00131   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps,            false);
00132   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before,  false);
00133   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
00134                               round_style,
00135                               round_toward_zero);
00136 };
00137 
00138 // Base class for integers.
00139 
00140 template <class _Int,
00141           _Int __imin,
00142           _Int __imax,
00143           int __idigits = -1>
00144 class _Integer_limits : public _Numeric_limits_base<_Int> 
00145 {
00146 public:
00147   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
00148 
00149   static _Int min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imin; }
00150   static _Int max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imax; }
00151 
00152   BOOST_STL_DECLARE_LIMITS_MEMBER(int,
00153                               digits,
00154                               (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
00155                                                    - (__imin == 0 ? 0 : 1) 
00156                                               : __idigits);
00157   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000); 
00158                                 // log 2 = 0.301029995664...
00159 
00160   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  __imin != 0);
00161   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
00162   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   true);
00163   BOOST_STL_DECLARE_LIMITS_MEMBER(int,  radix,      2);
00164 
00165   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
00166   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
00167 };
00168 
00169 #if defined(BOOST_BIG_ENDIAN)
00170 
00171  template<class Number, unsigned int Word>
00172  struct float_helper{
00173   static Number get_word() throw() {
00174     // sizeof(long double) == 16
00175     const unsigned int _S_word[4] = { Word, 0, 0, 0 };
00176     return *reinterpret_cast<const Number*>(&_S_word);
00177   } 
00178 };
00179 
00180 #else
00181 
00182  template<class Number, unsigned int Word>
00183  struct float_helper{
00184   static Number get_word() throw() {
00185     // sizeof(long double) == 12, but only 10 bytes significant
00186     const unsigned int _S_word[4] = { 0, 0, 0, Word };
00187     return *reinterpret_cast<const Number*>(
00188         reinterpret_cast<const char *>(&_S_word)+16-
00189                 (sizeof(Number) == 12 ? 10 : sizeof(Number)));
00190   } 
00191 };
00192 
00193 #endif
00194 
00195 // Base class for floating-point numbers.
00196 template <class __number,
00197          int __Digits, int __Digits10,
00198          int __MinExp, int __MaxExp,
00199          int __MinExp10, int __MaxExp10,
00200          unsigned int __InfinityWord,
00201          unsigned int __QNaNWord, unsigned int __SNaNWord,
00202          bool __IsIEC559,
00203          float_round_style __RoundStyle>
00204 class _Floating_limits : public _Numeric_limits_base<__number>
00205 {
00206 public:
00207   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
00208 
00209   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   __Digits);
00210   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
00211 
00212   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
00213 
00214   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
00215 
00216   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   __MinExp);
00217   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   __MaxExp);
00218   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
00219   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
00220 
00221   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      true);
00222   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN,     true);
00223   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
00224   BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
00225                               has_denorm,
00226                               denorm_indeterminate);
00227   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
00228 
00229  
00230   static __number infinity() throw() {
00231     return float_helper<__number, __InfinityWord>::get_word();
00232   }
00233   static __number quiet_NaN() throw() {
00234     return float_helper<__number,__QNaNWord>::get_word();
00235   }
00236   static __number signaling_NaN() throw() {
00237     return float_helper<__number,__SNaNWord>::get_word();
00238   }
00239 
00240   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559,       __IsIEC559);
00241   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded,      true);
00242   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps,           false /* was: true */ );
00243   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
00244 
00245   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
00246 };
00247 
00248 // Class numeric_limits
00249 
00250 // The unspecialized class.
00251 
00252 template<class T> 
00253 class numeric_limits : public _Numeric_limits_base<T> {};
00254 
00255 // Specializations for all built-in integral types.
00256 
00257 template<>
00258 class numeric_limits<bool>
00259   : public _Integer_limits<bool, false, true, 0>
00260 {};
00261 
00262 template<>
00263 class numeric_limits<char>
00264   : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
00265 {};
00266 
00267 template<>
00268 class numeric_limits<signed char>
00269   : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
00270 {};
00271 
00272 template<>
00273 class numeric_limits<unsigned char>
00274   : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
00275 {};
00276 
00277 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
00278 template<>
00279 class numeric_limits<wchar_t>
00280 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
00281 #if defined(_WIN32) || defined(__CYGWIN__)
00282   : public _Integer_limits<wchar_t, 0, USHRT_MAX>
00283 #elif defined(__hppa)
00284 // wchar_t has "unsigned int" as the underlying type
00285   : public _Integer_limits<wchar_t, 0, UINT_MAX>
00286 #else
00287 // assume that wchar_t has "int" as the underlying type
00288   : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
00289 #endif
00290 #else
00291 // we have WCHAR_MIN and WCHAR_MAX defined, so use it
00292   : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
00293 #endif
00294 {};
00295 #endif
00296 
00297 template<>
00298 class numeric_limits<short>
00299   : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
00300 {};
00301 
00302 template<>
00303 class numeric_limits<unsigned short>
00304   : public _Integer_limits<unsigned short, 0, USHRT_MAX>
00305 {};
00306 
00307 template<>
00308 class numeric_limits<int>
00309   : public _Integer_limits<int, INT_MIN, INT_MAX>
00310 {};
00311 
00312 template<>
00313 class numeric_limits<unsigned int>
00314   : public _Integer_limits<unsigned int, 0, UINT_MAX>
00315 {};
00316 
00317 template<>
00318 class numeric_limits<long>
00319   : public _Integer_limits<long, LONG_MIN, LONG_MAX>
00320 {};
00321 
00322 template<>
00323 class numeric_limits<unsigned long>
00324   : public _Integer_limits<unsigned long, 0, ULONG_MAX>
00325 {};
00326 
00327 #ifdef __GNUC__
00328 
00329 // Some compilers have long long, but don't define the
00330 // LONGLONG_MIN and LONGLONG_MAX macros in limits.h.  This
00331 // assumes that long long is 64 bits.
00332 #if !defined(LONGLONG_MAX) && !defined(ULONGLONG_MAX)
00333 
00334 # define ULONGLONG_MAX 0xffffffffffffffffLLU
00335 # define LONGLONG_MAX 0x7fffffffffffffffLL
00336 
00337 #endif
00338 
00339 #if !defined(LONGLONG_MIN)
00340 # define LONGLONG_MIN (-LONGLONG_MAX - 1)
00341 #endif 
00342 
00343 
00344 #if !defined(ULONGLONG_MIN)
00345 # define ULONGLONG_MIN 0
00346 #endif 
00347 
00348 #endif /* __GNUC__ */
00349 
00350 // Specializations for all built-in floating-point type.
00351 
00352 template<> class numeric_limits<float>
00353   : public _Floating_limits<float, 
00354                             FLT_MANT_DIG,   // Binary digits of precision
00355                             FLT_DIG,        // Decimal digits of precision
00356                             FLT_MIN_EXP,    // Minimum exponent
00357                             FLT_MAX_EXP,    // Maximum exponent
00358                             FLT_MIN_10_EXP, // Minimum base 10 exponent
00359                             FLT_MAX_10_EXP, // Maximum base 10 exponent
00360 #if defined(BOOST_BIG_ENDIAN)
00361                             0x7f80 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
00362                             0x7f81 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
00363                             0x7fc1 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
00364 #else
00365                             0x7f800000u,    // Last word of +infinity
00366                             0x7f810000u,    // Last word of quiet NaN
00367                             0x7fc10000u,    // Last word of signaling NaN
00368 #endif
00369                             true,           // conforms to iec559
00370                             round_to_nearest>
00371 {
00372 public:
00373   static float min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return FLT_MIN; }
00374   static float denorm_min() throw() { return FLT_MIN; }
00375   static float max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return FLT_MAX; }
00376   static float epsilon() throw() { return FLT_EPSILON; }
00377   static float round_error() throw() { return 0.5f; } // Units: ulps.
00378 };
00379 
00380 template<> class numeric_limits<double>
00381   : public _Floating_limits<double, 
00382                             DBL_MANT_DIG,   // Binary digits of precision
00383                             DBL_DIG,        // Decimal digits of precision
00384                             DBL_MIN_EXP,    // Minimum exponent
00385                             DBL_MAX_EXP,    // Maximum exponent
00386                             DBL_MIN_10_EXP, // Minimum base 10 exponent
00387                             DBL_MAX_10_EXP, // Maximum base 10 exponent
00388 #if defined(BOOST_BIG_ENDIAN)
00389                             0x7ff0 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
00390                             0x7ff1 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
00391                             0x7ff9 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
00392 #else
00393                             0x7ff00000u,    // Last word of +infinity
00394                             0x7ff10000u,    // Last word of quiet NaN
00395                             0x7ff90000u,    // Last word of signaling NaN
00396 #endif
00397                             true,           // conforms to iec559
00398                             round_to_nearest>
00399 {
00400 public:
00401   static double min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return DBL_MIN; }
00402   static double denorm_min() throw() { return DBL_MIN; }
00403   static double max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return DBL_MAX; }
00404   static double epsilon() throw() { return DBL_EPSILON; }
00405   static double round_error() throw() { return 0.5; } // Units: ulps.
00406 };
00407 
00408 template<> class numeric_limits<long double>
00409   : public _Floating_limits<long double, 
00410                             LDBL_MANT_DIG,  // Binary digits of precision
00411                             LDBL_DIG,       // Decimal digits of precision
00412                             LDBL_MIN_EXP,   // Minimum exponent
00413                             LDBL_MAX_EXP,   // Maximum exponent
00414                             LDBL_MIN_10_EXP,// Minimum base 10 exponent
00415                             LDBL_MAX_10_EXP,// Maximum base 10 exponent
00416 #if defined(BOOST_BIG_ENDIAN)
00417                             0x7ff0 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
00418                             0x7ff1 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
00419                             0x7ff9 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
00420 #else
00421                             0x7fff8000u,    // Last word of +infinity
00422                             0x7fffc000u,    // Last word of quiet NaN
00423                             0x7fff9000u,    // Last word of signaling NaN
00424 #endif
00425                             false,          // Doesn't conform to iec559
00426                             round_to_nearest>
00427 {
00428 public:
00429   static long double min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return LDBL_MIN; }
00430   static long double denorm_min() throw() { return LDBL_MIN; }
00431   static long double max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return LDBL_MAX; }
00432   static long double epsilon() throw() { return LDBL_EPSILON; }
00433   static long double round_error() throw() { return 4; } // Units: ulps.
00434 };
00435 
00436 } // namespace std
00437 
00438 #endif /* BOOST_SGI_CPP_LIMITS */
00439 
00440 // Local Variables:
00441 // mode:C++
00442 // End:
00443 
00444 
00445 

Generated on Wed Jan 21 15:32:09 2009 for SystemC by  doxygen 1.5.5