jslong.c revision 6b15695578f07a3f72c4c9475c1a261a3021472a
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm/* ***** BEGIN LICENSE BLOCK *****
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Version: MPL 1.1/GPL 2.0/LGPL 2.1
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * The contents of this file are subject to the Mozilla Public License Version
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * 1.1 (the "License"); you may not use this file except in compliance with
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * the License. You may obtain a copy of the License at
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen * Software distributed under the License is distributed on an "AS IS" basis,
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * for the specific language governing rights and limitations under the
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * License.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * The Original Code is Mozilla Communicator client code, released
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * March 31, 1998.
6656f193fdace606d1b162d6dea0223bc295f0a6cilix * The Initial Developer of the Original Code is
6656f193fdace606d1b162d6dea0223bc295f0a6cilix * Netscape Communications Corporation.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Portions created by the Initial Developer are Copyright (C) 1998
77a4a003111bd5cfb771d4849801c898aeb889b0cilix * the Initial Developer. All Rights Reserved.
04c99c338ffdc6e10cb6f5c18f6f06b3f555e8ebcilix * Contributor(s):
04c99c338ffdc6e10cb6f5c18f6f06b3f555e8ebcilix * Alternatively, the contents of this file may be used under the terms of
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * either of the GNU General Public License Version 2 or later (the "GPL"),
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * in which case the provisions of the GPL or the LGPL are applicable instead
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * of those above. If you wish to allow use of your version of this file only
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * under the terms of either the GPL or the LGPL, and not to allow others to
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * use your version of this file under the terms of the MPL, indicate your
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * decision by deleting the provisions above and replace them with the notice
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * and other provisions required by the GPL or the LGPL. If you do not delete
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * the provisions above, a recipient may use your version of this file under
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen * the terms of any one of the MPL, the GPL or the LGPL.
2e2b8bd323e3693f9d86f545ce049d3f1b45d1c2cilix * ***** END LICENSE BLOCK ***** */
dc98accfae7a38326b92d74fa4330ac8ccb5b778jfbarraudstatic JSInt64 ll_zero = JSLL_INIT( 0x00000000,0x00000000 );
dc98accfae7a38326b92d74fa4330ac8ccb5b778jfbarraudstatic JSInt64 ll_maxint = JSLL_INIT( 0x7fffffff, 0xffffffff );
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrmstatic JSInt64 ll_minint = JSLL_INIT( 0x80000000, 0x00000000 );
ab99111a42436818e6902e044c8f3af2b724263bcilixJS_PUBLIC_API(JSInt64) JSLL_Zero(void) { return ll_zero; }
76db360f5f052775326e6d406b9e1e9e2966e11acilixJS_PUBLIC_API(JSInt64) JSLL_MaxInt(void) { return ll_maxint; }
3d0482af18ffb591c1d8ddecf516629e1bcd2ae4cilixJS_PUBLIC_API(JSInt64) JSLL_MinInt(void) { return ll_minint; }
2f5c0701b333a695eedb1680beb1adf95c0723dacilix** Divide 64-bit a by 32-bit b, which must be normalized so its high bit is 1.
0a75b58e47d3de42550c4f7960e253ea3befc09ajohanengelenstatic void norm_udivmod32(JSUint32 *qp, JSUint32 *rp, JSUint64 a, JSUint32 b)
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm if (r1 < m) {
9de5f3bfa8e35ad5e6adcd6eaad4d0d49e7043a3johanengelen if (r1 >= b /* i.e., we didn't get a carry when adding to r1 */
9de5f3bfa8e35ad5e6adcd6eaad4d0d49e7043a3johanengelen if ((t = a >> 16) != 0)
9de5f3bfa8e35ad5e6adcd6eaad4d0d49e7043a3johanengelen r -= 16, a = t;
4afe3fc6b9c122bc5c02b27aea3845ba41384d2acilix if ((t = a >> 8) != 0)
9de5f3bfa8e35ad5e6adcd6eaad4d0d49e7043a3johanengelen r -= 8, a = t;
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm if ((t = a >> 4) != 0)
72b7b31db250f20b90730d2888e6a554b434a407johanengelen r -= 4, a = t;
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm if ((t = a >> 2) != 0)
a45e91fd09273632e9acf849bb72621832156f07cilix r -= 2, a = t;
494c671e141564431d7d05f141c885d9a2789db5cilix if ((t = a >> 1) != 0)
a45e91fd09273632e9acf849bb72621832156f07cilix r -= 1, a = t;
a45e91fd09273632e9acf849bb72621832156f07cilixJS_PUBLIC_API(void) jsll_udivmod(JSUint64 *qp, JSUint64 *rp, JSUint64 a, JSUint64 b)
8c39cbeab9949a0a7d6ae66b768a7352019e42f8johanengelen if (b.hi == 0) {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm /* (0 q0) = (n1 n0) / (0 D0) */
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen * Normalize, i.e. make the most significant bit of the
dc98accfae7a38326b92d74fa4330ac8ccb5b778jfbarraud * denominator be set.
92fe3142613d000eff89db8a983b3b18b14eee79johanengelen /* remainder is in n0 >> lsh */
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm /* (q1 q0) = (n1 n0) / (0 d0) */
92fe3142613d000eff89db8a983b3b18b14eee79johanengelen if (b.lo == 0) /* user wants to divide by zero! */
0563fd55cbad59e8a878e6d4cbbdd8e47f74488djohanengelen * From (n1 >= b.lo)
0563fd55cbad59e8a878e6d4cbbdd8e47f74488djohanengelen * && (the most significant bit of b.lo is set),
0563fd55cbad59e8a878e6d4cbbdd8e47f74488djohanengelen * conclude that
0563fd55cbad59e8a878e6d4cbbdd8e47f74488djohanengelen * (the most significant bit of n1 is set)
8d9f5d586a04809427ce1df284a5720112177991cilix * && (the leading quotient digit q1 = 1).
8d9f5d586a04809427ce1df284a5720112177991cilix * This special case is necessary, not an optimization
70eb1fc448cb08acf3468f80fa2296c03b32afd2cilix * (Shifts counts of 32 are undefined).
6f4a90e526af850ffc36064f58f09c190f3b633fjohanengelen * Normalize.
3d0482af18ffb591c1d8ddecf516629e1bcd2ae4cilix /* n1 != b.lo... */
b320a8d186114a5122ddc3afbe95110eb6cb10cecilix /* remainder in n0 >> lsh */
2f5c0701b333a695eedb1680beb1adf95c0723dacilix /* (0 0) = (n1 n0) / (D1 d0) */
add2ffae3c4686b50d888775bbdf083a4726a210johanengelen /* remainder in (n1 n0) */
4afe3fc6b9c122bc5c02b27aea3845ba41384d2acilix /* (0 q0) = (n1 n0) / (d1 d0) */
ecf048161ae0284d01d34ca5844204775c0d3fdecilix if (lsh == 0) {
3515994554d167522343ce57417648b39370ccabcilix * From (n1 >= b.hi)
3515994554d167522343ce57417648b39370ccabcilix * && (the most significant bit of b.hi is set),
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * conclude that
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * (the most significant bit of n1 is set)
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * && (the quotient digit q0 = 0 or 1).
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * This special case is necessary, not an optimization.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * The condition on the next line takes advantage of that
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * n1 >= b.hi (true due to control flow).
6656f193fdace606d1b162d6dea0223bc295f0a6cilix * Normalize.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm /* Remainder is ((n1 n0) - (m1 m0)) >> lsh */
c90010388b0d4045c26e81c2be28beedcb36c7d3cilix#endif /* !JS_HAVE_LONG_LONG */