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

Unified Diff: src/runtime/runtime-simd.cc

Issue 1160443009: Add SIMD.Float32x4 functions. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Tests working. Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/runtime/runtime.h ('k') | src/type-info.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/runtime/runtime-simd.cc
diff --git a/src/runtime/runtime-simd.cc b/src/runtime/runtime-simd.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5ae3abf4fd218083e2c5b6642c66df03fff39db5
--- /dev/null
+++ b/src/runtime/runtime-simd.cc
@@ -0,0 +1,298 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/v8.h"
+
+#include "src/arguments.h"
+#include "src/base/macros.h"
+#include "src/conversions.h"
+#include "src/runtime/runtime-utils.h"
+
+// Implement Single Instruction Multiple Data (SIMD) operations as defined in
+// the SIMD.js draft spec:
+// http://littledan.github.io/simd.html
+
+#define NumberToFloat32x4Component NumberToFloat
+
+#define CONVERT_SIMD_LANE_ARG_CHECKED(name, index, lanes) \
+ RUNTIME_ASSERT(args[index]->IsSmi()); \
+ int name = args.smi_at(index); \
+ RUNTIME_ASSERT(name >= 0 && name < lanes);
+
+#define SIMD_CHECK_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Check) { \
+ HandleScope scope(isolate); \
+ CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
+ return *a; \
+ }
+
+#define SIMD_EXTRACT_LANE_FUNCTION(type, lanes) \
+ RUNTIME_FUNCTION(Runtime_##type##ExtractLane) { \
+ HandleScope scope(isolate); \
+ DCHECK(args.length() == 2); \
+ CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, lanes); \
+ return *isolate->factory()->NewNumber(a->get_lane(lane)); \
+ }
+
+#define SIMD4_CREATE_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_Create##type) { \
+ HandleScope scope(isolate); \
+ DCHECK(args.length() == 4); \
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(w, 0); \
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(x, 1); \
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(y, 2); \
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(z, 3); \
+ return *isolate->factory()->NewFloat32x4( \
+ NumberTo##type##Component(*w), NumberTo##type##Component(*x), \
+ NumberTo##type##Component(*y), NumberTo##type##Component(*z)); \
+ }
+
+#define SIMD_CREATE_WRAPPER_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_New##type##Wrapper) { \
+ HandleScope scope(isolate); \
+ DCHECK(args.length() == 1); \
+ CONVERT_ARG_HANDLE_CHECKED(type, value, 0); \
+ return *Object::ToObject(isolate, value).ToHandleChecked(); \
+ }
+
+#define SIMD4_EXTRACT_LANE_FUNCTION(type) SIMD_EXTRACT_LANE_FUNCTION(type, 4)
+
+#define SIMD4_REPLACE_LANE_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##ReplaceLane) { \
+ HandleScope scope(isolate); \
+ DCHECK(args.length() == 3); \
+ CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(lane, 1, 4); \
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(replacement, 2); \
+ Handle<type> result = isolate->factory()->New##type( \
+ a->get_lane(0), a->get_lane(1), a->get_lane(2), a->get_lane(3)); \
+ result->set_lane(lane, NumberTo##type##Component(*replacement)); \
+ return *result; \
+ }
+
+#define SIMD4_UNARY_OP(type, a, op, result) \
+ DCHECK(args.length() == 1); \
+ CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
+ Handle<type> result = \
+ isolate->factory()->New##type(op(a->get_lane(0)), op(a->get_lane(1)), \
+ op(a->get_lane(2)), op(a->get_lane(3)));
+
+#define SIMD4_ABS_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Abs) { \
+ HandleScope scope(isolate); \
+ SIMD4_UNARY_OP(type, a, std::abs, result); \
+ return *result; \
+ }
+
+#define SIMD4_NEG_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Neg) { \
+ HandleScope scope(isolate); \
+ SIMD4_UNARY_OP(type, a, -, result); \
+ return *result; \
+ }
+
+#define SIMD4_SQRT_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Sqrt) { \
+ HandleScope scope(isolate); \
+ SIMD4_UNARY_OP(type, a, std::sqrt, result); \
+ return *result; \
+ }
+
+#define SIMD4_RECIP_APPROX_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##RecipApprox) { \
+ HandleScope scope(isolate); \
+ SIMD4_UNARY_OP(type, a, RecipApprox, result); \
+ return *result; \
+ }
+
+#define SIMD4_RECIP_SQRT_APPROX_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##RecipSqrtApprox) { \
+ HandleScope scope(isolate); \
+ SIMD4_UNARY_OP(type, a, RecipSqrtApprox, result); \
+ return *result; \
+ }
+
+#define SIMD4_BINARY_OP(type, a, b, op, result) \
+ DCHECK(args.length() == 2); \
+ CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
+ CONVERT_ARG_HANDLE_CHECKED(type, b, 1); \
+ Handle<type> result = isolate->factory()->New##type( \
+ op(a->get_lane(0), b->get_lane(0)), op(a->get_lane(1), b->get_lane(1)), \
+ op(a->get_lane(2), b->get_lane(2)), op(a->get_lane(3), b->get_lane(3)));
+
+// Macros to make infix arithmetic operators look like f(a, b).
+#define BINARY_ADD(a, b) (a) + (b)
+#define BINARY_SUB(a, b) (a) - (b)
+#define BINARY_MUL(a, b) (a) * (b)
+#define BINARY_DIV(a, b) (a) / (b)
+
+#define SIMD4_ADD_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Add) { \
+ HandleScope scope(isolate); \
+ SIMD4_BINARY_OP(type, a, b, BINARY_ADD, result); \
+ return *result; \
+ }
+
+#define SIMD4_SUB_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Sub) { \
+ HandleScope scope(isolate); \
+ SIMD4_BINARY_OP(type, a, b, BINARY_SUB, result); \
+ return *result; \
+ }
+
+#define SIMD4_MUL_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Mul) { \
+ HandleScope scope(isolate); \
+ SIMD4_BINARY_OP(type, a, b, BINARY_MUL, result); \
+ return *result; \
+ }
+
+#define SIMD4_DIV_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Div) { \
+ HandleScope scope(isolate); \
+ SIMD4_BINARY_OP(type, a, b, BINARY_DIV, result); \
+ return *result; \
+ }
+
+#define SIMD4_MIN_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Min) { \
+ HandleScope scope(isolate); \
+ SIMD4_BINARY_OP(type, a, b, Min, result); \
+ return *result; \
+ }
+
+#define SIMD4_MAX_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Max) { \
+ HandleScope scope(isolate); \
+ SIMD4_BINARY_OP(type, a, b, Max, result); \
+ return *result; \
+ }
+
+#define SIMD4_MINNUM_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##MinNum) { \
+ HandleScope scope(isolate); \
+ SIMD4_BINARY_OP(type, a, b, MinNumber, result); \
+ return *result; \
+ }
+
+#define SIMD4_MAXNUM_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##MaxNum) { \
+ HandleScope scope(isolate); \
+ SIMD4_BINARY_OP(type, a, b, MaxNumber, result); \
+ return *result; \
+ }
+
+#define SIMD4_SWIZZLE_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Swizzle) { \
+ HandleScope scope(isolate); \
+ DCHECK(args.length() == 5); \
+ CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(w, 1, 4); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(x, 2, 4); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(y, 3, 4); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(z, 4, 4); \
+ Handle<type> result = isolate->factory()->New##type( \
+ a->get_lane(w), a->get_lane(x), a->get_lane(y), a->get_lane(z)); \
+ return *result; \
+ }
+
+#define SIMD4_SHUFFLE_FUNCTION(type) \
+ RUNTIME_FUNCTION(Runtime_##type##Shuffle) { \
+ HandleScope scope(isolate); \
+ DCHECK(args.length() == 6); \
+ CONVERT_ARG_HANDLE_CHECKED(type, a, 0); \
+ CONVERT_ARG_HANDLE_CHECKED(type, b, 1); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(w, 2, 8); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(x, 3, 8); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(y, 4, 8); \
+ CONVERT_SIMD_LANE_ARG_CHECKED(z, 5, 8); \
+ float values[8]; \
+ values[0] = a->get_lane(0); \
+ values[1] = a->get_lane(1); \
+ values[2] = a->get_lane(2); \
+ values[3] = a->get_lane(3); \
+ values[4] = b->get_lane(0); \
+ values[5] = b->get_lane(1); \
+ values[6] = b->get_lane(2); \
+ values[7] = b->get_lane(3); \
+ Handle<type> result = isolate->factory()->New##type(values[w], values[x], \
+ values[y], values[z]); \
+ return *result; \
+ }
+
+#define SIMD4_FUNCTIONS(type) \
+ SIMD4_CREATE_FUNCTION(type) \
+ SIMD_CREATE_WRAPPER_FUNCTION(type) \
+ SIMD_CHECK_FUNCTION(type) \
+ SIMD4_EXTRACT_LANE_FUNCTION(type) \
+ SIMD4_REPLACE_LANE_FUNCTION(type) \
+ SIMD4_ABS_FUNCTION(type) \
+ SIMD4_NEG_FUNCTION(type) \
+ SIMD4_SQRT_FUNCTION(type) \
+ SIMD4_RECIP_APPROX_FUNCTION(type) \
+ SIMD4_RECIP_SQRT_APPROX_FUNCTION(type) \
+ SIMD4_ADD_FUNCTION(type) \
+ SIMD4_SUB_FUNCTION(type) \
+ SIMD4_MUL_FUNCTION(type) \
+ SIMD4_DIV_FUNCTION(type) \
+ SIMD4_MIN_FUNCTION(type) \
+ SIMD4_MAX_FUNCTION(type) \
+ SIMD4_MINNUM_FUNCTION(type) \
+ SIMD4_MAXNUM_FUNCTION(type) \
+ SIMD4_SWIZZLE_FUNCTION(type) \
+ SIMD4_SHUFFLE_FUNCTION(type)
+
+
+namespace v8 {
+namespace internal {
+
+namespace {
+
+// Convert from Number object to float.
+inline float NumberToFloat(Object* number) {
+ // Don't bother checking for Smi, we might still overflow a float.
+ return DoubleToFloat32(number->Number());
+}
+
+
+inline float RecipApprox(float a) { return 1.0f / a; }
+
+
+inline float RecipSqrtApprox(float a) { return 1.0f / std::sqrt(a); }
+
+
+inline float Min(float a, float b) {
+ if (a < b) return a;
+ if (a > b) return b;
+ if (a == b) return std::signbit(a) ? a : b;
+ return std::numeric_limits<float>::quiet_NaN();
+}
+
+
+inline float Max(float a, float b) {
+ if (a > b) return a;
+ if (a < b) return b;
+ if (a == b) return std::signbit(b) ? a : b;
+ return std::numeric_limits<float>::quiet_NaN();
+}
+
+
+inline float MinNumber(float a, float b) {
+ if (a != a) return b;
+ if (b != b) return a;
+ return Min(a, b);
+}
+
+
+inline float MaxNumber(float a, float b) {
+ if (a != a) return b;
+ if (b != b) return a;
+ return Max(a, b);
+}
+} // namespace
+
+SIMD4_FUNCTIONS(Float32x4)
+}
+} // namespace v8::internal
« no previous file with comments | « src/runtime/runtime.h ('k') | src/type-info.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698