[simd.js] Macro-ize more SIMD code.
authorbbudge <bbudge@chromium.org>
Tue, 18 Aug 2015 20:39:43 +0000 (13:39 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 18 Aug 2015 20:39:55 +0000 (20:39 +0000)
Use macros for SIMD get/set_lane, and print fns.

LOG=N
BUG=v8:4124

Review URL: https://codereview.chromium.org/1302513004

Cr-Commit-Position: refs/heads/master@{#30239}

src/objects-inl.h
src/objects-printer.cc

index cc1ae85..b3713b6 100644 (file)
@@ -1258,6 +1258,18 @@ MaybeHandle<Object> Object::GetProperty(Isolate* isolate, Handle<Object> object,
 #define WRITE_INTPTR_FIELD(p, offset, value) \
   (*reinterpret_cast<intptr_t*>(FIELD_ADDR(p, offset)) = value)
 
+#define READ_UINT8_FIELD(p, offset) \
+  (*reinterpret_cast<const uint8_t*>(FIELD_ADDR_CONST(p, offset)))
+
+#define WRITE_UINT8_FIELD(p, offset, value) \
+  (*reinterpret_cast<uint8_t*>(FIELD_ADDR(p, offset)) = value)
+
+#define READ_INT8_FIELD(p, offset) \
+  (*reinterpret_cast<const int8_t*>(FIELD_ADDR_CONST(p, offset)))
+
+#define WRITE_INT8_FIELD(p, offset, value) \
+  (*reinterpret_cast<int8_t*>(FIELD_ADDR(p, offset)) = value)
+
 #define READ_UINT16_FIELD(p, offset) \
   (*reinterpret_cast<const uint16_t*>(FIELD_ADDR_CONST(p, offset)))
 
@@ -1544,184 +1556,71 @@ SIMD128_TYPES(SIMD128_VALUE_EQUALS)
 #undef SIMD128_VALUE_EQUALS
 
 
-float Float32x4::get_lane(int lane) const {
-  DCHECK(lane < 4 && lane >= 0);
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  return READ_FLOAT_FIELD(this, kValueOffset + lane * kFloatSize);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  return READ_FLOAT_FIELD(this, kValueOffset + (3 - lane) * kFloatSize);
-#else
-#error Unknown byte ordering
-#endif
-}
-
-
-void Float32x4::set_lane(int lane, float value) {
-  DCHECK(lane < 4 && lane >= 0);
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  WRITE_FLOAT_FIELD(this, kValueOffset + lane * kFloatSize, value);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  WRITE_FLOAT_FIELD(this, kValueOffset + (3 - lane) * kFloatSize, value);
-#else
-#error Unknown byte ordering
-#endif
-}
-
-
-int32_t Int32x4::get_lane(int lane) const {
-  DCHECK(lane < 4 && lane >= 0);
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  return READ_INT32_FIELD(this, kValueOffset + lane * kInt32Size);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  return READ_INT32_FIELD(this, kValueOffset + (3 - lane) * kInt32Size);
-#else
-#error Unknown byte ordering
-#endif
-}
-
-
-void Int32x4::set_lane(int lane, int32_t value) {
-  DCHECK(lane < 4 && lane >= 0);
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  WRITE_INT32_FIELD(this, kValueOffset + lane * kInt32Size, value);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  WRITE_INT32_FIELD(this, kValueOffset + (3 - lane) * kInt32Size, value);
-#else
-#error Unknown byte ordering
-#endif
-}
-
-
-bool Bool32x4::get_lane(int lane) const {
-  DCHECK(lane < 4 && lane >= 0);
-  int32_t value;
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  value = READ_INT32_FIELD(this, kValueOffset + lane * kInt32Size);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  value = READ_INT32_FIELD(this, kValueOffset + (3 - lane) * kInt32Size);
-#else
-#error Unknown byte ordering
-#endif
-  DCHECK(value == 0 || value == -1);
-  return value != 0;
-}
-
-
-void Bool32x4::set_lane(int lane, bool value) {
-  DCHECK(lane < 4 && lane >= 0);
-  int32_t int_val = value ? -1 : 0;
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  WRITE_INT32_FIELD(this, kValueOffset + lane * kInt32Size, int_val);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  WRITE_INT32_FIELD(this, kValueOffset + (3 - lane) * kInt32Size, int_val);
-#else
-#error Unknown byte ordering
-#endif
-}
-
-
-int16_t Int16x8::get_lane(int lane) const {
-  DCHECK(lane < 8 && lane >= 0);
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  return READ_INT16_FIELD(this, kValueOffset + lane * kShortSize);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  return READ_INT16_FIELD(this, kValueOffset + (7 - lane) * kShortSize);
-#else
-#error Unknown byte ordering
-#endif
-}
-
-
-void Int16x8::set_lane(int lane, int16_t value) {
-  DCHECK(lane < 8 && lane >= 0);
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  WRITE_INT16_FIELD(this, kValueOffset + lane * kShortSize, value);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  WRITE_INT16_FIELD(this, kValueOffset + (7 - lane) * kShortSize, value);
-#else
-#error Unknown byte ordering
-#endif
-}
-
-
-bool Bool16x8::get_lane(int lane) const {
-  DCHECK(lane < 8 && lane >= 0);
-  int16_t value;
 #if defined(V8_TARGET_LITTLE_ENDIAN)
-  return READ_INT16_FIELD(this, kValueOffset + lane * kShortSize);
+#define SIMD128_READ_LANE(lane_type, lane_count, field_type, field_size) \
+  lane_type value =                                                      \
+      READ_##field_type##_FIELD(this, kValueOffset + lane * field_size);
 #elif defined(V8_TARGET_BIG_ENDIAN)
-  return READ_INT16_FIELD(this, kValueOffset + (7 - lane) * kShortSize);
+#define SIMD128_READ_LANE(lane_type, lane_count, field_type, field_size) \
+  lane_type value = READ_##field_type##_FIELD(                           \
+      this, kValueOffset + (lane_count - lane - 1) * field_size);
 #else
 #error Unknown byte ordering
 #endif
-  DCHECK(value == 0 || value == -1);
-  return value != 0;
-}
-
 
-void Bool16x8::set_lane(int lane, bool value) {
-  DCHECK(lane < 8 && lane >= 0);
-  int16_t int_val = value ? -1 : 0;
 #if defined(V8_TARGET_LITTLE_ENDIAN)
-  WRITE_INT16_FIELD(this, kValueOffset + lane * kShortSize, int_val);
+#define SIMD128_WRITE_LANE(lane_count, field_type, field_size, value) \
+  WRITE_##field_type##_FIELD(this, kValueOffset + lane * field_size, value);
 #elif defined(V8_TARGET_BIG_ENDIAN)
-  WRITE_INT16_FIELD(this, kValueOffset + (7 - lane) * kShortSize, int_val);
+#define SIMD128_WRITE_LANE(lane_count, field_type, field_size, value) \
+  WRITE_##field_type##_FIELD(                                         \
+      this, kValueOffset + (lane_count - lane - 1) * field_size, value);
 #else
 #error Unknown byte ordering
 #endif
-}
 
-
-int8_t Int8x16::get_lane(int lane) const {
-  DCHECK(lane < 16 && lane >= 0);
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  return READ_BYTE_FIELD(this, kValueOffset + lane * kCharSize);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  return READ_BYTE_FIELD(this, kValueOffset + (15 - lane) * kCharSize);
-#else
-#error Unknown byte ordering
-#endif
-}
-
-
-void Int8x16::set_lane(int lane, int8_t value) {
-  DCHECK(lane < 16 && lane >= 0);
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  WRITE_BYTE_FIELD(this, kValueOffset + lane * kCharSize, value);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  WRITE_BYTE_FIELD(this, kValueOffset + (15 - lane) * kCharSize, value);
-#else
-#error Unknown byte ordering
-#endif
-}
-
-
-bool Bool8x16::get_lane(int lane) const {
-  DCHECK(lane < 16 && lane >= 0);
-  int8_t value;
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  value = READ_BYTE_FIELD(this, kValueOffset + lane * kCharSize);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  value = READ_BYTE_FIELD(this, kValueOffset + (15 - lane) * kCharSize);
-#else
-#error Unknown byte ordering
-#endif
-  DCHECK(value == 0 || value == -1);
-  return value != 0;
-}
-
-
-void Bool8x16::set_lane(int lane, bool value) {
-  DCHECK(lane < 16 && lane >= 0);
-  int8_t int_val = value ? -1 : 0;
-#if defined(V8_TARGET_LITTLE_ENDIAN)
-  WRITE_BYTE_FIELD(this, kValueOffset + lane * kCharSize, int_val);
-#elif defined(V8_TARGET_BIG_ENDIAN)
-  WRITE_BYTE_FIELD(this, kValueOffset + (15 - lane) * kCharSize, int_val);
-#else
-#error Unknown byte ordering
-#endif
-}
+#define SIMD128_NUMERIC_LANE_FNS(type, lane_type, lane_count, field_type, \
+                                 field_size)                              \
+  lane_type type::get_lane(int lane) const {                              \
+    DCHECK(lane < lane_count && lane >= 0);                               \
+    SIMD128_READ_LANE(lane_type, lane_count, field_type, field_size)      \
+    return value;                                                         \
+  }                                                                       \
+                                                                          \
+  void type::set_lane(int lane, lane_type value) {                        \
+    DCHECK(lane < lane_count && lane >= 0);                               \
+    SIMD128_WRITE_LANE(lane_count, field_type, field_size, value)         \
+  }
+
+SIMD128_NUMERIC_LANE_FNS(Float32x4, float, 4, FLOAT, kFloatSize)
+SIMD128_NUMERIC_LANE_FNS(Int32x4, int32_t, 4, INT32, kInt32Size)
+SIMD128_NUMERIC_LANE_FNS(Int16x8, int16_t, 8, INT16, kShortSize)
+SIMD128_NUMERIC_LANE_FNS(Int8x16, int8_t, 16, INT8, kCharSize)
+#undef SIMD128_NUMERIC_LANE_FNS
+
+
+#define SIMD128_BOOLEAN_LANE_FNS(type, lane_type, lane_count, field_type, \
+                                 field_size)                              \
+  bool type::get_lane(int lane) const {                                   \
+    DCHECK(lane < lane_count && lane >= 0);                               \
+    SIMD128_READ_LANE(lane_type, lane_count, field_type, field_size)      \
+    DCHECK(value == 0 || value == -1);                                    \
+    return value != 0;                                                    \
+  }                                                                       \
+                                                                          \
+  void type::set_lane(int lane, bool value) {                             \
+    DCHECK(lane < lane_count && lane >= 0);                               \
+    int32_t int_val = value ? -1 : 0;                                     \
+    SIMD128_WRITE_LANE(lane_count, field_type, field_size, int_val)       \
+  }
+
+SIMD128_BOOLEAN_LANE_FNS(Bool32x4, int32_t, 4, INT32, kInt32Size)
+SIMD128_BOOLEAN_LANE_FNS(Bool16x8, int16_t, 8, INT16, kShortSize)
+SIMD128_BOOLEAN_LANE_FNS(Bool8x16, int8_t, 16, INT8, kCharSize)
+#undef SIMD128_BOOLEAN_LANE_FNS
+
+#undef SIMD128_READ_LANE
+#undef SIMD128_WRITE_LANE
 
 
 ACCESSORS(JSObject, properties, FixedArray, kPropertiesOffset)
@@ -7913,6 +7812,10 @@ String::SubStringRange::iterator String::SubStringRange::end() {
 #undef WRITE_INT_FIELD
 #undef READ_INTPTR_FIELD
 #undef WRITE_INTPTR_FIELD
+#undef READ_UINT8_FIELD
+#undef WRITE_UINT8_FIELD
+#undef READ_INT8_FIELD
+#undef WRITE_INT8_FIELD
 #undef READ_UINT16_FIELD
 #undef WRITE_UINT16_FIELD
 #undef READ_INT16_FIELD
index 3a5bb80..799561e 100644 (file)
@@ -210,62 +210,34 @@ void Float32x4::Float32x4Print(std::ostream& os) {  // NOLINT
 }
 
 
-void Int32x4::Int32x4Print(std::ostream& os) {  // NOLINT
-  char arr[100];
-  Vector<char> buffer(arr, arraysize(arr));
-  os << std::string(IntToCString(get_lane(0), buffer)) << ", "
-     << std::string(IntToCString(get_lane(1), buffer)) << ", "
-     << std::string(IntToCString(get_lane(2), buffer)) << ", "
-     << std::string(IntToCString(get_lane(3), buffer));
-}
-
-
-void Bool32x4::Bool32x4Print(std::ostream& os) {  // NOLINT
-  os << std::string(get_lane(0) ? "true" : "false") << ", "
-     << std::string(get_lane(1) ? "true" : "false") << ", "
-     << std::string(get_lane(2) ? "true" : "false") << ", "
-     << std::string(get_lane(3) ? "true" : "false");
-}
-
-
-void Int16x8::Int16x8Print(std::ostream& os) {  // NOLINT
-  char arr[100];
-  Vector<char> buffer(arr, arraysize(arr));
-  os << std::string(IntToCString(get_lane(0), buffer));
-  for (int i = 1; i < 8; i++) {
-    os << ", " << std::string(IntToCString(get_lane(i), buffer));
-  }
-}
-
-
-void Bool16x8::Bool16x8Print(std::ostream& os) {  // NOLINT
-  char arr[100];
-  Vector<char> buffer(arr, arraysize(arr));
-  os << std::string(get_lane(0) ? "true" : "false");
-  for (int i = 1; i < 8; i++) {
-    os << ", " << std::string(get_lane(i) ? "true" : "false");
-  }
-}
-
-
-void Int8x16::Int8x16Print(std::ostream& os) {  // NOLINT
-  char arr[100];
-  Vector<char> buffer(arr, arraysize(arr));
-  os << std::string(IntToCString(get_lane(0), buffer));
-  for (int i = 1; i < 16; i++) {
-    os << ", " << std::string(IntToCString(get_lane(i), buffer));
+#define SIMD128_INT_PRINT_FUNCTION(type, lane_count)                \
+  void type::type##Print(std::ostream& os) {                        \
+    char arr[100];                                                  \
+    Vector<char> buffer(arr, arraysize(arr));                       \
+    os << std::string(IntToCString(get_lane(0), buffer));           \
+    for (int i = 1; i < lane_count; i++) {                          \
+      os << ", " << std::string(IntToCString(get_lane(i), buffer)); \
+    }                                                               \
   }
-}
-
-
-void Bool8x16::Bool8x16Print(std::ostream& os) {  // NOLINT
-  char arr[100];
-  Vector<char> buffer(arr, arraysize(arr));
-  os << std::string(get_lane(0) ? "true" : "false");
-  for (int i = 1; i < 16; i++) {
-    os << ", " << std::string(get_lane(i) ? "true" : "false");
+SIMD128_INT_PRINT_FUNCTION(Int32x4, 4)
+SIMD128_INT_PRINT_FUNCTION(Int16x8, 8)
+SIMD128_INT_PRINT_FUNCTION(Int8x16, 16)
+#undef SIMD128_INT_PRINT_FUNCTION
+
+
+#define SIMD128_BOOL_PRINT_FUNCTION(type, lane_count)            \
+  void type::type##Print(std::ostream& os) {                     \
+    char arr[100];                                               \
+    Vector<char> buffer(arr, arraysize(arr));                    \
+    os << std::string(get_lane(0) ? "true" : "false");           \
+    for (int i = 1; i < lane_count; i++) {                       \
+      os << ", " << std::string(get_lane(i) ? "true" : "false"); \
+    }                                                            \
   }
-}
+SIMD128_BOOL_PRINT_FUNCTION(Bool32x4, 4)
+SIMD128_BOOL_PRINT_FUNCTION(Bool16x8, 8)
+SIMD128_BOOL_PRINT_FUNCTION(Bool8x16, 16)
+#undef SIMD128_BOOL_PRINT_FUNCTION
 
 
 void ByteArray::ByteArrayPrint(std::ostream& os) {  // NOLINT