limits.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // Copyright 2001 John Maddock
  2. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  3. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. /*
  5. * Copyright (c) 1997
  6. * Silicon Graphics Computer Systems, Inc.
  7. *
  8. * Permission to use, copy, modify, distribute and sell this software
  9. * and its documentation for any purpose is hereby granted without fee,
  10. * provided that the above copyright notice appear in all copies and
  11. * that both that copyright notice and this permission notice appear
  12. * in supporting documentation. Silicon Graphics makes no
  13. * representations about the suitability of this software for any
  14. * purpose. It is provided "as is" without express or implied warranty.
  15. */
  16. /* NOTE: This is not portable code. Parts of numeric_limits<> are
  17. * inherently machine-dependent, and this file is written for the MIPS
  18. * architecture and the SGI MIPSpro C++ compiler. Parts of it (in
  19. * particular, some of the characteristics of floating-point types)
  20. * are almost certainly incorrect for any other platform.
  21. */
  22. /* The above comment is almost certainly out of date. This file works
  23. * on systems other than SGI MIPSpro C++ now.
  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. #ifndef BOOST_SGI_CPP_LIMITS
  39. #define BOOST_SGI_CPP_LIMITS
  40. #include <climits>
  41. #include <cfloat>
  42. #include <boost/config.hpp>
  43. #include <boost/detail/endian.hpp>
  44. #ifndef BOOST_NO_CWCHAR
  45. #include <cwchar> // for WCHAR_MIN and WCHAR_MAX
  46. #endif
  47. namespace std {
  48. enum float_round_style {
  49. round_indeterminate = -1,
  50. round_toward_zero = 0,
  51. round_to_nearest = 1,
  52. round_toward_infinity = 2,
  53. round_toward_neg_infinity = 3
  54. };
  55. enum float_denorm_style {
  56. denorm_indeterminate = -1,
  57. denorm_absent = 0,
  58. denorm_present = 1
  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. #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  73. # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
  74. enum { __mem_name = __mem_value }
  75. #else /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
  76. # define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
  77. static const __mem_type __mem_name = __mem_value
  78. #endif /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
  79. // Base class for all specializations of numeric_limits.
  80. template <class __number>
  81. class _Numeric_limits_base {
  82. public:
  83. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
  84. static __number min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
  85. static __number max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
  86. BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, 0);
  87. BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
  88. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, false);
  89. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
  90. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, false);
  91. BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
  92. static __number epsilon() throw() { return __number(); }
  93. static __number round_error() throw() { return __number(); }
  94. BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, 0);
  95. BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
  96. BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, 0);
  97. BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
  98. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, false);
  99. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, false);
  100. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
  101. BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
  102. has_denorm,
  103. denorm_absent);
  104. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
  105. static __number infinity() throw() { return __number(); }
  106. static __number quiet_NaN() throw() { return __number(); }
  107. static __number signaling_NaN() throw() { return __number(); }
  108. static __number denorm_min() throw() { return __number(); }
  109. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, false);
  110. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
  111. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, false);
  112. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false);
  113. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
  114. BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
  115. round_style,
  116. round_toward_zero);
  117. };
  118. // Base class for integers.
  119. template <class _Int,
  120. _Int __imin,
  121. _Int __imax,
  122. int __idigits = -1>
  123. class _Integer_limits : public _Numeric_limits_base<_Int>
  124. {
  125. public:
  126. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
  127. static _Int min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imin; }
  128. static _Int max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imax; }
  129. BOOST_STL_DECLARE_LIMITS_MEMBER(int,
  130. digits,
  131. (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
  132. - (__imin == 0 ? 0 : 1)
  133. : __idigits);
  134. BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000);
  135. // log 2 = 0.301029995664...
  136. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, __imin != 0);
  137. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
  138. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact, true);
  139. BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
  140. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
  141. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
  142. };
  143. #if defined(BOOST_BIG_ENDIAN)
  144. template<class Number, unsigned int Word>
  145. struct float_helper{
  146. static Number get_word() throw() {
  147. // sizeof(long double) == 16
  148. const unsigned int _S_word[4] = { Word, 0, 0, 0 };
  149. return *reinterpret_cast<const Number*>(&_S_word);
  150. }
  151. };
  152. #else
  153. template<class Number, unsigned int Word>
  154. struct float_helper{
  155. static Number get_word() throw() {
  156. // sizeof(long double) == 12, but only 10 bytes significant
  157. const unsigned int _S_word[4] = { 0, 0, 0, Word };
  158. return *reinterpret_cast<const Number*>(
  159. reinterpret_cast<const char *>(&_S_word)+16-
  160. (sizeof(Number) == 12 ? 10 : sizeof(Number)));
  161. }
  162. };
  163. #endif
  164. // Base class for floating-point numbers.
  165. template <class __number,
  166. int __Digits, int __Digits10,
  167. int __MinExp, int __MaxExp,
  168. int __MinExp10, int __MaxExp10,
  169. unsigned int __InfinityWord,
  170. unsigned int __QNaNWord, unsigned int __SNaNWord,
  171. bool __IsIEC559,
  172. float_round_style __RoundStyle>
  173. class _Floating_limits : public _Numeric_limits_base<__number>
  174. {
  175. public:
  176. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
  177. BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits, __Digits);
  178. BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
  179. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
  180. BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
  181. BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent, __MinExp);
  182. BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent, __MaxExp);
  183. BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
  184. BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
  185. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, true);
  186. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, true);
  187. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
  188. BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
  189. has_denorm,
  190. denorm_indeterminate);
  191. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
  192. static __number infinity() throw() {
  193. return float_helper<__number, __InfinityWord>::get_word();
  194. }
  195. static __number quiet_NaN() throw() {
  196. return float_helper<__number,__QNaNWord>::get_word();
  197. }
  198. static __number signaling_NaN() throw() {
  199. return float_helper<__number,__SNaNWord>::get_word();
  200. }
  201. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, __IsIEC559);
  202. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
  203. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps, false /* was: true */ );
  204. BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
  205. BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
  206. };
  207. // Class numeric_limits
  208. // The unspecialized class.
  209. template<class T>
  210. class numeric_limits : public _Numeric_limits_base<T> {};
  211. // Specializations for all built-in integral types.
  212. template<>
  213. class numeric_limits<bool>
  214. : public _Integer_limits<bool, false, true, 0>
  215. {};
  216. template<>
  217. class numeric_limits<char>
  218. : public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
  219. {};
  220. template<>
  221. class numeric_limits<signed char>
  222. : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
  223. {};
  224. template<>
  225. class numeric_limits<unsigned char>
  226. : public _Integer_limits<unsigned char, 0, UCHAR_MAX>
  227. {};
  228. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  229. template<>
  230. class numeric_limits<wchar_t>
  231. #if !defined(WCHAR_MAX) || !defined(WCHAR_MIN)
  232. #if defined(_WIN32) || defined(__CYGWIN__)
  233. : public _Integer_limits<wchar_t, 0, USHRT_MAX>
  234. #elif defined(__hppa)
  235. // wchar_t has "unsigned int" as the underlying type
  236. : public _Integer_limits<wchar_t, 0, UINT_MAX>
  237. #else
  238. // assume that wchar_t has "int" as the underlying type
  239. : public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
  240. #endif
  241. #else
  242. // we have WCHAR_MIN and WCHAR_MAX defined, so use it
  243. : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX>
  244. #endif
  245. {};
  246. #endif
  247. template<>
  248. class numeric_limits<short>
  249. : public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
  250. {};
  251. template<>
  252. class numeric_limits<unsigned short>
  253. : public _Integer_limits<unsigned short, 0, USHRT_MAX>
  254. {};
  255. template<>
  256. class numeric_limits<int>
  257. : public _Integer_limits<int, INT_MIN, INT_MAX>
  258. {};
  259. template<>
  260. class numeric_limits<unsigned int>
  261. : public _Integer_limits<unsigned int, 0, UINT_MAX>
  262. {};
  263. template<>
  264. class numeric_limits<long>
  265. : public _Integer_limits<long, LONG_MIN, LONG_MAX>
  266. {};
  267. template<>
  268. class numeric_limits<unsigned long>
  269. : public _Integer_limits<unsigned long, 0, ULONG_MAX>
  270. {};
  271. #ifdef __GNUC__
  272. // Some compilers have long long, but don't define the
  273. // LONGLONG_MIN and LONGLONG_MAX macros in limits.h. This
  274. // assumes that long long is 64 bits.
  275. #if !defined(LONGLONG_MAX) && !defined(ULONGLONG_MAX)
  276. # define ULONGLONG_MAX 0xffffffffffffffffLLU
  277. # define LONGLONG_MAX 0x7fffffffffffffffLL
  278. #endif
  279. #if !defined(LONGLONG_MIN)
  280. # define LONGLONG_MIN (-LONGLONG_MAX - 1)
  281. #endif
  282. #if !defined(ULONGLONG_MIN)
  283. # define ULONGLONG_MIN 0
  284. #endif
  285. #endif /* __GNUC__ */
  286. // Specializations for all built-in floating-point type.
  287. template<> class numeric_limits<float>
  288. : public _Floating_limits<float,
  289. FLT_MANT_DIG, // Binary digits of precision
  290. FLT_DIG, // Decimal digits of precision
  291. FLT_MIN_EXP, // Minimum exponent
  292. FLT_MAX_EXP, // Maximum exponent
  293. FLT_MIN_10_EXP, // Minimum base 10 exponent
  294. FLT_MAX_10_EXP, // Maximum base 10 exponent
  295. #if defined(BOOST_BIG_ENDIAN)
  296. 0x7f80 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
  297. 0x7f81 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
  298. 0x7fc1 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
  299. #else
  300. 0x7f800000u, // Last word of +infinity
  301. 0x7f810000u, // Last word of quiet NaN
  302. 0x7fc10000u, // Last word of signaling NaN
  303. #endif
  304. true, // conforms to iec559
  305. round_to_nearest>
  306. {
  307. public:
  308. static float min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return FLT_MIN; }
  309. static float denorm_min() throw() { return FLT_MIN; }
  310. static float max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return FLT_MAX; }
  311. static float epsilon() throw() { return FLT_EPSILON; }
  312. static float round_error() throw() { return 0.5f; } // Units: ulps.
  313. };
  314. template<> class numeric_limits<double>
  315. : public _Floating_limits<double,
  316. DBL_MANT_DIG, // Binary digits of precision
  317. DBL_DIG, // Decimal digits of precision
  318. DBL_MIN_EXP, // Minimum exponent
  319. DBL_MAX_EXP, // Maximum exponent
  320. DBL_MIN_10_EXP, // Minimum base 10 exponent
  321. DBL_MAX_10_EXP, // Maximum base 10 exponent
  322. #if defined(BOOST_BIG_ENDIAN)
  323. 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
  324. 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
  325. 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
  326. #else
  327. 0x7ff00000u, // Last word of +infinity
  328. 0x7ff10000u, // Last word of quiet NaN
  329. 0x7ff90000u, // Last word of signaling NaN
  330. #endif
  331. true, // conforms to iec559
  332. round_to_nearest>
  333. {
  334. public:
  335. static double min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return DBL_MIN; }
  336. static double denorm_min() throw() { return DBL_MIN; }
  337. static double max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return DBL_MAX; }
  338. static double epsilon() throw() { return DBL_EPSILON; }
  339. static double round_error() throw() { return 0.5; } // Units: ulps.
  340. };
  341. template<> class numeric_limits<long double>
  342. : public _Floating_limits<long double,
  343. LDBL_MANT_DIG, // Binary digits of precision
  344. LDBL_DIG, // Decimal digits of precision
  345. LDBL_MIN_EXP, // Minimum exponent
  346. LDBL_MAX_EXP, // Maximum exponent
  347. LDBL_MIN_10_EXP,// Minimum base 10 exponent
  348. LDBL_MAX_10_EXP,// Maximum base 10 exponent
  349. #if defined(BOOST_BIG_ENDIAN)
  350. 0x7ff0 << (sizeof(int)*CHAR_BIT-16), // Last word of +infinity
  351. 0x7ff1 << (sizeof(int)*CHAR_BIT-16), // Last word of quiet NaN
  352. 0x7ff9 << (sizeof(int)*CHAR_BIT-16), // Last word of signaling NaN
  353. #else
  354. 0x7fff8000u, // Last word of +infinity
  355. 0x7fffc000u, // Last word of quiet NaN
  356. 0x7fff9000u, // Last word of signaling NaN
  357. #endif
  358. false, // Doesn't conform to iec559
  359. round_to_nearest>
  360. {
  361. public:
  362. static long double min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return LDBL_MIN; }
  363. static long double denorm_min() throw() { return LDBL_MIN; }
  364. static long double max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return LDBL_MAX; }
  365. static long double epsilon() throw() { return LDBL_EPSILON; }
  366. static long double round_error() throw() { return 4; } // Units: ulps.
  367. };
  368. } // namespace std
  369. #endif /* BOOST_SGI_CPP_LIMITS */
  370. // Local Variables:
  371. // mode:C++
  372. // End: