Back to index

lightning-sunbird  0.9+nobinonly
jslong.h
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 /*
00040 ** File:                jslong.h
00041 ** Description: Portable access to 64 bit numerics
00042 **
00043 ** Long-long (64-bit signed integer type) support. Some C compilers
00044 ** don't support 64 bit integers yet, so we use these macros to
00045 ** support both machines that do and don't.
00046 **/
00047 #ifndef jslong_h___
00048 #define jslong_h___
00049 
00050 #include "jstypes.h"
00051 
00052 JS_BEGIN_EXTERN_C
00053 
00054 /***********************************************************************
00055 ** DEFINES:     JSLL_MaxInt
00056 **              JSLL_MinInt
00057 **              JSLL_Zero
00058 ** DESCRIPTION:
00059 **      Various interesting constants and static variable
00060 **      initializer
00061 ***********************************************************************/
00062 #ifdef HAVE_WATCOM_BUG_2
00063 JSInt64 __pascal __loadds __export
00064     JSLL_MaxInt(void);
00065 JSInt64 __pascal __loadds __export
00066     JSLL_MinInt(void);
00067 JSInt64 __pascal __loadds __export
00068     JSLL_Zero(void);
00069 #else
00070 extern JS_PUBLIC_API(JSInt64) JSLL_MaxInt(void);
00071 extern JS_PUBLIC_API(JSInt64) JSLL_MinInt(void);
00072 extern JS_PUBLIC_API(JSInt64) JSLL_Zero(void);
00073 #endif
00074 
00075 #define JSLL_MAXINT   JSLL_MaxInt()
00076 #define JSLL_MININT   JSLL_MinInt()
00077 #define JSLL_ZERO     JSLL_Zero()
00078 
00079 #ifdef JS_HAVE_LONG_LONG
00080 
00081 #if JS_BYTES_PER_LONG == 8
00082 #define JSLL_INIT(hi, lo)  ((hi ## L << 32) + lo ## L)
00083 #elif (defined(WIN32) || defined(WIN16)) && !defined(__GNUC__)
00084 #define JSLL_INIT(hi, lo)  ((hi ## i64 << 32) + lo ## i64)
00085 #else
00086 #define JSLL_INIT(hi, lo)  ((hi ## LL << 32) + lo ## LL)
00087 #endif
00088 
00089 /***********************************************************************
00090 ** MACROS:      JSLL_*
00091 ** DESCRIPTION:
00092 **      The following macros define portable access to the 64 bit
00093 **      math facilities.
00094 **
00095 ***********************************************************************/
00096 
00097 /***********************************************************************
00098 ** MACROS:      JSLL_<relational operators>
00099 **
00100 **  JSLL_IS_ZERO        Test for zero
00101 **  JSLL_EQ             Test for equality
00102 **  JSLL_NE             Test for inequality
00103 **  JSLL_GE_ZERO        Test for zero or positive
00104 **  JSLL_CMP            Compare two values
00105 ***********************************************************************/
00106 #define JSLL_IS_ZERO(a)       ((a) == 0)
00107 #define JSLL_EQ(a, b)         ((a) == (b))
00108 #define JSLL_NE(a, b)         ((a) != (b))
00109 #define JSLL_GE_ZERO(a)       ((a) >= 0)
00110 #define JSLL_CMP(a, op, b)    ((JSInt64)(a) op (JSInt64)(b))
00111 #define JSLL_UCMP(a, op, b)   ((JSUint64)(a) op (JSUint64)(b))
00112 
00113 /***********************************************************************
00114 ** MACROS:      JSLL_<logical operators>
00115 **
00116 **  JSLL_AND            Logical and
00117 **  JSLL_OR             Logical or
00118 **  JSLL_XOR            Logical exclusion
00119 **  JSLL_OR2            A disgusting deviation
00120 **  JSLL_NOT            Negation (one's compliment)
00121 ***********************************************************************/
00122 #define JSLL_AND(r, a, b)        ((r) = (a) & (b))
00123 #define JSLL_OR(r, a, b)        ((r) = (a) | (b))
00124 #define JSLL_XOR(r, a, b)        ((r) = (a) ^ (b))
00125 #define JSLL_OR2(r, a)        ((r) = (r) | (a))
00126 #define JSLL_NOT(r, a)        ((r) = ~(a))
00127 
00128 /***********************************************************************
00129 ** MACROS:      JSLL_<mathematical operators>
00130 **
00131 **  JSLL_NEG            Negation (two's compliment)
00132 **  JSLL_ADD            Summation (two's compliment)
00133 **  JSLL_SUB            Difference (two's compliment)
00134 ***********************************************************************/
00135 #define JSLL_NEG(r, a)        ((r) = -(a))
00136 #define JSLL_ADD(r, a, b)     ((r) = (a) + (b))
00137 #define JSLL_SUB(r, a, b)     ((r) = (a) - (b))
00138 
00139 /***********************************************************************
00140 ** MACROS:      JSLL_<mathematical operators>
00141 **
00142 **  JSLL_MUL            Product (two's compliment)
00143 **  JSLL_DIV            Quotient (two's compliment)
00144 **  JSLL_MOD            Modulus (two's compliment)
00145 ***********************************************************************/
00146 #define JSLL_MUL(r, a, b)        ((r) = (a) * (b))
00147 #define JSLL_DIV(r, a, b)        ((r) = (a) / (b))
00148 #define JSLL_MOD(r, a, b)        ((r) = (a) % (b))
00149 
00150 /***********************************************************************
00151 ** MACROS:      JSLL_<shifting operators>
00152 **
00153 **  JSLL_SHL            Shift left [0..64] bits
00154 **  JSLL_SHR            Shift right [0..64] bits with sign extension
00155 **  JSLL_USHR           Unsigned shift right [0..64] bits
00156 **  JSLL_ISHL           Signed shift left [0..64] bits
00157 ***********************************************************************/
00158 #define JSLL_SHL(r, a, b)     ((r) = (JSInt64)(a) << (b))
00159 #define JSLL_SHR(r, a, b)     ((r) = (JSInt64)(a) >> (b))
00160 #define JSLL_USHR(r, a, b)    ((r) = (JSUint64)(a) >> (b))
00161 #define JSLL_ISHL(r, a, b)    ((r) = (JSInt64)(a) << (b))
00162 
00163 /***********************************************************************
00164 ** MACROS:      JSLL_<conversion operators>
00165 **
00166 **  JSLL_L2I            Convert to signed 32 bit
00167 **  JSLL_L2UI           Convert to unsigned 32 bit
00168 **  JSLL_L2F            Convert to floating point
00169 **  JSLL_L2D            Convert to floating point
00170 **  JSLL_I2L            Convert signed to 64 bit
00171 **  JSLL_UI2L           Convert unsigned to 64 bit
00172 **  JSLL_F2L            Convert float to 64 bit
00173 **  JSLL_D2L            Convert float to 64 bit
00174 ***********************************************************************/
00175 #define JSLL_L2I(i, l)        ((i) = (JSInt32)(l))
00176 #define JSLL_L2UI(ui, l)        ((ui) = (JSUint32)(l))
00177 #define JSLL_L2F(f, l)        ((f) = (JSFloat64)(l))
00178 #define JSLL_L2D(d, l)        ((d) = (JSFloat64)(l))
00179 
00180 #define JSLL_I2L(l, i)        ((l) = (JSInt64)(i))
00181 #define JSLL_UI2L(l, ui)        ((l) = (JSInt64)(ui))
00182 #define JSLL_F2L(l, f)        ((l) = (JSInt64)(f))
00183 #define JSLL_D2L(l, d)        ((l) = (JSInt64)(d))
00184 
00185 /***********************************************************************
00186 ** MACROS:      JSLL_UDIVMOD
00187 ** DESCRIPTION:
00188 **  Produce both a quotient and a remainder given an unsigned
00189 ** INPUTS:      JSUint64 a: The dividend of the operation
00190 **              JSUint64 b: The quotient of the operation
00191 ** OUTPUTS:     JSUint64 *qp: pointer to quotient
00192 **              JSUint64 *rp: pointer to remainder
00193 ***********************************************************************/
00194 #define JSLL_UDIVMOD(qp, rp, a, b) \
00195     (*(qp) = ((JSUint64)(a) / (b)), \
00196      *(rp) = ((JSUint64)(a) % (b)))
00197 
00198 #else  /* !JS_HAVE_LONG_LONG */
00199 
00200 #ifdef IS_LITTLE_ENDIAN
00201 #define JSLL_INIT(hi, lo) {JS_INT32(lo), JS_INT32(hi)}
00202 #else
00203 #define JSLL_INIT(hi, lo) {JS_INT32(hi), JS_INT32(lo)}
00204 #endif
00205 
00206 #define JSLL_IS_ZERO(a)         (((a).hi == 0) && ((a).lo == 0))
00207 #define JSLL_EQ(a, b)           (((a).hi == (b).hi) && ((a).lo == (b).lo))
00208 #define JSLL_NE(a, b)           (((a).hi != (b).hi) || ((a).lo != (b).lo))
00209 #define JSLL_GE_ZERO(a)         (((a).hi >> 31) == 0)
00210 
00211 #ifdef DEBUG
00212 #define JSLL_CMP(a, op, b)      (JS_ASSERT((#op)[1] != '='), JSLL_REAL_CMP(a, op, b))
00213 #define JSLL_UCMP(a, op, b)     (JS_ASSERT((#op)[1] != '='), JSLL_REAL_UCMP(a, op, b))
00214 #else
00215 #define JSLL_CMP(a, op, b)      JSLL_REAL_CMP(a, op, b)
00216 #define JSLL_UCMP(a, op, b)     JSLL_REAL_UCMP(a, op, b)
00217 #endif
00218 
00219 #define JSLL_REAL_CMP(a,op,b)   (((JSInt32)(a).hi op (JSInt32)(b).hi) || \
00220                                  (((a).hi == (b).hi) && ((a).lo op (b).lo)))
00221 #define JSLL_REAL_UCMP(a,op,b)  (((a).hi op (b).hi) || \
00222                                  (((a).hi == (b).hi) && ((a).lo op (b).lo)))
00223 
00224 #define JSLL_AND(r, a, b)       ((r).lo = (a).lo & (b).lo, \
00225                                  (r).hi = (a).hi & (b).hi)
00226 #define JSLL_OR(r, a, b)        ((r).lo = (a).lo | (b).lo, \
00227                                  (r).hi = (a).hi | (b).hi)
00228 #define JSLL_XOR(r, a, b)       ((r).lo = (a).lo ^ (b).lo, \
00229                                  (r).hi = (a).hi ^ (b).hi)
00230 #define JSLL_OR2(r, a)          ((r).lo = (r).lo | (a).lo, \
00231                                  (r).hi = (r).hi | (a).hi)
00232 #define JSLL_NOT(r, a)          ((r).lo = ~(a).lo, \
00233                                  (r).hi = ~(a).hi)
00234 
00235 #define JSLL_NEG(r, a)          ((r).lo = -(JSInt32)(a).lo, \
00236                                  (r).hi = -(JSInt32)(a).hi - ((r).lo != 0))
00237 #define JSLL_ADD(r, a, b) { \
00238     JSInt64 _a, _b; \
00239     _a = a; _b = b; \
00240     (r).lo = _a.lo + _b.lo; \
00241     (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
00242 }
00243 
00244 #define JSLL_SUB(r, a, b) { \
00245     JSInt64 _a, _b; \
00246     _a = a; _b = b; \
00247     (r).lo = _a.lo - _b.lo; \
00248     (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
00249 }
00250 
00251 #define JSLL_MUL(r, a, b) { \
00252     JSInt64 _a, _b; \
00253     _a = a; _b = b; \
00254     JSLL_MUL32(r, _a.lo, _b.lo); \
00255     (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
00256 }
00257 
00258 #define jslo16(a)        ((a) & JS_BITMASK(16))
00259 #define jshi16(a)        ((a) >> 16)
00260 
00261 #define JSLL_MUL32(r, a, b) { \
00262      JSUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
00263      _a1 = jshi16(a), _a0 = jslo16(a); \
00264      _b1 = jshi16(b), _b0 = jslo16(b); \
00265      _y0 = _a0 * _b0; \
00266      _y1 = _a0 * _b1; \
00267      _y2 = _a1 * _b0; \
00268      _y3 = _a1 * _b1; \
00269      _y1 += jshi16(_y0);                         /* can't carry */ \
00270      _y1 += _y2;                                /* might carry */ \
00271      if (_y1 < _y2)    \
00272         _y3 += (JSUint32)(JS_BIT(16));  /* propagate */ \
00273      (r).lo = (jslo16(_y1) << 16) + jslo16(_y0); \
00274      (r).hi = _y3 + jshi16(_y1); \
00275 }
00276 
00277 #define JSLL_UDIVMOD(qp, rp, a, b)    jsll_udivmod(qp, rp, a, b)
00278 
00279 extern JS_PUBLIC_API(void) jsll_udivmod(JSUint64 *qp, JSUint64 *rp, JSUint64 a, JSUint64 b);
00280 
00281 #define JSLL_DIV(r, a, b) { \
00282     JSInt64 _a, _b; \
00283     JSUint32 _negative = (JSInt32)(a).hi < 0; \
00284     if (_negative) { \
00285     JSLL_NEG(_a, a); \
00286     } else { \
00287     _a = a; \
00288     } \
00289     if ((JSInt32)(b).hi < 0) { \
00290     _negative ^= 1; \
00291     JSLL_NEG(_b, b); \
00292     } else { \
00293     _b = b; \
00294     } \
00295     JSLL_UDIVMOD(&(r), 0, _a, _b); \
00296     if (_negative) \
00297     JSLL_NEG(r, r); \
00298 }
00299 
00300 #define JSLL_MOD(r, a, b) { \
00301     JSInt64 _a, _b; \
00302     JSUint32 _negative = (JSInt32)(a).hi < 0; \
00303     if (_negative) { \
00304     JSLL_NEG(_a, a); \
00305     } else { \
00306     _a = a; \
00307     } \
00308     if ((JSInt32)(b).hi < 0) { \
00309     JSLL_NEG(_b, b); \
00310     } else { \
00311     _b = b; \
00312     } \
00313     JSLL_UDIVMOD(0, &(r), _a, _b); \
00314     if (_negative) \
00315     JSLL_NEG(r, r); \
00316 }
00317 
00318 #define JSLL_SHL(r, a, b) { \
00319     if (b) { \
00320     JSInt64 _a; \
00321         _a = a; \
00322         if ((b) < 32) { \
00323         (r).lo = _a.lo << ((b) & 31); \
00324         (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
00325     } else { \
00326         (r).lo = 0; \
00327         (r).hi = _a.lo << ((b) & 31); \
00328     } \
00329     } else { \
00330     (r) = (a); \
00331     } \
00332 }
00333 
00334 /* a is an JSInt32, b is JSInt32, r is JSInt64 */
00335 #define JSLL_ISHL(r, a, b) { \
00336     if (b) { \
00337     JSInt64 _a; \
00338     _a.lo = (a); \
00339     _a.hi = 0; \
00340         if ((b) < 32) { \
00341         (r).lo = (a) << ((b) & 31); \
00342         (r).hi = ((a) >> (32 - (b))); \
00343     } else { \
00344         (r).lo = 0; \
00345         (r).hi = (a) << ((b) & 31); \
00346     } \
00347     } else { \
00348     (r).lo = (a); \
00349     (r).hi = 0; \
00350     } \
00351 }
00352 
00353 #define JSLL_SHR(r, a, b) { \
00354     if (b) { \
00355     JSInt64 _a; \
00356         _a = a; \
00357     if ((b) < 32) { \
00358         (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
00359         (r).hi = (JSInt32)_a.hi >> ((b) & 31); \
00360     } else { \
00361         (r).lo = (JSInt32)_a.hi >> ((b) & 31); \
00362         (r).hi = (JSInt32)_a.hi >> 31; \
00363     } \
00364     } else { \
00365     (r) = (a); \
00366     } \
00367 }
00368 
00369 #define JSLL_USHR(r, a, b) { \
00370     if (b) { \
00371     JSInt64 _a; \
00372         _a = a; \
00373     if ((b) < 32) { \
00374         (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
00375         (r).hi = _a.hi >> ((b) & 31); \
00376     } else { \
00377         (r).lo = _a.hi >> ((b) & 31); \
00378         (r).hi = 0; \
00379     } \
00380     } else { \
00381     (r) = (a); \
00382     } \
00383 }
00384 
00385 #define JSLL_L2I(i, l)        ((i) = (l).lo)
00386 #define JSLL_L2UI(ui, l)        ((ui) = (l).lo)
00387 #define JSLL_L2F(f, l)        { double _d; JSLL_L2D(_d, l); (f) = (JSFloat64)_d; }
00388 
00389 #define JSLL_L2D(d, l) { \
00390     int _negative; \
00391     JSInt64 _absval; \
00392  \
00393     _negative = (l).hi >> 31; \
00394     if (_negative) { \
00395     JSLL_NEG(_absval, l); \
00396     } else { \
00397     _absval = l; \
00398     } \
00399     (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
00400     if (_negative) \
00401     (d) = -(d); \
00402 }
00403 
00404 #define JSLL_I2L(l, i)        { JSInt32 _i = (i) >> 31; (l).lo = (i); (l).hi = _i; }
00405 #define JSLL_UI2L(l, ui)      ((l).lo = (ui), (l).hi = 0)
00406 #define JSLL_F2L(l, f)        { double _d = (double)f; JSLL_D2L(l, _d); }
00407 
00408 #define JSLL_D2L(l, d) { \
00409     int _negative; \
00410     double _absval, _d_hi; \
00411     JSInt64 _lo_d; \
00412  \
00413     _negative = ((d) < 0); \
00414     _absval = _negative ? -(d) : (d); \
00415  \
00416     (l).hi = _absval / 4.294967296e9; \
00417     (l).lo = 0; \
00418     JSLL_L2D(_d_hi, l); \
00419     _absval -= _d_hi; \
00420     _lo_d.hi = 0; \
00421     if (_absval < 0) { \
00422     _lo_d.lo = -_absval; \
00423     JSLL_SUB(l, l, _lo_d); \
00424     } else { \
00425     _lo_d.lo = _absval; \
00426     JSLL_ADD(l, l, _lo_d); \
00427     } \
00428  \
00429     if (_negative) \
00430     JSLL_NEG(l, l); \
00431 }
00432 
00433 #endif /* !JS_HAVE_LONG_LONG */
00434 
00435 JS_END_EXTERN_C
00436 
00437 #endif /* jslong_h___ */