]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/limits.hpp
update to boost 1.32.0
[lyx.git] / boost / boost / detail / limits.hpp
1 /*
2  * Copyright (c) 1997
3  * Silicon Graphics Computer Systems, Inc.
4  *
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.
12  */
13
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.
19  */
20
21 /* The above comment is almost certainly out of date. This file works
22  * on systems other than SGI MIPSpro C++ now.
23  */
24
25 /*
26  * Revision history:
27  * 21 Sep 2001:
28  *    Only include <cwchar> if BOOST_NO_CWCHAR is defined. (Darin Adler)
29  * 10 Aug 2001:
30  *    Added MIPS (big endian) to the big endian family. (Jens Maurer)
31  * 13 Apr 2001:
32  *    Added powerpc to the big endian family. (Jeremy Siek)
33  * 5 Apr 2001:
34  *    Added sparc (big endian) processor support (John Maddock).
35  * Initial sub:
36  *      Modified by Jens Maurer for gcc 2.95 on x86.
37  */
38
39 #ifndef BOOST_SGI_CPP_LIMITS
40 #define BOOST_SGI_CPP_LIMITS
41
42 #include <climits>
43 #include <cfloat>
44 #include <boost/config.hpp>
45
46 #ifndef BOOST_NO_CWCHAR
47 #include <cwchar> // for WCHAR_MIN and WCHAR_MAX
48 #endif
49
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
56 #else
57 #error The file boost/detail/limits.hpp needs to be set up for your CPU type.
58 #endif
59
60 namespace std {
61
62 enum float_round_style {
63   round_indeterminate       = -1,
64   round_toward_zero         =  0,
65   round_to_nearest          =  1,
66   round_toward_infinity     =  2,
67   round_toward_neg_infinity =  3
68 };
69
70 enum float_denorm_style {
71   denorm_indeterminate = -1,
72   denorm_absent        =  0,
73   denorm_present       =  1
74 };
75
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.
81 //
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.
88
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 */
96
97 // Base class for all specializations of numeric_limits.
98 template <class __number>
99 class _Numeric_limits_base {
100 public:
101   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
102
103   static __number min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
104   static __number max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
105
106   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   0);
107   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
108
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);
112
113   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
114
115   static __number epsilon() throw()     { return __number(); }
116   static __number round_error() throw() { return __number(); }
117
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);
122
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,
127                               has_denorm,
128                               denorm_absent);
129   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
130
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(); }
135
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);
139
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,
143                               round_style,
144                               round_toward_zero);
145 };
146
147 // Base class for integers.
148
149 template <class _Int,
150           _Int __imin,
151           _Int __imax,
152           int __idigits = -1>
153 class _Integer_limits : public _Numeric_limits_base<_Int> 
154 {
155 public:
156   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
157
158   static _Int min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imin; }
159   static _Int max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imax; }
160
161   BOOST_STL_DECLARE_LIMITS_MEMBER(int,
162                               digits,
163                               (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
164                                                    - (__imin == 0 ? 0 : 1) 
165                                               : __idigits);
166   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000); 
167                                 // log 2 = 0.301029995664...
168
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);
173
174   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
175   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
176 };
177
178 #if defined(BOOST_BIG_ENDIAN)
179
180  template<class Number, unsigned int Word>
181  struct float_helper{
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);
186   } 
187 };
188
189 #else
190
191  template<class Number, unsigned int Word>
192  struct float_helper{
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)));
199   } 
200 };
201
202 #endif
203
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,
211          bool __IsIEC559,
212          float_round_style __RoundStyle>
213 class _Floating_limits : public _Numeric_limits_base<__number>
214 {
215 public:
216   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
217
218   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   __Digits);
219   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
220
221   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
222
223   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
224
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);
229
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,
234                               has_denorm,
235                               denorm_indeterminate);
236   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
237
238  
239   static __number infinity() throw() {
240     return float_helper<__number, __InfinityWord>::get_word();
241   }
242   static __number quiet_NaN() throw() {
243     return float_helper<__number,__QNaNWord>::get_word();
244   }
245   static __number signaling_NaN() throw() {
246     return float_helper<__number,__SNaNWord>::get_word();
247   }
248
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);
253
254   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
255 };
256
257 // Class numeric_limits
258
259 // The unspecialized class.
260
261 template<class T> 
262 class numeric_limits : public _Numeric_limits_base<T> {};
263
264 // Specializations for all built-in integral types.
265
266 template<>
267 class numeric_limits<bool>
268   : public _Integer_limits<bool, false, true, 0>
269 {};
270
271 template<>
272 class numeric_limits<char>
273   : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
274 {};
275
276 template<>
277 class numeric_limits<signed char>
278   : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
279 {};
280
281 template<>
282 class numeric_limits<unsigned char>
283   : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
284 {};
285
286 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
287 template<>
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>
295 #else
296 // assume that wchar_t has "int" as the underlying type
297   : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
298 #endif
299 #else
300 // we have WCHAR_MIN and WCHAR_MAX defined, so use it
301   : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
302 #endif
303 {};
304 #endif
305
306 template<>
307 class numeric_limits<short>
308   : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
309 {};
310
311 template<>
312 class numeric_limits<unsigned short>
313   : public _Integer_limits<unsigned short, 0, USHRT_MAX>
314 {};
315
316 template<>
317 class numeric_limits<int>
318   : public _Integer_limits<int, INT_MIN, INT_MAX>
319 {};
320
321 template<>
322 class numeric_limits<unsigned int>
323   : public _Integer_limits<unsigned int, 0, UINT_MAX>
324 {};
325
326 template<>
327 class numeric_limits<long>
328   : public _Integer_limits<long, LONG_MIN, LONG_MAX>
329 {};
330
331 template<>
332 class numeric_limits<unsigned long>
333   : public _Integer_limits<unsigned long, 0, ULONG_MAX>
334 {};
335
336 #ifdef __GNUC__
337
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)
342
343 # define ULONGLONG_MAX 0xffffffffffffffffLLU
344 # define LONGLONG_MAX 0x7fffffffffffffffLL
345
346 #endif
347
348 #if !defined(LONGLONG_MIN)
349 # define LONGLONG_MIN (-LONGLONG_MAX - 1)
350 #endif 
351
352
353 #if !defined(ULONGLONG_MIN)
354 # define ULONGLONG_MIN 0
355 #endif 
356
357 #endif /* __GNUC__ */
358
359 // Specializations for all built-in floating-point type.
360
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
373 #else
374                             0x7f800000u,    // Last word of +infinity
375                             0x7f810000u,    // Last word of quiet NaN
376                             0x7fc10000u,    // Last word of signaling NaN
377 #endif
378                             true,           // conforms to iec559
379                             round_to_nearest>
380 {
381 public:
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.
387 };
388
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
401 #else
402                             0x7ff00000u,    // Last word of +infinity
403                             0x7ff10000u,    // Last word of quiet NaN
404                             0x7ff90000u,    // Last word of signaling NaN
405 #endif
406                             true,           // conforms to iec559
407                             round_to_nearest>
408 {
409 public:
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.
415 };
416
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
429 #else
430                             0x7fff8000u,    // Last word of +infinity
431                             0x7fffc000u,    // Last word of quiet NaN
432                             0x7fff9000u,    // Last word of signaling NaN
433 #endif
434                             false,          // Doesn't conform to iec559
435                             round_to_nearest>
436 {
437 public:
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.
443 };
444
445 } // namespace std
446
447 #endif /* BOOST_SGI_CPP_LIMITS */
448
449 // Local Variables:
450 // mode:C++
451 // End:
452
453
454