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
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/
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
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
43 #ifndef JS_HAVE_LONG_LONG
45 ** Divide 64-bit a by 32-bit b, which must be normalized so its high bit is 1.
47 static void norm_udivmod32(JSUint32
*qp
, JSUint32
*rp
, JSUint64 a
, JSUint32 b
)
49 JSUint32 d1
, d0
, q1
, q0
;
57 r1
= (r1
<< 16) | jshi16(a
.lo
);
60 if (r1
>= b
/* i.e., we didn't get a carry when adding to r1 */
69 r0
= (r0
<< 16) | jslo16(a
.lo
);
77 *qp
= (q1
<< 16) | q0
;
81 static JSUint32
CountLeadingZeros(JSUint32 a
)
86 if ((t
= a
>> 16) != 0)
88 if ((t
= a
>> 8) != 0)
90 if ((t
= a
>> 4) != 0)
92 if ((t
= a
>> 2) != 0)
94 if ((t
= a
>> 1) != 0)
101 JS_PUBLIC_API(void) jsll_udivmod(JSUint64
*qp
, JSUint64
*rp
, JSUint64 a
, JSUint64 b
)
112 /* (0 q0) = (n1 n0) / (0 D0) */
114 lsh
= CountLeadingZeros(b
.lo
);
118 * Normalize, i.e. make the most significant bit of the
119 * denominator be set.
122 n1
= (n1
<< lsh
) | (n0
>> (32 - lsh
));
126 a
.lo
= n0
, a
.hi
= n1
;
127 norm_udivmod32(&q0
, &n0
, a
, b
.lo
);
130 /* remainder is in n0 >> lsh */
132 /* (q1 q0) = (n1 n0) / (0 d0) */
134 if (b
.lo
== 0) /* user wants to divide by zero! */
135 b
.lo
= 1 / b
.lo
; /* so go ahead and crash */
137 lsh
= CountLeadingZeros(b
.lo
);
142 * && (the most significant bit of b.lo is set),
144 * (the most significant bit of n1 is set)
145 * && (the leading quotient digit q1 = 1).
147 * This special case is necessary, not an optimization
148 * (Shifts counts of 32 are undefined).
160 n1
= (n1
<< lsh
) | (n0
>> rsh
);
163 a
.lo
= n1
, a
.hi
= n2
;
164 norm_udivmod32(&q1
, &n1
, a
, b
.lo
);
169 a
.lo
= n0
, a
.hi
= n1
;
170 norm_udivmod32(&q0
, &n0
, a
, b
.lo
);
172 /* remainder in n0 >> lsh */
181 /* (0 0) = (n1 n0) / (D1 d0) */
186 /* remainder in (n1 n0) */
192 /* (0 q0) = (n1 n0) / (d1 d0) */
194 lsh
= CountLeadingZeros(b
.hi
);
198 * && (the most significant bit of b.hi is set),
200 * (the most significant bit of n1 is set)
201 * && (the quotient digit q0 = 0 or 1).
203 * This special case is necessary, not an optimization.
207 * The condition on the next line takes advantage of that
208 * n1 >= b.hi (true due to control flow).
210 if (n1
> b
.hi
|| n0
>= b
.lo
) {
212 a
.lo
= n0
, a
.hi
= n1
;
231 b
.hi
= (b
.hi
<< lsh
) | (b
.lo
>> rsh
);
234 n1
= (n1
<< lsh
) | (n0
>> rsh
);
237 a
.lo
= n1
, a
.hi
= n2
;
238 norm_udivmod32(&q0
, &n1
, a
, b
.hi
);
239 JSLL_MUL32(m
, q0
, b
.lo
);
241 if ((m
.hi
> n1
) || ((m
.hi
== n1
) && (m
.lo
> n0
))) {
248 /* Remainder is ((n1 n0) - (m1 m0)) >> lsh */
250 a
.lo
= n0
, a
.hi
= n1
;
252 rp
->lo
= (a
.hi
<< rsh
) | (a
.lo
>> lsh
);
253 rp
->hi
= a
.hi
>> lsh
;
264 #endif /* !JS_HAVE_LONG_LONG */