3 * Silicon Graphics Computer Systems, Inc.
5 * Permission to use, copy, modify, distribute and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation. Silicon Graphics makes no
10 * representations about the suitability of this software for any
11 * purpose. It is provided "as is" without express or implied warranty.
14 /* NOTE: This is not portable code. Parts of numeric_limits<> are
15 * inherently machine-dependent, and this file is written for the MIPS
16 * architecture and the SGI MIPSpro C++ compiler. Parts of it (in
17 * particular, some of the characteristics of floating-point types)
18 * are almost certainly incorrect for any other platform.
24 * Added MIPS (big endian) to the big endian family. (Jens Maurer)
26 * Added powerpc to the big endian family. (Jeremy Siek)
28 * Added sparc (big endian) processor support (John Maddock).
30 * Modified by Jens Maurer for gcc 2.95 on x86.
33 #ifndef BOOST_SGI_CPP_LIMITS
34 #define BOOST_SGI_CPP_LIMITS
38 #include <cwchar> // for WCHAR_MIN and WCHAR_MAX
39 #include <boost/config.hpp>
41 #if defined(__sparc) || defined(__sparc__) || defined(__powerpc__) || defined(__ppc__) || defined(__hppa) || defined(_MIPSEB)
42 #define BOOST_BIG_ENDIAN
43 #elif defined(__i386__)
44 #define BOOST_LITTLE_ENDIAN
46 #error The file boost/detail/limits.hpp needs to be set up for your CPU type.
51 enum float_round_style {
52 round_indeterminate = -1,
53 round_toward_zero = 0,
55 round_toward_infinity = 2,
56 round_toward_neg_infinity = 3
59 enum float_denorm_style {
60 denorm_indeterminate = -1,
65 // The C++ standard (section 18.2.1) requires that some of the members of
66 // numeric_limits be static const data members that are given constant-
67 // initializers within the class declaration. On compilers where the
68 // BOOST_NO_INCLASS_MEMBER_INITIALIZATION macro is defined, it is impossible to write
69 // a standard-conforming numeric_limits class.
71 // There are two possible workarounds: either initialize the data
72 // members outside the class, or change them from data members to
73 // enums. Neither workaround is satisfactory: the former makes it
74 // impossible to use the data members in constant-expressions, and the
75 // latter means they have the wrong type and that it is impossible to
76 // take their addresses. We choose the former workaround.
78 #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
79 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
80 enum { __mem_name = __mem_value }
81 #else /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
82 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
83 static const __mem_type __mem_name = __mem_value
84 #endif /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
86 // Base class for all specializations of numeric_limits.
88 template <class __number>
89 class _Numeric_limits_base {
91 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
93 static __number min() throw() { return __number(); }
94 static __number max() throw() { return __number(); }
96 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, 0);
97 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
99 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, false);
100 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
101 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, false);
103 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
105 static __number epsilon() throw() { return __number(); }
106 static __number round_error() throw() { return __number(); }
108 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, 0);
109 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
110 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, 0);
111 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
113 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, false);
114 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, false);
115 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
116 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
119 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
121 static __number infinity() throw() { return __number(); }
122 static __number quiet_NaN() throw() { return __number(); }
123 static __number signaling_NaN() throw() { return __number(); }
124 static __number denorm_min() throw() { return __number(); }
126 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, false);
127 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
128 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, false);
130 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false);
131 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
132 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
137 // Base class for integers.
139 template <class _Int,
143 class _Integer_limits : public _Numeric_limits_base<_Int>
146 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
148 static _Int min() throw() { return __imin; }
149 static _Int max() throw() { return __imax; }
151 BOOST_STL_DECLARE_LIMITS_MEMBER(int,
153 (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
154 - (__imin == 0 ? 0 : 1)
156 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000);
157 // log 2 = 0.301029995664...
159 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, __imin != 0);
160 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
161 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, true);
162 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
164 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
165 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
168 #if defined(BOOST_BIG_ENDIAN)
170 template<class Number, unsigned int Word>
172 static Number get_word() throw() {
173 // sizeof(long double) == 16
174 const unsigned int _S_word[4] = { Word, 0, 0, 0 };
175 return *reinterpret_cast<const Number*>(&_S_word);
181 template<class Number, unsigned int Word>
183 static Number get_word() throw() {
184 // sizeof(long double) == 12, but only 10 bytes significant
185 const unsigned int _S_word[4] = { 0, 0, 0, Word };
186 return *reinterpret_cast<const Number*>(
187 reinterpret_cast<const char *>(&_S_word)+16-
188 (sizeof(Number) == 12 ? 10 : sizeof(Number)));
194 // Base class for floating-point numbers.
195 template <class __number,
196 int __Digits, int __Digits10,
197 int __MinExp, int __MaxExp,
198 int __MinExp10, int __MaxExp10,
199 unsigned int __InfinityWord,
200 unsigned int __QNaNWord, unsigned int __SNaNWord,
202 float_round_style __RoundStyle>
203 class _Floating_limits : public _Numeric_limits_base<__number>
206 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
208 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, __Digits);
209 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
211 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
213 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
215 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, __MinExp);
216 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, __MaxExp);
217 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
218 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
220 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, true);
221 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, true);
222 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
223 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
225 denorm_indeterminate);
226 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
229 static __number infinity() throw() {
230 return float_helper<__number, __InfinityWord>::get_word();
232 static __number quiet_NaN() throw() {
233 return float_helper<__number,__QNaNWord>::get_word();
235 static __number signaling_NaN() throw() {
236 return float_helper<__number,__SNaNWord>::get_word();
239 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, __IsIEC559);
240 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
241 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false /* was: true */ );
242 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
244 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
247 // Class numeric_limits
249 // The unspecialized class.
252 class numeric_limits : public _Numeric_limits_base<T> {};
254 // Specializations for all built-in integral types.
257 class numeric_limits<bool>
258 : public _Integer_limits<bool, false, true, 0>
262 class numeric_limits<char>
263 : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
267 class numeric_limits<signed char>
268 : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
272 class numeric_limits<unsigned char>
273 : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
276 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
277 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
278 #if !defined(_WIN32) && !defined(__CYGWIN__)
280 class numeric_limits<wchar_t>
281 : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
285 class numeric_limits<wchar_t>
286 : public _Integer_limits<wchar_t, 0, USHRT_MAX>
291 class numeric_limits<wchar_t>
292 : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
298 class numeric_limits<short>
299 : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
303 class numeric_limits<unsigned short>
304 : public _Integer_limits<unsigned short, 0, USHRT_MAX>
308 class numeric_limits<int>
309 : public _Integer_limits<int, INT_MIN, INT_MAX>
313 class numeric_limits<unsigned int>
314 : public _Integer_limits<unsigned int, 0, UINT_MAX>
318 class numeric_limits<long>
319 : public _Integer_limits<long, LONG_MIN, LONG_MAX>
323 class numeric_limits<unsigned long>
324 : public _Integer_limits<unsigned long, 0, ULONG_MAX>
329 // Some compilers have long long, but don't define the
330 // LONGLONG_MIN and LONGLONG_MAX macros in limits.h. This
331 // assumes that long long is 64 bits.
332 #if !defined(LONGLONG_MIN) && !defined(LONGLONG_MAX) \
333 && !defined(ULONGLONG_MAX)
335 #define ULONGLONG_MAX 0xffffffffffffffffLLU
336 #define LONGLONG_MAX 0x7fffffffffffffffLL
337 #define LONGLONG_MIN (-LONGLONG_MAX - 1)
342 class numeric_limits<long long>
343 : public _Integer_limits<long long, LONGLONG_MIN, LONGLONG_MAX>
347 class numeric_limits<unsigned long long>
348 : public _Integer_limits<unsigned long long, 0, ULONGLONG_MAX>
351 #endif /* __GNUC__ */
353 // Specializations for all built-in floating-point type.
355 template<> class numeric_limits<float>
356 : public _Floating_limits<float,
357 FLT_MANT_DIG, // Binary digits of precision
358 FLT_DIG, // Decimal digits of precision
359 FLT_MIN_EXP, // Minimum exponent
360 FLT_MAX_EXP, // Maximum exponent
361 FLT_MIN_10_EXP, // Minimum base 10 exponent
362 FLT_MAX_10_EXP, // Maximum base 10 exponent
363 #if defined(BOOST_BIG_ENDIAN)
364 0x7f80 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
365 0x7f81 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
366 0x7fc1 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
368 0x7f800000u, // Last word of +infinity
369 0x7f810000u, // Last word of quiet NaN
370 0x7fc10000u, // Last word of signaling NaN
372 true, // conforms to iec559
376 static float min() throw() { return FLT_MIN; }
377 static float denorm_min() throw() { return FLT_MIN; }
378 static float max() throw() { return FLT_MAX; }
379 static float epsilon() throw() { return FLT_EPSILON; }
380 static float round_error() throw() { return 0.5f; } // Units: ulps.
383 template<> class numeric_limits<double>
384 : public _Floating_limits<double,
385 DBL_MANT_DIG, // Binary digits of precision
386 DBL_DIG, // Decimal digits of precision
387 DBL_MIN_EXP, // Minimum exponent
388 DBL_MAX_EXP, // Maximum exponent
389 DBL_MIN_10_EXP, // Minimum base 10 exponent
390 DBL_MAX_10_EXP, // Maximum base 10 exponent
391 #if defined(BOOST_BIG_ENDIAN)
392 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
393 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
394 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
396 0x7ff00000u, // Last word of +infinity
397 0x7ff10000u, // Last word of quiet NaN
398 0x7ff90000u, // Last word of signaling NaN
400 true, // conforms to iec559
404 static double min() throw() { return DBL_MIN; }
405 static double denorm_min() throw() { return DBL_MIN; }
406 static double max() throw() { return DBL_MAX; }
407 static double epsilon() throw() { return DBL_EPSILON; }
408 static double round_error() throw() { return 0.5; } // Units: ulps.
411 template<> class numeric_limits<long double>
412 : public _Floating_limits<long double,
413 LDBL_MANT_DIG, // Binary digits of precision
414 LDBL_DIG, // Decimal digits of precision
415 LDBL_MIN_EXP, // Minimum exponent
416 LDBL_MAX_EXP, // Maximum exponent
417 LDBL_MIN_10_EXP,// Minimum base 10 exponent
418 LDBL_MAX_10_EXP,// Maximum base 10 exponent
419 #if defined(BOOST_BIG_ENDIAN)
420 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
421 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
422 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
424 0x7fff8000u, // Last word of +infinity
425 0x7fffc000u, // Last word of quiet NaN
426 0x7fff9000u, // Last word of signaling NaN
428 false, // Doesn't conform to iec559
432 static long double min() throw() { return LDBL_MIN; }
433 static long double denorm_min() throw() { return LDBL_MIN; }
434 static long double max() throw() { return LDBL_MAX; }
435 static long double epsilon() throw() { return LDBL_EPSILON; }
436 static long double round_error() throw() { return 4; } // Units: ulps.
441 #endif /* BOOST_SGI_CPP_LIMITS */