1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
---|
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 | #ifndef jsnum_h___ |
---|
36 | #define jsnum_h___ |
---|
37 | /* |
---|
38 | * JS number (IEEE double) interface. |
---|
39 | * |
---|
40 | * JS numbers are optimistically stored in the top 31 bits of 32-bit integers, |
---|
41 | * but floating point literals, results that overflow 31 bits, and division and |
---|
42 | * modulus operands and results require a 64-bit IEEE double. These are GC'ed |
---|
43 | * and pointed to by 32-bit jsvals on the stack and in object properties. |
---|
44 | * |
---|
45 | * When a JS number is treated as an object (followed by . or []), the runtime |
---|
46 | * wraps it with a JSObject whose valueOf method returns the unwrapped number. |
---|
47 | */ |
---|
48 | |
---|
49 | JS_BEGIN_EXTERN_C |
---|
50 | |
---|
51 | /* |
---|
52 | * Stefan Hanske <sh990154@mail.uni-greifswald.de> reports: |
---|
53 | * ARM is a little endian architecture but 64 bit double words are stored |
---|
54 | * differently: the 32 bit words are in little endian byte order, the two words |
---|
55 | * are stored in big endian`s way. |
---|
56 | */ |
---|
57 | |
---|
58 | #if defined(__arm) || defined(__arm32__) || defined(__arm26__) || defined(__arm__) |
---|
59 | #define CPU_IS_ARM |
---|
60 | #endif |
---|
61 | |
---|
62 | #if (__GNUC__ == 2 && __GNUC_MINOR__ > 95) || __GNUC__ > 2 |
---|
63 | /* |
---|
64 | * This version of the macros is safe for the alias optimizations that gcc |
---|
65 | * does, but uses gcc-specific extensions. |
---|
66 | */ |
---|
67 | typedef union { |
---|
68 | jsdouble value; |
---|
69 | struct { |
---|
70 | #if defined(IS_LITTLE_ENDIAN) && !defined(CPU_IS_ARM) |
---|
71 | uint32 lsw; |
---|
72 | uint32 msw; |
---|
73 | #else |
---|
74 | uint32 msw; |
---|
75 | uint32 lsw; |
---|
76 | #endif |
---|
77 | } parts; |
---|
78 | } js_ieee_double_shape_type; |
---|
79 | |
---|
80 | #define JSDOUBLE_HI32(x) (__extension__ ({ js_ieee_double_shape_type sh_u; \ |
---|
81 | sh_u.value = (x); \ |
---|
82 | sh_u.parts.msw; })) |
---|
83 | #define JSDOUBLE_LO32(x) (__extension__ ({ js_ieee_double_shape_type sh_u; \ |
---|
84 | sh_u.value = (x); \ |
---|
85 | sh_u.parts.lsw; })) |
---|
86 | #define JSDOUBLE_SET_HI32(x, y) (__extension__ ({ \ |
---|
87 | js_ieee_double_shape_type sh_u; \ |
---|
88 | sh_u.value = (x); \ |
---|
89 | sh_u.parts.msw = (y); \ |
---|
90 | (x) = sh_u.value; })) |
---|
91 | #define JSDOUBLE_SET_LO32(x, y) (__extension__ ({ \ |
---|
92 | js_ieee_double_shape_type sh_u; \ |
---|
93 | sh_u.value = (x); \ |
---|
94 | sh_u.parts.lsw = (y); \ |
---|
95 | (x) = sh_u.value; })) |
---|
96 | |
---|
97 | #else /* GNUC */ |
---|
98 | |
---|
99 | /* |
---|
100 | * We don't know of any non-gcc compilers that perform alias optimization, |
---|
101 | * so this code should work. |
---|
102 | */ |
---|
103 | |
---|
104 | #if defined(IS_LITTLE_ENDIAN) && !defined(CPU_IS_ARM) |
---|
105 | #define JSDOUBLE_HI32(x) (((uint32 *)&(x))[1]) |
---|
106 | #define JSDOUBLE_LO32(x) (((uint32 *)&(x))[0]) |
---|
107 | #else |
---|
108 | #define JSDOUBLE_HI32(x) (((uint32 *)&(x))[0]) |
---|
109 | #define JSDOUBLE_LO32(x) (((uint32 *)&(x))[1]) |
---|
110 | #endif |
---|
111 | |
---|
112 | #define JSDOUBLE_SET_HI32(x, y) (JSDOUBLE_HI32(x)=(y)) |
---|
113 | #define JSDOUBLE_SET_LO32(x, y) (JSDOUBLE_LO32(x)=(y)) |
---|
114 | |
---|
115 | #endif /* GNUC */ |
---|
116 | |
---|
117 | #define JSDOUBLE_HI32_SIGNBIT 0x80000000 |
---|
118 | #define JSDOUBLE_HI32_EXPMASK 0x7ff00000 |
---|
119 | #define JSDOUBLE_HI32_MANTMASK 0x000fffff |
---|
120 | |
---|
121 | #define JSDOUBLE_IS_NaN(x) \ |
---|
122 | ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) == JSDOUBLE_HI32_EXPMASK && \ |
---|
123 | (JSDOUBLE_LO32(x) || (JSDOUBLE_HI32(x) & JSDOUBLE_HI32_MANTMASK))) |
---|
124 | |
---|
125 | #define JSDOUBLE_IS_INFINITE(x) \ |
---|
126 | ((JSDOUBLE_HI32(x) & ~JSDOUBLE_HI32_SIGNBIT) == JSDOUBLE_HI32_EXPMASK && \ |
---|
127 | !JSDOUBLE_LO32(x)) |
---|
128 | |
---|
129 | #define JSDOUBLE_IS_FINITE(x) \ |
---|
130 | ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) != JSDOUBLE_HI32_EXPMASK) |
---|
131 | |
---|
132 | #define JSDOUBLE_IS_NEGZERO(d) (JSDOUBLE_HI32(d) == JSDOUBLE_HI32_SIGNBIT && \ |
---|
133 | JSDOUBLE_LO32(d) == 0) |
---|
134 | |
---|
135 | /* |
---|
136 | * JSDOUBLE_IS_INT first checks that d is neither NaN nor infinite, to avoid |
---|
137 | * raising SIGFPE on platforms such as Alpha Linux, then (only if the cast is |
---|
138 | * safe) leaves i as (jsint)d. This also avoid anomalous NaN floating point |
---|
139 | * comparisons under MSVC. |
---|
140 | */ |
---|
141 | #define JSDOUBLE_IS_INT(d, i) (JSDOUBLE_IS_FINITE(d) \ |
---|
142 | && !JSDOUBLE_IS_NEGZERO(d) \ |
---|
143 | && ((d) == (i = (jsint)(d)))) |
---|
144 | |
---|
145 | /* Initialize number constants and runtime state for the first context. */ |
---|
146 | extern JSBool |
---|
147 | js_InitRuntimeNumberState(JSContext *cx); |
---|
148 | |
---|
149 | extern void |
---|
150 | js_FinishRuntimeNumberState(JSContext *cx); |
---|
151 | |
---|
152 | /* Initialize the Number class, returning its prototype object. */ |
---|
153 | extern JSObject * |
---|
154 | js_InitNumberClass(JSContext *cx, JSObject *obj); |
---|
155 | |
---|
156 | /* |
---|
157 | * String constants for global function names, used in jsapi.c and jsnum.c. |
---|
158 | */ |
---|
159 | extern const char js_Infinity_str[]; |
---|
160 | extern const char js_NaN_str[]; |
---|
161 | extern const char js_isNaN_str[]; |
---|
162 | extern const char js_isFinite_str[]; |
---|
163 | extern const char js_parseFloat_str[]; |
---|
164 | extern const char js_parseInt_str[]; |
---|
165 | |
---|
166 | /* GC-allocate a new JS number. */ |
---|
167 | extern jsdouble * |
---|
168 | js_NewDouble(JSContext *cx, jsdouble d); |
---|
169 | |
---|
170 | extern void |
---|
171 | js_FinalizeDouble(JSContext *cx, jsdouble *dp); |
---|
172 | |
---|
173 | extern JSBool |
---|
174 | js_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval); |
---|
175 | |
---|
176 | extern JSBool |
---|
177 | js_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval); |
---|
178 | |
---|
179 | /* Construct a Number instance that wraps around d. */ |
---|
180 | extern JSObject * |
---|
181 | js_NumberToObject(JSContext *cx, jsdouble d); |
---|
182 | |
---|
183 | /* Convert a number to a GC'ed string. */ |
---|
184 | extern JSString * |
---|
185 | js_NumberToString(JSContext *cx, jsdouble d); |
---|
186 | |
---|
187 | /* |
---|
188 | * Convert a value to a number, returning false after reporting any error, |
---|
189 | * otherwise returning true with *dp set. |
---|
190 | */ |
---|
191 | extern JSBool |
---|
192 | js_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp); |
---|
193 | |
---|
194 | /* |
---|
195 | * Convert a value or a double to an int32, according to the ECMA rules |
---|
196 | * for ToInt32. |
---|
197 | */ |
---|
198 | extern JSBool |
---|
199 | js_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip); |
---|
200 | |
---|
201 | extern JSBool |
---|
202 | js_DoubleToECMAInt32(JSContext *cx, jsdouble d, int32 *ip); |
---|
203 | |
---|
204 | /* |
---|
205 | * Convert a value or a double to a uint32, according to the ECMA rules |
---|
206 | * for ToUint32. |
---|
207 | */ |
---|
208 | extern JSBool |
---|
209 | js_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip); |
---|
210 | |
---|
211 | extern JSBool |
---|
212 | js_DoubleToECMAUint32(JSContext *cx, jsdouble d, uint32 *ip); |
---|
213 | |
---|
214 | /* |
---|
215 | * Convert a value to a number, then to an int32 if it fits by rounding to |
---|
216 | * nearest; but failing with an error report if the double is out of range |
---|
217 | * or unordered. |
---|
218 | */ |
---|
219 | extern JSBool |
---|
220 | js_ValueToInt32(JSContext *cx, jsval v, int32 *ip); |
---|
221 | |
---|
222 | /* |
---|
223 | * Convert a value to a number, then to a uint16 according to the ECMA rules |
---|
224 | * for ToUint16. |
---|
225 | */ |
---|
226 | extern JSBool |
---|
227 | js_ValueToUint16(JSContext *cx, jsval v, uint16 *ip); |
---|
228 | |
---|
229 | /* |
---|
230 | * Convert a jsdouble to an integral number, stored in a jsdouble. |
---|
231 | * If d is NaN, return 0. If d is an infinity, return it without conversion. |
---|
232 | */ |
---|
233 | extern jsdouble |
---|
234 | js_DoubleToInteger(jsdouble d); |
---|
235 | |
---|
236 | /* |
---|
237 | * Similar to strtod except that replaces overflows with infinities of the correct |
---|
238 | * sign and underflows with zeros of the correct sign. Guaranteed to return the |
---|
239 | * closest double number to the given input in dp. |
---|
240 | * Also allows inputs of the form [+|-]Infinity, which produce an infinity of the |
---|
241 | * appropriate sign. The case of the "Infinity" string must match. |
---|
242 | * If the string does not have a number in it, set *ep to s and return 0.0 in dp. |
---|
243 | * Return false if out of memory. |
---|
244 | */ |
---|
245 | extern JSBool |
---|
246 | js_strtod(JSContext *cx, const jschar *s, const jschar **ep, jsdouble *dp); |
---|
247 | |
---|
248 | /* |
---|
249 | * Similar to strtol except that handles integers of arbitrary size. Guaranteed to |
---|
250 | * return the closest double number to the given input when radix is 10 or a power of 2. |
---|
251 | * May experience roundoff errors for very large numbers of a different radix. |
---|
252 | * If the string does not have a number in it, set *ep to s and return 0.0 in dp. |
---|
253 | * Return false if out of memory. |
---|
254 | */ |
---|
255 | extern JSBool |
---|
256 | js_strtointeger(JSContext *cx, const jschar *s, const jschar **ep, jsint radix, jsdouble *dp); |
---|
257 | |
---|
258 | JS_END_EXTERN_C |
---|
259 | |
---|
260 | #endif /* jsnum_h___ */ |
---|