1 // Copyright 2001 John Maddock
\r
2 // Distributed under the Boost Software License, Version 1.0. (See accompany-
\r
3 // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
7 * Silicon Graphics Computer Systems, Inc.
\r
9 * Permission to use, copy, modify, distribute and sell this software
\r
10 * and its documentation for any purpose is hereby granted without fee,
\r
11 * provided that the above copyright notice appear in all copies and
\r
12 * that both that copyright notice and this permission notice appear
\r
13 * in supporting documentation. Silicon Graphics makes no
\r
14 * representations about the suitability of this software for any
\r
15 * purpose. It is provided "as is" without express or implied warranty.
\r
18 /* NOTE: This is not portable code. Parts of numeric_limits<> are
\r
19 * inherently machine-dependent, and this file is written for the MIPS
\r
20 * architecture and the SGI MIPSpro C++ compiler. Parts of it (in
\r
21 * particular, some of the characteristics of floating-point types)
\r
22 * are almost certainly incorrect for any other platform.
\r
25 /* The above comment is almost certainly out of date. This file works
\r
26 * on systems other than SGI MIPSpro C++ now.
\r
32 * Only include <cwchar> if BOOST_NO_CWCHAR is defined. (Darin Adler)
\r
34 * Added MIPS (big endian) to the big endian family. (Jens Maurer)
\r
36 * Added powerpc to the big endian family. (Jeremy Siek)
\r
38 * Added sparc (big endian) processor support (John Maddock).
\r
40 * Modified by Jens Maurer for gcc 2.95 on x86.
\r
43 #ifndef BOOST_SGI_CPP_LIMITS
\r
44 #define BOOST_SGI_CPP_LIMITS
\r
48 #include <boost/config.hpp>
\r
49 #include <boost/detail/endian.hpp>
\r
51 #ifndef BOOST_NO_CWCHAR
\r
52 #include <cwchar> // for WCHAR_MIN and WCHAR_MAX
\r
57 enum float_round_style {
\r
58 round_indeterminate = -1,
\r
59 round_toward_zero = 0,
\r
60 round_to_nearest = 1,
\r
61 round_toward_infinity = 2,
\r
62 round_toward_neg_infinity = 3
\r
65 enum float_denorm_style {
\r
66 denorm_indeterminate = -1,
\r
71 // The C++ standard (section 18.2.1) requires that some of the members of
\r
72 // numeric_limits be static const data members that are given constant-
\r
73 // initializers within the class declaration. On compilers where the
\r
74 // BOOST_NO_INCLASS_MEMBER_INITIALIZATION macro is defined, it is impossible to write
\r
75 // a standard-conforming numeric_limits class.
\r
77 // There are two possible workarounds: either initialize the data
\r
78 // members outside the class, or change them from data members to
\r
79 // enums. Neither workaround is satisfactory: the former makes it
\r
80 // impossible to use the data members in constant-expressions, and the
\r
81 // latter means they have the wrong type and that it is impossible to
\r
82 // take their addresses. We choose the former workaround.
\r
84 #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
\r
85 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
\r
86 enum { __mem_name = __mem_value }
\r
87 #else /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
\r
88 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
\r
89 static const __mem_type __mem_name = __mem_value
\r
90 #endif /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
\r
92 // Base class for all specializations of numeric_limits.
\r
93 template <class __number>
\r
94 class _Numeric_limits_base {
\r
96 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
\r
98 static __number min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
\r
99 static __number max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
\r
101 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, 0);
\r
102 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
\r
104 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, false);
\r
105 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
\r
106 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, false);
\r
108 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
\r
110 static __number epsilon() throw() { return __number(); }
\r
111 static __number round_error() throw() { return __number(); }
\r
113 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, 0);
\r
114 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
\r
115 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, 0);
\r
116 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
\r
118 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, false);
\r
119 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, false);
\r
120 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
\r
121 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
\r
124 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
\r
126 static __number infinity() throw() { return __number(); }
\r
127 static __number quiet_NaN() throw() { return __number(); }
\r
128 static __number signaling_NaN() throw() { return __number(); }
\r
129 static __number denorm_min() throw() { return __number(); }
\r
131 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, false);
\r
132 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
\r
133 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, false);
\r
135 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false);
\r
136 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
\r
137 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
\r
139 round_toward_zero);
\r
142 // Base class for integers.
\r
144 template <class _Int,
\r
147 int __idigits = -1>
\r
148 class _Integer_limits : public _Numeric_limits_base<_Int>
\r
151 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
\r
153 static _Int min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imin; }
\r
154 static _Int max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imax; }
\r
156 BOOST_STL_DECLARE_LIMITS_MEMBER(int,
\r
158 (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
\r
159 - (__imin == 0 ? 0 : 1)
\r
161 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000);
\r
162 // log 2 = 0.301029995664...
\r
164 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, __imin != 0);
\r
165 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
\r
166 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, true);
\r
167 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
\r
169 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
\r
170 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
\r
173 #if defined(BOOST_BIG_ENDIAN)
\r
175 template<class Number, unsigned int Word>
\r
176 struct float_helper{
\r
177 static Number get_word() throw() {
\r
178 // sizeof(long double) == 16
\r
179 const unsigned int _S_word[4] = { Word, 0, 0, 0 };
\r
180 return *reinterpret_cast<const Number*>(&_S_word);
\r
186 template<class Number, unsigned int Word>
\r
187 struct float_helper{
\r
188 static Number get_word() throw() {
\r
189 // sizeof(long double) == 12, but only 10 bytes significant
\r
190 const unsigned int _S_word[4] = { 0, 0, 0, Word };
\r
191 return *reinterpret_cast<const Number*>(
\r
192 reinterpret_cast<const char *>(&_S_word)+16-
\r
193 (sizeof(Number) == 12 ? 10 : sizeof(Number)));
\r
199 // Base class for floating-point numbers.
\r
200 template <class __number,
\r
201 int __Digits, int __Digits10,
\r
202 int __MinExp, int __MaxExp,
\r
203 int __MinExp10, int __MaxExp10,
\r
204 unsigned int __InfinityWord,
\r
205 unsigned int __QNaNWord, unsigned int __SNaNWord,
\r
207 float_round_style __RoundStyle>
\r
208 class _Floating_limits : public _Numeric_limits_base<__number>
\r
211 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
\r
213 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, __Digits);
\r
214 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
\r
216 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
\r
218 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
\r
220 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, __MinExp);
\r
221 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, __MaxExp);
\r
222 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
\r
223 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
\r
225 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, true);
\r
226 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, true);
\r
227 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
\r
228 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
\r
230 denorm_indeterminate);
\r
231 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
\r
234 static __number infinity() throw() {
\r
235 return float_helper<__number, __InfinityWord>::get_word();
\r
237 static __number quiet_NaN() throw() {
\r
238 return float_helper<__number,__QNaNWord>::get_word();
\r
240 static __number signaling_NaN() throw() {
\r
241 return float_helper<__number,__SNaNWord>::get_word();
\r
244 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, __IsIEC559);
\r
245 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
\r
246 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false /* was: true */ );
\r
247 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
\r
249 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
\r
252 // Class numeric_limits
\r
254 // The unspecialized class.
\r
257 class numeric_limits : public _Numeric_limits_base<T> {};
\r
259 // Specializations for all built-in integral types.
\r
262 class numeric_limits<bool>
\r
263 : public _Integer_limits<bool, false, true, 0>
\r
267 class numeric_limits<char>
\r
268 : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
\r
272 class numeric_limits<signed char>
\r
273 : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
\r
277 class numeric_limits<unsigned char>
\r
278 : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
\r
281 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
\r
283 class numeric_limits<wchar_t>
\r
284 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
\r
285 #if defined(_WIN32) || defined(__CYGWIN__)
\r
286 : public _Integer_limits<wchar_t, 0, USHRT_MAX>
\r
287 #elif defined(__hppa)
\r
288 // wchar_t has "unsigned int" as the underlying type
\r
289 : public _Integer_limits<wchar_t, 0, UINT_MAX>
\r
291 // assume that wchar_t has "int" as the underlying type
\r
292 : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
\r
295 // we have WCHAR_MIN and WCHAR_MAX defined, so use it
\r
296 : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
\r
302 class numeric_limits<short>
\r
303 : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
\r
307 class numeric_limits<unsigned short>
\r
308 : public _Integer_limits<unsigned short, 0, USHRT_MAX>
\r
312 class numeric_limits<int>
\r
313 : public _Integer_limits<int, INT_MIN, INT_MAX>
\r
317 class numeric_limits<unsigned int>
\r
318 : public _Integer_limits<unsigned int, 0, UINT_MAX>
\r
322 class numeric_limits<long>
\r
323 : public _Integer_limits<long, LONG_MIN, LONG_MAX>
\r
327 class numeric_limits<unsigned long>
\r
328 : public _Integer_limits<unsigned long, 0, ULONG_MAX>
\r
333 // Some compilers have long long, but don't define the
\r
334 // LONGLONG_MIN and LONGLONG_MAX macros in limits.h. This
\r
335 // assumes that long long is 64 bits.
\r
336 #if !defined(LONGLONG_MAX) && !defined(ULONGLONG_MAX)
\r
338 # define ULONGLONG_MAX 0xffffffffffffffffLLU
\r
339 # define LONGLONG_MAX 0x7fffffffffffffffLL
\r
343 #if !defined(LONGLONG_MIN)
\r
344 # define LONGLONG_MIN (-LONGLONG_MAX - 1)
\r
348 #if !defined(ULONGLONG_MIN)
\r
349 # define ULONGLONG_MIN 0
\r
352 #endif /* __GNUC__ */
\r
354 // Specializations for all built-in floating-point type.
\r
356 template<> class numeric_limits<float>
\r
357 : public _Floating_limits<float,
\r
358 FLT_MANT_DIG, // Binary digits of precision
\r
359 FLT_DIG, // Decimal digits of precision
\r
360 FLT_MIN_EXP, // Minimum exponent
\r
361 FLT_MAX_EXP, // Maximum exponent
\r
362 FLT_MIN_10_EXP, // Minimum base 10 exponent
\r
363 FLT_MAX_10_EXP, // Maximum base 10 exponent
\r
364 #if defined(BOOST_BIG_ENDIAN)
\r
365 0x7f80 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
\r
366 0x7f81 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
\r
367 0x7fc1 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
\r
369 0x7f800000u, // Last word of +infinity
\r
370 0x7f810000u, // Last word of quiet NaN
\r
371 0x7fc10000u, // Last word of signaling NaN
\r
373 true, // conforms to iec559
\r
377 static float min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return FLT_MIN; }
\r
378 static float denorm_min() throw() { return FLT_MIN; }
\r
379 static float max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return FLT_MAX; }
\r
380 static float epsilon() throw() { return FLT_EPSILON; }
\r
381 static float round_error() throw() { return 0.5f; } // Units: ulps.
\r
384 template<> class numeric_limits<double>
\r
385 : public _Floating_limits<double,
\r
386 DBL_MANT_DIG, // Binary digits of precision
\r
387 DBL_DIG, // Decimal digits of precision
\r
388 DBL_MIN_EXP, // Minimum exponent
\r
389 DBL_MAX_EXP, // Maximum exponent
\r
390 DBL_MIN_10_EXP, // Minimum base 10 exponent
\r
391 DBL_MAX_10_EXP, // Maximum base 10 exponent
\r
392 #if defined(BOOST_BIG_ENDIAN)
\r
393 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
\r
394 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
\r
395 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
\r
397 0x7ff00000u, // Last word of +infinity
\r
398 0x7ff10000u, // Last word of quiet NaN
\r
399 0x7ff90000u, // Last word of signaling NaN
\r
401 true, // conforms to iec559
\r
405 static double min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return DBL_MIN; }
\r
406 static double denorm_min() throw() { return DBL_MIN; }
\r
407 static double max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return DBL_MAX; }
\r
408 static double epsilon() throw() { return DBL_EPSILON; }
\r
409 static double round_error() throw() { return 0.5; } // Units: ulps.
\r
412 template<> class numeric_limits<long double>
\r
413 : public _Floating_limits<long double,
\r
414 LDBL_MANT_DIG, // Binary digits of precision
\r
415 LDBL_DIG, // Decimal digits of precision
\r
416 LDBL_MIN_EXP, // Minimum exponent
\r
417 LDBL_MAX_EXP, // Maximum exponent
\r
418 LDBL_MIN_10_EXP,// Minimum base 10 exponent
\r
419 LDBL_MAX_10_EXP,// Maximum base 10 exponent
\r
420 #if defined(BOOST_BIG_ENDIAN)
\r
421 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
\r
422 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
\r
423 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
\r
425 0x7fff8000u, // Last word of +infinity
\r
426 0x7fffc000u, // Last word of quiet NaN
\r
427 0x7fff9000u, // Last word of signaling NaN
\r
429 false, // Doesn't conform to iec559
\r
433 static long double min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return LDBL_MIN; }
\r
434 static long double denorm_min() throw() { return LDBL_MIN; }
\r
435 static long double max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return LDBL_MAX; }
\r
436 static long double epsilon() throw() { return LDBL_EPSILON; }
\r
437 static long double round_error() throw() { return 4; } // Units: ulps.
\r
442 #endif /* BOOST_SGI_CPP_LIMITS */
\r
444 // Local Variables:
\r