summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/compiler/test-run-properties.cc
blob: b7677f7fd207fbeafb2b3a7eb8690d2bc9bbf24d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2014 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 "test/cctest/compiler/function-tester.h"

using namespace v8::internal;
using namespace v8::internal::compiler;

template <typename U>
static void TypedArrayLoadHelper(const char* array_type) {
  static const uint32_t kValues[] = {
      0x00000000, 0x00000001, 0x00000023, 0x00000042, 0x12345678, 0x87654321,
      0x0000003f, 0x0000007f, 0x00003fff, 0x00007fff, 0x3fffffff, 0x7fffffff,
      0x000000ff, 0x00000080, 0x0000ffff, 0x00008000, 0xffffffff, 0x80000000};
  EmbeddedVector<char, 1024> values_buffer;
  StringBuilder values_builder(values_buffer.start(), values_buffer.length());
  for (size_t i = 0; i < arraysize(kValues); ++i) {
    values_builder.AddFormatted("a[%d] = 0x%08x;", i, kValues[i]);
  }

  // Note that below source creates two different typed arrays with the same
  // elements kind to get coverage for both (on heap / with external backing
  // store) access patterns.
  const char* source =
      "(function(a) {"
      "  var x = (a = new %sArray(%d)); %s;"
      "  var y = (a = new %sArray(%d)); %s; %%TypedArrayGetBuffer(y);"
      "  if (!%%HasFixed%sElements(x)) %%AbortJS('x');"
      "  if (!%%HasFixed%sElements(y)) %%AbortJS('y');"
      "  function f(a,b) {"
      "    a = a | 0; b = b | 0;"
      "    return x[a] + y[b];"
      "  }"
      "  return f;"
      "})()";
  EmbeddedVector<char, 1024> source_buffer;
  SNPrintF(source_buffer, source, array_type, arraysize(kValues),
           values_buffer.start(), array_type, arraysize(kValues),
           values_buffer.start(), array_type, array_type);

  FunctionTester T(
      source_buffer.start(),
      CompilationInfo::kContextSpecializing | CompilationInfo::kTypingEnabled);
  for (size_t i = 0; i < arraysize(kValues); ++i) {
    for (size_t j = 0; j < arraysize(kValues); ++j) {
      volatile U value_a = static_cast<U>(kValues[i]);
      volatile U value_b = static_cast<U>(kValues[j]);
      double expected =
          static_cast<double>(value_a) + static_cast<double>(value_b);
      T.CheckCall(T.Val(expected), T.Val(static_cast<double>(i)),
                  T.Val(static_cast<double>(j)));
    }
  }
}


TEST(TypedArrayLoad) {
  FLAG_typed_array_max_size_in_heap = 256;
  TypedArrayLoadHelper<int8_t>("Int8");
  TypedArrayLoadHelper<uint8_t>("Uint8");
  TypedArrayLoadHelper<int16_t>("Int16");
  TypedArrayLoadHelper<uint16_t>("Uint16");
  TypedArrayLoadHelper<int32_t>("Int32");
  TypedArrayLoadHelper<uint32_t>("Uint32");
  TypedArrayLoadHelper<float>("Float32");
  TypedArrayLoadHelper<double>("Float64");
  // TODO(mstarzinger): Add tests for ClampedUint8.
}


template <typename U>
static void TypedArrayStoreHelper(const char* array_type) {
  static const uint32_t kValues[] = {
      0x00000000, 0x00000001, 0x00000023, 0x00000042, 0x12345678, 0x87654321,
      0x0000003f, 0x0000007f, 0x00003fff, 0x00007fff, 0x3fffffff, 0x7fffffff,
      0x000000ff, 0x00000080, 0x0000ffff, 0x00008000, 0xffffffff, 0x80000000};
  EmbeddedVector<char, 1024> values_buffer;
  StringBuilder values_builder(values_buffer.start(), values_buffer.length());
  for (size_t i = 0; i < arraysize(kValues); ++i) {
    values_builder.AddFormatted("a[%d] = 0x%08x;", i, kValues[i]);
  }

  // Note that below source creates two different typed arrays with the same
  // elements kind to get coverage for both (on heap/with external backing
  // store) access patterns.
  const char* source =
      "(function(a) {"
      "  var x = (a = new %sArray(%d)); %s;"
      "  var y = (a = new %sArray(%d)); %s; %%TypedArrayGetBuffer(y);"
      "  if (!%%HasFixed%sElements(x)) %%AbortJS('x');"
      "  if (!%%HasFixed%sElements(y)) %%AbortJS('y');"
      "  function f(a,b) {"
      "    a = a | 0; b = b | 0;"
      "    var t = x[a];"
      "    x[a] = y[b];"
      "    y[b] = t;"
      "    t = y[b];"
      "    y[b] = x[a];"
      "    x[a] = t;"
      "    return x[a] + y[b];"
      "  }"
      "  return f;"
      "})()";
  EmbeddedVector<char, 2048> source_buffer;
  SNPrintF(source_buffer, source, array_type, arraysize(kValues),
           values_buffer.start(), array_type, arraysize(kValues),
           values_buffer.start(), array_type, array_type);

  FunctionTester T(
      source_buffer.start(),
      CompilationInfo::kContextSpecializing | CompilationInfo::kTypingEnabled);
  for (size_t i = 0; i < arraysize(kValues); ++i) {
    for (size_t j = 0; j < arraysize(kValues); ++j) {
      volatile U value_a = static_cast<U>(kValues[i]);
      volatile U value_b = static_cast<U>(kValues[j]);
      double expected =
          static_cast<double>(value_a) + static_cast<double>(value_b);
      T.CheckCall(T.Val(expected), T.Val(static_cast<double>(i)),
                  T.Val(static_cast<double>(j)));
    }
  }
}


TEST(TypedArrayStore) {
  FLAG_typed_array_max_size_in_heap = 256;
  TypedArrayStoreHelper<int8_t>("Int8");
  TypedArrayStoreHelper<uint8_t>("Uint8");
  TypedArrayStoreHelper<int16_t>("Int16");
  TypedArrayStoreHelper<uint16_t>("Uint16");
  TypedArrayStoreHelper<int32_t>("Int32");
  TypedArrayStoreHelper<uint32_t>("Uint32");
  TypedArrayStoreHelper<float>("Float32");
  TypedArrayStoreHelper<double>("Float64");
  // TODO(mstarzinger): Add tests for ClampedUint8.
}