Back to index

lightning-sunbird  0.9+nobinonly
jsnum.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef jsnum_h___
00041 #define jsnum_h___
00042 /*
00043  * JS number (IEEE double) interface.
00044  *
00045  * JS numbers are optimistically stored in the top 31 bits of 32-bit integers,
00046  * but floating point literals, results that overflow 31 bits, and division and
00047  * modulus operands and results require a 64-bit IEEE double.  These are GC'ed
00048  * and pointed to by 32-bit jsvals on the stack and in object properties.
00049  *
00050  * When a JS number is treated as an object (followed by . or []), the runtime
00051  * wraps it with a JSObject whose valueOf method returns the unwrapped number.
00052  */
00053 
00054 JS_BEGIN_EXTERN_C
00055 
00056 /*
00057  * Stefan Hanske <sh990154@mail.uni-greifswald.de> reports:
00058  *  ARM is a little endian architecture but 64 bit double words are stored
00059  * differently: the 32 bit words are in little endian byte order, the two words
00060  * are stored in big endian`s way.
00061  */
00062 
00063 #if defined(__arm) || defined(__arm32__) || defined(__arm26__) || defined(__arm__)
00064 #ifndef __QNXNTO__
00065 #define CPU_IS_ARM
00066 #endif
00067 #endif
00068 
00069 typedef union jsdpun {
00070     struct {
00071 #if defined(IS_LITTLE_ENDIAN) && !defined(CPU_IS_ARM)
00072         uint32 lo, hi;
00073 #else
00074         uint32 hi, lo;
00075 #endif
00076     } s;
00077     jsdouble d;
00078 } jsdpun;
00079 
00080 #if (__GNUC__ == 2 && __GNUC_MINOR__ > 95) || __GNUC__ > 2
00081 /*
00082  * This version of the macros is safe for the alias optimizations that gcc
00083  * does, but uses gcc-specific extensions.
00084  */
00085 
00086 #define JSDOUBLE_HI32(x) (__extension__ ({ jsdpun u; u.d = (x); u.s.hi; }))
00087 #define JSDOUBLE_LO32(x) (__extension__ ({ jsdpun u; u.d = (x); u.s.lo; }))
00088 #define JSDOUBLE_SET_HI32(x, y) \
00089     (__extension__ ({ jsdpun u; u.d = (x); u.s.hi = (y); (x) = u.d; }))
00090 #define JSDOUBLE_SET_LO32(x, y) \
00091     (__extension__ ({ jsdpun u; u.d = (x); u.s.lo = (y); (x) = u.d; }))
00092 
00093 #else /* not or old GNUC */
00094 
00095 /*
00096  * We don't know of any non-gcc compilers that perform alias optimization,
00097  * so this code should work.
00098  */
00099 
00100 #if defined(IS_LITTLE_ENDIAN) && !defined(CPU_IS_ARM)
00101 #define JSDOUBLE_HI32(x)        (((uint32 *)&(x))[1])
00102 #define JSDOUBLE_LO32(x)        (((uint32 *)&(x))[0])
00103 #else
00104 #define JSDOUBLE_HI32(x)        (((uint32 *)&(x))[0])
00105 #define JSDOUBLE_LO32(x)        (((uint32 *)&(x))[1])
00106 #endif
00107 
00108 #define JSDOUBLE_SET_HI32(x, y) (JSDOUBLE_HI32(x)=(y))
00109 #define JSDOUBLE_SET_LO32(x, y) (JSDOUBLE_LO32(x)=(y))
00110 
00111 #endif /* not or old GNUC */
00112 
00113 #define JSDOUBLE_HI32_SIGNBIT   0x80000000
00114 #define JSDOUBLE_HI32_EXPMASK   0x7ff00000
00115 #define JSDOUBLE_HI32_MANTMASK  0x000fffff
00116 
00117 #define JSDOUBLE_IS_NaN(x)                                                    \
00118     ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) == JSDOUBLE_HI32_EXPMASK &&   \
00119      (JSDOUBLE_LO32(x) || (JSDOUBLE_HI32(x) & JSDOUBLE_HI32_MANTMASK)))
00120 
00121 #define JSDOUBLE_IS_INFINITE(x)                                               \
00122     ((JSDOUBLE_HI32(x) & ~JSDOUBLE_HI32_SIGNBIT) == JSDOUBLE_HI32_EXPMASK &&  \
00123      !JSDOUBLE_LO32(x))
00124 
00125 #define JSDOUBLE_IS_FINITE(x)                                                 \
00126     ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) != JSDOUBLE_HI32_EXPMASK)
00127 
00128 #define JSDOUBLE_IS_NEGZERO(d)  (JSDOUBLE_HI32(d) == JSDOUBLE_HI32_SIGNBIT && \
00129                                  JSDOUBLE_LO32(d) == 0)
00130 
00131 /*
00132  * JSDOUBLE_IS_INT first checks that d is neither NaN nor infinite, to avoid
00133  * raising SIGFPE on platforms such as Alpha Linux, then (only if the cast is
00134  * safe) leaves i as (jsint)d.  This also avoid anomalous NaN floating point
00135  * comparisons under MSVC.
00136  */
00137 #define JSDOUBLE_IS_INT(d, i) (JSDOUBLE_IS_FINITE(d)                          \
00138                                && !JSDOUBLE_IS_NEGZERO(d)                     \
00139                                && ((d) == (i = (jsint)(d))))
00140 
00141 #if defined(XP_WIN)
00142 #define JSDOUBLE_COMPARE(LVAL, OP, RVAL, IFNAN)                               \
00143     ((JSDOUBLE_IS_NaN(LVAL) || JSDOUBLE_IS_NaN(RVAL))                         \
00144      ? (IFNAN)                                                                \
00145      : (LVAL) OP (RVAL))
00146 #else
00147 #define JSDOUBLE_COMPARE(LVAL, OP, RVAL, IFNAN) ((LVAL) OP (RVAL))
00148 #endif
00149 
00150 /* Initialize number constants and runtime state for the first context. */
00151 extern JSBool
00152 js_InitRuntimeNumberState(JSContext *cx);
00153 
00154 extern void
00155 js_FinishRuntimeNumberState(JSContext *cx);
00156 
00157 /* Initialize the Number class, returning its prototype object. */
00158 extern JSClass js_NumberClass;
00159 
00160 extern JSObject *
00161 js_InitNumberClass(JSContext *cx, JSObject *obj);
00162 
00163 /*
00164  * String constants for global function names, used in jsapi.c and jsnum.c.
00165  */
00166 extern const char js_Infinity_str[];
00167 extern const char js_NaN_str[];
00168 extern const char js_isNaN_str[];
00169 extern const char js_isFinite_str[];
00170 extern const char js_parseFloat_str[];
00171 extern const char js_parseInt_str[];
00172 
00173 /* GC-allocate a new JS number. */
00174 extern jsdouble *
00175 js_NewDouble(JSContext *cx, jsdouble d, uintN gcflag);
00176 
00177 extern void
00178 js_FinalizeDouble(JSContext *cx, jsdouble *dp);
00179 
00180 extern JSBool
00181 js_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
00182 
00183 extern JSBool
00184 js_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
00185 
00186 /* Construct a Number instance that wraps around d. */
00187 extern JSObject *
00188 js_NumberToObject(JSContext *cx, jsdouble d);
00189 
00190 /* Convert a number to a GC'ed string. */
00191 extern JSString *
00192 js_NumberToString(JSContext *cx, jsdouble d);
00193 
00194 /*
00195  * Convert a value to a number, returning false after reporting any error,
00196  * otherwise returning true with *dp set.
00197  */
00198 extern JSBool
00199 js_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
00200 
00201 /*
00202  * Convert a value or a double to an int32, according to the ECMA rules
00203  * for ToInt32.
00204  */
00205 extern JSBool
00206 js_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip);
00207 
00208 extern JSBool
00209 js_DoubleToECMAInt32(JSContext *cx, jsdouble d, int32 *ip);
00210 
00211 /*
00212  * Convert a value or a double to a uint32, according to the ECMA rules
00213  * for ToUint32.
00214  */
00215 extern JSBool
00216 js_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip);
00217 
00218 extern JSBool
00219 js_DoubleToECMAUint32(JSContext *cx, jsdouble d, uint32 *ip);
00220 
00221 /*
00222  * Convert a value to a number, then to an int32 if it fits by rounding to
00223  * nearest; but failing with an error report if the double is out of range
00224  * or unordered.
00225  */
00226 extern JSBool
00227 js_ValueToInt32(JSContext *cx, jsval v, int32 *ip);
00228 
00229 /*
00230  * Convert a value to a number, then to a uint16 according to the ECMA rules
00231  * for ToUint16.
00232  */
00233 extern JSBool
00234 js_ValueToUint16(JSContext *cx, jsval v, uint16 *ip);
00235 
00236 /*
00237  * Convert a jsdouble to an integral number, stored in a jsdouble.
00238  * If d is NaN, return 0.  If d is an infinity, return it without conversion.
00239  */
00240 extern jsdouble
00241 js_DoubleToInteger(jsdouble d);
00242 
00243 /*
00244  * Similar to strtod except that it replaces overflows with infinities of the
00245  * correct sign, and underflows with zeros of the correct sign.  Guaranteed to
00246  * return the closest double number to the given input in dp.
00247  *
00248  * Also allows inputs of the form [+|-]Infinity, which produce an infinity of
00249  * the appropriate sign.  The case of the "Infinity" string must match exactly.
00250  * If the string does not contain a number, set *ep to s and return 0.0 in dp.
00251  * Return false if out of memory.
00252  */
00253 extern JSBool
00254 js_strtod(JSContext *cx, const jschar *s, const jschar **ep, jsdouble *dp);
00255 
00256 /*
00257  * Similar to strtol except that it handles integers of arbitrary size.
00258  * Guaranteed to return the closest double number to the given input when radix
00259  * is 10 or a power of 2.  Callers may see round-off errors for very large
00260  * numbers of a different radix than 10 or a power of 2.
00261  *
00262  * If the string does not contain a number, set *ep to s and return 0.0 in dp.
00263  * Return false if out of memory.
00264  */
00265 extern JSBool
00266 js_strtointeger(JSContext *cx, const jschar *s, const jschar **ep, jsint radix, jsdouble *dp);
00267 
00268 JS_END_EXTERN_C
00269 
00270 #endif /* jsnum_h___ */