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 | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 393 | static const std::vector<uint32_t> cvt_trunc_uint32_test_values() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 400 | static const std::vector<int32_t> cvt_trunc_int32_test_values() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 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 ... } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 411 | #define FOR_INPUTS(ctype, itype, var, test_vector) \ |
| 412 | std::vector<ctype> var##_vec = test_vector(); \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 413 | for (std::vector<ctype>::iterator var = var##_vec.begin(); \ |
| 414 | var != var##_vec.end(); ++var) |
| 415 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 416 | #define FOR_INPUTS2(ctype, itype, var, var2, test_vector) \ |
| 417 | std::vector<ctype> var##_vec = test_vector(); \ |
| 418 | std::vector<ctype>::iterator var; \ |
| 419 | std::vector<ctype>::reverse_iterator var2; \ |
| 420 | for (var = var##_vec.begin(), var2 = var##_vec.rbegin(); \ |
| 421 | var != var##_vec.end(); ++var, ++var2) |
| 422 | |
| 423 | #define FOR_ENUM_INPUTS(var, type, test_vector) \ |
| 424 | FOR_INPUTS(enum type, type, var, test_vector) |
| 425 | #define FOR_STRUCT_INPUTS(var, type, test_vector) \ |
| 426 | FOR_INPUTS(struct type, type, var, test_vector) |
| 427 | #define FOR_UINT32_INPUTS(var, test_vector) \ |
| 428 | FOR_INPUTS(uint32_t, uint32, var, test_vector) |
| 429 | #define FOR_INT32_INPUTS(var, test_vector) \ |
| 430 | FOR_INPUTS(int32_t, int32, var, test_vector) |
| 431 | #define FOR_INT32_INPUTS2(var, var2, test_vector) \ |
| 432 | FOR_INPUTS2(int32_t, int32, var, var2, test_vector) |
| 433 | |
| 434 | #define FOR_UINT64_INPUTS(var, test_vector) \ |
| 435 | FOR_INPUTS(uint64_t, uint32, var, test_vector) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 436 | |
| 437 | template <typename RET_TYPE, typename IN_TYPE, typename Func> |
| 438 | RET_TYPE run_Cvt(IN_TYPE x, Func GenerateConvertInstructionFunc) { |
| 439 | typedef RET_TYPE (*F_CVT)(IN_TYPE x0, int x1, int x2, int x3, int x4); |
| 440 | |
| 441 | Isolate* isolate = CcTest::i_isolate(); |
| 442 | HandleScope scope(isolate); |
| 443 | MacroAssembler assm(isolate, nullptr, 0, |
| 444 | v8::internal::CodeObjectRequired::kYes); |
| 445 | MacroAssembler* masm = &assm; |
| 446 | |
| 447 | __ mtc1(a0, f4); |
| 448 | GenerateConvertInstructionFunc(masm); |
| 449 | __ mfc1(v0, f2); |
| 450 | __ jr(ra); |
| 451 | __ nop(); |
| 452 | |
| 453 | CodeDesc desc; |
| 454 | assm.GetCode(&desc); |
| 455 | Handle<Code> code = isolate->factory()->NewCode( |
| 456 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 457 | |
| 458 | F_CVT f = FUNCTION_CAST<F_CVT>(code->entry()); |
| 459 | |
| 460 | return reinterpret_cast<RET_TYPE>( |
| 461 | CALL_GENERATED_CODE(isolate, f, x, 0, 0, 0, 0)); |
| 462 | } |
| 463 | |
| 464 | TEST(cvt_s_w_Trunc_uw_s) { |
| 465 | CcTest::InitializeVM(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 466 | FOR_UINT32_INPUTS(i, cvt_trunc_uint32_test_values) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 467 | uint32_t input = *i; |
| 468 | CHECK_EQ(static_cast<float>(input), |
| 469 | run_Cvt<uint32_t>(input, [](MacroAssembler* masm) { |
| 470 | __ cvt_s_w(f0, f4); |
| 471 | __ Trunc_uw_s(f2, f0, f1); |
| 472 | })); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | TEST(cvt_d_w_Trunc_w_d) { |
| 477 | CcTest::InitializeVM(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 478 | FOR_INT32_INPUTS(i, cvt_trunc_int32_test_values) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 479 | int32_t input = *i; |
| 480 | CHECK_EQ(static_cast<double>(input), |
| 481 | run_Cvt<int32_t>(input, [](MacroAssembler* masm) { |
| 482 | __ cvt_d_w(f0, f4); |
| 483 | __ Trunc_w_d(f2, f0); |
| 484 | })); |
| 485 | } |
| 486 | } |
| 487 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 488 | static const std::vector<int32_t> overflow_int32_test_values() { |
| 489 | static const int32_t kValues[] = { |
| 490 | static_cast<int32_t>(0xf0000000), static_cast<int32_t>(0x00000001), |
| 491 | static_cast<int32_t>(0xff000000), static_cast<int32_t>(0x0000f000), |
| 492 | static_cast<int32_t>(0x0f000000), static_cast<int32_t>(0x991234ab), |
| 493 | static_cast<int32_t>(0xb0ffff01), static_cast<int32_t>(0x00006fff), |
| 494 | static_cast<int32_t>(0xffffffff)}; |
| 495 | return std::vector<int32_t>(&kValues[0], &kValues[arraysize(kValues)]); |
| 496 | } |
| 497 | |
| 498 | enum OverflowBranchType { |
| 499 | kAddBranchOverflow, |
| 500 | kSubBranchOverflow, |
| 501 | }; |
| 502 | |
| 503 | struct OverflowRegisterCombination { |
| 504 | Register dst; |
| 505 | Register left; |
| 506 | Register right; |
| 507 | Register scratch; |
| 508 | }; |
| 509 | |
| 510 | static const std::vector<enum OverflowBranchType> overflow_branch_type() { |
| 511 | static const enum OverflowBranchType kValues[] = {kAddBranchOverflow, |
| 512 | kSubBranchOverflow}; |
| 513 | return std::vector<enum OverflowBranchType>(&kValues[0], |
| 514 | &kValues[arraysize(kValues)]); |
| 515 | } |
| 516 | |
| 517 | static const std::vector<struct OverflowRegisterCombination> |
| 518 | overflow_register_combination() { |
| 519 | static const struct OverflowRegisterCombination kValues[] = { |
| 520 | {t0, t1, t2, t3}, {t0, t0, t2, t3}, {t0, t1, t0, t3}, {t0, t1, t1, t3}}; |
| 521 | return std::vector<struct OverflowRegisterCombination>( |
| 522 | &kValues[0], &kValues[arraysize(kValues)]); |
| 523 | } |
| 524 | |
| 525 | template <typename T> |
| 526 | static bool IsAddOverflow(T x, T y) { |
| 527 | DCHECK(std::numeric_limits<T>::is_integer); |
| 528 | T max = std::numeric_limits<T>::max(); |
| 529 | T min = std::numeric_limits<T>::min(); |
| 530 | |
| 531 | return (x > 0 && y > (max - x)) || (x < 0 && y < (min - x)); |
| 532 | } |
| 533 | |
| 534 | template <typename T> |
| 535 | static bool IsSubOverflow(T x, T y) { |
| 536 | DCHECK(std::numeric_limits<T>::is_integer); |
| 537 | T max = std::numeric_limits<T>::max(); |
| 538 | T min = std::numeric_limits<T>::min(); |
| 539 | |
| 540 | return (y > 0 && x < (min + y)) || (y < 0 && x > (max + y)); |
| 541 | } |
| 542 | |
| 543 | template <typename IN_TYPE, typename Func> |
| 544 | static bool runOverflow(IN_TYPE valLeft, IN_TYPE valRight, |
| 545 | Func GenerateOverflowInstructions) { |
| 546 | typedef int32_t (*F_CVT)(char* x0, int x1, int x2, int x3, int x4); |
| 547 | |
| 548 | Isolate* isolate = CcTest::i_isolate(); |
| 549 | HandleScope scope(isolate); |
| 550 | MacroAssembler assm(isolate, nullptr, 0, |
| 551 | v8::internal::CodeObjectRequired::kYes); |
| 552 | MacroAssembler* masm = &assm; |
| 553 | |
| 554 | GenerateOverflowInstructions(masm, valLeft, valRight); |
| 555 | __ jr(ra); |
| 556 | __ nop(); |
| 557 | |
| 558 | CodeDesc desc; |
| 559 | assm.GetCode(&desc); |
| 560 | Handle<Code> code = isolate->factory()->NewCode( |
| 561 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 562 | |
| 563 | F_CVT f = FUNCTION_CAST<F_CVT>(code->entry()); |
| 564 | |
| 565 | int32_t r = |
| 566 | reinterpret_cast<int32_t>(CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0)); |
| 567 | |
| 568 | DCHECK(r == 0 || r == 1); |
| 569 | return r; |
| 570 | } |
| 571 | |
| 572 | TEST(BranchOverflowInt32BothLabels) { |
| 573 | FOR_INT32_INPUTS(i, overflow_int32_test_values) { |
| 574 | FOR_INT32_INPUTS(j, overflow_int32_test_values) { |
| 575 | FOR_ENUM_INPUTS(br, OverflowBranchType, overflow_branch_type) { |
| 576 | FOR_STRUCT_INPUTS(regComb, OverflowRegisterCombination, |
| 577 | overflow_register_combination) { |
| 578 | int32_t ii = *i; |
| 579 | int32_t jj = *j; |
| 580 | enum OverflowBranchType branchType = *br; |
| 581 | struct OverflowRegisterCombination rc = *regComb; |
| 582 | |
| 583 | // If left and right register are same then left and right |
| 584 | // test values must also be same, otherwise we skip the test |
| 585 | if (rc.left.code() == rc.right.code()) { |
| 586 | if (ii != jj) { |
| 587 | continue; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | bool res1 = runOverflow<int32_t>( |
| 592 | ii, jj, [branchType, rc](MacroAssembler* masm, int32_t valLeft, |
| 593 | int32_t valRight) { |
| 594 | Label overflow, no_overflow, end; |
| 595 | __ li(rc.left, valLeft); |
| 596 | __ li(rc.right, valRight); |
| 597 | switch (branchType) { |
| 598 | case kAddBranchOverflow: |
| 599 | __ AddBranchOvf(rc.dst, rc.left, rc.right, &overflow, |
| 600 | &no_overflow, rc.scratch); |
| 601 | break; |
| 602 | case kSubBranchOverflow: |
| 603 | __ SubBranchOvf(rc.dst, rc.left, rc.right, &overflow, |
| 604 | &no_overflow, rc.scratch); |
| 605 | break; |
| 606 | } |
| 607 | __ li(v0, 2); |
| 608 | __ Branch(&end); |
| 609 | __ bind(&overflow); |
| 610 | __ li(v0, 1); |
| 611 | __ Branch(&end); |
| 612 | __ bind(&no_overflow); |
| 613 | __ li(v0, 0); |
| 614 | __ bind(&end); |
| 615 | }); |
| 616 | |
| 617 | bool res2 = runOverflow<int32_t>( |
| 618 | ii, jj, [branchType, rc](MacroAssembler* masm, int32_t valLeft, |
| 619 | int32_t valRight) { |
| 620 | Label overflow, no_overflow, end; |
| 621 | __ li(rc.left, valLeft); |
| 622 | switch (branchType) { |
| 623 | case kAddBranchOverflow: |
| 624 | __ AddBranchOvf(rc.dst, rc.left, Operand(valRight), |
| 625 | &overflow, &no_overflow, rc.scratch); |
| 626 | break; |
| 627 | case kSubBranchOverflow: |
| 628 | __ SubBranchOvf(rc.dst, rc.left, Operand(valRight), |
| 629 | &overflow, &no_overflow, rc.scratch); |
| 630 | break; |
| 631 | } |
| 632 | __ li(v0, 2); |
| 633 | __ Branch(&end); |
| 634 | __ bind(&overflow); |
| 635 | __ li(v0, 1); |
| 636 | __ Branch(&end); |
| 637 | __ bind(&no_overflow); |
| 638 | __ li(v0, 0); |
| 639 | __ bind(&end); |
| 640 | }); |
| 641 | |
| 642 | switch (branchType) { |
| 643 | case kAddBranchOverflow: |
| 644 | CHECK_EQ(IsAddOverflow<int32_t>(ii, jj), res1); |
| 645 | CHECK_EQ(IsAddOverflow<int32_t>(ii, jj), res2); |
| 646 | break; |
| 647 | case kSubBranchOverflow: |
| 648 | CHECK_EQ(IsSubOverflow<int32_t>(ii, jj), res1); |
| 649 | CHECK_EQ(IsSubOverflow<int32_t>(ii, jj), res2); |
| 650 | break; |
| 651 | default: |
| 652 | UNREACHABLE(); |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | TEST(BranchOverflowInt32LeftLabel) { |
| 661 | FOR_INT32_INPUTS(i, overflow_int32_test_values) { |
| 662 | FOR_INT32_INPUTS(j, overflow_int32_test_values) { |
| 663 | FOR_ENUM_INPUTS(br, OverflowBranchType, overflow_branch_type) { |
| 664 | FOR_STRUCT_INPUTS(regComb, OverflowRegisterCombination, |
| 665 | overflow_register_combination) { |
| 666 | int32_t ii = *i; |
| 667 | int32_t jj = *j; |
| 668 | enum OverflowBranchType branchType = *br; |
| 669 | struct OverflowRegisterCombination rc = *regComb; |
| 670 | |
| 671 | // If left and right register are same then left and right |
| 672 | // test values must also be same, otherwise we skip the test |
| 673 | if (rc.left.code() == rc.right.code()) { |
| 674 | if (ii != jj) { |
| 675 | continue; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | bool res1 = runOverflow<int32_t>( |
| 680 | ii, jj, [branchType, rc](MacroAssembler* masm, int32_t valLeft, |
| 681 | int32_t valRight) { |
| 682 | Label overflow, end; |
| 683 | __ li(rc.left, valLeft); |
| 684 | __ li(rc.right, valRight); |
| 685 | switch (branchType) { |
| 686 | case kAddBranchOverflow: |
| 687 | __ AddBranchOvf(rc.dst, rc.left, rc.right, &overflow, NULL, |
| 688 | rc.scratch); |
| 689 | break; |
| 690 | case kSubBranchOverflow: |
| 691 | __ SubBranchOvf(rc.dst, rc.left, rc.right, &overflow, NULL, |
| 692 | rc.scratch); |
| 693 | break; |
| 694 | } |
| 695 | __ li(v0, 0); |
| 696 | __ Branch(&end); |
| 697 | __ bind(&overflow); |
| 698 | __ li(v0, 1); |
| 699 | __ bind(&end); |
| 700 | }); |
| 701 | |
| 702 | bool res2 = runOverflow<int32_t>( |
| 703 | ii, jj, [branchType, rc](MacroAssembler* masm, int32_t valLeft, |
| 704 | int32_t valRight) { |
| 705 | Label overflow, end; |
| 706 | __ li(rc.left, valLeft); |
| 707 | switch (branchType) { |
| 708 | case kAddBranchOverflow: |
| 709 | __ AddBranchOvf(rc.dst, rc.left, Operand(valRight), |
| 710 | &overflow, NULL, rc.scratch); |
| 711 | break; |
| 712 | case kSubBranchOverflow: |
| 713 | __ SubBranchOvf(rc.dst, rc.left, Operand(valRight), |
| 714 | &overflow, NULL, rc.scratch); |
| 715 | break; |
| 716 | } |
| 717 | __ li(v0, 0); |
| 718 | __ Branch(&end); |
| 719 | __ bind(&overflow); |
| 720 | __ li(v0, 1); |
| 721 | __ bind(&end); |
| 722 | }); |
| 723 | |
| 724 | switch (branchType) { |
| 725 | case kAddBranchOverflow: |
| 726 | CHECK_EQ(IsAddOverflow<int32_t>(ii, jj), res1); |
| 727 | CHECK_EQ(IsAddOverflow<int32_t>(ii, jj), res2); |
| 728 | break; |
| 729 | case kSubBranchOverflow: |
| 730 | CHECK_EQ(IsSubOverflow<int32_t>(ii, jj), res1); |
| 731 | CHECK_EQ(IsSubOverflow<int32_t>(ii, jj), res2); |
| 732 | break; |
| 733 | default: |
| 734 | UNREACHABLE(); |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | TEST(BranchOverflowInt32RightLabel) { |
| 743 | FOR_INT32_INPUTS(i, overflow_int32_test_values) { |
| 744 | FOR_INT32_INPUTS(j, overflow_int32_test_values) { |
| 745 | FOR_ENUM_INPUTS(br, OverflowBranchType, overflow_branch_type) { |
| 746 | FOR_STRUCT_INPUTS(regComb, OverflowRegisterCombination, |
| 747 | overflow_register_combination) { |
| 748 | int32_t ii = *i; |
| 749 | int32_t jj = *j; |
| 750 | enum OverflowBranchType branchType = *br; |
| 751 | struct OverflowRegisterCombination rc = *regComb; |
| 752 | |
| 753 | // If left and right register are same then left and right |
| 754 | // test values must also be same, otherwise we skip the test |
| 755 | if (rc.left.code() == rc.right.code()) { |
| 756 | if (ii != jj) { |
| 757 | continue; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | bool res1 = runOverflow<int32_t>( |
| 762 | ii, jj, [branchType, rc](MacroAssembler* masm, int32_t valLeft, |
| 763 | int32_t valRight) { |
| 764 | Label no_overflow, end; |
| 765 | __ li(rc.left, valLeft); |
| 766 | __ li(rc.right, valRight); |
| 767 | switch (branchType) { |
| 768 | case kAddBranchOverflow: |
| 769 | __ AddBranchOvf(rc.dst, rc.left, rc.right, NULL, |
| 770 | &no_overflow, rc.scratch); |
| 771 | break; |
| 772 | case kSubBranchOverflow: |
| 773 | __ SubBranchOvf(rc.dst, rc.left, rc.right, NULL, |
| 774 | &no_overflow, rc.scratch); |
| 775 | break; |
| 776 | } |
| 777 | __ li(v0, 1); |
| 778 | __ Branch(&end); |
| 779 | __ bind(&no_overflow); |
| 780 | __ li(v0, 0); |
| 781 | __ bind(&end); |
| 782 | }); |
| 783 | |
| 784 | bool res2 = runOverflow<int32_t>( |
| 785 | ii, jj, [branchType, rc](MacroAssembler* masm, int32_t valLeft, |
| 786 | int32_t valRight) { |
| 787 | Label no_overflow, end; |
| 788 | __ li(rc.left, valLeft); |
| 789 | switch (branchType) { |
| 790 | case kAddBranchOverflow: |
| 791 | __ AddBranchOvf(rc.dst, rc.left, Operand(valRight), NULL, |
| 792 | &no_overflow, rc.scratch); |
| 793 | break; |
| 794 | case kSubBranchOverflow: |
| 795 | __ SubBranchOvf(rc.dst, rc.left, Operand(valRight), NULL, |
| 796 | &no_overflow, rc.scratch); |
| 797 | break; |
| 798 | } |
| 799 | __ li(v0, 1); |
| 800 | __ Branch(&end); |
| 801 | __ bind(&no_overflow); |
| 802 | __ li(v0, 0); |
| 803 | __ bind(&end); |
| 804 | }); |
| 805 | |
| 806 | switch (branchType) { |
| 807 | case kAddBranchOverflow: |
| 808 | CHECK_EQ(IsAddOverflow<int32_t>(ii, jj), res1); |
| 809 | CHECK_EQ(IsAddOverflow<int32_t>(ii, jj), res2); |
| 810 | break; |
| 811 | case kSubBranchOverflow: |
| 812 | CHECK_EQ(IsSubOverflow<int32_t>(ii, jj), res1); |
| 813 | CHECK_EQ(IsSubOverflow<int32_t>(ii, jj), res2); |
| 814 | break; |
| 815 | default: |
| 816 | UNREACHABLE(); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 824 | TEST(min_max_nan) { |
| 825 | CcTest::InitializeVM(); |
| 826 | Isolate* isolate = CcTest::i_isolate(); |
| 827 | HandleScope scope(isolate); |
| 828 | MacroAssembler assembler(isolate, nullptr, 0, |
| 829 | v8::internal::CodeObjectRequired::kYes); |
| 830 | MacroAssembler* masm = &assembler; |
| 831 | |
| 832 | struct TestFloat { |
| 833 | double a; |
| 834 | double b; |
| 835 | double c; |
| 836 | double d; |
| 837 | float e; |
| 838 | float f; |
| 839 | float g; |
| 840 | float h; |
| 841 | }; |
| 842 | |
| 843 | TestFloat test; |
| 844 | const double dnan = std::numeric_limits<double>::quiet_NaN(); |
| 845 | const double dinf = std::numeric_limits<double>::infinity(); |
| 846 | const double dminf = -std::numeric_limits<double>::infinity(); |
| 847 | const float fnan = std::numeric_limits<float>::quiet_NaN(); |
| 848 | const float finf = std::numeric_limits<float>::infinity(); |
| 849 | const float fminf = std::numeric_limits<float>::infinity(); |
| 850 | const int kTableLength = 13; |
| 851 | |
| 852 | double inputsa[kTableLength] = {2.0, 3.0, -0.0, 0.0, 42.0, dinf, dminf, |
| 853 | dinf, dnan, 3.0, dinf, dnan, dnan}; |
| 854 | double inputsb[kTableLength] = {3.0, 2.0, 0.0, -0.0, dinf, 42.0, dinf, |
| 855 | dminf, 3.0, dnan, dnan, dinf, dnan}; |
| 856 | double outputsdmin[kTableLength] = {2.0, 2.0, -0.0, -0.0, 42.0, |
| 857 | 42.0, dminf, dminf, dnan, dnan, |
| 858 | dnan, dnan, dnan}; |
| 859 | double outputsdmax[kTableLength] = {3.0, 3.0, 0.0, 0.0, dinf, dinf, dinf, |
| 860 | dinf, dnan, dnan, dnan, dnan, dnan}; |
| 861 | |
| 862 | float inputse[kTableLength] = {2.0, 3.0, -0.0, 0.0, 42.0, finf, fminf, |
| 863 | finf, fnan, 3.0, finf, fnan, fnan}; |
| 864 | float inputsf[kTableLength] = {3.0, 2.0, 0.0, -0.0, finf, 42.0, finf, |
| 865 | fminf, 3.0, fnan, fnan, finf, fnan}; |
| 866 | float outputsfmin[kTableLength] = {2.0, 2.0, -0.0, -0.0, 42.0, 42.0, fminf, |
| 867 | fminf, fnan, fnan, fnan, fnan, fnan}; |
| 868 | float outputsfmax[kTableLength] = {3.0, 3.0, 0.0, 0.0, finf, finf, finf, |
| 869 | finf, fnan, fnan, fnan, fnan, fnan}; |
| 870 | |
| 871 | auto handle_dnan = [masm](FPURegister dst, Label* nan, Label* back) { |
| 872 | __ bind(nan); |
| 873 | __ LoadRoot(at, Heap::kNanValueRootIndex); |
| 874 | __ ldc1(dst, FieldMemOperand(at, HeapNumber::kValueOffset)); |
| 875 | __ Branch(back); |
| 876 | }; |
| 877 | |
| 878 | auto handle_snan = [masm, fnan](FPURegister dst, Label* nan, Label* back) { |
| 879 | __ bind(nan); |
| 880 | __ Move(dst, fnan); |
| 881 | __ Branch(back); |
| 882 | }; |
| 883 | |
| 884 | Label handle_mind_nan, handle_maxd_nan, handle_mins_nan, handle_maxs_nan; |
| 885 | Label back_mind_nan, back_maxd_nan, back_mins_nan, back_maxs_nan; |
| 886 | |
| 887 | __ push(s6); |
| 888 | __ InitializeRootRegister(); |
| 889 | __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, a))); |
| 890 | __ ldc1(f8, MemOperand(a0, offsetof(TestFloat, b))); |
| 891 | __ lwc1(f2, MemOperand(a0, offsetof(TestFloat, e))); |
| 892 | __ lwc1(f6, MemOperand(a0, offsetof(TestFloat, f))); |
| 893 | __ MinNaNCheck_d(f10, f4, f8, &handle_mind_nan); |
| 894 | __ bind(&back_mind_nan); |
| 895 | __ MaxNaNCheck_d(f12, f4, f8, &handle_maxd_nan); |
| 896 | __ bind(&back_maxd_nan); |
| 897 | __ MinNaNCheck_s(f14, f2, f6, &handle_mins_nan); |
| 898 | __ bind(&back_mins_nan); |
| 899 | __ MaxNaNCheck_s(f16, f2, f6, &handle_maxs_nan); |
| 900 | __ bind(&back_maxs_nan); |
| 901 | __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, c))); |
| 902 | __ sdc1(f12, MemOperand(a0, offsetof(TestFloat, d))); |
| 903 | __ swc1(f14, MemOperand(a0, offsetof(TestFloat, g))); |
| 904 | __ swc1(f16, MemOperand(a0, offsetof(TestFloat, h))); |
| 905 | __ pop(s6); |
| 906 | __ jr(ra); |
| 907 | __ nop(); |
| 908 | |
| 909 | handle_dnan(f10, &handle_mind_nan, &back_mind_nan); |
| 910 | handle_dnan(f12, &handle_maxd_nan, &back_maxd_nan); |
| 911 | handle_snan(f14, &handle_mins_nan, &back_mins_nan); |
| 912 | handle_snan(f16, &handle_maxs_nan, &back_maxs_nan); |
| 913 | |
| 914 | CodeDesc desc; |
| 915 | masm->GetCode(&desc); |
| 916 | Handle<Code> code = isolate->factory()->NewCode( |
| 917 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 918 | ::F3 f = FUNCTION_CAST<::F3>(code->entry()); |
| 919 | for (int i = 0; i < kTableLength; i++) { |
| 920 | test.a = inputsa[i]; |
| 921 | test.b = inputsb[i]; |
| 922 | test.e = inputse[i]; |
| 923 | test.f = inputsf[i]; |
| 924 | |
| 925 | CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0); |
| 926 | |
| 927 | CHECK_EQ(0, memcmp(&test.c, &outputsdmin[i], sizeof(test.c))); |
| 928 | CHECK_EQ(0, memcmp(&test.d, &outputsdmax[i], sizeof(test.d))); |
| 929 | CHECK_EQ(0, memcmp(&test.g, &outputsfmin[i], sizeof(test.g))); |
| 930 | CHECK_EQ(0, memcmp(&test.h, &outputsfmax[i], sizeof(test.h))); |
| 931 | } |
| 932 | } |
| 933 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 934 | template <typename IN_TYPE, typename Func> |
| 935 | bool run_Unaligned(char* memory_buffer, int32_t in_offset, int32_t out_offset, |
| 936 | IN_TYPE value, Func GenerateUnalignedInstructionFunc) { |
| 937 | typedef int32_t (*F_CVT)(char* x0, int x1, int x2, int x3, int x4); |
| 938 | |
| 939 | Isolate* isolate = CcTest::i_isolate(); |
| 940 | HandleScope scope(isolate); |
| 941 | MacroAssembler assm(isolate, nullptr, 0, |
| 942 | v8::internal::CodeObjectRequired::kYes); |
| 943 | MacroAssembler* masm = &assm; |
| 944 | IN_TYPE res; |
| 945 | |
| 946 | GenerateUnalignedInstructionFunc(masm, in_offset, out_offset); |
| 947 | __ jr(ra); |
| 948 | __ nop(); |
| 949 | |
| 950 | CodeDesc desc; |
| 951 | assm.GetCode(&desc); |
| 952 | Handle<Code> code = isolate->factory()->NewCode( |
| 953 | desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 954 | |
| 955 | F_CVT f = FUNCTION_CAST<F_CVT>(code->entry()); |
| 956 | |
| 957 | MemCopy(memory_buffer + in_offset, &value, sizeof(IN_TYPE)); |
| 958 | CALL_GENERATED_CODE(isolate, f, memory_buffer, 0, 0, 0, 0); |
| 959 | MemCopy(&res, memory_buffer + out_offset, sizeof(IN_TYPE)); |
| 960 | |
| 961 | return res == value; |
| 962 | } |
| 963 | |
| 964 | static const std::vector<uint64_t> unsigned_test_values() { |
| 965 | static const uint64_t kValues[] = { |
| 966 | 0x2180f18a06384414, 0x000a714532102277, 0xbc1acccf180649f0, |
| 967 | 0x8000000080008000, 0x0000000000000001, 0xffffffffffffffff, |
| 968 | }; |
| 969 | return std::vector<uint64_t>(&kValues[0], &kValues[arraysize(kValues)]); |
| 970 | } |
| 971 | |
| 972 | static const std::vector<int32_t> unsigned_test_offset() { |
| 973 | static const int32_t kValues[] = {// value, offset |
| 974 | -132 * KB, -21 * KB, 0, 19 * KB, 135 * KB}; |
| 975 | return std::vector<int32_t>(&kValues[0], &kValues[arraysize(kValues)]); |
| 976 | } |
| 977 | |
| 978 | static const std::vector<int32_t> unsigned_test_offset_increment() { |
| 979 | static const int32_t kValues[] = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}; |
| 980 | return std::vector<int32_t>(&kValues[0], &kValues[arraysize(kValues)]); |
| 981 | } |
| 982 | |
| 983 | TEST(Ulh) { |
| 984 | CcTest::InitializeVM(); |
| 985 | |
| 986 | static const int kBufferSize = 300 * KB; |
| 987 | char memory_buffer[kBufferSize]; |
| 988 | char* buffer_middle = memory_buffer + (kBufferSize / 2); |
| 989 | |
| 990 | FOR_UINT64_INPUTS(i, unsigned_test_values) { |
| 991 | FOR_INT32_INPUTS2(j1, j2, unsigned_test_offset) { |
| 992 | FOR_INT32_INPUTS2(k1, k2, unsigned_test_offset_increment) { |
| 993 | uint16_t value = static_cast<uint64_t>(*i & 0xFFFF); |
| 994 | int32_t in_offset = *j1 + *k1; |
| 995 | int32_t out_offset = *j2 + *k2; |
| 996 | |
| 997 | CHECK_EQ(true, run_Unaligned<uint16_t>( |
| 998 | buffer_middle, in_offset, out_offset, value, |
| 999 | [](MacroAssembler* masm, int32_t in_offset, |
| 1000 | int32_t out_offset) { |
| 1001 | __ Ulh(v0, MemOperand(a0, in_offset)); |
| 1002 | __ Ush(v0, MemOperand(a0, out_offset), v0); |
| 1003 | })); |
| 1004 | CHECK_EQ(true, run_Unaligned<uint16_t>( |
| 1005 | buffer_middle, in_offset, out_offset, value, |
| 1006 | [](MacroAssembler* masm, int32_t in_offset, |
| 1007 | int32_t out_offset) { |
| 1008 | __ mov(t0, a0); |
| 1009 | __ Ulh(a0, MemOperand(a0, in_offset)); |
| 1010 | __ Ush(a0, MemOperand(t0, out_offset), v0); |
| 1011 | })); |
| 1012 | CHECK_EQ(true, run_Unaligned<uint16_t>( |
| 1013 | buffer_middle, in_offset, out_offset, value, |
| 1014 | [](MacroAssembler* masm, int32_t in_offset, |
| 1015 | int32_t out_offset) { |
| 1016 | __ mov(t0, a0); |
| 1017 | __ Ulhu(a0, MemOperand(a0, in_offset)); |
| 1018 | __ Ush(a0, MemOperand(t0, out_offset), t1); |
| 1019 | })); |
| 1020 | CHECK_EQ(true, run_Unaligned<uint16_t>( |
| 1021 | buffer_middle, in_offset, out_offset, value, |
| 1022 | [](MacroAssembler* masm, int32_t in_offset, |
| 1023 | int32_t out_offset) { |
| 1024 | __ Ulhu(v0, MemOperand(a0, in_offset)); |
| 1025 | __ Ush(v0, MemOperand(a0, out_offset), t1); |
| 1026 | })); |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | TEST(Ulh_bitextension) { |
| 1033 | CcTest::InitializeVM(); |
| 1034 | |
| 1035 | static const int kBufferSize = 300 * KB; |
| 1036 | char memory_buffer[kBufferSize]; |
| 1037 | char* buffer_middle = memory_buffer + (kBufferSize / 2); |
| 1038 | |
| 1039 | FOR_UINT64_INPUTS(i, unsigned_test_values) { |
| 1040 | FOR_INT32_INPUTS2(j1, j2, unsigned_test_offset) { |
| 1041 | FOR_INT32_INPUTS2(k1, k2, unsigned_test_offset_increment) { |
| 1042 | uint16_t value = static_cast<uint64_t>(*i & 0xFFFF); |
| 1043 | int32_t in_offset = *j1 + *k1; |
| 1044 | int32_t out_offset = *j2 + *k2; |
| 1045 | |
| 1046 | CHECK_EQ(true, run_Unaligned<uint16_t>( |
| 1047 | buffer_middle, in_offset, out_offset, value, |
| 1048 | [](MacroAssembler* masm, int32_t in_offset, |
| 1049 | int32_t out_offset) { |
| 1050 | Label success, fail, end, different; |
| 1051 | __ Ulh(t0, MemOperand(a0, in_offset)); |
| 1052 | __ Ulhu(t1, MemOperand(a0, in_offset)); |
| 1053 | __ Branch(&different, ne, t0, Operand(t1)); |
| 1054 | |
| 1055 | // If signed and unsigned values are same, check |
| 1056 | // the upper bits to see if they are zero |
| 1057 | __ sra(t0, t0, 15); |
| 1058 | __ Branch(&success, eq, t0, Operand(zero_reg)); |
| 1059 | __ Branch(&fail); |
| 1060 | |
| 1061 | // If signed and unsigned values are different, |
| 1062 | // check that the upper bits are complementary |
| 1063 | __ bind(&different); |
| 1064 | __ sra(t1, t1, 15); |
| 1065 | __ Branch(&fail, ne, t1, Operand(1)); |
| 1066 | __ sra(t0, t0, 15); |
| 1067 | __ addiu(t0, t0, 1); |
| 1068 | __ Branch(&fail, ne, t0, Operand(zero_reg)); |
| 1069 | // Fall through to success |
| 1070 | |
| 1071 | __ bind(&success); |
| 1072 | __ Ulh(t0, MemOperand(a0, in_offset)); |
| 1073 | __ Ush(t0, MemOperand(a0, out_offset), v0); |
| 1074 | __ Branch(&end); |
| 1075 | __ bind(&fail); |
| 1076 | __ Ush(zero_reg, MemOperand(a0, out_offset), v0); |
| 1077 | __ bind(&end); |
| 1078 | })); |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | TEST(Ulw) { |
| 1085 | CcTest::InitializeVM(); |
| 1086 | |
| 1087 | static const int kBufferSize = 300 * KB; |
| 1088 | char memory_buffer[kBufferSize]; |
| 1089 | char* buffer_middle = memory_buffer + (kBufferSize / 2); |
| 1090 | |
| 1091 | FOR_UINT64_INPUTS(i, unsigned_test_values) { |
| 1092 | FOR_INT32_INPUTS2(j1, j2, unsigned_test_offset) { |
| 1093 | FOR_INT32_INPUTS2(k1, k2, unsigned_test_offset_increment) { |
| 1094 | uint32_t value = static_cast<uint32_t>(*i & 0xFFFFFFFF); |
| 1095 | int32_t in_offset = *j1 + *k1; |
| 1096 | int32_t out_offset = *j2 + *k2; |
| 1097 | |
| 1098 | CHECK_EQ(true, run_Unaligned<uint32_t>( |
| 1099 | buffer_middle, in_offset, out_offset, value, |
| 1100 | [](MacroAssembler* masm, int32_t in_offset, |
| 1101 | int32_t out_offset) { |
| 1102 | __ Ulw(v0, MemOperand(a0, in_offset)); |
| 1103 | __ Usw(v0, MemOperand(a0, out_offset)); |
| 1104 | })); |
| 1105 | CHECK_EQ(true, |
| 1106 | run_Unaligned<uint32_t>( |
| 1107 | buffer_middle, in_offset, out_offset, (uint32_t)value, |
| 1108 | [](MacroAssembler* masm, int32_t in_offset, |
| 1109 | int32_t out_offset) { |
| 1110 | __ mov(t0, a0); |
| 1111 | __ Ulw(a0, MemOperand(a0, in_offset)); |
| 1112 | __ Usw(a0, MemOperand(t0, out_offset)); |
| 1113 | })); |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | TEST(Ulwc1) { |
| 1120 | CcTest::InitializeVM(); |
| 1121 | |
| 1122 | static const int kBufferSize = 300 * KB; |
| 1123 | char memory_buffer[kBufferSize]; |
| 1124 | char* buffer_middle = memory_buffer + (kBufferSize / 2); |
| 1125 | |
| 1126 | FOR_UINT64_INPUTS(i, unsigned_test_values) { |
| 1127 | FOR_INT32_INPUTS2(j1, j2, unsigned_test_offset) { |
| 1128 | FOR_INT32_INPUTS2(k1, k2, unsigned_test_offset_increment) { |
| 1129 | float value = static_cast<float>(*i & 0xFFFFFFFF); |
| 1130 | int32_t in_offset = *j1 + *k1; |
| 1131 | int32_t out_offset = *j2 + *k2; |
| 1132 | |
| 1133 | CHECK_EQ(true, run_Unaligned<float>( |
| 1134 | buffer_middle, in_offset, out_offset, value, |
| 1135 | [](MacroAssembler* masm, int32_t in_offset, |
| 1136 | int32_t out_offset) { |
| 1137 | __ Ulwc1(f0, MemOperand(a0, in_offset), t0); |
| 1138 | __ Uswc1(f0, MemOperand(a0, out_offset), t0); |
| 1139 | })); |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | TEST(Uldc1) { |
| 1146 | CcTest::InitializeVM(); |
| 1147 | |
| 1148 | static const int kBufferSize = 300 * KB; |
| 1149 | char memory_buffer[kBufferSize]; |
| 1150 | char* buffer_middle = memory_buffer + (kBufferSize / 2); |
| 1151 | |
| 1152 | FOR_UINT64_INPUTS(i, unsigned_test_values) { |
| 1153 | FOR_INT32_INPUTS2(j1, j2, unsigned_test_offset) { |
| 1154 | FOR_INT32_INPUTS2(k1, k2, unsigned_test_offset_increment) { |
| 1155 | double value = static_cast<double>(*i); |
| 1156 | int32_t in_offset = *j1 + *k1; |
| 1157 | int32_t out_offset = *j2 + *k2; |
| 1158 | |
| 1159 | CHECK_EQ(true, run_Unaligned<double>( |
| 1160 | buffer_middle, in_offset, out_offset, value, |
| 1161 | [](MacroAssembler* masm, int32_t in_offset, |
| 1162 | int32_t out_offset) { |
| 1163 | __ Uldc1(f0, MemOperand(a0, in_offset), t0); |
| 1164 | __ Usdc1(f0, MemOperand(a0, out_offset), t0); |
| 1165 | })); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1171 | #undef __ |