// 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 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 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 distinct // elements kind to get coverage for both access patterns: // - IsFixedTypedArrayElementsKind(x) // - IsExternalArrayElementsKind(y) 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 (!%%HasExternal%sElements(y)) %%AbortJS('y');" " function f(a,b) {" " a = a | 0; b = b | 0;" " return x[a] + y[b];" " }" " return f;" "})()"; EmbeddedVector 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(kValues[i]); volatile U value_b = static_cast(kValues[j]); double expected = static_cast(value_a) + static_cast(value_b); T.CheckCall(T.Val(expected), T.Val(static_cast(i)), T.Val(static_cast(j))); } } } TEST(TypedArrayLoad) { FLAG_typed_array_max_size_in_heap = 256; TypedArrayLoadHelper("Int8"); TypedArrayLoadHelper("Uint8"); TypedArrayLoadHelper("Int16"); TypedArrayLoadHelper("Uint16"); TypedArrayLoadHelper("Int32"); TypedArrayLoadHelper("Uint32"); TypedArrayLoadHelper("Float32"); TypedArrayLoadHelper("Float64"); // TODO(mstarzinger): Add tests for ClampedUint8. } template 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 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 distinct // elements kind to get coverage for both access patterns: // - IsFixedTypedArrayElementsKind(x) // - IsExternalArrayElementsKind(y) 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 (!%%HasExternal%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 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(kValues[i]); volatile U value_b = static_cast(kValues[j]); double expected = static_cast(value_a) + static_cast(value_b); T.CheckCall(T.Val(expected), T.Val(static_cast(i)), T.Val(static_cast(j))); } } } TEST(TypedArrayStore) { FLAG_typed_array_max_size_in_heap = 256; TypedArrayStoreHelper("Int8"); TypedArrayStoreHelper("Uint8"); TypedArrayStoreHelper("Int16"); TypedArrayStoreHelper("Uint16"); TypedArrayStoreHelper("Int32"); TypedArrayStoreHelper("Uint32"); TypedArrayStoreHelper("Float32"); TypedArrayStoreHelper("Float64"); // TODO(mstarzinger): Add tests for ClampedUint8. }