Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <stdlib.h> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 29 | #include <iostream> // NOLINT(readability/streams) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | #include "src/base/utils/random-number-generator.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | #include "src/macro-assembler.h" |
| 33 | #include "src/mips/macro-assembler-mips.h" |
| 34 | #include "src/mips/simulator-mips.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 35 | #include "src/v8.h" |
| 36 | #include "test/cctest/cctest.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | using namespace v8::internal; |
| 40 | |
| 41 | typedef void* (*F)(int x, int y, int p2, int p3, int p4); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 42 | typedef Object* (*F1)(int x, int p1, int p2, int p3, int p4); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 43 | typedef Object* (*F3)(void* p, int p1, int p2, int p3, int p4); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | |
| 45 | #define __ masm-> |
| 46 | |
| 47 | |
| 48 | static byte to_non_zero(int n) { |
| 49 | return static_cast<unsigned>(n) % 255 + 1; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | static bool all_zeroes(const byte* beg, const byte* end) { |
| 54 | CHECK(beg); |
| 55 | CHECK(beg <= end); |
| 56 | while (beg < end) { |
| 57 | if (*beg++ != 0) |
| 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | TEST(CopyBytes) { |
| 65 | CcTest::InitializeVM(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | Isolate* isolate = CcTest::i_isolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 67 | HandleScope handles(isolate); |
| 68 | |
| 69 | const int data_size = 1 * KB; |
| 70 | size_t act_size; |
| 71 | |
| 72 | // Allocate two blocks to copy data between. |
| 73 | byte* src_buffer = |
| 74 | static_cast<byte*>(v8::base::OS::Allocate(data_size, &act_size, 0)); |
| 75 | CHECK(src_buffer); |
| 76 | CHECK(act_size >= static_cast<size_t>(data_size)); |
| 77 | byte* dest_buffer = |
| 78 | static_cast<byte*>(v8::base::OS::Allocate(data_size, &act_size, 0)); |
| 79 | CHECK(dest_buffer); |
| 80 | CHECK(act_size >= static_cast<size_t>(data_size)); |
| 81 | |
| 82 | // Storage for a0 and a1. |
| 83 | byte* a0_; |
| 84 | byte* a1_; |
| 85 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 86 | MacroAssembler assembler(isolate, NULL, 0, |
| 87 | v8::internal::CodeObjectRequired::kYes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | MacroAssembler* masm = &assembler; |
| 89 | |
| 90 | // Code to be generated: The stuff in CopyBytes followed by a store of a0 and |
| 91 | // a1, respectively. |
| 92 | __ CopyBytes(a0, a1, a2, a3); |
| 93 | __ li(a2, Operand(reinterpret_cast<int>(&a0_))); |
| 94 | __ li(a3, Operand(reinterpret_cast<int>(&a1_))); |
| 95 | __ sw(a0, MemOperand(a2)); |
| 96 | __ jr(ra); |
| 97 | __ sw(a1, MemOperand(a3)); |
| 98 | |
| 99 | CodeDesc desc; |
| 100 | masm->GetCode(&desc); |
| 101 | Handle<Code> code = isolate->factory()->NewCode( |
| 102 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 103 | |
| 104 | ::F f = FUNCTION_CAST< ::F>(code->entry()); |
| 105 | |
| 106 | // Initialise source data with non-zero bytes. |
| 107 | for (int i = 0; i < data_size; i++) { |
| 108 | src_buffer[i] = to_non_zero(i); |
| 109 | } |
| 110 | |
| 111 | const int fuzz = 11; |
| 112 | |
| 113 | for (int size = 0; size < 600; size++) { |
| 114 | for (const byte* src = src_buffer; src < src_buffer + fuzz; src++) { |
| 115 | for (byte* dest = dest_buffer; dest < dest_buffer + fuzz; dest++) { |
| 116 | memset(dest_buffer, 0, data_size); |
| 117 | CHECK(dest + size < dest_buffer + data_size); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | (void)CALL_GENERATED_CODE(isolate, f, reinterpret_cast<int>(src), |
| 119 | reinterpret_cast<int>(dest), size, 0, 0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 120 | // a0 and a1 should point at the first byte after the copied data. |
| 121 | CHECK_EQ(src + size, a0_); |
| 122 | CHECK_EQ(dest + size, a1_); |
| 123 | // Check that we haven't written outside the target area. |
| 124 | CHECK(all_zeroes(dest_buffer, dest)); |
| 125 | CHECK(all_zeroes(dest + size, dest_buffer + data_size)); |
| 126 | // Check the target area. |
| 127 | CHECK_EQ(0, memcmp(src, dest, size)); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Check that the source data hasn't been clobbered. |
| 133 | for (int i = 0; i < data_size; i++) { |
| 134 | CHECK(src_buffer[i] == to_non_zero(i)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | static void TestNaN(const char *code) { |
| 140 | // NaN value is different on MIPS and x86 architectures, and TEST(NaNx) |
| 141 | // tests checks the case where a x86 NaN value is serialized into the |
| 142 | // snapshot on the simulator during cross compilation. |
| 143 | v8::HandleScope scope(CcTest::isolate()); |
| 144 | v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION); |
| 145 | v8::Context::Scope context_scope(context); |
| 146 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 147 | v8::Local<v8::Script> script = |
| 148 | v8::Script::Compile(context, v8_str(code)).ToLocalChecked(); |
| 149 | v8::Local<v8::Object> result = |
| 150 | v8::Local<v8::Object>::Cast(script->Run(context).ToLocalChecked()); |
| 151 | i::Handle<i::JSReceiver> o = v8::Utils::OpenHandle(*result); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 152 | i::Handle<i::JSArray> array1(reinterpret_cast<i::JSArray*>(*o)); |
| 153 | i::FixedDoubleArray* a = i::FixedDoubleArray::cast(array1->elements()); |
| 154 | double value = a->get_scalar(0); |
| 155 | CHECK(std::isnan(value) && |
| 156 | bit_cast<uint64_t>(value) == |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 157 | bit_cast<uint64_t>(std::numeric_limits<double>::quiet_NaN())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | TEST(NaN0) { |
| 162 | TestNaN( |
| 163 | "var result;" |
| 164 | "for (var i = 0; i < 2; i++) {" |
| 165 | " result = new Array(Number.NaN, Number.POSITIVE_INFINITY);" |
| 166 | "}" |
| 167 | "result;"); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | TEST(NaN1) { |
| 172 | TestNaN( |
| 173 | "var result;" |
| 174 | "for (var i = 0; i < 2; i++) {" |
| 175 | " result = [NaN];" |
| 176 | "}" |
| 177 | "result;"); |
| 178 | } |
| 179 | |
| 180 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 181 | TEST(jump_tables4) { |
| 182 | // Similar to test-assembler-mips jump_tables1, with extra test for branch |
| 183 | // trampoline required before emission of the dd table (where trampolines are |
| 184 | // blocked), and proper transition to long-branch mode. |
| 185 | // Regression test for v8:4294. |
| 186 | CcTest::InitializeVM(); |
| 187 | Isolate* isolate = CcTest::i_isolate(); |
| 188 | HandleScope scope(isolate); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 189 | MacroAssembler assembler(isolate, nullptr, 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 190 | v8::internal::CodeObjectRequired::kYes); |
| 191 | MacroAssembler* masm = &assembler; |
| 192 | |
| 193 | const int kNumCases = 512; |
| 194 | int values[kNumCases]; |
| 195 | isolate->random_number_generator()->NextBytes(values, sizeof(values)); |
| 196 | Label labels[kNumCases]; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 197 | Label near_start, end, done; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 198 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 199 | __ Push(ra); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 200 | __ mov(v0, zero_reg); |
| 201 | |
| 202 | __ Branch(&end); |
| 203 | __ bind(&near_start); |
| 204 | |
| 205 | // Generate slightly less than 32K instructions, which will soon require |
| 206 | // trampoline for branch distance fixup. |
| 207 | for (int i = 0; i < 32768 - 256; ++i) { |
| 208 | __ addiu(v0, v0, 1); |
| 209 | } |
| 210 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 211 | __ GenerateSwitchTable(a0, kNumCases, |
| 212 | [&labels](size_t i) { return labels + i; }); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 213 | |
| 214 | for (int i = 0; i < kNumCases; ++i) { |
| 215 | __ bind(&labels[i]); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 216 | __ li(v0, values[i]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 217 | __ Branch(&done); |
| 218 | } |
| 219 | |
| 220 | __ bind(&done); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 221 | __ Pop(ra); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 222 | __ jr(ra); |
| 223 | __ nop(); |
| 224 | |
| 225 | __ bind(&end); |
| 226 | __ Branch(&near_start); |
| 227 | |
| 228 | CodeDesc desc; |
| 229 | masm->GetCode(&desc); |
| 230 | Handle<Code> code = isolate->factory()->NewCode( |
| 231 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 232 | #ifdef OBJECT_PRINT |
| 233 | code->Print(std::cout); |
| 234 | #endif |
| 235 | F1 f = FUNCTION_CAST<F1>(code->entry()); |
| 236 | for (int i = 0; i < kNumCases; ++i) { |
| 237 | int res = |
| 238 | reinterpret_cast<int>(CALL_GENERATED_CODE(isolate, f, i, 0, 0, 0, 0)); |
| 239 | ::printf("f(%d) = %d\n", i, res); |
| 240 | CHECK_EQ(values[i], res); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
| 245 | TEST(jump_tables5) { |
| 246 | if (!IsMipsArchVariant(kMips32r6)) return; |
| 247 | |
| 248 | // Similar to test-assembler-mips jump_tables1, with extra test for emitting a |
| 249 | // compact branch instruction before emission of the dd table. |
| 250 | CcTest::InitializeVM(); |
| 251 | Isolate* isolate = CcTest::i_isolate(); |
| 252 | HandleScope scope(isolate); |
| 253 | MacroAssembler assembler(isolate, nullptr, 0, |
| 254 | v8::internal::CodeObjectRequired::kYes); |
| 255 | MacroAssembler* masm = &assembler; |
| 256 | |
| 257 | const int kNumCases = 512; |
| 258 | int values[kNumCases]; |
| 259 | isolate->random_number_generator()->NextBytes(values, sizeof(values)); |
| 260 | Label labels[kNumCases]; |
| 261 | Label done; |
| 262 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 263 | __ Push(ra); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 264 | |
| 265 | { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 266 | __ BlockTrampolinePoolFor(kNumCases + 6 + 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 267 | PredictableCodeSizeScope predictable( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 268 | masm, kNumCases * kPointerSize + ((6 + 1) * Assembler::kInstrSize)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 269 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 270 | __ addiupc(at, 6 + 1); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 271 | __ Lsa(at, at, a0, 2); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 272 | __ lw(at, MemOperand(at)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 273 | __ jalr(at); |
| 274 | __ nop(); // Branch delay slot nop. |
| 275 | __ bc(&done); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 276 | // A nop instruction must be generated by the forbidden slot guard |
| 277 | // (Assembler::dd(Label*)). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 278 | for (int i = 0; i < kNumCases; ++i) { |
| 279 | __ dd(&labels[i]); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | for (int i = 0; i < kNumCases; ++i) { |
| 284 | __ bind(&labels[i]); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 285 | __ li(v0, values[i]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 286 | __ jr(ra); |
| 287 | __ nop(); |
| 288 | } |
| 289 | |
| 290 | __ bind(&done); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 291 | __ Pop(ra); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 292 | __ jr(ra); |
| 293 | __ nop(); |
| 294 | |
| 295 | CodeDesc desc; |
| 296 | masm->GetCode(&desc); |
| 297 | Handle<Code> code = isolate->factory()->NewCode( |
| 298 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 299 | #ifdef OBJECT_PRINT |
| 300 | code->Print(std::cout); |
| 301 | #endif |
| 302 | F1 f = FUNCTION_CAST<F1>(code->entry()); |
| 303 | for (int i = 0; i < kNumCases; ++i) { |
| 304 | int32_t res = reinterpret_cast<int32_t>( |
| 305 | CALL_GENERATED_CODE(isolate, f, i, 0, 0, 0, 0)); |
| 306 | ::printf("f(%d) = %d\n", i, res); |
| 307 | CHECK_EQ(values[i], res); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | |
| 312 | static uint32_t run_lsa(uint32_t rt, uint32_t rs, int8_t sa) { |
| 313 | Isolate* isolate = CcTest::i_isolate(); |
| 314 | HandleScope scope(isolate); |
| 315 | MacroAssembler assembler(isolate, nullptr, 0, |
| 316 | v8::internal::CodeObjectRequired::kYes); |
| 317 | MacroAssembler* masm = &assembler; |
| 318 | |
| 319 | __ Lsa(v0, a0, a1, sa); |
| 320 | __ jr(ra); |
| 321 | __ nop(); |
| 322 | |
| 323 | CodeDesc desc; |
| 324 | assembler.GetCode(&desc); |
| 325 | Handle<Code> code = isolate->factory()->NewCode( |
| 326 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 327 | |
| 328 | F1 f = FUNCTION_CAST<F1>(code->entry()); |
| 329 | |
| 330 | uint32_t res = reinterpret_cast<uint32_t>( |
| 331 | CALL_GENERATED_CODE(isolate, f, rt, rs, 0, 0, 0)); |
| 332 | |
| 333 | return res; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | TEST(Lsa) { |
| 338 | CcTest::InitializeVM(); |
| 339 | struct TestCaseLsa { |
| 340 | int32_t rt; |
| 341 | int32_t rs; |
| 342 | uint8_t sa; |
| 343 | uint32_t expected_res; |
| 344 | }; |
| 345 | |
| 346 | struct TestCaseLsa tc[] = {// rt, rs, sa, expected_res |
| 347 | {0x4, 0x1, 1, 0x6}, |
| 348 | {0x4, 0x1, 2, 0x8}, |
| 349 | {0x4, 0x1, 3, 0xc}, |
| 350 | {0x4, 0x1, 4, 0x14}, |
| 351 | {0x4, 0x1, 5, 0x24}, |
| 352 | {0x0, 0x1, 1, 0x2}, |
| 353 | {0x0, 0x1, 2, 0x4}, |
| 354 | {0x0, 0x1, 3, 0x8}, |
| 355 | {0x0, 0x1, 4, 0x10}, |
| 356 | {0x0, 0x1, 5, 0x20}, |
| 357 | {0x4, 0x0, 1, 0x4}, |
| 358 | {0x4, 0x0, 2, 0x4}, |
| 359 | {0x4, 0x0, 3, 0x4}, |
| 360 | {0x4, 0x0, 4, 0x4}, |
| 361 | {0x4, 0x0, 5, 0x4}, |
| 362 | |
| 363 | // Shift overflow. |
| 364 | {0x4, INT32_MAX, 1, 0x2}, |
| 365 | {0x4, INT32_MAX >> 1, 2, 0x0}, |
| 366 | {0x4, INT32_MAX >> 2, 3, 0xfffffffc}, |
| 367 | {0x4, INT32_MAX >> 3, 4, 0xfffffff4}, |
| 368 | {0x4, INT32_MAX >> 4, 5, 0xffffffe4}, |
| 369 | |
| 370 | // Signed addition overflow. |
| 371 | {INT32_MAX - 1, 0x1, 1, 0x80000000}, |
| 372 | {INT32_MAX - 3, 0x1, 2, 0x80000000}, |
| 373 | {INT32_MAX - 7, 0x1, 3, 0x80000000}, |
| 374 | {INT32_MAX - 15, 0x1, 4, 0x80000000}, |
| 375 | {INT32_MAX - 31, 0x1, 5, 0x80000000}, |
| 376 | |
| 377 | // Addition overflow. |
| 378 | {-2, 0x1, 1, 0x0}, |
| 379 | {-4, 0x1, 2, 0x0}, |
| 380 | {-8, 0x1, 3, 0x0}, |
| 381 | {-16, 0x1, 4, 0x0}, |
| 382 | {-32, 0x1, 5, 0x0}}; |
| 383 | |
| 384 | size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseLsa); |
| 385 | for (size_t i = 0; i < nr_test_cases; ++i) { |
| 386 | uint32_t res = run_lsa(tc[i].rt, tc[i].rs, tc[i].sa); |
| 387 | PrintF("0x%x =? 0x%x == lsa(v0, %x, %x, %hhu)\n", tc[i].expected_res, res, |
| 388 | tc[i].rt, tc[i].rs, tc[i].sa); |
| 389 | CHECK_EQ(tc[i].expected_res, res); |
| 390 | } |
| 391 | } |
| 392 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 393 | static const std::vector<uint32_t> uint32_test_values() { |
| 394 | static const uint32_t kValues[] = {0x00000000, 0x00000001, 0x00ffff00, |
| 395 | 0x7fffffff, 0x80000000, 0x80000001, |
| 396 | 0x80ffff00, 0x8fffffff, 0xffffffff}; |
| 397 | return std::vector<uint32_t>(&kValues[0], &kValues[arraysize(kValues)]); |
| 398 | } |
| 399 | |
| 400 | static const std::vector<int32_t> int32_test_values() { |
| 401 | static const int32_t kValues[] = { |
| 402 | static_cast<int32_t>(0x00000000), static_cast<int32_t>(0x00000001), |
| 403 | static_cast<int32_t>(0x00ffff00), static_cast<int32_t>(0x7fffffff), |
| 404 | static_cast<int32_t>(0x80000000), static_cast<int32_t>(0x80000001), |
| 405 | static_cast<int32_t>(0x80ffff00), static_cast<int32_t>(0x8fffffff), |
| 406 | static_cast<int32_t>(0xffffffff)}; |
| 407 | return std::vector<int32_t>(&kValues[0], &kValues[arraysize(kValues)]); |
| 408 | } |
| 409 | |
| 410 | // Helper macros that can be used in FOR_INT32_INPUTS(i) { ... *i ... } |
| 411 | #define FOR_INPUTS(ctype, itype, var) \ |
| 412 | std::vector<ctype> var##_vec = itype##_test_values(); \ |
| 413 | for (std::vector<ctype>::iterator var = var##_vec.begin(); \ |
| 414 | var != var##_vec.end(); ++var) |
| 415 | |
| 416 | #define FOR_UINT32_INPUTS(var) FOR_INPUTS(uint32_t, uint32, var) |
| 417 | #define FOR_INT32_INPUTS(var) FOR_INPUTS(int32_t, int32, var) |
| 418 | |
| 419 | template <typename RET_TYPE, typename IN_TYPE, typename Func> |
| 420 | RET_TYPE run_Cvt(IN_TYPE x, Func GenerateConvertInstructionFunc) { |
| 421 | typedef RET_TYPE (*F_CVT)(IN_TYPE x0, int x1, int x2, int x3, int x4); |
| 422 | |
| 423 | Isolate* isolate = CcTest::i_isolate(); |
| 424 | HandleScope scope(isolate); |
| 425 | MacroAssembler assm(isolate, nullptr, 0, |
| 426 | v8::internal::CodeObjectRequired::kYes); |
| 427 | MacroAssembler* masm = &assm; |
| 428 | |
| 429 | __ mtc1(a0, f4); |
| 430 | GenerateConvertInstructionFunc(masm); |
| 431 | __ mfc1(v0, f2); |
| 432 | __ jr(ra); |
| 433 | __ nop(); |
| 434 | |
| 435 | CodeDesc desc; |
| 436 | assm.GetCode(&desc); |
| 437 | Handle<Code> code = isolate->factory()->NewCode( |
| 438 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 439 | |
| 440 | F_CVT f = FUNCTION_CAST<F_CVT>(code->entry()); |
| 441 | |
| 442 | return reinterpret_cast<RET_TYPE>( |
| 443 | CALL_GENERATED_CODE(isolate, f, x, 0, 0, 0, 0)); |
| 444 | } |
| 445 | |
| 446 | TEST(cvt_s_w_Trunc_uw_s) { |
| 447 | CcTest::InitializeVM(); |
| 448 | FOR_UINT32_INPUTS(i) { |
| 449 | uint32_t input = *i; |
| 450 | CHECK_EQ(static_cast<float>(input), |
| 451 | run_Cvt<uint32_t>(input, [](MacroAssembler* masm) { |
| 452 | __ cvt_s_w(f0, f4); |
| 453 | __ Trunc_uw_s(f2, f0, f1); |
| 454 | })); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | TEST(cvt_d_w_Trunc_w_d) { |
| 459 | CcTest::InitializeVM(); |
| 460 | FOR_INT32_INPUTS(i) { |
| 461 | int32_t input = *i; |
| 462 | CHECK_EQ(static_cast<double>(input), |
| 463 | run_Cvt<int32_t>(input, [](MacroAssembler* masm) { |
| 464 | __ cvt_d_w(f0, f4); |
| 465 | __ Trunc_w_d(f2, f0); |
| 466 | })); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | TEST(min_max_nan) { |
| 471 | CcTest::InitializeVM(); |
| 472 | Isolate* isolate = CcTest::i_isolate(); |
| 473 | HandleScope scope(isolate); |
| 474 | MacroAssembler assembler(isolate, nullptr, 0, |
| 475 | v8::internal::CodeObjectRequired::kYes); |
| 476 | MacroAssembler* masm = &assembler; |
| 477 | |
| 478 | struct TestFloat { |
| 479 | double a; |
| 480 | double b; |
| 481 | double c; |
| 482 | double d; |
| 483 | float e; |
| 484 | float f; |
| 485 | float g; |
| 486 | float h; |
| 487 | }; |
| 488 | |
| 489 | TestFloat test; |
| 490 | const double dnan = std::numeric_limits<double>::quiet_NaN(); |
| 491 | const double dinf = std::numeric_limits<double>::infinity(); |
| 492 | const double dminf = -std::numeric_limits<double>::infinity(); |
| 493 | const float fnan = std::numeric_limits<float>::quiet_NaN(); |
| 494 | const float finf = std::numeric_limits<float>::infinity(); |
| 495 | const float fminf = std::numeric_limits<float>::infinity(); |
| 496 | const int kTableLength = 13; |
| 497 | |
| 498 | double inputsa[kTableLength] = {2.0, 3.0, -0.0, 0.0, 42.0, dinf, dminf, |
| 499 | dinf, dnan, 3.0, dinf, dnan, dnan}; |
| 500 | double inputsb[kTableLength] = {3.0, 2.0, 0.0, -0.0, dinf, 42.0, dinf, |
| 501 | dminf, 3.0, dnan, dnan, dinf, dnan}; |
| 502 | double outputsdmin[kTableLength] = {2.0, 2.0, -0.0, -0.0, 42.0, |
| 503 | 42.0, dminf, dminf, dnan, dnan, |
| 504 | dnan, dnan, dnan}; |
| 505 | double outputsdmax[kTableLength] = {3.0, 3.0, 0.0, 0.0, dinf, dinf, dinf, |
| 506 | dinf, dnan, dnan, dnan, dnan, dnan}; |
| 507 | |
| 508 | float inputse[kTableLength] = {2.0, 3.0, -0.0, 0.0, 42.0, finf, fminf, |
| 509 | finf, fnan, 3.0, finf, fnan, fnan}; |
| 510 | float inputsf[kTableLength] = {3.0, 2.0, 0.0, -0.0, finf, 42.0, finf, |
| 511 | fminf, 3.0, fnan, fnan, finf, fnan}; |
| 512 | float outputsfmin[kTableLength] = {2.0, 2.0, -0.0, -0.0, 42.0, 42.0, fminf, |
| 513 | fminf, fnan, fnan, fnan, fnan, fnan}; |
| 514 | float outputsfmax[kTableLength] = {3.0, 3.0, 0.0, 0.0, finf, finf, finf, |
| 515 | finf, fnan, fnan, fnan, fnan, fnan}; |
| 516 | |
| 517 | auto handle_dnan = [masm](FPURegister dst, Label* nan, Label* back) { |
| 518 | __ bind(nan); |
| 519 | __ LoadRoot(at, Heap::kNanValueRootIndex); |
| 520 | __ ldc1(dst, FieldMemOperand(at, HeapNumber::kValueOffset)); |
| 521 | __ Branch(back); |
| 522 | }; |
| 523 | |
| 524 | auto handle_snan = [masm, fnan](FPURegister dst, Label* nan, Label* back) { |
| 525 | __ bind(nan); |
| 526 | __ Move(dst, fnan); |
| 527 | __ Branch(back); |
| 528 | }; |
| 529 | |
| 530 | Label handle_mind_nan, handle_maxd_nan, handle_mins_nan, handle_maxs_nan; |
| 531 | Label back_mind_nan, back_maxd_nan, back_mins_nan, back_maxs_nan; |
| 532 | |
| 533 | __ push(s6); |
| 534 | __ InitializeRootRegister(); |
| 535 | __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, a))); |
| 536 | __ ldc1(f8, MemOperand(a0, offsetof(TestFloat, b))); |
| 537 | __ lwc1(f2, MemOperand(a0, offsetof(TestFloat, e))); |
| 538 | __ lwc1(f6, MemOperand(a0, offsetof(TestFloat, f))); |
| 539 | __ MinNaNCheck_d(f10, f4, f8, &handle_mind_nan); |
| 540 | __ bind(&back_mind_nan); |
| 541 | __ MaxNaNCheck_d(f12, f4, f8, &handle_maxd_nan); |
| 542 | __ bind(&back_maxd_nan); |
| 543 | __ MinNaNCheck_s(f14, f2, f6, &handle_mins_nan); |
| 544 | __ bind(&back_mins_nan); |
| 545 | __ MaxNaNCheck_s(f16, f2, f6, &handle_maxs_nan); |
| 546 | __ bind(&back_maxs_nan); |
| 547 | __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, c))); |
| 548 | __ sdc1(f12, MemOperand(a0, offsetof(TestFloat, d))); |
| 549 | __ swc1(f14, MemOperand(a0, offsetof(TestFloat, g))); |
| 550 | __ swc1(f16, MemOperand(a0, offsetof(TestFloat, h))); |
| 551 | __ pop(s6); |
| 552 | __ jr(ra); |
| 553 | __ nop(); |
| 554 | |
| 555 | handle_dnan(f10, &handle_mind_nan, &back_mind_nan); |
| 556 | handle_dnan(f12, &handle_maxd_nan, &back_maxd_nan); |
| 557 | handle_snan(f14, &handle_mins_nan, &back_mins_nan); |
| 558 | handle_snan(f16, &handle_maxs_nan, &back_maxs_nan); |
| 559 | |
| 560 | CodeDesc desc; |
| 561 | masm->GetCode(&desc); |
| 562 | Handle<Code> code = isolate->factory()->NewCode( |
| 563 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 564 | ::F3 f = FUNCTION_CAST<::F3>(code->entry()); |
| 565 | for (int i = 0; i < kTableLength; i++) { |
| 566 | test.a = inputsa[i]; |
| 567 | test.b = inputsb[i]; |
| 568 | test.e = inputse[i]; |
| 569 | test.f = inputsf[i]; |
| 570 | |
| 571 | CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0); |
| 572 | |
| 573 | CHECK_EQ(0, memcmp(&test.c, &outputsdmin[i], sizeof(test.c))); |
| 574 | CHECK_EQ(0, memcmp(&test.d, &outputsdmax[i], sizeof(test.d))); |
| 575 | CHECK_EQ(0, memcmp(&test.g, &outputsfmin[i], sizeof(test.g))); |
| 576 | CHECK_EQ(0, memcmp(&test.h, &outputsfmax[i], sizeof(test.h))); |
| 577 | } |
| 578 | } |
| 579 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 580 | #undef __ |