|
|
1.1 root 1: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2: *
3: * ***** BEGIN LICENSE BLOCK *****
4: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5: *
6: * The contents of this file are subject to the Mozilla Public License Version
7: * 1.1 (the "License"); you may not use this file except in compliance with
8: * the License. You may obtain a copy of the License at
9: * http://www.mozilla.org/MPL/
10: *
11: * Software distributed under the License is distributed on an "AS IS" basis,
12: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13: * for the specific language governing rights and limitations under the
14: * License.
15: *
16: * The Original Code is Mozilla Communicator client code, released
17: * March 31, 1998.
18: *
19: * The Initial Developer of the Original Code is
20: * Netscape Communications Corporation.
21: * Portions created by the Initial Developer are Copyright (C) 1998
22: * the Initial Developer. All Rights Reserved.
23: *
24: * Contributor(s):
25: *
26: * Alternatively, the contents of this file may be used under the terms of
27: * either of the GNU General Public License Version 2 or later (the "GPL"),
28: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29: * in which case the provisions of the GPL or the LGPL are applicable instead
30: * of those above. If you wish to allow use of your version of this file only
31: * under the terms of either the GPL or the LGPL, and not to allow others to
32: * use your version of this file under the terms of the MPL, indicate your
33: * decision by deleting the provisions above and replace them with the notice
34: * and other provisions required by the GPL or the LGPL. If you do not delete
35: * the provisions above, a recipient may use your version of this file under
36: * the terms of any one of the MPL, the GPL or the LGPL.
37: *
38: * ***** END LICENSE BLOCK ***** */
39:
40: #ifndef jsnum_h___
41: #define jsnum_h___
42: /*
43: * JS number (IEEE double) interface.
44: *
45: * JS numbers are optimistically stored in the top 31 bits of 32-bit integers,
46: * but floating point literals, results that overflow 31 bits, and division and
47: * modulus operands and results require a 64-bit IEEE double. These are GC'ed
48: * and pointed to by 32-bit jsvals on the stack and in object properties.
49: *
50: * When a JS number is treated as an object (followed by . or []), the runtime
51: * wraps it with a JSObject whose valueOf method returns the unwrapped number.
52: */
53:
54: JS_BEGIN_EXTERN_C
55:
56: /*
57: * Stefan Hanske <[email protected]> reports:
58: * ARM is a little endian architecture but 64 bit double words are stored
59: * differently: the 32 bit words are in little endian byte order, the two words
60: * are stored in big endian`s way.
61: */
62:
63: #if defined(__arm) || defined(__arm32__) || defined(__arm26__) || defined(__arm__)
64: #define CPU_IS_ARM
65: #endif
66:
67: typedef union jsdpun {
68: struct {
69: #if defined(IS_LITTLE_ENDIAN) && !defined(CPU_IS_ARM)
70: uint32 lo, hi;
71: #else
72: uint32 hi, lo;
73: #endif
74: } s;
75: jsdouble d;
76: } jsdpun;
77:
78: #if (__GNUC__ == 2 && __GNUC_MINOR__ > 95) || __GNUC__ > 2
79: /*
80: * This version of the macros is safe for the alias optimizations that gcc
81: * does, but uses gcc-specific extensions.
82: */
83:
84: #define JSDOUBLE_HI32(x) (__extension__ ({ jsdpun u; u.d = (x); u.s.hi; }))
85: #define JSDOUBLE_LO32(x) (__extension__ ({ jsdpun u; u.d = (x); u.s.lo; }))
86: #define JSDOUBLE_SET_HI32(x, y) \
87: (__extension__ ({ jsdpun u; u.d = (x); u.s.hi = (y); (x) = u.d; }))
88: #define JSDOUBLE_SET_LO32(x, y) \
89: (__extension__ ({ jsdpun u; u.d = (x); u.s.lo = (y); (x) = u.d; }))
90:
91: #else /* not or old GNUC */
92:
93: /*
94: * We don't know of any non-gcc compilers that perform alias optimization,
95: * so this code should work.
96: */
97:
98: #if defined(IS_LITTLE_ENDIAN) && !defined(CPU_IS_ARM)
99: #define JSDOUBLE_HI32(x) (((uint32 *)&(x))[1])
100: #define JSDOUBLE_LO32(x) (((uint32 *)&(x))[0])
101: #else
102: #define JSDOUBLE_HI32(x) (((uint32 *)&(x))[0])
103: #define JSDOUBLE_LO32(x) (((uint32 *)&(x))[1])
104: #endif
105:
106: #define JSDOUBLE_SET_HI32(x, y) (JSDOUBLE_HI32(x)=(y))
107: #define JSDOUBLE_SET_LO32(x, y) (JSDOUBLE_LO32(x)=(y))
108:
109: #endif /* not or old GNUC */
110:
111: #define JSDOUBLE_HI32_SIGNBIT 0x80000000
112: #define JSDOUBLE_HI32_EXPMASK 0x7ff00000
113: #define JSDOUBLE_HI32_MANTMASK 0x000fffff
114:
115: #define JSDOUBLE_IS_NaN(x) \
116: ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) == JSDOUBLE_HI32_EXPMASK && \
117: (JSDOUBLE_LO32(x) || (JSDOUBLE_HI32(x) & JSDOUBLE_HI32_MANTMASK)))
118:
119: #define JSDOUBLE_IS_INFINITE(x) \
120: ((JSDOUBLE_HI32(x) & ~JSDOUBLE_HI32_SIGNBIT) == JSDOUBLE_HI32_EXPMASK && \
121: !JSDOUBLE_LO32(x))
122:
123: #define JSDOUBLE_IS_FINITE(x) \
124: ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) != JSDOUBLE_HI32_EXPMASK)
125:
126: #define JSDOUBLE_IS_NEGZERO(d) (JSDOUBLE_HI32(d) == JSDOUBLE_HI32_SIGNBIT && \
127: JSDOUBLE_LO32(d) == 0)
128:
129: /*
130: * JSDOUBLE_IS_INT first checks that d is neither NaN nor infinite, to avoid
131: * raising SIGFPE on platforms such as Alpha Linux, then (only if the cast is
132: * safe) leaves i as (jsint)d. This also avoid anomalous NaN floating point
133: * comparisons under MSVC.
134: */
135: #define JSDOUBLE_IS_INT(d, i) (JSDOUBLE_IS_FINITE(d) \
136: && !JSDOUBLE_IS_NEGZERO(d) \
137: && ((d) == (i = (jsint)(d))))
138:
139: /* Initialize number constants and runtime state for the first context. */
140: extern JSBool
141: js_InitRuntimeNumberState(JSContext *cx);
142:
143: extern void
144: js_FinishRuntimeNumberState(JSContext *cx);
145:
146: /* Initialize the Number class, returning its prototype object. */
147: extern JSObject *
148: js_InitNumberClass(JSContext *cx, JSObject *obj);
149:
150: /*
151: * String constants for global function names, used in jsapi.c and jsnum.c.
152: */
153: extern const char js_Infinity_str[];
154: extern const char js_NaN_str[];
155: extern const char js_isNaN_str[];
156: extern const char js_isFinite_str[];
157: extern const char js_parseFloat_str[];
158: extern const char js_parseInt_str[];
159:
160: /* GC-allocate a new JS number. */
161: extern jsdouble *
162: js_NewDouble(JSContext *cx, jsdouble d);
163:
164: extern void
165: js_FinalizeDouble(JSContext *cx, jsdouble *dp);
166:
167: extern JSBool
168: js_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
169:
170: extern JSBool
171: js_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
172:
173: /* Construct a Number instance that wraps around d. */
174: extern JSObject *
175: js_NumberToObject(JSContext *cx, jsdouble d);
176:
177: /* Convert a number to a GC'ed string. */
178: extern JSString *
179: js_NumberToString(JSContext *cx, jsdouble d);
180:
181: /*
182: * Convert a value to a number, returning false after reporting any error,
183: * otherwise returning true with *dp set.
184: */
185: extern JSBool
186: js_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
187:
188: /*
189: * Convert a value or a double to an int32, according to the ECMA rules
190: * for ToInt32.
191: */
192: extern JSBool
193: js_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip);
194:
195: extern JSBool
196: js_DoubleToECMAInt32(JSContext *cx, jsdouble d, int32 *ip);
197:
198: /*
199: * Convert a value or a double to a uint32, according to the ECMA rules
200: * for ToUint32.
201: */
202: extern JSBool
203: js_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip);
204:
205: extern JSBool
206: js_DoubleToECMAUint32(JSContext *cx, jsdouble d, uint32 *ip);
207:
208: /*
209: * Convert a value to a number, then to an int32 if it fits by rounding to
210: * nearest; but failing with an error report if the double is out of range
211: * or unordered.
212: */
213: extern JSBool
214: js_ValueToInt32(JSContext *cx, jsval v, int32 *ip);
215:
216: /*
217: * Convert a value to a number, then to a uint16 according to the ECMA rules
218: * for ToUint16.
219: */
220: extern JSBool
221: js_ValueToUint16(JSContext *cx, jsval v, uint16 *ip);
222:
223: /*
224: * Convert a jsdouble to an integral number, stored in a jsdouble.
225: * If d is NaN, return 0. If d is an infinity, return it without conversion.
226: */
227: extern jsdouble
228: js_DoubleToInteger(jsdouble d);
229:
230: /*
231: * Similar to strtod except that it replaces overflows with infinities of the
232: * correct sign, and underflows with zeros of the correct sign. Guaranteed to
233: * return the closest double number to the given input in dp.
234: *
235: * Also allows inputs of the form [+|-]Infinity, which produce an infinity of
236: * the appropriate sign. The case of the "Infinity" string must match exactly.
237: * If the string does not contain a number, set *ep to s and return 0.0 in dp.
238: * Return false if out of memory.
239: */
240: extern JSBool
241: js_strtod(JSContext *cx, const jschar *s, const jschar **ep, jsdouble *dp);
242:
243: /*
244: * Similar to strtol except that it handles integers of arbitrary size.
245: * Guaranteed to return the closest double number to the given input when radix
246: * is 10 or a power of 2. Callers may see round-off errors for very large
247: * numbers of a different radix than 10 or a power of 2.
248: *
249: * If the string does not contain a number, set *ep to s and return 0.0 in dp.
250: * Return false if out of memory.
251: */
252: extern JSBool
253: js_strtointeger(JSContext *cx, const jschar *s, const jschar **ep, jsint radix, jsdouble *dp);
254:
255: JS_END_EXTERN_C
256:
257: #endif /* jsnum_h___ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.