1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 ** Description: Portable access to 64 bit numerics
10 ** Long-long (64-bit signed integer type) support. Some C compilers
11 ** don't support 64 bit integers yet, so we use these macros to
12 ** support both machines that do and don't.
21 /***********************************************************************
27 ** Various interesting constants and static variable
29 ***********************************************************************/
30 NSPR_API(PRInt64
) LL_MaxInt(void);
31 NSPR_API(PRInt64
) LL_MinInt(void);
32 NSPR_API(PRInt64
) LL_Zero(void);
33 NSPR_API(PRUint64
) LL_MaxUint(void);
35 #if defined(HAVE_LONG_LONG)
37 /* Keep this in sync with prtypes.h. */
38 #if PR_BYTES_PER_LONG == 8 && !defined(PR_ALTERNATE_INT64_TYPEDEF)
39 #define LL_MAXINT 9223372036854775807L
40 #define LL_MININT (-LL_MAXINT - 1L)
42 #define LL_MAXUINT 18446744073709551615UL
43 #define LL_INIT(hi, lo) ((hi ## L << 32) + lo ## L)
44 #elif defined(WIN32) && !defined(__GNUC__)
45 #define LL_MAXINT 9223372036854775807i64
46 #define LL_MININT (-LL_MAXINT - 1i64)
48 #define LL_MAXUINT 18446744073709551615ui64
49 #define LL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64)
51 #define LL_MAXINT 9223372036854775807LL
52 #define LL_MININT (-LL_MAXINT - 1LL)
54 #define LL_MAXUINT 18446744073709551615ULL
55 #define LL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL)
58 /***********************************************************************
61 ** The following macros define portable access to the 64 bit
64 ***********************************************************************/
66 /***********************************************************************
67 ** MACROS: LL_<relational operators>
69 ** LL_IS_ZERO Test for zero
70 ** LL_EQ Test for equality
71 ** LL_NE Test for inequality
72 ** LL_GE_ZERO Test for zero or positive
73 ** LL_CMP Compare two values
74 ***********************************************************************/
75 #define LL_IS_ZERO(a) ((a) == 0)
76 #define LL_EQ(a, b) ((a) == (b))
77 #define LL_NE(a, b) ((a) != (b))
78 #define LL_GE_ZERO(a) ((a) >= 0)
79 #define LL_CMP(a, op, b) ((PRInt64)(a) op (PRInt64)(b))
80 #define LL_UCMP(a, op, b) ((PRUint64)(a) op (PRUint64)(b))
82 /***********************************************************************
83 ** MACROS: LL_<logical operators>
87 ** LL_XOR Logical exclusion
88 ** LL_OR2 A disgusting deviation
89 ** LL_NOT Negation (one's complement)
90 ***********************************************************************/
91 #define LL_AND(r, a, b) ((r) = (a) & (b))
92 #define LL_OR(r, a, b) ((r) = (a) | (b))
93 #define LL_XOR(r, a, b) ((r) = (a) ^ (b))
94 #define LL_OR2(r, a) ((r) = (r) | (a))
95 #define LL_NOT(r, a) ((r) = ~(a))
97 /***********************************************************************
98 ** MACROS: LL_<mathematical operators>
100 ** LL_NEG Negation (two's complement)
101 ** LL_ADD Summation (two's complement)
102 ** LL_SUB Difference (two's complement)
103 ***********************************************************************/
104 #define LL_NEG(r, a) ((r) = -(a))
105 #define LL_ADD(r, a, b) ((r) = (a) + (b))
106 #define LL_SUB(r, a, b) ((r) = (a) - (b))
108 /***********************************************************************
109 ** MACROS: LL_<mathematical operators>
111 ** LL_MUL Product (two's complement)
112 ** LL_DIV Quotient (two's complement)
113 ** LL_MOD Modulus (two's complement)
114 ***********************************************************************/
115 #define LL_MUL(r, a, b) ((r) = (a) * (b))
116 #define LL_DIV(r, a, b) ((r) = (a) / (b))
117 #define LL_MOD(r, a, b) ((r) = (a) % (b))
119 /***********************************************************************
120 ** MACROS: LL_<shifting operators>
122 ** LL_SHL Shift left [0..64] bits
123 ** LL_SHR Shift right [0..64] bits with sign extension
124 ** LL_USHR Unsigned shift right [0..64] bits
125 ** LL_ISHL Signed shift left [0..64] bits
126 ***********************************************************************/
127 #define LL_SHL(r, a, b) ((r) = (PRInt64)(a) << (b))
128 #define LL_SHR(r, a, b) ((r) = (PRInt64)(a) >> (b))
129 #define LL_USHR(r, a, b) ((r) = (PRUint64)(a) >> (b))
130 #define LL_ISHL(r, a, b) ((r) = (PRInt64)(a) << (b))
132 /***********************************************************************
133 ** MACROS: LL_<conversion operators>
135 ** LL_L2I Convert to signed 32 bit
136 ** LL_L2UI Convert to unsigned 32 bit
137 ** LL_L2F Convert to floating point
138 ** LL_L2D Convert to floating point
139 ** LL_I2L Convert signed to 64 bit
140 ** LL_UI2L Convert unsigned to 64 bit
141 ** LL_F2L Convert float to 64 bit
142 ** LL_D2L Convert float to 64 bit
143 ***********************************************************************/
144 #define LL_L2I(i, l) ((i) = (PRInt32)(l))
145 #define LL_L2UI(ui, l) ((ui) = (PRUint32)(l))
146 #define LL_L2F(f, l) ((f) = (PRFloat64)(l))
147 #define LL_L2D(d, l) ((d) = (PRFloat64)(l))
149 #define LL_I2L(l, i) ((l) = (PRInt64)(i))
150 #define LL_UI2L(l, ui) ((l) = (PRInt64)(ui))
151 #define LL_F2L(l, f) ((l) = (PRInt64)(f))
152 #define LL_D2L(l, d) ((l) = (PRInt64)(d))
154 /***********************************************************************
155 ** MACROS: LL_UDIVMOD
157 ** Produce both a quotient and a remainder given an unsigned
158 ** INPUTS: PRUint64 a: The dividend of the operation
159 ** PRUint64 b: The quotient of the operation
160 ** OUTPUTS: PRUint64 *qp: pointer to quotient
161 ** PRUint64 *rp: pointer to remainder
162 ***********************************************************************/
163 #define LL_UDIVMOD(qp, rp, a, b) \
164 (*(qp) = ((PRUint64)(a) / (b)), \
165 *(rp) = ((PRUint64)(a) % (b)))
167 #else /* !HAVE_LONG_LONG */
169 #define LL_MAXINT LL_MaxInt()
170 #define LL_MININT LL_MinInt()
171 #define LL_ZERO LL_Zero()
172 #define LL_MAXUINT LL_MaxUint()
174 #ifdef IS_LITTLE_ENDIAN
175 #define LL_INIT(hi, lo) {PR_UINT32(lo), PR_UINT32(hi)}
177 #define LL_INIT(hi, lo) {PR_UINT32(hi), PR_UINT32(lo)}
180 #define LL_IS_ZERO(a) (((a).hi == 0) && ((a).lo == 0))
181 #define LL_EQ(a, b) (((a).hi == (b).hi) && ((a).lo == (b).lo))
182 #define LL_NE(a, b) (((a).hi != (b).hi) || ((a).lo != (b).lo))
183 #define LL_GE_ZERO(a) (((a).hi >> 31) == 0)
185 #define LL_CMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
186 ((PRInt32)(a).hi op (PRInt32)(b).hi))
187 #define LL_UCMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
190 #define LL_AND(r, a, b) ((r).lo = (a).lo & (b).lo, \
191 (r).hi = (a).hi & (b).hi)
192 #define LL_OR(r, a, b) ((r).lo = (a).lo | (b).lo, \
193 (r).hi = (a).hi | (b).hi)
194 #define LL_XOR(r, a, b) ((r).lo = (a).lo ^ (b).lo, \
195 (r).hi = (a).hi ^ (b).hi)
196 #define LL_OR2(r, a) ((r).lo = (r).lo | (a).lo, \
197 (r).hi = (r).hi | (a).hi)
198 #define LL_NOT(r, a) ((r).lo = ~(a).lo, \
201 #define LL_NEG(r, a) ((r).lo = -(PRInt32)(a).lo, \
202 (r).hi = -(PRInt32)(a).hi - ((r).lo != 0))
203 #define LL_ADD(r, a, b) { \
206 (r).lo = _a.lo + _b.lo; \
207 (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
210 #define LL_SUB(r, a, b) { \
213 (r).lo = _a.lo - _b.lo; \
214 (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
217 #define LL_MUL(r, a, b) { \
220 LL_MUL32(r, _a.lo, _b.lo); \
221 (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
224 #define _lo16(a) ((a) & PR_BITMASK(16))
225 #define _hi16(a) ((a) >> 16)
227 #define LL_MUL32(r, a, b) { \
228 PRUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
229 _a1 = _hi16(a), _a0 = _lo16(a); \
230 _b1 = _hi16(b), _b0 = _lo16(b); \
235 _y1 += _hi16(_y0); /* can't carry */ \
236 _y1 += _y2; /* might carry */ \
238 _y3 += (PRUint32)(PR_BIT(16)); /* propagate */ \
239 (r).lo = (_lo16(_y1) << 16) + _lo16(_y0); \
240 (r).hi = _y3 + _hi16(_y1); \
243 #define LL_UDIVMOD(qp, rp, a, b) ll_udivmod(qp, rp, a, b)
245 NSPR_API(void) ll_udivmod(PRUint64
*qp
, PRUint64
*rp
, PRUint64 a
, PRUint64 b
);
247 #define LL_DIV(r, a, b) { \
249 PRUint32 _negative = (PRInt32)(a).hi < 0; \
255 if ((PRInt32)(b).hi < 0) { \
261 LL_UDIVMOD(&(r), 0, _a, _b); \
266 #define LL_MOD(r, a, b) { \
268 PRUint32 _negative = (PRInt32)(a).hi < 0; \
274 if ((PRInt32)(b).hi < 0) { \
279 LL_UDIVMOD(0, &(r), _a, _b); \
284 #define LL_SHL(r, a, b) { \
289 (r).lo = _a.lo << ((b) & 31); \
290 (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
293 (r).hi = _a.lo << ((b) & 31); \
300 /* a is an PRInt32, b is PRInt32, r is PRInt64 */
301 #define LL_ISHL(r, a, b) { \
307 (r).lo = (a) << ((b) & 31); \
308 (r).hi = ((a) >> (32 - (b))); \
311 (r).hi = (a) << ((b) & 31); \
319 #define LL_SHR(r, a, b) { \
324 (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
325 (r).hi = (PRInt32)_a.hi >> ((b) & 31); \
327 (r).lo = (PRInt32)_a.hi >> ((b) & 31); \
328 (r).hi = (PRInt32)_a.hi >> 31; \
335 #define LL_USHR(r, a, b) { \
340 (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
341 (r).hi = _a.hi >> ((b) & 31); \
343 (r).lo = _a.hi >> ((b) & 31); \
351 #define LL_L2I(i, l) ((i) = (l).lo)
352 #define LL_L2UI(ui, l) ((ui) = (l).lo)
353 #define LL_L2F(f, l) { double _d; LL_L2D(_d, l); (f) = (PRFloat64)_d; }
355 #define LL_L2D(d, l) { \
359 _negative = (l).hi >> 31; \
361 LL_NEG(_absval, l); \
365 (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
370 #define LL_I2L(l, i) { PRInt32 _i = ((PRInt32)(i)) >> 31; (l).lo = (i); (l).hi = _i; }
371 #define LL_UI2L(l, ui) ((l).lo = (ui), (l).hi = 0)
372 #define LL_F2L(l, f) { double _d = (double)f; LL_D2L(l, _d); }
374 #define LL_D2L(l, d) { \
376 double _absval, _d_hi; \
379 _negative = ((d) < 0); \
380 _absval = _negative ? -(d) : (d); \
382 (l).hi = _absval / 4.294967296e9; \
388 _lo_d.lo = -_absval; \
389 LL_SUB(l, l, _lo_d); \
391 _lo_d.lo = _absval; \
392 LL_ADD(l, l, _lo_d); \
399 #endif /* !HAVE_LONG_LONG */
403 #endif /* prlong_h___ */