nsprpub/pr/include/prlong.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial