home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jsnum.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.3 KB  |  128 lines

  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 License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef jsnum_h___
  20. #define jsnum_h___
  21. /*
  22.  * JS number (IEEE double) interface.
  23.  *
  24.  * JS numbers are optimistically stored in the top 31 bits of 32-bit integers,
  25.  * but floating point literals, results that overflow 31 bits, and division and
  26.  * modulus operands and results require a 64-bit IEEE double.  These are GC'ed
  27.  * and pointed to by 32-bit jsvals on the stack and in object properties.
  28.  *
  29.  * When a JS number is treated as an object (followed by . or []), the runtime
  30.  * wraps it with a JSObject whose valueOf method returns the unwrapped number.
  31.  */
  32.  
  33. PR_BEGIN_EXTERN_C
  34.  
  35. #ifdef IS_LITTLE_ENDIAN
  36. #define JSDOUBLE_HI32(x)        (((uint32 *)&(x))[1])
  37. #define JSDOUBLE_LO32(x)        (((uint32 *)&(x))[0])
  38. #else
  39. #define JSDOUBLE_HI32(x)        (((uint32 *)&(x))[0])
  40. #define JSDOUBLE_LO32(x)        (((uint32 *)&(x))[1])
  41. #endif
  42. #define JSDOUBLE_HI32_SIGNBIT   0x80000000
  43. #define JSDOUBLE_HI32_EXPMASK   0x7ff00000
  44. #define JSDOUBLE_HI32_MANTMASK  0x000fffff
  45.  
  46. #define JSDOUBLE_IS_NaN(x)                                                    \
  47.     ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) == JSDOUBLE_HI32_EXPMASK &&   \
  48.      (JSDOUBLE_LO32(x) || (JSDOUBLE_HI32(x) & JSDOUBLE_HI32_MANTMASK)))
  49.  
  50. #define JSDOUBLE_IS_INFINITE(x)                                               \
  51.     ((JSDOUBLE_HI32(x) & ~JSDOUBLE_HI32_SIGNBIT) == JSDOUBLE_HI32_EXPMASK &&   \
  52.      !JSDOUBLE_LO32(x))
  53.  
  54. #define JSDOUBLE_IS_FINITE(x)                                                 \
  55.     ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) != JSDOUBLE_HI32_EXPMASK)
  56.  
  57. #define JSDOUBLE_IS_NEGZERO(d)  (JSDOUBLE_HI32(d) == JSDOUBLE_HI32_SIGNBIT && \
  58.                  JSDOUBLE_LO32(d) == 0)
  59.  
  60. #define JSDOUBLE_IS_INT_2(d, i)    (!JSDOUBLE_IS_NEGZERO(d) && (jsdouble)i == d)
  61.  
  62. #ifdef XP_PC
  63. /* XXX MSVC miscompiles NaN floating point comparisons for ==, !=, <, and <= */
  64. #define JSDOUBLE_IS_INT(d, i)    (!JSDOUBLE_IS_NaN(d) && JSDOUBLE_IS_INT_2(d, i))
  65. #else
  66. #define JSDOUBLE_IS_INT(d, i)    JSDOUBLE_IS_INT_2(d, i)
  67. #endif
  68.  
  69. /* Initialize the Number class, returning its prototype object. */
  70. extern JSObject *
  71. js_InitNumberClass(JSContext *cx, JSObject *obj);
  72.  
  73. /* GC-allocate a new JS number. */
  74. extern jsdouble *
  75. js_NewDouble(JSContext *cx, jsdouble d);
  76.  
  77. extern void
  78. js_FinalizeDouble(JSContext *cx, jsdouble *dp);
  79.  
  80. extern JSBool
  81. js_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
  82.  
  83. extern JSBool
  84. js_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
  85.  
  86. /* Construct a Number instance that wraps around d. */
  87. extern JSObject *
  88. js_NumberToObject(JSContext *cx, jsdouble d);
  89.  
  90. /* Convert a number to a GC'ed string. */
  91. extern JSString *
  92. js_NumberToString(JSContext *cx, jsdouble d);
  93.  
  94. /*
  95.  * Convert a value to a number, returning false after reporting any error,
  96.  * otherwise returning true with *dp set.
  97.  */
  98. extern JSBool
  99. js_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
  100.  
  101. /*
  102.  * Convert a value to a number, then to an int32 if it fits (discarding any
  103.  * fractional part, but failing with an error if the double is out of range
  104.  * or unordered).
  105.  */
  106. extern JSBool
  107. js_ValueToInt32(JSContext *cx, jsval v, int32 *ip);
  108.  
  109. extern JSBool
  110. js_ValueToUint16(JSContext *cx, jsval v, uint16 *ip);
  111.  
  112. /*
  113.  * Convert a jsdouble to an integral number, stored in a jsdouble.
  114.  * If d is NaN, return 0.  If d is an infinity, return it without conversion.
  115.  */
  116. extern jsdouble
  117. js_DoubleToInteger(jsdouble d);
  118.  
  119. extern JSBool
  120. js_strtod(const jschar *s, jschar **ep, jsdouble *dp);
  121.  
  122. extern JSBool
  123. js_strtol(const jschar *s, jschar **ep, jsint radix, jsdouble *dp);
  124.  
  125. PR_END_EXTERN_C
  126.  
  127. #endif /* jsnum_h___ */
  128.