stdcpp/src/uint64.h
changeset 31 ce057bb09d0b
child 34 5fae379060a7
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 //	uint64.h
       
    18 //	minimal double precision unsigned int arithmetics for numeric_facets support.
       
    19 //	Written by Tsutomu Yoshida, Minokamo, Japan. 03/25/2000
       
    20 
       
    21 #ifndef _UINT64_H
       
    22 #define _UINT64_H
       
    23 
       
    24 
       
    25 
       
    26 template <class _Tp>
       
    27 class _compound_int : public _Tp
       
    28 {
       
    29 public:
       
    30   typedef _compound_int<_Tp> _Self;
       
    31 
       
    32   // Constructors, destructor, assignment operator.
       
    33   _compound_int();										// platform specific
       
    34   _compound_int(unsigned long);							// platform specific
       
    35   _compound_int(unsigned long hi, unsigned long lo);	// platform specific
       
    36   _compound_int(const _Tp& rhs) : _Tp(rhs) {}
       
    37 
       
    38   // Arithmetic op= operations involving two _compound_int.
       
    39   _Self& operator+= (const _Self&);						// platform specific
       
    40   _Self& operator-= (const _Self&);						// platform specific
       
    41   _Self& operator*= (const _Self&);						// platform specific
       
    42   _Self& operator/= (const _Self&);						// platform specific
       
    43   _Self& operator%= (const _Self&);						// platform specific
       
    44   _Self& operator&= (const _Self&);						// platform specific
       
    45   _Self& operator|= (const _Self&);						// platform specific
       
    46   _Self& operator^= (const _Self&);						// platform specific
       
    47 
       
    48   // Arithmetic op= operations involving built-in integer.
       
    49   _Self& operator<<= (unsigned int);					// platform specific
       
    50   _Self& operator>>= (unsigned int);					// platform specific
       
    51   
       
    52   _Self& operator=  (unsigned long rhs) { return *this =  _Self(rhs); }
       
    53   _Self& operator+= (unsigned long rhs) { return *this += _Self(rhs); }
       
    54   _Self& operator-= (unsigned long rhs) { return *this -= _Self(rhs); }
       
    55   _Self& operator*= (unsigned long rhs) { return *this *= _Self(rhs); }
       
    56   _Self& operator/= (unsigned long rhs) { return *this /= _Self(rhs); }
       
    57   _Self& operator%= (unsigned long rhs) { return *this %= _Self(rhs); }
       
    58   _Self& operator&= (unsigned long rhs) { return *this &= _Self(rhs); }
       
    59   _Self& operator|= (unsigned long rhs) { return *this |= _Self(rhs); }
       
    60   _Self& operator^= (unsigned long rhs) { return *this ^= _Self(rhs); }
       
    61 
       
    62   // Increment and decrement
       
    63   _Self& operator++() { return (*this) += 1; }
       
    64   _Self& operator--() { return (*this) -= 1; }
       
    65   _Self operator++(int) { _Self temp(*this); ++(*this); return temp; }
       
    66   _Self operator--(int) { _Self temp(*this); --(*this); return temp; }
       
    67 };
       
    68 
       
    69 // Comparison operators.
       
    70 template <class _Tp> bool operator==(const _compound_int<_Tp>&, const _compound_int<_Tp>&);	// platform specific
       
    71 template <class _Tp> bool operator<(const _compound_int<_Tp>&, const _compound_int<_Tp>&);	// platform specific
       
    72 
       
    73 template <class _Tp> inline bool operator==(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs == _compound_int<_Tp>(rhs); }
       
    74 template <class _Tp> inline bool operator==(unsigned long lhs, const _compound_int<_Tp>& rhs) { return rhs == lhs; }
       
    75 
       
    76 template <class _Tp> inline bool operator< (const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs < _compound_int<_Tp>(rhs); }
       
    77 template <class _Tp> inline bool operator< (unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) < rhs; }
       
    78 
       
    79 template <class _Tp> inline bool operator!=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs == rhs); }
       
    80 template <class _Tp> inline bool operator!=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs == rhs); }
       
    81 
       
    82 template <class _Tp> inline bool operator> (const _compound_int<_Tp>& lhs, unsigned long rhs) { return rhs < lhs; }
       
    83 template <class _Tp> inline bool operator> (unsigned long lhs, const _compound_int<_Tp>& rhs) { return rhs < lhs; }
       
    84 
       
    85 template <class _Tp> inline bool operator<=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs > rhs); }
       
    86 template <class _Tp> inline bool operator<=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs > rhs); }
       
    87 
       
    88 template <class _Tp> inline bool operator>=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs < rhs); }
       
    89 template <class _Tp> inline bool operator>=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs < rhs); }
       
    90 
       
    91 // Unary non-member arithmetic operators.
       
    92 template <class _Tp> unsigned long to_ulong(const _compound_int<_Tp>&);			// platform specific
       
    93 template <class _Tp> _compound_int<_Tp> operator~(const _compound_int<_Tp>&);	// platform specific
       
    94 
       
    95 template <class _Tp> inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& val) {return val;}
       
    96 template <class _Tp> inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& val) {return 0 - val;}
       
    97 template <class _Tp> inline bool operator!(const _compound_int<_Tp>& val) {return val==0;}
       
    98 
       
    99 // Non-member arithmetic operations involving two _compound_int arguments
       
   100 template <class _Tp> inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp += rhs; }
       
   101 template <class _Tp> inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp -= rhs; }
       
   102 template <class _Tp> inline _compound_int<_Tp> operator*(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp *= rhs; }
       
   103 template <class _Tp> inline _compound_int<_Tp> operator/(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp /= rhs; }
       
   104 template <class _Tp> inline _compound_int<_Tp> operator%(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp %= rhs; }
       
   105 template <class _Tp> inline _compound_int<_Tp> operator&(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp &= rhs; }
       
   106 template <class _Tp> inline _compound_int<_Tp> operator|(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp |= rhs; }
       
   107 template <class _Tp> inline _compound_int<_Tp> operator^(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp ^= rhs; }
       
   108 
       
   109 // Non-member arithmetic operations involving one built-in integer argument.
       
   110 template <class _Tp> inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs + _compound_int<_Tp>(rhs); }
       
   111 template <class _Tp> inline _compound_int<_Tp> operator+(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) + rhs; }
       
   112 
       
   113 template <class _Tp> inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs - _compound_int<_Tp>(rhs); }
       
   114 template <class _Tp> inline _compound_int<_Tp> operator-(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) - rhs; }
       
   115 
       
   116 template <class _Tp> inline _compound_int<_Tp> operator*(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs * _compound_int<_Tp>(rhs); }
       
   117 template <class _Tp> inline _compound_int<_Tp> operator*(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) * rhs; }
       
   118 
       
   119 template <class _Tp> inline _compound_int<_Tp> operator/(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs / _compound_int<_Tp>(rhs); }
       
   120 template <class _Tp> inline _compound_int<_Tp> operator/(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) / rhs; }
       
   121 
       
   122 template <class _Tp> inline _compound_int<_Tp> operator%(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs % _compound_int<_Tp>(rhs); }
       
   123 template <class _Tp> inline _compound_int<_Tp> operator%(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) % rhs; }
       
   124 
       
   125 template <class _Tp> inline _compound_int<_Tp> operator&(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs & _compound_int<_Tp>(rhs); }
       
   126 template <class _Tp> inline _compound_int<_Tp> operator&(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) & rhs; }
       
   127 
       
   128 template <class _Tp> inline _compound_int<_Tp> operator|(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs | _compound_int<_Tp>(rhs); }
       
   129 template <class _Tp> inline _compound_int<_Tp> operator|(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) | rhs; }
       
   130 
       
   131 template <class _Tp> inline _compound_int<_Tp> operator^(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs ^ _compound_int<_Tp>(rhs); }
       
   132 template <class _Tp> inline _compound_int<_Tp> operator^(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) ^ rhs; }
       
   133 
       
   134 template <class _Tp> inline _compound_int<_Tp> operator<<(const _compound_int<_Tp>& lhs, unsigned int rhs) { _compound_int<_Tp> temp(lhs); return temp <<= rhs; }
       
   135 template <class _Tp> inline _compound_int<_Tp> operator>>(const _compound_int<_Tp>& lhs, unsigned int rhs) { _compound_int<_Tp> temp(lhs); return temp >>= rhs; }
       
   136 
       
   137 
       
   138 
       
   139 
       
   140 // platform specific specializations
       
   141 
       
   142 #if defined(__MRC__)||defined(__SC__)
       
   143 
       
   144 _STLP_END_NAMESPACE // ugly!
       
   145 # include <Math64.h>
       
   146 # include <utility>
       
   147 # undef modff		//*TY 04/06/2000 - defined in <math.h> which conflicts with <fp.h> definition
       
   148 # include <fp.h>
       
   149 
       
   150 _STLP_BEGIN_NAMESPACE
       
   151 
       
   152 # if TYPE_LONGLONG 
       
   153 typedef UInt64 uint64;
       
   154 # define ULL(x) (U64SetU(x))
       
   155 
       
   156 # else
       
   157 //	Apple's mpw sc compiler for 68k macintosh does not support native 64bit integer type. 
       
   158 //	Instead, it comes with external support library and struct data type representing 64bit int:
       
   159 //	
       
   160 //		struct UnsignedWide {
       
   161 //			UInt32 	hi;
       
   162 //			UInt32 	lo;
       
   163 //		};
       
   164 
       
   165 typedef _compound_int<UnsignedWide> uint64;
       
   166 # define ULL(x) uint64(x)
       
   167 # define ULL2(hi,lo) {hi,lo}
       
   168 
       
   169 // Constructors, destructor, assignment operator.
       
   170 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int() { hi = 0; lo = 0; }
       
   171 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int(unsigned long val) { hi = 0; lo = val; }
       
   172 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int(unsigned long h, unsigned long l) { hi = h; lo = l; }
       
   173 
       
   174 // Arithmetic op= operations involving two _compound_int.
       
   175 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator+= (const _compound_int<UnsignedWide>& rhs) { *this = U64Add( *this, rhs ); return *this; }
       
   176 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator-= (const _compound_int<UnsignedWide>& rhs) { *this = U64Subtract( *this, rhs ); return *this; }
       
   177 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator*= (const _compound_int<UnsignedWide>& rhs) { *this = U64Multiply( *this, rhs ); return *this; }
       
   178 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator/= (const _compound_int<UnsignedWide>& rhs) { *this = U64Divide( *this, rhs, NULL ); return *this; }
       
   179 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator%= (const _compound_int<UnsignedWide>& rhs) { U64Divide( *this, rhs, this ); return *this; }
       
   180 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator&= (const _compound_int<UnsignedWide>& rhs) { *this = U64BitwiseAnd( *this, rhs ); return *this; }
       
   181 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator|= (const _compound_int<UnsignedWide>& rhs) { *this = U64BitwiseOr( *this, rhs ); return *this; }
       
   182 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator^= (const _compound_int<UnsignedWide>& rhs) { *this = U64BitwiseEor( *this, rhs ); return *this; }
       
   183 
       
   184 // Arithmetic op= operations involving built-in integer.
       
   185 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator<<= (unsigned int rhs) { *this = U64ShiftLeft( *this, rhs ); return *this; }
       
   186 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator>>= (unsigned int rhs) { *this = U64ShiftRight( *this, rhs ); return *this; }
       
   187 
       
   188 // Comparison operators.
       
   189 _STLP_TEMPLATE_NULL inline bool operator==(const _compound_int<UnsignedWide>& lhs, const _compound_int<UnsignedWide>& rhs) { return (lhs.hi == rhs.hi) && (lhs.lo == rhs.lo); }
       
   190 _STLP_TEMPLATE_NULL inline bool operator< (const _compound_int<UnsignedWide>& lhs, const _compound_int<UnsignedWide>& rhs) { return U64Compare( lhs, rhs ) < 0; }
       
   191 _STLP_TEMPLATE_NULL inline bool operator==(const _compound_int<UnsignedWide>& lhs, unsigned long rhs) { return (lhs.hi == 0) && (lhs.lo == rhs); }
       
   192 
       
   193 // Unary non-member arithmetic operators.
       
   194 _STLP_TEMPLATE_NULL inline unsigned long to_ulong(const _compound_int<UnsignedWide>& val) { return val.lo; }
       
   195 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide> operator~(const _compound_int<UnsignedWide>& val) { return U64BitwiseNot( val ); }
       
   196 _STLP_TEMPLATE_NULL inline bool operator!(const _compound_int<UnsignedWide>& val) { return !((val.hi == 0) && (val.lo == 0)); }
       
   197 
       
   198 # endif // TYPE_LONGLONG
       
   199 #endif // __MRC__
       
   200 #endif // _UINT64_H