2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-09-16 23:53:12 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1998-09-16 23:53:12 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-09-16 23:53:12 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-09-16 23:53:12 +04:00
|
|
|
|
|
|
|
#ifndef nsInt64_h__
|
|
|
|
#define nsInt64_h__
|
|
|
|
|
|
|
|
#include "prlong.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class encapsulates full 64-bit integer functionality and
|
|
|
|
* provides simple arithmetic and conversion operations.
|
|
|
|
*/
|
1998-10-07 00:06:06 +04:00
|
|
|
|
|
|
|
// If you ever decide that you need to add a non-inline method to this
|
|
|
|
// class, be sure to change the class declaration to "class NS_BASE
|
|
|
|
// nsInt64".
|
|
|
|
|
2004-06-26 22:32:15 +04:00
|
|
|
template<class T>
|
|
|
|
class nsTInt64
|
1998-09-16 23:53:12 +04:00
|
|
|
{
|
2000-03-29 04:13:46 +04:00
|
|
|
public: //XXX should be private
|
2004-06-26 22:32:15 +04:00
|
|
|
T mValue;
|
1998-09-16 23:53:12 +04:00
|
|
|
|
1998-10-07 00:06:06 +04:00
|
|
|
public:
|
1998-09-16 23:53:12 +04:00
|
|
|
/**
|
|
|
|
* Construct a new 64-bit integer.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64(void) : mValue(LL_ZERO) {
|
1998-09-16 23:53:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new 64-bit integer from a 32-bit signed integer
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64(const PRInt32 aInt32) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_I2L(mValue, aInt32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new 64-bit integer from a 32-bit unsigned integer
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64(const PRUint32 aUint32) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_UI2L(mValue, aUint32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new 64-bit integer from a floating point value.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64(const PRFloat64 aFloat64) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_D2L(mValue, aFloat64);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new 64-bit integer from a native 64-bit integer
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64(const T aInt64) : mValue(aInt64) {
|
1998-09-16 23:53:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new 64-bit integer from another 64-bit integer
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64(const nsTInt64& aObject) : mValue(aObject.mValue) {
|
1998-09-16 23:53:12 +04:00
|
|
|
}
|
|
|
|
|
2004-06-26 22:32:15 +04:00
|
|
|
// ~nsTInt64(void) -- XXX destructor unnecessary
|
1998-09-16 23:53:12 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign a 64-bit integer to another 64-bit integer
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
const nsTInt64& operator =(const nsTInt64& aObject) {
|
1998-09-16 23:53:12 +04:00
|
|
|
mValue = aObject.mValue;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a 64-bit integer to a signed 32-bit value
|
|
|
|
*/
|
|
|
|
operator PRInt32(void) const {
|
|
|
|
PRInt32 result;
|
|
|
|
LL_L2I(result, mValue);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a 64-bit integer to an unsigned 32-bit value
|
|
|
|
*/
|
|
|
|
operator PRUint32(void) const {
|
|
|
|
PRUint32 result;
|
|
|
|
LL_L2UI(result, mValue);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a 64-bit integer to a floating point value
|
|
|
|
*/
|
|
|
|
operator PRFloat64(void) const {
|
|
|
|
PRFloat64 result;
|
|
|
|
LL_L2D(result, mValue);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a 64-bit integer to a native 64-bit integer.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
operator T() const {
|
1998-09-16 23:53:12 +04:00
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform unary negation on a 64-bit integer.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
const nsTInt64 operator -(void) {
|
|
|
|
nsTInt64 result;
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_NEG(result.mValue, mValue);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arithmetic operators
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment a 64-bit integer by a 64-bit integer amount.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator +=(const nsTInt64& aObject) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_ADD(mValue, mValue, aObject.mValue);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrement a 64-bit integer by a 64-bit integer amount.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator -=(const nsTInt64& aObject) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_SUB(mValue, mValue, aObject.mValue);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiply a 64-bit integer by a 64-bit integer amount.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator *=(const nsTInt64& aObject) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_MUL(mValue, mValue, aObject.mValue);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Divide a 64-bit integer by a 64-bit integer amount.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator /=(const nsTInt64& aObject) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_DIV(mValue, mValue, aObject.mValue);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the modulus of a 64-bit integer to a 64-bit value.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator %=(const nsTInt64& aObject) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_MOD(mValue, mValue, aObject.mValue);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2002-09-11 06:33:43 +04:00
|
|
|
/**
|
|
|
|
* Shift a 64-bit integer left.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator <<=(int aCount) {
|
2002-09-11 06:33:43 +04:00
|
|
|
LL_SHL(mValue, mValue, aCount);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shift a 64-bit signed integer right.
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator >>=(int aCount) {
|
2002-09-11 06:33:43 +04:00
|
|
|
LL_SHR(mValue, mValue, aCount);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1998-09-16 23:53:12 +04:00
|
|
|
// Comparison operators
|
2004-06-26 22:32:15 +04:00
|
|
|
/**
|
|
|
|
* Add two 64-bit integers.
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator +(const nsTInt64& aObject2) const {
|
|
|
|
return nsTInt64(*this) += aObject2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtract one 64-bit integer from another.
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator -(const nsTInt64& aObject2) const {
|
|
|
|
return nsTInt64(*this) -= aObject2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiply two 64-bit integers
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator *(const nsTInt64& aObject2) const {
|
|
|
|
return nsTInt64(*this) *= aObject2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Divide one 64-bit integer by another
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator /(const nsTInt64& aObject2) const {
|
|
|
|
return nsTInt64(*this) /= aObject2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the modulus of two 64-bit integers
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator %(const nsTInt64& aObject2) const {
|
|
|
|
return nsTInt64(*this) %= aObject2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shift left a 64-bit integer
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator <<(int aCount) const {
|
|
|
|
return nsTInt64(*this) <<= aCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shift right a signed 64-bit integer
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator >>(int aCount) const {
|
|
|
|
return nsTInt64(*this) >>= aCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if two 64-bit integers are equal
|
|
|
|
*/
|
|
|
|
inline PRBool
|
|
|
|
operator ==(const nsTInt64& aObject2) const {
|
|
|
|
return LL_EQ(mValue, aObject2.mValue);
|
|
|
|
}
|
|
|
|
|
2005-01-09 23:43:35 +03:00
|
|
|
inline PRBool
|
|
|
|
operator ==(T aValue) const {
|
|
|
|
return LL_EQ(mValue, aValue);
|
|
|
|
}
|
|
|
|
|
2004-06-26 22:32:15 +04:00
|
|
|
/**
|
|
|
|
* Determine if two 64-bit integers are not equal
|
|
|
|
*/
|
|
|
|
inline PRBool
|
|
|
|
operator !=(const nsTInt64& aObject2) const {
|
|
|
|
return LL_NE(mValue, aObject2.mValue);
|
|
|
|
}
|
|
|
|
|
2005-01-09 23:43:35 +03:00
|
|
|
inline PRBool
|
|
|
|
operator !=(T aValue) const {
|
|
|
|
return LL_NE(mValue, aValue);
|
|
|
|
}
|
2004-06-26 22:32:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a bitwise AND of two 64-bit integers
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator &(const nsTInt64& aObject2) const {
|
|
|
|
return nsTInt64(*this) &= aObject2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a bitwise OR of two 64-bit integers
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator |(const nsTInt64& aObject2) const {
|
|
|
|
return nsTInt64(*this) |= aObject2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a bitwise XOR of two 64-bit integers
|
|
|
|
*/
|
|
|
|
inline const nsTInt64
|
|
|
|
operator ^(const nsTInt64& aObject2) const {
|
|
|
|
return nsTInt64(*this) ^= aObject2;
|
|
|
|
}
|
1998-09-16 23:53:12 +04:00
|
|
|
|
|
|
|
// Bitwise operators
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the bitwise NOT of a 64-bit integer
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
const nsTInt64 operator ~(void) const {
|
|
|
|
nsTInt64 result;
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_NOT(result.mValue, mValue);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the bitwise AND with another 64-bit integer
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator &=(const nsTInt64& aObject) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_AND(mValue, mValue, aObject.mValue);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the bitwise OR with another 64-bit integer
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator |=(const nsTInt64& aObject) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_OR(mValue, mValue, aObject.mValue);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the bitwise XOR with another 64-bit integer
|
|
|
|
*/
|
2004-06-26 22:32:15 +04:00
|
|
|
nsTInt64& operator ^=(const nsTInt64& aObject) {
|
1998-09-16 23:53:12 +04:00
|
|
|
LL_XOR(mValue, mValue, aObject.mValue);
|
|
|
|
return *this;
|
|
|
|
}
|
2005-01-09 23:43:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow doing if (!some_nsInt64)
|
|
|
|
*/
|
|
|
|
PRBool operator!() const {
|
|
|
|
return LL_IS_ZERO(mValue);
|
|
|
|
}
|
|
|
|
|
1998-09-16 23:53:12 +04:00
|
|
|
};
|
|
|
|
|
2004-06-26 22:32:15 +04:00
|
|
|
typedef nsTInt64<PRInt64> nsInt64;
|
|
|
|
typedef nsTInt64<PRUint64> nsUint64;
|
2000-04-27 11:01:58 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if one 64-bit integer is strictly greater than another, using signed values
|
|
|
|
*/
|
|
|
|
inline PRBool
|
|
|
|
operator >(const nsInt64& aObject1, const nsInt64& aObject2) {
|
|
|
|
return LL_CMP(aObject1.mValue, >, aObject2.mValue);
|
|
|
|
}
|
|
|
|
|
2004-06-26 22:32:15 +04:00
|
|
|
inline PRBool
|
|
|
|
operator >(const nsUint64& aObject1, const nsUint64& aObject2) {
|
|
|
|
return LL_UCMP(aObject1.mValue, >, aObject2.mValue);
|
|
|
|
}
|
|
|
|
|
2000-04-27 11:01:58 +04:00
|
|
|
/**
|
|
|
|
* Determine if one 64-bit integer is greater than or equal to another, using signed values
|
|
|
|
*/
|
|
|
|
inline PRBool
|
|
|
|
operator >=(const nsInt64& aObject1, const nsInt64& aObject2) {
|
|
|
|
return ! LL_CMP(aObject1.mValue, <, aObject2.mValue);
|
|
|
|
}
|
|
|
|
|
2004-06-26 22:32:15 +04:00
|
|
|
inline PRBool
|
|
|
|
operator >=(const nsUint64& aObject1, const nsUint64& aObject2) {
|
|
|
|
return ! LL_UCMP(aObject1.mValue, <, aObject2.mValue);
|
|
|
|
}
|
|
|
|
|
1998-09-16 23:53:12 +04:00
|
|
|
/**
|
|
|
|
* Determine if one 64-bit integer is strictly less than another, using signed values
|
|
|
|
*/
|
|
|
|
inline PRBool
|
|
|
|
operator <(const nsInt64& aObject1, const nsInt64& aObject2) {
|
|
|
|
return LL_CMP(aObject1.mValue, <, aObject2.mValue);
|
|
|
|
}
|
|
|
|
|
2004-06-26 22:32:15 +04:00
|
|
|
inline PRBool
|
|
|
|
operator <(const nsUint64& aObject1, const nsUint64& aObject2) {
|
|
|
|
return LL_UCMP(aObject1.mValue, <, aObject2.mValue);
|
|
|
|
}
|
|
|
|
|
2000-04-27 11:01:58 +04:00
|
|
|
/**
|
|
|
|
* Determine if one 64-bit integers is less than or equal to another, using signed values
|
|
|
|
*/
|
|
|
|
inline PRBool
|
|
|
|
operator <=(const nsInt64& aObject1, const nsInt64& aObject2) {
|
|
|
|
return ! LL_CMP(aObject1.mValue, >, aObject2.mValue);
|
|
|
|
}
|
|
|
|
|
2004-06-26 22:32:15 +04:00
|
|
|
inline PRBool
|
|
|
|
operator <=(const nsUint64& aObject1, const nsUint64& aObject2) {
|
|
|
|
return ! LL_UCMP(aObject1.mValue, >, aObject2.mValue);
|
1998-09-16 23:53:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // nsInt64_h__
|