PageRenderTime 35ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsnum.h

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