]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/limits.hpp
update from boost cvs
[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  * 10 Aug 2001:
24  *    Added MIPS (big endian) to the big endian family. (Jens Maurer)
25  * 13 Apr 2001:
26  *    Added powerpc to the big endian family. (Jeremy Siek)
27  * 5 Apr 2001:
28  *    Added sparc (big endian) processor support (John Maddock).
29  * Initial sub:
30  *      Modified by Jens Maurer for gcc 2.95 on x86.
31  */
32
33 #ifndef BOOST_SGI_CPP_LIMITS
34 #define BOOST_SGI_CPP_LIMITS
35
36 #include <climits>
37 #include <cfloat>
38 #include <cwchar>             // for WCHAR_MIN and WCHAR_MAX
39 #include <boost/config.hpp>
40
41 #if defined(__sparc) || defined(__sparc__) || defined(__powerpc__) || defined(__ppc__) || defined(__hppa) || defined(_MIPSEB)
42 #define BOOST_BIG_ENDIAN
43 #elif defined(__i386__)
44 #define BOOST_LITTLE_ENDIAN
45 #else
46 #error The file boost/detail/limits.hpp needs to be set up for your CPU type.
47 #endif
48
49 namespace std {
50
51 enum float_round_style {
52   round_indeterminate       = -1,
53   round_toward_zero         =  0,
54   round_to_nearest          =  1,
55   round_toward_infinity     =  2,
56   round_toward_neg_infinity =  3
57 };
58
59 enum float_denorm_style {
60   denorm_indeterminate = -1,
61   denorm_absent        =  0,
62   denorm_present       =  1
63 };
64
65 // The C++ standard (section 18.2.1) requires that some of the members of
66 // numeric_limits be static const data members that are given constant-
67 // initializers within the class declaration.  On compilers where the
68 // BOOST_NO_INCLASS_MEMBER_INITIALIZATION macro is defined, it is impossible to write
69 // a standard-conforming numeric_limits class.
70 //
71 // There are two possible workarounds: either initialize the data
72 // members outside the class, or change them from data members to
73 // enums.  Neither workaround is satisfactory: the former makes it
74 // impossible to use the data members in constant-expressions, and the
75 // latter means they have the wrong type and that it is impossible to
76 // take their addresses.  We choose the former workaround.
77
78 #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
79 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
80   enum { __mem_name = __mem_value }
81 #else /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
82 # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
83   static const __mem_type __mem_name = __mem_value
84 #endif /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
85
86 // Base class for all specializations of numeric_limits.
87
88 template <class __number>
89 class _Numeric_limits_base {
90 public:
91   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
92
93   static __number min() throw() { return __number(); }
94   static __number max() throw() { return __number(); }
95
96   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   0);
97   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
98
99   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  false);
100   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
101   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   false);
102
103   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
104
105   static __number epsilon() throw()     { return __number(); }
106   static __number round_error() throw() { return __number(); }
107
108   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   0);
109   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
110   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   0);
111   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
112
113   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      false);
114   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN,     false);
115   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
116   BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
117                               has_denorm,
118                               denorm_absent);
119   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
120
121   static __number infinity() throw()      { return __number(); }
122   static __number quiet_NaN() throw()     { return __number(); }
123   static __number signaling_NaN() throw() { return __number(); }
124   static __number denorm_min() throw()    { return __number(); }
125
126   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559,  false);
127   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
128   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo,  false);
129
130   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps,            false);
131   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before,  false);
132   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
133                               round_style,
134                               round_toward_zero);
135 };
136
137 // Base class for integers.
138
139 template <class _Int,
140           _Int __imin,
141           _Int __imax,
142           int __idigits = -1>
143 class _Integer_limits : public _Numeric_limits_base<_Int> 
144 {
145 public:
146   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
147
148   static _Int min() throw() { return __imin; }
149   static _Int max() throw() { return __imax; }
150
151   BOOST_STL_DECLARE_LIMITS_MEMBER(int,
152                               digits,
153                               (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
154                                                    - (__imin == 0 ? 0 : 1) 
155                                               : __idigits);
156   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000); 
157                                 // log 2 = 0.301029995664...
158
159   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  __imin != 0);
160   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
161   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   true);
162   BOOST_STL_DECLARE_LIMITS_MEMBER(int,  radix,      2);
163
164   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
165   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
166 };
167
168 #if defined(BOOST_BIG_ENDIAN)
169
170  template<class Number, unsigned int Word>
171  struct float_helper{
172   static Number get_word() throw() {
173     // sizeof(long double) == 16
174     const unsigned int _S_word[4] = { Word, 0, 0, 0 };
175     return *reinterpret_cast<const Number*>(&_S_word);
176   } 
177 };
178
179 #else
180
181  template<class Number, unsigned int Word>
182  struct float_helper{
183   static Number get_word() throw() {
184     // sizeof(long double) == 12, but only 10 bytes significant
185     const unsigned int _S_word[4] = { 0, 0, 0, Word };
186     return *reinterpret_cast<const Number*>(
187         reinterpret_cast<const char *>(&_S_word)+16-
188                 (sizeof(Number) == 12 ? 10 : sizeof(Number)));
189   } 
190 };
191
192 #endif
193
194 // Base class for floating-point numbers.
195 template <class __number,
196          int __Digits, int __Digits10,
197          int __MinExp, int __MaxExp,
198          int __MinExp10, int __MaxExp10,
199          unsigned int __InfinityWord,
200          unsigned int __QNaNWord, unsigned int __SNaNWord,
201          bool __IsIEC559,
202          float_round_style __RoundStyle>
203 class _Floating_limits : public _Numeric_limits_base<__number>
204 {
205 public:
206   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
207
208   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   __Digits);
209   BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
210
211   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
212
213   BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
214
215   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   __MinExp);
216   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   __MaxExp);
217   BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
218   BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
219
220   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      true);
221   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN,     true);
222   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
223   BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
224                               has_denorm,
225                               denorm_indeterminate);
226   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);
227
228  
229   static __number infinity() throw() {
230     return float_helper<__number, __InfinityWord>::get_word();
231   }
232   static __number quiet_NaN() throw() {
233     return float_helper<__number,__QNaNWord>::get_word();
234   }
235   static __number signaling_NaN() throw() {
236     return float_helper<__number,__SNaNWord>::get_word();
237   }
238
239   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559,       __IsIEC559);
240   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded,      true);
241   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps,           false /* was: true */ );
242   BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
243
244   BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
245 };
246
247 // Class numeric_limits
248
249 // The unspecialized class.
250
251 template<class T> 
252 class numeric_limits : public _Numeric_limits_base<T> {};
253
254 // Specializations for all built-in integral types.
255
256 template<>
257 class numeric_limits<bool>
258   : public _Integer_limits<bool, false, true, 0>
259 {};
260
261 template<>
262 class numeric_limits<char>
263   : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
264 {};
265
266 template<>
267 class numeric_limits<signed char>
268   : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
269 {};
270
271 template<>
272 class numeric_limits<unsigned char>
273   : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
274 {};
275
276 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
277 #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
278 #if !defined(_WIN32) && !defined(__CYGWIN__)
279 template<>
280 class numeric_limits<wchar_t>
281   : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
282 {};
283 #else
284 template<>
285 class numeric_limits<wchar_t>
286   : public _Integer_limits<wchar_t, 0, USHRT_MAX>
287 {};
288 #endif
289 #else
290 template<>
291 class numeric_limits<wchar_t>
292   : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
293 {};
294 #endif
295 #endif
296
297 template<>
298 class numeric_limits<short>
299   : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
300 {};
301
302 template<>
303 class numeric_limits<unsigned short>
304   : public _Integer_limits<unsigned short, 0, USHRT_MAX>
305 {};
306
307 template<>
308 class numeric_limits<int>
309   : public _Integer_limits<int, INT_MIN, INT_MAX>
310 {};
311
312 template<>
313 class numeric_limits<unsigned int>
314   : public _Integer_limits<unsigned int, 0, UINT_MAX>
315 {};
316
317 template<>
318 class numeric_limits<long>
319   : public _Integer_limits<long, LONG_MIN, LONG_MAX>
320 {};
321
322 template<>
323 class numeric_limits<unsigned long>
324   : public _Integer_limits<unsigned long, 0, ULONG_MAX>
325 {};
326
327 #ifdef __GNUC__
328
329 // Some compilers have long long, but don't define the
330 // LONGLONG_MIN and LONGLONG_MAX macros in limits.h.  This
331 // assumes that long long is 64 bits.
332 #if !defined(LONGLONG_MIN) && !defined(LONGLONG_MAX) \
333                            && !defined(ULONGLONG_MAX)
334
335 #define ULONGLONG_MAX 0xffffffffffffffffLLU
336 #define LONGLONG_MAX 0x7fffffffffffffffLL
337 #define LONGLONG_MIN (-LONGLONG_MAX - 1)
338
339 #endif
340
341 template<>
342 class numeric_limits<long long>
343   : public _Integer_limits<long long, LONGLONG_MIN, LONGLONG_MAX>
344 {};
345
346 template<>
347 class numeric_limits<unsigned long long>
348   : public _Integer_limits<unsigned long long, 0, ULONGLONG_MAX>
349 {};
350
351 #endif /* __GNUC__ */
352
353 // Specializations for all built-in floating-point type.
354
355 template<> class numeric_limits<float>
356   : public _Floating_limits<float, 
357                             FLT_MANT_DIG,   // Binary digits of precision
358                             FLT_DIG,        // Decimal digits of precision
359                             FLT_MIN_EXP,    // Minimum exponent
360                             FLT_MAX_EXP,    // Maximum exponent
361                             FLT_MIN_10_EXP, // Minimum base 10 exponent
362                             FLT_MAX_10_EXP, // Maximum base 10 exponent
363 #if defined(BOOST_BIG_ENDIAN)
364                             0x7f80 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
365                             0x7f81 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
366                             0x7fc1 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
367 #else
368                             0x7f800000u,    // Last word of +infinity
369                             0x7f810000u,    // Last word of quiet NaN
370                             0x7fc10000u,    // Last word of signaling NaN
371 #endif
372                             true,           // conforms to iec559
373                             round_to_nearest>
374 {
375 public:
376   static float min() throw() { return FLT_MIN; }
377   static float denorm_min() throw() { return FLT_MIN; }
378   static float max() throw() { return FLT_MAX; }
379   static float epsilon() throw() { return FLT_EPSILON; }
380   static float round_error() throw() { return 0.5f; } // Units: ulps.
381 };
382
383 template<> class numeric_limits<double>
384   : public _Floating_limits<double, 
385                             DBL_MANT_DIG,   // Binary digits of precision
386                             DBL_DIG,        // Decimal digits of precision
387                             DBL_MIN_EXP,    // Minimum exponent
388                             DBL_MAX_EXP,    // Maximum exponent
389                             DBL_MIN_10_EXP, // Minimum base 10 exponent
390                             DBL_MAX_10_EXP, // Maximum base 10 exponent
391 #if defined(BOOST_BIG_ENDIAN)
392                             0x7ff0 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
393                             0x7ff1 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
394                             0x7ff9 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
395 #else
396                             0x7ff00000u,    // Last word of +infinity
397                             0x7ff10000u,    // Last word of quiet NaN
398                             0x7ff90000u,    // Last word of signaling NaN
399 #endif
400                             true,           // conforms to iec559
401                             round_to_nearest>
402 {
403 public:
404   static double min() throw() { return DBL_MIN; }
405   static double denorm_min() throw() { return DBL_MIN; }
406   static double max() throw() { return DBL_MAX; }
407   static double epsilon() throw() { return DBL_EPSILON; }
408   static double round_error() throw() { return 0.5; } // Units: ulps.
409 };
410
411 template<> class numeric_limits<long double>
412   : public _Floating_limits<long double, 
413                             LDBL_MANT_DIG,  // Binary digits of precision
414                             LDBL_DIG,       // Decimal digits of precision
415                             LDBL_MIN_EXP,   // Minimum exponent
416                             LDBL_MAX_EXP,   // Maximum exponent
417                             LDBL_MIN_10_EXP,// Minimum base 10 exponent
418                             LDBL_MAX_10_EXP,// Maximum base 10 exponent
419 #if defined(BOOST_BIG_ENDIAN)
420                             0x7ff0 << (sizeof(int)*CHAR_BIT-16),    // Last word of +infinity
421                             0x7ff1 << (sizeof(int)*CHAR_BIT-16),    // Last word of quiet NaN
422                             0x7ff9 << (sizeof(int)*CHAR_BIT-16),    // Last word of signaling NaN
423 #else
424                             0x7fff8000u,    // Last word of +infinity
425                             0x7fffc000u,    // Last word of quiet NaN
426                             0x7fff9000u,    // Last word of signaling NaN
427 #endif
428                             false,          // Doesn't conform to iec559
429                             round_to_nearest>
430 {
431 public:
432   static long double min() throw() { return LDBL_MIN; }
433   static long double denorm_min() throw() { return LDBL_MIN; }
434   static long double max() throw() { return LDBL_MAX; }
435   static long double epsilon() throw() { return LDBL_EPSILON; }
436   static long double round_error() throw() { return 4; } // Units: ulps.
437 };
438
439 } // namespace std
440
441 #endif /* BOOST_SGI_CPP_LIMITS */
442
443 // Local Variables:
444 // mode:C++
445 // End:
446
447
448