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 powerpc to the big endian family. (Jeremy Siek)
26 * Added sparc (big endian) processor support (John Maddock).
28 * Modified by Jens Maurer for gcc 2.95 on x86.
31 #ifndef BOOST_SGI_CPP_LIMITS
32 #define BOOST_SGI_CPP_LIMITS
36 #include <boost/config.hpp>
38 #if defined(__sparc) || defined(__sparc__) || defined(__powerpc__) || defined(__hppa)
39 #define BOOST_BIG_ENDIAN
40 #elif !defined(__i386__)
41 #error The file boost/detail/limits.hpp needs to be set up for your CPU type.
46 enum float_round_style {
47 round_indeterminate = -1,
48 round_toward_zero = 0,
50 round_toward_infinity = 2,
51 round_toward_neg_infinity = 3
54 enum float_denorm_style {
55 denorm_indeterminate = -1,
60 // The C++ standard (section 18.2.1) requires that some of the members of
61 // numeric_limits be static const data members that are given constant-
62 // initializers within the class declaration. On compilers where the
63 // BOOST_NO_INCLASS_MEMBER_INITIALIZATION macro is defined, it is impossible to write
64 // a standard-conforming numeric_limits class.
66 // There are two possible workarounds: either initialize the data
67 // members outside the class, or change them from data members to
68 // enums. Neither workaround is satisfactory: the former makes it
69 // impossible to use the data members in constant-expressions, and the
70 // latter means they have the wrong type and that it is impossible to
71 // take their addresses. We choose the former workaround.
73 #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
74 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
75 enum { __mem_name = __mem_value }
76 #else /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
77 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
78 static const __mem_type __mem_name = __mem_value
79 #endif /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
81 // Base class for all specializations of numeric_limits.
83 template <class __number>
84 class _Numeric_limits_base {
86 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
88 static __number min() throw() { return __number(); }
89 static __number max() throw() { return __number(); }
91 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, 0);
92 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
94 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, false);
95 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
96 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, false);
98 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
100 static __number epsilon() throw() { return __number(); }
101 static __number round_error() throw() { return __number(); }
103 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, 0);
104 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
105 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, 0);
106 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
108 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, false);
109 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, false);
110 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
111 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
114 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
116 static __number infinity() throw() { return __number(); }
117 static __number quiet_NaN() throw() { return __number(); }
118 static __number signaling_NaN() throw() { return __number(); }
119 static __number denorm_min() throw() { return __number(); }
121 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, false);
122 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
123 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, false);
125 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false);
126 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
127 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
132 // Base class for integers.
134 template <class _Int,
138 class _Integer_limits : public _Numeric_limits_base<_Int>
141 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
143 static _Int min() throw() { return __imin; }
144 static _Int max() throw() { return __imax; }
146 BOOST_STL_DECLARE_LIMITS_MEMBER(int,
148 (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
149 - (__imin == 0 ? 0 : 1)
151 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000);
152 // log 2 = 0.301029995664...
154 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, __imin != 0);
155 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
156 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, true);
157 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
159 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
160 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
163 #if defined(BOOST_BIG_ENDIAN)
165 template<class Number, unsigned int Word>
167 static Number get_word() throw() {
168 // sizeof(long double) == 16
169 const unsigned int _S_word[4] = { Word, 0, 0, 0 };
170 return *reinterpret_cast<const Number*>(&_S_word);
176 template<class Number, unsigned int Word>
178 static Number get_word() throw() {
179 // sizeof(long double) == 12, but only 10 bytes significant
180 const unsigned int _S_word[4] = { 0, 0, 0, Word };
181 return *reinterpret_cast<const Number*>(
182 reinterpret_cast<const char *>(&_S_word)+16-
183 (sizeof(Number) == 12 ? 10 : sizeof(Number)));
189 // Base class for floating-point numbers.
190 template <class __number,
191 int __Digits, int __Digits10,
192 int __MinExp, int __MaxExp,
193 int __MinExp10, int __MaxExp10,
194 unsigned int __InfinityWord,
195 unsigned int __QNaNWord, unsigned int __SNaNWord,
197 float_round_style __RoundStyle>
198 class _Floating_limits : public _Numeric_limits_base<__number>
201 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
203 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, __Digits);
204 BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
206 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
208 BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
210 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, __MinExp);
211 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, __MaxExp);
212 BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
213 BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
215 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, true);
216 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, true);
217 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
218 BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
220 denorm_indeterminate);
221 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
224 static __number infinity() throw() {
225 return float_helper<__number, __InfinityWord>::get_word();
227 static __number quiet_NaN() throw() {
228 return float_helper<__number,__QNaNWord>::get_word();
230 static __number signaling_NaN() throw() {
231 return float_helper<__number,__SNaNWord>::get_word();
234 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, __IsIEC559);
235 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
236 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false /* was: true */ );
237 BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
239 BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
242 // Class numeric_limits
244 // The unspecialized class.
247 class numeric_limits : public _Numeric_limits_base<T> {};
249 // Specializations for all built-in integral types.
252 class numeric_limits<bool>
253 : public _Integer_limits<bool, false, true, 0>
257 class numeric_limits<char>
258 : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
262 class numeric_limits<signed char>
263 : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
267 class numeric_limits<unsigned char>
268 : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
271 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
272 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
273 #if !defined(_WIN32) && !defined(__CYGWIN__)
275 class numeric_limits<wchar_t>
276 : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
280 class numeric_limits<wchar_t>
281 : public _Integer_limits<wchar_t, 0, USHRT_MAX>
286 class numeric_limits<wchar_t>
287 : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
293 class numeric_limits<short>
294 : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
298 class numeric_limits<unsigned short>
299 : public _Integer_limits<unsigned short, 0, USHRT_MAX>
303 class numeric_limits<int>
304 : public _Integer_limits<int, INT_MIN, INT_MAX>
308 class numeric_limits<unsigned int>
309 : public _Integer_limits<unsigned int, 0, UINT_MAX>
313 class numeric_limits<long>
314 : public _Integer_limits<long, LONG_MIN, LONG_MAX>
318 class numeric_limits<unsigned long>
319 : public _Integer_limits<unsigned long, 0, ULONG_MAX>
324 // Some compilers have long long, but don't define the
325 // LONGLONG_MIN and LONGLONG_MAX macros in limits.h. This
326 // assumes that long long is 64 bits.
327 #if !defined(LONGLONG_MIN) && !defined(LONGLONG_MAX) \
328 && !defined(ULONGLONG_MAX)
330 #define ULONGLONG_MAX 0xffffffffffffffffLLU
331 #define LONGLONG_MAX 0x7fffffffffffffffLL
332 #define LONGLONG_MIN (-LONGLONG_MAX - 1)
337 class numeric_limits<long long>
338 : public _Integer_limits<long long, LONGLONG_MIN, LONGLONG_MAX>
342 class numeric_limits<unsigned long long>
343 : public _Integer_limits<unsigned long long, 0, ULONGLONG_MAX>
346 #endif /* __GNUC__ */
348 // Specializations for all built-in floating-point type.
350 template<> class numeric_limits<float>
351 : public _Floating_limits<float,
352 FLT_MANT_DIG, // Binary digits of precision
353 FLT_DIG, // Decimal digits of precision
354 FLT_MIN_EXP, // Minimum exponent
355 FLT_MAX_EXP, // Maximum exponent
356 FLT_MIN_10_EXP, // Minimum base 10 exponent
357 FLT_MAX_10_EXP, // Maximum base 10 exponent
358 #if defined(BOOST_BIG_ENDIAN)
359 0x7f80 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
360 0x7f81 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
361 0x7fc1 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
363 0x7f800000u, // Last word of +infinity
364 0x7f810000u, // Last word of quiet NaN
365 0x7fc10000u, // Last word of signaling NaN
367 true, // conforms to iec559
371 static float min() throw() { return FLT_MIN; }
372 static float denorm_min() throw() { return FLT_MIN; }
373 static float max() throw() { return FLT_MAX; }
374 static float epsilon() throw() { return FLT_EPSILON; }
375 static float round_error() throw() { return 0.5f; } // Units: ulps.
378 template<> class numeric_limits<double>
379 : public _Floating_limits<double,
380 DBL_MANT_DIG, // Binary digits of precision
381 DBL_DIG, // Decimal digits of precision
382 DBL_MIN_EXP, // Minimum exponent
383 DBL_MAX_EXP, // Maximum exponent
384 DBL_MIN_10_EXP, // Minimum base 10 exponent
385 DBL_MAX_10_EXP, // Maximum base 10 exponent
386 #if defined(BOOST_BIG_ENDIAN)
387 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
388 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
389 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
391 0x7ff00000u, // Last word of +infinity
392 0x7ff10000u, // Last word of quiet NaN
393 0x7ff90000u, // Last word of signaling NaN
395 true, // conforms to iec559
399 static double min() throw() { return DBL_MIN; }
400 static double denorm_min() throw() { return DBL_MIN; }
401 static double max() throw() { return DBL_MAX; }
402 static double epsilon() throw() { return DBL_EPSILON; }
403 static double round_error() throw() { return 0.5; } // Units: ulps.
406 template<> class numeric_limits<long double>
407 : public _Floating_limits<long double,
408 LDBL_MANT_DIG, // Binary digits of precision
409 LDBL_DIG, // Decimal digits of precision
410 LDBL_MIN_EXP, // Minimum exponent
411 LDBL_MAX_EXP, // Maximum exponent
412 LDBL_MIN_10_EXP,// Minimum base 10 exponent
413 LDBL_MAX_10_EXP,// Maximum base 10 exponent
414 #if defined(BOOST_BIG_ENDIAN)
415 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
416 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
417 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
419 0x7fff8000u, // Last word of +infinity
420 0x7fffc000u, // Last word of quiet NaN
421 0x7fff9000u, // Last word of signaling NaN
423 false, // Doesn't conform to iec559
427 static long double min() throw() { return LDBL_MIN; }
428 static long double denorm_min() throw() { return LDBL_MIN; }
429 static long double max() throw() { return LDBL_MAX; }
430 static long double epsilon() throw() { return LDBL_EPSILON; }
431 static long double round_error() throw() { return 4; } // Units: ulps.
436 #endif /* BOOST_SGI_CPP_LIMITS */