src/sysc/packages/boost/detail/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 
00046 #ifndef BOOST_NO_CWCHAR
00047 #include <cwchar> // for WCHAR_MIN and WCHAR_MAX
00048 #endif
00049 
00050 // The macros are not named appropriately.  We don't care about integer
00051 // bit layout, but about floating-point NaN (etc.) bit patterns.
00052 #if defined(__sparc) || defined(__sparc__) || defined(__powerpc__) || defined(__ppc__) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER)
00053 #define BOOST_BIG_ENDIAN
00054 #elif defined(__i386__) || defined(__alpha__)
00055 #define BOOST_LITTLE_ENDIAN
00056 #else
00057 #error The file boost/detail/limits.hpp needs to be set up for your CPU type.
00058 #endif
00059 
00060 namespace std {
00061 
00062 enum float_round_style {
00063   round_indeterminate       = -1,
00064   round_toward_zero         =  0,
00065   round_to_nearest          =  1,
00066   round_toward_infinity     =  2,
00067   round_toward_neg_infinity =  3
00068 };
00069 
00070 enum float_denorm_style {
00071   denorm_indeterminate = -1,
00072   denorm_absent        =  0,
00073   denorm_present       =  1
00074 };
00075 
00076 // The C++ standard (section 18.2.1) requires that some of the members of
00077 // numeric_limits be static const data members that are given constant-
00078 // initializers within the class declaration.  On compilers where the
00079 // BOOST_NO_INCLASS_MEMBER_INITIALIZATION macro is defined, it is impossible to write
00080 // a standard-conforming numeric_limits class.
00081 //
00082 // There are two possible workarounds: either initialize the data
00083 // members outside the class, or change them from data members to
00084 // enums.  Neither workaround is satisfactory: the former makes it
00085 // impossible to use the data members in constant-expressions, and the
00086 // latter means they have the wrong type and that it is impossible to
00087 // take their addresses.  We choose the former workaround.
00088 
00089 #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
00090 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
00091   enum { __mem_name = __mem_value }
00092 #else /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
00093 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
00094   static const __mem_type __mem_name = __mem_value
00095 #endif /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
00096 
00097 // Deal with min/max for MinGW
00098 #ifdef min
00099 # undef min
00100 #endif
00101 
00102 #ifdef max
00103 # undef max
00104 #endif
00105 
00106 // Base class for all specializations of numeric_limits.
00107 template <class __number>
00108 class _Numeric_limits_base {
00109 public:
00110   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
00111 
00112   static __number min() throw() { return __number(); }
00113   static __number max() throw() { return __number(); }
00114 
00115   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   0);
00116   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
00117 
00118   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  false);
00119   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
00120   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   false);
00121 
00122   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
00123 
00124   static __number epsilon() throw()     { return __number(); }
00125   static __number round_error() throw() { return __number(); }
00126 
00127   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   0);
00128   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
00129   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   0);
00130   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
00131 
00132   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      false);
00133   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN,     false);
00134   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
00135   BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
00136                               has_denorm,
00137                               denorm_absent);
00138   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
00139 
00140   static __number infinity() throw()      { return __number(); }
00141   static __number quiet_NaN() throw()     { return __number(); }
00142   static __number signaling_NaN() throw() { return __number(); }
00143   static __number denorm_min() throw()    { return __number(); }
00144 
00145   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559,  false);
00146   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
00147   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo,  false);
00148 
00149   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps,            false);
00150   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before,  false);
00151   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
00152                               round_style,
00153                               round_toward_zero);
00154 };
00155 
00156 // Base class for integers.
00157 
00158 template <class _Int,
00159           _Int __imin,
00160           _Int __imax,
00161           int __idigits = -1>
00162 class _Integer_limits : public _Numeric_limits_base<_Int> 
00163 {
00164 public:
00165   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
00166 
00167   static _Int min() throw() { return __imin; }
00168   static _Int max() throw() { return __imax; }
00169 
00170   BOOST_STL_DECLARE_LIMITS_MEMBER(int,
00171                               digits,
00172                               (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
00173                                                    - (__imin == 0 ? 0 : 1) 
00174                                               : __idigits);
00175   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000); 
00176                                 // log 2 = 0.301029995664...
00177 
00178   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  __imin != 0);
00179   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
00180   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   true);
00181   BOOST_STL_DECLARE_LIMITS_MEMBER(int,  radix,      2);
00182 
00183   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
00184   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
00185 };
00186 
00187 #if defined(BOOST_BIG_ENDIAN)
00188 
00189  template<class Number, unsigned int Word>
00190  struct float_helper{
00191   static Number get_word() throw() {
00192     // sizeof(long double) == 16
00193     const unsigned int _S_word[4] = { Word, 0, 0, 0 };
00194     return *reinterpret_cast<const Number*>(&_S_word);
00195   } 
00196 };
00197 
00198 #else
00199 
00200  template<class Number, unsigned int Word>
00201  struct float_helper{
00202   static Number get_word() throw() {
00203     // sizeof(long double) == 12, but only 10 bytes significant
00204     const unsigned int _S_word[4] = { 0, 0, 0, Word };
00205     return *reinterpret_cast<const Number*>(
00206         reinterpret_cast<const char *>(&_S_word)+16-
00207                 (sizeof(Number) == 12 ? 10 : sizeof(Number)));
00208   } 
00209 };
00210 
00211 #endif
00212 
00213 // Base class for floating-point numbers.
00214 template <class __number,
00215          int __Digits, int __Digits10,
00216          int __MinExp, int __MaxExp,
00217          int __MinExp10, int __MaxExp10,
00218          unsigned int __InfinityWord,
00219          unsigned int __QNaNWord, unsigned int __SNaNWord,
00220          bool __IsIEC559,
00221          float_round_style __RoundStyle>
00222 class _Floating_limits : public _Numeric_limits_base<__number>
00223 {
00224 public:
00225   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
00226 
00227   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   __Digits);
00228   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
00229 
00230   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
00231 
00232   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
00233 
00234   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   __MinExp);
00235   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   __MaxExp);
00236   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
00237   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
00238 
00239   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      true);
00240   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN,     true);
00241   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
00242   BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
00243                               has_denorm,
00244                               denorm_indeterminate);
00245   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
00246 
00247  
00248   static __number infinity() throw() {
00249     return float_helper<__number, __InfinityWord>::get_word();
00250   }
00251   static __number quiet_NaN() throw() {
00252     return float_helper<__number,__QNaNWord>::get_word();
00253   }
00254   static __number signaling_NaN() throw() {
00255     return float_helper<__number,__SNaNWord>::get_word();
00256   }
00257 
00258   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559,       __IsIEC559);
00259   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded,      true);
00260   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps,           false /* was: true */ );
00261   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
00262 
00263   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
00264 };
00265 
00266 // Class numeric_limits
00267 
00268 // The unspecialized class.
00269 
00270 template<class T> 
00271 class numeric_limits : public _Numeric_limits_base<T> {};
00272 
00273 // Specializations for all built-in integral types.
00274 
00275 template<>
00276 class numeric_limits<bool>
00277   : public _Integer_limits<bool, false, true, 0>
00278 {};
00279 
00280 template<>
00281 class numeric_limits<char>
00282   : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
00283 {};
00284 
00285 template<>
00286 class numeric_limits<signed char>
00287   : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
00288 {};
00289 
00290 template<>
00291 class numeric_limits<unsigned char>
00292   : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
00293 {};
00294 
00295 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
00296 template<>
00297 class numeric_limits<wchar_t>
00298 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
00299 #if defined(_WIN32) || defined(__CYGWIN__)
00300   : public _Integer_limits<wchar_t, 0, USHRT_MAX>
00301 #elif defined(__hppa)
00302 // wchar_t has "unsigned int" as the underlying type
00303   : public _Integer_limits<wchar_t, 0, UINT_MAX>
00304 #else
00305 // assume that wchar_t has "int" as the underlying type
00306   : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
00307 #endif
00308 #else
00309 // we have WCHAR_MIN and WCHAR_MAX defined, so use it
00310   : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
00311 #endif
00312 {};
00313 #endif
00314 
00315 template<>
00316 class numeric_limits<short>
00317   : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
00318 {};
00319 
00320 template<>
00321 class numeric_limits<unsigned short>
00322   : public _Integer_limits<unsigned short, 0, USHRT_MAX>
00323 {};
00324 
00325 template<>
00326 class numeric_limits<int>
00327   : public _Integer_limits<int, INT_MIN, INT_MAX>
00328 {};
00329 
00330 template<>
00331 class numeric_limits<unsigned int>
00332   : public _Integer_limits<unsigned int, 0, UINT_MAX>
00333 {};
00334 
00335 template<>
00336 class numeric_limits<long>
00337   : public _Integer_limits<long, LONG_MIN, LONG_MAX>
00338 {};
00339 
00340 template<>
00341 class numeric_limits<unsigned long>
00342   : public _Integer_limits<unsigned long, 0, ULONG_MAX>
00343 {};
00344 
00345 #ifdef __GNUC__
00346 
00347 // Some compilers have long long, but don't define the
00348 // LONGLONG_MIN and LONGLONG_MAX macros in limits.h.  This
00349 // assumes that long long is 64 bits.
00350 #if !defined(LONGLONG_MAX) && !defined(ULONGLONG_MAX)
00351 
00352 # define ULONGLONG_MAX 0xffffffffffffffffLLU
00353 # define LONGLONG_MAX 0x7fffffffffffffffLL
00354 
00355 #endif
00356 
00357 #if !defined(LONGLONG_MIN)
00358 # define LONGLONG_MIN (-LONGLONG_MAX - 1)
00359 #endif 
00360 
00361 
00362 #if !defined(ULONGLONG_MIN)
00363 # define ULONGLONG_MIN 0
00364 #endif 
00365 
00366 #endif /* __GNUC__ */
00367 
00368 // Specializations for all built-in floating-point type.
00369 
00370 template<> class numeric_limits<float>
00371   : public _Floating_limits<float, 
00372                             FLT_MANT_DIG,   // Binary digits of precision
00373                             FLT_DIG,        // Decimal digits of precision
00374                             FLT_MIN_EXP,    // Minimum exponent
00375                             FLT_MAX_EXP,    // Maximum exponent
00376                             FLT_MIN_10_EXP, // Minimum base 10 exponent
00377                             FLT_MAX_10_EXP, // Maximum base 10 exponent
00378 #if defined(BOOST_BIG_ENDIAN)
00379                             0x7f80 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
00380                             0x7f81 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
00381                             0x7fc1 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
00382 #else
00383                             0x7f800000u,    // Last word of +infinity
00384                             0x7f810000u,    // Last word of quiet NaN
00385                             0x7fc10000u,    // Last word of signaling NaN
00386 #endif
00387                             true,           // conforms to iec559
00388                             round_to_nearest>
00389 {
00390 public:
00391   static float min() throw() { return FLT_MIN; }
00392   static float denorm_min() throw() { return FLT_MIN; }
00393   static float max() throw() { return FLT_MAX; }
00394   static float epsilon() throw() { return FLT_EPSILON; }
00395   static float round_error() throw() { return 0.5f; } // Units: ulps.
00396 };
00397 
00398 template<> class numeric_limits<double>
00399   : public _Floating_limits<double, 
00400                             DBL_MANT_DIG,   // Binary digits of precision
00401                             DBL_DIG,        // Decimal digits of precision
00402                             DBL_MIN_EXP,    // Minimum exponent
00403                             DBL_MAX_EXP,    // Maximum exponent
00404                             DBL_MIN_10_EXP, // Minimum base 10 exponent
00405                             DBL_MAX_10_EXP, // Maximum base 10 exponent
00406 #if defined(BOOST_BIG_ENDIAN)
00407                             0x7ff0 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
00408                             0x7ff1 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
00409                             0x7ff9 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
00410 #else
00411                             0x7ff00000u,    // Last word of +infinity
00412                             0x7ff10000u,    // Last word of quiet NaN
00413                             0x7ff90000u,    // Last word of signaling NaN
00414 #endif
00415                             true,           // conforms to iec559
00416                             round_to_nearest>
00417 {
00418 public:
00419   static double min() throw() { return DBL_MIN; }
00420   static double denorm_min() throw() { return DBL_MIN; }
00421   static double max() throw() { return DBL_MAX; }
00422   static double epsilon() throw() { return DBL_EPSILON; }
00423   static double round_error() throw() { return 0.5; } // Units: ulps.
00424 };
00425 
00426 template<> class numeric_limits<long double>
00427   : public _Floating_limits<long double, 
00428                             LDBL_MANT_DIG,  // Binary digits of precision
00429                             LDBL_DIG,       // Decimal digits of precision
00430                             LDBL_MIN_EXP,   // Minimum exponent
00431                             LDBL_MAX_EXP,   // Maximum exponent
00432                             LDBL_MIN_10_EXP,// Minimum base 10 exponent
00433                             LDBL_MAX_10_EXP,// Maximum base 10 exponent
00434 #if defined(BOOST_BIG_ENDIAN)
00435                             0x7ff0 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
00436                             0x7ff1 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
00437                             0x7ff9 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
00438 #else
00439                             0x7fff8000u,    // Last word of +infinity
00440                             0x7fffc000u,    // Last word of quiet NaN
00441                             0x7fff9000u,    // Last word of signaling NaN
00442 #endif
00443                             false,          // Doesn't conform to iec559
00444                             round_to_nearest>
00445 {
00446 public:
00447   static long double min() throw() { return LDBL_MIN; }
00448   static long double denorm_min() throw() { return LDBL_MIN; }
00449   static long double max() throw() { return LDBL_MAX; }
00450   static long double epsilon() throw() { return LDBL_EPSILON; }
00451   static long double round_error() throw() { return 4; } // Units: ulps.
00452 };
00453 
00454 } // namespace std
00455 
00456 #endif /* BOOST_SGI_CPP_LIMITS */
00457 
00458 // Local Variables:
00459 // mode:C++
00460 // End:
00461 
00462 
00463 

Generated on Wed Apr 25 13:53:28 2007 for SystemC by  doxygen 1.5.1