OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef WASM_EXTERNAL_REFS_H | 5 #include <math.h> |
6 #define WASM_EXTERNAL_REFS_H | 6 #include <stdint.h> |
| 7 #include <stdlib.h> |
| 8 #include <limits> |
| 9 |
| 10 #include "include/v8config.h" |
| 11 |
| 12 #include "src/wasm/wasm-external-refs.h" |
7 | 13 |
8 namespace v8 { | 14 namespace v8 { |
9 namespace internal { | 15 namespace internal { |
10 namespace wasm { | 16 namespace wasm { |
11 | 17 |
12 static void f32_trunc_wrapper(float* param) { *param = truncf(*param); } | 18 void f32_trunc_wrapper(float* param) { *param = truncf(*param); } |
13 | 19 |
14 static void f32_floor_wrapper(float* param) { *param = floorf(*param); } | 20 void f32_floor_wrapper(float* param) { *param = floorf(*param); } |
15 | 21 |
16 static void f32_ceil_wrapper(float* param) { *param = ceilf(*param); } | 22 void f32_ceil_wrapper(float* param) { *param = ceilf(*param); } |
17 | 23 |
18 static void f32_nearest_int_wrapper(float* param) { | 24 void f32_nearest_int_wrapper(float* param) { *param = nearbyintf(*param); } |
19 *param = nearbyintf(*param); | |
20 } | |
21 | 25 |
22 static void f64_trunc_wrapper(double* param) { *param = trunc(*param); } | 26 void f64_trunc_wrapper(double* param) { *param = trunc(*param); } |
23 | 27 |
24 static void f64_floor_wrapper(double* param) { *param = floor(*param); } | 28 void f64_floor_wrapper(double* param) { *param = floor(*param); } |
25 | 29 |
26 static void f64_ceil_wrapper(double* param) { *param = ceil(*param); } | 30 void f64_ceil_wrapper(double* param) { *param = ceil(*param); } |
27 | 31 |
28 static void f64_nearest_int_wrapper(double* param) { | 32 void f64_nearest_int_wrapper(double* param) { *param = nearbyint(*param); } |
29 *param = nearbyint(*param); | |
30 } | |
31 | 33 |
32 static void int64_to_float32_wrapper(int64_t* input, float* output) { | 34 void int64_to_float32_wrapper(int64_t* input, float* output) { |
33 *output = static_cast<float>(*input); | 35 *output = static_cast<float>(*input); |
34 } | 36 } |
35 | 37 |
36 static void uint64_to_float32_wrapper(uint64_t* input, float* output) { | 38 void uint64_to_float32_wrapper(uint64_t* input, float* output) { |
37 #if V8_CC_MSVC | 39 #if V8_CC_MSVC |
38 // With MSVC we use static_cast<float>(uint32_t) instead of | 40 // With MSVC we use static_cast<float>(uint32_t) instead of |
39 // static_cast<float>(uint64_t) to achieve round-to-nearest-ties-even | 41 // static_cast<float>(uint64_t) to achieve round-to-nearest-ties-even |
40 // semantics. The idea is to calculate | 42 // semantics. The idea is to calculate |
41 // static_cast<float>(high_word) * 2^32 + static_cast<float>(low_word). To | 43 // static_cast<float>(high_word) * 2^32 + static_cast<float>(low_word). To |
42 // achieve proper rounding in all cases we have to adjust the high_word | 44 // achieve proper rounding in all cases we have to adjust the high_word |
43 // with a "rounding bit" sometimes. The rounding bit is stored in the LSB of | 45 // with a "rounding bit" sometimes. The rounding bit is stored in the LSB of |
44 // the high_word if the low_word may affect the rounding of the high_word. | 46 // the high_word if the low_word may affect the rounding of the high_word. |
45 uint32_t low_word = static_cast<uint32_t>(*input & 0xffffffff); | 47 uint32_t low_word = static_cast<uint32_t>(*input & 0xffffffff); |
46 uint32_t high_word = static_cast<uint32_t>(*input >> 32); | 48 uint32_t high_word = static_cast<uint32_t>(*input >> 32); |
(...skipping 13 matching lines...) Expand all Loading... |
60 float result = static_cast<float>(high_word); | 62 float result = static_cast<float>(high_word); |
61 result *= shift; | 63 result *= shift; |
62 result += static_cast<float>(low_word); | 64 result += static_cast<float>(low_word); |
63 *output = result; | 65 *output = result; |
64 | 66 |
65 #else | 67 #else |
66 *output = static_cast<float>(*input); | 68 *output = static_cast<float>(*input); |
67 #endif | 69 #endif |
68 } | 70 } |
69 | 71 |
70 static void int64_to_float64_wrapper(int64_t* input, double* output) { | 72 void int64_to_float64_wrapper(int64_t* input, double* output) { |
71 *output = static_cast<double>(*input); | 73 *output = static_cast<double>(*input); |
72 } | 74 } |
73 | 75 |
74 static void uint64_to_float64_wrapper(uint64_t* input, double* output) { | 76 void uint64_to_float64_wrapper(uint64_t* input, double* output) { |
75 #if V8_CC_MSVC | 77 #if V8_CC_MSVC |
76 // With MSVC we use static_cast<double>(uint32_t) instead of | 78 // With MSVC we use static_cast<double>(uint32_t) instead of |
77 // static_cast<double>(uint64_t) to achieve round-to-nearest-ties-even | 79 // static_cast<double>(uint64_t) to achieve round-to-nearest-ties-even |
78 // semantics. The idea is to calculate | 80 // semantics. The idea is to calculate |
79 // static_cast<double>(high_word) * 2^32 + static_cast<double>(low_word). | 81 // static_cast<double>(high_word) * 2^32 + static_cast<double>(low_word). |
80 uint32_t low_word = static_cast<uint32_t>(*input & 0xffffffff); | 82 uint32_t low_word = static_cast<uint32_t>(*input & 0xffffffff); |
81 uint32_t high_word = static_cast<uint32_t>(*input >> 32); | 83 uint32_t high_word = static_cast<uint32_t>(*input >> 32); |
82 | 84 |
83 double shift = static_cast<double>(1ull << 32); | 85 double shift = static_cast<double>(1ull << 32); |
84 | 86 |
85 double result = static_cast<double>(high_word); | 87 double result = static_cast<double>(high_word); |
86 result *= shift; | 88 result *= shift; |
87 result += static_cast<double>(low_word); | 89 result += static_cast<double>(low_word); |
88 *output = result; | 90 *output = result; |
89 | 91 |
90 #else | 92 #else |
91 *output = static_cast<double>(*input); | 93 *output = static_cast<double>(*input); |
92 #endif | 94 #endif |
93 } | 95 } |
94 | 96 |
95 static int32_t float32_to_int64_wrapper(float* input, int64_t* output) { | 97 int32_t float32_to_int64_wrapper(float* input, int64_t* output) { |
96 // We use "<" here to check the upper bound because of rounding problems: With | 98 // We use "<" here to check the upper bound because of rounding problems: With |
97 // "<=" some inputs would be considered within int64 range which are actually | 99 // "<=" some inputs would be considered within int64 range which are actually |
98 // not within int64 range. | 100 // not within int64 range. |
99 if (*input >= static_cast<float>(std::numeric_limits<int64_t>::min()) && | 101 if (*input >= static_cast<float>(std::numeric_limits<int64_t>::min()) && |
100 *input < static_cast<float>(std::numeric_limits<int64_t>::max())) { | 102 *input < static_cast<float>(std::numeric_limits<int64_t>::max())) { |
101 *output = static_cast<int64_t>(*input); | 103 *output = static_cast<int64_t>(*input); |
102 return 1; | 104 return 1; |
103 } | 105 } |
104 return 0; | 106 return 0; |
105 } | 107 } |
106 | 108 |
107 static int32_t float32_to_uint64_wrapper(float* input, uint64_t* output) { | 109 int32_t float32_to_uint64_wrapper(float* input, uint64_t* output) { |
108 // We use "<" here to check the upper bound because of rounding problems: With | 110 // We use "<" here to check the upper bound because of rounding problems: With |
109 // "<=" some inputs would be considered within uint64 range which are actually | 111 // "<=" some inputs would be considered within uint64 range which are actually |
110 // not within uint64 range. | 112 // not within uint64 range. |
111 if (*input > -1.0 && | 113 if (*input > -1.0 && |
112 *input < static_cast<float>(std::numeric_limits<uint64_t>::max())) { | 114 *input < static_cast<float>(std::numeric_limits<uint64_t>::max())) { |
113 *output = static_cast<uint64_t>(*input); | 115 *output = static_cast<uint64_t>(*input); |
114 return 1; | 116 return 1; |
115 } | 117 } |
116 return 0; | 118 return 0; |
117 } | 119 } |
118 | 120 |
119 static int32_t float64_to_int64_wrapper(double* input, int64_t* output) { | 121 int32_t float64_to_int64_wrapper(double* input, int64_t* output) { |
120 // We use "<" here to check the upper bound because of rounding problems: With | 122 // We use "<" here to check the upper bound because of rounding problems: With |
121 // "<=" some inputs would be considered within int64 range which are actually | 123 // "<=" some inputs would be considered within int64 range which are actually |
122 // not within int64 range. | 124 // not within int64 range. |
123 if (*input >= static_cast<double>(std::numeric_limits<int64_t>::min()) && | 125 if (*input >= static_cast<double>(std::numeric_limits<int64_t>::min()) && |
124 *input < static_cast<double>(std::numeric_limits<int64_t>::max())) { | 126 *input < static_cast<double>(std::numeric_limits<int64_t>::max())) { |
125 *output = static_cast<int64_t>(*input); | 127 *output = static_cast<int64_t>(*input); |
126 return 1; | 128 return 1; |
127 } | 129 } |
128 return 0; | 130 return 0; |
129 } | 131 } |
130 | 132 |
131 static int32_t float64_to_uint64_wrapper(double* input, uint64_t* output) { | 133 int32_t float64_to_uint64_wrapper(double* input, uint64_t* output) { |
132 // We use "<" here to check the upper bound because of rounding problems: With | 134 // We use "<" here to check the upper bound because of rounding problems: With |
133 // "<=" some inputs would be considered within uint64 range which are actually | 135 // "<=" some inputs would be considered within uint64 range which are actually |
134 // not within uint64 range. | 136 // not within uint64 range. |
135 if (*input > -1.0 && | 137 if (*input > -1.0 && |
136 *input < static_cast<double>(std::numeric_limits<uint64_t>::max())) { | 138 *input < static_cast<double>(std::numeric_limits<uint64_t>::max())) { |
137 *output = static_cast<uint64_t>(*input); | 139 *output = static_cast<uint64_t>(*input); |
138 return 1; | 140 return 1; |
139 } | 141 } |
140 return 0; | 142 return 0; |
141 } | 143 } |
142 | 144 |
143 static int32_t int64_div_wrapper(int64_t* dst, int64_t* src) { | 145 int32_t int64_div_wrapper(int64_t* dst, int64_t* src) { |
144 if (*src == 0) { | 146 if (*src == 0) { |
145 return 0; | 147 return 0; |
146 } | 148 } |
147 if (*src == -1 && *dst == std::numeric_limits<int64_t>::min()) { | 149 if (*src == -1 && *dst == std::numeric_limits<int64_t>::min()) { |
148 return -1; | 150 return -1; |
149 } | 151 } |
150 *dst /= *src; | 152 *dst /= *src; |
151 return 1; | 153 return 1; |
152 } | 154 } |
153 | 155 |
154 static int32_t int64_mod_wrapper(int64_t* dst, int64_t* src) { | 156 int32_t int64_mod_wrapper(int64_t* dst, int64_t* src) { |
155 if (*src == 0) { | 157 if (*src == 0) { |
156 return 0; | 158 return 0; |
157 } | 159 } |
158 *dst %= *src; | 160 *dst %= *src; |
159 return 1; | 161 return 1; |
160 } | 162 } |
161 | 163 |
162 static int32_t uint64_div_wrapper(uint64_t* dst, uint64_t* src) { | 164 int32_t uint64_div_wrapper(uint64_t* dst, uint64_t* src) { |
163 if (*src == 0) { | 165 if (*src == 0) { |
164 return 0; | 166 return 0; |
165 } | 167 } |
166 *dst /= *src; | 168 *dst /= *src; |
167 return 1; | 169 return 1; |
168 } | 170 } |
169 | 171 |
170 static int32_t uint64_mod_wrapper(uint64_t* dst, uint64_t* src) { | 172 int32_t uint64_mod_wrapper(uint64_t* dst, uint64_t* src) { |
171 if (*src == 0) { | 173 if (*src == 0) { |
172 return 0; | 174 return 0; |
173 } | 175 } |
174 *dst %= *src; | 176 *dst %= *src; |
175 return 1; | 177 return 1; |
176 } | 178 } |
177 } // namespace wasm | 179 } // namespace wasm |
178 } // namespace internal | 180 } // namespace internal |
179 } // namespace v8 | 181 } // namespace v8 |
180 | |
181 #endif | |
OLD | NEW |