Back to index

lightning-sunbird  0.9+nobinonly
jslong.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "jsstddef.h"
00040 #include "jstypes.h"
00041 #include "jslong.h"
00042 
00043 static JSInt64 ll_zero = JSLL_INIT( 0x00000000,0x00000000 );
00044 static JSInt64 ll_maxint = JSLL_INIT( 0x7fffffff, 0xffffffff );
00045 static JSInt64 ll_minint = JSLL_INIT( 0x80000000, 0x00000000 );
00046 
00047 #ifdef HAVE_WATCOM_BUG_2
00048 JSInt64 __pascal __loadds __export
00049     JSLL_Zero(void) { return ll_zero; }
00050 JSInt64 __pascal __loadds __export
00051     JSLL_MaxInt(void) { return ll_maxint; }
00052 JSInt64 __pascal __loadds __export
00053     JSLL_MinInt(void) { return ll_minint; }
00054 #else
00055 JS_PUBLIC_API(JSInt64) JSLL_Zero(void) { return ll_zero; }
00056 JS_PUBLIC_API(JSInt64) JSLL_MaxInt(void) { return ll_maxint; }
00057 JS_PUBLIC_API(JSInt64) JSLL_MinInt(void) { return ll_minint; }
00058 #endif
00059 
00060 #ifndef JS_HAVE_LONG_LONG
00061 /*
00062 ** Divide 64-bit a by 32-bit b, which must be normalized so its high bit is 1.
00063 */
00064 static void norm_udivmod32(JSUint32 *qp, JSUint32 *rp, JSUint64 a, JSUint32 b)
00065 {
00066     JSUint32 d1, d0, q1, q0;
00067     JSUint32 r1, r0, m;
00068 
00069     d1 = jshi16(b);
00070     d0 = jslo16(b);
00071     r1 = a.hi % d1;
00072     q1 = a.hi / d1;
00073     m = q1 * d0;
00074     r1 = (r1 << 16) | jshi16(a.lo);
00075     if (r1 < m) {
00076         q1--, r1 += b;
00077         if (r1 >= b     /* i.e., we didn't get a carry when adding to r1 */
00078             && r1 < m) {
00079             q1--, r1 += b;
00080         }
00081     }
00082     r1 -= m;
00083     r0 = r1 % d1;
00084     q0 = r1 / d1;
00085     m = q0 * d0;
00086     r0 = (r0 << 16) | jslo16(a.lo);
00087     if (r0 < m) {
00088         q0--, r0 += b;
00089         if (r0 >= b
00090             && r0 < m) {
00091             q0--, r0 += b;
00092         }
00093     }
00094     *qp = (q1 << 16) | q0;
00095     *rp = r0 - m;
00096 }
00097 
00098 static JSUint32 CountLeadingZeros(JSUint32 a)
00099 {
00100     JSUint32 t;
00101     JSUint32 r = 32;
00102 
00103     if ((t = a >> 16) != 0)
00104         r -= 16, a = t;
00105     if ((t = a >> 8) != 0)
00106         r -= 8, a = t;
00107     if ((t = a >> 4) != 0)
00108         r -= 4, a = t;
00109     if ((t = a >> 2) != 0)
00110         r -= 2, a = t;
00111     if ((t = a >> 1) != 0)
00112         r -= 1, a = t;
00113     if (a & 1)
00114         r--;
00115     return r;
00116 }
00117 
00118 JS_PUBLIC_API(void) jsll_udivmod(JSUint64 *qp, JSUint64 *rp, JSUint64 a, JSUint64 b)
00119 {
00120     JSUint32 n0, n1, n2;
00121     JSUint32 q0, q1;
00122     JSUint32 rsh, lsh;
00123 
00124     n0 = a.lo;
00125     n1 = a.hi;
00126 
00127     if (b.hi == 0) {
00128         if (b.lo > n1) {
00129             /* (0 q0) = (n1 n0) / (0 D0) */
00130 
00131             lsh = CountLeadingZeros(b.lo);
00132 
00133             if (lsh) {
00134                 /*
00135                  * Normalize, i.e. make the most significant bit of the
00136                  * denominator be set.
00137                  */
00138                 b.lo = b.lo << lsh;
00139                 n1 = (n1 << lsh) | (n0 >> (32 - lsh));
00140                 n0 = n0 << lsh;
00141             }
00142 
00143             a.lo = n0, a.hi = n1;
00144             norm_udivmod32(&q0, &n0, a, b.lo);
00145             q1 = 0;
00146 
00147             /* remainder is in n0 >> lsh */
00148         } else {
00149             /* (q1 q0) = (n1 n0) / (0 d0) */
00150 
00151             if (b.lo == 0)              /* user wants to divide by zero! */
00152                 b.lo = 1 / b.lo;        /* so go ahead and crash */
00153 
00154             lsh = CountLeadingZeros(b.lo);
00155 
00156             if (lsh == 0) {
00157                 /*
00158                  * From (n1 >= b.lo)
00159                  *   && (the most significant bit of b.lo is set),
00160                  * conclude that
00161                  *      (the most significant bit of n1 is set)
00162                  *   && (the leading quotient digit q1 = 1).
00163                  *
00164                  * This special case is necessary, not an optimization
00165                  * (Shifts counts of 32 are undefined).
00166                  */
00167                 n1 -= b.lo;
00168                 q1 = 1;
00169             } else {
00170                 /*
00171                  * Normalize.
00172                  */
00173                 rsh = 32 - lsh;
00174 
00175                 b.lo = b.lo << lsh;
00176                 n2 = n1 >> rsh;
00177                 n1 = (n1 << lsh) | (n0 >> rsh);
00178                 n0 = n0 << lsh;
00179 
00180                 a.lo = n1, a.hi = n2;
00181                 norm_udivmod32(&q1, &n1, a, b.lo);
00182             }
00183 
00184             /* n1 != b.lo... */
00185 
00186             a.lo = n0, a.hi = n1;
00187             norm_udivmod32(&q0, &n0, a, b.lo);
00188 
00189             /* remainder in n0 >> lsh */
00190         }
00191 
00192         if (rp) {
00193             rp->lo = n0 >> lsh;
00194             rp->hi = 0;
00195         }
00196     } else {
00197         if (b.hi > n1) {
00198             /* (0 0) = (n1 n0) / (D1 d0) */
00199 
00200             q0 = 0;
00201             q1 = 0;
00202 
00203             /* remainder in (n1 n0) */
00204             if (rp) {
00205                 rp->lo = n0;
00206                 rp->hi = n1;
00207             }
00208         } else {
00209             /* (0 q0) = (n1 n0) / (d1 d0) */
00210 
00211             lsh = CountLeadingZeros(b.hi);
00212             if (lsh == 0) {
00213                 /*
00214                  * From (n1 >= b.hi)
00215                  *   && (the most significant bit of b.hi is set),
00216                  * conclude that
00217                  *      (the most significant bit of n1 is set)
00218                  *   && (the quotient digit q0 = 0 or 1).
00219                  *
00220                  * This special case is necessary, not an optimization.
00221                  */
00222 
00223                 /*
00224                  * The condition on the next line takes advantage of that
00225                  * n1 >= b.hi (true due to control flow).
00226                  */
00227                 if (n1 > b.hi || n0 >= b.lo) {
00228                     q0 = 1;
00229                     a.lo = n0, a.hi = n1;
00230                     JSLL_SUB(a, a, b);
00231                 } else {
00232                     q0 = 0;
00233                 }
00234                 q1 = 0;
00235 
00236                 if (rp) {
00237                     rp->lo = n0;
00238                     rp->hi = n1;
00239                 }
00240             } else {
00241                 JSInt64 m;
00242 
00243                 /*
00244                  * Normalize.
00245                  */
00246                 rsh = 32 - lsh;
00247 
00248                 b.hi = (b.hi << lsh) | (b.lo >> rsh);
00249                 b.lo = b.lo << lsh;
00250                 n2 = n1 >> rsh;
00251                 n1 = (n1 << lsh) | (n0 >> rsh);
00252                 n0 = n0 << lsh;
00253 
00254                 a.lo = n1, a.hi = n2;
00255                 norm_udivmod32(&q0, &n1, a, b.hi);
00256                 JSLL_MUL32(m, q0, b.lo);
00257 
00258                 if ((m.hi > n1) || ((m.hi == n1) && (m.lo > n0))) {
00259                     q0--;
00260                     JSLL_SUB(m, m, b);
00261                 }
00262 
00263                 q1 = 0;
00264 
00265                 /* Remainder is ((n1 n0) - (m1 m0)) >> lsh */
00266                 if (rp) {
00267                     a.lo = n0, a.hi = n1;
00268                     JSLL_SUB(a, a, m);
00269                     rp->lo = (a.hi << rsh) | (a.lo >> lsh);
00270                     rp->hi = a.hi >> lsh;
00271                 }
00272             }
00273         }
00274     }
00275 
00276     if (qp) {
00277         qp->lo = q0;
00278         qp->hi = q1;
00279     }
00280 }
00281 #endif /* !JS_HAVE_LONG_LONG */