1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * The contents of this file are subject to the Mozilla Public
4 * License Version 1.1 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of
6 * the License at http://www.mozilla.org/MPL/
8 * Software distributed under the License is distributed on an "AS
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the License for the specific language governing
11 * rights and limitations under the License.
13 * The Original Code is the Netscape Portable Runtime (NSPR).
15 * The Initial Developer of the Original Code is Netscape
16 * Communications Corporation. Portions created by Netscape are
17 * Copyright (C) 1998-2000 Netscape Communications Corporation. All
22 * Alternatively, the contents of this file may be used under the
23 * terms of the GNU General Public License Version 2 or later (the
24 * "GPL"), in which case the provisions of the GPL are applicable
25 * instead of those above. If you wish to allow use of your
26 * version of this file only under the terms of the GPL and not to
27 * allow others to use your version of this file under the MPL,
28 * indicate your decision by deleting the provisions above and
29 * replace them with the notice and other provisions required by
30 * the GPL. If you do not delete the provisions above, a recipient
31 * may use your version of this file under either the MPL or the
37 ** Description: Portable access to 64 bit numerics
39 ** Long-long (64-bit signed integer type) support. Some C compilers
40 ** don't support 64 bit integers yet, so we use these macros to
41 ** support both machines that do and don't.
50 /***********************************************************************
55 ** Various interesting constants and static variable
57 ***********************************************************************/
58 #if defined(HAVE_WATCOM_BUG_2)
59 PRInt64 __pascal __loadds __export
61 PRInt64 __pascal __loadds __export
63 PRInt64 __pascal __loadds __export
66 NSPR_API(PRInt64
) LL_MaxInt(void);
67 NSPR_API(PRInt64
) LL_MinInt(void);
68 NSPR_API(PRInt64
) LL_Zero(void);
71 #define LL_MAXINT LL_MaxInt()
72 #define LL_MININT LL_MinInt()
73 #define LL_ZERO LL_Zero()
75 #if defined(HAVE_LONG_LONG)
77 #if PR_BYTES_PER_LONG == 8
78 #define LL_INIT(hi, lo) ((hi ## L << 32) + lo ## L)
79 #elif (defined(WIN32) || defined(WIN16)) && !defined(__GNUC__)
80 #define LL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64)
82 #define LL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL)
85 /***********************************************************************
88 ** The following macros define portable access to the 64 bit
91 ***********************************************************************/
93 /***********************************************************************
94 ** MACROS: LL_<relational operators>
96 ** LL_IS_ZERO Test for zero
97 ** LL_EQ Test for equality
98 ** LL_NE Test for inequality
99 ** LL_GE_ZERO Test for zero or positive
100 ** LL_CMP Compare two values
101 ***********************************************************************/
102 #define LL_IS_ZERO(a) ((a) == 0)
103 #define LL_EQ(a, b) ((a) == (b))
104 #define LL_NE(a, b) ((a) != (b))
105 #define LL_GE_ZERO(a) ((a) >= 0)
106 #define LL_CMP(a, op, b) ((PRInt64)(a) op (PRInt64)(b))
107 #define LL_UCMP(a, op, b) ((PRUint64)(a) op (PRUint64)(b))
109 /***********************************************************************
110 ** MACROS: LL_<logical operators>
112 ** LL_AND Logical and
114 ** LL_XOR Logical exclusion
115 ** LL_OR2 A disgusting deviation
116 ** LL_NOT Negation (one's complement)
117 ***********************************************************************/
118 #define LL_AND(r, a, b) ((r) = (a) & (b))
119 #define LL_OR(r, a, b) ((r) = (a) | (b))
120 #define LL_XOR(r, a, b) ((r) = (a) ^ (b))
121 #define LL_OR2(r, a) ((r) = (r) | (a))
122 #define LL_NOT(r, a) ((r) = ~(a))
124 /***********************************************************************
125 ** MACROS: LL_<mathematical operators>
127 ** LL_NEG Negation (two's complement)
128 ** LL_ADD Summation (two's complement)
129 ** LL_SUB Difference (two's complement)
130 ***********************************************************************/
131 #define LL_NEG(r, a) ((r) = -(a))
132 #define LL_ADD(r, a, b) ((r) = (a) + (b))
133 #define LL_SUB(r, a, b) ((r) = (a) - (b))
135 /***********************************************************************
136 ** MACROS: LL_<mathematical operators>
138 ** LL_MUL Product (two's complement)
139 ** LL_DIV Quotient (two's complement)
140 ** LL_MOD Modulus (two's complement)
141 ***********************************************************************/
142 #define LL_MUL(r, a, b) ((r) = (a) * (b))
143 #define LL_DIV(r, a, b) ((r) = (a) / (b))
144 #define LL_MOD(r, a, b) ((r) = (a) % (b))
146 /***********************************************************************
147 ** MACROS: LL_<shifting operators>
149 ** LL_SHL Shift left [0..64] bits
150 ** LL_SHR Shift right [0..64] bits with sign extension
151 ** LL_USHR Unsigned shift right [0..64] bits
152 ** LL_ISHL Signed shift left [0..64] bits
153 ***********************************************************************/
154 #define LL_SHL(r, a, b) ((r) = (PRInt64)(a) << (b))
155 #define LL_SHR(r, a, b) ((r) = (PRInt64)(a) >> (b))
156 #define LL_USHR(r, a, b) ((r) = (PRUint64)(a) >> (b))
157 #define LL_ISHL(r, a, b) ((r) = (PRInt64)(a) << (b))
159 /***********************************************************************
160 ** MACROS: LL_<conversion operators>
162 ** LL_L2I Convert to signed 32 bit
163 ** LL_L2UI Convert to unsigned 32 bit
164 ** LL_L2F Convert to floating point
165 ** LL_L2D Convert to floating point
166 ** LL_I2L Convert signed to 64 bit
167 ** LL_UI2L Convert unsigned to 64 bit
168 ** LL_F2L Convert float to 64 bit
169 ** LL_D2L Convert float to 64 bit
170 ***********************************************************************/
171 #define LL_L2I(i, l) ((i) = (PRInt32)(l))
172 #define LL_L2UI(ui, l) ((ui) = (PRUint32)(l))
173 #define LL_L2F(f, l) ((f) = (PRFloat64)(l))
174 #define LL_L2D(d, l) ((d) = (PRFloat64)(l))
176 #define LL_I2L(l, i) ((l) = (PRInt64)(i))
177 #define LL_UI2L(l, ui) ((l) = (PRInt64)(ui))
178 #define LL_F2L(l, f) ((l) = (PRInt64)(f))
179 #define LL_D2L(l, d) ((l) = (PRInt64)(d))
181 /***********************************************************************
182 ** MACROS: LL_UDIVMOD
184 ** Produce both a quotient and a remainder given an unsigned
185 ** INPUTS: PRUint64 a: The dividend of the operation
186 ** PRUint64 b: The quotient of the operation
187 ** OUTPUTS: PRUint64 *qp: pointer to quotient
188 ** PRUint64 *rp: pointer to remainder
189 ***********************************************************************/
190 #define LL_UDIVMOD(qp, rp, a, b) \
191 (*(qp) = ((PRUint64)(a) / (b)), \
192 *(rp) = ((PRUint64)(a) % (b)))
194 #else /* !HAVE_LONG_LONG */
196 #ifdef IS_LITTLE_ENDIAN
197 #define LL_INIT(hi, lo) {PR_INT32(lo), PR_INT32(hi)}
199 #define LL_INIT(hi, lo) {PR_INT32(hi), PR_INT32(lo)}
202 #define LL_IS_ZERO(a) (((a).hi == 0) && ((a).lo == 0))
203 #define LL_EQ(a, b) (((a).hi == (b).hi) && ((a).lo == (b).lo))
204 #define LL_NE(a, b) (((a).hi != (b).hi) || ((a).lo != (b).lo))
205 #define LL_GE_ZERO(a) (((a).hi >> 31) == 0)
207 #define LL_CMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
208 ((PRInt32)(a).hi op (PRInt32)(b).hi))
209 #define LL_UCMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
212 #define LL_AND(r, a, b) ((r).lo = (a).lo & (b).lo, \
213 (r).hi = (a).hi & (b).hi)
214 #define LL_OR(r, a, b) ((r).lo = (a).lo | (b).lo, \
215 (r).hi = (a).hi | (b).hi)
216 #define LL_XOR(r, a, b) ((r).lo = (a).lo ^ (b).lo, \
217 (r).hi = (a).hi ^ (b).hi)
218 #define LL_OR2(r, a) ((r).lo = (r).lo | (a).lo, \
219 (r).hi = (r).hi | (a).hi)
220 #define LL_NOT(r, a) ((r).lo = ~(a).lo, \
223 #define LL_NEG(r, a) ((r).lo = -(PRInt32)(a).lo, \
224 (r).hi = -(PRInt32)(a).hi - ((r).lo != 0))
225 #define LL_ADD(r, a, b) { \
228 (r).lo = _a.lo + _b.lo; \
229 (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
232 #define LL_SUB(r, a, b) { \
235 (r).lo = _a.lo - _b.lo; \
236 (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
239 #define LL_MUL(r, a, b) { \
242 LL_MUL32(r, _a.lo, _b.lo); \
243 (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
246 #define _lo16(a) ((a) & PR_BITMASK(16))
247 #define _hi16(a) ((a) >> 16)
249 #define LL_MUL32(r, a, b) { \
250 PRUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
251 _a1 = _hi16(a), _a0 = _lo16(a); \
252 _b1 = _hi16(b), _b0 = _lo16(b); \
257 _y1 += _hi16(_y0); /* can't carry */ \
258 _y1 += _y2; /* might carry */ \
260 _y3 += (PRUint32)(PR_BIT(16)); /* propagate */ \
261 (r).lo = (_lo16(_y1) << 16) + _lo16(_y0); \
262 (r).hi = _y3 + _hi16(_y1); \
265 #define LL_UDIVMOD(qp, rp, a, b) ll_udivmod(qp, rp, a, b)
267 NSPR_API(void) ll_udivmod(PRUint64
*qp
, PRUint64
*rp
, PRUint64 a
, PRUint64 b
);
269 #define LL_DIV(r, a, b) { \
271 PRUint32 _negative = (PRInt32)(a).hi < 0; \
277 if ((PRInt32)(b).hi < 0) { \
283 LL_UDIVMOD(&(r), 0, _a, _b); \
288 #define LL_MOD(r, a, b) { \
290 PRUint32 _negative = (PRInt32)(a).hi < 0; \
296 if ((PRInt32)(b).hi < 0) { \
301 LL_UDIVMOD(0, &(r), _a, _b); \
306 #define LL_SHL(r, a, b) { \
311 (r).lo = _a.lo << ((b) & 31); \
312 (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
315 (r).hi = _a.lo << ((b) & 31); \
322 /* a is an PRInt32, b is PRInt32, r is PRInt64 */
323 #define LL_ISHL(r, a, b) { \
329 (r).lo = (a) << ((b) & 31); \
330 (r).hi = ((a) >> (32 - (b))); \
333 (r).hi = (a) << ((b) & 31); \
341 #define LL_SHR(r, a, b) { \
346 (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
347 (r).hi = (PRInt32)_a.hi >> ((b) & 31); \
349 (r).lo = (PRInt32)_a.hi >> ((b) & 31); \
350 (r).hi = (PRInt32)_a.hi >> 31; \
357 #define LL_USHR(r, a, b) { \
362 (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
363 (r).hi = _a.hi >> ((b) & 31); \
365 (r).lo = _a.hi >> ((b) & 31); \
373 #define LL_L2I(i, l) ((i) = (l).lo)
374 #define LL_L2UI(ui, l) ((ui) = (l).lo)
375 #define LL_L2F(f, l) { double _d; LL_L2D(_d, l); (f) = (PRFloat64)_d; }
377 #define LL_L2D(d, l) { \
381 _negative = (l).hi >> 31; \
383 LL_NEG(_absval, l); \
387 (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
392 #define LL_I2L(l, i) { PRInt32 _i = ((PRInt32)(i)) >> 31; (l).lo = (i); (l).hi = _i; }
393 #define LL_UI2L(l, ui) ((l).lo = (ui), (l).hi = 0)
394 #define LL_F2L(l, f) { double _d = (double)f; LL_D2L(l, _d); }
396 #define LL_D2L(l, d) { \
398 double _absval, _d_hi; \
401 _negative = ((d) < 0); \
402 _absval = _negative ? -(d) : (d); \
404 (l).hi = _absval / 4.294967296e9; \
410 _lo_d.lo = -_absval; \
411 LL_SUB(l, l, _lo_d); \
413 _lo_d.lo = _absval; \
414 LL_ADD(l, l, _lo_d); \
421 #endif /* !HAVE_LONG_LONG */
425 #endif /* prlong_h___ */