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) || defined(_POWER)
53 #define BOOST_BIG_ENDIAN
54 #elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__)
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 // Base class for all specializations of numeric_limits.
98 template <class __number>
99 class _Numeric_limits_base {
101 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
103 static __number min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
104 static __number max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
106 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, 0);
107 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
109 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, false);
110 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
111 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, false);
113 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
115 static __number epsilon() throw() { return __number(); }
116 static __number round_error() throw() { return __number(); }
118 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, 0);
119 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
120 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, 0);
121 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
123 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, false);
124 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, false);
125 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
126 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
129 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
131 static __number infinity() throw() { return __number(); }
132 static __number quiet_NaN() throw() { return __number(); }
133 static __number signaling_NaN() throw() { return __number(); }
134 static __number denorm_min() throw() { return __number(); }
136 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, false);
137 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
138 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, false);
140 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false);
141 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
142 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
147 // Base class for integers.
149 template <class _Int,
153 class _Integer_limits : public _Numeric_limits_base<_Int>
156 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
158 static _Int min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imin; }
159 static _Int max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imax; }
161 BOOST_STL_DECLARE_LIMITS_MEMBER(int,
163 (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
164 - (__imin == 0 ? 0 : 1)
166 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000);
167 // log 2 = 0.301029995664...
169 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, __imin != 0);
170 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
171 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, true);
172 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
174 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
175 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
178 #if defined(BOOST_BIG_ENDIAN)
180 template<class Number, unsigned int Word>
182 static Number get_word() throw() {
183 // sizeof(long double) == 16
184 const unsigned int _S_word[4] = { Word, 0, 0, 0 };
185 return *reinterpret_cast<const Number*>(&_S_word);
191 template<class Number, unsigned int Word>
193 static Number get_word() throw() {
194 // sizeof(long double) == 12, but only 10 bytes significant
195 const unsigned int _S_word[4] = { 0, 0, 0, Word };
196 return *reinterpret_cast<const Number*>(
197 reinterpret_cast<const char *>(&_S_word)+16-
198 (sizeof(Number) == 12 ? 10 : sizeof(Number)));
204 // Base class for floating-point numbers.
205 template <class __number,
206 int __Digits, int __Digits10,
207 int __MinExp, int __MaxExp,
208 int __MinExp10, int __MaxExp10,
209 unsigned int __InfinityWord,
210 unsigned int __QNaNWord, unsigned int __SNaNWord,
212 float_round_style __RoundStyle>
213 class _Floating_limits : public _Numeric_limits_base<__number>
216 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
218 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, __Digits);
219 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
221 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
223 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
225 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, __MinExp);
226 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, __MaxExp);
227 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
228 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
230 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, true);
231 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, true);
232 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
233 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
235 denorm_indeterminate);
236 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
239 static __number infinity() throw() {
240 return float_helper<__number, __InfinityWord>::get_word();
242 static __number quiet_NaN() throw() {
243 return float_helper<__number,__QNaNWord>::get_word();
245 static __number signaling_NaN() throw() {
246 return float_helper<__number,__SNaNWord>::get_word();
249 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, __IsIEC559);
250 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
251 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false /* was: true */ );
252 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
254 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
257 // Class numeric_limits
259 // The unspecialized class.
262 class numeric_limits : public _Numeric_limits_base<T> {};
264 // Specializations for all built-in integral types.
267 class numeric_limits<bool>
268 : public _Integer_limits<bool, false, true, 0>
272 class numeric_limits<char>
273 : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
277 class numeric_limits<signed char>
278 : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
282 class numeric_limits<unsigned char>
283 : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
286 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
288 class numeric_limits<wchar_t>
289 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
290 #if defined(_WIN32) || defined(__CYGWIN__)
291 : public _Integer_limits<wchar_t, 0, USHRT_MAX>
292 #elif defined(__hppa)
293 // wchar_t has "unsigned int" as the underlying type
294 : public _Integer_limits<wchar_t, 0, UINT_MAX>
296 // assume that wchar_t has "int" as the underlying type
297 : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
300 // we have WCHAR_MIN and WCHAR_MAX defined, so use it
301 : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
307 class numeric_limits<short>
308 : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
312 class numeric_limits<unsigned short>
313 : public _Integer_limits<unsigned short, 0, USHRT_MAX>
317 class numeric_limits<int>
318 : public _Integer_limits<int, INT_MIN, INT_MAX>
322 class numeric_limits<unsigned int>
323 : public _Integer_limits<unsigned int, 0, UINT_MAX>
327 class numeric_limits<long>
328 : public _Integer_limits<long, LONG_MIN, LONG_MAX>
332 class numeric_limits<unsigned long>
333 : public _Integer_limits<unsigned long, 0, ULONG_MAX>
338 // Some compilers have long long, but don't define the
339 // LONGLONG_MIN and LONGLONG_MAX macros in limits.h. This
340 // assumes that long long is 64 bits.
341 #if !defined(LONGLONG_MAX) && !defined(ULONGLONG_MAX)
343 # define ULONGLONG_MAX 0xffffffffffffffffLLU
344 # define LONGLONG_MAX 0x7fffffffffffffffLL
348 #if !defined(LONGLONG_MIN)
349 # define LONGLONG_MIN (-LONGLONG_MAX - 1)
353 #if !defined(ULONGLONG_MIN)
354 # define ULONGLONG_MIN 0
357 #endif /* __GNUC__ */
359 // Specializations for all built-in floating-point type.
361 template<> class numeric_limits<float>
362 : public _Floating_limits<float,
363 FLT_MANT_DIG, // Binary digits of precision
364 FLT_DIG, // Decimal digits of precision
365 FLT_MIN_EXP, // Minimum exponent
366 FLT_MAX_EXP, // Maximum exponent
367 FLT_MIN_10_EXP, // Minimum base 10 exponent
368 FLT_MAX_10_EXP, // Maximum base 10 exponent
369 #if defined(BOOST_BIG_ENDIAN)
370 0x7f80 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
371 0x7f81 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
372 0x7fc1 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
374 0x7f800000u, // Last word of +infinity
375 0x7f810000u, // Last word of quiet NaN
376 0x7fc10000u, // Last word of signaling NaN
378 true, // conforms to iec559
382 static float min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return FLT_MIN; }
383 static float denorm_min() throw() { return FLT_MIN; }
384 static float max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return FLT_MAX; }
385 static float epsilon() throw() { return FLT_EPSILON; }
386 static float round_error() throw() { return 0.5f; } // Units: ulps.
389 template<> class numeric_limits<double>
390 : public _Floating_limits<double,
391 DBL_MANT_DIG, // Binary digits of precision
392 DBL_DIG, // Decimal digits of precision
393 DBL_MIN_EXP, // Minimum exponent
394 DBL_MAX_EXP, // Maximum exponent
395 DBL_MIN_10_EXP, // Minimum base 10 exponent
396 DBL_MAX_10_EXP, // Maximum base 10 exponent
397 #if defined(BOOST_BIG_ENDIAN)
398 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
399 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
400 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
402 0x7ff00000u, // Last word of +infinity
403 0x7ff10000u, // Last word of quiet NaN
404 0x7ff90000u, // Last word of signaling NaN
406 true, // conforms to iec559
410 static double min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return DBL_MIN; }
411 static double denorm_min() throw() { return DBL_MIN; }
412 static double max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return DBL_MAX; }
413 static double epsilon() throw() { return DBL_EPSILON; }
414 static double round_error() throw() { return 0.5; } // Units: ulps.
417 template<> class numeric_limits<long double>
418 : public _Floating_limits<long double,
419 LDBL_MANT_DIG, // Binary digits of precision
420 LDBL_DIG, // Decimal digits of precision
421 LDBL_MIN_EXP, // Minimum exponent
422 LDBL_MAX_EXP, // Maximum exponent
423 LDBL_MIN_10_EXP,// Minimum base 10 exponent
424 LDBL_MAX_10_EXP,// Maximum base 10 exponent
425 #if defined(BOOST_BIG_ENDIAN)
426 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
427 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
428 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
430 0x7fff8000u, // Last word of +infinity
431 0x7fffc000u, // Last word of quiet NaN
432 0x7fff9000u, // Last word of signaling NaN
434 false, // Doesn't conform to iec559
438 static long double min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return LDBL_MIN; }
439 static long double denorm_min() throw() { return LDBL_MIN; }
440 static long double max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return LDBL_MAX; }
441 static long double epsilon() throw() { return LDBL_EPSILON; }
442 static long double round_error() throw() { return 4; } // Units: ulps.
447 #endif /* BOOST_SGI_CPP_LIMITS */