OLD | NEW |
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/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.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/factory.h" | 10 #include "src/factory.h" |
11 #include "src/objects-inl.h" | 11 #include "src/objects-inl.h" |
12 | 12 |
13 // Implement Single Instruction Multiple Data (SIMD) operations as defined in | 13 // Implement Single Instruction Multiple Data (SIMD) operations as defined in |
14 // the SIMD.js draft spec: | 14 // the SIMD.js draft spec: |
15 // http://littledan.github.io/simd.html | 15 // http://littledan.github.io/simd.html |
16 | 16 |
17 namespace v8 { | 17 namespace v8 { |
18 namespace internal { | 18 namespace internal { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Functions to convert Numbers to SIMD component types. | 22 // Functions to convert Numbers to SIMD component types. |
23 | 23 |
| 24 template <typename T, typename F> |
| 25 static bool CanCast(F from) { |
| 26 // A float can't represent 2^31 - 1 or 2^32 - 1 exactly, so promote the limits |
| 27 // to double. Otherwise, the limit is truncated and numbers like 2^31 or 2^32 |
| 28 // get through, causing any static_cast to be undefined. |
| 29 return from >= static_cast<double>(std::numeric_limits<T>::min()) && |
| 30 from <= static_cast<double>(std::numeric_limits<T>::max()); |
| 31 } |
| 32 |
| 33 |
| 34 // Explicitly specialize for conversions to float, which always succeed. |
| 35 template <> |
| 36 bool CanCast<float>(int32_t from) { |
| 37 return true; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
24 template <typename T> | 42 template <typename T> |
25 static T ConvertNumber(double number); | 43 static T ConvertNumber(double number); |
26 | 44 |
27 | 45 |
28 template <> | 46 template <> |
29 float ConvertNumber<float>(double number) { | 47 float ConvertNumber<float>(double number) { |
30 return DoubleToFloat32(number); | 48 return DoubleToFloat32(number); |
31 } | 49 } |
32 | 50 |
33 | 51 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 122 } |
105 | 123 |
106 | 124 |
107 inline float MaxNumber(float a, float b) { | 125 inline float MaxNumber(float a, float b) { |
108 if (std::isnan(a)) return b; | 126 if (std::isnan(a)) return b; |
109 if (std::isnan(b)) return a; | 127 if (std::isnan(b)) return a; |
110 return Max(a, b); | 128 return Max(a, b); |
111 } | 129 } |
112 | 130 |
113 | 131 |
114 inline bool CanCast(int32_t a) { return true; } | |
115 | |
116 | |
117 inline bool CanCast(float a) { | |
118 return a > std::numeric_limits<int32_t>::min() && | |
119 a < std::numeric_limits<int32_t>::max(); | |
120 } | |
121 | |
122 } // namespace | |
123 | |
124 //------------------------------------------------------------------- | 132 //------------------------------------------------------------------- |
125 | 133 |
126 // SIMD helper functions. | 134 // SIMD helper functions. |
127 | 135 |
128 RUNTIME_FUNCTION(Runtime_IsSimdValue) { | 136 RUNTIME_FUNCTION(Runtime_IsSimdValue) { |
129 HandleScope scope(isolate); | 137 HandleScope scope(isolate); |
130 DCHECK(args.length() == 1); | 138 DCHECK(args.length() == 1); |
131 return isolate->heap()->ToBoolean(args[0]->IsSimd128Value()); | 139 return isolate->heap()->ToBoolean(args[0]->IsSimd128Value()); |
132 } | 140 } |
133 | 141 |
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 | 760 |
753 #define SIMD_FROM_FUNCTION(type, lane_type, lane_count, from_type, from_ctype) \ | 761 #define SIMD_FROM_FUNCTION(type, lane_type, lane_count, from_type, from_ctype) \ |
754 RUNTIME_FUNCTION(Runtime_##type##From##from_type) { \ | 762 RUNTIME_FUNCTION(Runtime_##type##From##from_type) { \ |
755 static const int kLaneCount = lane_count; \ | 763 static const int kLaneCount = lane_count; \ |
756 HandleScope scope(isolate); \ | 764 HandleScope scope(isolate); \ |
757 DCHECK(args.length() == 1); \ | 765 DCHECK(args.length() == 1); \ |
758 CONVERT_ARG_HANDLE_CHECKED(from_type, a, 0); \ | 766 CONVERT_ARG_HANDLE_CHECKED(from_type, a, 0); \ |
759 lane_type lanes[kLaneCount]; \ | 767 lane_type lanes[kLaneCount]; \ |
760 for (int i = 0; i < kLaneCount; i++) { \ | 768 for (int i = 0; i < kLaneCount; i++) { \ |
761 from_ctype a_value = a->get_lane(i); \ | 769 from_ctype a_value = a->get_lane(i); \ |
762 RUNTIME_ASSERT(CanCast(a_value)); \ | 770 RUNTIME_ASSERT(CanCast<lane_type>(a_value)); \ |
763 lanes[i] = static_cast<lane_type>(a_value); \ | 771 lanes[i] = static_cast<lane_type>(a_value); \ |
764 } \ | 772 } \ |
765 Handle<type> result = isolate->factory()->New##type(lanes); \ | 773 Handle<type> result = isolate->factory()->New##type(lanes); \ |
766 return *result; \ | 774 return *result; \ |
767 } | 775 } |
768 | 776 |
769 SIMD_FROM_TYPES(SIMD_FROM_FUNCTION) | 777 SIMD_FROM_TYPES(SIMD_FROM_FUNCTION) |
770 | 778 |
771 #define SIMD_FROM_BITS_TYPES(FUNCTION) \ | 779 #define SIMD_FROM_BITS_TYPES(FUNCTION) \ |
772 FUNCTION(Float32x4, float, 4, Int32x4) \ | 780 FUNCTION(Float32x4, float, 4, Int32x4) \ |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
812 | 820 |
813 RUNTIME_FUNCTION(Runtime_Int8x16UnsignedExtractLane) { | 821 RUNTIME_FUNCTION(Runtime_Int8x16UnsignedExtractLane) { |
814 HandleScope scope(isolate); | 822 HandleScope scope(isolate); |
815 DCHECK(args.length() == 2); | 823 DCHECK(args.length() == 2); |
816 CONVERT_ARG_HANDLE_CHECKED(Int8x16, a, 0); | 824 CONVERT_ARG_HANDLE_CHECKED(Int8x16, a, 0); |
817 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, 16); | 825 CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, 16); |
818 return *isolate->factory()->NewNumber(bit_cast<uint8_t>(a->get_lane(lane))); | 826 return *isolate->factory()->NewNumber(bit_cast<uint8_t>(a->get_lane(lane))); |
819 } | 827 } |
820 } // namespace internal | 828 } // namespace internal |
821 } // namespace v8 | 829 } // namespace v8 |
OLD | NEW |