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