]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/limits.hpp
add _POWER
[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__)
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 // Deal with min/max for MinGW
98 #ifdef min
99 # undef min
100 #endif
101
102 #ifdef max
103 # undef max
104 #endif
105
106 // Base class for all specializations of numeric_limits.
107 template <class __number>
108 class _Numeric_limits_base {
109 public:
110   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
111
112   static __number min() throw() { return __number(); }
113   static __number max() throw() { return __number(); }
114
115   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   0);
116   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
117
118   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  false);
119   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
120   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   false);
121
122   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
123
124   static __number epsilon() throw()     { return __number(); }
125   static __number round_error() throw() { return __number(); }
126
127   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   0);
128   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
129   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   0);
130   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
131
132   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      false);
133   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN,     false);
134   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
135   BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
136                               has_denorm,
137                               denorm_absent);
138   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
139
140   static __number infinity() throw()      { return __number(); }
141   static __number quiet_NaN() throw()     { return __number(); }
142   static __number signaling_NaN() throw() { return __number(); }
143   static __number denorm_min() throw()    { return __number(); }
144
145   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559,  false);
146   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
147   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo,  false);
148
149   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps,            false);
150   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before,  false);
151   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
152                               round_style,
153                               round_toward_zero);
154 };
155
156 // Base class for integers.
157
158 template <class _Int,
159           _Int __imin,
160           _Int __imax,
161           int __idigits = -1>
162 class _Integer_limits : public _Numeric_limits_base<_Int>
163 {
164 public:
165   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
166
167   static _Int min() throw() { return __imin; }
168   static _Int max() throw() { return __imax; }
169
170   BOOST_STL_DECLARE_LIMITS_MEMBER(int,
171                               digits,
172                               (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
173                                                    - (__imin == 0 ? 0 : 1)
174                                               : __idigits);
175   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000);
176                                 // log 2 = 0.301029995664...
177
178   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  __imin != 0);
179   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
180   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   true);
181   BOOST_STL_DECLARE_LIMITS_MEMBER(int,  radix,      2);
182
183   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
184   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
185 };
186
187 #if defined(BOOST_BIG_ENDIAN)
188
189  template<class Number, unsigned int Word>
190  struct float_helper{
191   static Number get_word() throw() {
192     // sizeof(long double) == 16
193     const unsigned int _S_word[4] = { Word, 0, 0, 0 };
194     return *reinterpret_cast<const Number*>(&_S_word);
195   }
196 };
197
198 #else
199
200  template<class Number, unsigned int Word>
201  struct float_helper{
202   static Number get_word() throw() {
203     // sizeof(long double) == 12, but only 10 bytes significant
204     const unsigned int _S_word[4] = { 0, 0, 0, Word };
205     return *reinterpret_cast<const Number*>(
206         reinterpret_cast<const char *>(&_S_word)+16-
207                 (sizeof(Number) == 12 ? 10 : sizeof(Number)));
208   }
209 };
210
211 #endif
212
213 // Base class for floating-point numbers.
214 template <class __number,
215          int __Digits, int __Digits10,
216          int __MinExp, int __MaxExp,
217          int __MinExp10, int __MaxExp10,
218          unsigned int __InfinityWord,
219          unsigned int __QNaNWord, unsigned int __SNaNWord,
220          bool __IsIEC559,
221          float_round_style __RoundStyle>
222 class _Floating_limits : public _Numeric_limits_base<__number>
223 {
224 public:
225   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
226
227   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   __Digits);
228   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
229
230   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
231
232   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
233
234   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   __MinExp);
235   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   __MaxExp);
236   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
237   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
238
239   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      true);
240   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN,     true);
241   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
242   BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
243                               has_denorm,
244                               denorm_indeterminate);
245   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
246
247
248   static __number infinity() throw() {
249     return float_helper<__number, __InfinityWord>::get_word();
250   }
251   static __number quiet_NaN() throw() {
252     return float_helper<__number,__QNaNWord>::get_word();
253   }
254   static __number signaling_NaN() throw() {
255     return float_helper<__number,__SNaNWord>::get_word();
256   }
257
258   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559,       __IsIEC559);
259   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded,      true);
260   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps,           false /* was: true */ );
261   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
262
263   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
264 };
265
266 // Class numeric_limits
267
268 // The unspecialized class.
269
270 template<class T>
271 class numeric_limits : public _Numeric_limits_base<T> {};
272
273 // Specializations for all built-in integral types.
274
275 template<>
276 class numeric_limits<bool>
277   : public _Integer_limits<bool, false, true, 0>
278 {};
279
280 template<>
281 class numeric_limits<char>
282   : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
283 {};
284
285 template<>
286 class numeric_limits<signed char>
287   : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
288 {};
289
290 template<>
291 class numeric_limits<unsigned char>
292   : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
293 {};
294
295 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
296 template<>
297 class numeric_limits<wchar_t>
298 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
299 #if defined(_WIN32) || defined(__CYGWIN__)
300   : public _Integer_limits<wchar_t, 0, USHRT_MAX>
301 #elif defined(__hppa)
302 // wchar_t has "unsigned int" as the underlying type
303   : public _Integer_limits<wchar_t, 0, UINT_MAX>
304 #else
305 // assume that wchar_t has "int" as the underlying type
306   : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
307 #endif
308 #else
309 // we have WCHAR_MIN and WCHAR_MAX defined, so use it
310   : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
311 #endif
312 {};
313 #endif
314
315 template<>
316 class numeric_limits<short>
317   : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
318 {};
319
320 template<>
321 class numeric_limits<unsigned short>
322   : public _Integer_limits<unsigned short, 0, USHRT_MAX>
323 {};
324
325 template<>
326 class numeric_limits<int>
327   : public _Integer_limits<int, INT_MIN, INT_MAX>
328 {};
329
330 template<>
331 class numeric_limits<unsigned int>
332   : public _Integer_limits<unsigned int, 0, UINT_MAX>
333 {};
334
335 template<>
336 class numeric_limits<long>
337   : public _Integer_limits<long, LONG_MIN, LONG_MAX>
338 {};
339
340 template<>
341 class numeric_limits<unsigned long>
342   : public _Integer_limits<unsigned long, 0, ULONG_MAX>
343 {};
344
345 #ifdef __GNUC__
346
347 // Some compilers have long long, but don't define the
348 // LONGLONG_MIN and LONGLONG_MAX macros in limits.h.  This
349 // assumes that long long is 64 bits.
350 #if !defined(LONGLONG_MAX) && !defined(ULONGLONG_MAX)
351
352 # define ULONGLONG_MAX 0xffffffffffffffffLLU
353 # define LONGLONG_MAX 0x7fffffffffffffffLL
354
355 #endif
356
357 #if !defined(LONGLONG_MIN)
358 # define LONGLONG_MIN (-LONGLONG_MAX - 1)
359 #endif
360
361 template<>
362 class numeric_limits<long long>
363   : public _Integer_limits<long long, LONGLONG_MIN, LONGLONG_MAX>
364 {};
365
366 template<>
367 class numeric_limits<unsigned long long>
368   : public _Integer_limits<unsigned long long, 0, ULONGLONG_MAX>
369 {};
370
371 #endif /* __GNUC__ */
372
373 // Specializations for all built-in floating-point type.
374
375 template<> class numeric_limits<float>
376   : public _Floating_limits<float,
377                             FLT_MANT_DIG,   // Binary digits of precision
378                             FLT_DIG,        // Decimal digits of precision
379                             FLT_MIN_EXP,    // Minimum exponent
380                             FLT_MAX_EXP,    // Maximum exponent
381                             FLT_MIN_10_EXP, // Minimum base 10 exponent
382                             FLT_MAX_10_EXP, // Maximum base 10 exponent
383 #if defined(BOOST_BIG_ENDIAN)
384                             0x7f80 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
385                             0x7f81 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
386                             0x7fc1 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
387 #else
388                             0x7f800000u,    // Last word of +infinity
389                             0x7f810000u,    // Last word of quiet NaN
390                             0x7fc10000u,    // Last word of signaling NaN
391 #endif
392                             true,           // conforms to iec559
393                             round_to_nearest>
394 {
395 public:
396   static float min() throw() { return FLT_MIN; }
397   static float denorm_min() throw() { return FLT_MIN; }
398   static float max() throw() { return FLT_MAX; }
399   static float epsilon() throw() { return FLT_EPSILON; }
400   static float round_error() throw() { return 0.5f; } // Units: ulps.
401 };
402
403 template<> class numeric_limits<double>
404   : public _Floating_limits<double,
405                             DBL_MANT_DIG,   // Binary digits of precision
406                             DBL_DIG,        // Decimal digits of precision
407                             DBL_MIN_EXP,    // Minimum exponent
408                             DBL_MAX_EXP,    // Maximum exponent
409                             DBL_MIN_10_EXP, // Minimum base 10 exponent
410                             DBL_MAX_10_EXP, // Maximum base 10 exponent
411 #if defined(BOOST_BIG_ENDIAN)
412                             0x7ff0 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
413                             0x7ff1 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
414                             0x7ff9 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
415 #else
416                             0x7ff00000u,    // Last word of +infinity
417                             0x7ff10000u,    // Last word of quiet NaN
418                             0x7ff90000u,    // Last word of signaling NaN
419 #endif
420                             true,           // conforms to iec559
421                             round_to_nearest>
422 {
423 public:
424   static double min() throw() { return DBL_MIN; }
425   static double denorm_min() throw() { return DBL_MIN; }
426   static double max() throw() { return DBL_MAX; }
427   static double epsilon() throw() { return DBL_EPSILON; }
428   static double round_error() throw() { return 0.5; } // Units: ulps.
429 };
430
431 template<> class numeric_limits<long double>
432   : public _Floating_limits<long double,
433                             LDBL_MANT_DIG,  // Binary digits of precision
434                             LDBL_DIG,       // Decimal digits of precision
435                             LDBL_MIN_EXP,   // Minimum exponent
436                             LDBL_MAX_EXP,   // Maximum exponent
437                             LDBL_MIN_10_EXP,// Minimum base 10 exponent
438                             LDBL_MAX_10_EXP,// Maximum base 10 exponent
439 #if defined(BOOST_BIG_ENDIAN)
440                             0x7ff0 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
441                             0x7ff1 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
442                             0x7ff9 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
443 #else
444                             0x7fff8000u,    // Last word of +infinity
445                             0x7fffc000u,    // Last word of quiet NaN
446                             0x7fff9000u,    // Last word of signaling NaN
447 #endif
448                             false,          // Doesn't conform to iec559
449                             round_to_nearest>
450 {
451 public:
452   static long double min() throw() { return LDBL_MIN; }
453   static long double denorm_min() throw() { return LDBL_MIN; }
454   static long double max() throw() { return LDBL_MAX; }
455   static long double epsilon() throw() { return LDBL_EPSILON; }
456   static long double round_error() throw() { return 4; } // Units: ulps.
457 };
458
459 } // namespace std
460
461 #endif /* BOOST_SGI_CPP_LIMITS */
462
463 // Local Variables:
464 // mode:C++
465 // End: