| 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/. */ |
| 5 |
|
| 6 |
/* |
| 7 |
** File: prlong.h |
| 8 |
** Description: Portable access to 64 bit numerics |
| 9 |
** |
| 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. |
| 13 |
**/ |
| 14 |
#ifndef prlong_h___ |
| 15 |
#define prlong_h___ |
| 16 |
|
| 17 |
#include "prtypes.h" |
| 18 |
|
| 19 |
PR_BEGIN_EXTERN_C |
| 20 |
|
| 21 |
/*********************************************************************** |
| 22 |
** DEFINES: LL_MaxInt |
| 23 |
** LL_MinInt |
| 24 |
** LL_Zero |
| 25 |
** LL_MaxUint |
| 26 |
** DESCRIPTION: |
| 27 |
** Various interesting constants and static variable |
| 28 |
** initializer |
| 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); |
| 34 |
|
| 35 |
#if defined(HAVE_LONG_LONG) |
| 36 |
|
| 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) |
| 41 |
#define LL_ZERO 0L |
| 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) |
| 47 |
#define LL_ZERO 0i64 |
| 48 |
#define LL_MAXUINT 18446744073709551615ui64 |
| 49 |
#define LL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64) |
| 50 |
#else |
| 51 |
#define LL_MAXINT 9223372036854775807LL |
| 52 |
#define LL_MININT (-LL_MAXINT - 1LL) |
| 53 |
#define LL_ZERO 0LL |
| 54 |
#define LL_MAXUINT 18446744073709551615ULL |
| 55 |
#define LL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL) |
| 56 |
#endif |
| 57 |
|
| 58 |
/*********************************************************************** |
| 59 |
** MACROS: LL_* |
| 60 |
** DESCRIPTION: |
| 61 |
** The following macros define portable access to the 64 bit |
| 62 |
** math facilities. |
| 63 |
** |
| 64 |
***********************************************************************/ |
| 65 |
|
| 66 |
/*********************************************************************** |
| 67 |
** MACROS: LL_<relational operators> |
| 68 |
** |
| 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)) |
| 81 |
|
| 82 |
/*********************************************************************** |
| 83 |
** MACROS: LL_<logical operators> |
| 84 |
** |
| 85 |
** LL_AND Logical and |
| 86 |
** LL_OR Logical or |
| 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)) |
| 96 |
|
| 97 |
/*********************************************************************** |
| 98 |
** MACROS: LL_<mathematical operators> |
| 99 |
** |
| 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)) |
| 107 |
|
| 108 |
/*********************************************************************** |
| 109 |
** MACROS: LL_<mathematical operators> |
| 110 |
** |
| 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)) |
| 118 |
|
| 119 |
/*********************************************************************** |
| 120 |
** MACROS: LL_<shifting operators> |
| 121 |
** |
| 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)) |
| 131 |
|
| 132 |
/*********************************************************************** |
| 133 |
** MACROS: LL_<conversion operators> |
| 134 |
** |
| 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)) |
| 148 |
|
| 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)) |
| 153 |
|
| 154 |
/*********************************************************************** |
| 155 |
** MACROS: LL_UDIVMOD |
| 156 |
** DESCRIPTION: |
| 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))) |
| 166 |
|
| 167 |
#else /* !HAVE_LONG_LONG */ |
| 168 |
|
| 169 |
#define LL_MAXINT LL_MaxInt() |
| 170 |
#define LL_MININT LL_MinInt() |
| 171 |
#define LL_ZERO LL_Zero() |
| 172 |
#define LL_MAXUINT LL_MaxUint() |
| 173 |
|
| 174 |
#ifdef IS_LITTLE_ENDIAN |
| 175 |
#define LL_INIT(hi, lo) {PR_UINT32(lo), PR_UINT32(hi)} |
| 176 |
#else |
| 177 |
#define LL_INIT(hi, lo) {PR_UINT32(hi), PR_UINT32(lo)} |
| 178 |
#endif |
| 179 |
|
| 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) |
| 184 |
|
| 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) : \ |
| 188 |
((a).hi op (b).hi)) |
| 189 |
|
| 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, \ |
| 199 |
(r).hi = ~(a).hi) |
| 200 |
|
| 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) { \ |
| 204 |
PRInt64 _a, _b; \ |
| 205 |
_a = a; _b = b; \ |
| 206 |
(r).lo = _a.lo + _b.lo; \ |
| 207 |
(r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \ |
| 208 |
} |
| 209 |
|
| 210 |
#define LL_SUB(r, a, b) { \ |
| 211 |
PRInt64 _a, _b; \ |
| 212 |
_a = a; _b = b; \ |
| 213 |
(r).lo = _a.lo - _b.lo; \ |
| 214 |
(r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \ |
| 215 |
} |
| 216 |
|
| 217 |
#define LL_MUL(r, a, b) { \ |
| 218 |
PRInt64 _a, _b; \ |
| 219 |
_a = a; _b = b; \ |
| 220 |
LL_MUL32(r, _a.lo, _b.lo); \ |
| 221 |
(r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \ |
| 222 |
} |
| 223 |
|
| 224 |
#define _lo16(a) ((a) & PR_BITMASK(16)) |
| 225 |
#define _hi16(a) ((a) >> 16) |
| 226 |
|
| 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); \ |
| 231 |
_y0 = _a0 * _b0; \ |
| 232 |
_y1 = _a0 * _b1; \ |
| 233 |
_y2 = _a1 * _b0; \ |
| 234 |
_y3 = _a1 * _b1; \ |
| 235 |
_y1 += _hi16(_y0); /* can't carry */ \ |
| 236 |
_y1 += _y2; /* might carry */ \ |
| 237 |
if (_y1 < _y2) \ |
| 238 |
_y3 += (PRUint32)(PR_BIT(16)); /* propagate */ \ |
| 239 |
(r).lo = (_lo16(_y1) << 16) + _lo16(_y0); \ |
| 240 |
(r).hi = _y3 + _hi16(_y1); \ |
| 241 |
} |
| 242 |
|
| 243 |
#define LL_UDIVMOD(qp, rp, a, b) ll_udivmod(qp, rp, a, b) |
| 244 |
|
| 245 |
NSPR_API(void) ll_udivmod(PRUint64 *qp, PRUint64 *rp, PRUint64 a, PRUint64 b); |
| 246 |
|
| 247 |
#define LL_DIV(r, a, b) { \ |
| 248 |
PRInt64 _a, _b; \ |
| 249 |
PRUint32 _negative = (PRInt32)(a).hi < 0; \ |
| 250 |
if (_negative) { \ |
| 251 |
LL_NEG(_a, a); \ |
| 252 |
} else { \ |
| 253 |
_a = a; \ |
| 254 |
} \ |
| 255 |
if ((PRInt32)(b).hi < 0) { \ |
| 256 |
_negative ^= 1; \ |
| 257 |
LL_NEG(_b, b); \ |
| 258 |
} else { \ |
| 259 |
_b = b; \ |
| 260 |
} \ |
| 261 |
LL_UDIVMOD(&(r), 0, _a, _b); \ |
| 262 |
if (_negative) \ |
| 263 |
LL_NEG(r, r); \ |
| 264 |
} |
| 265 |
|
| 266 |
#define LL_MOD(r, a, b) { \ |
| 267 |
PRInt64 _a, _b; \ |
| 268 |
PRUint32 _negative = (PRInt32)(a).hi < 0; \ |
| 269 |
if (_negative) { \ |
| 270 |
LL_NEG(_a, a); \ |
| 271 |
} else { \ |
| 272 |
_a = a; \ |
| 273 |
} \ |
| 274 |
if ((PRInt32)(b).hi < 0) { \ |
| 275 |
LL_NEG(_b, b); \ |
| 276 |
} else { \ |
| 277 |
_b = b; \ |
| 278 |
} \ |
| 279 |
LL_UDIVMOD(0, &(r), _a, _b); \ |
| 280 |
if (_negative) \ |
| 281 |
LL_NEG(r, r); \ |
| 282 |
} |
| 283 |
|
| 284 |
#define LL_SHL(r, a, b) { \ |
| 285 |
if (b) { \ |
| 286 |
PRInt64 _a; \ |
| 287 |
_a = a; \ |
| 288 |
if ((b) < 32) { \ |
| 289 |
(r).lo = _a.lo << ((b) & 31); \ |
| 290 |
(r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \ |
| 291 |
} else { \ |
| 292 |
(r).lo = 0; \ |
| 293 |
(r).hi = _a.lo << ((b) & 31); \ |
| 294 |
} \ |
| 295 |
} else { \ |
| 296 |
(r) = (a); \ |
| 297 |
} \ |
| 298 |
} |
| 299 |
|
| 300 |
/* a is an PRInt32, b is PRInt32, r is PRInt64 */ |
| 301 |
#define LL_ISHL(r, a, b) { \ |
| 302 |
if (b) { \ |
| 303 |
PRInt64 _a; \ |
| 304 |
_a.lo = (a); \ |
| 305 |
_a.hi = 0; \ |
| 306 |
if ((b) < 32) { \ |
| 307 |
(r).lo = (a) << ((b) & 31); \ |
| 308 |
(r).hi = ((a) >> (32 - (b))); \ |
| 309 |
} else { \ |
| 310 |
(r).lo = 0; \ |
| 311 |
(r).hi = (a) << ((b) & 31); \ |
| 312 |
} \ |
| 313 |
} else { \ |
| 314 |
(r).lo = (a); \ |
| 315 |
(r).hi = 0; \ |
| 316 |
} \ |
| 317 |
} |
| 318 |
|
| 319 |
#define LL_SHR(r, a, b) { \ |
| 320 |
if (b) { \ |
| 321 |
PRInt64 _a; \ |
| 322 |
_a = a; \ |
| 323 |
if ((b) < 32) { \ |
| 324 |
(r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \ |
| 325 |
(r).hi = (PRInt32)_a.hi >> ((b) & 31); \ |
| 326 |
} else { \ |
| 327 |
(r).lo = (PRInt32)_a.hi >> ((b) & 31); \ |
| 328 |
(r).hi = (PRInt32)_a.hi >> 31; \ |
| 329 |
} \ |
| 330 |
} else { \ |
| 331 |
(r) = (a); \ |
| 332 |
} \ |
| 333 |
} |
| 334 |
|
| 335 |
#define LL_USHR(r, a, b) { \ |
| 336 |
if (b) { \ |
| 337 |
PRInt64 _a; \ |
| 338 |
_a = a; \ |
| 339 |
if ((b) < 32) { \ |
| 340 |
(r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \ |
| 341 |
(r).hi = _a.hi >> ((b) & 31); \ |
| 342 |
} else { \ |
| 343 |
(r).lo = _a.hi >> ((b) & 31); \ |
| 344 |
(r).hi = 0; \ |
| 345 |
} \ |
| 346 |
} else { \ |
| 347 |
(r) = (a); \ |
| 348 |
} \ |
| 349 |
} |
| 350 |
|
| 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; } |
| 354 |
|
| 355 |
#define LL_L2D(d, l) { \ |
| 356 |
int _negative; \ |
| 357 |
PRInt64 _absval; \ |
| 358 |
\ |
| 359 |
_negative = (l).hi >> 31; \ |
| 360 |
if (_negative) { \ |
| 361 |
LL_NEG(_absval, l); \ |
| 362 |
} else { \ |
| 363 |
_absval = l; \ |
| 364 |
} \ |
| 365 |
(d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \ |
| 366 |
if (_negative) \ |
| 367 |
(d) = -(d); \ |
| 368 |
} |
| 369 |
|
| 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); } |
| 373 |
|
| 374 |
#define LL_D2L(l, d) { \ |
| 375 |
int _negative; \ |
| 376 |
double _absval, _d_hi; \ |
| 377 |
PRInt64 _lo_d; \ |
| 378 |
\ |
| 379 |
_negative = ((d) < 0); \ |
| 380 |
_absval = _negative ? -(d) : (d); \ |
| 381 |
\ |
| 382 |
(l).hi = _absval / 4.294967296e9; \ |
| 383 |
(l).lo = 0; \ |
| 384 |
LL_L2D(_d_hi, l); \ |
| 385 |
_absval -= _d_hi; \ |
| 386 |
_lo_d.hi = 0; \ |
| 387 |
if (_absval < 0) { \ |
| 388 |
_lo_d.lo = -_absval; \ |
| 389 |
LL_SUB(l, l, _lo_d); \ |
| 390 |
} else { \ |
| 391 |
_lo_d.lo = _absval; \ |
| 392 |
LL_ADD(l, l, _lo_d); \ |
| 393 |
} \ |
| 394 |
\ |
| 395 |
if (_negative) \ |
| 396 |
LL_NEG(l, l); \ |
| 397 |
} |
| 398 |
|
| 399 |
#endif /* !HAVE_LONG_LONG */ |
| 400 |
|
| 401 |
PR_END_EXTERN_C |
| 402 |
|
| 403 |
#endif /* prlong_h___ */ |