1 #include "duk_internal.h"
2 
3 #if defined(DUK_USE_FASTINT)
4 
5 /*
6  *  Manually optimized double-to-fastint downgrade check.
7  *
8  *  This check has a large impact on performance, especially for fastint
9  *  slow paths, so must be changed carefully.  The code should probably be
10  *  optimized for the case where the result does not fit into a fastint,
11  *  to minimize the penalty for "slow path code" dealing with fractions etc.
12  *
13  *  At least on one tested soft float ARM platform double-to-int64 coercion
14  *  is very slow (and sometimes produces incorrect results, see self tests).
15  *  This algorithm combines a fastint compatibility check and extracting the
16  *  integer value from an IEEE double for setting the tagged fastint.  For
17  *  other platforms a more naive approach might be better.
18  *
19  *  See doc/fastint.rst for details.
20  */
21 
duk_tval_set_number_chkfast_fast(duk_tval * tv,duk_double_t x)22 DUK_INTERNAL DUK_ALWAYS_INLINE void duk_tval_set_number_chkfast_fast(duk_tval *tv, duk_double_t x) {
23 	duk_double_union du;
24 	duk_int64_t i;
25 	duk_small_int_t expt;
26 	duk_small_int_t shift;
27 
28 	/* XXX: optimize for packed duk_tval directly? */
29 
30 	du.d = x;
31 	i = (duk_int64_t) DUK_DBLUNION_GET_INT64(&du);
32 	expt = (duk_small_int_t) ((i >> 52) & 0x07ff);
33 	shift = expt - 1023;
34 
35 	if (shift >= 0 && shift <= 46) {  /* exponents 1023 to 1069 */
36 		duk_int64_t t;
37 
38 		if (((DUK_I64_CONSTANT(0x000fffffffffffff) >> shift) & i) == 0) {
39 			t = i | DUK_I64_CONSTANT(0x0010000000000000);  /* implicit leading one */
40 			t = t & DUK_I64_CONSTANT(0x001fffffffffffff);
41 			t = t >> (52 - shift);
42 			if (i < 0) {
43 				t = -t;
44 			}
45 			DUK_TVAL_SET_FASTINT(tv, t);
46 			return;
47 		}
48 	} else if (shift == -1023) {  /* exponent 0 */
49 		if (i >= 0 && (i & DUK_I64_CONSTANT(0x000fffffffffffff)) == 0) {
50 			/* Note: reject negative zero. */
51 			DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) 0);
52 			return;
53 		}
54 	} else if (shift == 47) {  /* exponent 1070 */
55 		if (i < 0 && (i & DUK_I64_CONSTANT(0x000fffffffffffff)) == 0) {
56 			DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) DUK_FASTINT_MIN);
57 			return;
58 		}
59 	}
60 
61 	DUK_TVAL_SET_DOUBLE(tv, x);
62 	return;
63 }
64 
duk_tval_set_number_chkfast_slow(duk_tval * tv,duk_double_t x)65 DUK_INTERNAL DUK_NOINLINE void duk_tval_set_number_chkfast_slow(duk_tval *tv, duk_double_t x) {
66 	duk_tval_set_number_chkfast_fast(tv, x);
67 }
68 
69 /*
70  *  Manually optimized number-to-double conversion
71  */
72 
73 #if defined(DUK_USE_FASTINT) && defined(DUK_USE_PACKED_TVAL)
duk_tval_get_number_packed(duk_tval * tv)74 DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_packed(duk_tval *tv) {
75 	duk_double_union du;
76 	duk_uint64_t t;
77 
78 	t = (duk_uint64_t) DUK_DBLUNION_GET_UINT64(tv);
79 	if ((t >> 48) != DUK_TAG_FASTINT) {
80 		return tv->d;
81 	} else if (t & DUK_U64_CONSTANT(0x0000800000000000)) {
82 		t = (duk_uint64_t) (-((duk_int64_t) t));  /* avoid unary minus on unsigned */
83 		t = t & DUK_U64_CONSTANT(0x0000ffffffffffff);  /* negative */
84 		t |= DUK_U64_CONSTANT(0xc330000000000000);
85 		DUK_DBLUNION_SET_UINT64(&du, t);
86 		return du.d + 4503599627370496.0;  /* 1 << 52 */
87 	} else if (t != 0) {
88 		t &= DUK_U64_CONSTANT(0x0000ffffffffffff);  /* positive */
89 		t |= DUK_U64_CONSTANT(0x4330000000000000);
90 		DUK_DBLUNION_SET_UINT64(&du, t);
91 		return du.d - 4503599627370496.0;  /* 1 << 52 */
92 	} else {
93 		return 0.0;  /* zero */
94 	}
95 }
96 #endif  /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
97 
98 #if 0  /* unused */
99 #if defined(DUK_USE_FASTINT) && !defined(DUK_USE_PACKED_TVAL)
100 DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked(duk_tval *tv) {
101 	duk_double_union du;
102 	duk_uint64_t t;
103 
104 	DUK_ASSERT(tv->t == DUK_TAG_NUMBER || tv->t == DUK_TAG_FASTINT);
105 
106 	if (tv->t == DUK_TAG_FASTINT) {
107 		if (tv->v.fi >= 0) {
108 			t = DUK_U64_CONSTANT(0x4330000000000000) | (duk_uint64_t) tv->v.fi;
109 			DUK_DBLUNION_SET_UINT64(&du, t);
110 			return du.d - 4503599627370496.0;  /* 1 << 52 */
111 		} else {
112 			t = DUK_U64_CONSTANT(0xc330000000000000) | (duk_uint64_t) (-tv->v.fi);
113 			DUK_DBLUNION_SET_UINT64(&du, t);
114 			return du.d + 4503599627370496.0;  /* 1 << 52 */
115 		}
116 	} else {
117 		return tv->v.d;
118 	}
119 }
120 #endif  /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
121 #endif  /* 0 */
122 
123 #if defined(DUK_USE_FASTINT) && !defined(DUK_USE_PACKED_TVAL)
duk_tval_get_number_unpacked_fastint(duk_tval * tv)124 DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv) {
125 	duk_double_union du;
126 	duk_uint64_t t;
127 
128 	DUK_ASSERT(tv->t == DUK_TAG_FASTINT);
129 
130 	if (tv->v.fi >= 0) {
131 		t = DUK_U64_CONSTANT(0x4330000000000000) | (duk_uint64_t) tv->v.fi;
132 		DUK_DBLUNION_SET_UINT64(&du, t);
133 		return du.d - 4503599627370496.0;  /* 1 << 52 */
134 	} else {
135 		t = DUK_U64_CONSTANT(0xc330000000000000) | (duk_uint64_t) (-tv->v.fi);
136 		DUK_DBLUNION_SET_UINT64(&du, t);
137 		return du.d + 4503599627370496.0;  /* 1 << 52 */
138 	}
139 }
140 #endif  /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
141 
142 #endif  /* DUK_USE_FASTINT */
143 
144 /*
145  *  Assertion helpers.
146  */
147 
148 #if defined(DUK_USE_ASSERTIONS)
duk_tval_assert_valid(duk_tval * tv)149 DUK_INTERNAL void duk_tval_assert_valid(duk_tval *tv) {
150 	DUK_ASSERT(tv != NULL);
151 }
152 #endif
153