genericopenlibs/cppstdlib/stl/src/uint64.h
changeset 0 e4d67989cc36
child 18 47c74d1534e1
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 //  uint64.h
       
     2 //  minimal double precision unsigned int arithmetics for numeric_facets support.
       
     3 //  Written by Tsutomu Yoshida, Minokamo, Japan. 03/25/2000
       
     4 #ifndef _UINT64_H
       
     5 #define _UINT64_H
       
     6 
       
     7 template <class _Tp>
       
     8 class _compound_int : public _Tp {
       
     9 public:
       
    10   typedef _compound_int<_Tp> _Self;
       
    11 
       
    12   // Constructors, destructor, assignment operator.
       
    13   _compound_int();                    // platform specific
       
    14   _compound_int(unsigned long);              // platform specific
       
    15   _compound_int(unsigned long hi, unsigned long lo);  // platform specific
       
    16   _compound_int(const _Tp& rhs) : _Tp(rhs) {}
       
    17 
       
    18   // Arithmetic op= operations involving two _compound_int.
       
    19   _Self& operator+= (const _Self&);            // platform specific
       
    20   _Self& operator-= (const _Self&);            // platform specific
       
    21   _Self& operator*= (const _Self&);            // platform specific
       
    22   _Self& operator/= (const _Self&);            // platform specific
       
    23   _Self& operator%= (const _Self&);            // platform specific
       
    24   _Self& operator&= (const _Self&);            // platform specific
       
    25   _Self& operator|= (const _Self&);            // platform specific
       
    26   _Self& operator^= (const _Self&);            // platform specific
       
    27 
       
    28   // Arithmetic op= operations involving built-in integer.
       
    29   _Self& operator<<= (unsigned int);          // platform specific
       
    30   _Self& operator>>= (unsigned int);          // platform specific
       
    31 
       
    32   _Self& operator=  (unsigned long rhs) { return *this =  _Self(rhs); }
       
    33   _Self& operator+= (unsigned long rhs) { return *this += _Self(rhs); }
       
    34   _Self& operator-= (unsigned long rhs) { return *this -= _Self(rhs); }
       
    35   _Self& operator*= (unsigned long rhs) { return *this *= _Self(rhs); }
       
    36   _Self& operator/= (unsigned long rhs) { return *this /= _Self(rhs); }
       
    37   _Self& operator%= (unsigned long rhs) { return *this %= _Self(rhs); }
       
    38   _Self& operator&= (unsigned long rhs) { return *this &= _Self(rhs); }
       
    39   _Self& operator|= (unsigned long rhs) { return *this |= _Self(rhs); }
       
    40   _Self& operator^= (unsigned long rhs) { return *this ^= _Self(rhs); }
       
    41 
       
    42   // Increment and decrement
       
    43   _Self& operator++() { return (*this) += 1; }
       
    44   _Self& operator--() { return (*this) -= 1; }
       
    45   _Self operator++(int) { _Self temp(*this); ++(*this); return temp; }
       
    46   _Self operator--(int) { _Self temp(*this); --(*this); return temp; }
       
    47 };
       
    48 
       
    49 // Comparison operators.
       
    50 template <class _Tp> bool operator==(const _compound_int<_Tp>&, const _compound_int<_Tp>&);  // platform specific
       
    51 template <class _Tp> bool operator<(const _compound_int<_Tp>&, const _compound_int<_Tp>&);  // platform specific
       
    52 
       
    53 template <class _Tp> inline bool operator==(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs == _compound_int<_Tp>(rhs); }
       
    54 template <class _Tp> inline bool operator==(unsigned long lhs, const _compound_int<_Tp>& rhs) { return rhs == lhs; }
       
    55 
       
    56 template <class _Tp> inline bool operator< (const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs < _compound_int<_Tp>(rhs); }
       
    57 template <class _Tp> inline bool operator< (unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) < rhs; }
       
    58 
       
    59 template <class _Tp> inline bool operator!=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs == rhs); }
       
    60 template <class _Tp> inline bool operator!=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs == rhs); }
       
    61 
       
    62 template <class _Tp> inline bool operator> (const _compound_int<_Tp>& lhs, unsigned long rhs) { return rhs < lhs; }
       
    63 template <class _Tp> inline bool operator> (unsigned long lhs, const _compound_int<_Tp>& rhs) { return rhs < lhs; }
       
    64 
       
    65 template <class _Tp> inline bool operator<=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs > rhs); }
       
    66 template <class _Tp> inline bool operator<=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs > rhs); }
       
    67 
       
    68 template <class _Tp> inline bool operator>=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs < rhs); }
       
    69 template <class _Tp> inline bool operator>=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs < rhs); }
       
    70 
       
    71 // Unary non-member arithmetic operators.
       
    72 template <class _Tp> unsigned long to_ulong(const _compound_int<_Tp>&);      // platform specific
       
    73 template <class _Tp> _compound_int<_Tp> operator~(const _compound_int<_Tp>&);  // platform specific
       
    74 
       
    75 template <class _Tp> inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& val) {return val;}
       
    76 template <class _Tp> inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& val) {return 0 - val;}
       
    77 template <class _Tp> inline bool operator!(const _compound_int<_Tp>& val) {return val==0;}
       
    78 
       
    79 // Non-member arithmetic operations involving two _compound_int arguments
       
    80 template <class _Tp>
       
    81 inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs)
       
    82 { _compound_int<_Tp> temp(lhs); return temp += rhs; }
       
    83 template <class _Tp>
       
    84 inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs)
       
    85 { _compound_int<_Tp> temp(lhs); return temp -= rhs; }
       
    86 template <class _Tp>
       
    87 inline _compound_int<_Tp> operator*(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs)
       
    88 { _compound_int<_Tp> temp(lhs); return temp *= rhs; }
       
    89 template <class _Tp>
       
    90 inline _compound_int<_Tp> operator/(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs)
       
    91 { _compound_int<_Tp> temp(lhs); return temp /= rhs; }
       
    92 template <class _Tp>
       
    93 inline _compound_int<_Tp> operator%(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs)
       
    94 { _compound_int<_Tp> temp(lhs); return temp %= rhs; }
       
    95 template <class _Tp>
       
    96 inline _compound_int<_Tp> operator&(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs)
       
    97 { _compound_int<_Tp> temp(lhs); return temp &= rhs; }
       
    98 template <class _Tp>
       
    99 inline _compound_int<_Tp> operator|(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs)
       
   100 { _compound_int<_Tp> temp(lhs); return temp |= rhs; }
       
   101 template <class _Tp>
       
   102 inline _compound_int<_Tp> operator^(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs)
       
   103 { _compound_int<_Tp> temp(lhs); return temp ^= rhs; }
       
   104 
       
   105 // Non-member arithmetic operations involving one built-in integer argument.
       
   106 template <class _Tp>
       
   107 inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs + _compound_int<_Tp>(rhs); }
       
   108 template <class _Tp>
       
   109 inline _compound_int<_Tp> operator+(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) + rhs; }
       
   110 
       
   111 template <class _Tp>
       
   112 inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs - _compound_int<_Tp>(rhs); }
       
   113 template <class _Tp>
       
   114 inline _compound_int<_Tp> operator-(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) - rhs; }
       
   115 
       
   116 template <class _Tp>
       
   117 inline _compound_int<_Tp> operator*(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs * _compound_int<_Tp>(rhs); }
       
   118 template <class _Tp>
       
   119 inline _compound_int<_Tp> operator*(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) * rhs; }
       
   120 
       
   121 template <class _Tp>
       
   122 inline _compound_int<_Tp> operator/(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs / _compound_int<_Tp>(rhs); }
       
   123 template <class _Tp>
       
   124 inline _compound_int<_Tp> operator/(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) / rhs; }
       
   125 
       
   126 template <class _Tp>
       
   127 inline _compound_int<_Tp> operator%(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs % _compound_int<_Tp>(rhs); }
       
   128 template <class _Tp>
       
   129 inline _compound_int<_Tp> operator%(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) % rhs; }
       
   130 
       
   131 template <class _Tp>
       
   132 inline _compound_int<_Tp> operator&(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs & _compound_int<_Tp>(rhs); }
       
   133 template <class _Tp>
       
   134 inline _compound_int<_Tp> operator&(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) & rhs; }
       
   135 
       
   136 template <class _Tp>
       
   137 inline _compound_int<_Tp> operator|(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs | _compound_int<_Tp>(rhs); }
       
   138 template <class _Tp>
       
   139 inline _compound_int<_Tp> operator|(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) | rhs; }
       
   140 
       
   141 template <class _Tp>
       
   142 inline _compound_int<_Tp> operator^(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs ^ _compound_int<_Tp>(rhs); }
       
   143 template <class _Tp>
       
   144 inline _compound_int<_Tp> operator^(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) ^ rhs; }
       
   145 
       
   146 template <class _Tp>
       
   147 inline _compound_int<_Tp> operator<<(const _compound_int<_Tp>& lhs, unsigned int rhs) { _compound_int<_Tp> temp(lhs); return temp <<= rhs; }
       
   148 template <class _Tp>
       
   149 inline _compound_int<_Tp> operator>>(const _compound_int<_Tp>& lhs, unsigned int rhs) { _compound_int<_Tp> temp(lhs); return temp >>= rhs; }
       
   150 
       
   151 // platform specific specializations
       
   152 #if defined (__MRC__) || defined (__SC__)
       
   153 
       
   154 _STLP_END_NAMESPACE
       
   155 #  include <Math64.h>
       
   156 #  include <utility>
       
   157 #  undef modff    //*TY 04/06/2000 - defined in <math.h> which conflicts with <fp.h> definition
       
   158 #  include <fp.h>
       
   159 
       
   160 _STLP_BEGIN_NAMESPACE
       
   161 
       
   162 #  if TYPE_LONGLONG
       
   163 typedef UInt64 uint64;
       
   164 #    define ULL(x) (U64SetU(x))
       
   165 
       
   166 #  else
       
   167 //  Apple's mpw sc compiler for 68k macintosh does not support native 64bit integer type.
       
   168 //  Instead, it comes with external support library and struct data type representing 64bit int:
       
   169 //
       
   170 //    struct UnsignedWide {
       
   171 //      UInt32   hi;
       
   172 //      UInt32   lo;
       
   173 //    };
       
   174 
       
   175 typedef _compound_int<UnsignedWide> uint64;
       
   176 #    define ULL(x) uint64(x)
       
   177 #    define ULL2(hi,lo) {hi,lo}
       
   178 
       
   179 // Constructors, destructor, assignment operator.
       
   180 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int() { hi = 0; lo = 0; }
       
   181 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int(unsigned long val) { hi = 0; lo = val; }
       
   182 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int(unsigned long h, unsigned long l) { hi = h; lo = l; }
       
   183 
       
   184 // Arithmetic op= operations involving two _compound_int.
       
   185 _STLP_TEMPLATE_NULL
       
   186 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator+= (const _compound_int<UnsignedWide>& rhs)
       
   187 { *this = U64Add( *this, rhs ); return *this; }
       
   188 _STLP_TEMPLATE_NULL
       
   189 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator-= (const _compound_int<UnsignedWide>& rhs)
       
   190 { *this = U64Subtract( *this, rhs ); return *this; }
       
   191 _STLP_TEMPLATE_NULL
       
   192 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator*= (const _compound_int<UnsignedWide>& rhs)
       
   193 { *this = U64Multiply( *this, rhs ); return *this; }
       
   194 _STLP_TEMPLATE_NULL
       
   195 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator/= (const _compound_int<UnsignedWide>& rhs)
       
   196 { *this = U64Divide( *this, rhs, NULL ); return *this; }
       
   197 _STLP_TEMPLATE_NULL
       
   198 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator%= (const _compound_int<UnsignedWide>& rhs)
       
   199 { U64Divide( *this, rhs, this ); return *this; }
       
   200 _STLP_TEMPLATE_NULL
       
   201 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator&= (const _compound_int<UnsignedWide>& rhs)
       
   202 { *this = U64BitwiseAnd( *this, rhs ); return *this; }
       
   203 _STLP_TEMPLATE_NULL
       
   204 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator|= (const _compound_int<UnsignedWide>& rhs)
       
   205 { *this = U64BitwiseOr( *this, rhs ); return *this; }
       
   206 _STLP_TEMPLATE_NULL
       
   207 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator^= (const _compound_int<UnsignedWide>& rhs)
       
   208 { *this = U64BitwiseEor( *this, rhs ); return *this; }
       
   209 
       
   210 // Arithmetic op= operations involving built-in integer.
       
   211 _STLP_TEMPLATE_NULL
       
   212 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator<<= (unsigned int rhs)
       
   213 { *this = U64ShiftLeft( *this, rhs ); return *this; }
       
   214 _STLP_TEMPLATE_NULL
       
   215 inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator>>= (unsigned int rhs)
       
   216 { *this = U64ShiftRight( *this, rhs ); return *this; }
       
   217 
       
   218 // Comparison operators.
       
   219 _STLP_TEMPLATE_NULL
       
   220 inline bool operator==(const _compound_int<UnsignedWide>& lhs, const _compound_int<UnsignedWide>& rhs)
       
   221 { return (lhs.hi == rhs.hi) && (lhs.lo == rhs.lo); }
       
   222 _STLP_TEMPLATE_NULL
       
   223 inline bool operator< (const _compound_int<UnsignedWide>& lhs, const _compound_int<UnsignedWide>& rhs)
       
   224 { return U64Compare( lhs, rhs ) < 0; }
       
   225 _STLP_TEMPLATE_NULL
       
   226 inline bool operator==(const _compound_int<UnsignedWide>& lhs, unsigned long rhs)
       
   227 { return (lhs.hi == 0) && (lhs.lo == rhs); }
       
   228 
       
   229 // Unary non-member arithmetic operators.
       
   230 _STLP_TEMPLATE_NULL
       
   231 inline unsigned long to_ulong(const _compound_int<UnsignedWide>& val) { return val.lo; }
       
   232 _STLP_TEMPLATE_NULL
       
   233 inline _compound_int<UnsignedWide> operator~(const _compound_int<UnsignedWide>& val) { return U64BitwiseNot( val ); }
       
   234 _STLP_TEMPLATE_NULL
       
   235 inline bool operator!(const _compound_int<UnsignedWide>& val) { return !((val.hi == 0) && (val.lo == 0)); }
       
   236 
       
   237 #  endif // TYPE_LONGLONG
       
   238 #endif // __MRC__
       
   239 
       
   240 #endif // _UINT64_H