os/ossrv/stdcpp/src/uint64.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
//	uint64.h
sl@0
     2
//	minimal double precision unsigned int arithmetics for numeric_facets support.
sl@0
     3
//	Written by Tsutomu Yoshida, Minokamo, Japan. 03/25/2000
sl@0
     4
sl@0
     5
#ifndef _UINT64_H
sl@0
     6
#define _UINT64_H
sl@0
     7
sl@0
     8
sl@0
     9
sl@0
    10
template <class _Tp>
sl@0
    11
class _compound_int : public _Tp
sl@0
    12
{
sl@0
    13
public:
sl@0
    14
  typedef _compound_int<_Tp> _Self;
sl@0
    15
sl@0
    16
  // Constructors, destructor, assignment operator.
sl@0
    17
  _compound_int();										// platform specific
sl@0
    18
  _compound_int(unsigned long);							// platform specific
sl@0
    19
  _compound_int(unsigned long hi, unsigned long lo);	// platform specific
sl@0
    20
  _compound_int(const _Tp& rhs) : _Tp(rhs) {}
sl@0
    21
sl@0
    22
  // Arithmetic op= operations involving two _compound_int.
sl@0
    23
  _Self& operator+= (const _Self&);						// platform specific
sl@0
    24
  _Self& operator-= (const _Self&);						// platform specific
sl@0
    25
  _Self& operator*= (const _Self&);						// platform specific
sl@0
    26
  _Self& operator/= (const _Self&);						// platform specific
sl@0
    27
  _Self& operator%= (const _Self&);						// platform specific
sl@0
    28
  _Self& operator&= (const _Self&);						// platform specific
sl@0
    29
  _Self& operator|= (const _Self&);						// platform specific
sl@0
    30
  _Self& operator^= (const _Self&);						// platform specific
sl@0
    31
sl@0
    32
  // Arithmetic op= operations involving built-in integer.
sl@0
    33
  _Self& operator<<= (unsigned int);					// platform specific
sl@0
    34
  _Self& operator>>= (unsigned int);					// platform specific
sl@0
    35
  
sl@0
    36
  _Self& operator=  (unsigned long rhs) { return *this =  _Self(rhs); }
sl@0
    37
  _Self& operator+= (unsigned long rhs) { return *this += _Self(rhs); }
sl@0
    38
  _Self& operator-= (unsigned long rhs) { return *this -= _Self(rhs); }
sl@0
    39
  _Self& operator*= (unsigned long rhs) { return *this *= _Self(rhs); }
sl@0
    40
  _Self& operator/= (unsigned long rhs) { return *this /= _Self(rhs); }
sl@0
    41
  _Self& operator%= (unsigned long rhs) { return *this %= _Self(rhs); }
sl@0
    42
  _Self& operator&= (unsigned long rhs) { return *this &= _Self(rhs); }
sl@0
    43
  _Self& operator|= (unsigned long rhs) { return *this |= _Self(rhs); }
sl@0
    44
  _Self& operator^= (unsigned long rhs) { return *this ^= _Self(rhs); }
sl@0
    45
sl@0
    46
  // Increment and decrement
sl@0
    47
  _Self& operator++() { return (*this) += 1; }
sl@0
    48
  _Self& operator--() { return (*this) -= 1; }
sl@0
    49
  _Self operator++(int) { _Self temp(*this); ++(*this); return temp; }
sl@0
    50
  _Self operator--(int) { _Self temp(*this); --(*this); return temp; }
sl@0
    51
};
sl@0
    52
sl@0
    53
// Comparison operators.
sl@0
    54
template <class _Tp> bool operator==(const _compound_int<_Tp>&, const _compound_int<_Tp>&);	// platform specific
sl@0
    55
template <class _Tp> bool operator<(const _compound_int<_Tp>&, const _compound_int<_Tp>&);	// platform specific
sl@0
    56
sl@0
    57
template <class _Tp> inline bool operator==(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs == _compound_int<_Tp>(rhs); }
sl@0
    58
template <class _Tp> inline bool operator==(unsigned long lhs, const _compound_int<_Tp>& rhs) { return rhs == lhs; }
sl@0
    59
sl@0
    60
template <class _Tp> inline bool operator< (const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs < _compound_int<_Tp>(rhs); }
sl@0
    61
template <class _Tp> inline bool operator< (unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) < rhs; }
sl@0
    62
sl@0
    63
template <class _Tp> inline bool operator!=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs == rhs); }
sl@0
    64
template <class _Tp> inline bool operator!=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs == rhs); }
sl@0
    65
sl@0
    66
template <class _Tp> inline bool operator> (const _compound_int<_Tp>& lhs, unsigned long rhs) { return rhs < lhs; }
sl@0
    67
template <class _Tp> inline bool operator> (unsigned long lhs, const _compound_int<_Tp>& rhs) { return rhs < lhs; }
sl@0
    68
sl@0
    69
template <class _Tp> inline bool operator<=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs > rhs); }
sl@0
    70
template <class _Tp> inline bool operator<=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs > rhs); }
sl@0
    71
sl@0
    72
template <class _Tp> inline bool operator>=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs < rhs); }
sl@0
    73
template <class _Tp> inline bool operator>=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs < rhs); }
sl@0
    74
sl@0
    75
// Unary non-member arithmetic operators.
sl@0
    76
template <class _Tp> unsigned long to_ulong(const _compound_int<_Tp>&);			// platform specific
sl@0
    77
template <class _Tp> _compound_int<_Tp> operator~(const _compound_int<_Tp>&);	// platform specific
sl@0
    78
sl@0
    79
template <class _Tp> inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& val) {return val;}
sl@0
    80
template <class _Tp> inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& val) {return 0 - val;}
sl@0
    81
template <class _Tp> inline bool operator!(const _compound_int<_Tp>& val) {return val==0;}
sl@0
    82
sl@0
    83
// Non-member arithmetic operations involving two _compound_int arguments
sl@0
    84
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; }
sl@0
    85
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; }
sl@0
    86
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; }
sl@0
    87
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; }
sl@0
    88
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; }
sl@0
    89
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; }
sl@0
    90
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; }
sl@0
    91
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; }
sl@0
    92
sl@0
    93
// Non-member arithmetic operations involving one built-in integer argument.
sl@0
    94
template <class _Tp> inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs + _compound_int<_Tp>(rhs); }
sl@0
    95
template <class _Tp> inline _compound_int<_Tp> operator+(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) + rhs; }
sl@0
    96
sl@0
    97
template <class _Tp> inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs - _compound_int<_Tp>(rhs); }
sl@0
    98
template <class _Tp> inline _compound_int<_Tp> operator-(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) - rhs; }
sl@0
    99
sl@0
   100
template <class _Tp> inline _compound_int<_Tp> operator*(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs * _compound_int<_Tp>(rhs); }
sl@0
   101
template <class _Tp> inline _compound_int<_Tp> operator*(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) * rhs; }
sl@0
   102
sl@0
   103
template <class _Tp> inline _compound_int<_Tp> operator/(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs / _compound_int<_Tp>(rhs); }
sl@0
   104
template <class _Tp> inline _compound_int<_Tp> operator/(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) / rhs; }
sl@0
   105
sl@0
   106
template <class _Tp> inline _compound_int<_Tp> operator%(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs % _compound_int<_Tp>(rhs); }
sl@0
   107
template <class _Tp> inline _compound_int<_Tp> operator%(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) % rhs; }
sl@0
   108
sl@0
   109
template <class _Tp> inline _compound_int<_Tp> operator&(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs & _compound_int<_Tp>(rhs); }
sl@0
   110
template <class _Tp> inline _compound_int<_Tp> operator&(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) & rhs; }
sl@0
   111
sl@0
   112
template <class _Tp> inline _compound_int<_Tp> operator|(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs | _compound_int<_Tp>(rhs); }
sl@0
   113
template <class _Tp> inline _compound_int<_Tp> operator|(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) | rhs; }
sl@0
   114
sl@0
   115
template <class _Tp> inline _compound_int<_Tp> operator^(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs ^ _compound_int<_Tp>(rhs); }
sl@0
   116
template <class _Tp> inline _compound_int<_Tp> operator^(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) ^ rhs; }
sl@0
   117
sl@0
   118
template <class _Tp> inline _compound_int<_Tp> operator<<(const _compound_int<_Tp>& lhs, unsigned int rhs) { _compound_int<_Tp> temp(lhs); return temp <<= rhs; }
sl@0
   119
template <class _Tp> inline _compound_int<_Tp> operator>>(const _compound_int<_Tp>& lhs, unsigned int rhs) { _compound_int<_Tp> temp(lhs); return temp >>= rhs; }
sl@0
   120
sl@0
   121
sl@0
   122
sl@0
   123
sl@0
   124
// platform specific specializations
sl@0
   125
sl@0
   126
#if defined(__MRC__)||defined(__SC__)
sl@0
   127
sl@0
   128
_STLP_END_NAMESPACE // ugly!
sl@0
   129
# include <Math64.h>
sl@0
   130
# include <utility>
sl@0
   131
# undef modff		//*TY 04/06/2000 - defined in <math.h> which conflicts with <fp.h> definition
sl@0
   132
# include <fp.h>
sl@0
   133
sl@0
   134
_STLP_BEGIN_NAMESPACE
sl@0
   135
sl@0
   136
# if TYPE_LONGLONG 
sl@0
   137
typedef UInt64 uint64;
sl@0
   138
# define ULL(x) (U64SetU(x))
sl@0
   139
sl@0
   140
# else
sl@0
   141
//	Apple's mpw sc compiler for 68k macintosh does not support native 64bit integer type. 
sl@0
   142
//	Instead, it comes with external support library and struct data type representing 64bit int:
sl@0
   143
//	
sl@0
   144
//		struct UnsignedWide {
sl@0
   145
//			UInt32 	hi;
sl@0
   146
//			UInt32 	lo;
sl@0
   147
//		};
sl@0
   148
sl@0
   149
typedef _compound_int<UnsignedWide> uint64;
sl@0
   150
# define ULL(x) uint64(x)
sl@0
   151
# define ULL2(hi,lo) {hi,lo}
sl@0
   152
sl@0
   153
// Constructors, destructor, assignment operator.
sl@0
   154
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int() { hi = 0; lo = 0; }
sl@0
   155
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int(unsigned long val) { hi = 0; lo = val; }
sl@0
   156
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int(unsigned long h, unsigned long l) { hi = h; lo = l; }
sl@0
   157
sl@0
   158
// Arithmetic op= operations involving two _compound_int.
sl@0
   159
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator+= (const _compound_int<UnsignedWide>& rhs) { *this = U64Add( *this, rhs ); return *this; }
sl@0
   160
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator-= (const _compound_int<UnsignedWide>& rhs) { *this = U64Subtract( *this, rhs ); return *this; }
sl@0
   161
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator*= (const _compound_int<UnsignedWide>& rhs) { *this = U64Multiply( *this, rhs ); return *this; }
sl@0
   162
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator/= (const _compound_int<UnsignedWide>& rhs) { *this = U64Divide( *this, rhs, NULL ); return *this; }
sl@0
   163
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator%= (const _compound_int<UnsignedWide>& rhs) { U64Divide( *this, rhs, this ); return *this; }
sl@0
   164
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator&= (const _compound_int<UnsignedWide>& rhs) { *this = U64BitwiseAnd( *this, rhs ); return *this; }
sl@0
   165
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator|= (const _compound_int<UnsignedWide>& rhs) { *this = U64BitwiseOr( *this, rhs ); return *this; }
sl@0
   166
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator^= (const _compound_int<UnsignedWide>& rhs) { *this = U64BitwiseEor( *this, rhs ); return *this; }
sl@0
   167
sl@0
   168
// Arithmetic op= operations involving built-in integer.
sl@0
   169
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator<<= (unsigned int rhs) { *this = U64ShiftLeft( *this, rhs ); return *this; }
sl@0
   170
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator>>= (unsigned int rhs) { *this = U64ShiftRight( *this, rhs ); return *this; }
sl@0
   171
sl@0
   172
// Comparison operators.
sl@0
   173
_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); }
sl@0
   174
_STLP_TEMPLATE_NULL inline bool operator< (const _compound_int<UnsignedWide>& lhs, const _compound_int<UnsignedWide>& rhs) { return U64Compare( lhs, rhs ) < 0; }
sl@0
   175
_STLP_TEMPLATE_NULL inline bool operator==(const _compound_int<UnsignedWide>& lhs, unsigned long rhs) { return (lhs.hi == 0) && (lhs.lo == rhs); }
sl@0
   176
sl@0
   177
// Unary non-member arithmetic operators.
sl@0
   178
_STLP_TEMPLATE_NULL inline unsigned long to_ulong(const _compound_int<UnsignedWide>& val) { return val.lo; }
sl@0
   179
_STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide> operator~(const _compound_int<UnsignedWide>& val) { return U64BitwiseNot( val ); }
sl@0
   180
_STLP_TEMPLATE_NULL inline bool operator!(const _compound_int<UnsignedWide>& val) { return !((val.hi == 0) && (val.lo == 0)); }
sl@0
   181
sl@0
   182
# endif // TYPE_LONGLONG
sl@0
   183
#endif // __MRC__
sl@0
   184
#endif // _UINT64_H