Back to index

lightning-sunbird  0.9+nobinonly
prlong.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 the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** File:                prlong.h
00040 ** Description: Portable access to 64 bit numerics
00041 **
00042 ** Long-long (64-bit signed integer type) support. Some C compilers
00043 ** don't support 64 bit integers yet, so we use these macros to
00044 ** support both machines that do and don't.
00045 **/
00046 #ifndef prlong_h___
00047 #define prlong_h___
00048 
00049 #include "prtypes.h"
00050 
00051 PR_BEGIN_EXTERN_C
00052 
00053 /***********************************************************************
00054 ** DEFINES:     LL_MaxInt
00055 **              LL_MinInt
00056 **              LL_Zero
00057 **              LL_MaxUint
00058 ** DESCRIPTION:
00059 **      Various interesting constants and static variable
00060 **      initializer
00061 ***********************************************************************/
00062 #if defined(HAVE_WATCOM_BUG_2)
00063 PRInt64 __pascal __loadds __export
00064     LL_MaxInt(void);
00065 PRInt64 __pascal __loadds __export
00066     LL_MinInt(void);
00067 PRInt64 __pascal __loadds __export
00068     LL_Zero(void);
00069 PRUint64 __pascal __loadds __export
00070     LL_MaxUint(void);
00071 #else
00072 NSPR_API(PRInt64) LL_MaxInt(void);
00073 NSPR_API(PRInt64) LL_MinInt(void);
00074 NSPR_API(PRInt64) LL_Zero(void);
00075 NSPR_API(PRUint64) LL_MaxUint(void);
00076 #endif
00077 
00078 #if defined(HAVE_LONG_LONG)
00079 
00080 #if PR_BYTES_PER_LONG == 8
00081 #define LL_MAXINT   9223372036854775807L
00082 #define LL_MININT   (-LL_MAXINT - 1L)
00083 #define LL_ZERO     0L
00084 #define LL_MAXUINT  18446744073709551615UL
00085 #define LL_INIT(hi, lo)  ((hi ## L << 32) + lo ## L)
00086 #elif (defined(WIN32) || defined(WIN16)) && !defined(__GNUC__)
00087 #define LL_MAXINT   9223372036854775807i64
00088 #define LL_MININT   (-LL_MAXINT - 1i64)
00089 #define LL_ZERO     0i64
00090 #define LL_MAXUINT  18446744073709551615ui64
00091 #define LL_INIT(hi, lo)  ((hi ## i64 << 32) + lo ## i64)
00092 #else
00093 #define LL_MAXINT   9223372036854775807LL
00094 #define LL_MININT   (-LL_MAXINT - 1LL)
00095 #define LL_ZERO     0LL
00096 #define LL_MAXUINT  18446744073709551615ULL
00097 #define LL_INIT(hi, lo)  ((hi ## LL << 32) + lo ## LL)
00098 #endif
00099 
00100 /***********************************************************************
00101 ** MACROS:      LL_*
00102 ** DESCRIPTION:
00103 **      The following macros define portable access to the 64 bit
00104 **      math facilities.
00105 **
00106 ***********************************************************************/
00107 
00108 /***********************************************************************
00109 ** MACROS:      LL_<relational operators>
00110 **
00111 **  LL_IS_ZERO        Test for zero
00112 **  LL_EQ             Test for equality
00113 **  LL_NE             Test for inequality
00114 **  LL_GE_ZERO        Test for zero or positive
00115 **  LL_CMP            Compare two values
00116 ***********************************************************************/
00117 #define LL_IS_ZERO(a)       ((a) == 0)
00118 #define LL_EQ(a, b)         ((a) == (b))
00119 #define LL_NE(a, b)         ((a) != (b))
00120 #define LL_GE_ZERO(a)       ((a) >= 0)
00121 #define LL_CMP(a, op, b)    ((PRInt64)(a) op (PRInt64)(b))
00122 #define LL_UCMP(a, op, b)   ((PRUint64)(a) op (PRUint64)(b))
00123 
00124 /***********************************************************************
00125 ** MACROS:      LL_<logical operators>
00126 **
00127 **  LL_AND            Logical and
00128 **  LL_OR             Logical or
00129 **  LL_XOR            Logical exclusion
00130 **  LL_OR2            A disgusting deviation
00131 **  LL_NOT            Negation (one's complement)
00132 ***********************************************************************/
00133 #define LL_AND(r, a, b)        ((r) = (a) & (b))
00134 #define LL_OR(r, a, b)        ((r) = (a) | (b))
00135 #define LL_XOR(r, a, b)        ((r) = (a) ^ (b))
00136 #define LL_OR2(r, a)        ((r) = (r) | (a))
00137 #define LL_NOT(r, a)        ((r) = ~(a))
00138 
00139 /***********************************************************************
00140 ** MACROS:      LL_<mathematical operators>
00141 **
00142 **  LL_NEG            Negation (two's complement)
00143 **  LL_ADD            Summation (two's complement)
00144 **  LL_SUB            Difference (two's complement)
00145 ***********************************************************************/
00146 #define LL_NEG(r, a)        ((r) = -(a))
00147 #define LL_ADD(r, a, b)     ((r) = (a) + (b))
00148 #define LL_SUB(r, a, b)     ((r) = (a) - (b))
00149 
00150 /***********************************************************************
00151 ** MACROS:      LL_<mathematical operators>
00152 **
00153 **  LL_MUL            Product (two's complement)
00154 **  LL_DIV            Quotient (two's complement)
00155 **  LL_MOD            Modulus (two's complement)
00156 ***********************************************************************/
00157 #define LL_MUL(r, a, b)        ((r) = (a) * (b))
00158 #define LL_DIV(r, a, b)        ((r) = (a) / (b))
00159 #define LL_MOD(r, a, b)        ((r) = (a) % (b))
00160 
00161 /***********************************************************************
00162 ** MACROS:      LL_<shifting operators>
00163 **
00164 **  LL_SHL            Shift left [0..64] bits
00165 **  LL_SHR            Shift right [0..64] bits with sign extension
00166 **  LL_USHR           Unsigned shift right [0..64] bits
00167 **  LL_ISHL           Signed shift left [0..64] bits
00168 ***********************************************************************/
00169 #define LL_SHL(r, a, b)     ((r) = (PRInt64)(a) << (b))
00170 #define LL_SHR(r, a, b)     ((r) = (PRInt64)(a) >> (b))
00171 #define LL_USHR(r, a, b)    ((r) = (PRUint64)(a) >> (b))
00172 #define LL_ISHL(r, a, b)    ((r) = (PRInt64)(a) << (b))
00173 
00174 /***********************************************************************
00175 ** MACROS:      LL_<conversion operators>
00176 **
00177 **  LL_L2I            Convert to signed 32 bit
00178 **  LL_L2UI           Convert to unsigned 32 bit
00179 **  LL_L2F            Convert to floating point
00180 **  LL_L2D            Convert to floating point
00181 **  LL_I2L            Convert signed to 64 bit
00182 **  LL_UI2L           Convert unsigned to 64 bit
00183 **  LL_F2L            Convert float to 64 bit
00184 **  LL_D2L            Convert float to 64 bit
00185 ***********************************************************************/
00186 #define LL_L2I(i, l)        ((i) = (PRInt32)(l))
00187 #define LL_L2UI(ui, l)        ((ui) = (PRUint32)(l))
00188 #define LL_L2F(f, l)        ((f) = (PRFloat64)(l))
00189 #define LL_L2D(d, l)        ((d) = (PRFloat64)(l))
00190 
00191 #define LL_I2L(l, i)        ((l) = (PRInt64)(i))
00192 #define LL_UI2L(l, ui)        ((l) = (PRInt64)(ui))
00193 #define LL_F2L(l, f)        ((l) = (PRInt64)(f))
00194 #define LL_D2L(l, d)        ((l) = (PRInt64)(d))
00195 
00196 /***********************************************************************
00197 ** MACROS:      LL_UDIVMOD
00198 ** DESCRIPTION:
00199 **  Produce both a quotient and a remainder given an unsigned 
00200 ** INPUTS:      PRUint64 a: The dividend of the operation
00201 **              PRUint64 b: The quotient of the operation
00202 ** OUTPUTS:     PRUint64 *qp: pointer to quotient
00203 **              PRUint64 *rp: pointer to remainder
00204 ***********************************************************************/
00205 #define LL_UDIVMOD(qp, rp, a, b) \
00206     (*(qp) = ((PRUint64)(a) / (b)), \
00207      *(rp) = ((PRUint64)(a) % (b)))
00208 
00209 #else  /* !HAVE_LONG_LONG */
00210 
00211 #define LL_MAXINT   LL_MaxInt()
00212 #define LL_MININT   LL_MinInt()
00213 #define LL_ZERO     LL_Zero()
00214 #define LL_MAXUINT  LL_MaxUint()
00215 
00216 #ifdef IS_LITTLE_ENDIAN
00217 #define LL_INIT(hi, lo) {PR_UINT32(lo), PR_UINT32(hi)}
00218 #else
00219 #define LL_INIT(hi, lo) {PR_UINT32(hi), PR_UINT32(lo)}
00220 #endif
00221 
00222 #define LL_IS_ZERO(a)        (((a).hi == 0) && ((a).lo == 0))
00223 #define LL_EQ(a, b)        (((a).hi == (b).hi) && ((a).lo == (b).lo))
00224 #define LL_NE(a, b)        (((a).hi != (b).hi) || ((a).lo != (b).lo))
00225 #define LL_GE_ZERO(a)        (((a).hi >> 31) == 0)
00226 
00227 #define LL_CMP(a, op, b)    (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
00228                  ((PRInt32)(a).hi op (PRInt32)(b).hi))
00229 #define LL_UCMP(a, op, b)    (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
00230                  ((a).hi op (b).hi))
00231 
00232 #define LL_AND(r, a, b)        ((r).lo = (a).lo & (b).lo, \
00233                  (r).hi = (a).hi & (b).hi)
00234 #define LL_OR(r, a, b)        ((r).lo = (a).lo | (b).lo, \
00235                  (r).hi = (a).hi | (b).hi)
00236 #define LL_XOR(r, a, b)        ((r).lo = (a).lo ^ (b).lo, \
00237                  (r).hi = (a).hi ^ (b).hi)
00238 #define LL_OR2(r, a)        ((r).lo = (r).lo | (a).lo, \
00239                  (r).hi = (r).hi | (a).hi)
00240 #define LL_NOT(r, a)        ((r).lo = ~(a).lo, \
00241                  (r).hi = ~(a).hi)
00242 
00243 #define LL_NEG(r, a)        ((r).lo = -(PRInt32)(a).lo, \
00244                  (r).hi = -(PRInt32)(a).hi - ((r).lo != 0))
00245 #define LL_ADD(r, a, b) { \
00246     PRInt64 _a, _b; \
00247     _a = a; _b = b; \
00248     (r).lo = _a.lo + _b.lo; \
00249     (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
00250 }
00251 
00252 #define LL_SUB(r, a, b) { \
00253     PRInt64 _a, _b; \
00254     _a = a; _b = b; \
00255     (r).lo = _a.lo - _b.lo; \
00256     (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
00257 }
00258 
00259 #define LL_MUL(r, a, b) { \
00260     PRInt64 _a, _b; \
00261     _a = a; _b = b; \
00262     LL_MUL32(r, _a.lo, _b.lo); \
00263     (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
00264 }
00265 
00266 #define _lo16(a)        ((a) & PR_BITMASK(16))
00267 #define _hi16(a)        ((a) >> 16)
00268 
00269 #define LL_MUL32(r, a, b) { \
00270      PRUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
00271      _a1 = _hi16(a), _a0 = _lo16(a); \
00272      _b1 = _hi16(b), _b0 = _lo16(b); \
00273      _y0 = _a0 * _b0; \
00274      _y1 = _a0 * _b1; \
00275      _y2 = _a1 * _b0; \
00276      _y3 = _a1 * _b1; \
00277      _y1 += _hi16(_y0);                         /* can't carry */ \
00278      _y1 += _y2;                                /* might carry */ \
00279      if (_y1 < _y2)    \
00280         _y3 += (PRUint32)(PR_BIT(16));  /* propagate */ \
00281      (r).lo = (_lo16(_y1) << 16) + _lo16(_y0); \
00282      (r).hi = _y3 + _hi16(_y1); \
00283 }
00284 
00285 #define LL_UDIVMOD(qp, rp, a, b)    ll_udivmod(qp, rp, a, b)
00286 
00287 NSPR_API(void) ll_udivmod(PRUint64 *qp, PRUint64 *rp, PRUint64 a, PRUint64 b);
00288 
00289 #define LL_DIV(r, a, b) { \
00290     PRInt64 _a, _b; \
00291     PRUint32 _negative = (PRInt32)(a).hi < 0; \
00292     if (_negative) { \
00293     LL_NEG(_a, a); \
00294     } else { \
00295     _a = a; \
00296     } \
00297     if ((PRInt32)(b).hi < 0) { \
00298     _negative ^= 1; \
00299     LL_NEG(_b, b); \
00300     } else { \
00301     _b = b; \
00302     } \
00303     LL_UDIVMOD(&(r), 0, _a, _b); \
00304     if (_negative) \
00305     LL_NEG(r, r); \
00306 }
00307 
00308 #define LL_MOD(r, a, b) { \
00309     PRInt64 _a, _b; \
00310     PRUint32 _negative = (PRInt32)(a).hi < 0; \
00311     if (_negative) { \
00312     LL_NEG(_a, a); \
00313     } else { \
00314     _a = a; \
00315     } \
00316     if ((PRInt32)(b).hi < 0) { \
00317     LL_NEG(_b, b); \
00318     } else { \
00319     _b = b; \
00320     } \
00321     LL_UDIVMOD(0, &(r), _a, _b); \
00322     if (_negative) \
00323     LL_NEG(r, r); \
00324 }
00325 
00326 #define LL_SHL(r, a, b) { \
00327     if (b) { \
00328     PRInt64 _a; \
00329         _a = a; \
00330         if ((b) < 32) { \
00331         (r).lo = _a.lo << ((b) & 31); \
00332         (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
00333     } else { \
00334         (r).lo = 0; \
00335         (r).hi = _a.lo << ((b) & 31); \
00336     } \
00337     } else { \
00338     (r) = (a); \
00339     } \
00340 }
00341 
00342 /* a is an PRInt32, b is PRInt32, r is PRInt64 */
00343 #define LL_ISHL(r, a, b) { \
00344     if (b) { \
00345     PRInt64 _a; \
00346     _a.lo = (a); \
00347     _a.hi = 0; \
00348         if ((b) < 32) { \
00349         (r).lo = (a) << ((b) & 31); \
00350         (r).hi = ((a) >> (32 - (b))); \
00351     } else { \
00352         (r).lo = 0; \
00353         (r).hi = (a) << ((b) & 31); \
00354     } \
00355     } else { \
00356     (r).lo = (a); \
00357     (r).hi = 0; \
00358     } \
00359 }
00360 
00361 #define LL_SHR(r, a, b) { \
00362     if (b) { \
00363     PRInt64 _a; \
00364         _a = a; \
00365     if ((b) < 32) { \
00366         (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
00367         (r).hi = (PRInt32)_a.hi >> ((b) & 31); \
00368     } else { \
00369         (r).lo = (PRInt32)_a.hi >> ((b) & 31); \
00370         (r).hi = (PRInt32)_a.hi >> 31; \
00371     } \
00372     } else { \
00373     (r) = (a); \
00374     } \
00375 }
00376 
00377 #define LL_USHR(r, a, b) { \
00378     if (b) { \
00379     PRInt64 _a; \
00380         _a = a; \
00381     if ((b) < 32) { \
00382         (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
00383         (r).hi = _a.hi >> ((b) & 31); \
00384     } else { \
00385         (r).lo = _a.hi >> ((b) & 31); \
00386         (r).hi = 0; \
00387     } \
00388     } else { \
00389     (r) = (a); \
00390     } \
00391 }
00392 
00393 #define LL_L2I(i, l)        ((i) = (l).lo)
00394 #define LL_L2UI(ui, l)        ((ui) = (l).lo)
00395 #define LL_L2F(f, l)        { double _d; LL_L2D(_d, l); (f) = (PRFloat64)_d; }
00396 
00397 #define LL_L2D(d, l) { \
00398     int _negative; \
00399     PRInt64 _absval; \
00400  \
00401     _negative = (l).hi >> 31; \
00402     if (_negative) { \
00403     LL_NEG(_absval, l); \
00404     } else { \
00405     _absval = l; \
00406     } \
00407     (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
00408     if (_negative) \
00409     (d) = -(d); \
00410 }
00411 
00412 #define LL_I2L(l, i)        { PRInt32 _i = ((PRInt32)(i)) >> 31; (l).lo = (i); (l).hi = _i; }
00413 #define LL_UI2L(l, ui)      ((l).lo = (ui), (l).hi = 0)
00414 #define LL_F2L(l, f)        { double _d = (double)f; LL_D2L(l, _d); }
00415 
00416 #define LL_D2L(l, d) { \
00417     int _negative; \
00418     double _absval, _d_hi; \
00419     PRInt64 _lo_d; \
00420  \
00421     _negative = ((d) < 0); \
00422     _absval = _negative ? -(d) : (d); \
00423  \
00424     (l).hi = _absval / 4.294967296e9; \
00425     (l).lo = 0; \
00426     LL_L2D(_d_hi, l); \
00427     _absval -= _d_hi; \
00428     _lo_d.hi = 0; \
00429     if (_absval < 0) { \
00430     _lo_d.lo = -_absval; \
00431     LL_SUB(l, l, _lo_d); \
00432     } else { \
00433     _lo_d.lo = _absval; \
00434     LL_ADD(l, l, _lo_d); \
00435     } \
00436  \
00437     if (_negative) \
00438     LL_NEG(l, l); \
00439 }
00440 
00441 #endif /* !HAVE_LONG_LONG */
00442 
00443 PR_END_EXTERN_C
00444 
00445 #endif /* prlong_h___ */