home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _limits.h < prev    next >
C/C++ Source or Header  |  2001-12-05  |  15KB  |  550 lines

  1. /*
  2.  * Copyright (c) 1997
  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.  
  19. /* NOTE: This may be not portable code. Parts of numeric_limits<> are
  20.  * inherently machine-dependent.  At present this file is suitable
  21.  * for the MIPS, SPARC, Alpha and ia32 architectures.
  22.  */
  23.  
  24. #ifndef _STLP_INTERNAL_LIMITS_H
  25. # define _STLP_INTERNAL_LIMITS_H
  26.  
  27. #ifndef _STLP_CLIMITS
  28. # include <climits>
  29. #endif
  30.  
  31. #ifndef _STLP_CFLOAT
  32. # include <cfloat>
  33. #endif
  34.  
  35. #if !defined (_STLP_NO_WCHAR_T)
  36. # include <cwchar>
  37. #endif
  38.  
  39. _STLP_BEGIN_NAMESPACE
  40.  
  41. enum float_round_style {
  42.   round_indeterminate       = -1,
  43.   round_toward_zero         =  0,
  44.   round_to_nearest          =  1,
  45.   round_toward_infinity     =  2,
  46.   round_toward_neg_infinity =  3
  47. };
  48.  
  49. enum float_denorm_style {
  50.   denorm_indeterminate = -1,
  51.   denorm_absent        =  0,
  52.   denorm_present       =  1
  53. };
  54.  
  55. // Base class for all specializations of numeric_limits.
  56.  
  57. template <class __number>
  58. class _Numeric_limits_base {
  59. public:
  60.  
  61.   static __number (_STLP_CALL min)() _STLP_NOTHROW { return __number(); }
  62.   static __number (_STLP_CALL max)() _STLP_NOTHROW { return __number(); }
  63.  
  64. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  65.   enum {
  66. # else
  67.   static const int 
  68. # endif
  69.   
  70.   digits = 0,
  71.   digits10 = 0,
  72.   radix = 0,
  73.   min_exponent = 0,
  74.   min_exponent10 = 0,
  75.   max_exponent = 0,
  76.   max_exponent10 = 0
  77.  
  78. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  79.   ,  
  80.   has_denorm = denorm_absent,
  81.   round_style = round_toward_zero,
  82. # else
  83.   ;
  84.   static const float_denorm_style has_denorm =  denorm_absent;
  85.   static const float_round_style round_style = round_toward_zero;
  86.   static const bool 
  87. # endif
  88.  
  89.     is_specialized = false,
  90.     is_signed  = false,
  91.     is_integer = false,
  92.     is_exact = false,
  93.     has_infinity = false,
  94.     has_quiet_NaN = false,
  95.     has_signaling_NaN = false,
  96.     has_denorm_loss = false,
  97.     is_iec559 = false,
  98.     is_bounded = false,
  99.     is_modulo = false,
  100.     traps = false,
  101.     tinyness_before = false
  102. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  103.   }
  104. # endif
  105.   ;
  106.     
  107.   static __number _STLP_CALL epsilon() _STLP_NOTHROW     { return __number(); }
  108.   static __number _STLP_CALL round_error() _STLP_NOTHROW { return __number(); }
  109.  
  110.   static __number _STLP_CALL infinity() _STLP_NOTHROW      { return __number(); }
  111.   static __number _STLP_CALL quiet_NaN() _STLP_NOTHROW     { return __number(); }
  112.   static __number _STLP_CALL signaling_NaN() _STLP_NOTHROW { return __number(); }
  113.   static __number _STLP_CALL denorm_min() _STLP_NOTHROW    { return __number(); }
  114.  
  115.  
  116. };
  117.  
  118. // Base class for integers.
  119.  
  120. # ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
  121. #  ifdef _STLP_LONG_LONG
  122. #   define _STLP_LIMITS_MIN_TYPE _STLP_LONG_LONG
  123. #   define _STLP_LIMITS_MAX_TYPE unsigned _STLP_LONG_LONG
  124. #  else
  125. #   define _STLP_LIMITS_MIN_TYPE long
  126. #   define _STLP_LIMITS_MAX_TYPE unsigned long
  127. #  endif
  128. # else
  129. #   define _STLP_LIMITS_MIN_TYPE _Int
  130. #   define _STLP_LIMITS_MAX_TYPE _Int
  131. # endif /* _STLP_LIMITED_DEFAULT_TEMPLATES */
  132.  
  133. template <class _Int,
  134.           _STLP_LIMITS_MIN_TYPE __imin,
  135.           _STLP_LIMITS_MAX_TYPE __imax,
  136.           int __idigits, bool __ismod>
  137. class _Integer_limits : public _Numeric_limits_base<_Int> 
  138. {
  139. public:
  140.  
  141.   static _Int (_STLP_CALL min) () _STLP_NOTHROW { return (_Int)__imin; }
  142.   static _Int (_STLP_CALL max) () _STLP_NOTHROW { return (_Int)__imax; }
  143.  
  144. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  145.   enum {
  146. # else
  147.   static const int 
  148. # endif  
  149.   digits = (__idigits < 0) ?
  150.   ((int)((sizeof(_Int) * (CHAR_BIT))) - ((__imin == 0) ? 0 : 1))
  151.   : (__idigits),
  152.   digits10 = (digits * 301UL) / 1000,
  153.   radix = 2
  154. # if ! defined ( _STLP_STATIC_CONST_INIT_BUG)
  155.   ;
  156.   static const bool
  157. # else
  158.   ,
  159. # endif
  160.   is_specialized = true,
  161.   is_signed = (__imin != 0),
  162.   is_integer = true,
  163.   is_exact = true,
  164.   is_bounded = true,
  165.   is_modulo = __ismod
  166. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  167.   }
  168. # endif
  169.   ;
  170. };
  171.  
  172. // Base class for floating-point numbers.
  173. template <class __number,
  174.          int __Digits, int __Digits10,
  175.          int __MinExp, int __MaxExp,
  176.          int __MinExp10, int __MaxExp10,
  177.          bool __IsIEC559,
  178.          float_round_style __RoundStyle>
  179. class _Floating_limits : public _Numeric_limits_base<__number>
  180. {
  181. public:
  182.  
  183. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  184.   enum {
  185. # else
  186.   static const int 
  187. # endif  
  188.  
  189.   digits = __Digits,
  190.   digits10 = __Digits10,
  191.  
  192.   radix = (  FLT_RADIX /* 2 */ ),
  193.   min_exponent = __MinExp, 
  194.   max_exponent = __MaxExp,
  195.   min_exponent10 = __MinExp10,
  196.   max_exponent10 = __MaxExp10
  197.  
  198. # if defined (_STLP_STATIC_CONST_INIT_BUG)
  199.   ,  
  200.   has_denorm = denorm_indeterminate,
  201.   round_style = __RoundStyle,
  202. # else
  203.   ;
  204.   static const float_denorm_style has_denorm = denorm_indeterminate;
  205.   static const float_round_style round_style = __RoundStyle;
  206.   static const bool 
  207. # endif
  208.  
  209.   is_specialized = true,
  210.   is_signed = true, 
  211.   has_infinity     =  true,
  212.   has_quiet_NaN    =  true,
  213.   has_signaling_NaN=  true,
  214.   has_denorm_loss  =  false,
  215.   is_iec559      =  __IsIEC559,
  216.   is_bounded     =  true,
  217.   traps          =  true,
  218.   tinyness_before=  false
  219.  
  220. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  221.   }
  222. # endif
  223.   ;
  224.  
  225. };
  226.  
  227. // Class numeric_limits
  228.  
  229. // The unspecialized class.
  230.  
  231. template<class _Tp> 
  232. class numeric_limits : public _Numeric_limits_base<_Tp> {};
  233.  
  234. // Specializations for all built-in integral types.
  235.  
  236. #ifndef _STLP_NO_BOOL
  237.  
  238. _STLP_TEMPLATE_NULL
  239. class   numeric_limits<bool>
  240.   : public _Integer_limits<bool, false, true, 1, false>
  241. {};
  242.  
  243. #endif /* _STLP_NO_BOOL */
  244.  
  245. _STLP_TEMPLATE_NULL
  246. class   numeric_limits<char>
  247.   : public _Integer_limits<char, CHAR_MIN, CHAR_MAX, -1, true>
  248. {};
  249.  
  250. # ifndef _STLP_NO_SIGNED_BUILTINS
  251. _STLP_TEMPLATE_NULL
  252. class   numeric_limits<signed char>
  253.   : public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX, -1, true>
  254. {};
  255. # endif
  256.  
  257. _STLP_TEMPLATE_NULL
  258. class   numeric_limits<unsigned char>
  259.   : public _Integer_limits<unsigned char, 0, UCHAR_MAX, -1, true>
  260. {};
  261.  
  262. #if !(defined ( _STLP_NO_WCHAR_T ) || defined (_STLP_WCHAR_T_IS_USHORT))
  263.  
  264. _STLP_TEMPLATE_NULL
  265. class   numeric_limits<wchar_t>
  266.   : public _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX, -1, true>
  267. {};
  268.  
  269. #endif
  270.  
  271. _STLP_TEMPLATE_NULL
  272. class   numeric_limits<short>
  273.   : public _Integer_limits<short, SHRT_MIN, SHRT_MAX, -1, true>
  274. {};
  275.  
  276. _STLP_TEMPLATE_NULL
  277. class   numeric_limits<unsigned short>
  278.   : public _Integer_limits<unsigned short, 0, USHRT_MAX, -1, true>
  279. {};
  280.  
  281. # if defined (__xlC__) && (__xlC__ == 0x500)
  282. #  undef INT_MIN
  283. #  define INT_MIN -2147483648
  284. # endif
  285.  
  286.  
  287. _STLP_TEMPLATE_NULL
  288. class   numeric_limits<int>
  289.   : public _Integer_limits<int, INT_MIN, INT_MAX, -1, true>
  290. {};
  291.  
  292. _STLP_TEMPLATE_NULL
  293. class   numeric_limits<unsigned int>
  294.   : public _Integer_limits<unsigned int, 0, UINT_MAX, -1, true>
  295. {};
  296.  
  297. _STLP_TEMPLATE_NULL
  298. class   numeric_limits<long>
  299.   : public _Integer_limits<long, LONG_MIN, LONG_MAX, -1, true>
  300. {};
  301.  
  302. _STLP_TEMPLATE_NULL
  303. class   numeric_limits<unsigned long>
  304.   : public _Integer_limits<unsigned long, 0, ULONG_MAX, -1, true>
  305. {};
  306.  
  307. #ifdef _STLP_LONG_LONG
  308.  
  309. # if defined (_STLP_MSVC) || defined (__BORLANDC__)
  310.  
  311. #    define LONGLONG_MAX     0x7fffffffffffffffi64
  312. #    define LONGLONG_MIN     (-LONGLONG_MAX-1i64)
  313. #    define ULONGLONG_MAX    0xffffffffffffffffUi64
  314.  
  315. # else
  316.  
  317. #  ifndef   LONGLONG_MAX
  318. #    define LONGLONG_MAX     0x7fffffffffffffffLL
  319. #  endif
  320. #  ifndef   LONGLONG_MIN
  321. #    define LONGLONG_MIN     (-LONGLONG_MAX-1LL)
  322. #  endif
  323. #  ifndef   ULONGLONG_MAX
  324. #    define ULONGLONG_MAX    0xffffffffffffffffULL
  325. #  endif
  326.  
  327. # endif
  328.  
  329. #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ <= 96)
  330.  
  331. _STLP_TEMPLATE_NULL
  332. class   numeric_limits<_STLP_LONG_LONG>
  333.   : public _Integer_limits<_STLP_LONG_LONG, LONGLONG_MIN, LONGLONG_MAX, -1, true>
  334. {};
  335.  
  336. _STLP_TEMPLATE_NULL
  337. class   numeric_limits<unsigned _STLP_LONG_LONG>
  338.   : public _Integer_limits<unsigned _STLP_LONG_LONG, 0, ULONGLONG_MAX, -1, true>
  339. {};
  340. #else /* gcc 2.97 (after 2000-11-01), 2.98, 3.0 */
  341. /*
  342.  newest gcc has new mangling scheme, that has problem
  343.  with generating name [instantiated] of template specialization like
  344.  _Integer_limits<_STLP_LONG_LONG, LONGLONG_MIN, LONGLONG_MAX, -1, true>
  345.                                   ~~~~~~~~~~~~  ~~~~~~~~~~~~
  346.  Below is code that solve this problem.
  347.    - ptr
  348.  */
  349. _STLP_TEMPLATE_NULL
  350. class   numeric_limits<_STLP_LONG_LONG>
  351.   : public _Numeric_limits_base<_STLP_LONG_LONG> 
  352. {
  353. public:
  354.  
  355.   static _STLP_LONG_LONG (_STLP_CALL min) () _STLP_NOTHROW { return LONGLONG_MIN; }
  356.   static _STLP_LONG_LONG (_STLP_CALL max) () _STLP_NOTHROW { return LONGLONG_MAX; }
  357.  
  358. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  359.   enum {
  360. # else
  361.   static const int 
  362. # endif  
  363.   digits = ((int)((sizeof(_STLP_LONG_LONG) * (CHAR_BIT))) - 1),
  364.   digits10 = (digits * 301UL) / 1000,
  365.   radix = 2
  366. # if ! defined ( _STLP_STATIC_CONST_INIT_BUG)
  367.   ;
  368.   static const bool
  369. # else
  370.   ,
  371. # endif
  372.   is_specialized = true,
  373.   is_signed = true,
  374.   is_integer = true,
  375.   is_exact = true,
  376.   is_bounded = true,
  377.   is_modulo = true
  378. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  379.   }
  380. # endif
  381.   ;
  382. };
  383.  
  384. _STLP_TEMPLATE_NULL
  385. class   numeric_limits<unsigned _STLP_LONG_LONG>
  386.   : public _Numeric_limits_base<unsigned _STLP_LONG_LONG> 
  387. {
  388. public:
  389.  
  390.   static unsigned _STLP_LONG_LONG (_STLP_CALL min) () _STLP_NOTHROW { return 0ULL; }
  391.   static unsigned _STLP_LONG_LONG (_STLP_CALL max) () _STLP_NOTHROW { return ULONGLONG_MAX; }
  392.  
  393. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  394.   enum {
  395. # else
  396.   static const int 
  397. # endif  
  398.   digits = ((int)((sizeof(unsigned _STLP_LONG_LONG) * (CHAR_BIT)))),
  399.   digits10 = (digits * 301UL) / 1000,
  400.   radix = 2
  401. # if ! defined ( _STLP_STATIC_CONST_INIT_BUG)
  402.   ;
  403.   static const bool
  404. # else
  405.   ,
  406. # endif
  407.   is_specialized = true,
  408.   is_signed = false,
  409.   is_integer = true,
  410.   is_exact = true,
  411.   is_bounded = true,
  412.   is_modulo = true
  413. # if defined ( _STLP_STATIC_CONST_INIT_BUG)
  414.   }
  415. # endif
  416.   ;
  417. };
  418.  
  419. # endif /* __GNUC__ > 2000-11-01 */
  420.  
  421. #endif /* _STLP_LONG_LONG */
  422.  
  423. // Specializations for all built-in floating-point types.
  424.  
  425. union _F_rep
  426. {
  427.   unsigned short rep[2];
  428.   float val;
  429. };
  430. union _D_rep
  431. {
  432.   unsigned short rep[4];
  433.   double val;
  434. };
  435.  
  436. # ifndef _STLP_NO_LONG_DOUBLE
  437. union _L_rep
  438. {
  439.   unsigned short rep[8];
  440.   long double val;
  441. };
  442. # endif
  443.  
  444. template <class __dummy>
  445. class _LimG 
  446. {
  447. public:  
  448.   static const _F_rep _F_inf;
  449.   static const _F_rep _F_qNaN;
  450.   static const _F_rep _F_sNaN;
  451.   static const _D_rep _D_inf;
  452.   static const _D_rep _D_qNaN;
  453.   static const _D_rep _D_sNaN;
  454.   
  455. # ifndef _STLP_NO_LONG_DOUBLE
  456.   static const _L_rep _L_inf;
  457.   static const _L_rep _L_qNaN;
  458.   static const _L_rep _L_sNaN;
  459. # endif
  460. };
  461.  
  462. # if defined (_STLP_USE_TEMPLATE_EXPORT) 
  463. _STLP_EXPORT_TEMPLATE_CLASS _LimG<bool>;
  464. # endif
  465.  
  466. _STLP_TEMPLATE_NULL class   numeric_limits<float>
  467.   : public _Floating_limits<float, 
  468.                             FLT_MANT_DIG,   // Binary digits of precision
  469.                             FLT_DIG,        // Decimal digits of precision
  470.                             FLT_MIN_EXP,    // Minimum exponent
  471.                             FLT_MAX_EXP,    // Maximum exponent
  472.                             FLT_MIN_10_EXP, // Minimum base 10 exponent
  473.                             FLT_MAX_10_EXP, // Maximum base 10 exponent
  474.                             true,           // conforms to iec559
  475.                             round_to_nearest>
  476. {
  477. public:
  478.   static float (_STLP_CALL min) () _STLP_NOTHROW { return FLT_MIN; }
  479.   static float _STLP_CALL denorm_min() _STLP_NOTHROW { return FLT_MIN; }
  480.   static float (_STLP_CALL max) () _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return FLT_MAX; }
  481.   static float _STLP_CALL epsilon() _STLP_NOTHROW { return FLT_EPSILON; }
  482.   static float _STLP_CALL round_error() _STLP_NOTHROW { return 0.5f; } // Units: ulps.
  483.   static  float _STLP_CALL infinity() { return _LimG<bool>::_F_inf.val; }
  484.   static  float _STLP_CALL quiet_NaN() { return _LimG<bool>::_F_qNaN.val; }
  485.   static  float _STLP_CALL signaling_NaN() { return _LimG<bool>::_F_sNaN.val; }
  486. };
  487.  
  488. _STLP_TEMPLATE_NULL class   numeric_limits<double>
  489.   : public _Floating_limits<double, 
  490.                             DBL_MANT_DIG,   // Binary digits of precision
  491.                             DBL_DIG,        // Decimal digits of precision
  492.                             DBL_MIN_EXP,    // Minimum exponent
  493.                             DBL_MAX_EXP,    // Maximum exponent
  494.                             DBL_MIN_10_EXP, // Minimum base 10 exponent
  495.                             DBL_MAX_10_EXP, // Maximum base 10 exponent
  496.                             true,           // conforms to iec559
  497.                             round_to_nearest>
  498. {
  499. public:
  500.   static double (_STLP_CALL min)() _STLP_NOTHROW { return DBL_MIN; }
  501.   static double _STLP_CALL denorm_min() _STLP_NOTHROW { return DBL_MIN; }
  502.   static double (_STLP_CALL max)() _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return DBL_MAX; }
  503.   static double _STLP_CALL epsilon() _STLP_NOTHROW { return DBL_EPSILON; }
  504.   static double _STLP_CALL round_error() _STLP_NOTHROW { return 0.5; } // Units: ulps.
  505.   static  double _STLP_CALL infinity() { return _LimG<bool>::_D_inf.val; }
  506.   static  double _STLP_CALL quiet_NaN(){ return _LimG<bool>::_D_qNaN.val; }
  507.   static  double _STLP_CALL signaling_NaN() { return _LimG<bool>::_D_sNaN.val; }
  508. };
  509.  
  510. # ifndef _STLP_NO_LONG_DOUBLE
  511.  
  512. _STLP_TEMPLATE_NULL 
  513. class   numeric_limits<long double>
  514.   : public _Floating_limits<long double, 
  515.                             LDBL_MANT_DIG,  // Binary digits of precision
  516.                             LDBL_DIG,       // Decimal digits of precision
  517.                             LDBL_MIN_EXP,   // Minimum exponent
  518.                             LDBL_MAX_EXP,   // Maximum exponent
  519.                             LDBL_MIN_10_EXP,// Minimum base 10 exponent
  520.                             LDBL_MAX_10_EXP,// Maximum base 10 exponent
  521.                             false,          // Doesn't conform to iec559
  522.                             round_to_nearest>
  523. {
  524. public:
  525.   static long double (_STLP_CALL min) () _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return LDBL_MIN; }
  526.   static long double _STLP_CALL denorm_min() _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return LDBL_MIN; }
  527.   static long double (_STLP_CALL max) () _STLP_NOTHROW { _STLP_USING_VENDOR_CSTD return LDBL_MAX; }
  528.   static long double _STLP_CALL epsilon() _STLP_NOTHROW { return LDBL_EPSILON; }
  529.   static long double _STLP_CALL round_error() _STLP_NOTHROW { return 4; } // Units: ulps.
  530.   static long double _STLP_CALL infinity() { return _LimG<bool>::_L_inf.val; } 
  531.   static long double _STLP_CALL quiet_NaN() { return _LimG<bool>::_L_qNaN.val; }
  532.   static long double _STLP_CALL signaling_NaN() { return _LimG<bool>::_L_sNaN.val; }
  533. };
  534.  
  535. # endif
  536.  
  537. // We write special values (Inf and NaN) as bit patterns and 
  538. // cast the the appropriate floating-point types. 
  539. _STLP_END_NAMESPACE
  540.  
  541. # if !defined (_STLP_LINK_TIME_INSTANTIATION)
  542. #  include <stl/_limits.c>
  543. # endif
  544.  
  545. #endif
  546.  
  547. // Local Variables:
  548. // mode:C++
  549. // End:
  550.