OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "test/cctest/compiler/function-tester.h" | 7 #include "test/cctest/compiler/function-tester.h" |
8 | 8 |
9 using namespace v8::internal; | 9 using namespace v8::internal; |
10 using namespace v8::internal::compiler; | 10 using namespace v8::internal::compiler; |
11 | 11 |
12 template <typename U> | 12 template <typename U> |
13 static void TypedArrayLoadHelper(const char* array_type) { | 13 static void TypedArrayLoadHelper(const char* array_type) { |
14 const int64_t values[] = { | 14 static const uint32_t kValues[] = { |
15 0x00000000, 0x00000001, 0x00000023, 0x00000042, 0x12345678, 0x87654321, | 15 0x00000000, 0x00000001, 0x00000023, 0x00000042, 0x12345678, 0x87654321, |
16 0x0000003f, 0x0000007f, 0x00003fff, 0x00007fff, 0x3fffffff, 0x7fffffff, | 16 0x0000003f, 0x0000007f, 0x00003fff, 0x00007fff, 0x3fffffff, 0x7fffffff, |
17 0x000000ff, 0x00000080, 0x0000ffff, 0x00008000, 0xffffffff, 0x80000000, | 17 0x000000ff, 0x00000080, 0x0000ffff, 0x00008000, 0xffffffff, 0x80000000}; |
18 }; | |
19 size_t size = arraysize(values); | |
20 EmbeddedVector<char, 1024> values_buffer; | 18 EmbeddedVector<char, 1024> values_buffer; |
21 StringBuilder values_builder(values_buffer.start(), values_buffer.length()); | 19 StringBuilder values_builder(values_buffer.start(), values_buffer.length()); |
22 for (unsigned i = 0; i < size; i++) { | 20 for (size_t i = 0; i < arraysize(kValues); ++i) { |
23 values_builder.AddFormatted("a[%d] = 0x%08x;", i, values[i]); | 21 values_builder.AddFormatted("a[%d] = 0x%08x;", i, kValues[i]); |
24 } | 22 } |
25 | 23 |
26 // Note that below source creates two different typed arrays with distinct | 24 // Note that below source creates two different typed arrays with distinct |
27 // elements kind to get coverage for both access patterns: | 25 // elements kind to get coverage for both access patterns: |
28 // - IsFixedTypedArrayElementsKind(x) | 26 // - IsFixedTypedArrayElementsKind(x) |
29 // - IsExternalArrayElementsKind(y) | 27 // - IsExternalArrayElementsKind(y) |
30 const char* source = | 28 const char* source = |
31 "(function(a) {" | 29 "(function(a) {" |
32 " var x = (a = new %sArray(%d)); %s;" | 30 " var x = (a = new %sArray(%d)); %s;" |
33 " var y = (a = new %sArray(%d)); %s; %%TypedArrayGetBuffer(y);" | 31 " var y = (a = new %sArray(%d)); %s; %%TypedArrayGetBuffer(y);" |
34 " if (!%%HasFixed%sElements(x)) %%AbortJS('x');" | 32 " if (!%%HasFixed%sElements(x)) %%AbortJS('x');" |
35 " if (!%%HasExternal%sElements(y)) %%AbortJS('y');" | 33 " if (!%%HasExternal%sElements(y)) %%AbortJS('y');" |
36 " function f(a,b) {" | 34 " function f(a,b) {" |
37 " a = a | 0; b = b | 0;" | 35 " a = a | 0; b = b | 0;" |
38 " return x[a] + y[b];" | 36 " return x[a] + y[b];" |
39 " }" | 37 " }" |
40 " return f;" | 38 " return f;" |
41 "})()"; | 39 "})()"; |
42 EmbeddedVector<char, 1024> source_buffer; | 40 EmbeddedVector<char, 1024> source_buffer; |
43 SNPrintF(source_buffer, source, array_type, size, values_buffer.start(), | 41 SNPrintF(source_buffer, source, array_type, arraysize(kValues), |
44 array_type, size, values_buffer.start(), array_type, array_type); | 42 values_buffer.start(), array_type, arraysize(kValues), |
| 43 values_buffer.start(), array_type, array_type); |
45 | 44 |
46 FunctionTester T( | 45 FunctionTester T( |
47 source_buffer.start(), | 46 source_buffer.start(), |
48 CompilationInfo::kContextSpecializing | CompilationInfo::kTypingEnabled); | 47 CompilationInfo::kContextSpecializing | CompilationInfo::kTypingEnabled); |
49 for (unsigned i = 0; i < size; i++) { | 48 for (size_t i = 0; i < arraysize(kValues); ++i) { |
50 for (unsigned j = 0; j < size; j++) { | 49 for (size_t j = 0; j < arraysize(kValues); ++j) { |
51 double value_a = static_cast<U>(values[i]); | 50 double value_a = static_cast<U>(kValues[i]); |
52 double value_b = static_cast<U>(values[j]); | 51 double value_b = static_cast<U>(kValues[j]); |
53 double expected = value_a + value_b; | 52 double expected = value_a + value_b; |
54 T.CheckCall(T.Val(expected), T.Val(i), T.Val(j)); | 53 T.CheckCall(T.Val(expected), T.Val(i), T.Val(j)); |
55 } | 54 } |
56 } | 55 } |
57 } | 56 } |
58 | 57 |
59 | 58 |
60 TEST(TypedArrayLoad) { | 59 TEST(TypedArrayLoad) { |
61 FLAG_typed_array_max_size_in_heap = 256; | 60 FLAG_typed_array_max_size_in_heap = 256; |
62 TypedArrayLoadHelper<int8_t>("Int8"); | 61 TypedArrayLoadHelper<int8_t>("Int8"); |
63 TypedArrayLoadHelper<uint8_t>("Uint8"); | 62 TypedArrayLoadHelper<uint8_t>("Uint8"); |
64 TypedArrayLoadHelper<int16_t>("Int16"); | 63 TypedArrayLoadHelper<int16_t>("Int16"); |
65 TypedArrayLoadHelper<uint16_t>("Uint16"); | 64 TypedArrayLoadHelper<uint16_t>("Uint16"); |
66 TypedArrayLoadHelper<int32_t>("Int32"); | 65 TypedArrayLoadHelper<int32_t>("Int32"); |
67 TypedArrayLoadHelper<uint32_t>("Uint32"); | 66 TypedArrayLoadHelper<uint32_t>("Uint32"); |
68 TypedArrayLoadHelper<double>("Float64"); | 67 TypedArrayLoadHelper<double>("Float64"); |
69 // TODO(mstarzinger): Add tests for Float32. | 68 // TODO(mstarzinger): Add tests for Float32. |
70 // TODO(mstarzinger): Add tests for ClampedUint8. | 69 // TODO(mstarzinger): Add tests for ClampedUint8. |
71 } | 70 } |
OLD | NEW |