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: #include "prlong.h" andre@0: andre@0: static PRInt64 ll_zero = LL_INIT( 0x00000000,0x00000000 ); andre@0: static PRInt64 ll_maxint = LL_INIT( 0x7fffffff, 0xffffffff ); andre@0: static PRInt64 ll_minint = LL_INIT( 0x80000000, 0x00000000 ); andre@0: static PRUint64 ll_maxuint = LL_INIT( 0xffffffff, 0xffffffff ); andre@0: andre@0: PR_IMPLEMENT(PRInt64) LL_Zero(void) { return ll_zero; } andre@0: PR_IMPLEMENT(PRInt64) LL_MaxInt(void) { return ll_maxint; } andre@0: PR_IMPLEMENT(PRInt64) LL_MinInt(void) { return ll_minint; } andre@0: PR_IMPLEMENT(PRUint64) LL_MaxUint(void) { return ll_maxuint; } andre@0: andre@0: #ifndef HAVE_LONG_LONG andre@0: /* andre@0: ** Divide 64-bit a by 32-bit b, which must be normalized so its high bit is 1. andre@0: */ andre@0: static void norm_udivmod32(PRUint32 *qp, PRUint32 *rp, PRUint64 a, PRUint32 b) andre@0: { andre@0: PRUint32 d1, d0, q1, q0; andre@0: PRUint32 r1, r0, m; andre@0: andre@0: d1 = _hi16(b); andre@0: d0 = _lo16(b); andre@0: r1 = a.hi % d1; andre@0: q1 = a.hi / d1; andre@0: m = q1 * d0; andre@0: r1 = (r1 << 16) | _hi16(a.lo); andre@0: if (r1 < m) { andre@0: q1--, r1 += b; andre@0: if (r1 >= b /* i.e., we didn't get a carry when adding to r1 */ andre@0: && r1 < m) { andre@0: q1--, r1 += b; andre@0: } andre@0: } andre@0: r1 -= m; andre@0: r0 = r1 % d1; andre@0: q0 = r1 / d1; andre@0: m = q0 * d0; andre@0: r0 = (r0 << 16) | _lo16(a.lo); andre@0: if (r0 < m) { andre@0: q0--, r0 += b; andre@0: if (r0 >= b andre@0: && r0 < m) { andre@0: q0--, r0 += b; andre@0: } andre@0: } andre@0: *qp = (q1 << 16) | q0; andre@0: *rp = r0 - m; andre@0: } andre@0: andre@0: static PRUint32 CountLeadingZeros(PRUint32 a) andre@0: { andre@0: PRUint32 t; andre@0: PRUint32 r = 32; andre@0: andre@0: if ((t = a >> 16) != 0) andre@0: r -= 16, a = t; andre@0: if ((t = a >> 8) != 0) andre@0: r -= 8, a = t; andre@0: if ((t = a >> 4) != 0) andre@0: r -= 4, a = t; andre@0: if ((t = a >> 2) != 0) andre@0: r -= 2, a = t; andre@0: if ((t = a >> 1) != 0) andre@0: r -= 1, a = t; andre@0: if (a & 1) andre@0: r--; andre@0: return r; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) ll_udivmod(PRUint64 *qp, PRUint64 *rp, PRUint64 a, PRUint64 b) andre@0: { andre@0: PRUint32 n0, n1, n2; andre@0: PRUint32 q0, q1; andre@0: PRUint32 rsh, lsh; andre@0: andre@0: n0 = a.lo; andre@0: n1 = a.hi; andre@0: andre@0: if (b.hi == 0) { andre@0: if (b.lo > n1) { andre@0: /* (0 q0) = (n1 n0) / (0 D0) */ andre@0: andre@0: lsh = CountLeadingZeros(b.lo); andre@0: andre@0: if (lsh) { andre@0: /* andre@0: * Normalize, i.e. make the most significant bit of the andre@0: * denominator be set. andre@0: */ andre@0: b.lo = b.lo << lsh; andre@0: n1 = (n1 << lsh) | (n0 >> (32 - lsh)); andre@0: n0 = n0 << lsh; andre@0: } andre@0: andre@0: a.lo = n0, a.hi = n1; andre@0: norm_udivmod32(&q0, &n0, a, b.lo); andre@0: q1 = 0; andre@0: andre@0: /* remainder is in n0 >> lsh */ andre@0: } else { andre@0: /* (q1 q0) = (n1 n0) / (0 d0) */ andre@0: andre@0: if (b.lo == 0) /* user wants to divide by zero! */ andre@0: b.lo = 1 / b.lo; /* so go ahead and crash */ andre@0: andre@0: lsh = CountLeadingZeros(b.lo); andre@0: andre@0: if (lsh == 0) { andre@0: /* andre@0: * From (n1 >= b.lo) andre@0: * && (the most significant bit of b.lo is set), andre@0: * conclude that andre@0: * (the most significant bit of n1 is set) andre@0: * && (the leading quotient digit q1 = 1). andre@0: * andre@0: * This special case is necessary, not an optimization andre@0: * (Shifts counts of 32 are undefined). andre@0: */ andre@0: n1 -= b.lo; andre@0: q1 = 1; andre@0: } else { andre@0: /* andre@0: * Normalize. andre@0: */ andre@0: rsh = 32 - lsh; andre@0: andre@0: b.lo = b.lo << lsh; andre@0: n2 = n1 >> rsh; andre@0: n1 = (n1 << lsh) | (n0 >> rsh); andre@0: n0 = n0 << lsh; andre@0: andre@0: a.lo = n1, a.hi = n2; andre@0: norm_udivmod32(&q1, &n1, a, b.lo); andre@0: } andre@0: andre@0: /* n1 != b.lo... */ andre@0: andre@0: a.lo = n0, a.hi = n1; andre@0: norm_udivmod32(&q0, &n0, a, b.lo); andre@0: andre@0: /* remainder in n0 >> lsh */ andre@0: } andre@0: andre@0: if (rp) { andre@0: rp->lo = n0 >> lsh; andre@0: rp->hi = 0; andre@0: } andre@0: } else { andre@0: if (b.hi > n1) { andre@0: /* (0 0) = (n1 n0) / (D1 d0) */ andre@0: andre@0: q0 = 0; andre@0: q1 = 0; andre@0: andre@0: /* remainder in (n1 n0) */ andre@0: if (rp) { andre@0: rp->lo = n0; andre@0: rp->hi = n1; andre@0: } andre@0: } else { andre@0: /* (0 q0) = (n1 n0) / (d1 d0) */ andre@0: andre@0: lsh = CountLeadingZeros(b.hi); andre@0: if (lsh == 0) { andre@0: /* andre@0: * From (n1 >= b.hi) andre@0: * && (the most significant bit of b.hi is set), andre@0: * conclude that andre@0: * (the most significant bit of n1 is set) andre@0: * && (the quotient digit q0 = 0 or 1). andre@0: * andre@0: * This special case is necessary, not an optimization. andre@0: */ andre@0: andre@0: /* andre@0: * The condition on the next line takes advantage of that andre@0: * n1 >= b.hi (true due to control flow). andre@0: */ andre@0: if (n1 > b.hi || n0 >= b.lo) { andre@0: q0 = 1; andre@0: a.lo = n0, a.hi = n1; andre@0: LL_SUB(a, a, b); andre@0: } else { andre@0: q0 = 0; andre@0: } andre@0: q1 = 0; andre@0: andre@0: if (rp) { andre@0: rp->lo = n0; andre@0: rp->hi = n1; andre@0: } andre@0: } else { andre@0: PRInt64 m; andre@0: andre@0: /* andre@0: * Normalize. andre@0: */ andre@0: rsh = 32 - lsh; andre@0: andre@0: b.hi = (b.hi << lsh) | (b.lo >> rsh); andre@0: b.lo = b.lo << lsh; andre@0: n2 = n1 >> rsh; andre@0: n1 = (n1 << lsh) | (n0 >> rsh); andre@0: n0 = n0 << lsh; andre@0: andre@0: a.lo = n1, a.hi = n2; andre@0: norm_udivmod32(&q0, &n1, a, b.hi); andre@0: LL_MUL32(m, q0, b.lo); andre@0: andre@0: if ((m.hi > n1) || ((m.hi == n1) && (m.lo > n0))) { andre@0: q0--; andre@0: LL_SUB(m, m, b); andre@0: } andre@0: andre@0: q1 = 0; andre@0: andre@0: /* Remainder is ((n1 n0) - (m1 m0)) >> lsh */ andre@0: if (rp) { andre@0: a.lo = n0, a.hi = n1; andre@0: LL_SUB(a, a, m); andre@0: rp->lo = (a.hi << rsh) | (a.lo >> lsh); andre@0: rp->hi = a.hi >> lsh; andre@0: } andre@0: } andre@0: } andre@0: } andre@0: andre@0: if (qp) { andre@0: qp->lo = q0; andre@0: qp->hi = q1; andre@0: } andre@0: } andre@0: #endif /* !HAVE_LONG_LONG */