_limits.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright (c) 1998,1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #ifndef _STLP_LIMITS_C
  19. #define _STLP_LIMITS_C
  20. #ifndef _STLP_INTERNAL_LIMITS
  21. # include <stl/_limits.h>
  22. #endif
  23. //==========================================================
  24. // numeric_limits static members
  25. //==========================================================
  26. _STLP_BEGIN_NAMESPACE
  27. _STLP_MOVE_TO_PRIV_NAMESPACE
  28. #if !defined (_STLP_STATIC_CONST_INIT_BUG) && !defined (_STLP_NO_STATIC_CONST_DEFINITION)
  29. # define __declare_numeric_base_member(__type, __mem) \
  30. template <class __number> \
  31. const __type _Numeric_limits_base<__number>:: __mem
  32. __declare_numeric_base_member(bool, is_specialized);
  33. __declare_numeric_base_member(int, digits);
  34. __declare_numeric_base_member(int, digits10);
  35. __declare_numeric_base_member(bool, is_signed);
  36. __declare_numeric_base_member(bool, is_integer);
  37. __declare_numeric_base_member(bool, is_exact);
  38. __declare_numeric_base_member(int, radix);
  39. __declare_numeric_base_member(int, min_exponent);
  40. __declare_numeric_base_member(int, max_exponent);
  41. __declare_numeric_base_member(int, min_exponent10);
  42. __declare_numeric_base_member(int, max_exponent10);
  43. __declare_numeric_base_member(bool, has_infinity);
  44. __declare_numeric_base_member(bool, has_quiet_NaN);
  45. __declare_numeric_base_member(bool, has_signaling_NaN);
  46. __declare_numeric_base_member(float_denorm_style, has_denorm);
  47. __declare_numeric_base_member(bool, has_denorm_loss);
  48. __declare_numeric_base_member(bool, is_iec559);
  49. __declare_numeric_base_member(bool, is_bounded);
  50. __declare_numeric_base_member(bool, is_modulo);
  51. __declare_numeric_base_member(bool, traps);
  52. __declare_numeric_base_member(bool, tinyness_before);
  53. __declare_numeric_base_member(float_round_style, round_style);
  54. # undef __declare_numeric_base_member
  55. # define __declare_integer_limits_member(__type, __mem) \
  56. template <class _Int, _STLP_LIMITS_MIN_TYPE __imin, _STLP_LIMITS_MAX_TYPE __imax, int __idigits, bool __ismod> \
  57. const __type _Integer_limits<_Int, __imin, __imax, __idigits, __ismod>:: __mem
  58. __declare_integer_limits_member(bool, is_specialized);
  59. __declare_integer_limits_member(int, digits);
  60. __declare_integer_limits_member(int, digits10);
  61. __declare_integer_limits_member(bool, is_signed);
  62. __declare_integer_limits_member(bool, is_integer);
  63. __declare_integer_limits_member(bool, is_exact);
  64. __declare_integer_limits_member(int, radix);
  65. __declare_integer_limits_member(bool, is_bounded);
  66. __declare_integer_limits_member(bool, is_modulo);
  67. # undef __declare_integer_limits_member
  68. # if defined (__GNUC__) && (__GNUC__ != 2 || __GNUC_MINOR__ > 96) && (__GNUC__ != 3 || __GNUC_MINOR__ == 0) && (__GNUC__ <= 3)
  69. _STLP_MOVE_TO_STD_NAMESPACE
  70. # define __declare_numeric_limits_member(__integer) \
  71. _STLP_TEMPLATE_NULL const int numeric_limits<__integer>::digits; \
  72. _STLP_TEMPLATE_NULL const int numeric_limits<__integer>::digits10; \
  73. _STLP_TEMPLATE_NULL const int numeric_limits<__integer>::radix; \
  74. _STLP_TEMPLATE_NULL const bool numeric_limits<__integer>::is_specialized; \
  75. _STLP_TEMPLATE_NULL const bool numeric_limits<__integer>::is_signed; \
  76. _STLP_TEMPLATE_NULL const bool numeric_limits<__integer>::is_integer; \
  77. _STLP_TEMPLATE_NULL const bool numeric_limits<__integer>::is_exact; \
  78. _STLP_TEMPLATE_NULL const bool numeric_limits<__integer>::is_bounded; \
  79. _STLP_TEMPLATE_NULL const bool numeric_limits<__integer>::is_modulo
  80. __declare_numeric_limits_member(_STLP_LONG_LONG);
  81. __declare_numeric_limits_member(unsigned _STLP_LONG_LONG);
  82. # undef __declare_numeric_limits_member
  83. _STLP_MOVE_TO_PRIV_NAMESPACE
  84. # endif
  85. # define __declare_float_limits_member(__type, __mem) \
  86. template <class __number, \
  87. int __Digits, int __Digits10, \
  88. int __MinExp, int __MaxExp, \
  89. int __MinExp10, int __MaxExp10, \
  90. bool __IsIEC559, \
  91. float_denorm_style __DenormStyle, \
  92. float_round_style __RoundStyle> \
  93. const __type _Floating_limits< __number, __Digits, __Digits10, \
  94. __MinExp, __MaxExp, __MinExp10, __MaxExp10, \
  95. __IsIEC559, __DenormStyle, __RoundStyle>::\
  96. __mem
  97. __declare_float_limits_member(bool, is_specialized);
  98. __declare_float_limits_member(int, digits);
  99. __declare_float_limits_member(int, digits10);
  100. __declare_float_limits_member(bool, is_signed);
  101. __declare_float_limits_member(int, radix);
  102. __declare_float_limits_member(int, min_exponent);
  103. __declare_float_limits_member(int, max_exponent);
  104. __declare_float_limits_member(int, min_exponent10);
  105. __declare_float_limits_member(int, max_exponent10);
  106. __declare_float_limits_member(bool, has_infinity);
  107. __declare_float_limits_member(bool, has_quiet_NaN);
  108. __declare_float_limits_member(bool, has_signaling_NaN);
  109. __declare_float_limits_member(float_denorm_style, has_denorm);
  110. __declare_float_limits_member(bool, has_denorm_loss);
  111. __declare_float_limits_member(bool, is_iec559);
  112. __declare_float_limits_member(bool, is_bounded);
  113. __declare_float_limits_member(bool, traps);
  114. __declare_float_limits_member(bool, tinyness_before);
  115. __declare_float_limits_member(float_round_style, round_style);
  116. # undef __declare_float_limits_member
  117. #endif
  118. #if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
  119. # if defined (__GNUC__) || defined (__BORLANDC__)
  120. # define _STLP_ADDITIONAL_OPEN_BRACKET {
  121. # define _STLP_ADDITIONAL_CLOSE_BRACKET }
  122. # else
  123. # define _STLP_ADDITIONAL_OPEN_BRACKET
  124. # define _STLP_ADDITIONAL_CLOSE_BRACKET
  125. # endif
  126. /* The following code has been extracted from the boost libraries (www.boost.org) and
  127. * adapted with the STLport portability macros. Advantage on previous technique is that
  128. * computation of infinity and NaN values is only based on big/little endianess, compiler
  129. * float, double or long double representation is taken into account thanks to the sizeof
  130. * operator. */
  131. template<class _Number, unsigned short _Word>
  132. struct float_helper {
  133. union _WordsNumber {
  134. unsigned short _Words[8];
  135. _Number _num;
  136. };
  137. static _Number get_word_higher() _STLP_NOTHROW {
  138. _WordsNumber __tmp = { _STLP_ADDITIONAL_OPEN_BRACKET _Word, 0, 0, 0, 0, 0, 0, 0 _STLP_ADDITIONAL_CLOSE_BRACKET };
  139. return __tmp._num;
  140. }
  141. static _Number get_word_lower() _STLP_NOTHROW {
  142. _WordsNumber __tmp = { _STLP_ADDITIONAL_OPEN_BRACKET 0, 0, 0, 0, 0, 0, 0, 0 _STLP_ADDITIONAL_CLOSE_BRACKET };
  143. __tmp._Words[(sizeof(_Number) >= 12 ? 10 : sizeof(_Number)) / sizeof(unsigned short) - 1] = _Word;
  144. return __tmp._num;
  145. }
  146. static _Number get_from_last_word() _STLP_NOTHROW {
  147. # if defined (_STLP_BIG_ENDIAN)
  148. return get_word_higher();
  149. # else /* _STLP_LITTLE_ENDIAN */
  150. return get_word_lower();
  151. # endif
  152. }
  153. static _Number get_from_first_word() _STLP_NOTHROW {
  154. # if defined (_STLP_BIG_ENDIAN)
  155. return get_word_lower();
  156. # else /* _STLP_LITTLE_ENDIAN */
  157. return get_word_higher();
  158. # endif
  159. }
  160. };
  161. # if !defined (_STLP_NO_LONG_DOUBLE) && !defined (_STLP_BIG_ENDIAN)
  162. template<class _Number, unsigned short _Word1, unsigned short _Word2>
  163. struct float_helper2 {
  164. union _WordsNumber {
  165. unsigned short _Words[8];
  166. _Number _num;
  167. };
  168. //static _Number get_word_higher() _STLP_NOTHROW {
  169. // _WordsNumber __tmp = { _STLP_ADDITIONAL_OPEN_BRACKET _Word1, _Word2, 0, 0, 0, 0, 0, 0 _STLP_ADDITIONAL_CLOSE_BRACKET };
  170. // return __tmp._num;
  171. //}
  172. static _Number get_word_lower() _STLP_NOTHROW {
  173. _WordsNumber __tmp = { _STLP_ADDITIONAL_OPEN_BRACKET 0, 0, 0, 0, 0, 0, 0, 0 _STLP_ADDITIONAL_CLOSE_BRACKET };
  174. __tmp._Words[(sizeof(_Number) >= 12 ? 10 : sizeof(_Number)) / sizeof(unsigned short) - 2] = _Word1;
  175. __tmp._Words[(sizeof(_Number) >= 12 ? 10 : sizeof(_Number)) / sizeof(unsigned short) - 1] = _Word2;
  176. return __tmp._num;
  177. }
  178. static _Number get_from_last_word() _STLP_NOTHROW {
  179. //# if defined (_STLP_BIG_ENDIAN)
  180. // return get_word_higher();
  181. //# else /* _STLP_LITTLE_ENDIAN */
  182. return get_word_lower();
  183. //# endif
  184. }
  185. };
  186. # endif
  187. /* Former values kept in case moving to boost code has introduce a regression on
  188. * some platform. */
  189. #if 0
  190. # if defined (_STLP_BIG_ENDIAN)
  191. # if defined (__OS400__)
  192. # define _STLP_FLOAT_INF_REP { 0x7f80, 0 }
  193. # define _STLP_FLOAT_QNAN_REP { 0xffc0, 0 }
  194. # define _STLP_FLOAT_SNAN_REP { 0xff80, 0 }
  195. # define _STLP_DOUBLE_INF_REP { 0x7ff0, 0, 0, 0 }
  196. # define _STLP_DOUBLE_QNAN_REP { 0xfff8, 0, 0, 0 }
  197. # define _STLP_DOUBLE_SNAN_REP { 0xfff0, 0, 0, 0 }
  198. # define _STLP_LDOUBLE_INF_REP { 0x7ff0, 0, 0, 0, 0, 0, 0, 0 }
  199. # define _STLP_LDOUBLE_QNAN_REP { 0xfff8, 0, 0, 0, 0, 0, 0, 0 }
  200. # define _STLP_LDOUBLE_SNAN_REP { 0xfff0, 0, 0, 0, 0, 0, 0, 0 }
  201. # else /* __OS400__ */
  202. # define _STLP_FLOAT_INF_REP { 0x7f80, 0 }
  203. # define _STLP_FLOAT_QNAN_REP { 0x7fc1, 0 }
  204. # define _STLP_FLOAT_SNAN_REP { 0x7f81, 0 }
  205. # define _STLP_DOUBLE_INF_REP { 0x7ff0, 0, 0, 0 }
  206. # define _STLP_DOUBLE_QNAN_REP { 0x7ff9, 0, 0, 0 }
  207. # define _STLP_DOUBLE_SNAN_REP { 0x7ff1, 0, 0, 0 }
  208. # define _STLP_LDOUBLE_INF_REP { 0x7ff0, 0, 0, 0, 0, 0, 0, 0 }
  209. # define _STLP_LDOUBLE_QNAN_REP { 0x7ff1, 0, 0, 0, 0, 0, 0, 0 }
  210. # define _STLP_LDOUBLE_SNAN_REP { 0x7ff9, 0, 0, 0, 0, 0, 0, 0 }
  211. # endif /* __OS400__ */
  212. # else /* _STLP_LITTLE_ENDIAN */
  213. # if defined(__DECCXX)
  214. # define _STLP_FLOAT_INF_REP { 0, 0x7f80 }
  215. # define _STLP_FLOAT_QNAN_REP { 0, 0xffc0 }
  216. # define _STLP_FLOAT_SNAN_REP { 0x5555, 0x7f85 }
  217. # define _STLP_DOUBLE_INF_REP { 0, 0, 0, 0x7ff0 }
  218. # define _STLP_DOUBLE_QNAN_REP { 0, 0, 0, 0xfff8 }
  219. # define _STLP_DOUBLE_SNAN_REP { 0x5555, 0x5555, 0x5555, 0x7ff5 }
  220. # define _STLP_LDOUBLE_INF_REP { 0, 0, 0, 0, 0, 0, 0, 0x7fff }
  221. # define _STLP_LDOUBLE_QNAN_REP { 0, 0, 0, 0, 0, 0, 0x8000, 0xffff }
  222. # define _STLP_LDOUBLE_SNAN_REP { 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x7fff}
  223. # else
  224. # define _STLP_FLOAT_INF_REP { 0, 0x7f80 }
  225. # define _STLP_FLOAT_QNAN_REP { 0, 0x7fc0 }
  226. # define _STLP_FLOAT_SNAN_REP { 0, 0x7fa0 }
  227. # define _STLP_DOUBLE_INF_REP { 0, 0, 0, 0x7ff0 }
  228. # define _STLP_DOUBLE_QNAN_REP { 0, 0, 0, 0x7ff8 }
  229. # define _STLP_DOUBLE_SNAN_REP { 0, 0, 0, 0x7ff4 }
  230. # if defined (_STLP_MSVC) || defined (__ICL)
  231. # define _STLP_LDOUBLE_INF_REP { 0, 0, 0, 0x7FF0, 0 }
  232. # define _STLP_LDOUBLE_QNAN_REP { 0, 0, 0, 0xFFF8, 0 }
  233. # define _STLP_LDOUBLE_SNAN_REP { 0, 0, 0, 0xFFF8, 0 }
  234. # elif defined (__BORLANDC__)
  235. # define _STLP_LDOUBLE_INF_REP { 0, 0, 0, 0x8000, 0x7fff }
  236. # define _STLP_LDOUBLE_QNAN_REP { 0, 0, 0, 0xc000, 0x7fff }
  237. # define _STLP_LDOUBLE_SNAN_REP { 0, 0, 0, 0xa000, 0x7fff }
  238. # else
  239. # define _STLP_LDOUBLE_INF_REP { 0, 0, 0, 0x8000, 0x7fff, 0 }
  240. # define _STLP_LDOUBLE_QNAN_REP { 0, 0, 0, 0xa000, 0x7fff, 0 }
  241. # define _STLP_LDOUBLE_SNAN_REP { 0, 0, 0, 0xc000, 0x7fff, 0 }
  242. # endif
  243. # endif
  244. # endif
  245. union _F_rep {
  246. unsigned short rep[2];
  247. float val;
  248. };
  249. union _D_rep {
  250. unsigned short rep[4];
  251. double val;
  252. };
  253. # ifndef _STLP_NO_LONG_DOUBLE
  254. union _LD_rep {
  255. unsigned short rep[8];
  256. long double val;
  257. };
  258. # endif
  259. #endif
  260. template <class __dummy>
  261. float _STLP_CALL _LimG<__dummy>::get_F_inf() {
  262. typedef float_helper<float, 0x7f80u> _FloatHelper;
  263. return _FloatHelper::get_from_last_word();
  264. }
  265. template <class __dummy>
  266. float _STLP_CALL _LimG<__dummy>::get_F_qNaN() {
  267. typedef float_helper<float, 0x7f81u> _FloatHelper;
  268. return _FloatHelper::get_from_last_word();
  269. }
  270. template <class __dummy>
  271. float _STLP_CALL _LimG<__dummy>::get_F_sNaN() {
  272. typedef float_helper<float, 0x7fc1u> _FloatHelper;
  273. return _FloatHelper::get_from_last_word();
  274. }
  275. template <class __dummy>
  276. float _STLP_CALL _LimG<__dummy>::get_F_denormMin() {
  277. typedef float_helper<float, 0x0001u> _FloatHelper;
  278. return _FloatHelper::get_from_first_word();
  279. }
  280. template <int __use_double_limits>
  281. class _NumericLimitsAccess;
  282. _STLP_TEMPLATE_NULL
  283. class _NumericLimitsAccess<1> {
  284. public:
  285. static double get_inf() {
  286. typedef float_helper<double, 0x7ff0u> _FloatHelper;
  287. return _FloatHelper::get_from_last_word();
  288. }
  289. static double get_qNaN() {
  290. typedef float_helper<double, 0x7ff1u> _FloatHelper;
  291. return _FloatHelper::get_from_last_word();
  292. }
  293. static double get_sNaN() {
  294. typedef float_helper<double, 0x7ff9u> _FloatHelper;
  295. return _FloatHelper::get_from_last_word();
  296. }
  297. };
  298. template <class __dummy>
  299. double _STLP_CALL _LimG<__dummy>::get_D_inf()
  300. { return _NumericLimitsAccess<1>::get_inf(); }
  301. template <class __dummy>
  302. double _STLP_CALL _LimG<__dummy>::get_D_qNaN()
  303. { return _NumericLimitsAccess<1>::get_qNaN(); }
  304. template <class __dummy>
  305. double _STLP_CALL _LimG<__dummy>::get_D_sNaN()
  306. { return _NumericLimitsAccess<1>::get_sNaN(); }
  307. template <class __dummy>
  308. double _STLP_CALL _LimG<__dummy>::get_D_denormMin() {
  309. typedef float_helper<double, 0x0001u> _FloatHelper;
  310. return _FloatHelper::get_from_first_word();
  311. }
  312. # if !defined (_STLP_NO_LONG_DOUBLE)
  313. _STLP_TEMPLATE_NULL
  314. class _NumericLimitsAccess<0> {
  315. public:
  316. static long double get_inf() {
  317. # if defined (_STLP_BIG_ENDIAN)
  318. typedef float_helper<long double, 0x7ff0u> _FloatHelper;
  319. # else
  320. typedef float_helper2<long double, 0x8000u, 0x7fffu> _FloatHelper;
  321. # endif
  322. return _FloatHelper::get_from_last_word();
  323. }
  324. static long double get_qNaN() {
  325. # if defined (_STLP_BIG_ENDIAN)
  326. typedef float_helper<long double, 0x7ff1u> _FloatHelper;
  327. # else
  328. typedef float_helper2<long double, 0xc000u, 0x7fffu> _FloatHelper;
  329. # endif
  330. return _FloatHelper::get_from_last_word();
  331. }
  332. static long double get_sNaN() {
  333. # if defined (_STLP_BIG_ENDIAN)
  334. typedef float_helper<long double, 0x7ff9u> _FloatHelper;
  335. # else
  336. typedef float_helper2<long double, 0x9000u, 0x7fffu> _FloatHelper;
  337. # endif
  338. return _FloatHelper::get_from_last_word();
  339. }
  340. };
  341. template <class __dummy>
  342. long double _STLP_CALL _LimG<__dummy>::get_LD_inf() {
  343. const int __use_double_limits = sizeof(double) == sizeof(long double) ? 1 : 0;
  344. return _NumericLimitsAccess<__use_double_limits>::get_inf();
  345. }
  346. template <class __dummy>
  347. long double _STLP_CALL _LimG<__dummy>::get_LD_qNaN() {
  348. const int __use_double_limits = sizeof(double) == sizeof(long double) ? 1 : 0;
  349. return _NumericLimitsAccess<__use_double_limits>::get_qNaN();
  350. }
  351. template <class __dummy>
  352. long double _STLP_CALL _LimG<__dummy>::get_LD_sNaN() {
  353. const int __use_double_limits = sizeof(double) == sizeof(long double) ? 1 : 0;
  354. return _NumericLimitsAccess<__use_double_limits>::get_sNaN();
  355. }
  356. template <class __dummy>
  357. long double _STLP_CALL _LimG<__dummy>::get_LD_denormMin() {
  358. typedef float_helper<long double, 0x0001u> _FloatHelper;
  359. return _FloatHelper::get_from_first_word();
  360. }
  361. # endif
  362. #endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
  363. #undef _STLP_LIMITS_MIN_TYPE
  364. #undef _STLP_LIMITS_MAX_TYPE
  365. _STLP_MOVE_TO_STD_NAMESPACE
  366. _STLP_END_NAMESPACE
  367. #endif /* _STLP_LIMITS_C_INCLUDED */