Save all modification
[mozilla-1.9/m8.git] / js / src / jsnum.h
blob222cb3a3269229a3411f8fff5a837f8304ddd2e6
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
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.
24 * Contributor(s):
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.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jsnum_h___
41 #define jsnum_h___
43 * JS number (IEEE double) interface.
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.
51 JS_BEGIN_EXTERN_C
54 * The ARM architecture supports two floating point models: VFP and FPA. When
55 * targetting FPA, doubles are mixed-endian on little endian ARMs (meaning that
56 * the high and low words are in big endian order).
58 #if defined(__arm) || defined(__arm32__) || defined(__arm26__) || defined(__arm__)
59 #if !defined(__VFP_FP__)
60 #define FPU_IS_ARM_FPA
61 #endif
62 #endif
64 typedef union jsdpun {
65 struct {
66 #if defined(IS_LITTLE_ENDIAN) && !defined(FPU_IS_ARM_FPA)
67 uint32 lo, hi;
68 #else
69 uint32 hi, lo;
70 #endif
71 } s;
72 jsdouble d;
73 } jsdpun;
75 #if (__GNUC__ == 2 && __GNUC_MINOR__ > 95) || __GNUC__ > 2
77 * This version of the macros is safe for the alias optimizations that gcc
78 * does, but uses gcc-specific extensions.
81 #define JSDOUBLE_HI32(x) (__extension__ ({ jsdpun u; u.d = (x); u.s.hi; }))
82 #define JSDOUBLE_LO32(x) (__extension__ ({ jsdpun u; u.d = (x); u.s.lo; }))
83 #define JSDOUBLE_SET_HI32(x, y) \
84 (__extension__ ({ jsdpun u; u.d = (x); u.s.hi = (y); (x) = u.d; }))
85 #define JSDOUBLE_SET_LO32(x, y) \
86 (__extension__ ({ jsdpun u; u.d = (x); u.s.lo = (y); (x) = u.d; }))
88 #else /* not or old GNUC */
91 * We don't know of any non-gcc compilers that perform alias optimization,
92 * so this code should work.
95 #if defined(IS_LITTLE_ENDIAN) && !defined(FPU_IS_ARM_FPA)
96 #define JSDOUBLE_HI32(x) (((uint32 *)&(x))[1])
97 #define JSDOUBLE_LO32(x) (((uint32 *)&(x))[0])
98 #else
99 #define JSDOUBLE_HI32(x) (((uint32 *)&(x))[0])
100 #define JSDOUBLE_LO32(x) (((uint32 *)&(x))[1])
101 #endif
103 #define JSDOUBLE_SET_HI32(x, y) (JSDOUBLE_HI32(x)=(y))
104 #define JSDOUBLE_SET_LO32(x, y) (JSDOUBLE_LO32(x)=(y))
106 #endif /* not or old GNUC */
108 #define JSDOUBLE_HI32_SIGNBIT 0x80000000
109 #define JSDOUBLE_HI32_EXPMASK 0x7ff00000
110 #define JSDOUBLE_HI32_MANTMASK 0x000fffff
112 #define JSDOUBLE_IS_NaN(x) \
113 ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) == JSDOUBLE_HI32_EXPMASK && \
114 (JSDOUBLE_LO32(x) || (JSDOUBLE_HI32(x) & JSDOUBLE_HI32_MANTMASK)))
116 #define JSDOUBLE_IS_INFINITE(x) \
117 ((JSDOUBLE_HI32(x) & ~JSDOUBLE_HI32_SIGNBIT) == JSDOUBLE_HI32_EXPMASK && \
118 !JSDOUBLE_LO32(x))
120 #define JSDOUBLE_IS_FINITE(x) \
121 ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) != JSDOUBLE_HI32_EXPMASK)
123 #define JSDOUBLE_IS_NEGZERO(d) (JSDOUBLE_HI32(d) == JSDOUBLE_HI32_SIGNBIT && \
124 JSDOUBLE_LO32(d) == 0)
127 * JSDOUBLE_IS_INT first checks that d is neither NaN nor infinite, to avoid
128 * raising SIGFPE on platforms such as Alpha Linux, then (only if the cast is
129 * safe) leaves i as (jsint)d. This also avoid anomalous NaN floating point
130 * comparisons under MSVC.
132 #define JSDOUBLE_IS_INT(d, i) (JSDOUBLE_IS_FINITE(d) \
133 && !JSDOUBLE_IS_NEGZERO(d) \
134 && ((d) == (i = (jsint)(d))))
136 #if defined(XP_WIN)
137 #define JSDOUBLE_COMPARE(LVAL, OP, RVAL, IFNAN) \
138 ((JSDOUBLE_IS_NaN(LVAL) || JSDOUBLE_IS_NaN(RVAL)) \
139 ? (IFNAN) \
140 : (LVAL) OP (RVAL))
141 #else
142 #define JSDOUBLE_COMPARE(LVAL, OP, RVAL, IFNAN) ((LVAL) OP (RVAL))
143 #endif
145 /* Initialize number constants and runtime state for the first context. */
146 extern JSBool
147 js_InitRuntimeNumberState(JSContext *cx);
149 extern void
150 js_FinishRuntimeNumberState(JSContext *cx);
152 /* Initialize the Number class, returning its prototype object. */
153 extern JSClass js_NumberClass;
155 extern JSObject *
156 js_InitNumberClass(JSContext *cx, JSObject *obj);
159 * String constants for global function names, used in jsapi.c and jsnum.c.
161 extern const char js_Infinity_str[];
162 extern const char js_NaN_str[];
163 extern const char js_isNaN_str[];
164 extern const char js_isFinite_str[];
165 extern const char js_parseFloat_str[];
166 extern const char js_parseInt_str[];
168 /* GC-allocate a new JS number. */
169 extern jsdouble *
170 js_NewDouble(JSContext *cx, jsdouble d, uintN gcflag);
172 extern void
173 js_FinalizeDouble(JSContext *cx, jsdouble *dp);
175 extern JSBool
176 js_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
178 extern JSBool
179 js_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
181 /* Convert a number to a GC'ed string. */
182 extern JSString *
183 js_NumberToString(JSContext *cx, jsdouble d);
186 * Convert a value to a number, returning false after reporting any error,
187 * otherwise returning true with *dp set.
189 extern JSBool
190 js_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
193 * Convert a value or a double to an int32, according to the ECMA rules
194 * for ToInt32.
196 extern JSBool
197 js_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip);
199 extern JSBool
200 js_DoubleToECMAInt32(JSContext *cx, jsdouble d, int32 *ip);
203 * Convert a value or a double to a uint32, according to the ECMA rules
204 * for ToUint32.
206 extern JSBool
207 js_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip);
209 extern JSBool
210 js_DoubleToECMAUint32(JSContext *cx, jsdouble d, uint32 *ip);
213 * Convert a value to a number, then to an int32 if it fits by rounding to
214 * nearest; but failing with an error report if the double is out of range
215 * or unordered.
217 extern JSBool
218 js_ValueToInt32(JSContext *cx, jsval v, int32 *ip);
221 * Convert a value to a number, then to a uint16 according to the ECMA rules
222 * for ToUint16.
224 extern JSBool
225 js_ValueToUint16(JSContext *cx, jsval v, uint16 *ip);
228 * Convert a jsdouble to an integral number, stored in a jsdouble.
229 * If d is NaN, return 0. If d is an infinity, return it without conversion.
231 extern jsdouble
232 js_DoubleToInteger(jsdouble d);
235 * Similar to strtod except that it replaces overflows with infinities of the
236 * correct sign, and underflows with zeros of the correct sign. Guaranteed to
237 * return the closest double number to the given input in dp.
239 * Also allows inputs of the form [+|-]Infinity, which produce an infinity of
240 * the appropriate sign. The case of the "Infinity" string must match exactly.
241 * If the string does not contain a number, set *ep to s and return 0.0 in dp.
242 * Return false if out of memory.
244 extern JSBool
245 js_strtod(JSContext *cx, const jschar *s, const jschar *send,
246 const jschar **ep, jsdouble *dp);
249 * Similar to strtol except that it handles integers of arbitrary size.
250 * Guaranteed to return the closest double number to the given input when radix
251 * is 10 or a power of 2. Callers may see round-off errors for very large
252 * numbers of a different radix than 10 or a power of 2.
254 * If the string does not contain a number, set *ep to s and return 0.0 in dp.
255 * Return false if out of memory.
257 extern JSBool
258 js_strtointeger(JSContext *cx, const jschar *s, const jschar *send,
259 const jschar **ep, jsint radix, jsdouble *dp);
261 JS_END_EXTERN_C
263 #endif /* jsnum_h___ */