source: trunk/testcase/floatingpoint/jsnumexception.c@ 375

Last change on this file since 375 was 375, checked in by bird, 22 years ago

#563: let's get some output as well..

  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1#include <stdio.h>
2#include <float.h>
3
4#define JSDOUBLE_HI32_SIGNBIT 0x80000000
5#define JSDOUBLE_HI32_EXPMASK 0x7ff00000
6#define JSDOUBLE_HI32_MANTMASK 0x000fffff
7#define JSVAL_INT_POW2(n) ((unsigned long)1 << (n))
8#define JSVAL_INT_MAX (JSVAL_INT_POW2(30) - 1)
9#define JSVAL_INT_MIN ((unsigned long)1 - JSVAL_INT_POW2(30))
10
11typedef unsigned uint32;
12typedef uint32 jsuint;
13typedef int jsint;
14
15
16#define JSDOUBLE_HI32(x) (((uint32 *)&(x))[1])
17#define JSDOUBLE_LO32(x) (((uint32 *)&(x))[0])
18
19#define JSDOUBLE_IS_NEGZERO(d) (JSDOUBLE_HI32(d) == JSDOUBLE_HI32_SIGNBIT && \
20 JSDOUBLE_LO32(d) == 0)
21#define JSDOUBLE_IS_FINITE(x) \
22 ((JSDOUBLE_HI32(x) & JSDOUBLE_HI32_EXPMASK) != JSDOUBLE_HI32_EXPMASK)
23
24#define JSDOUBLE_IS_INT(d, i) (JSDOUBLE_IS_FINITE(d) \
25 && !JSDOUBLE_IS_NEGZERO(d) \
26 && ((d) == (i = (jsint)(d))))
27
28#define INT_FITS_IN_JSVAL(i) ((jsuint)((i)+JSVAL_INT_MAX) <= 2*JSVAL_INT_MAX)
29
30
31int js_NewNumberValue(void *cx, double d, int *rval)
32{
33 int i = -1;
34#if 1
35 if ( JSDOUBLE_IS_INT(d, i)
36 && INT_FITS_IN_JSVAL(i))
37 {
38 *rval = i;
39 return 1;
40 }
41
42#else
43 if (((((unsigned *)&d)[1]) & JSDOUBLE_HI32_EXPMASK) != JSDOUBLE_HI32_EXPMASK )
44 if ( !((((unsigned *)&d)[1]) == JSDOUBLE_HI32_SIGNBIT && (((unsigned *)&d)[0]) == 0))
45 if (d == (i = (int)d))
46 if ((unsigned long)((i)+JSVAL_INT_MAX) <= 2*JSVAL_INT_MAX)
47 return i;
48#endif
49 *rval = i;
50 return 0;
51}
52
53
54int main()
55{
56 double rd1 = 0.0;
57 double rd2 = 1.000000000e8;
58 double rd3 = 2.147746133e9;
59 int l = -1;
60
61 _control87(0x262, 0xFFF);
62
63 l = -1;
64 js_NewNumberValue(NULL, rd1, &l);
65 printf("%11d %f\n", l, rd1);
66
67 l = -1;
68 js_NewNumberValue(NULL, rd2, &l);
69 printf("%11d %f\n", l, rd2);
70
71 l = -1;
72 js_NewNumberValue(NULL, rd3, &l);
73 printf("%11d %f\n", l, rd3);
74 return 0;
75}
76
Note: See TracBrowser for help on using the repository browser.