Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: src/runtime/runtime-simd.cc

Issue 1230343003: V8: Add SIMD functions. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Remove name table size hack. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 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 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/arguments.h" 7 #include "src/arguments.h"
8 #include "src/base/macros.h" 8 #include "src/base/macros.h"
9 #include "src/conversions.h" 9 #include "src/conversions.h"
10 #include "src/runtime/runtime-utils.h" 10 #include "src/runtime/runtime-utils.h"
11 11
12 // Implement Single Instruction Multiple Data (SIMD) operations as defined in 12 // Implement Single Instruction Multiple Data (SIMD) operations as defined in
13 // the SIMD.js draft spec: 13 // the SIMD.js draft spec:
14 // http://littledan.github.io/simd.html 14 // http://littledan.github.io/simd.html
15 15
16 #define CONVERT_SIMD_LANE_ARG_CHECKED(name, index, lanes) \
17 CONVERT_INT32_ARG_CHECKED(name, index); \
18 RUNTIME_ASSERT(name >= 0 && name < lanes);
19
20 #define SIMD_CREATE_NUMERIC_FUNCTION(type, lane_type, lane_count) \
21 RUNTIME_FUNCTION(Runtime_Create##type) { \
22 static const int kLaneCount = lane_count; \
23 HandleScope scope(isolate); \
24 DCHECK(args.length() == kLaneCount); \
25 lane_type lanes[kLaneCount]; \
26 for (int i = 0; i < kLaneCount; i++) { \
27 CONVERT_NUMBER_ARG_HANDLE_CHECKED(number, i); \
28 lanes[i] = ConvertNumber<lane_type>(number->Number()); \
29 } \
30 return *isolate->factory()->New##type(lanes); \
31 }
32
33 #define SIMD_CREATE_BOOLEAN_FUNCTION(type, lane_count) \
34 RUNTIME_FUNCTION(Runtime_Create##type) { \
35 static const int kLaneCount = lane_count; \
36 HandleScope scope(isolate); \
37 DCHECK(args.length() == kLaneCount); \
38 bool lanes[kLaneCount]; \
39 for (int i = 0; i < kLaneCount; i++) { \
40 lanes[i] = args[i]->BooleanValue(); \
41 } \
42 return *isolate->factory()->New##type(lanes); \
43 }
44
45 #define SIMD_CHECK_FUNCTION(type) \
46 RUNTIME_FUNCTION(Runtime_##type##Check) { \
47 HandleScope scope(isolate); \
48 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
49 return *a; \
50 }
51
52 #define SIMD_EXTRACT_LANE_FUNCTION(type, lanes, extract_fn) \
53 RUNTIME_FUNCTION(Runtime_##type##ExtractLane) { \
54 HandleScope scope(isolate); \
55 DCHECK(args.length() == 2); \
56 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
57 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, lanes); \
58 return *isolate->factory()->extract_fn(a->get_lane(lane)); \
59 }
60
61 #define SIMD_REPLACE_NUMERIC_LANE_FUNCTION(type, lane_type, lane_count) \
62 RUNTIME_FUNCTION(Runtime_##type##ReplaceLane) { \
63 static const int kLaneCount = lane_count; \
64 HandleScope scope(isolate); \
65 DCHECK(args.length() == 3); \
66 CONVERT_ARG_HANDLE_CHECKED(type, simd, 0); \
67 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, kLaneCount); \
68 CONVERT_NUMBER_ARG_HANDLE_CHECKED(number, 2); \
69 lane_type lanes[kLaneCount]; \
70 for (int i = 0; i < kLaneCount; i++) { \
71 lanes[i] = simd->get_lane(i); \
72 } \
73 lanes[lane] = ConvertNumber<lane_type>(number->Number()); \
74 Handle<type> result = isolate->factory()->New##type(lanes); \
75 return *result; \
76 }
77
78 #define SIMD_REPLACE_BOOLEAN_LANE_FUNCTION(type, lane_count) \
79 RUNTIME_FUNCTION(Runtime_##type##ReplaceLane) { \
80 static const int kLaneCount = lane_count; \
81 HandleScope scope(isolate); \
82 DCHECK(args.length() == 3); \
83 CONVERT_ARG_HANDLE_CHECKED(type, simd, 0); \
84 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, kLaneCount); \
85 bool lanes[kLaneCount]; \
86 for (int i = 0; i < kLaneCount; i++) { \
87 lanes[i] = simd->get_lane(i); \
88 } \
89 lanes[lane] = args[2]->BooleanValue(); \
90 Handle<type> result = isolate->factory()->New##type(lanes); \
91 return *result; \
92 }
93
94
95 namespace v8 { 16 namespace v8 {
96 namespace internal { 17 namespace internal {
97 18
98 namespace { 19 namespace {
99 20
100 // Functions to convert Numbers to SIMD component types. 21 // Functions to convert Numbers to SIMD component types.
101 22
102 template <typename T> 23 template <typename T>
103 static T ConvertNumber(double number); 24 static T ConvertNumber(double number);
104 25
(...skipping 22 matching lines...) Expand all
127 } 48 }
128 49
129 50
130 bool Equals(Float32x4* a, Float32x4* b) { 51 bool Equals(Float32x4* a, Float32x4* b) {
131 for (int i = 0; i < 4; i++) { 52 for (int i = 0; i < 4; i++) {
132 if (a->get_lane(i) != b->get_lane(i)) return false; 53 if (a->get_lane(i) != b->get_lane(i)) return false;
133 } 54 }
134 return true; 55 return true;
135 } 56 }
136 57
58
59 // TODO(bbudge): Make this consistent with SIMD instruction results.
60 inline float RecipApprox(float a) { return 1.0f / a; }
61
62
63 // TODO(bbudge): Make this consistent with SIMD instruction results.
64 inline float RecipSqrtApprox(float a) { return 1.0f / std::sqrt(a); }
65
66
67 // Saturating addition for int16_t and int8_t.
68 template <typename T>
69 inline T AddSaturate(T a, T b) {
70 const T max = std::numeric_limits<T>::max();
71 const T min = std::numeric_limits<T>::min();
72 int32_t result = a + b;
73 if (result > max) return max;
74 if (result < min) return min;
75 return result;
76 }
77
78
79 // Saturating subtraction for int16_t and int8_t.
80 template <typename T>
81 inline T SubSaturate(T a, T b) {
82 const T max = std::numeric_limits<T>::max();
83 const T min = std::numeric_limits<T>::min();
84 int32_t result = a - b;
85 if (result > max) return max;
86 if (result < min) return min;
87 return result;
88 }
89
90
91 inline float Min(float a, float b) {
92 if (a < b) return a;
93 if (a > b) return b;
94 if (a == b) return std::signbit(a) ? a : b;
95 return std::numeric_limits<float>::quiet_NaN();
96 }
97
98
99 inline float Max(float a, float b) {
100 if (a > b) return a;
101 if (a < b) return b;
102 if (a == b) return std::signbit(b) ? a : b;
103 return std::numeric_limits<float>::quiet_NaN();
104 }
105
106
107 inline float MinNumber(float a, float b) {
108 if (std::isnan(a)) return b;
109 if (std::isnan(b)) return a;
110 return Min(a, b);
111 }
112
113
114 inline float MaxNumber(float a, float b) {
115 if (std::isnan(a)) return b;
116 if (std::isnan(b)) return a;
117 return Max(a, b);
118 }
119
120
121 inline bool CanCast(int32_t a) { return true; }
122
123
124 inline bool CanCast(float a) {
125 return a > std::numeric_limits<int32_t>::min() &&
126 a < std::numeric_limits<int32_t>::max();
127 }
128
137 } // namespace 129 } // namespace
138 130
131 //-------------------------------------------------------------------
132
133 // SIMD helper functions.
139 134
140 RUNTIME_FUNCTION(Runtime_IsSimdValue) { 135 RUNTIME_FUNCTION(Runtime_IsSimdValue) {
141 HandleScope scope(isolate); 136 HandleScope scope(isolate);
142 DCHECK(args.length() == 1); 137 DCHECK(args.length() == 1);
143 return isolate->heap()->ToBoolean(args[0]->IsSimd128Value()); 138 return isolate->heap()->ToBoolean(args[0]->IsSimd128Value());
144 } 139 }
145 140
146 141
147 RUNTIME_FUNCTION(Runtime_SimdToObject) { 142 RUNTIME_FUNCTION(Runtime_SimdToObject) {
148 HandleScope scope(isolate); 143 HandleScope scope(isolate);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 result = Float32x4::cast(*a)->SameValueZero(Float32x4::cast(b)); 200 result = Float32x4::cast(*a)->SameValueZero(Float32x4::cast(b));
206 } else { 201 } else {
207 result = a->BitwiseEquals(b); 202 result = a->BitwiseEquals(b);
208 } 203 }
209 } 204 }
210 } 205 }
211 return isolate->heap()->ToBoolean(result); 206 return isolate->heap()->ToBoolean(result);
212 } 207 }
213 208
214 209
215 SIMD_CREATE_NUMERIC_FUNCTION(Float32x4, float, 4) 210 //-------------------------------------------------------------------
216 SIMD_CREATE_NUMERIC_FUNCTION(Int32x4, int32_t, 4) 211
217 SIMD_CREATE_BOOLEAN_FUNCTION(Bool32x4, 4) 212 // Utility macros.
218 SIMD_CREATE_NUMERIC_FUNCTION(Int16x8, int16_t, 8) 213
219 SIMD_CREATE_BOOLEAN_FUNCTION(Bool16x8, 8) 214 #define CONVERT_SIMD_LANE_ARG_CHECKED(name, index, lanes) \
220 SIMD_CREATE_NUMERIC_FUNCTION(Int8x16, int8_t, 16) 215 CONVERT_INT32_ARG_CHECKED(name, index); \
221 SIMD_CREATE_BOOLEAN_FUNCTION(Bool8x16, 16) 216 RUNTIME_ASSERT(name >= 0 && name < lanes);
222 217
223 218 #define SIMD_UNARY_OP(type, lane_type, lane_count, op, result) \
224 SIMD_CHECK_FUNCTION(Float32x4) 219 static const int kLaneCount = lane_count; \
225 SIMD_CHECK_FUNCTION(Int32x4) 220 DCHECK(args.length() == 1); \
226 SIMD_CHECK_FUNCTION(Bool32x4) 221 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
227 SIMD_CHECK_FUNCTION(Int16x8) 222 lane_type lanes[kLaneCount]; \
228 SIMD_CHECK_FUNCTION(Bool16x8) 223 for (int i = 0; i < kLaneCount; i++) { \
229 SIMD_CHECK_FUNCTION(Int8x16) 224 lanes[i] = op(a->get_lane(i)); \
230 SIMD_CHECK_FUNCTION(Bool8x16) 225 } \
231 226 Handle<type> result = isolate->factory()->New##type(lanes);
232 227
233 SIMD_EXTRACT_LANE_FUNCTION(Float32x4, 4, NewNumber) 228 #define SIMD_BINARY_OP(type, lane_type, lane_count, op, result) \
234 SIMD_EXTRACT_LANE_FUNCTION(Int32x4, 4, NewNumber) 229 static const int kLaneCount = lane_count; \
235 SIMD_EXTRACT_LANE_FUNCTION(Bool32x4, 4, ToBoolean) 230 DCHECK(args.length() == 2); \
236 SIMD_EXTRACT_LANE_FUNCTION(Int16x8, 8, NewNumber) 231 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
237 SIMD_EXTRACT_LANE_FUNCTION(Bool16x8, 8, ToBoolean) 232 CONVERT_ARG_HANDLE_CHECKED(type, b, 1); \
238 SIMD_EXTRACT_LANE_FUNCTION(Int8x16, 16, NewNumber) 233 lane_type lanes[kLaneCount]; \
239 SIMD_EXTRACT_LANE_FUNCTION(Bool8x16, 16, ToBoolean) 234 for (int i = 0; i < kLaneCount; i++) { \
240 235 lanes[i] = op(a->get_lane(i), b->get_lane(i)); \
236 } \
237 Handle<type> result = isolate->factory()->New##type(lanes);
238
239 #define SIMD_RELATIONAL_OP(type, bool_type, lane_count, a, b, op, result) \
240 static const int kLaneCount = lane_count; \
241 DCHECK(args.length() == 2); \
242 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
243 CONVERT_ARG_HANDLE_CHECKED(type, b, 1); \
244 bool lanes[kLaneCount]; \
245 for (int i = 0; i < kLaneCount; i++) { \
246 lanes[i] = a->get_lane(i) op b->get_lane(i); \
247 } \
248 Handle<bool_type> result = isolate->factory()->New##bool_type(lanes);
249
250 //-------------------------------------------------------------------
251
252 // Common functions.
253
254 #define GET_NUMERIC_ARG(lane_type, name, index) \
255 CONVERT_NUMBER_ARG_HANDLE_CHECKED(a, index); \
256 name = ConvertNumber<lane_type>(a->Number());
257
258 #define GET_BOOLEAN_ARG(lane_type, name, index) \
259 name = args[index]->BooleanValue();
260
261 #define SIMD_ALL_TYPES(FUNCTION) \
262 FUNCTION(Float32x4, float, 4, NewNumber, GET_NUMERIC_ARG) \
263 FUNCTION(Int32x4, int32_t, 4, NewNumber, GET_NUMERIC_ARG) \
264 FUNCTION(Bool32x4, bool, 4, ToBoolean, GET_BOOLEAN_ARG) \
265 FUNCTION(Int16x8, int16_t, 8, NewNumber, GET_NUMERIC_ARG) \
266 FUNCTION(Bool16x8, bool, 8, ToBoolean, GET_BOOLEAN_ARG) \
267 FUNCTION(Int8x16, int8_t, 16, NewNumber, GET_NUMERIC_ARG) \
268 FUNCTION(Bool8x16, bool, 16, ToBoolean, GET_BOOLEAN_ARG)
269
270 #define SIMD_CREATE_FUNCTION(type, lane_type, lane_count, extract, replace) \
271 RUNTIME_FUNCTION(Runtime_Create##type) { \
272 static const int kLaneCount = lane_count; \
273 HandleScope scope(isolate); \
274 DCHECK(args.length() == kLaneCount); \
275 lane_type lanes[kLaneCount]; \
276 for (int i = 0; i < kLaneCount; i++) { \
277 replace(lane_type, lanes[i], i) \
278 } \
279 return *isolate->factory()->New##type(lanes); \
280 }
281
282 #define SIMD_EXTRACT_FUNCTION(type, lane_type, lane_count, extract, replace) \
283 RUNTIME_FUNCTION(Runtime_##type##ExtractLane) { \
284 HandleScope scope(isolate); \
285 DCHECK(args.length() == 2); \
286 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
287 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, lane_count); \
288 return *isolate->factory()->extract(a->get_lane(lane)); \
289 }
290
291 #define SIMD_REPLACE_FUNCTION(type, lane_type, lane_count, extract, replace) \
292 RUNTIME_FUNCTION(Runtime_##type##ReplaceLane) { \
293 static const int kLaneCount = lane_count; \
294 HandleScope scope(isolate); \
295 DCHECK(args.length() == 3); \
296 CONVERT_ARG_HANDLE_CHECKED(type, simd, 0); \
297 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, kLaneCount); \
298 lane_type lanes[kLaneCount]; \
299 for (int i = 0; i < kLaneCount; i++) { \
300 lanes[i] = simd->get_lane(i); \
301 } \
302 replace(lane_type, lanes[lane], 2); \
303 Handle<type> result = isolate->factory()->New##type(lanes); \
304 return *result; \
305 }
306
307 #define SIMD_CHECK_FUNCTION(type, lane_type, lane_count, extract, replace) \
308 RUNTIME_FUNCTION(Runtime_##type##Check) { \
309 HandleScope scope(isolate); \
310 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
311 return *a; \
312 }
313
314 #define SIMD_SWIZZLE_FUNCTION(type, lane_type, lane_count, extract, replace) \
315 RUNTIME_FUNCTION(Runtime_##type##Swizzle) { \
316 static const int kLaneCount = lane_count; \
317 HandleScope scope(isolate); \
318 DCHECK(args.length() == 1 + kLaneCount); \
319 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
320 lane_type lanes[kLaneCount]; \
321 for (int i = 0; i < kLaneCount; i++) { \
322 CONVERT_SIMD_LANE_ARG_CHECKED(index, i + 1, kLaneCount); \
323 lanes[i] = a->get_lane(index); \
324 } \
325 Handle<type> result = isolate->factory()->New##type(lanes); \
326 return *result; \
327 }
328
329 #define SIMD_SHUFFLE_FUNCTION(type, lane_type, lane_count, extract, replace) \
330 RUNTIME_FUNCTION(Runtime_##type##Shuffle) { \
331 static const int kLaneCount = lane_count; \
332 HandleScope scope(isolate); \
333 DCHECK(args.length() == 2 + kLaneCount); \
334 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
335 CONVERT_ARG_HANDLE_CHECKED(type, b, 1); \
336 lane_type lanes[kLaneCount]; \
337 for (int i = 0; i < kLaneCount; i++) { \
338 CONVERT_SIMD_LANE_ARG_CHECKED(index, i + 2, kLaneCount * 2); \
339 lanes[i] = index < kLaneCount ? a->get_lane(index) \
340 : b->get_lane(index - kLaneCount); \
341 } \
342 Handle<type> result = isolate->factory()->New##type(lanes); \
343 return *result; \
344 }
345
346 SIMD_ALL_TYPES(SIMD_CREATE_FUNCTION)
347 SIMD_ALL_TYPES(SIMD_EXTRACT_FUNCTION)
348 SIMD_ALL_TYPES(SIMD_REPLACE_FUNCTION)
349 SIMD_ALL_TYPES(SIMD_CHECK_FUNCTION)
350 SIMD_ALL_TYPES(SIMD_SWIZZLE_FUNCTION)
351 SIMD_ALL_TYPES(SIMD_SHUFFLE_FUNCTION)
352
353 //-------------------------------------------------------------------
354
355 // Float-only functions.
356
357 #define SIMD_ABS_FUNCTION(type, lane_type, lane_count) \
358 RUNTIME_FUNCTION(Runtime_##type##Abs) { \
359 HandleScope scope(isolate); \
360 SIMD_UNARY_OP(type, lane_type, lane_count, std::abs, result); \
361 return *result; \
362 }
363
364 #define SIMD_SQRT_FUNCTION(type, lane_type, lane_count) \
365 RUNTIME_FUNCTION(Runtime_##type##Sqrt) { \
366 HandleScope scope(isolate); \
367 SIMD_UNARY_OP(type, lane_type, lane_count, std::sqrt, result); \
368 return *result; \
369 }
370
371 #define SIMD_RECIP_APPROX_FUNCTION(type, lane_type, lane_count) \
372 RUNTIME_FUNCTION(Runtime_##type##RecipApprox) { \
373 HandleScope scope(isolate); \
374 SIMD_UNARY_OP(type, lane_type, lane_count, RecipApprox, result); \
375 return *result; \
376 }
377
378 #define SIMD_RECIP_SQRT_APPROX_FUNCTION(type, lane_type, lane_count) \
379 RUNTIME_FUNCTION(Runtime_##type##RecipSqrtApprox) { \
380 HandleScope scope(isolate); \
381 SIMD_UNARY_OP(type, lane_type, lane_count, RecipSqrtApprox, result); \
382 return *result; \
383 }
384
385 #define BINARY_DIV(a, b) (a) / (b)
386 #define SIMD_DIV_FUNCTION(type, lane_type, lane_count) \
387 RUNTIME_FUNCTION(Runtime_##type##Div) { \
388 HandleScope scope(isolate); \
389 SIMD_BINARY_OP(type, lane_type, lane_count, BINARY_DIV, result); \
390 return *result; \
391 }
392
393 #define SIMD_MINNUM_FUNCTION(type, lane_type, lane_count) \
394 RUNTIME_FUNCTION(Runtime_##type##MinNum) { \
395 HandleScope scope(isolate); \
396 SIMD_BINARY_OP(type, lane_type, lane_count, MinNumber, result); \
397 return *result; \
398 }
399
400 #define SIMD_MAXNUM_FUNCTION(type, lane_type, lane_count) \
401 RUNTIME_FUNCTION(Runtime_##type##MaxNum) { \
402 HandleScope scope(isolate); \
403 SIMD_BINARY_OP(type, lane_type, lane_count, MaxNumber, result); \
404 return *result; \
405 }
406
407 SIMD_ABS_FUNCTION(Float32x4, float, 4)
408 SIMD_SQRT_FUNCTION(Float32x4, float, 4)
409 SIMD_RECIP_APPROX_FUNCTION(Float32x4, float, 4)
410 SIMD_RECIP_SQRT_APPROX_FUNCTION(Float32x4, float, 4)
411 SIMD_DIV_FUNCTION(Float32x4, float, 4)
412 SIMD_MINNUM_FUNCTION(Float32x4, float, 4)
413 SIMD_MAXNUM_FUNCTION(Float32x4, float, 4)
414
415 //-------------------------------------------------------------------
416
417 // Int-only functions.
418
419 #define SIMD_INT_TYPES(FUNCTION) \
420 FUNCTION(Int32x4, int32_t, 32, 4) \
421 FUNCTION(Int16x8, int16_t, 16, 8) \
422 FUNCTION(Int8x16, int8_t, 8, 16)
423
424 #define CONVERT_SHIFT_ARG_CHECKED(name, index) \
425 RUNTIME_ASSERT(args[index]->IsNumber()); \
426 int32_t signed_shift = 0; \
427 RUNTIME_ASSERT(args[index]->ToInt32(&signed_shift)); \
428 uint32_t name = bit_cast<uint32_t>(signed_shift);
429
430 #define SIMD_LSL_FUNCTION(type, lane_type, lane_bits, lane_count) \
431 RUNTIME_FUNCTION(Runtime_##type##ShiftLeftByScalar) { \
432 static const int kLaneCount = lane_count; \
433 HandleScope scope(isolate); \
434 DCHECK(args.length() == 2); \
435 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
436 CONVERT_SHIFT_ARG_CHECKED(shift, 1); \
437 lane_type lanes[kLaneCount] = {0}; \
438 if (shift < lane_bits) { \
439 for (int i = 0; i < kLaneCount; i++) { \
440 lanes[i] = a->get_lane(i) << shift; \
441 } \
442 } \
443 Handle<type> result = isolate->factory()->New##type(lanes); \
444 return *result; \
445 }
446
447 #define SIMD_LSR_FUNCTION(type, lane_type, lane_bits, lane_count) \
448 RUNTIME_FUNCTION(Runtime_##type##ShiftRightLogicalByScalar) { \
449 static const int kLaneCount = lane_count; \
450 HandleScope scope(isolate); \
451 DCHECK(args.length() == 2); \
452 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
453 CONVERT_SHIFT_ARG_CHECKED(shift, 1); \
454 lane_type lanes[kLaneCount] = {0}; \
455 if (shift < lane_bits) { \
456 for (int i = 0; i < kLaneCount; i++) { \
457 lanes[i] = static_cast<lane_type>( \
458 bit_cast<u##lane_type>(a->get_lane(i)) >> shift); \
459 } \
460 } \
461 Handle<type> result = isolate->factory()->New##type(lanes); \
462 return *result; \
463 }
464
465 #define SIMD_ASR_FUNCTION(type, lane_type, lane_bits, lane_count) \
466 RUNTIME_FUNCTION(Runtime_##type##ShiftRightArithmeticByScalar) { \
467 static const int kLaneCount = lane_count; \
468 HandleScope scope(isolate); \
469 DCHECK(args.length() == 2); \
470 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
471 CONVERT_SHIFT_ARG_CHECKED(shift, 1); \
472 if (shift >= lane_bits) shift = lane_bits - 1; \
473 lane_type lanes[kLaneCount]; \
474 for (int i = 0; i < kLaneCount; i++) { \
475 int64_t shifted = static_cast<int64_t>(a->get_lane(i)) >> shift; \
476 lanes[i] = static_cast<lane_type>(shifted); \
477 } \
478 Handle<type> result = isolate->factory()->New##type(lanes); \
479 return *result; \
480 }
481
482 SIMD_INT_TYPES(SIMD_LSL_FUNCTION)
483 SIMD_INT_TYPES(SIMD_LSR_FUNCTION)
484 SIMD_INT_TYPES(SIMD_ASR_FUNCTION)
485
486 //-------------------------------------------------------------------
487
488 // Bool-only functions.
489
490 #define SIMD_BOOL_TYPES(FUNCTION) \
491 FUNCTION(Bool32x4, 4) \
492 FUNCTION(Bool16x8, 8) \
493 FUNCTION(Bool8x16, 16)
494
495 #define SIMD_ANY_FUNCTION(type, lane_count) \
496 RUNTIME_FUNCTION(Runtime_##type##AnyTrue) { \
497 HandleScope scope(isolate); \
498 DCHECK(args.length() == 1); \
499 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
500 bool result = false; \
501 for (int i = 0; i < lane_count; i++) { \
502 if (a->get_lane(i)) { \
503 result = true; \
504 break; \
505 } \
506 } \
507 return isolate->heap()->ToBoolean(result); \
508 }
509
510 #define SIMD_ALL_FUNCTION(type, lane_count) \
511 RUNTIME_FUNCTION(Runtime_##type##AllTrue) { \
512 HandleScope scope(isolate); \
513 DCHECK(args.length() == 1); \
514 CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
515 bool result = true; \
516 for (int i = 0; i < lane_count; i++) { \
517 if (!a->get_lane(i)) { \
518 result = false; \
519 break; \
520 } \
521 } \
522 return isolate->heap()->ToBoolean(result); \
523 }
524
525 SIMD_BOOL_TYPES(SIMD_ANY_FUNCTION)
526 SIMD_BOOL_TYPES(SIMD_ALL_FUNCTION)
527
528 //-------------------------------------------------------------------
529
530 // Small Int-only functions.
531
532 #define SIMD_SMALL_INT_TYPES(FUNCTION) \
533 FUNCTION(Int16x8, int16_t, 8) \
534 FUNCTION(Int8x16, int8_t, 16)
535
536 #define SIMD_ADD_SATURATE_FUNCTION(type, lane_type, lane_count) \
537 RUNTIME_FUNCTION(Runtime_##type##AddSaturate) { \
538 HandleScope scope(isolate); \
539 SIMD_BINARY_OP(type, lane_type, lane_count, AddSaturate, result); \
540 return *result; \
541 }
542
543 #define BINARY_SUB(a, b) (a) - (b)
544 #define SIMD_SUB_SATURATE_FUNCTION(type, lane_type, lane_count) \
545 RUNTIME_FUNCTION(Runtime_##type##SubSaturate) { \
546 HandleScope scope(isolate); \
547 SIMD_BINARY_OP(type, lane_type, lane_count, SubSaturate, result); \
548 return *result; \
549 }
550
551 SIMD_SMALL_INT_TYPES(SIMD_ADD_SATURATE_FUNCTION)
552 SIMD_SMALL_INT_TYPES(SIMD_SUB_SATURATE_FUNCTION)
553
554 //-------------------------------------------------------------------
555
556 // Numeric functions.
557
558 #define SIMD_NUMERIC_TYPES(FUNCTION) \
559 FUNCTION(Float32x4, float, 4) \
560 FUNCTION(Int32x4, int32_t, 4) \
561 FUNCTION(Int16x8, int16_t, 8) \
562 FUNCTION(Int8x16, int8_t, 16)
563
564 #define SIMD_NEG_FUNCTION(type, lane_type, lane_count) \
565 RUNTIME_FUNCTION(Runtime_##type##Neg) { \
566 HandleScope scope(isolate); \
567 SIMD_UNARY_OP(type, lane_type, lane_count, -, result); \
568 return *result; \
569 }
570
571 #define BINARY_ADD(a, b) (a) + (b)
572 #define SIMD_ADD_FUNCTION(type, lane_type, lane_count) \
573 RUNTIME_FUNCTION(Runtime_##type##Add) { \
574 HandleScope scope(isolate); \
575 SIMD_BINARY_OP(type, lane_type, lane_count, BINARY_ADD, result); \
576 return *result; \
577 }
578
579 #define BINARY_SUB(a, b) (a) - (b)
580 #define SIMD_SUB_FUNCTION(type, lane_type, lane_count) \
581 RUNTIME_FUNCTION(Runtime_##type##Sub) { \
582 HandleScope scope(isolate); \
583 SIMD_BINARY_OP(type, lane_type, lane_count, BINARY_SUB, result); \
584 return *result; \
585 }
586
587 #define BINARY_MUL(a, b) (a) * (b)
588 #define SIMD_MUL_FUNCTION(type, lane_type, lane_count) \
589 RUNTIME_FUNCTION(Runtime_##type##Mul) { \
590 HandleScope scope(isolate); \
591 SIMD_BINARY_OP(type, lane_type, lane_count, BINARY_MUL, result); \
592 return *result; \
593 }
594
595 #define SIMD_MIN_FUNCTION(type, lane_type, lane_count) \
596 RUNTIME_FUNCTION(Runtime_##type##Min) { \
597 HandleScope scope(isolate); \
598 SIMD_BINARY_OP(type, lane_type, lane_count, Min, result); \
599 return *result; \
600 }
601
602 #define SIMD_MAX_FUNCTION(type, lane_type, lane_count) \
603 RUNTIME_FUNCTION(Runtime_##type##Max) { \
604 HandleScope scope(isolate); \
605 SIMD_BINARY_OP(type, lane_type, lane_count, Max, result); \
606 return *result; \
607 }
608
609 SIMD_NUMERIC_TYPES(SIMD_NEG_FUNCTION)
610 SIMD_NUMERIC_TYPES(SIMD_ADD_FUNCTION)
611 SIMD_NUMERIC_TYPES(SIMD_SUB_FUNCTION)
612 SIMD_NUMERIC_TYPES(SIMD_MUL_FUNCTION)
613 SIMD_NUMERIC_TYPES(SIMD_MIN_FUNCTION)
614 SIMD_NUMERIC_TYPES(SIMD_MAX_FUNCTION)
615
616 //-------------------------------------------------------------------
617
618 // Relational functions.
619
620 #define SIMD_RELATIONAL_TYPES(FUNCTION) \
621 FUNCTION(Float32x4, Bool32x4, 4) \
622 FUNCTION(Int32x4, Bool32x4, 4) \
623 FUNCTION(Int16x8, Bool16x8, 8) \
624 FUNCTION(Int8x16, Bool8x16, 16)
625
626 #define SIMD_EQUALITY_TYPES(FUNCTION) \
627 SIMD_RELATIONAL_TYPES(FUNCTION) \
628 FUNCTION(Bool32x4, Bool32x4, 4) \
629 FUNCTION(Bool16x8, Bool16x8, 8) \
630 FUNCTION(Bool8x16, Bool8x16, 16)
631
632 #define SIMD_EQUAL_FUNCTION(type, bool_type, lane_count) \
633 RUNTIME_FUNCTION(Runtime_##type##Equal) { \
634 HandleScope scope(isolate); \
635 SIMD_RELATIONAL_OP(type, bool_type, lane_count, a, b, ==, result); \
636 return *result; \
637 }
638
639 #define SIMD_NOT_EQUAL_FUNCTION(type, bool_type, lane_count) \
640 RUNTIME_FUNCTION(Runtime_##type##NotEqual) { \
641 HandleScope scope(isolate); \
642 SIMD_RELATIONAL_OP(type, bool_type, lane_count, a, b, !=, result); \
643 return *result; \
644 }
645
646 SIMD_EQUALITY_TYPES(SIMD_EQUAL_FUNCTION)
647 SIMD_EQUALITY_TYPES(SIMD_NOT_EQUAL_FUNCTION)
648
649 #define SIMD_LESS_THAN_FUNCTION(type, bool_type, lane_count) \
650 RUNTIME_FUNCTION(Runtime_##type##LessThan) { \
651 HandleScope scope(isolate); \
652 SIMD_RELATIONAL_OP(type, bool_type, lane_count, a, b, <, result); \
653 return *result; \
654 }
655
656 #define SIMD_LESS_THAN_OR_EQUAL_FUNCTION(type, bool_type, lane_count) \
657 RUNTIME_FUNCTION(Runtime_##type##LessThanOrEqual) { \
658 HandleScope scope(isolate); \
659 SIMD_RELATIONAL_OP(type, bool_type, lane_count, a, b, <=, result); \
660 return *result; \
661 }
662
663 #define SIMD_GREATER_THAN_FUNCTION(type, bool_type, lane_count) \
664 RUNTIME_FUNCTION(Runtime_##type##GreaterThan) { \
665 HandleScope scope(isolate); \
666 SIMD_RELATIONAL_OP(type, bool_type, lane_count, a, b, >, result); \
667 return *result; \
668 }
669
670 #define SIMD_GREATER_THAN_OR_EQUAL_FUNCTION(type, bool_type, lane_count) \
671 RUNTIME_FUNCTION(Runtime_##type##GreaterThanOrEqual) { \
672 HandleScope scope(isolate); \
673 SIMD_RELATIONAL_OP(type, bool_type, lane_count, a, b, >=, result); \
674 return *result; \
675 }
676
677 SIMD_RELATIONAL_TYPES(SIMD_LESS_THAN_FUNCTION)
678 SIMD_RELATIONAL_TYPES(SIMD_LESS_THAN_OR_EQUAL_FUNCTION)
679 SIMD_RELATIONAL_TYPES(SIMD_GREATER_THAN_FUNCTION)
680 SIMD_RELATIONAL_TYPES(SIMD_GREATER_THAN_OR_EQUAL_FUNCTION)
681
682 //-------------------------------------------------------------------
683
684 // Logical functions.
685
686 #define SIMD_LOGICAL_TYPES(FUNCTION) \
687 FUNCTION(Int32x4, int32_t, 4, _INT) \
688 FUNCTION(Int16x8, int16_t, 8, _INT) \
689 FUNCTION(Int8x16, int8_t, 16, _INT) \
690 FUNCTION(Bool32x4, bool, 4, _BOOL) \
691 FUNCTION(Bool16x8, bool, 8, _BOOL) \
692 FUNCTION(Bool8x16, bool, 16, _BOOL)
693
694 #define BINARY_AND_INT(a, b) (a) & (b)
695 #define BINARY_AND_BOOL(a, b) (a) && (b)
696 #define SIMD_AND_FUNCTION(type, lane_type, lane_count, op) \
697 RUNTIME_FUNCTION(Runtime_##type##And) { \
698 HandleScope scope(isolate); \
699 SIMD_BINARY_OP(type, lane_type, lane_count, BINARY_AND##op, result); \
700 return *result; \
701 }
702
703 #define BINARY_OR_INT(a, b) (a) | (b)
704 #define BINARY_OR_BOOL(a, b) (a) || (b)
705 #define SIMD_OR_FUNCTION(type, lane_type, lane_count, op) \
706 RUNTIME_FUNCTION(Runtime_##type##Or) { \
707 HandleScope scope(isolate); \
708 SIMD_BINARY_OP(type, lane_type, lane_count, BINARY_OR##op, result); \
709 return *result; \
710 }
711
712 #define BINARY_XOR_INT(a, b) (a) ^ (b)
713 #define BINARY_XOR_BOOL(a, b) (a) != (b)
714 #define SIMD_XOR_FUNCTION(type, lane_type, lane_count, op) \
715 RUNTIME_FUNCTION(Runtime_##type##Xor) { \
716 HandleScope scope(isolate); \
717 SIMD_BINARY_OP(type, lane_type, lane_count, BINARY_XOR##op, result); \
718 return *result; \
719 }
720
721 #define UNARY_NOT_INT ~
722 #define UNARY_NOT_BOOL !
723 #define SIMD_NOT_FUNCTION(type, lane_type, lane_count, op) \
724 RUNTIME_FUNCTION(Runtime_##type##Not) { \
725 HandleScope scope(isolate); \
726 SIMD_UNARY_OP(type, lane_type, lane_count, UNARY_NOT##op, result); \
727 return *result; \
728 }
729
730 SIMD_LOGICAL_TYPES(SIMD_AND_FUNCTION)
731 SIMD_LOGICAL_TYPES(SIMD_OR_FUNCTION)
732 SIMD_LOGICAL_TYPES(SIMD_XOR_FUNCTION)
733 SIMD_LOGICAL_TYPES(SIMD_NOT_FUNCTION)
734
735 //-------------------------------------------------------------------
736
737 // Select functions.
738
739 #define SIMD_SELECT_TYPES(FUNCTION) \
740 FUNCTION(Float32x4, float, Bool32x4, 4) \
741 FUNCTION(Int32x4, int32_t, Bool32x4, 4) \
742 FUNCTION(Int16x8, int16_t, Bool16x8, 8) \
743 FUNCTION(Int8x16, int8_t, Bool8x16, 16)
744
745 #define SIMD_SELECT_FUNCTION(type, lane_type, bool_type, lane_count) \
746 RUNTIME_FUNCTION(Runtime_##type##Select) { \
747 static const int kLaneCount = lane_count; \
748 HandleScope scope(isolate); \
749 DCHECK(args.length() == 3); \
750 CONVERT_ARG_HANDLE_CHECKED(bool_type, mask, 0); \
751 CONVERT_ARG_HANDLE_CHECKED(type, a, 1); \
752 CONVERT_ARG_HANDLE_CHECKED(type, b, 2); \
753 lane_type lanes[kLaneCount]; \
754 for (int i = 0; i < kLaneCount; i++) { \
755 lanes[i] = mask->get_lane(i) ? a->get_lane(i) : b->get_lane(i); \
756 } \
757 Handle<type> result = isolate->factory()->New##type(lanes); \
758 return *result; \
759 }
760
761 SIMD_SELECT_TYPES(SIMD_SELECT_FUNCTION)
762
763 //-------------------------------------------------------------------
764
765 // Casting functions.
766
767 #define SIMD_FROM_TYPES(FUNCTION) \
768 FUNCTION(Float32x4, float, 4, Int32x4, int32_t) \
769 FUNCTION(Int32x4, int32_t, 4, Float32x4, float)
770
771 #define SIMD_FROM_FUNCTION(type, lane_type, lane_count, from_type, from_ctype) \
772 RUNTIME_FUNCTION(Runtime_##type##From##from_type) { \
773 static const int kLaneCount = lane_count; \
774 HandleScope scope(isolate); \
775 DCHECK(args.length() == 1); \
776 CONVERT_ARG_HANDLE_CHECKED(from_type, a, 0); \
777 lane_type lanes[kLaneCount]; \
778 for (int i = 0; i < kLaneCount; i++) { \
779 from_ctype a_value = a->get_lane(i); \
780 RUNTIME_ASSERT(CanCast(a_value)); \
781 lanes[i] = static_cast<lane_type>(a_value); \
782 } \
783 Handle<type> result = isolate->factory()->New##type(lanes); \
784 return *result; \
785 }
786
787 SIMD_FROM_TYPES(SIMD_FROM_FUNCTION)
788
789 #define SIMD_FROM_BITS_TYPES(FUNCTION) \
790 FUNCTION(Float32x4, float, 4, Int32x4) \
791 FUNCTION(Float32x4, float, 4, Int16x8) \
792 FUNCTION(Float32x4, float, 4, Int8x16) \
793 FUNCTION(Int32x4, int32_t, 4, Float32x4) \
794 FUNCTION(Int32x4, int32_t, 4, Int16x8) \
795 FUNCTION(Int32x4, int32_t, 4, Int8x16) \
796 FUNCTION(Int16x8, int16_t, 8, Float32x4) \
797 FUNCTION(Int16x8, int16_t, 8, Int32x4) \
798 FUNCTION(Int16x8, int16_t, 8, Int8x16) \
799 FUNCTION(Int8x16, int8_t, 16, Float32x4) \
800 FUNCTION(Int8x16, int8_t, 16, Int32x4) \
801 FUNCTION(Int8x16, int8_t, 16, Int16x8)
802
803 #define SIMD_FROM_BITS_FUNCTION(type, lane_type, lane_count, from_type) \
804 RUNTIME_FUNCTION(Runtime_##type##From##from_type##Bits) { \
805 static const int kLaneCount = lane_count; \
806 HandleScope scope(isolate); \
807 DCHECK(args.length() == 1); \
808 CONVERT_ARG_HANDLE_CHECKED(from_type, a, 0); \
809 lane_type lanes[kLaneCount]; \
810 a->CopyBits(lanes); \
811 Handle<type> result = isolate->factory()->New##type(lanes); \
812 return *result; \
813 }
814
815 SIMD_FROM_BITS_TYPES(SIMD_FROM_BITS_FUNCTION)
816
817 //-------------------------------------------------------------------
818
819 // Unsigned extract functions.
820 // TODO(bbudge): remove when spec changes to include unsigned int types.
241 821
242 RUNTIME_FUNCTION(Runtime_Int16x8UnsignedExtractLane) { 822 RUNTIME_FUNCTION(Runtime_Int16x8UnsignedExtractLane) {
243 HandleScope scope(isolate); 823 HandleScope scope(isolate);
244 DCHECK(args.length() == 2); 824 DCHECK(args.length() == 2);
245 CONVERT_ARG_HANDLE_CHECKED(Int16x8, a, 0); 825 CONVERT_ARG_HANDLE_CHECKED(Int16x8, a, 0);
246 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, 8); 826 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, 8);
247 return *isolate->factory()->NewNumber(bit_cast<uint16_t>(a->get_lane(lane))); 827 return *isolate->factory()->NewNumber(bit_cast<uint16_t>(a->get_lane(lane)));
248 } 828 }
249 829
250 830
251 RUNTIME_FUNCTION(Runtime_Int8x16UnsignedExtractLane) { 831 RUNTIME_FUNCTION(Runtime_Int8x16UnsignedExtractLane) {
252 HandleScope scope(isolate); 832 HandleScope scope(isolate);
253 DCHECK(args.length() == 2); 833 DCHECK(args.length() == 2);
254 CONVERT_ARG_HANDLE_CHECKED(Int8x16, a, 0); 834 CONVERT_ARG_HANDLE_CHECKED(Int8x16, a, 0);
255 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, 16); 835 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, 16);
256 return *isolate->factory()->NewNumber(bit_cast<uint8_t>(a->get_lane(lane))); 836 return *isolate->factory()->NewNumber(bit_cast<uint8_t>(a->get_lane(lane)));
257 } 837 }
258
259
260 SIMD_REPLACE_NUMERIC_LANE_FUNCTION(Float32x4, float, 4)
261 SIMD_REPLACE_NUMERIC_LANE_FUNCTION(Int32x4, int32_t, 4)
262 SIMD_REPLACE_BOOLEAN_LANE_FUNCTION(Bool32x4, 4)
263 SIMD_REPLACE_NUMERIC_LANE_FUNCTION(Int16x8, int16_t, 8)
264 SIMD_REPLACE_BOOLEAN_LANE_FUNCTION(Bool16x8, 8)
265 SIMD_REPLACE_NUMERIC_LANE_FUNCTION(Int8x16, int8_t, 16)
266 SIMD_REPLACE_BOOLEAN_LANE_FUNCTION(Bool8x16, 16)
267 } // namespace internal 838 } // namespace internal
268 } // namespace v8 839 } // namespace v8
OLDNEW
« no previous file with comments | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698