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.
21 /* The above comment is almost certainly out of date. This file works
22 * on systems other than SGI MIPSpro C++ now.
28 * Only include <cwchar> if BOOST_NO_CWCHAR is defined. (Darin Adler)
30 * Added MIPS (big endian) to the big endian family. (Jens Maurer)
32 * Added powerpc to the big endian family. (Jeremy Siek)
34 * Added sparc (big endian) processor support (John Maddock).
36 * Modified by Jens Maurer for gcc 2.95 on x86.
39 #ifndef BOOST_SGI_CPP_LIMITS
40 #define BOOST_SGI_CPP_LIMITS
44 #include <boost/config.hpp>
46 #ifndef BOOST_NO_CWCHAR
47 #include <cwchar> // for WCHAR_MIN and WCHAR_MAX
50 // The macros are not named appropriately. We don't care about integer
51 // bit layout, but about floating-point NaN (etc.) bit patterns.
52 #if defined(__sparc) || defined(__sparc__) || defined(__powerpc__) || defined(__ppc__) || defined(__hppa) || defined(_MIPSEB)
53 #define BOOST_BIG_ENDIAN
54 #elif defined(__i386__) || defined(__alpha__)
55 #define BOOST_LITTLE_ENDIAN
57 #error The file boost/detail/limits.hpp needs to be set up for your CPU type.
62 enum float_round_style {
63 round_indeterminate = -1,
64 round_toward_zero = 0,
66 round_toward_infinity = 2,
67 round_toward_neg_infinity = 3
70 enum float_denorm_style {
71 denorm_indeterminate = -1,
76 // The C++ standard (section 18.2.1) requires that some of the members of
77 // numeric_limits be static const data members that are given constant-
78 // initializers within the class declaration. On compilers where the
79 // BOOST_NO_INCLASS_MEMBER_INITIALIZATION macro is defined, it is impossible to write
80 // a standard-conforming numeric_limits class.
82 // There are two possible workarounds: either initialize the data
83 // members outside the class, or change them from data members to
84 // enums. Neither workaround is satisfactory: the former makes it
85 // impossible to use the data members in constant-expressions, and the
86 // latter means they have the wrong type and that it is impossible to
87 // take their addresses. We choose the former workaround.
89 #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
90 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
91 enum { __mem_name = __mem_value }
92 #else /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
93 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
94 static const __mem_type __mem_name = __mem_value
95 #endif /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
97 // Deal with min/max for MinGW
106 // Base class for all specializations of numeric_limits.
107 template <class __number>
108 class _Numeric_limits_base {
110 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
112 static __number min() throw() { return __number(); }
113 static __number max() throw() { return __number(); }
115 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, 0);
116 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
118 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, false);
119 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
120 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, false);
122 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
124 static __number epsilon() throw() { return __number(); }
125 static __number round_error() throw() { return __number(); }
127 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, 0);
128 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
129 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, 0);
130 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
132 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, false);
133 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, false);
134 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
135 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
138 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
140 static __number infinity() throw() { return __number(); }
141 static __number quiet_NaN() throw() { return __number(); }
142 static __number signaling_NaN() throw() { return __number(); }
143 static __number denorm_min() throw() { return __number(); }
145 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, false);
146 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
147 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, false);
149 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false);
150 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
151 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
156 // Base class for integers.
158 template <class _Int,
162 class _Integer_limits : public _Numeric_limits_base<_Int>
165 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
167 static _Int min() throw() { return __imin; }
168 static _Int max() throw() { return __imax; }
170 BOOST_STL_DECLARE_LIMITS_MEMBER(int,
172 (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
173 - (__imin == 0 ? 0 : 1)
175 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000);
176 // log 2 = 0.301029995664...
178 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, __imin != 0);
179 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
180 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, true);
181 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
183 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
184 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
187 #if defined(BOOST_BIG_ENDIAN)
189 template<class Number, unsigned int Word>
191 static Number get_word() throw() {
192 // sizeof(long double) == 16
193 const unsigned int _S_word[4] = { Word, 0, 0, 0 };
194 return *reinterpret_cast<const Number*>(&_S_word);
200 template<class Number, unsigned int Word>
202 static Number get_word() throw() {
203 // sizeof(long double) == 12, but only 10 bytes significant
204 const unsigned int _S_word[4] = { 0, 0, 0, Word };
205 return *reinterpret_cast<const Number*>(
206 reinterpret_cast<const char *>(&_S_word)+16-
207 (sizeof(Number) == 12 ? 10 : sizeof(Number)));
213 // Base class for floating-point numbers.
214 template <class __number,
215 int __Digits, int __Digits10,
216 int __MinExp, int __MaxExp,
217 int __MinExp10, int __MaxExp10,
218 unsigned int __InfinityWord,
219 unsigned int __QNaNWord, unsigned int __SNaNWord,
221 float_round_style __RoundStyle>
222 class _Floating_limits : public _Numeric_limits_base<__number>
225 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
227 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, __Digits);
228 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
230 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
232 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
234 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, __MinExp);
235 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, __MaxExp);
236 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
237 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
239 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, true);
240 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, true);
241 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
242 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
244 denorm_indeterminate);
245 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
248 static __number infinity() throw() {
249 return float_helper<__number, __InfinityWord>::get_word();
251 static __number quiet_NaN() throw() {
252 return float_helper<__number,__QNaNWord>::get_word();
254 static __number signaling_NaN() throw() {
255 return float_helper<__number,__SNaNWord>::get_word();
258 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, __IsIEC559);
259 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
260 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false /* was: true */ );
261 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
263 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
266 // Class numeric_limits
268 // The unspecialized class.
271 class numeric_limits : public _Numeric_limits_base<T> {};
273 // Specializations for all built-in integral types.
276 class numeric_limits<bool>
277 : public _Integer_limits<bool, false, true, 0>
281 class numeric_limits<char>
282 : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
286 class numeric_limits<signed char>
287 : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
291 class numeric_limits<unsigned char>
292 : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
295 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
297 class numeric_limits<wchar_t>
298 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
299 #if defined(_WIN32) || defined(__CYGWIN__)
300 : public _Integer_limits<wchar_t, 0, USHRT_MAX>
301 #elif defined(__hppa)
302 // wchar_t has "unsigned int" as the underlying type
303 : public _Integer_limits<wchar_t, 0, UINT_MAX>
305 // assume that wchar_t has "int" as the underlying type
306 : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
309 // we have WCHAR_MIN and WCHAR_MAX defined, so use it
310 : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
316 class numeric_limits<short>
317 : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
321 class numeric_limits<unsigned short>
322 : public _Integer_limits<unsigned short, 0, USHRT_MAX>
326 class numeric_limits<int>
327 : public _Integer_limits<int, INT_MIN, INT_MAX>
331 class numeric_limits<unsigned int>
332 : public _Integer_limits<unsigned int, 0, UINT_MAX>
336 class numeric_limits<long>
337 : public _Integer_limits<long, LONG_MIN, LONG_MAX>
341 class numeric_limits<unsigned long>
342 : public _Integer_limits<unsigned long, 0, ULONG_MAX>
347 // Some compilers have long long, but don't define the
348 // LONGLONG_MIN and LONGLONG_MAX macros in limits.h. This
349 // assumes that long long is 64 bits.
350 #if !defined(LONGLONG_MAX) && !defined(ULONGLONG_MAX)
352 # define ULONGLONG_MAX 0xffffffffffffffffLLU
353 # define LONGLONG_MAX 0x7fffffffffffffffLL
357 #if !defined(LONGLONG_MIN)
358 # define LONGLONG_MIN (-LONGLONG_MAX - 1)
362 class numeric_limits<long long>
363 : public _Integer_limits<long long, LONGLONG_MIN, LONGLONG_MAX>
367 class numeric_limits<unsigned long long>
368 : public _Integer_limits<unsigned long long, 0, ULONGLONG_MAX>
371 #endif /* __GNUC__ */
373 // Specializations for all built-in floating-point type.
375 template<> class numeric_limits<float>
376 : public _Floating_limits<float,
377 FLT_MANT_DIG, // Binary digits of precision
378 FLT_DIG, // Decimal digits of precision
379 FLT_MIN_EXP, // Minimum exponent
380 FLT_MAX_EXP, // Maximum exponent
381 FLT_MIN_10_EXP, // Minimum base 10 exponent
382 FLT_MAX_10_EXP, // Maximum base 10 exponent
383 #if defined(BOOST_BIG_ENDIAN)
384 0x7f80 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
385 0x7f81 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
386 0x7fc1 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
388 0x7f800000u, // Last word of +infinity
389 0x7f810000u, // Last word of quiet NaN
390 0x7fc10000u, // Last word of signaling NaN
392 true, // conforms to iec559
396 static float min() throw() { return FLT_MIN; }
397 static float denorm_min() throw() { return FLT_MIN; }
398 static float max() throw() { return FLT_MAX; }
399 static float epsilon() throw() { return FLT_EPSILON; }
400 static float round_error() throw() { return 0.5f; } // Units: ulps.
403 template<> class numeric_limits<double>
404 : public _Floating_limits<double,
405 DBL_MANT_DIG, // Binary digits of precision
406 DBL_DIG, // Decimal digits of precision
407 DBL_MIN_EXP, // Minimum exponent
408 DBL_MAX_EXP, // Maximum exponent
409 DBL_MIN_10_EXP, // Minimum base 10 exponent
410 DBL_MAX_10_EXP, // Maximum base 10 exponent
411 #if defined(BOOST_BIG_ENDIAN)
412 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
413 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
414 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
416 0x7ff00000u, // Last word of +infinity
417 0x7ff10000u, // Last word of quiet NaN
418 0x7ff90000u, // Last word of signaling NaN
420 true, // conforms to iec559
424 static double min() throw() { return DBL_MIN; }
425 static double denorm_min() throw() { return DBL_MIN; }
426 static double max() throw() { return DBL_MAX; }
427 static double epsilon() throw() { return DBL_EPSILON; }
428 static double round_error() throw() { return 0.5; } // Units: ulps.
431 template<> class numeric_limits<long double>
432 : public _Floating_limits<long double,
433 LDBL_MANT_DIG, // Binary digits of precision
434 LDBL_DIG, // Decimal digits of precision
435 LDBL_MIN_EXP, // Minimum exponent
436 LDBL_MAX_EXP, // Maximum exponent
437 LDBL_MIN_10_EXP,// Minimum base 10 exponent
438 LDBL_MAX_10_EXP,// Maximum base 10 exponent
439 #if defined(BOOST_BIG_ENDIAN)
440 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
441 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
442 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
444 0x7fff8000u, // Last word of +infinity
445 0x7fffc000u, // Last word of quiet NaN
446 0x7fff9000u, // Last word of signaling NaN
448 false, // Doesn't conform to iec559
452 static long double min() throw() { return LDBL_MIN; }
453 static long double denorm_min() throw() { return LDBL_MIN; }
454 static long double max() throw() { return LDBL_MAX; }
455 static long double epsilon() throw() { return LDBL_EPSILON; }
456 static long double round_error() throw() { return 4; } // Units: ulps.
461 #endif /* BOOST_SGI_CPP_LIMITS */