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 ***** */
41 ** Description: Portable access to 64 bit numerics
43 ** Long-long (64-bit signed integer type) support. Some C compilers
44 ** don't support 64 bit integers yet, so we use these macros to
45 ** support both machines that do and don't.
54 /***********************************************************************
55 ** DEFINES: JSLL_MaxInt
59 ** Various interesting constants and static variable
61 ***********************************************************************/
62 #ifdef HAVE_WATCOM_BUG_2
63 JSInt64 __pascal __loadds __export
65 JSInt64 __pascal __loadds __export
67 JSInt64 __pascal __loadds __export
70 extern JS_PUBLIC_API(JSInt64
) JSLL_MaxInt(void);
71 extern JS_PUBLIC_API(JSInt64
) JSLL_MinInt(void);
72 extern JS_PUBLIC_API(JSInt64
) JSLL_Zero(void);
75 #define JSLL_MAXINT JSLL_MaxInt()
76 #define JSLL_MININT JSLL_MinInt()
77 #define JSLL_ZERO JSLL_Zero()
79 #ifdef JS_HAVE_LONG_LONG
81 #if JS_BYTES_PER_LONG == 8
82 #define JSLL_INIT(hi, lo) ((hi ## L << 32) + lo ## L)
83 #elif (defined(WIN32) || defined(WIN16)) && !defined(__GNUC__)
84 #define JSLL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64)
86 #define JSLL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL)
89 /***********************************************************************
92 ** The following macros define portable access to the 64 bit
95 ***********************************************************************/
97 /***********************************************************************
98 ** MACROS: JSLL_<relational operators>
100 ** JSLL_IS_ZERO Test for zero
101 ** JSLL_EQ Test for equality
102 ** JSLL_NE Test for inequality
103 ** JSLL_GE_ZERO Test for zero or positive
104 ** JSLL_CMP Compare two values
105 ***********************************************************************/
106 #define JSLL_IS_ZERO(a) ((a) == 0)
107 #define JSLL_EQ(a, b) ((a) == (b))
108 #define JSLL_NE(a, b) ((a) != (b))
109 #define JSLL_GE_ZERO(a) ((a) >= 0)
110 #define JSLL_CMP(a, op, b) ((JSInt64)(a) op (JSInt64)(b))
111 #define JSLL_UCMP(a, op, b) ((JSUint64)(a) op (JSUint64)(b))
113 /***********************************************************************
114 ** MACROS: JSLL_<logical operators>
116 ** JSLL_AND Logical and
117 ** JSLL_OR Logical or
118 ** JSLL_XOR Logical exclusion
119 ** JSLL_OR2 A disgusting deviation
120 ** JSLL_NOT Negation (one's compliment)
121 ***********************************************************************/
122 #define JSLL_AND(r, a, b) ((r) = (a) & (b))
123 #define JSLL_OR(r, a, b) ((r) = (a) | (b))
124 #define JSLL_XOR(r, a, b) ((r) = (a) ^ (b))
125 #define JSLL_OR2(r, a) ((r) = (r) | (a))
126 #define JSLL_NOT(r, a) ((r) = ~(a))
128 /***********************************************************************
129 ** MACROS: JSLL_<mathematical operators>
131 ** JSLL_NEG Negation (two's compliment)
132 ** JSLL_ADD Summation (two's compliment)
133 ** JSLL_SUB Difference (two's compliment)
134 ***********************************************************************/
135 #define JSLL_NEG(r, a) ((r) = -(a))
136 #define JSLL_ADD(r, a, b) ((r) = (a) + (b))
137 #define JSLL_SUB(r, a, b) ((r) = (a) - (b))
139 /***********************************************************************
140 ** MACROS: JSLL_<mathematical operators>
142 ** JSLL_MUL Product (two's compliment)
143 ** JSLL_DIV Quotient (two's compliment)
144 ** JSLL_MOD Modulus (two's compliment)
145 ***********************************************************************/
146 #define JSLL_MUL(r, a, b) ((r) = (a) * (b))
147 #define JSLL_DIV(r, a, b) ((r) = (a) / (b))
148 #define JSLL_MOD(r, a, b) ((r) = (a) % (b))
150 /***********************************************************************
151 ** MACROS: JSLL_<shifting operators>
153 ** JSLL_SHL Shift left [0..64] bits
154 ** JSLL_SHR Shift right [0..64] bits with sign extension
155 ** JSLL_USHR Unsigned shift right [0..64] bits
156 ** JSLL_ISHL Signed shift left [0..64] bits
157 ***********************************************************************/
158 #define JSLL_SHL(r, a, b) ((r) = (JSInt64)(a) << (b))
159 #define JSLL_SHR(r, a, b) ((r) = (JSInt64)(a) >> (b))
160 #define JSLL_USHR(r, a, b) ((r) = (JSUint64)(a) >> (b))
161 #define JSLL_ISHL(r, a, b) ((r) = (JSInt64)(a) << (b))
163 /***********************************************************************
164 ** MACROS: JSLL_<conversion operators>
166 ** JSLL_L2I Convert to signed 32 bit
167 ** JSLL_L2UI Convert to unsigned 32 bit
168 ** JSLL_L2F Convert to floating point
169 ** JSLL_L2D Convert to floating point
170 ** JSLL_I2L Convert signed to 64 bit
171 ** JSLL_UI2L Convert unsigned to 64 bit
172 ** JSLL_F2L Convert float to 64 bit
173 ** JSLL_D2L Convert float to 64 bit
174 ***********************************************************************/
175 #define JSLL_L2I(i, l) ((i) = (JSInt32)(l))
176 #define JSLL_L2UI(ui, l) ((ui) = (JSUint32)(l))
177 #define JSLL_L2F(f, l) ((f) = (JSFloat64)(l))
178 #define JSLL_L2D(d, l) ((d) = (JSFloat64)(l))
180 #define JSLL_I2L(l, i) ((l) = (JSInt64)(i))
181 #define JSLL_UI2L(l, ui) ((l) = (JSInt64)(ui))
182 #define JSLL_F2L(l, f) ((l) = (JSInt64)(f))
183 #define JSLL_D2L(l, d) ((l) = (JSInt64)(d))
185 /***********************************************************************
186 ** MACROS: JSLL_UDIVMOD
188 ** Produce both a quotient and a remainder given an unsigned
189 ** INPUTS: JSUint64 a: The dividend of the operation
190 ** JSUint64 b: The quotient of the operation
191 ** OUTPUTS: JSUint64 *qp: pointer to quotient
192 ** JSUint64 *rp: pointer to remainder
193 ***********************************************************************/
194 #define JSLL_UDIVMOD(qp, rp, a, b) \
195 (*(qp) = ((JSUint64)(a) / (b)), \
196 *(rp) = ((JSUint64)(a) % (b)))
198 #else /* !JS_HAVE_LONG_LONG */
200 #ifdef IS_LITTLE_ENDIAN
201 #define JSLL_INIT(hi, lo) {JS_INT32(lo), JS_INT32(hi)}
203 #define JSLL_INIT(hi, lo) {JS_INT32(hi), JS_INT32(lo)}
206 #define JSLL_IS_ZERO(a) (((a).hi == 0) && ((a).lo == 0))
207 #define JSLL_EQ(a, b) (((a).hi == (b).hi) && ((a).lo == (b).lo))
208 #define JSLL_NE(a, b) (((a).hi != (b).hi) || ((a).lo != (b).lo))
209 #define JSLL_GE_ZERO(a) (((a).hi >> 31) == 0)
212 #define JSLL_CMP(a, op, b) (JS_ASSERT((#op)[1] != '='), JSLL_REAL_CMP(a, op, b))
213 #define JSLL_UCMP(a, op, b) (JS_ASSERT((#op)[1] != '='), JSLL_REAL_UCMP(a, op, b))
215 #define JSLL_CMP(a, op, b) JSLL_REAL_CMP(a, op, b)
216 #define JSLL_UCMP(a, op, b) JSLL_REAL_UCMP(a, op, b)
219 #define JSLL_REAL_CMP(a,op,b) (((JSInt32)(a).hi op (JSInt32)(b).hi) || \
220 (((a).hi == (b).hi) && ((a).lo op (b).lo)))
221 #define JSLL_REAL_UCMP(a,op,b) (((a).hi op (b).hi) || \
222 (((a).hi == (b).hi) && ((a).lo op (b).lo)))
224 #define JSLL_AND(r, a, b) ((r).lo = (a).lo & (b).lo, \
225 (r).hi = (a).hi & (b).hi)
226 #define JSLL_OR(r, a, b) ((r).lo = (a).lo | (b).lo, \
227 (r).hi = (a).hi | (b).hi)
228 #define JSLL_XOR(r, a, b) ((r).lo = (a).lo ^ (b).lo, \
229 (r).hi = (a).hi ^ (b).hi)
230 #define JSLL_OR2(r, a) ((r).lo = (r).lo | (a).lo, \
231 (r).hi = (r).hi | (a).hi)
232 #define JSLL_NOT(r, a) ((r).lo = ~(a).lo, \
235 #define JSLL_NEG(r, a) ((r).lo = -(JSInt32)(a).lo, \
236 (r).hi = -(JSInt32)(a).hi - ((r).lo != 0))
237 #define JSLL_ADD(r, a, b) { \
240 (r).lo = _a.lo + _b.lo; \
241 (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
244 #define JSLL_SUB(r, a, b) { \
247 (r).lo = _a.lo - _b.lo; \
248 (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
251 #define JSLL_MUL(r, a, b) { \
254 JSLL_MUL32(r, _a.lo, _b.lo); \
255 (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
258 #define jslo16(a) ((a) & JS_BITMASK(16))
259 #define jshi16(a) ((a) >> 16)
261 #define JSLL_MUL32(r, a, b) { \
262 JSUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
263 _a1 = jshi16(a), _a0 = jslo16(a); \
264 _b1 = jshi16(b), _b0 = jslo16(b); \
269 _y1 += jshi16(_y0); /* can't carry */ \
270 _y1 += _y2; /* might carry */ \
272 _y3 += (JSUint32)(JS_BIT(16)); /* propagate */ \
273 (r).lo = (jslo16(_y1) << 16) + jslo16(_y0); \
274 (r).hi = _y3 + jshi16(_y1); \
277 #define JSLL_UDIVMOD(qp, rp, a, b) jsll_udivmod(qp, rp, a, b)
279 extern JS_PUBLIC_API(void) jsll_udivmod(JSUint64
*qp
, JSUint64
*rp
, JSUint64 a
, JSUint64 b
);
281 #define JSLL_DIV(r, a, b) { \
283 JSUint32 _negative = (JSInt32)(a).hi < 0; \
289 if ((JSInt32)(b).hi < 0) { \
295 JSLL_UDIVMOD(&(r), 0, _a, _b); \
300 #define JSLL_MOD(r, a, b) { \
302 JSUint32 _negative = (JSInt32)(a).hi < 0; \
308 if ((JSInt32)(b).hi < 0) { \
313 JSLL_UDIVMOD(0, &(r), _a, _b); \
318 #define JSLL_SHL(r, a, b) { \
323 (r).lo = _a.lo << ((b) & 31); \
324 (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
327 (r).hi = _a.lo << ((b) & 31); \
334 /* a is an JSInt32, b is JSInt32, r is JSInt64 */
335 #define JSLL_ISHL(r, a, b) { \
341 (r).lo = (a) << ((b) & 31); \
342 (r).hi = ((a) >> (32 - (b))); \
345 (r).hi = (a) << ((b) & 31); \
353 #define JSLL_SHR(r, a, b) { \
358 (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
359 (r).hi = (JSInt32)_a.hi >> ((b) & 31); \
361 (r).lo = (JSInt32)_a.hi >> ((b) & 31); \
362 (r).hi = (JSInt32)_a.hi >> 31; \
369 #define JSLL_USHR(r, a, b) { \
374 (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
375 (r).hi = _a.hi >> ((b) & 31); \
377 (r).lo = _a.hi >> ((b) & 31); \
385 #define JSLL_L2I(i, l) ((i) = (l).lo)
386 #define JSLL_L2UI(ui, l) ((ui) = (l).lo)
387 #define JSLL_L2F(f, l) { double _d; JSLL_L2D(_d, l); (f) = (JSFloat64)_d; }
389 #define JSLL_L2D(d, l) { \
393 _negative = (l).hi >> 31; \
395 JSLL_NEG(_absval, l); \
399 (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
404 #define JSLL_I2L(l, i) { JSInt32 _i = (i) >> 31; (l).lo = (i); (l).hi = _i; }
405 #define JSLL_UI2L(l, ui) ((l).lo = (ui), (l).hi = 0)
406 #define JSLL_F2L(l, f) { double _d = (double)f; JSLL_D2L(l, _d); }
408 #define JSLL_D2L(l, d) { \
410 double _absval, _d_hi; \
413 _negative = ((d) < 0); \
414 _absval = _negative ? -(d) : (d); \
416 (l).hi = _absval / 4.294967296e9; \
418 JSLL_L2D(_d_hi, l); \
422 _lo_d.lo = -_absval; \
423 JSLL_SUB(l, l, _lo_d); \
425 _lo_d.lo = _absval; \
426 JSLL_ADD(l, l, _lo_d); \
433 #endif /* !JS_HAVE_LONG_LONG */
437 #endif /* jslong_h___ */