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 3 // found in the LICENSE file. 4 5 #include "test/cctest/compiler/function-tester.h" 6 7 namespace v8 { 8 namespace internal { 9 namespace compiler { 10 11 template <typename U> 12 static void TypedArrayLoadHelper(const char* array_type) { 13 static const uint32_t kValues[] = { 14 0x00000000, 0x00000001, 0x00000023, 0x00000042, 0x12345678, 0x87654321, 15 0x0000003f, 0x0000007f, 0x00003fff, 0x00007fff, 0x3fffffff, 0x7fffffff, 16 0x000000ff, 0x00000080, 0x0000ffff, 0x00008000, 0xffffffff, 0x80000000}; 17 EmbeddedVector<char, 1024> values_buffer; 18 StringBuilder values_builder(values_buffer.start(), values_buffer.length()); 19 for (size_t i = 0; i < arraysize(kValues); ++i) { 20 values_builder.AddFormatted("a[%d] = 0x%08x;", i, kValues[i]); 21 } 22 23 // Note that below source creates two different typed arrays with the same 24 // elements kind to get coverage for both (on heap / with external backing 25 // store) access patterns. 26 const char* source = 27 "(function(a) {" 28 " var x = (a = new %sArray(%d)); %s;" 29 " var y = (a = new %sArray(%d)); %s; %%TypedArrayGetBuffer(y);" 30 " if (!%%HasFixed%sElements(x)) %%AbortJS('x');" 31 " if (!%%HasFixed%sElements(y)) %%AbortJS('y');" 32 " function f(a,b) {" 33 " a = a | 0; b = b | 0;" 34 " return x[a] + y[b];" 35 " }" 36 " return f;" 37 "})()"; 38 EmbeddedVector<char, 1024> source_buffer; 39 SNPrintF(source_buffer, source, array_type, arraysize(kValues), 40 values_buffer.start(), array_type, arraysize(kValues), 41 values_buffer.start(), array_type, array_type); 42 43 FunctionTester T(source_buffer.start(), 44 CompilationInfo::kFunctionContextSpecializing | 45 CompilationInfo::kTypingEnabled); 46 for (size_t i = 0; i < arraysize(kValues); ++i) { 47 for (size_t j = 0; j < arraysize(kValues); ++j) { 48 volatile U value_a = static_cast<U>(kValues[i]); 49 volatile U value_b = static_cast<U>(kValues[j]); 50 double expected = 51 static_cast<double>(value_a) + static_cast<double>(value_b); 52 T.CheckCall(T.Val(expected), T.Val(static_cast<double>(i)), 53 T.Val(static_cast<double>(j))); 54 } 55 } 56 } 57 58 59 TEST(TypedArrayLoad) { 60 FLAG_typed_array_max_size_in_heap = 256; 61 TypedArrayLoadHelper<int8_t>("Int8"); 62 TypedArrayLoadHelper<uint8_t>("Uint8"); 63 TypedArrayLoadHelper<int16_t>("Int16"); 64 TypedArrayLoadHelper<uint16_t>("Uint16"); 65 TypedArrayLoadHelper<int32_t>("Int32"); 66 TypedArrayLoadHelper<uint32_t>("Uint32"); 67 TypedArrayLoadHelper<float>("Float32"); 68 TypedArrayLoadHelper<double>("Float64"); 69 // TODO(mstarzinger): Add tests for ClampedUint8. 70 } 71 72 73 template <typename U> 74 static void TypedArrayStoreHelper(const char* array_type) { 75 static const uint32_t kValues[] = { 76 0x00000000, 0x00000001, 0x00000023, 0x00000042, 0x12345678, 0x87654321, 77 0x0000003f, 0x0000007f, 0x00003fff, 0x00007fff, 0x3fffffff, 0x7fffffff, 78 0x000000ff, 0x00000080, 0x0000ffff, 0x00008000, 0xffffffff, 0x80000000}; 79 EmbeddedVector<char, 1024> values_buffer; 80 StringBuilder values_builder(values_buffer.start(), values_buffer.length()); 81 for (size_t i = 0; i < arraysize(kValues); ++i) { 82 values_builder.AddFormatted("a[%d] = 0x%08x;", i, kValues[i]); 83 } 84 85 // Note that below source creates two different typed arrays with the same 86 // elements kind to get coverage for both (on heap/with external backing 87 // store) access patterns. 88 const char* source = 89 "(function(a) {" 90 " var x = (a = new %sArray(%d)); %s;" 91 " var y = (a = new %sArray(%d)); %s; %%TypedArrayGetBuffer(y);" 92 " if (!%%HasFixed%sElements(x)) %%AbortJS('x');" 93 " if (!%%HasFixed%sElements(y)) %%AbortJS('y');" 94 " function f(a,b) {" 95 " a = a | 0; b = b | 0;" 96 " var t = x[a];" 97 " x[a] = y[b];" 98 " y[b] = t;" 99 " t = y[b];" 100 " y[b] = x[a];" 101 " x[a] = t;" 102 " return x[a] + y[b];" 103 " }" 104 " return f;" 105 "})()"; 106 EmbeddedVector<char, 2048> source_buffer; 107 SNPrintF(source_buffer, source, array_type, arraysize(kValues), 108 values_buffer.start(), array_type, arraysize(kValues), 109 values_buffer.start(), array_type, array_type); 110 111 FunctionTester T(source_buffer.start(), 112 CompilationInfo::kFunctionContextSpecializing | 113 CompilationInfo::kTypingEnabled); 114 for (size_t i = 0; i < arraysize(kValues); ++i) { 115 for (size_t j = 0; j < arraysize(kValues); ++j) { 116 volatile U value_a = static_cast<U>(kValues[i]); 117 volatile U value_b = static_cast<U>(kValues[j]); 118 double expected = 119 static_cast<double>(value_a) + static_cast<double>(value_b); 120 T.CheckCall(T.Val(expected), T.Val(static_cast<double>(i)), 121 T.Val(static_cast<double>(j))); 122 } 123 } 124 } 125 126 127 TEST(TypedArrayStore) { 128 FLAG_typed_array_max_size_in_heap = 256; 129 TypedArrayStoreHelper<int8_t>("Int8"); 130 TypedArrayStoreHelper<uint8_t>("Uint8"); 131 TypedArrayStoreHelper<int16_t>("Int16"); 132 TypedArrayStoreHelper<uint16_t>("Uint16"); 133 TypedArrayStoreHelper<int32_t>("Int32"); 134 TypedArrayStoreHelper<uint32_t>("Uint32"); 135 TypedArrayStoreHelper<float>("Float32"); 136 TypedArrayStoreHelper<double>("Float64"); 137 // TODO(mstarzinger): Add tests for ClampedUint8. 138 } 139 140 } // namespace compiler 141 } // namespace internal 142 } // namespace v8 143