1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
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 the Netscape Portable Runtime (NSPR).
|
---|
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-2000
|
---|
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 the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * 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 | /*
|
---|
39 | ** File: prlong.h
|
---|
40 | ** Description: Portable access to 64 bit numerics
|
---|
41 | **
|
---|
42 | ** Long-long (64-bit signed integer type) support. Some C compilers
|
---|
43 | ** don't support 64 bit integers yet, so we use these macros to
|
---|
44 | ** support both machines that do and don't.
|
---|
45 | **/
|
---|
46 | #ifndef prlong_h___
|
---|
47 | #define prlong_h___
|
---|
48 |
|
---|
49 | #include "prtypes.h"
|
---|
50 |
|
---|
51 | PR_BEGIN_EXTERN_C
|
---|
52 |
|
---|
53 | /***********************************************************************
|
---|
54 | ** DEFINES: LL_MaxInt
|
---|
55 | ** LL_MinInt
|
---|
56 | ** LL_Zero
|
---|
57 | ** LL_MaxUint
|
---|
58 | ** DESCRIPTION:
|
---|
59 | ** Various interesting constants and static variable
|
---|
60 | ** initializer
|
---|
61 | ***********************************************************************/
|
---|
62 | #if defined(HAVE_WATCOM_BUG_2)
|
---|
63 | PRInt64 __pascal __loadds __export
|
---|
64 | LL_MaxInt(void);
|
---|
65 | PRInt64 __pascal __loadds __export
|
---|
66 | LL_MinInt(void);
|
---|
67 | PRInt64 __pascal __loadds __export
|
---|
68 | LL_Zero(void);
|
---|
69 | PRUint64 __pascal __loadds __export
|
---|
70 | LL_MaxUint(void);
|
---|
71 | #else
|
---|
72 | NSPR_API(PRInt64) LL_MaxInt(void);
|
---|
73 | NSPR_API(PRInt64) LL_MinInt(void);
|
---|
74 | NSPR_API(PRInt64) LL_Zero(void);
|
---|
75 | NSPR_API(PRUint64) LL_MaxUint(void);
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | #define LL_MAXINT LL_MaxInt()
|
---|
79 | #define LL_MININT LL_MinInt()
|
---|
80 | #define LL_ZERO LL_Zero()
|
---|
81 | #define LL_MAXUINT LL_MaxUint()
|
---|
82 |
|
---|
83 | #if defined(HAVE_LONG_LONG)
|
---|
84 |
|
---|
85 | #if PR_BYTES_PER_LONG == 8
|
---|
86 | #define LL_INIT(hi, lo) ((hi ## L << 32) + lo ## L)
|
---|
87 | #elif (defined(WIN32) || defined(WIN16)) && !defined(__GNUC__)
|
---|
88 | #define LL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64)
|
---|
89 | #else
|
---|
90 | #define LL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL)
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | /***********************************************************************
|
---|
94 | ** MACROS: LL_*
|
---|
95 | ** DESCRIPTION:
|
---|
96 | ** The following macros define portable access to the 64 bit
|
---|
97 | ** math facilities.
|
---|
98 | **
|
---|
99 | ***********************************************************************/
|
---|
100 |
|
---|
101 | /***********************************************************************
|
---|
102 | ** MACROS: LL_<relational operators>
|
---|
103 | **
|
---|
104 | ** LL_IS_ZERO Test for zero
|
---|
105 | ** LL_EQ Test for equality
|
---|
106 | ** LL_NE Test for inequality
|
---|
107 | ** LL_GE_ZERO Test for zero or positive
|
---|
108 | ** LL_CMP Compare two values
|
---|
109 | ***********************************************************************/
|
---|
110 | #define LL_IS_ZERO(a) ((a) == 0)
|
---|
111 | #define LL_EQ(a, b) ((a) == (b))
|
---|
112 | #define LL_NE(a, b) ((a) != (b))
|
---|
113 | #define LL_GE_ZERO(a) ((a) >= 0)
|
---|
114 | #define LL_CMP(a, op, b) ((PRInt64)(a) op (PRInt64)(b))
|
---|
115 | #define LL_UCMP(a, op, b) ((PRUint64)(a) op (PRUint64)(b))
|
---|
116 |
|
---|
117 | /***********************************************************************
|
---|
118 | ** MACROS: LL_<logical operators>
|
---|
119 | **
|
---|
120 | ** LL_AND Logical and
|
---|
121 | ** LL_OR Logical or
|
---|
122 | ** LL_XOR Logical exclusion
|
---|
123 | ** LL_OR2 A disgusting deviation
|
---|
124 | ** LL_NOT Negation (one's complement)
|
---|
125 | ***********************************************************************/
|
---|
126 | #define LL_AND(r, a, b) ((r) = (a) & (b))
|
---|
127 | #define LL_OR(r, a, b) ((r) = (a) | (b))
|
---|
128 | #define LL_XOR(r, a, b) ((r) = (a) ^ (b))
|
---|
129 | #define LL_OR2(r, a) ((r) = (r) | (a))
|
---|
130 | #define LL_NOT(r, a) ((r) = ~(a))
|
---|
131 |
|
---|
132 | /***********************************************************************
|
---|
133 | ** MACROS: LL_<mathematical operators>
|
---|
134 | **
|
---|
135 | ** LL_NEG Negation (two's complement)
|
---|
136 | ** LL_ADD Summation (two's complement)
|
---|
137 | ** LL_SUB Difference (two's complement)
|
---|
138 | ***********************************************************************/
|
---|
139 | #define LL_NEG(r, a) ((r) = -(a))
|
---|
140 | #define LL_ADD(r, a, b) ((r) = (a) + (b))
|
---|
141 | #define LL_SUB(r, a, b) ((r) = (a) - (b))
|
---|
142 |
|
---|
143 | /***********************************************************************
|
---|
144 | ** MACROS: LL_<mathematical operators>
|
---|
145 | **
|
---|
146 | ** LL_MUL Product (two's complement)
|
---|
147 | ** LL_DIV Quotient (two's complement)
|
---|
148 | ** LL_MOD Modulus (two's complement)
|
---|
149 | ***********************************************************************/
|
---|
150 | #define LL_MUL(r, a, b) ((r) = (a) * (b))
|
---|
151 | #define LL_DIV(r, a, b) ((r) = (a) / (b))
|
---|
152 | #define LL_MOD(r, a, b) ((r) = (a) % (b))
|
---|
153 |
|
---|
154 | /***********************************************************************
|
---|
155 | ** MACROS: LL_<shifting operators>
|
---|
156 | **
|
---|
157 | ** LL_SHL Shift left [0..64] bits
|
---|
158 | ** LL_SHR Shift right [0..64] bits with sign extension
|
---|
159 | ** LL_USHR Unsigned shift right [0..64] bits
|
---|
160 | ** LL_ISHL Signed shift left [0..64] bits
|
---|
161 | ***********************************************************************/
|
---|
162 | #define LL_SHL(r, a, b) ((r) = (PRInt64)(a) << (b))
|
---|
163 | #define LL_SHR(r, a, b) ((r) = (PRInt64)(a) >> (b))
|
---|
164 | #define LL_USHR(r, a, b) ((r) = (PRUint64)(a) >> (b))
|
---|
165 | #define LL_ISHL(r, a, b) ((r) = (PRInt64)(a) << (b))
|
---|
166 |
|
---|
167 | /***********************************************************************
|
---|
168 | ** MACROS: LL_<conversion operators>
|
---|
169 | **
|
---|
170 | ** LL_L2I Convert to signed 32 bit
|
---|
171 | ** LL_L2UI Convert to unsigned 32 bit
|
---|
172 | ** LL_L2F Convert to floating point
|
---|
173 | ** LL_L2D Convert to floating point
|
---|
174 | ** LL_I2L Convert signed to 64 bit
|
---|
175 | ** LL_UI2L Convert unsigned to 64 bit
|
---|
176 | ** LL_F2L Convert float to 64 bit
|
---|
177 | ** LL_D2L Convert float to 64 bit
|
---|
178 | ***********************************************************************/
|
---|
179 | #define LL_L2I(i, l) ((i) = (PRInt32)(l))
|
---|
180 | #define LL_L2UI(ui, l) ((ui) = (PRUint32)(l))
|
---|
181 | #define LL_L2F(f, l) ((f) = (PRFloat64)(l))
|
---|
182 | #define LL_L2D(d, l) ((d) = (PRFloat64)(l))
|
---|
183 |
|
---|
184 | #define LL_I2L(l, i) ((l) = (PRInt64)(i))
|
---|
185 | #define LL_UI2L(l, ui) ((l) = (PRInt64)(ui))
|
---|
186 | #define LL_F2L(l, f) ((l) = (PRInt64)(f))
|
---|
187 | #define LL_D2L(l, d) ((l) = (PRInt64)(d))
|
---|
188 |
|
---|
189 | /***********************************************************************
|
---|
190 | ** MACROS: LL_UDIVMOD
|
---|
191 | ** DESCRIPTION:
|
---|
192 | ** Produce both a quotient and a remainder given an unsigned
|
---|
193 | ** INPUTS: PRUint64 a: The dividend of the operation
|
---|
194 | ** PRUint64 b: The quotient of the operation
|
---|
195 | ** OUTPUTS: PRUint64 *qp: pointer to quotient
|
---|
196 | ** PRUint64 *rp: pointer to remainder
|
---|
197 | ***********************************************************************/
|
---|
198 | #define LL_UDIVMOD(qp, rp, a, b) \
|
---|
199 | (*(qp) = ((PRUint64)(a) / (b)), \
|
---|
200 | *(rp) = ((PRUint64)(a) % (b)))
|
---|
201 |
|
---|
202 | #else /* !HAVE_LONG_LONG */
|
---|
203 |
|
---|
204 | #ifdef IS_LITTLE_ENDIAN
|
---|
205 | #define LL_INIT(hi, lo) {PR_UINT32(lo), PR_UINT32(hi)}
|
---|
206 | #else
|
---|
207 | #define LL_INIT(hi, lo) {PR_UINT32(hi), PR_UINT32(lo)}
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | #define LL_IS_ZERO(a) (((a).hi == 0) && ((a).lo == 0))
|
---|
211 | #define LL_EQ(a, b) (((a).hi == (b).hi) && ((a).lo == (b).lo))
|
---|
212 | #define LL_NE(a, b) (((a).hi != (b).hi) || ((a).lo != (b).lo))
|
---|
213 | #define LL_GE_ZERO(a) (((a).hi >> 31) == 0)
|
---|
214 |
|
---|
215 | #define LL_CMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
|
---|
216 | ((PRInt32)(a).hi op (PRInt32)(b).hi))
|
---|
217 | #define LL_UCMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
|
---|
218 | ((a).hi op (b).hi))
|
---|
219 |
|
---|
220 | #define LL_AND(r, a, b) ((r).lo = (a).lo & (b).lo, \
|
---|
221 | (r).hi = (a).hi & (b).hi)
|
---|
222 | #define LL_OR(r, a, b) ((r).lo = (a).lo | (b).lo, \
|
---|
223 | (r).hi = (a).hi | (b).hi)
|
---|
224 | #define LL_XOR(r, a, b) ((r).lo = (a).lo ^ (b).lo, \
|
---|
225 | (r).hi = (a).hi ^ (b).hi)
|
---|
226 | #define LL_OR2(r, a) ((r).lo = (r).lo | (a).lo, \
|
---|
227 | (r).hi = (r).hi | (a).hi)
|
---|
228 | #define LL_NOT(r, a) ((r).lo = ~(a).lo, \
|
---|
229 | (r).hi = ~(a).hi)
|
---|
230 |
|
---|
231 | #define LL_NEG(r, a) ((r).lo = -(PRInt32)(a).lo, \
|
---|
232 | (r).hi = -(PRInt32)(a).hi - ((r).lo != 0))
|
---|
233 | #define LL_ADD(r, a, b) { \
|
---|
234 | PRInt64 _a, _b; \
|
---|
235 | _a = a; _b = b; \
|
---|
236 | (r).lo = _a.lo + _b.lo; \
|
---|
237 | (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
|
---|
238 | }
|
---|
239 |
|
---|
240 | #define LL_SUB(r, a, b) { \
|
---|
241 | PRInt64 _a, _b; \
|
---|
242 | _a = a; _b = b; \
|
---|
243 | (r).lo = _a.lo - _b.lo; \
|
---|
244 | (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
|
---|
245 | }
|
---|
246 |
|
---|
247 | #define LL_MUL(r, a, b) { \
|
---|
248 | PRInt64 _a, _b; \
|
---|
249 | _a = a; _b = b; \
|
---|
250 | LL_MUL32(r, _a.lo, _b.lo); \
|
---|
251 | (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
|
---|
252 | }
|
---|
253 |
|
---|
254 | #define _lo16(a) ((a) & PR_BITMASK(16))
|
---|
255 | #define _hi16(a) ((a) >> 16)
|
---|
256 |
|
---|
257 | #define LL_MUL32(r, a, b) { \
|
---|
258 | PRUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
|
---|
259 | _a1 = _hi16(a), _a0 = _lo16(a); \
|
---|
260 | _b1 = _hi16(b), _b0 = _lo16(b); \
|
---|
261 | _y0 = _a0 * _b0; \
|
---|
262 | _y1 = _a0 * _b1; \
|
---|
263 | _y2 = _a1 * _b0; \
|
---|
264 | _y3 = _a1 * _b1; \
|
---|
265 | _y1 += _hi16(_y0); /* can't carry */ \
|
---|
266 | _y1 += _y2; /* might carry */ \
|
---|
267 | if (_y1 < _y2) \
|
---|
268 | _y3 += (PRUint32)(PR_BIT(16)); /* propagate */ \
|
---|
269 | (r).lo = (_lo16(_y1) << 16) + _lo16(_y0); \
|
---|
270 | (r).hi = _y3 + _hi16(_y1); \
|
---|
271 | }
|
---|
272 |
|
---|
273 | #define LL_UDIVMOD(qp, rp, a, b) ll_udivmod(qp, rp, a, b)
|
---|
274 |
|
---|
275 | NSPR_API(void) ll_udivmod(PRUint64 *qp, PRUint64 *rp, PRUint64 a, PRUint64 b);
|
---|
276 |
|
---|
277 | #define LL_DIV(r, a, b) { \
|
---|
278 | PRInt64 _a, _b; \
|
---|
279 | PRUint32 _negative = (PRInt32)(a).hi < 0; \
|
---|
280 | if (_negative) { \
|
---|
281 | LL_NEG(_a, a); \
|
---|
282 | } else { \
|
---|
283 | _a = a; \
|
---|
284 | } \
|
---|
285 | if ((PRInt32)(b).hi < 0) { \
|
---|
286 | _negative ^= 1; \
|
---|
287 | LL_NEG(_b, b); \
|
---|
288 | } else { \
|
---|
289 | _b = b; \
|
---|
290 | } \
|
---|
291 | LL_UDIVMOD(&(r), 0, _a, _b); \
|
---|
292 | if (_negative) \
|
---|
293 | LL_NEG(r, r); \
|
---|
294 | }
|
---|
295 |
|
---|
296 | #define LL_MOD(r, a, b) { \
|
---|
297 | PRInt64 _a, _b; \
|
---|
298 | PRUint32 _negative = (PRInt32)(a).hi < 0; \
|
---|
299 | if (_negative) { \
|
---|
300 | LL_NEG(_a, a); \
|
---|
301 | } else { \
|
---|
302 | _a = a; \
|
---|
303 | } \
|
---|
304 | if ((PRInt32)(b).hi < 0) { \
|
---|
305 | LL_NEG(_b, b); \
|
---|
306 | } else { \
|
---|
307 | _b = b; \
|
---|
308 | } \
|
---|
309 | LL_UDIVMOD(0, &(r), _a, _b); \
|
---|
310 | if (_negative) \
|
---|
311 | LL_NEG(r, r); \
|
---|
312 | }
|
---|
313 |
|
---|
314 | #define LL_SHL(r, a, b) { \
|
---|
315 | if (b) { \
|
---|
316 | PRInt64 _a; \
|
---|
317 | _a = a; \
|
---|
318 | if ((b) < 32) { \
|
---|
319 | (r).lo = _a.lo << ((b) & 31); \
|
---|
320 | (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
|
---|
321 | } else { \
|
---|
322 | (r).lo = 0; \
|
---|
323 | (r).hi = _a.lo << ((b) & 31); \
|
---|
324 | } \
|
---|
325 | } else { \
|
---|
326 | (r) = (a); \
|
---|
327 | } \
|
---|
328 | }
|
---|
329 |
|
---|
330 | /* a is an PRInt32, b is PRInt32, r is PRInt64 */
|
---|
331 | #define LL_ISHL(r, a, b) { \
|
---|
332 | if (b) { \
|
---|
333 | PRInt64 _a; \
|
---|
334 | _a.lo = (a); \
|
---|
335 | _a.hi = 0; \
|
---|
336 | if ((b) < 32) { \
|
---|
337 | (r).lo = (a) << ((b) & 31); \
|
---|
338 | (r).hi = ((a) >> (32 - (b))); \
|
---|
339 | } else { \
|
---|
340 | (r).lo = 0; \
|
---|
341 | (r).hi = (a) << ((b) & 31); \
|
---|
342 | } \
|
---|
343 | } else { \
|
---|
344 | (r).lo = (a); \
|
---|
345 | (r).hi = 0; \
|
---|
346 | } \
|
---|
347 | }
|
---|
348 |
|
---|
349 | #define LL_SHR(r, a, b) { \
|
---|
350 | if (b) { \
|
---|
351 | PRInt64 _a; \
|
---|
352 | _a = a; \
|
---|
353 | if ((b) < 32) { \
|
---|
354 | (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
|
---|
355 | (r).hi = (PRInt32)_a.hi >> ((b) & 31); \
|
---|
356 | } else { \
|
---|
357 | (r).lo = (PRInt32)_a.hi >> ((b) & 31); \
|
---|
358 | (r).hi = (PRInt32)_a.hi >> 31; \
|
---|
359 | } \
|
---|
360 | } else { \
|
---|
361 | (r) = (a); \
|
---|
362 | } \
|
---|
363 | }
|
---|
364 |
|
---|
365 | #define LL_USHR(r, a, b) { \
|
---|
366 | if (b) { \
|
---|
367 | PRInt64 _a; \
|
---|
368 | _a = a; \
|
---|
369 | if ((b) < 32) { \
|
---|
370 | (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
|
---|
371 | (r).hi = _a.hi >> ((b) & 31); \
|
---|
372 | } else { \
|
---|
373 | (r).lo = _a.hi >> ((b) & 31); \
|
---|
374 | (r).hi = 0; \
|
---|
375 | } \
|
---|
376 | } else { \
|
---|
377 | (r) = (a); \
|
---|
378 | } \
|
---|
379 | }
|
---|
380 |
|
---|
381 | #define LL_L2I(i, l) ((i) = (l).lo)
|
---|
382 | #define LL_L2UI(ui, l) ((ui) = (l).lo)
|
---|
383 | #define LL_L2F(f, l) { double _d; LL_L2D(_d, l); (f) = (PRFloat64)_d; }
|
---|
384 |
|
---|
385 | #define LL_L2D(d, l) { \
|
---|
386 | int _negative; \
|
---|
387 | PRInt64 _absval; \
|
---|
388 | \
|
---|
389 | _negative = (l).hi >> 31; \
|
---|
390 | if (_negative) { \
|
---|
391 | LL_NEG(_absval, l); \
|
---|
392 | } else { \
|
---|
393 | _absval = l; \
|
---|
394 | } \
|
---|
395 | (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
|
---|
396 | if (_negative) \
|
---|
397 | (d) = -(d); \
|
---|
398 | }
|
---|
399 |
|
---|
400 | #define LL_I2L(l, i) { PRInt32 _i = ((PRInt32)(i)) >> 31; (l).lo = (i); (l).hi = _i; }
|
---|
401 | #define LL_UI2L(l, ui) ((l).lo = (ui), (l).hi = 0)
|
---|
402 | #define LL_F2L(l, f) { double _d = (double)f; LL_D2L(l, _d); }
|
---|
403 |
|
---|
404 | #define LL_D2L(l, d) { \
|
---|
405 | int _negative; \
|
---|
406 | double _absval, _d_hi; \
|
---|
407 | PRInt64 _lo_d; \
|
---|
408 | \
|
---|
409 | _negative = ((d) < 0); \
|
---|
410 | _absval = _negative ? -(d) : (d); \
|
---|
411 | \
|
---|
412 | (l).hi = _absval / 4.294967296e9; \
|
---|
413 | (l).lo = 0; \
|
---|
414 | LL_L2D(_d_hi, l); \
|
---|
415 | _absval -= _d_hi; \
|
---|
416 | _lo_d.hi = 0; \
|
---|
417 | if (_absval < 0) { \
|
---|
418 | _lo_d.lo = -_absval; \
|
---|
419 | LL_SUB(l, l, _lo_d); \
|
---|
420 | } else { \
|
---|
421 | _lo_d.lo = _absval; \
|
---|
422 | LL_ADD(l, l, _lo_d); \
|
---|
423 | } \
|
---|
424 | \
|
---|
425 | if (_negative) \
|
---|
426 | LL_NEG(l, l); \
|
---|
427 | }
|
---|
428 |
|
---|
429 | #endif /* !HAVE_LONG_LONG */
|
---|
430 |
|
---|
431 | PR_END_EXTERN_C
|
---|
432 |
|
---|
433 | #endif /* prlong_h___ */
|
---|