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