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
|
---|
4 | *
|
---|
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/
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
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.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
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.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #ifndef nsInt64_h__
|
---|
39 | #define nsInt64_h__
|
---|
40 |
|
---|
41 | #include "prlong.h"
|
---|
42 | #include "nscore.h"
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * This class encapsulates full 64-bit integer functionality and
|
---|
46 | * provides simple arithmetic and conversion operations.
|
---|
47 | */
|
---|
48 |
|
---|
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".
|
---|
52 |
|
---|
53 | template<class T>
|
---|
54 | class nsTInt64
|
---|
55 | {
|
---|
56 | public: //XXX should be private
|
---|
57 | T mValue;
|
---|
58 |
|
---|
59 | public:
|
---|
60 | /**
|
---|
61 | * Construct a new 64-bit integer.
|
---|
62 | */
|
---|
63 | nsTInt64(void) : mValue(LL_ZERO) {
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Construct a new 64-bit integer from a 32-bit signed integer
|
---|
68 | */
|
---|
69 | nsTInt64(const PRInt32 aInt32) {
|
---|
70 | LL_I2L(mValue, aInt32);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Construct a new 64-bit integer from a 32-bit unsigned integer
|
---|
75 | */
|
---|
76 | nsTInt64(const PRUint32 aUint32) {
|
---|
77 | LL_UI2L(mValue, aUint32);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Construct a new 64-bit integer from a floating point value.
|
---|
82 | */
|
---|
83 | nsTInt64(const PRFloat64 aFloat64) {
|
---|
84 | LL_D2L(mValue, aFloat64);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Construct a new 64-bit integer from a native 64-bit integer
|
---|
89 | */
|
---|
90 | nsTInt64(const T aInt64) : mValue(aInt64) {
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Construct a new 64-bit integer from another 64-bit integer
|
---|
95 | */
|
---|
96 | nsTInt64(const nsTInt64& aObject) : mValue(aObject.mValue) {
|
---|
97 | }
|
---|
98 |
|
---|
99 | // ~nsTInt64(void) -- XXX destructor unnecessary
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Assign a 64-bit integer to another 64-bit integer
|
---|
103 | */
|
---|
104 | const nsTInt64& operator =(const nsTInt64& aObject) {
|
---|
105 | mValue = aObject.mValue;
|
---|
106 | return *this;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Convert a 64-bit integer to a signed 32-bit value
|
---|
111 | */
|
---|
112 | operator PRInt32(void) const {
|
---|
113 | PRInt32 result;
|
---|
114 | LL_L2I(result, mValue);
|
---|
115 | return result;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Convert a 64-bit integer to an unsigned 32-bit value
|
---|
120 | */
|
---|
121 | operator PRUint32(void) const {
|
---|
122 | PRUint32 result;
|
---|
123 | LL_L2UI(result, mValue);
|
---|
124 | return result;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Convert a 64-bit integer to a floating point value
|
---|
129 | */
|
---|
130 | operator PRFloat64(void) const {
|
---|
131 | PRFloat64 result;
|
---|
132 | LL_L2D(result, mValue);
|
---|
133 | return result;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Convert a 64-bit integer to a native 64-bit integer.
|
---|
138 | */
|
---|
139 | operator T() const {
|
---|
140 | return mValue;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Perform unary negation on a 64-bit integer.
|
---|
145 | */
|
---|
146 | const nsTInt64 operator -(void) {
|
---|
147 | nsTInt64 result;
|
---|
148 | LL_NEG(result.mValue, mValue);
|
---|
149 | return result;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // Arithmetic operators
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Increment a 64-bit integer by a 64-bit integer amount.
|
---|
156 | */
|
---|
157 | nsTInt64& operator +=(const nsTInt64& aObject) {
|
---|
158 | LL_ADD(mValue, mValue, aObject.mValue);
|
---|
159 | return *this;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Decrement a 64-bit integer by a 64-bit integer amount.
|
---|
164 | */
|
---|
165 | nsTInt64& operator -=(const nsTInt64& aObject) {
|
---|
166 | LL_SUB(mValue, mValue, aObject.mValue);
|
---|
167 | return *this;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Multiply a 64-bit integer by a 64-bit integer amount.
|
---|
172 | */
|
---|
173 | nsTInt64& operator *=(const nsTInt64& aObject) {
|
---|
174 | LL_MUL(mValue, mValue, aObject.mValue);
|
---|
175 | return *this;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Divide a 64-bit integer by a 64-bit integer amount.
|
---|
180 | */
|
---|
181 | nsTInt64& operator /=(const nsTInt64& aObject) {
|
---|
182 | LL_DIV(mValue, mValue, aObject.mValue);
|
---|
183 | return *this;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Compute the modulus of a 64-bit integer to a 64-bit value.
|
---|
188 | */
|
---|
189 | nsTInt64& operator %=(const nsTInt64& aObject) {
|
---|
190 | LL_MOD(mValue, mValue, aObject.mValue);
|
---|
191 | return *this;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Shift a 64-bit integer left.
|
---|
196 | */
|
---|
197 | nsTInt64& operator <<=(int aCount) {
|
---|
198 | LL_SHL(mValue, mValue, aCount);
|
---|
199 | return *this;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Shift a 64-bit signed integer right.
|
---|
204 | */
|
---|
205 | nsTInt64& operator >>=(int aCount) {
|
---|
206 | LL_SHR(mValue, mValue, aCount);
|
---|
207 | return *this;
|
---|
208 | }
|
---|
209 |
|
---|
210 | // Comparison operators
|
---|
211 | /**
|
---|
212 | * Add two 64-bit integers.
|
---|
213 | */
|
---|
214 | inline const nsTInt64
|
---|
215 | operator +(const nsTInt64& aObject2) const {
|
---|
216 | return nsTInt64(*this) += aObject2;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Subtract one 64-bit integer from another.
|
---|
221 | */
|
---|
222 | inline const nsTInt64
|
---|
223 | operator -(const nsTInt64& aObject2) const {
|
---|
224 | return nsTInt64(*this) -= aObject2;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Multiply two 64-bit integers
|
---|
229 | */
|
---|
230 | inline const nsTInt64
|
---|
231 | operator *(const nsTInt64& aObject2) const {
|
---|
232 | return nsTInt64(*this) *= aObject2;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Divide one 64-bit integer by another
|
---|
237 | */
|
---|
238 | inline const nsTInt64
|
---|
239 | operator /(const nsTInt64& aObject2) const {
|
---|
240 | return nsTInt64(*this) /= aObject2;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Compute the modulus of two 64-bit integers
|
---|
245 | */
|
---|
246 | inline const nsTInt64
|
---|
247 | operator %(const nsTInt64& aObject2) const {
|
---|
248 | return nsTInt64(*this) %= aObject2;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Shift left a 64-bit integer
|
---|
253 | */
|
---|
254 | inline const nsTInt64
|
---|
255 | operator <<(int aCount) const {
|
---|
256 | return nsTInt64(*this) <<= aCount;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Shift right a signed 64-bit integer
|
---|
261 | */
|
---|
262 | inline const nsTInt64
|
---|
263 | operator >>(int aCount) const {
|
---|
264 | return nsTInt64(*this) >>= aCount;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Determine if two 64-bit integers are equal
|
---|
269 | */
|
---|
270 | inline PRBool
|
---|
271 | operator ==(const nsTInt64& aObject2) const {
|
---|
272 | return LL_EQ(mValue, aObject2.mValue);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Determine if two 64-bit integers are not equal
|
---|
277 | */
|
---|
278 | inline PRBool
|
---|
279 | operator !=(const nsTInt64& aObject2) const {
|
---|
280 | return LL_NE(mValue, aObject2.mValue);
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Perform a bitwise AND of two 64-bit integers
|
---|
286 | */
|
---|
287 | inline const nsTInt64
|
---|
288 | operator &(const nsTInt64& aObject2) const {
|
---|
289 | return nsTInt64(*this) &= aObject2;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Perform a bitwise OR of two 64-bit integers
|
---|
294 | */
|
---|
295 | inline const nsTInt64
|
---|
296 | operator |(const nsTInt64& aObject2) const {
|
---|
297 | return nsTInt64(*this) |= aObject2;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Perform a bitwise XOR of two 64-bit integers
|
---|
302 | */
|
---|
303 | inline const nsTInt64
|
---|
304 | operator ^(const nsTInt64& aObject2) const {
|
---|
305 | return nsTInt64(*this) ^= aObject2;
|
---|
306 | }
|
---|
307 |
|
---|
308 | // Bitwise operators
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Compute the bitwise NOT of a 64-bit integer
|
---|
312 | */
|
---|
313 | const nsTInt64 operator ~(void) const {
|
---|
314 | nsTInt64 result;
|
---|
315 | LL_NOT(result.mValue, mValue);
|
---|
316 | return result;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Compute the bitwise AND with another 64-bit integer
|
---|
321 | */
|
---|
322 | nsTInt64& operator &=(const nsTInt64& aObject) {
|
---|
323 | LL_AND(mValue, mValue, aObject.mValue);
|
---|
324 | return *this;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Compute the bitwise OR with another 64-bit integer
|
---|
329 | */
|
---|
330 | nsTInt64& operator |=(const nsTInt64& aObject) {
|
---|
331 | LL_OR(mValue, mValue, aObject.mValue);
|
---|
332 | return *this;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Compute the bitwise XOR with another 64-bit integer
|
---|
337 | */
|
---|
338 | nsTInt64& operator ^=(const nsTInt64& aObject) {
|
---|
339 | LL_XOR(mValue, mValue, aObject.mValue);
|
---|
340 | return *this;
|
---|
341 | }
|
---|
342 | };
|
---|
343 |
|
---|
344 | typedef nsTInt64<PRInt64> nsInt64;
|
---|
345 | typedef nsTInt64<PRUint64> nsUint64;
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Determine if one 64-bit integer is strictly greater than another, using signed values
|
---|
349 | */
|
---|
350 | inline PRBool
|
---|
351 | operator >(const nsInt64& aObject1, const nsInt64& aObject2) {
|
---|
352 | return LL_CMP(aObject1.mValue, >, aObject2.mValue);
|
---|
353 | }
|
---|
354 |
|
---|
355 | inline PRBool
|
---|
356 | operator >(const nsUint64& aObject1, const nsUint64& aObject2) {
|
---|
357 | return LL_UCMP(aObject1.mValue, >, aObject2.mValue);
|
---|
358 | }
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Determine if one 64-bit integer is greater than or equal to another, using signed values
|
---|
362 | */
|
---|
363 | inline PRBool
|
---|
364 | operator >=(const nsInt64& aObject1, const nsInt64& aObject2) {
|
---|
365 | return ! LL_CMP(aObject1.mValue, <, aObject2.mValue);
|
---|
366 | }
|
---|
367 |
|
---|
368 | inline PRBool
|
---|
369 | operator >=(const nsUint64& aObject1, const nsUint64& aObject2) {
|
---|
370 | return ! LL_UCMP(aObject1.mValue, <, aObject2.mValue);
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Determine if one 64-bit integer is strictly less than another, using signed values
|
---|
375 | */
|
---|
376 | inline PRBool
|
---|
377 | operator <(const nsInt64& aObject1, const nsInt64& aObject2) {
|
---|
378 | return LL_CMP(aObject1.mValue, <, aObject2.mValue);
|
---|
379 | }
|
---|
380 |
|
---|
381 | inline PRBool
|
---|
382 | operator <(const nsUint64& aObject1, const nsUint64& aObject2) {
|
---|
383 | return LL_UCMP(aObject1.mValue, <, aObject2.mValue);
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Determine if one 64-bit integers is less than or equal to another, using signed values
|
---|
388 | */
|
---|
389 | inline PRBool
|
---|
390 | operator <=(const nsInt64& aObject1, const nsInt64& aObject2) {
|
---|
391 | return ! LL_CMP(aObject1.mValue, >, aObject2.mValue);
|
---|
392 | }
|
---|
393 |
|
---|
394 | inline PRBool
|
---|
395 | operator <=(const nsUint64& aObject1, const nsUint64& aObject2) {
|
---|
396 | return ! LL_UCMP(aObject1.mValue, >, aObject2.mValue);
|
---|
397 | }
|
---|
398 |
|
---|
399 | #endif // nsInt64_h__
|
---|