Refactor holey and packed elements-kind check

Bug: v8:6831
Change-Id: Idbfc2ddb46e258eee2ef23528531b666a532d72c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1599775
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Commit-Queue: Z Nguyen-Huu <duongn@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#61328}
diff --git a/src/elements-kind.h b/src/elements-kind.h
index 6bd2a6d..e916361 100644
--- a/src/elements-kind.h
+++ b/src/elements-kind.h
@@ -199,22 +199,19 @@
 }
 
 inline bool IsHoleyElementsKind(ElementsKind kind) {
-  return kind == HOLEY_SMI_ELEMENTS || kind == HOLEY_DOUBLE_ELEMENTS ||
-         kind == HOLEY_ELEMENTS;
+  return kind % 2 == 1 && kind <= HOLEY_DOUBLE_ELEMENTS;
 }
 
 inline bool IsHoleyElementsKindForRead(ElementsKind kind) {
-  return IsHoleyElementsKind(kind) || IsHoleyFrozenOrSealedElementsKind(kind);
+  return kind % 2 == 1 && kind <= HOLEY_FROZEN_ELEMENTS;
 }
 
 inline bool IsHoleyOrDictionaryElementsKind(ElementsKind kind) {
   return IsHoleyElementsKindForRead(kind) || kind == DICTIONARY_ELEMENTS;
 }
 
-
 inline bool IsFastPackedElementsKind(ElementsKind kind) {
-  return kind == PACKED_SMI_ELEMENTS || kind == PACKED_DOUBLE_ELEMENTS ||
-         kind == PACKED_ELEMENTS;
+  return kind % 2 == 0 && kind <= PACKED_DOUBLE_ELEMENTS;
 }
 
 
diff --git a/test/cctest/test-elements-kind.cc b/test/cctest/test-elements-kind.cc
index 55ab919..06510e2 100644
--- a/test/cctest/test-elements-kind.cc
+++ b/test/cctest/test-elements-kind.cc
@@ -58,6 +58,41 @@
   return EQUALS(isolate, handle(left, isolate), right);
 }
 
+bool ElementsKindIsHoleyElementsKindForRead(ElementsKind kind) {
+  switch (kind) {
+    case ElementsKind::HOLEY_SMI_ELEMENTS:
+    case ElementsKind::HOLEY_ELEMENTS:
+    case ElementsKind::HOLEY_DOUBLE_ELEMENTS:
+    case ElementsKind::HOLEY_SEALED_ELEMENTS:
+    case ElementsKind::HOLEY_FROZEN_ELEMENTS:
+      return true;
+    default:
+      return false;
+  }
+}
+
+bool ElementsKindIsHoleyElementsKind(ElementsKind kind) {
+  switch (kind) {
+    case ElementsKind::HOLEY_SMI_ELEMENTS:
+    case ElementsKind::HOLEY_ELEMENTS:
+    case ElementsKind::HOLEY_DOUBLE_ELEMENTS:
+      return true;
+    default:
+      return false;
+  }
+}
+
+bool ElementsKindIsFastPackedElementsKind(ElementsKind kind) {
+  switch (kind) {
+    case ElementsKind::PACKED_SMI_ELEMENTS:
+    case ElementsKind::PACKED_ELEMENTS:
+    case ElementsKind::PACKED_DOUBLE_ELEMENTS:
+      return true;
+    default:
+      return false;
+  }
+}
+
 }  // namespace
 
 
@@ -488,6 +523,29 @@
   CHECK_EQ(array->map(), *previous_map);
 }
 
+TEST(IsHoleyElementsKindForRead) {
+  for (int i = 0; i <= ElementsKind::LAST_ELEMENTS_KIND; i++) {
+    ElementsKind kind = static_cast<ElementsKind>(i);
+    CHECK_EQ(ElementsKindIsHoleyElementsKindForRead(kind),
+             IsHoleyElementsKindForRead(kind));
+  }
+}
+
+TEST(IsHoleyElementsKind) {
+  for (int i = 0; i <= ElementsKind::LAST_ELEMENTS_KIND; i++) {
+    ElementsKind kind = static_cast<ElementsKind>(i);
+    CHECK_EQ(ElementsKindIsHoleyElementsKind(kind), IsHoleyElementsKind(kind));
+  }
+}
+
+TEST(IsFastPackedElementsKind) {
+  for (int i = 0; i <= ElementsKind::LAST_ELEMENTS_KIND; i++) {
+    ElementsKind kind = static_cast<ElementsKind>(i);
+    CHECK_EQ(ElementsKindIsFastPackedElementsKind(kind),
+             IsFastPackedElementsKind(kind));
+  }
+}
+
 }  // namespace test_elements_kind
 }  // namespace internal
 }  // namespace v8