andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** File: prlong.h andre@0: ** Description: Portable access to 64 bit numerics andre@0: ** andre@0: ** Long-long (64-bit signed integer type) support. Some C compilers andre@0: ** don't support 64 bit integers yet, so we use these macros to andre@0: ** support both machines that do and don't. andre@0: **/ andre@0: #ifndef prlong_h___ andre@0: #define prlong_h___ andre@0: andre@0: #include "prtypes.h" andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /*********************************************************************** andre@0: ** DEFINES: LL_MaxInt andre@0: ** LL_MinInt andre@0: ** LL_Zero andre@0: ** LL_MaxUint andre@0: ** DESCRIPTION: andre@0: ** Various interesting constants and static variable andre@0: ** initializer andre@0: ***********************************************************************/ andre@0: NSPR_API(PRInt64) LL_MaxInt(void); andre@0: NSPR_API(PRInt64) LL_MinInt(void); andre@0: NSPR_API(PRInt64) LL_Zero(void); andre@0: NSPR_API(PRUint64) LL_MaxUint(void); andre@0: andre@0: #if defined(HAVE_LONG_LONG) andre@0: andre@0: /* Keep this in sync with prtypes.h. */ andre@0: #if PR_BYTES_PER_LONG == 8 && !defined(PR_ALTERNATE_INT64_TYPEDEF) andre@0: #define LL_MAXINT 9223372036854775807L andre@0: #define LL_MININT (-LL_MAXINT - 1L) andre@0: #define LL_ZERO 0L andre@0: #define LL_MAXUINT 18446744073709551615UL andre@0: #define LL_INIT(hi, lo) ((hi ## L << 32) + lo ## L) andre@0: #elif defined(WIN32) && !defined(__GNUC__) andre@0: #define LL_MAXINT 9223372036854775807i64 andre@0: #define LL_MININT (-LL_MAXINT - 1i64) andre@0: #define LL_ZERO 0i64 andre@0: #define LL_MAXUINT 18446744073709551615ui64 andre@0: #define LL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64) andre@0: #else andre@0: #define LL_MAXINT 9223372036854775807LL andre@0: #define LL_MININT (-LL_MAXINT - 1LL) andre@0: #define LL_ZERO 0LL andre@0: #define LL_MAXUINT 18446744073709551615ULL andre@0: #define LL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL) andre@0: #endif andre@0: andre@0: /*********************************************************************** andre@0: ** MACROS: LL_* andre@0: ** DESCRIPTION: andre@0: ** The following macros define portable access to the 64 bit andre@0: ** math facilities. andre@0: ** andre@0: ***********************************************************************/ andre@0: andre@0: /*********************************************************************** andre@0: ** MACROS: LL_ andre@0: ** andre@0: ** LL_IS_ZERO Test for zero andre@0: ** LL_EQ Test for equality andre@0: ** LL_NE Test for inequality andre@0: ** LL_GE_ZERO Test for zero or positive andre@0: ** LL_CMP Compare two values andre@0: ***********************************************************************/ andre@0: #define LL_IS_ZERO(a) ((a) == 0) andre@0: #define LL_EQ(a, b) ((a) == (b)) andre@0: #define LL_NE(a, b) ((a) != (b)) andre@0: #define LL_GE_ZERO(a) ((a) >= 0) andre@0: #define LL_CMP(a, op, b) ((PRInt64)(a) op (PRInt64)(b)) andre@0: #define LL_UCMP(a, op, b) ((PRUint64)(a) op (PRUint64)(b)) andre@0: andre@0: /*********************************************************************** andre@0: ** MACROS: LL_ andre@0: ** andre@0: ** LL_AND Logical and andre@0: ** LL_OR Logical or andre@0: ** LL_XOR Logical exclusion andre@0: ** LL_OR2 A disgusting deviation andre@0: ** LL_NOT Negation (one's complement) andre@0: ***********************************************************************/ andre@0: #define LL_AND(r, a, b) ((r) = (a) & (b)) andre@0: #define LL_OR(r, a, b) ((r) = (a) | (b)) andre@0: #define LL_XOR(r, a, b) ((r) = (a) ^ (b)) andre@0: #define LL_OR2(r, a) ((r) = (r) | (a)) andre@0: #define LL_NOT(r, a) ((r) = ~(a)) andre@0: andre@0: /*********************************************************************** andre@0: ** MACROS: LL_ andre@0: ** andre@0: ** LL_NEG Negation (two's complement) andre@0: ** LL_ADD Summation (two's complement) andre@0: ** LL_SUB Difference (two's complement) andre@0: ***********************************************************************/ andre@0: #define LL_NEG(r, a) ((r) = -(a)) andre@0: #define LL_ADD(r, a, b) ((r) = (a) + (b)) andre@0: #define LL_SUB(r, a, b) ((r) = (a) - (b)) andre@0: andre@0: /*********************************************************************** andre@0: ** MACROS: LL_ andre@0: ** andre@0: ** LL_MUL Product (two's complement) andre@0: ** LL_DIV Quotient (two's complement) andre@0: ** LL_MOD Modulus (two's complement) andre@0: ***********************************************************************/ andre@0: #define LL_MUL(r, a, b) ((r) = (a) * (b)) andre@0: #define LL_DIV(r, a, b) ((r) = (a) / (b)) andre@0: #define LL_MOD(r, a, b) ((r) = (a) % (b)) andre@0: andre@0: /*********************************************************************** andre@0: ** MACROS: LL_ andre@0: ** andre@0: ** LL_SHL Shift left [0..64] bits andre@0: ** LL_SHR Shift right [0..64] bits with sign extension andre@0: ** LL_USHR Unsigned shift right [0..64] bits andre@0: ** LL_ISHL Signed shift left [0..64] bits andre@0: ***********************************************************************/ andre@0: #define LL_SHL(r, a, b) ((r) = (PRInt64)(a) << (b)) andre@0: #define LL_SHR(r, a, b) ((r) = (PRInt64)(a) >> (b)) andre@0: #define LL_USHR(r, a, b) ((r) = (PRUint64)(a) >> (b)) andre@0: #define LL_ISHL(r, a, b) ((r) = (PRInt64)(a) << (b)) andre@0: andre@0: /*********************************************************************** andre@0: ** MACROS: LL_ andre@0: ** andre@0: ** LL_L2I Convert to signed 32 bit andre@0: ** LL_L2UI Convert to unsigned 32 bit andre@0: ** LL_L2F Convert to floating point andre@0: ** LL_L2D Convert to floating point andre@0: ** LL_I2L Convert signed to 64 bit andre@0: ** LL_UI2L Convert unsigned to 64 bit andre@0: ** LL_F2L Convert float to 64 bit andre@0: ** LL_D2L Convert float to 64 bit andre@0: ***********************************************************************/ andre@0: #define LL_L2I(i, l) ((i) = (PRInt32)(l)) andre@0: #define LL_L2UI(ui, l) ((ui) = (PRUint32)(l)) andre@0: #define LL_L2F(f, l) ((f) = (PRFloat64)(l)) andre@0: #define LL_L2D(d, l) ((d) = (PRFloat64)(l)) andre@0: andre@0: #define LL_I2L(l, i) ((l) = (PRInt64)(i)) andre@0: #define LL_UI2L(l, ui) ((l) = (PRInt64)(ui)) andre@0: #define LL_F2L(l, f) ((l) = (PRInt64)(f)) andre@0: #define LL_D2L(l, d) ((l) = (PRInt64)(d)) andre@0: andre@0: /*********************************************************************** andre@0: ** MACROS: LL_UDIVMOD andre@0: ** DESCRIPTION: andre@0: ** Produce both a quotient and a remainder given an unsigned andre@0: ** INPUTS: PRUint64 a: The dividend of the operation andre@0: ** PRUint64 b: The quotient of the operation andre@0: ** OUTPUTS: PRUint64 *qp: pointer to quotient andre@0: ** PRUint64 *rp: pointer to remainder andre@0: ***********************************************************************/ andre@0: #define LL_UDIVMOD(qp, rp, a, b) \ andre@0: (*(qp) = ((PRUint64)(a) / (b)), \ andre@0: *(rp) = ((PRUint64)(a) % (b))) andre@0: andre@0: #else /* !HAVE_LONG_LONG */ andre@0: andre@0: #define LL_MAXINT LL_MaxInt() andre@0: #define LL_MININT LL_MinInt() andre@0: #define LL_ZERO LL_Zero() andre@0: #define LL_MAXUINT LL_MaxUint() andre@0: andre@0: #ifdef IS_LITTLE_ENDIAN andre@0: #define LL_INIT(hi, lo) {PR_UINT32(lo), PR_UINT32(hi)} andre@0: #else andre@0: #define LL_INIT(hi, lo) {PR_UINT32(hi), PR_UINT32(lo)} andre@0: #endif andre@0: andre@0: #define LL_IS_ZERO(a) (((a).hi == 0) && ((a).lo == 0)) andre@0: #define LL_EQ(a, b) (((a).hi == (b).hi) && ((a).lo == (b).lo)) andre@0: #define LL_NE(a, b) (((a).hi != (b).hi) || ((a).lo != (b).lo)) andre@0: #define LL_GE_ZERO(a) (((a).hi >> 31) == 0) andre@0: andre@0: #define LL_CMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \ andre@0: ((PRInt32)(a).hi op (PRInt32)(b).hi)) andre@0: #define LL_UCMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \ andre@0: ((a).hi op (b).hi)) andre@0: andre@0: #define LL_AND(r, a, b) ((r).lo = (a).lo & (b).lo, \ andre@0: (r).hi = (a).hi & (b).hi) andre@0: #define LL_OR(r, a, b) ((r).lo = (a).lo | (b).lo, \ andre@0: (r).hi = (a).hi | (b).hi) andre@0: #define LL_XOR(r, a, b) ((r).lo = (a).lo ^ (b).lo, \ andre@0: (r).hi = (a).hi ^ (b).hi) andre@0: #define LL_OR2(r, a) ((r).lo = (r).lo | (a).lo, \ andre@0: (r).hi = (r).hi | (a).hi) andre@0: #define LL_NOT(r, a) ((r).lo = ~(a).lo, \ andre@0: (r).hi = ~(a).hi) andre@0: andre@0: #define LL_NEG(r, a) ((r).lo = -(PRInt32)(a).lo, \ andre@0: (r).hi = -(PRInt32)(a).hi - ((r).lo != 0)) andre@0: #define LL_ADD(r, a, b) { \ andre@0: PRInt64 _a, _b; \ andre@0: _a = a; _b = b; \ andre@0: (r).lo = _a.lo + _b.lo; \ andre@0: (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \ andre@0: } andre@0: andre@0: #define LL_SUB(r, a, b) { \ andre@0: PRInt64 _a, _b; \ andre@0: _a = a; _b = b; \ andre@0: (r).lo = _a.lo - _b.lo; \ andre@0: (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \ andre@0: } andre@0: andre@0: #define LL_MUL(r, a, b) { \ andre@0: PRInt64 _a, _b; \ andre@0: _a = a; _b = b; \ andre@0: LL_MUL32(r, _a.lo, _b.lo); \ andre@0: (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \ andre@0: } andre@0: andre@0: #define _lo16(a) ((a) & PR_BITMASK(16)) andre@0: #define _hi16(a) ((a) >> 16) andre@0: andre@0: #define LL_MUL32(r, a, b) { \ andre@0: PRUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \ andre@0: _a1 = _hi16(a), _a0 = _lo16(a); \ andre@0: _b1 = _hi16(b), _b0 = _lo16(b); \ andre@0: _y0 = _a0 * _b0; \ andre@0: _y1 = _a0 * _b1; \ andre@0: _y2 = _a1 * _b0; \ andre@0: _y3 = _a1 * _b1; \ andre@0: _y1 += _hi16(_y0); /* can't carry */ \ andre@0: _y1 += _y2; /* might carry */ \ andre@0: if (_y1 < _y2) \ andre@0: _y3 += (PRUint32)(PR_BIT(16)); /* propagate */ \ andre@0: (r).lo = (_lo16(_y1) << 16) + _lo16(_y0); \ andre@0: (r).hi = _y3 + _hi16(_y1); \ andre@0: } andre@0: andre@0: #define LL_UDIVMOD(qp, rp, a, b) ll_udivmod(qp, rp, a, b) andre@0: andre@0: NSPR_API(void) ll_udivmod(PRUint64 *qp, PRUint64 *rp, PRUint64 a, PRUint64 b); andre@0: andre@0: #define LL_DIV(r, a, b) { \ andre@0: PRInt64 _a, _b; \ andre@0: PRUint32 _negative = (PRInt32)(a).hi < 0; \ andre@0: if (_negative) { \ andre@0: LL_NEG(_a, a); \ andre@0: } else { \ andre@0: _a = a; \ andre@0: } \ andre@0: if ((PRInt32)(b).hi < 0) { \ andre@0: _negative ^= 1; \ andre@0: LL_NEG(_b, b); \ andre@0: } else { \ andre@0: _b = b; \ andre@0: } \ andre@0: LL_UDIVMOD(&(r), 0, _a, _b); \ andre@0: if (_negative) \ andre@0: LL_NEG(r, r); \ andre@0: } andre@0: andre@0: #define LL_MOD(r, a, b) { \ andre@0: PRInt64 _a, _b; \ andre@0: PRUint32 _negative = (PRInt32)(a).hi < 0; \ andre@0: if (_negative) { \ andre@0: LL_NEG(_a, a); \ andre@0: } else { \ andre@0: _a = a; \ andre@0: } \ andre@0: if ((PRInt32)(b).hi < 0) { \ andre@0: LL_NEG(_b, b); \ andre@0: } else { \ andre@0: _b = b; \ andre@0: } \ andre@0: LL_UDIVMOD(0, &(r), _a, _b); \ andre@0: if (_negative) \ andre@0: LL_NEG(r, r); \ andre@0: } andre@0: andre@0: #define LL_SHL(r, a, b) { \ andre@0: if (b) { \ andre@0: PRInt64 _a; \ andre@0: _a = a; \ andre@0: if ((b) < 32) { \ andre@0: (r).lo = _a.lo << ((b) & 31); \ andre@0: (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \ andre@0: } else { \ andre@0: (r).lo = 0; \ andre@0: (r).hi = _a.lo << ((b) & 31); \ andre@0: } \ andre@0: } else { \ andre@0: (r) = (a); \ andre@0: } \ andre@0: } andre@0: andre@0: /* a is an PRInt32, b is PRInt32, r is PRInt64 */ andre@0: #define LL_ISHL(r, a, b) { \ andre@0: if (b) { \ andre@0: PRInt64 _a; \ andre@0: _a.lo = (a); \ andre@0: _a.hi = 0; \ andre@0: if ((b) < 32) { \ andre@0: (r).lo = (a) << ((b) & 31); \ andre@0: (r).hi = ((a) >> (32 - (b))); \ andre@0: } else { \ andre@0: (r).lo = 0; \ andre@0: (r).hi = (a) << ((b) & 31); \ andre@0: } \ andre@0: } else { \ andre@0: (r).lo = (a); \ andre@0: (r).hi = 0; \ andre@0: } \ andre@0: } andre@0: andre@0: #define LL_SHR(r, a, b) { \ andre@0: if (b) { \ andre@0: PRInt64 _a; \ andre@0: _a = a; \ andre@0: if ((b) < 32) { \ andre@0: (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \ andre@0: (r).hi = (PRInt32)_a.hi >> ((b) & 31); \ andre@0: } else { \ andre@0: (r).lo = (PRInt32)_a.hi >> ((b) & 31); \ andre@0: (r).hi = (PRInt32)_a.hi >> 31; \ andre@0: } \ andre@0: } else { \ andre@0: (r) = (a); \ andre@0: } \ andre@0: } andre@0: andre@0: #define LL_USHR(r, a, b) { \ andre@0: if (b) { \ andre@0: PRInt64 _a; \ andre@0: _a = a; \ andre@0: if ((b) < 32) { \ andre@0: (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \ andre@0: (r).hi = _a.hi >> ((b) & 31); \ andre@0: } else { \ andre@0: (r).lo = _a.hi >> ((b) & 31); \ andre@0: (r).hi = 0; \ andre@0: } \ andre@0: } else { \ andre@0: (r) = (a); \ andre@0: } \ andre@0: } andre@0: andre@0: #define LL_L2I(i, l) ((i) = (l).lo) andre@0: #define LL_L2UI(ui, l) ((ui) = (l).lo) andre@0: #define LL_L2F(f, l) { double _d; LL_L2D(_d, l); (f) = (PRFloat64)_d; } andre@0: andre@0: #define LL_L2D(d, l) { \ andre@0: int _negative; \ andre@0: PRInt64 _absval; \ andre@0: \ andre@0: _negative = (l).hi >> 31; \ andre@0: if (_negative) { \ andre@0: LL_NEG(_absval, l); \ andre@0: } else { \ andre@0: _absval = l; \ andre@0: } \ andre@0: (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \ andre@0: if (_negative) \ andre@0: (d) = -(d); \ andre@0: } andre@0: andre@0: #define LL_I2L(l, i) { PRInt32 _i = ((PRInt32)(i)) >> 31; (l).lo = (i); (l).hi = _i; } andre@0: #define LL_UI2L(l, ui) ((l).lo = (ui), (l).hi = 0) andre@0: #define LL_F2L(l, f) { double _d = (double)f; LL_D2L(l, _d); } andre@0: andre@0: #define LL_D2L(l, d) { \ andre@0: int _negative; \ andre@0: double _absval, _d_hi; \ andre@0: PRInt64 _lo_d; \ andre@0: \ andre@0: _negative = ((d) < 0); \ andre@0: _absval = _negative ? -(d) : (d); \ andre@0: \ andre@0: (l).hi = _absval / 4.294967296e9; \ andre@0: (l).lo = 0; \ andre@0: LL_L2D(_d_hi, l); \ andre@0: _absval -= _d_hi; \ andre@0: _lo_d.hi = 0; \ andre@0: if (_absval < 0) { \ andre@0: _lo_d.lo = -_absval; \ andre@0: LL_SUB(l, l, _lo_d); \ andre@0: } else { \ andre@0: _lo_d.lo = _absval; \ andre@0: LL_ADD(l, l, _lo_d); \ andre@0: } \ andre@0: \ andre@0: if (_negative) \ andre@0: LL_NEG(l, l); \ andre@0: } andre@0: andre@0: #endif /* !HAVE_LONG_LONG */ andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* prlong_h___ */