|
|
1.1 root 1: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2: /*
3: * The contents of this file are subject to the Netscape Public
4: * License Version 1.1 (the "License"); you may not use this file
5: * except in compliance with the License. You may obtain a copy of
6: * the License at http://www.mozilla.org/NPL/
7: *
8: * Software distributed under the License is distributed on an "AS
9: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10: * implied. See the License for the specific language governing
11: * rights and limitations under the License.
12: *
13: * The Original Code is Mozilla Communicator client code, released
14: * March 31, 1998.
15: *
16: * The Initial Developer of the Original Code is Netscape
17: * Communications Corporation. Portions created by Netscape are
18: * Copyright (C) 1998 Netscape Communications Corporation. All
19: * Rights Reserved.
20: *
21: * Contributor(s):
22: *
23: * Alternatively, the contents of this file may be used under the
24: * terms of the GNU Public License (the "GPL"), in which case the
25: * provisions of the GPL are applicable instead of those above.
26: * If you wish to allow use of your version of this file only
27: * under the terms of the GPL and not to allow others to use your
28: * version of this file under the NPL, indicate your decision by
29: * deleting the provisions above and replace them with the notice
30: * and other provisions required by the GPL. If you do not delete
31: * the provisions above, a recipient may use your version of this
32: * file under either the NPL or the GPL.
33: */
34:
35: /*
36: ** File: jslong.h
37: ** Description: Portable access to 64 bit numerics
38: **
39: ** Long-long (64-bit signed integer type) support. Some C compilers
40: ** don't support 64 bit integers yet, so we use these macros to
41: ** support both machines that do and don't.
42: **/
43: #ifndef jslong_h___
44: #define jslong_h___
45:
46: #include "jstypes.h"
47:
48: JS_BEGIN_EXTERN_C
49:
50: /***********************************************************************
51: ** DEFINES: JSLL_MaxInt
52: ** JSLL_MinInt
53: ** JSLL_Zero
54: ** DESCRIPTION:
55: ** Various interesting constants and static variable
56: ** initializer
57: ***********************************************************************/
58: #ifdef HAVE_WATCOM_BUG_2
59: JSInt64 __pascal __loadds __export
60: JSLL_MaxInt(void);
61: JSInt64 __pascal __loadds __export
62: JSLL_MinInt(void);
63: JSInt64 __pascal __loadds __export
64: JSLL_Zero(void);
65: #else
66: extern JS_PUBLIC_API(JSInt64) JSLL_MaxInt(void);
67: extern JS_PUBLIC_API(JSInt64) JSLL_MinInt(void);
68: extern JS_PUBLIC_API(JSInt64) JSLL_Zero(void);
69: #endif
70:
71: #define JSLL_MAXINT JSLL_MaxInt()
72: #define JSLL_MININT JSLL_MinInt()
73: #define JSLL_ZERO JSLL_Zero()
74:
75: #ifdef JS_HAVE_LONG_LONG
76:
77: #if JS_BYTES_PER_LONG == 8
78: #define JSLL_INIT(hi, lo) ((hi ## L << 32) + lo ## L)
79: #elif (defined(WIN32) || defined(WIN16)) && !defined(__GNUC__)
80: #define JSLL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64)
81: #else
82: #define JSLL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL)
83: #endif
84:
85: /***********************************************************************
86: ** MACROS: JSLL_*
87: ** DESCRIPTION:
88: ** The following macros define portable access to the 64 bit
89: ** math facilities.
90: **
91: ***********************************************************************/
92:
93: /***********************************************************************
94: ** MACROS: JSLL_<relational operators>
95: **
96: ** JSLL_IS_ZERO Test for zero
97: ** JSLL_EQ Test for equality
98: ** JSLL_NE Test for inequality
99: ** JSLL_GE_ZERO Test for zero or positive
100: ** JSLL_CMP Compare two values
101: ***********************************************************************/
102: #define JSLL_IS_ZERO(a) ((a) == 0)
103: #define JSLL_EQ(a, b) ((a) == (b))
104: #define JSLL_NE(a, b) ((a) != (b))
105: #define JSLL_GE_ZERO(a) ((a) >= 0)
106: #define JSLL_CMP(a, op, b) ((JSInt64)(a) op (JSInt64)(b))
107: #define JSLL_UCMP(a, op, b) ((JSUint64)(a) op (JSUint64)(b))
108:
109: /***********************************************************************
110: ** MACROS: JSLL_<logical operators>
111: **
112: ** JSLL_AND Logical and
113: ** JSLL_OR Logical or
114: ** JSLL_XOR Logical exclusion
115: ** JSLL_OR2 A disgusting deviation
116: ** JSLL_NOT Negation (one's compliment)
117: ***********************************************************************/
118: #define JSLL_AND(r, a, b) ((r) = (a) & (b))
119: #define JSLL_OR(r, a, b) ((r) = (a) | (b))
120: #define JSLL_XOR(r, a, b) ((r) = (a) ^ (b))
121: #define JSLL_OR2(r, a) ((r) = (r) | (a))
122: #define JSLL_NOT(r, a) ((r) = ~(a))
123:
124: /***********************************************************************
125: ** MACROS: JSLL_<mathematical operators>
126: **
127: ** JSLL_NEG Negation (two's compliment)
128: ** JSLL_ADD Summation (two's compliment)
129: ** JSLL_SUB Difference (two's compliment)
130: ***********************************************************************/
131: #define JSLL_NEG(r, a) ((r) = -(a))
132: #define JSLL_ADD(r, a, b) ((r) = (a) + (b))
133: #define JSLL_SUB(r, a, b) ((r) = (a) - (b))
134:
135: /***********************************************************************
136: ** MACROS: JSLL_<mathematical operators>
137: **
138: ** JSLL_MUL Product (two's compliment)
139: ** JSLL_DIV Quotient (two's compliment)
140: ** JSLL_MOD Modulus (two's compliment)
141: ***********************************************************************/
142: #define JSLL_MUL(r, a, b) ((r) = (a) * (b))
143: #define JSLL_DIV(r, a, b) ((r) = (a) / (b))
144: #define JSLL_MOD(r, a, b) ((r) = (a) % (b))
145:
146: /***********************************************************************
147: ** MACROS: JSLL_<shifting operators>
148: **
149: ** JSLL_SHL Shift left [0..64] bits
150: ** JSLL_SHR Shift right [0..64] bits with sign extension
151: ** JSLL_USHR Unsigned shift right [0..64] bits
152: ** JSLL_ISHL Signed shift left [0..64] bits
153: ***********************************************************************/
154: #define JSLL_SHL(r, a, b) ((r) = (JSInt64)(a) << (b))
155: #define JSLL_SHR(r, a, b) ((r) = (JSInt64)(a) >> (b))
156: #define JSLL_USHR(r, a, b) ((r) = (JSUint64)(a) >> (b))
157: #define JSLL_ISHL(r, a, b) ((r) = (JSInt64)(a) << (b))
158:
159: /***********************************************************************
160: ** MACROS: JSLL_<conversion operators>
161: **
162: ** JSLL_L2I Convert to signed 32 bit
163: ** JSLL_L2UI Convert to unsigned 32 bit
164: ** JSLL_L2F Convert to floating point
165: ** JSLL_L2D Convert to floating point
166: ** JSLL_I2L Convert signed to 64 bit
167: ** JSLL_UI2L Convert unsigned to 64 bit
168: ** JSLL_F2L Convert float to 64 bit
169: ** JSLL_D2L Convert float to 64 bit
170: ***********************************************************************/
171: #define JSLL_L2I(i, l) ((i) = (JSInt32)(l))
172: #define JSLL_L2UI(ui, l) ((ui) = (JSUint32)(l))
173: #define JSLL_L2F(f, l) ((f) = (JSFloat64)(l))
174: #define JSLL_L2D(d, l) ((d) = (JSFloat64)(l))
175:
176: #define JSLL_I2L(l, i) ((l) = (JSInt64)(i))
177: #define JSLL_UI2L(l, ui) ((l) = (JSInt64)(ui))
178: #define JSLL_F2L(l, f) ((l) = (JSInt64)(f))
179: #define JSLL_D2L(l, d) ((l) = (JSInt64)(d))
180:
181: /***********************************************************************
182: ** MACROS: JSLL_UDIVMOD
183: ** DESCRIPTION:
184: ** Produce both a quotient and a remainder given an unsigned
185: ** INPUTS: JSUint64 a: The dividend of the operation
186: ** JSUint64 b: The quotient of the operation
187: ** OUTPUTS: JSUint64 *qp: pointer to quotient
188: ** JSUint64 *rp: pointer to remainder
189: ***********************************************************************/
190: #define JSLL_UDIVMOD(qp, rp, a, b) \
191: (*(qp) = ((JSUint64)(a) / (b)), \
192: *(rp) = ((JSUint64)(a) % (b)))
193:
194: #else /* !JS_HAVE_LONG_LONG */
195:
196: #ifdef IS_LITTLE_ENDIAN
197: #define JSLL_INIT(hi, lo) {JS_INT32(lo), JS_INT32(hi)}
198: #else
199: #define JSLL_INIT(hi, lo) {JS_INT32(hi), JS_INT32(lo)}
200: #endif
201:
202: #define JSLL_IS_ZERO(a) (((a).hi == 0) && ((a).lo == 0))
203: #define JSLL_EQ(a, b) (((a).hi == (b).hi) && ((a).lo == (b).lo))
204: #define JSLL_NE(a, b) (((a).hi != (b).hi) || ((a).lo != (b).lo))
205: #define JSLL_GE_ZERO(a) (((a).hi >> 31) == 0)
206:
207: #ifdef DEBUG
208: #define JSLL_CMP(a, op, b) (JS_ASSERT((#op)[1] != '='), JSLL_REAL_CMP(a, op, b))
209: #define JSLL_UCMP(a, op, b) (JS_ASSERT((#op)[1] != '='), JSLL_REAL_UCMP(a, op, b))
210: #else
211: #define JSLL_CMP(a, op, b) JSLL_REAL_CMP(a, op, b)
212: #define JSLL_UCMP(a, op, b) JSLL_REAL_UCMP(a, op, b)
213: #endif
214:
215: #define JSLL_REAL_CMP(a,op,b) (((JSInt32)(a).hi op (JSInt32)(b).hi) || \
216: (((a).hi == (b).hi) && ((a).lo op (b).lo)))
217: #define JSLL_REAL_UCMP(a,op,b) (((a).hi op (b).hi) || \
218: (((a).hi == (b).hi) && ((a).lo op (b).lo)))
219:
220: #define JSLL_AND(r, a, b) ((r).lo = (a).lo & (b).lo, \
221: (r).hi = (a).hi & (b).hi)
222: #define JSLL_OR(r, a, b) ((r).lo = (a).lo | (b).lo, \
223: (r).hi = (a).hi | (b).hi)
224: #define JSLL_XOR(r, a, b) ((r).lo = (a).lo ^ (b).lo, \
225: (r).hi = (a).hi ^ (b).hi)
226: #define JSLL_OR2(r, a) ((r).lo = (r).lo | (a).lo, \
227: (r).hi = (r).hi | (a).hi)
228: #define JSLL_NOT(r, a) ((r).lo = ~(a).lo, \
229: (r).hi = ~(a).hi)
230:
231: #define JSLL_NEG(r, a) ((r).lo = -(JSInt32)(a).lo, \
232: (r).hi = -(JSInt32)(a).hi - ((r).lo != 0))
233: #define JSLL_ADD(r, a, b) { \
234: JSInt64 _a, _b; \
235: _a = a; _b = b; \
236: (r).lo = _a.lo + _b.lo; \
237: (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
238: }
239:
240: #define JSLL_SUB(r, a, b) { \
241: JSInt64 _a, _b; \
242: _a = a; _b = b; \
243: (r).lo = _a.lo - _b.lo; \
244: (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
245: }
246:
247: #define JSLL_MUL(r, a, b) { \
248: JSInt64 _a, _b; \
249: _a = a; _b = b; \
250: JSLL_MUL32(r, _a.lo, _b.lo); \
251: (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
252: }
253:
254: #define jslo16(a) ((a) & JS_BITMASK(16))
255: #define jshi16(a) ((a) >> 16)
256:
257: #define JSLL_MUL32(r, a, b) { \
258: JSUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
259: _a1 = jshi16(a), _a0 = jslo16(a); \
260: _b1 = jshi16(b), _b0 = jslo16(b); \
261: _y0 = _a0 * _b0; \
262: _y1 = _a0 * _b1; \
263: _y2 = _a1 * _b0; \
264: _y3 = _a1 * _b1; \
265: _y1 += jshi16(_y0); /* can't carry */ \
266: _y1 += _y2; /* might carry */ \
267: if (_y1 < _y2) \
268: _y3 += (JSUint32)(JS_BIT(16)); /* propagate */ \
269: (r).lo = (jslo16(_y1) << 16) + jslo16(_y0); \
270: (r).hi = _y3 + jshi16(_y1); \
271: }
272:
273: #define JSLL_UDIVMOD(qp, rp, a, b) jsll_udivmod(qp, rp, a, b)
274:
275: extern JS_PUBLIC_API(void) jsll_udivmod(JSUint64 *qp, JSUint64 *rp, JSUint64 a, JSUint64 b);
276:
277: #define JSLL_DIV(r, a, b) { \
278: JSInt64 _a, _b; \
279: JSUint32 _negative = (JSInt32)(a).hi < 0; \
280: if (_negative) { \
281: JSLL_NEG(_a, a); \
282: } else { \
283: _a = a; \
284: } \
285: if ((JSInt32)(b).hi < 0) { \
286: _negative ^= 1; \
287: JSLL_NEG(_b, b); \
288: } else { \
289: _b = b; \
290: } \
291: JSLL_UDIVMOD(&(r), 0, _a, _b); \
292: if (_negative) \
293: JSLL_NEG(r, r); \
294: }
295:
296: #define JSLL_MOD(r, a, b) { \
297: JSInt64 _a, _b; \
298: JSUint32 _negative = (JSInt32)(a).hi < 0; \
299: if (_negative) { \
300: JSLL_NEG(_a, a); \
301: } else { \
302: _a = a; \
303: } \
304: if ((JSInt32)(b).hi < 0) { \
305: JSLL_NEG(_b, b); \
306: } else { \
307: _b = b; \
308: } \
309: JSLL_UDIVMOD(0, &(r), _a, _b); \
310: if (_negative) \
311: JSLL_NEG(r, r); \
312: }
313:
314: #define JSLL_SHL(r, a, b) { \
315: if (b) { \
316: JSInt64 _a; \
317: _a = a; \
318: if ((b) < 32) { \
319: (r).lo = _a.lo << ((b) & 31); \
320: (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
321: } else { \
322: (r).lo = 0; \
323: (r).hi = _a.lo << ((b) & 31); \
324: } \
325: } else { \
326: (r) = (a); \
327: } \
328: }
329:
330: /* a is an JSInt32, b is JSInt32, r is JSInt64 */
331: #define JSLL_ISHL(r, a, b) { \
332: if (b) { \
333: JSInt64 _a; \
334: _a.lo = (a); \
335: _a.hi = 0; \
336: if ((b) < 32) { \
337: (r).lo = (a) << ((b) & 31); \
338: (r).hi = ((a) >> (32 - (b))); \
339: } else { \
340: (r).lo = 0; \
341: (r).hi = (a) << ((b) & 31); \
342: } \
343: } else { \
344: (r).lo = (a); \
345: (r).hi = 0; \
346: } \
347: }
348:
349: #define JSLL_SHR(r, a, b) { \
350: if (b) { \
351: JSInt64 _a; \
352: _a = a; \
353: if ((b) < 32) { \
354: (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
355: (r).hi = (JSInt32)_a.hi >> ((b) & 31); \
356: } else { \
357: (r).lo = (JSInt32)_a.hi >> ((b) & 31); \
358: (r).hi = (JSInt32)_a.hi >> 31; \
359: } \
360: } else { \
361: (r) = (a); \
362: } \
363: }
364:
365: #define JSLL_USHR(r, a, b) { \
366: if (b) { \
367: JSInt64 _a; \
368: _a = a; \
369: if ((b) < 32) { \
370: (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
371: (r).hi = _a.hi >> ((b) & 31); \
372: } else { \
373: (r).lo = _a.hi >> ((b) & 31); \
374: (r).hi = 0; \
375: } \
376: } else { \
377: (r) = (a); \
378: } \
379: }
380:
381: #define JSLL_L2I(i, l) ((i) = (l).lo)
382: #define JSLL_L2UI(ui, l) ((ui) = (l).lo)
383: #define JSLL_L2F(f, l) { double _d; JSLL_L2D(_d, l); (f) = (JSFloat64)_d; }
384:
385: #define JSLL_L2D(d, l) { \
386: int _negative; \
387: JSInt64 _absval; \
388: \
389: _negative = (l).hi >> 31; \
390: if (_negative) { \
391: JSLL_NEG(_absval, l); \
392: } else { \
393: _absval = l; \
394: } \
395: (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
396: if (_negative) \
397: (d) = -(d); \
398: }
399:
400: #define JSLL_I2L(l, i) { JSInt32 _i = (i) >> 31; (l).lo = (i); (l).hi = _i; }
401: #define JSLL_UI2L(l, ui) ((l).lo = (ui), (l).hi = 0)
402: #define JSLL_F2L(l, f) { double _d = (double)f; JSLL_D2L(l, _d); }
403:
404: #define JSLL_D2L(l, d) { \
405: int _negative; \
406: double _absval, _d_hi; \
407: JSInt64 _lo_d; \
408: \
409: _negative = ((d) < 0); \
410: _absval = _negative ? -(d) : (d); \
411: \
412: (l).hi = _absval / 4.294967296e9; \
413: (l).lo = 0; \
414: JSLL_L2D(_d_hi, l); \
415: _absval -= _d_hi; \
416: _lo_d.hi = 0; \
417: if (_absval < 0) { \
418: _lo_d.lo = -_absval; \
419: JSLL_SUB(l, l, _lo_d); \
420: } else { \
421: _lo_d.lo = _absval; \
422: JSLL_ADD(l, l, _lo_d); \
423: } \
424: \
425: if (_negative) \
426: JSLL_NEG(l, l); \
427: }
428:
429: #endif /* !JS_HAVE_LONG_LONG */
430:
431: JS_END_EXTERN_C
432:
433: #endif /* jslong_h___ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.