1 |
siliconforks |
332 |
/* -*- 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 |
siliconforks |
399 |
|
43 |
siliconforks |
332 |
/* |
44 |
|
|
* JS number (IEEE double) interface. |
45 |
|
|
* |
46 |
|
|
* JS numbers are optimistically stored in the top 31 bits of 32-bit integers, |
47 |
|
|
* but floating point literals, results that overflow 31 bits, and division and |
48 |
|
|
* modulus operands and results require a 64-bit IEEE double. These are GC'ed |
49 |
|
|
* and pointed to by 32-bit jsvals on the stack and in object properties. |
50 |
|
|
*/ |
51 |
|
|
|
52 |
|
|
JS_BEGIN_EXTERN_C |
53 |
|
|
|
54 |
|
|
/* |
55 |
|
|
* The ARM architecture supports two floating point models: VFP and FPA. When |
56 |
|
|
* targetting FPA, doubles are mixed-endian on little endian ARMs (meaning that |
57 |
|
|
* the high and low words are in big endian order). |
58 |
|
|
*/ |
59 |
|
|
#if defined(__arm) || defined(__arm32__) || defined(__arm26__) || defined(__arm__) |
60 |
|
|
#if !defined(__VFP_FP__) |
61 |
|
|
#define FPU_IS_ARM_FPA |
62 |
|
|
#endif |
63 |
|
|
#endif |
64 |
|
|
|
65 |
|
|
typedef union jsdpun { |
66 |
|
|
struct { |
67 |
|
|
#if defined(IS_LITTLE_ENDIAN) && !defined(FPU_IS_ARM_FPA) |
68 |
|
|
uint32 lo, hi; |
69 |
|
|
#else |
70 |
|
|
uint32 hi, lo; |
71 |
|
|
#endif |
72 |
|
|
} s; |
73 |
|
|
uint64 u64; |
74 |
|
|
jsdouble d; |
75 |
|
|
} jsdpun; |
76 |
|
|
|
77 |
|
|
#if (__GNUC__ == 2 && __GNUC_MINOR__ > 95) || __GNUC__ > 2 |
78 |
|
|
/* |
79 |
|
|
* This version of the macros is safe for the alias optimizations that gcc |
80 |
|
|
* does, but uses gcc-specific extensions. |
81 |
|
|
*/ |
82 |
|
|
|
83 |
|
|
#define JSDOUBLE_HI32(x) (__extension__ ({ jsdpun u; u.d = (x); u.s.hi; })) |
84 |
|
|
#define JSDOUBLE_LO32(x) (__extension__ ({ jsdpun u; u.d = (x); u.s.lo; })) |
85 |
|
|
#define JSDOUBLE_SET_HI32(x, y) \ |
86 |
|
|
(__extension__ ({ jsdpun u; u.d = (x); u.s.hi = (y); (x) = u.d; })) |
87 |
|
|
#define JSDOUBLE_SET_LO32(x, y) \ |
88 |
|
|
(__extension__ ({ jsdpun u; u.d = (x); u.s.lo = (y); (x) = u.d; })) |
89 |
|
|
|
90 |
|
|
#else /* not or old GNUC */ |
91 |
|
|
|
92 |
|
|
/* |
93 |
|
|
* We don't know of any non-gcc compilers that perform alias optimization, |
94 |
|
|
* so this code should work. |
95 |
|
|
*/ |
96 |
|
|
|
97 |
|
|
#if defined(IS_LITTLE_ENDIAN) && !defined(FPU_IS_ARM_FPA) |
98 |
|
|
#define JSDOUBLE_HI32(x) (((uint32 *)&(x))[1]) |
99 |
|
|
#define JSDOUBLE_LO32(x) (((uint32 *)&(x))[0]) |
100 |
|
|
#else |
101 |
|
|
#define JSDOUBLE_HI32(x) (((uint32 *)&(x))[0]) |
102 |
|
|
#define JSDOUBLE_LO32(x) (((uint32 *)&(x))[1]) |
103 |
|
|
#endif |
104 |
|
|
|
105 |
|
|
#define JSDOUBLE_SET_HI32(x, y) (JSDOUBLE_HI32(x)=(y)) |
106 |
|
|
#define JSDOUBLE_SET_LO32(x, y) (JSDOUBLE_LO32(x)=(y)) |
107 |
|
|
|
108 |
|
|
#endif /* not or old GNUC */ |
109 |
|
|
|
110 |
|
|
#define JSDOUBLE_HI32_SIGNBIT 0x80000000 |
111 |
|
|
#define JSDOUBLE_HI32_EXPMASK 0x7ff00000 |
112 |
|
|
#define JSDOUBLE_HI32_MANTMASK 0x000fffff |
113 |
|
|
|
114 |
|
|
#define JSDOUBLE_IS_NaN(x) \ |
115 |
|
|
((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) == JSDOUBLE_HI32_EXPMASK && \ |
116 |
|
|
(JSDOUBLE_LO32(x) || (JSDOUBLE_HI32(x) & JSDOUBLE_HI32_MANTMASK))) |
117 |
|
|
|
118 |
|
|
#define JSDOUBLE_IS_INFINITE(x) \ |
119 |
|
|
((JSDOUBLE_HI32(x) & ~JSDOUBLE_HI32_SIGNBIT) == JSDOUBLE_HI32_EXPMASK && \ |
120 |
|
|
!JSDOUBLE_LO32(x)) |
121 |
|
|
|
122 |
|
|
#define JSDOUBLE_IS_FINITE(x) \ |
123 |
|
|
((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) != JSDOUBLE_HI32_EXPMASK) |
124 |
|
|
|
125 |
|
|
#define JSDOUBLE_IS_NEGZERO(d) (JSDOUBLE_HI32(d) == JSDOUBLE_HI32_SIGNBIT && \ |
126 |
|
|
JSDOUBLE_LO32(d) == 0) |
127 |
|
|
|
128 |
|
|
/* |
129 |
|
|
* JSDOUBLE_IS_INT first checks that d is neither NaN nor infinite, to avoid |
130 |
|
|
* raising SIGFPE on platforms such as Alpha Linux, then (only if the cast is |
131 |
|
|
* safe) leaves i as (jsint)d. This also avoid anomalous NaN floating point |
132 |
|
|
* comparisons under MSVC. |
133 |
|
|
*/ |
134 |
|
|
#define JSDOUBLE_IS_INT(d, i) (JSDOUBLE_IS_FINITE(d) \ |
135 |
|
|
&& !JSDOUBLE_IS_NEGZERO(d) \ |
136 |
|
|
&& ((d) == (i = (jsint)(d)))) |
137 |
|
|
|
138 |
|
|
#if defined(XP_WIN) |
139 |
|
|
#define JSDOUBLE_COMPARE(LVAL, OP, RVAL, IFNAN) \ |
140 |
|
|
((JSDOUBLE_IS_NaN(LVAL) || JSDOUBLE_IS_NaN(RVAL)) \ |
141 |
|
|
? (IFNAN) \ |
142 |
|
|
: (LVAL) OP (RVAL)) |
143 |
|
|
#else |
144 |
|
|
#define JSDOUBLE_COMPARE(LVAL, OP, RVAL, IFNAN) ((LVAL) OP (RVAL)) |
145 |
|
|
#endif |
146 |
|
|
|
147 |
|
|
extern jsdouble js_NaN; |
148 |
|
|
|
149 |
|
|
/* Initialize number constants and runtime state for the first context. */ |
150 |
|
|
extern JSBool |
151 |
|
|
js_InitRuntimeNumberState(JSContext *cx); |
152 |
|
|
|
153 |
|
|
extern void |
154 |
|
|
js_TraceRuntimeNumberState(JSTracer *trc); |
155 |
|
|
|
156 |
|
|
extern void |
157 |
|
|
js_FinishRuntimeNumberState(JSContext *cx); |
158 |
|
|
|
159 |
|
|
/* Initialize the Number class, returning its prototype object. */ |
160 |
|
|
extern JSClass js_NumberClass; |
161 |
|
|
|
162 |
|
|
extern JSObject * |
163 |
|
|
js_InitNumberClass(JSContext *cx, JSObject *obj); |
164 |
|
|
|
165 |
|
|
/* |
166 |
|
|
* String constants for global function names, used in jsapi.c and jsnum.c. |
167 |
|
|
*/ |
168 |
|
|
extern const char js_Infinity_str[]; |
169 |
|
|
extern const char js_NaN_str[]; |
170 |
|
|
extern const char js_isNaN_str[]; |
171 |
|
|
extern const char js_isFinite_str[]; |
172 |
|
|
extern const char js_parseFloat_str[]; |
173 |
|
|
extern const char js_parseInt_str[]; |
174 |
|
|
|
175 |
|
|
/* |
176 |
|
|
* vp must be a root. |
177 |
|
|
*/ |
178 |
|
|
extern JSBool |
179 |
|
|
js_NewNumberInRootedValue(JSContext *cx, jsdouble d, jsval *vp); |
180 |
|
|
|
181 |
siliconforks |
460 |
/* |
182 |
|
|
* Create a weakly rooted integer or double jsval as appropriate for the given |
183 |
|
|
* jsdouble. |
184 |
|
|
*/ |
185 |
|
|
extern JSBool |
186 |
|
|
js_NewWeaklyRootedNumber(JSContext *cx, jsdouble d, jsval *vp); |
187 |
|
|
|
188 |
siliconforks |
332 |
/* Convert a number to a GC'ed string. */ |
189 |
|
|
extern JSString * JS_FASTCALL |
190 |
|
|
js_NumberToString(JSContext *cx, jsdouble d); |
191 |
|
|
|
192 |
|
|
/* |
193 |
|
|
* Convert a value to a number. On exit JSVAL_IS_NULL(*vp) iff there was an |
194 |
|
|
* error. If on exit JSVAL_IS_NUMBER(*vp), then *vp holds the jsval that |
195 |
|
|
* matches the result. Otherwise *vp is JSVAL_TRUE indicating that the jsval |
196 |
|
|
* for result has to be created explicitly using, for example, the |
197 |
|
|
* js_NewNumberInRootedValue function. |
198 |
|
|
*/ |
199 |
|
|
extern jsdouble |
200 |
|
|
js_ValueToNumber(JSContext *cx, jsval* vp); |
201 |
|
|
|
202 |
|
|
/* |
203 |
|
|
* Convert a value to an int32 or uint32, according to the ECMA rules for |
204 |
|
|
* ToInt32 and ToUint32. On exit JSVAL_IS_NULL(*vp) iff there was an error. If |
205 |
|
|
* on exit JSVAL_IS_INT(*vp), then *vp holds the jsval matching the result. |
206 |
|
|
* Otherwise *vp is JSVAL_TRUE indicating that the jsval for result has to be |
207 |
|
|
* created explicitly using, for example, the js_NewNumberInRootedValue |
208 |
|
|
* function. |
209 |
|
|
*/ |
210 |
|
|
extern int32 |
211 |
|
|
js_ValueToECMAInt32(JSContext *cx, jsval *vp); |
212 |
|
|
|
213 |
|
|
extern uint32 |
214 |
|
|
js_ValueToECMAUint32(JSContext *cx, jsval *vp); |
215 |
|
|
|
216 |
|
|
/* |
217 |
|
|
* Specialized ToInt32 and ToUint32 converters for doubles. |
218 |
|
|
*/ |
219 |
|
|
extern int32 |
220 |
|
|
js_DoubleToECMAInt32(jsdouble d); |
221 |
|
|
|
222 |
|
|
extern uint32 |
223 |
|
|
js_DoubleToECMAUint32(jsdouble d); |
224 |
|
|
|
225 |
|
|
/* |
226 |
|
|
* Convert a value to a number, then to an int32 if it fits by rounding to |
227 |
|
|
* nearest; but failing with an error report if the double is out of range |
228 |
|
|
* or unordered. On exit JSVAL_IS_NULL(*vp) iff there was an error. If on exit |
229 |
|
|
* JSVAL_IS_INT(*vp), then *vp holds the jsval matching the result. Otherwise |
230 |
|
|
* *vp is JSVAL_TRUE indicating that the jsval for result has to be created |
231 |
|
|
* explicitly using, for example, the js_NewNumberInRootedValue function. |
232 |
|
|
*/ |
233 |
|
|
extern int32 |
234 |
|
|
js_ValueToInt32(JSContext *cx, jsval *vp); |
235 |
|
|
|
236 |
|
|
/* |
237 |
|
|
* Convert a value to a number, then to a uint16 according to the ECMA rules |
238 |
|
|
* for ToUint16. On exit JSVAL_IS_NULL(*vp) iff there was an error, otherwise |
239 |
|
|
* vp is jsval matching the result. |
240 |
|
|
*/ |
241 |
|
|
extern uint16 |
242 |
|
|
js_ValueToUint16(JSContext *cx, jsval *vp); |
243 |
|
|
|
244 |
|
|
/* |
245 |
|
|
* Convert a jsdouble to an integral number, stored in a jsdouble. |
246 |
|
|
* If d is NaN, return 0. If d is an infinity, return it without conversion. |
247 |
|
|
*/ |
248 |
|
|
extern jsdouble |
249 |
|
|
js_DoubleToInteger(jsdouble d); |
250 |
|
|
|
251 |
|
|
/* |
252 |
|
|
* Similar to strtod except that it replaces overflows with infinities of the |
253 |
|
|
* correct sign, and underflows with zeros of the correct sign. Guaranteed to |
254 |
|
|
* return the closest double number to the given input in dp. |
255 |
|
|
* |
256 |
|
|
* Also allows inputs of the form [+|-]Infinity, which produce an infinity of |
257 |
|
|
* the appropriate sign. The case of the "Infinity" string must match exactly. |
258 |
|
|
* If the string does not contain a number, set *ep to s and return 0.0 in dp. |
259 |
|
|
* Return false if out of memory. |
260 |
|
|
*/ |
261 |
|
|
extern JSBool |
262 |
|
|
js_strtod(JSContext *cx, const jschar *s, const jschar *send, |
263 |
|
|
const jschar **ep, jsdouble *dp); |
264 |
|
|
|
265 |
|
|
/* |
266 |
|
|
* Similar to strtol except that it handles integers of arbitrary size. |
267 |
|
|
* Guaranteed to return the closest double number to the given input when radix |
268 |
|
|
* is 10 or a power of 2. Callers may see round-off errors for very large |
269 |
|
|
* numbers of a different radix than 10 or a power of 2. |
270 |
|
|
* |
271 |
|
|
* If the string does not contain a number, set *ep to s and return 0.0 in dp. |
272 |
|
|
* Return false if out of memory. |
273 |
|
|
*/ |
274 |
|
|
extern JSBool |
275 |
|
|
js_strtointeger(JSContext *cx, const jschar *s, const jschar *send, |
276 |
|
|
const jschar **ep, jsint radix, jsdouble *dp); |
277 |
|
|
|
278 |
|
|
JS_END_EXTERN_C |
279 |
|
|
|
280 |
|
|
#endif /* jsnum_h___ */ |