]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/limits.hpp
some new boost files that we now use
[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 /*
22  * Revision history:
23  * 13 Apr 2001:
24  *    Added powerpc to the big endian family. (Jeremy Siek)
25  * 5 Apr 2001:
26  *    Added sparc (big endian) processor support (John Maddock).
27  * Initial sub:
28  *      Modified by Jens Maurer for gcc 2.95 on x86.
29  */
30
31 #ifndef BOOST_SGI_CPP_LIMITS
32 #define BOOST_SGI_CPP_LIMITS
33
34 #include <climits>
35 #include <cfloat>
36 #include <boost/config.hpp>
37
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.
42 #endif
43
44 namespace std {
45
46 enum float_round_style {
47   round_indeterminate       = -1,
48   round_toward_zero         =  0,
49   round_to_nearest          =  1,
50   round_toward_infinity     =  2,
51   round_toward_neg_infinity =  3
52 };
53
54 enum float_denorm_style {
55   denorm_indeterminate = -1,
56   denorm_absent        =  0,
57   denorm_present       =  1
58 };
59
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.
65 //
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.
72
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 */
80
81 // Base class for all specializations of numeric_limits.
82
83 template <class __number>
84 class _Numeric_limits_base {
85 public:
86   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
87
88   static __number min() throw() { return __number(); }
89   static __number max() throw() { return __number(); }
90
91   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   0);
92   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
93
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);
97
98   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
99
100   static __number epsilon() throw()     { return __number(); }
101   static __number round_error() throw() { return __number(); }
102
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);
107
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,
112                               has_denorm,
113                               denorm_absent);
114   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
115
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(); }
120
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);
124
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,
128                               round_style,
129                               round_toward_zero);
130 };
131
132 // Base class for integers.
133
134 template <class _Int,
135           _Int __imin,
136           _Int __imax,
137           int __idigits = -1>
138 class _Integer_limits : public _Numeric_limits_base<_Int> 
139 {
140 public:
141   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
142
143   static _Int min() throw() { return __imin; }
144   static _Int max() throw() { return __imax; }
145
146   BOOST_STL_DECLARE_LIMITS_MEMBER(int,
147                               digits,
148                               (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
149                                                    - (__imin == 0 ? 0 : 1) 
150                                               : __idigits);
151   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000); 
152                                 // log 2 = 0.301029995664...
153
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);
158
159   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
160   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
161 };
162
163 #if defined(BOOST_BIG_ENDIAN)
164
165  template<class Number, unsigned int Word>
166  struct float_helper{
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);
171   } 
172 };
173
174 #else
175
176  template<class Number, unsigned int Word>
177  struct float_helper{
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)));
184   } 
185 };
186
187 #endif
188
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,
196          bool __IsIEC559,
197          float_round_style __RoundStyle>
198 class _Floating_limits : public _Numeric_limits_base<__number>
199 {
200 public:
201   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
202
203   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   __Digits);
204   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
205
206   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
207
208   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
209
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);
214
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,
219                               has_denorm,
220                               denorm_indeterminate);
221   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
222
223  
224   static __number infinity() throw() {
225     return float_helper<__number, __InfinityWord>::get_word();
226   }
227   static __number quiet_NaN() throw() {
228     return float_helper<__number,__QNaNWord>::get_word();
229   }
230   static __number signaling_NaN() throw() {
231     return float_helper<__number,__SNaNWord>::get_word();
232   }
233
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);
238
239   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
240 };
241
242 // Class numeric_limits
243
244 // The unspecialized class.
245
246 template<class T> 
247 class numeric_limits : public _Numeric_limits_base<T> {};
248
249 // Specializations for all built-in integral types.
250
251 template<>
252 class numeric_limits<bool>
253   : public _Integer_limits<bool, false, true, 0>
254 {};
255
256 template<>
257 class numeric_limits<char>
258   : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
259 {};
260
261 template<>
262 class numeric_limits<signed char>
263   : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
264 {};
265
266 template<>
267 class numeric_limits<unsigned char>
268   : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
269 {};
270
271 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
272 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
273 #if !defined(_WIN32) && !defined(__CYGWIN__)
274 template<>
275 class numeric_limits<wchar_t>
276   : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
277 {};
278 #else
279 template<>
280 class numeric_limits<wchar_t>
281   : public _Integer_limits<wchar_t, 0, USHRT_MAX>
282 {};
283 #endif
284 #else
285 template<>
286 class numeric_limits<wchar_t>
287   : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
288 {};
289 #endif
290 #endif
291
292 template<>
293 class numeric_limits<short>
294   : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
295 {};
296
297 template<>
298 class numeric_limits<unsigned short>
299   : public _Integer_limits<unsigned short, 0, USHRT_MAX>
300 {};
301
302 template<>
303 class numeric_limits<int>
304   : public _Integer_limits<int, INT_MIN, INT_MAX>
305 {};
306
307 template<>
308 class numeric_limits<unsigned int>
309   : public _Integer_limits<unsigned int, 0, UINT_MAX>
310 {};
311
312 template<>
313 class numeric_limits<long>
314   : public _Integer_limits<long, LONG_MIN, LONG_MAX>
315 {};
316
317 template<>
318 class numeric_limits<unsigned long>
319   : public _Integer_limits<unsigned long, 0, ULONG_MAX>
320 {};
321
322 #ifdef __GNUC__
323
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)
329
330 #define ULONGLONG_MAX 0xffffffffffffffffLLU
331 #define LONGLONG_MAX 0x7fffffffffffffffLL
332 #define LONGLONG_MIN (-LONGLONG_MAX - 1)
333
334 #endif
335
336 template<>
337 class numeric_limits<long long>
338   : public _Integer_limits<long long, LONGLONG_MIN, LONGLONG_MAX>
339 {};
340
341 template<>
342 class numeric_limits<unsigned long long>
343   : public _Integer_limits<unsigned long long, 0, ULONGLONG_MAX>
344 {};
345
346 #endif /* __GNUC__ */
347
348 // Specializations for all built-in floating-point type.
349
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
362 #else
363                             0x7f800000u,    // Last word of +infinity
364                             0x7f810000u,    // Last word of quiet NaN
365                             0x7fc10000u,    // Last word of signaling NaN
366 #endif
367                             true,           // conforms to iec559
368                             round_to_nearest>
369 {
370 public:
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.
376 };
377
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
390 #else
391                             0x7ff00000u,    // Last word of +infinity
392                             0x7ff10000u,    // Last word of quiet NaN
393                             0x7ff90000u,    // Last word of signaling NaN
394 #endif
395                             true,           // conforms to iec559
396                             round_to_nearest>
397 {
398 public:
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.
404 };
405
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
418 #else
419                             0x7fff8000u,    // Last word of +infinity
420                             0x7fffc000u,    // Last word of quiet NaN
421                             0x7fff9000u,    // Last word of signaling NaN
422 #endif
423                             false,          // Doesn't conform to iec559
424                             round_to_nearest>
425 {
426 public:
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.
432 };
433
434 } // namespace std
435
436 #endif /* BOOST_SGI_CPP_LIMITS */
437
438 // Local Variables:
439 // mode:C++
440 // End:
441
442
443