Back out due to mochiserver breakage. (no_r=me)
[mozilla-central.git] / xpcom / ds / nsInt64.h
blob0add2f43812aea70162c5800fe63260b0a6268da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsInt64_h__
39 #define nsInt64_h__
41 #include "prlong.h"
42 #include "nscore.h"
44 /**
45 * This class encapsulates full 64-bit integer functionality and
46 * provides simple arithmetic and conversion operations.
49 // If you ever decide that you need to add a non-inline method to this
50 // class, be sure to change the class declaration to "class NS_BASE
51 // nsInt64".
53 template<class T>
54 class nsTInt64
56 public: //XXX should be private
57 T mValue;
59 public:
60 /**
61 * Construct a new 64-bit integer.
63 nsTInt64(void) : mValue(LL_ZERO) {
66 /**
67 * Construct a new 64-bit integer from a 32-bit signed integer
69 nsTInt64(const PRInt32 aInt32) {
70 LL_I2L(mValue, aInt32);
73 /**
74 * Construct a new 64-bit integer from a 32-bit unsigned integer
76 nsTInt64(const PRUint32 aUint32) {
77 LL_UI2L(mValue, aUint32);
80 /**
81 * Construct a new 64-bit integer from a floating point value.
83 nsTInt64(const PRFloat64 aFloat64) {
84 LL_D2L(mValue, aFloat64);
87 /**
88 * Construct a new 64-bit integer from a native 64-bit integer
90 nsTInt64(const T aInt64) : mValue(aInt64) {
93 /**
94 * Construct a new 64-bit integer from another 64-bit integer
96 nsTInt64(const nsTInt64& aObject) : mValue(aObject.mValue) {
99 // ~nsTInt64(void) -- XXX destructor unnecessary
102 * Assign a 64-bit integer to another 64-bit integer
104 const nsTInt64& operator =(const nsTInt64& aObject) {
105 mValue = aObject.mValue;
106 return *this;
110 * Convert a 64-bit integer to a signed 32-bit value
112 operator PRInt32(void) const {
113 PRInt32 result;
114 LL_L2I(result, mValue);
115 return result;
119 * Convert a 64-bit integer to an unsigned 32-bit value
121 operator PRUint32(void) const {
122 PRUint32 result;
123 LL_L2UI(result, mValue);
124 return result;
128 * Convert a 64-bit integer to a floating point value
130 operator PRFloat64(void) const {
131 PRFloat64 result;
132 LL_L2D(result, mValue);
133 return result;
137 * Convert a 64-bit integer to a native 64-bit integer.
139 operator T() const {
140 return mValue;
144 * Perform unary negation on a 64-bit integer.
146 const nsTInt64 operator -(void) {
147 nsTInt64 result;
148 LL_NEG(result.mValue, mValue);
149 return result;
152 // Arithmetic operators
155 * Increment a 64-bit integer by a 64-bit integer amount.
157 nsTInt64& operator +=(const nsTInt64& aObject) {
158 LL_ADD(mValue, mValue, aObject.mValue);
159 return *this;
163 * Decrement a 64-bit integer by a 64-bit integer amount.
165 nsTInt64& operator -=(const nsTInt64& aObject) {
166 LL_SUB(mValue, mValue, aObject.mValue);
167 return *this;
171 * Multiply a 64-bit integer by a 64-bit integer amount.
173 nsTInt64& operator *=(const nsTInt64& aObject) {
174 LL_MUL(mValue, mValue, aObject.mValue);
175 return *this;
179 * Divide a 64-bit integer by a 64-bit integer amount.
181 nsTInt64& operator /=(const nsTInt64& aObject) {
182 LL_DIV(mValue, mValue, aObject.mValue);
183 return *this;
187 * Compute the modulus of a 64-bit integer to a 64-bit value.
189 nsTInt64& operator %=(const nsTInt64& aObject) {
190 LL_MOD(mValue, mValue, aObject.mValue);
191 return *this;
195 * Shift a 64-bit integer left.
197 nsTInt64& operator <<=(int aCount) {
198 LL_SHL(mValue, mValue, aCount);
199 return *this;
203 * Shift a 64-bit signed integer right.
205 nsTInt64& operator >>=(int aCount) {
206 LL_SHR(mValue, mValue, aCount);
207 return *this;
210 // Comparison operators
212 * Add two 64-bit integers.
214 inline const nsTInt64
215 operator +(const nsTInt64& aObject2) const {
216 return nsTInt64(*this) += aObject2;
220 * Subtract one 64-bit integer from another.
222 inline const nsTInt64
223 operator -(const nsTInt64& aObject2) const {
224 return nsTInt64(*this) -= aObject2;
228 * Multiply two 64-bit integers
230 inline const nsTInt64
231 operator *(const nsTInt64& aObject2) const {
232 return nsTInt64(*this) *= aObject2;
236 * Divide one 64-bit integer by another
238 inline const nsTInt64
239 operator /(const nsTInt64& aObject2) const {
240 return nsTInt64(*this) /= aObject2;
244 * Compute the modulus of two 64-bit integers
246 inline const nsTInt64
247 operator %(const nsTInt64& aObject2) const {
248 return nsTInt64(*this) %= aObject2;
252 * Shift left a 64-bit integer
254 inline const nsTInt64
255 operator <<(int aCount) const {
256 return nsTInt64(*this) <<= aCount;
260 * Shift right a signed 64-bit integer
262 inline const nsTInt64
263 operator >>(int aCount) const {
264 return nsTInt64(*this) >>= aCount;
268 * Determine if two 64-bit integers are equal
270 inline PRBool
271 operator ==(const nsTInt64& aObject2) const {
272 return LL_EQ(mValue, aObject2.mValue);
275 inline PRBool
276 operator ==(T aValue) const {
277 return LL_EQ(mValue, aValue);
281 * Determine if two 64-bit integers are not equal
283 inline PRBool
284 operator !=(const nsTInt64& aObject2) const {
285 return LL_NE(mValue, aObject2.mValue);
288 inline PRBool
289 operator !=(T aValue) const {
290 return LL_NE(mValue, aValue);
294 * Perform a bitwise AND of two 64-bit integers
296 inline const nsTInt64
297 operator &(const nsTInt64& aObject2) const {
298 return nsTInt64(*this) &= aObject2;
302 * Perform a bitwise OR of two 64-bit integers
304 inline const nsTInt64
305 operator |(const nsTInt64& aObject2) const {
306 return nsTInt64(*this) |= aObject2;
310 * Perform a bitwise XOR of two 64-bit integers
312 inline const nsTInt64
313 operator ^(const nsTInt64& aObject2) const {
314 return nsTInt64(*this) ^= aObject2;
317 // Bitwise operators
320 * Compute the bitwise NOT of a 64-bit integer
322 const nsTInt64 operator ~(void) const {
323 nsTInt64 result;
324 LL_NOT(result.mValue, mValue);
325 return result;
329 * Compute the bitwise AND with another 64-bit integer
331 nsTInt64& operator &=(const nsTInt64& aObject) {
332 LL_AND(mValue, mValue, aObject.mValue);
333 return *this;
337 * Compute the bitwise OR with another 64-bit integer
339 nsTInt64& operator |=(const nsTInt64& aObject) {
340 LL_OR(mValue, mValue, aObject.mValue);
341 return *this;
345 * Compute the bitwise XOR with another 64-bit integer
347 nsTInt64& operator ^=(const nsTInt64& aObject) {
348 LL_XOR(mValue, mValue, aObject.mValue);
349 return *this;
354 * Allow doing if (!some_nsInt64)
356 PRBool operator!() const {
357 return LL_IS_ZERO(mValue);
362 typedef nsTInt64<PRInt64> nsInt64;
363 typedef nsTInt64<PRUint64> nsUint64;
366 * Determine if one 64-bit integer is strictly greater than another, using signed values
368 inline PRBool
369 operator >(const nsInt64& aObject1, const nsInt64& aObject2) {
370 return LL_CMP(aObject1.mValue, >, aObject2.mValue);
373 inline PRBool
374 operator >(const nsUint64& aObject1, const nsUint64& aObject2) {
375 return LL_UCMP(aObject1.mValue, >, aObject2.mValue);
379 * Determine if one 64-bit integer is greater than or equal to another, using signed values
381 inline PRBool
382 operator >=(const nsInt64& aObject1, const nsInt64& aObject2) {
383 return ! LL_CMP(aObject1.mValue, <, aObject2.mValue);
386 inline PRBool
387 operator >=(const nsUint64& aObject1, const nsUint64& aObject2) {
388 return ! LL_UCMP(aObject1.mValue, <, aObject2.mValue);
392 * Determine if one 64-bit integer is strictly less than another, using signed values
394 inline PRBool
395 operator <(const nsInt64& aObject1, const nsInt64& aObject2) {
396 return LL_CMP(aObject1.mValue, <, aObject2.mValue);
399 inline PRBool
400 operator <(const nsUint64& aObject1, const nsUint64& aObject2) {
401 return LL_UCMP(aObject1.mValue, <, aObject2.mValue);
405 * Determine if one 64-bit integers is less than or equal to another, using signed values
407 inline PRBool
408 operator <=(const nsInt64& aObject1, const nsInt64& aObject2) {
409 return ! LL_CMP(aObject1.mValue, >, aObject2.mValue);
412 inline PRBool
413 operator <=(const nsUint64& aObject1, const nsUint64& aObject2) {
414 return ! LL_UCMP(aObject1.mValue, >, aObject2.mValue);
417 #endif // nsInt64_h__