test-assembler-mips64.cc 176 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 29
#include <iostream>  // NOLINT(readability/streams)

30 31
#include "src/v8.h"

32
#include "src/base/utils/random-number-generator.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include "src/disassembler.h"
#include "src/factory.h"
#include "src/macro-assembler.h"
#include "src/mips64/macro-assembler-mips64.h"
#include "src/mips64/simulator-mips64.h"

#include "test/cctest/cctest.h"

using namespace v8::internal;


// Define these function prototypes to match JSEntryFunction in execution.cc.
typedef Object* (*F1)(int x, int p1, int p2, int p3, int p4);
typedef Object* (*F2)(int x, int y, int p2, int p3, int p4);
typedef Object* (*F3)(void* p, int p1, int p2, int p3, int p4);
48
typedef Object* (*F4)(int64_t x, int64_t y, int64_t p2, int64_t p3, int64_t p4);
49 50 51 52 53 54 55 56 57


#define __ assm.

TEST(MIPS0) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

58
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
59 60 61 62 63 64 65 66 67 68 69

  // Addition.
  __ addu(v0, a0, a1);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F2 f = FUNCTION_CAST<F2>(code->entry());
70 71
  int64_t res = reinterpret_cast<int64_t>(
      CALL_GENERATED_CODE(isolate, f, 0xab0, 0xc, 0, 0, 0));
72 73 74 75 76 77 78 79 80
  CHECK_EQ(0xabcL, res);
}


TEST(MIPS1) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

81
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  Label L, C;

  __ mov(a1, a0);
  __ li(v0, 0);
  __ b(&C);
  __ nop();

  __ bind(&L);
  __ addu(v0, v0, a1);
  __ addiu(a1, a1, -1);

  __ bind(&C);
  __ xori(v1, a1, 0);
  __ Branch(&L, ne, v1, Operand((int64_t)0));
  __ nop();

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F1 f = FUNCTION_CAST<F1>(code->entry());
106 107
  int64_t res = reinterpret_cast<int64_t>(
      CALL_GENERATED_CODE(isolate, f, 50, 0, 0, 0, 0));
108 109 110 111 112 113 114 115 116
  CHECK_EQ(1275L, res);
}


TEST(MIPS2) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

117
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

  Label exit, error;

  // ----- Test all instructions.

  // Test lui, ori, and addiu, used in the li pseudo-instruction.
  // This way we can then safely load registers with chosen values.

  __ ori(a4, zero_reg, 0);
  __ lui(a4, 0x1234);
  __ ori(a4, a4, 0);
  __ ori(a4, a4, 0x0f0f);
  __ ori(a4, a4, 0xf0f0);
  __ addiu(a5, a4, 1);
  __ addiu(a6, a5, -0x10);

  // Load values in temporary registers.
  __ li(a4, 0x00000004);
  __ li(a5, 0x00001234);
  __ li(a6, 0x12345678);
  __ li(a7, 0x7fffffff);
  __ li(t0, 0xfffffffc);
  __ li(t1, 0xffffedcc);
  __ li(t2, 0xedcba988);
  __ li(t3, 0x80000000);

  // SPECIAL class.
  __ srl(v0, a6, 8);    // 0x00123456
  __ sll(v0, v0, 11);   // 0x91a2b000
  __ sra(v0, v0, 3);    // 0xf2345600
  __ srav(v0, v0, a4);  // 0xff234560
  __ sllv(v0, v0, a4);  // 0xf2345600
  __ srlv(v0, v0, a4);  // 0x0f234560
  __ Branch(&error, ne, v0, Operand(0x0f234560));
  __ nop();

  __ addu(v0, a4, a5);  // 0x00001238
  __ subu(v0, v0, a4);  // 0x00001234
  __ Branch(&error, ne, v0, Operand(0x00001234));
  __ nop();
  __ addu(v1, a7, a4);  // 32bit addu result is sign-extended into 64bit reg.
  __ Branch(&error, ne, v1, Operand(0xffffffff80000003));
  __ nop();
  __ subu(v1, t3, a4);  // 0x7ffffffc
  __ Branch(&error, ne, v1, Operand(0x7ffffffc));
  __ nop();

  __ and_(v0, a5, a6);  // 0x0000000000001230
  __ or_(v0, v0, a5);   // 0x0000000000001234
  __ xor_(v0, v0, a6);  // 0x000000001234444c
  __ nor(v0, v0, a6);   // 0xffffffffedcba987
  __ Branch(&error, ne, v0, Operand(0xffffffffedcba983));
  __ nop();

  // Shift both 32bit number to left, to preserve meaning of next comparison.
  __ dsll32(a7, a7, 0);
  __ dsll32(t3, t3, 0);

  __ slt(v0, t3, a7);
  __ Branch(&error, ne, v0, Operand(0x1));
  __ nop();
  __ sltu(v0, t3, a7);
  __ Branch(&error, ne, v0, Operand(zero_reg));
  __ nop();

  // Restore original values in registers.
  __ dsrl32(a7, a7, 0);
  __ dsrl32(t3, t3, 0);
  // End of SPECIAL class.

  __ addiu(v0, zero_reg, 0x7421);  // 0x00007421
  __ addiu(v0, v0, -0x1);          // 0x00007420
  __ addiu(v0, v0, -0x20);         // 0x00007400
  __ Branch(&error, ne, v0, Operand(0x00007400));
  __ nop();
  __ addiu(v1, a7, 0x1);  // 0x80000000 - result is sign-extended.
  __ Branch(&error, ne, v1, Operand(0xffffffff80000000));
  __ nop();

  __ slti(v0, a5, 0x00002000);  // 0x1
  __ slti(v0, v0, 0xffff8000);  // 0x0
  __ Branch(&error, ne, v0, Operand(zero_reg));
  __ nop();
  __ sltiu(v0, a5, 0x00002000);  // 0x1
  __ sltiu(v0, v0, 0x00008000);  // 0x1
  __ Branch(&error, ne, v0, Operand(0x1));
  __ nop();

  __ andi(v0, a5, 0xf0f0);  // 0x00001030
  __ ori(v0, v0, 0x8a00);   // 0x00009a30
  __ xori(v0, v0, 0x83cc);  // 0x000019fc
  __ Branch(&error, ne, v0, Operand(0x000019fc));
  __ nop();
  __ lui(v1, 0x8123);  // Result is sign-extended into 64bit register.
  __ Branch(&error, ne, v1, Operand(0xffffffff81230000));
  __ nop();

  // Bit twiddling instructions & conditional moves.
  // Uses a4-t3 as set above.
  __ Clz(v0, a4);       // 29
  __ Clz(v1, a5);       // 19
  __ addu(v0, v0, v1);  // 48
  __ Clz(v1, a6);       // 3
  __ addu(v0, v0, v1);  // 51
  __ Clz(v1, t3);       // 0
  __ addu(v0, v0, v1);  // 51
  __ Branch(&error, ne, v0, Operand(51));
  __ Movn(a0, a7, a4);  // Move a0<-a7 (a4 is NOT 0).
  __ Ins(a0, a5, 12, 8);  // 0x7ff34fff
  __ Branch(&error, ne, a0, Operand(0x7ff34fff));
  __ Movz(a0, t2, t3);    // a0 not updated (t3 is NOT 0).
  __ Ext(a1, a0, 8, 12);  // 0x34f
  __ Branch(&error, ne, a1, Operand(0x34f));
  __ Movz(a0, t2, v1);    // a0<-t2, v0 is 0, from 8 instr back.
  __ Branch(&error, ne, a0, Operand(t2));

  // Everything was correctly executed. Load the expected result.
  __ li(v0, 0x31415926);
  __ b(&exit);
  __ nop();

  __ bind(&error);
  // Got an error. Return a wrong result.
  __ li(v0, 666);

  __ bind(&exit);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F2 f = FUNCTION_CAST<F2>(code->entry());
252 253
  int64_t res = reinterpret_cast<int64_t>(
      CALL_GENERATED_CODE(isolate, f, 0xab0, 0xc, 0, 0, 0));
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

  CHECK_EQ(0x31415926L, res);
}


TEST(MIPS3) {
  // Test floating point instructions.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  typedef struct {
    double a;
    double b;
    double c;
    double d;
    double e;
    double f;
    double g;
    double h;
    double i;
275 276 277 278 279 280 281
    float fa;
    float fb;
    float fc;
    float fd;
    float fe;
    float ff;
    float fg;
282 283 284 285 286
  } T;
  T t;

  // Create a function that accepts &t, and loads, manipulates, and stores
  // the doubles t.a ... t.f.
287
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
288 289
  Label L, C;

290
  // Double precision floating point instructions.
291 292
  __ ldc1(f4, MemOperand(a0, offsetof(T, a)) );
  __ ldc1(f6, MemOperand(a0, offsetof(T, b)) );
293
  __ add_d(f8, f4, f6);
294
  __ sdc1(f8, MemOperand(a0, offsetof(T, c)) );  // c = a + b.
295 296 297 298

  __ mov_d(f10, f8);  // c
  __ neg_d(f12, f6);  // -b
  __ sub_d(f10, f10, f12);
299
  __ sdc1(f10, MemOperand(a0, offsetof(T, d)) );  // d = c - (-b).
300

301
  __ sdc1(f4, MemOperand(a0, offsetof(T, b)) );   // b = a.
302 303 304 305 306

  __ li(a4, 120);
  __ mtc1(a4, f14);
  __ cvt_d_w(f14, f14);   // f14 = 120.0.
  __ mul_d(f10, f10, f14);
307
  __ sdc1(f10, MemOperand(a0, offsetof(T, e)) );  // e = d * 120 = 1.8066e16.
308 309

  __ div_d(f12, f10, f4);
310
  __ sdc1(f12, MemOperand(a0, offsetof(T, f)) );  // f = e / a = 120.44.
311 312

  __ sqrt_d(f14, f12);
313
  __ sdc1(f14, MemOperand(a0, offsetof(T, g)) );
314 315 316
  // g = sqrt(f) = 10.97451593465515908537

  if (kArchVariant == kMips64r2) {
317 318
    __ ldc1(f4, MemOperand(a0, offsetof(T, h)) );
    __ ldc1(f6, MemOperand(a0, offsetof(T, i)) );
319
    __ madd_d(f14, f6, f4, f6);
320
    __ sdc1(f14, MemOperand(a0, offsetof(T, h)) );
321 322
  }

323
  // Single precision floating point instructions.
324 325
  __ lwc1(f4, MemOperand(a0, offsetof(T, fa)) );
  __ lwc1(f6, MemOperand(a0, offsetof(T, fb)) );
326
  __ add_s(f8, f4, f6);
327
  __ swc1(f8, MemOperand(a0, offsetof(T, fc)) );  // fc = fa + fb.
328 329 330

  __ neg_s(f10, f6);  // -fb
  __ sub_s(f10, f8, f10);
331
  __ swc1(f10, MemOperand(a0, offsetof(T, fd)) );  // fd = fc - (-fb).
332

333
  __ swc1(f4, MemOperand(a0, offsetof(T, fb)) );   // fb = fa.
334 335 336 337 338

  __ li(t0, 120);
  __ mtc1(t0, f14);
  __ cvt_s_w(f14, f14);   // f14 = 120.0.
  __ mul_s(f10, f10, f14);
339
  __ swc1(f10, MemOperand(a0, offsetof(T, fe)) );  // fe = fd * 120
340 341

  __ div_s(f12, f10, f4);
342
  __ swc1(f12, MemOperand(a0, offsetof(T, ff)) );  // ff = fe / fa
343 344

  __ sqrt_s(f14, f12);
345
  __ swc1(f14, MemOperand(a0, offsetof(T, fg)) );
346

347 348 349 350 351 352 353 354
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
355
  // Double test values.
356 357 358 359 360 361 362 363
  t.a = 1.5e14;
  t.b = 2.75e11;
  t.c = 0.0;
  t.d = 0.0;
  t.e = 0.0;
  t.f = 0.0;
  t.h = 1.5;
  t.i = 2.75;
364 365 366 367 368 369 370
  // Single test values.
  t.fa = 1.5e6;
  t.fb = 2.75e4;
  t.fc = 0.0;
  t.fd = 0.0;
  t.fe = 0.0;
  t.ff = 0.0;
371
  Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
372
  USE(dummy);
373
  // Expected double results.
374 375 376 377 378 379 380 381 382 383
  CHECK_EQ(1.5e14, t.a);
  CHECK_EQ(1.5e14, t.b);
  CHECK_EQ(1.50275e14, t.c);
  CHECK_EQ(1.50550e14, t.d);
  CHECK_EQ(1.8066e16, t.e);
  CHECK_EQ(120.44, t.f);
  CHECK_EQ(10.97451593465515908537, t.g);
  if (kArchVariant == kMips64r2) {
    CHECK_EQ(6.875, t.h);
  }
384 385 386 387 388 389 390 391
  // Expected single results.
  CHECK_EQ(1.5e6, t.fa);
  CHECK_EQ(1.5e6, t.fb);
  CHECK_EQ(1.5275e06, t.fc);
  CHECK_EQ(1.5550e06, t.fd);
  CHECK_EQ(1.866e08, t.fe);
  CHECK_EQ(124.40000152587890625, t.ff);
  CHECK_EQ(11.1534748077392578125, t.fg);
392 393 394 395 396 397 398 399 400 401 402 403 404
}


TEST(MIPS4) {
  // Test moves between floating point and integer registers.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  typedef struct {
    double a;
    double b;
    double c;
405 406 407
    double d;
    int64_t high;
    int64_t low;
408 409 410 411 412 413
  } T;
  T t;

  Assembler assm(isolate, NULL, 0);
  Label L, C;

414 415
  __ ldc1(f4, MemOperand(a0, offsetof(T, a)));
  __ ldc1(f5, MemOperand(a0, offsetof(T, b)));
416 417 418 419 420 421 422 423 424 425 426 427 428 429

  // Swap f4 and f5, by using 3 integer registers, a4-a6,
  // both two 32-bit chunks, and one 64-bit chunk.
  // mXhc1 is mips32/64-r2 only, not r1,
  // but we will not support r1 in practice.
  __ mfc1(a4, f4);
  __ mfhc1(a5, f4);
  __ dmfc1(a6, f5);

  __ mtc1(a4, f5);
  __ mthc1(a5, f5);
  __ dmtc1(a6, f4);

  // Store the swapped f4 and f5 back to memory.
430 431
  __ sdc1(f4, MemOperand(a0, offsetof(T, a)));
  __ sdc1(f5, MemOperand(a0, offsetof(T, c)));
432 433

  // Test sign extension of move operations from coprocessor.
434
  __ ldc1(f4, MemOperand(a0, offsetof(T, d)));
435 436 437
  __ mfhc1(a4, f4);
  __ mfc1(a5, f4);

438 439
  __ sd(a4, MemOperand(a0, offsetof(T, high)));
  __ sd(a5, MemOperand(a0, offsetof(T, low)));
440 441 442 443 444 445 446 447 448 449 450 451

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  t.a = 1.5e22;
  t.b = 2.75e11;
  t.c = 17.17;
452
  t.d = -2.75e11;
453
  Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
454 455 456 457 458
  USE(dummy);

  CHECK_EQ(2.75e11, t.a);
  CHECK_EQ(2.75e11, t.b);
  CHECK_EQ(1.5e22, t.c);
459 460
  CHECK_EQ(static_cast<int64_t>(0xffffffffc25001d1L), t.high);
  CHECK_EQ(static_cast<int64_t>(0xffffffffbf800000L), t.low);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
}


TEST(MIPS5) {
  // Test conversions between doubles and integers.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  typedef struct {
    double a;
    double b;
    int i;
    int j;
  } T;
  T t;

  Assembler assm(isolate, NULL, 0);
  Label L, C;

  // Load all structure elements to registers.
482 483 484 485
  __ ldc1(f4, MemOperand(a0, offsetof(T, a)) );
  __ ldc1(f6, MemOperand(a0, offsetof(T, b)) );
  __ lw(a4, MemOperand(a0, offsetof(T, i)) );
  __ lw(a5, MemOperand(a0, offsetof(T, j)) );
486 487 488 489

  // Convert double in f4 to int in element i.
  __ cvt_w_d(f8, f4);
  __ mfc1(a6, f8);
490
  __ sw(a6, MemOperand(a0, offsetof(T, i)) );
491 492 493 494

  // Convert double in f6 to int in element j.
  __ cvt_w_d(f10, f6);
  __ mfc1(a7, f10);
495
  __ sw(a7, MemOperand(a0, offsetof(T, j)) );
496 497 498 499

  // Convert int in original i (a4) to double in a.
  __ mtc1(a4, f12);
  __ cvt_d_w(f0, f12);
500
  __ sdc1(f0, MemOperand(a0, offsetof(T, a)) );
501 502 503 504

  // Convert int in original j (a5) to double in b.
  __ mtc1(a5, f14);
  __ cvt_d_w(f2, f14);
505
  __ sdc1(f2, MemOperand(a0, offsetof(T, b)) );
506 507 508 509 510 511 512 513 514 515 516 517 518

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  t.a = 1.5e4;
  t.b = 2.75e8;
  t.i = 12345678;
  t.j = -100000;
519
  Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
  USE(dummy);

  CHECK_EQ(12345678.0, t.a);
  CHECK_EQ(-100000.0, t.b);
  CHECK_EQ(15000, t.i);
  CHECK_EQ(275000000, t.j);
}


TEST(MIPS6) {
  // Test simple memory loads and stores.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  typedef struct {
    uint32_t ui;
    int32_t si;
    int32_t r1;
    int32_t r2;
    int32_t r3;
    int32_t r4;
    int32_t r5;
    int32_t r6;
  } T;
  T t;

  Assembler assm(isolate, NULL, 0);
  Label L, C;

  // Basic word load/store.
551 552
  __ lw(a4, MemOperand(a0, offsetof(T, ui)) );
  __ sw(a4, MemOperand(a0, offsetof(T, r1)) );
553 554

  // lh with positive data.
555 556
  __ lh(a5, MemOperand(a0, offsetof(T, ui)) );
  __ sw(a5, MemOperand(a0, offsetof(T, r2)) );
557 558

  // lh with negative data.
559 560
  __ lh(a6, MemOperand(a0, offsetof(T, si)) );
  __ sw(a6, MemOperand(a0, offsetof(T, r3)) );
561 562

  // lhu with negative data.
563 564
  __ lhu(a7, MemOperand(a0, offsetof(T, si)) );
  __ sw(a7, MemOperand(a0, offsetof(T, r4)) );
565 566

  // lb with negative data.
567 568
  __ lb(t0, MemOperand(a0, offsetof(T, si)) );
  __ sw(t0, MemOperand(a0, offsetof(T, r5)) );
569 570 571 572

  // sh writes only 1/2 of word.
  __ lui(t1, 0x3333);
  __ ori(t1, t1, 0x3333);
573 574 575
  __ sw(t1, MemOperand(a0, offsetof(T, r6)) );
  __ lhu(t1, MemOperand(a0, offsetof(T, si)) );
  __ sh(t1, MemOperand(a0, offsetof(T, r6)) );
576 577 578 579 580 581 582 583 584 585 586

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  t.ui = 0x11223344;
  t.si = 0x99aabbcc;
587
  Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
588 589
  USE(dummy);

590
  CHECK_EQ(static_cast<int32_t>(0x11223344), t.r1);
591 592 593 594 595 596 597 598 599 600 601 602 603
  if (kArchEndian == kLittle)  {
    CHECK_EQ(static_cast<int32_t>(0x3344), t.r2);
    CHECK_EQ(static_cast<int32_t>(0xffffbbcc), t.r3);
    CHECK_EQ(static_cast<int32_t>(0x0000bbcc), t.r4);
    CHECK_EQ(static_cast<int32_t>(0xffffffcc), t.r5);
    CHECK_EQ(static_cast<int32_t>(0x3333bbcc), t.r6);
  } else {
    CHECK_EQ(static_cast<int32_t>(0x1122), t.r2);
    CHECK_EQ(static_cast<int32_t>(0xffff99aa), t.r3);
    CHECK_EQ(static_cast<int32_t>(0x000099aa), t.r4);
    CHECK_EQ(static_cast<int32_t>(0xffffff99), t.r5);
    CHECK_EQ(static_cast<int32_t>(0x99aa3333), t.r6);
  }
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
}


TEST(MIPS7) {
  // Test floating point compare and branch instructions.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  typedef struct {
    double a;
    double b;
    double c;
    double d;
    double e;
    double f;
    int32_t result;
  } T;
  T t;

  // Create a function that accepts &t, and loads, manipulates, and stores
  // the doubles t.a ... t.f.
626
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
627 628
  Label neither_is_nan, less_than, outa_here;

629 630
  __ ldc1(f4, MemOperand(a0, offsetof(T, a)) );
  __ ldc1(f6, MemOperand(a0, offsetof(T, b)) );
631 632 633 634 635 636 637
  if (kArchVariant != kMips64r6) {
    __ c(UN, D, f4, f6);
    __ bc1f(&neither_is_nan);
  } else {
    __ cmp(UN, L, f2, f4, f6);
    __ bc1eqz(&neither_is_nan, f2);
  }
638
  __ nop();
639
  __ sw(zero_reg, MemOperand(a0, offsetof(T, result)) );
640 641 642 643
  __ Branch(&outa_here);

  __ bind(&neither_is_nan);

644 645 646
  if (kArchVariant == kMips64r6) {
    __ cmp(OLT, L, f2, f6, f4);
    __ bc1nez(&less_than, f2);
647 648 649 650
  } else {
    __ c(OLT, D, f6, f4, 2);
    __ bc1t(&less_than, 2);
  }
651

652
  __ nop();
653
  __ sw(zero_reg, MemOperand(a0, offsetof(T, result)) );
654 655 656 657
  __ Branch(&outa_here);

  __ bind(&less_than);
  __ Addu(a4, zero_reg, Operand(1));
658
  __ sw(a4, MemOperand(a0, offsetof(T, result)) );  // Set true.
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679


  // This test-case should have additional tests.

  __ bind(&outa_here);

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  t.a = 1.5e14;
  t.b = 2.75e11;
  t.c = 2.0;
  t.d = -4.0;
  t.e = 0.0;
  t.f = 0.0;
  t.result = 0;
680
  Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
681 682 683 684 685 686 687 688
  USE(dummy);
  CHECK_EQ(1.5e14, t.a);
  CHECK_EQ(2.75e11, t.b);
  CHECK_EQ(1, t.result);
}


TEST(MIPS8) {
689 690 691 692 693
  if (kArchVariant == kMips64r2) {
    // Test ROTR and ROTRV instructions.
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
694

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    typedef struct {
      int32_t input;
      int32_t result_rotr_4;
      int32_t result_rotr_8;
      int32_t result_rotr_12;
      int32_t result_rotr_16;
      int32_t result_rotr_20;
      int32_t result_rotr_24;
      int32_t result_rotr_28;
      int32_t result_rotrv_4;
      int32_t result_rotrv_8;
      int32_t result_rotrv_12;
      int32_t result_rotrv_16;
      int32_t result_rotrv_20;
      int32_t result_rotrv_24;
      int32_t result_rotrv_28;
    } T;
    T t;
713

714 715
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
716

717
    // Basic word load.
718
    __ lw(a4, MemOperand(a0, offsetof(T, input)) );
719 720 721 722 723 724 725 726 727 728 729

    // ROTR instruction (called through the Ror macro).
    __ Ror(a5, a4, 0x0004);
    __ Ror(a6, a4, 0x0008);
    __ Ror(a7, a4, 0x000c);
    __ Ror(t0, a4, 0x0010);
    __ Ror(t1, a4, 0x0014);
    __ Ror(t2, a4, 0x0018);
    __ Ror(t3, a4, 0x001c);

    // Basic word store.
730 731 732 733 734 735 736
    __ sw(a5, MemOperand(a0, offsetof(T, result_rotr_4)) );
    __ sw(a6, MemOperand(a0, offsetof(T, result_rotr_8)) );
    __ sw(a7, MemOperand(a0, offsetof(T, result_rotr_12)) );
    __ sw(t0, MemOperand(a0, offsetof(T, result_rotr_16)) );
    __ sw(t1, MemOperand(a0, offsetof(T, result_rotr_20)) );
    __ sw(t2, MemOperand(a0, offsetof(T, result_rotr_24)) );
    __ sw(t3, MemOperand(a0, offsetof(T, result_rotr_28)) );
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

    // ROTRV instruction (called through the Ror macro).
    __ li(t3, 0x0004);
    __ Ror(a5, a4, t3);
    __ li(t3, 0x0008);
    __ Ror(a6, a4, t3);
    __ li(t3, 0x000C);
    __ Ror(a7, a4, t3);
    __ li(t3, 0x0010);
    __ Ror(t0, a4, t3);
    __ li(t3, 0x0014);
    __ Ror(t1, a4, t3);
    __ li(t3, 0x0018);
    __ Ror(t2, a4, t3);
    __ li(t3, 0x001C);
    __ Ror(t3, a4, t3);

    // Basic word store.
755 756 757 758 759 760 761
    __ sw(a5, MemOperand(a0, offsetof(T, result_rotrv_4)) );
    __ sw(a6, MemOperand(a0, offsetof(T, result_rotrv_8)) );
    __ sw(a7, MemOperand(a0, offsetof(T, result_rotrv_12)) );
    __ sw(t0, MemOperand(a0, offsetof(T, result_rotrv_16)) );
    __ sw(t1, MemOperand(a0, offsetof(T, result_rotrv_20)) );
    __ sw(t2, MemOperand(a0, offsetof(T, result_rotrv_24)) );
    __ sw(t3, MemOperand(a0, offsetof(T, result_rotrv_28)) );
762

763 764
    __ jr(ra);
    __ nop();
765

766 767 768 769 770 771
    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
    t.input = 0x12345678;
772
    Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0x0, 0, 0, 0);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
    USE(dummy);
    CHECK_EQ(static_cast<int32_t>(0x81234567), t.result_rotr_4);
    CHECK_EQ(static_cast<int32_t>(0x78123456), t.result_rotr_8);
    CHECK_EQ(static_cast<int32_t>(0x67812345), t.result_rotr_12);
    CHECK_EQ(static_cast<int32_t>(0x56781234), t.result_rotr_16);
    CHECK_EQ(static_cast<int32_t>(0x45678123), t.result_rotr_20);
    CHECK_EQ(static_cast<int32_t>(0x34567812), t.result_rotr_24);
    CHECK_EQ(static_cast<int32_t>(0x23456781), t.result_rotr_28);

    CHECK_EQ(static_cast<int32_t>(0x81234567), t.result_rotrv_4);
    CHECK_EQ(static_cast<int32_t>(0x78123456), t.result_rotrv_8);
    CHECK_EQ(static_cast<int32_t>(0x67812345), t.result_rotrv_12);
    CHECK_EQ(static_cast<int32_t>(0x56781234), t.result_rotrv_16);
    CHECK_EQ(static_cast<int32_t>(0x45678123), t.result_rotrv_20);
    CHECK_EQ(static_cast<int32_t>(0x34567812), t.result_rotrv_24);
    CHECK_EQ(static_cast<int32_t>(0x23456781), t.result_rotrv_28);
  }
790 791 792 793 794 795 796 797 798
}


TEST(MIPS9) {
  // Test BRANCH improvements.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

799
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
  Label exit, exit2, exit3;

  __ Branch(&exit, ge, a0, Operand(zero_reg));
  __ Branch(&exit2, ge, a0, Operand(0x00001FFF));
  __ Branch(&exit3, ge, a0, Operand(0x0001FFFF));

  __ bind(&exit);
  __ bind(&exit2);
  __ bind(&exit3);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
}


TEST(MIPS10) {
  // Test conversions between doubles and long integers.
  // Test hos the long ints map to FP regs pairs.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  typedef struct {
    double a;
    double a_converted;
    double b;
    int32_t dbl_mant;
    int32_t dbl_exp;
    int32_t long_hi;
    int32_t long_lo;
    int64_t long_as_int64;
    int32_t b_long_hi;
    int32_t b_long_lo;
    int64_t b_long_as_int64;
  } T;
  T t;

  Assembler assm(isolate, NULL, 0);
  Label L, C;

  if (kArchVariant == kMips64r2) {
    // Rewritten for FR=1 FPU mode:
    //  -  32 FP regs of 64-bits each, no odd/even pairs.
    //  -  Note that cvt_l_d/cvt_d_l ARE legal in FR=1 mode.
    // Load all structure elements to registers.
849
    __ ldc1(f0, MemOperand(a0, offsetof(T, a)));
850 851 852 853

    // Save the raw bits of the double.
    __ mfc1(a4, f0);
    __ mfhc1(a5, f0);
854 855
    __ sw(a4, MemOperand(a0, offsetof(T, dbl_mant)));
    __ sw(a5, MemOperand(a0, offsetof(T, dbl_exp)));
856 857 858 859 860

    // Convert double in f0 to long, save hi/lo parts.
    __ cvt_l_d(f0, f0);
    __ mfc1(a4, f0);  // f0 LS 32 bits of long.
    __ mfhc1(a5, f0);  // f0 MS 32 bits of long.
861 862
    __ sw(a4, MemOperand(a0, offsetof(T, long_lo)));
    __ sw(a5, MemOperand(a0, offsetof(T, long_hi)));
863 864 865 866 867 868

    // Combine the high/low ints, convert back to double.
    __ dsll32(a6, a5, 0);  // Move a5 to high bits of a6.
    __ or_(a6, a6, a4);
    __ dmtc1(a6, f1);
    __ cvt_d_l(f1, f1);
869
    __ sdc1(f1, MemOperand(a0, offsetof(T, a_converted)));
870 871 872


    // Convert the b long integers to double b.
873 874
    __ lw(a4, MemOperand(a0, offsetof(T, b_long_lo)));
    __ lw(a5, MemOperand(a0, offsetof(T, b_long_hi)));
875 876 877
    __ mtc1(a4, f8);  // f8 LS 32-bits.
    __ mthc1(a5, f8);  // f8 MS 32-bits.
    __ cvt_d_l(f10, f8);
878
    __ sdc1(f10, MemOperand(a0, offsetof(T, b)));
879 880

    // Convert double b back to long-int.
881
    __ ldc1(f31, MemOperand(a0, offsetof(T, b)));
882 883
    __ cvt_l_d(f31, f31);
    __ dmfc1(a7, f31);
884
    __ sd(a7, MemOperand(a0, offsetof(T, b_long_as_int64)));
885 886 887 888 889 890 891 892 893 894 895 896 897


    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
    t.a = 2.147483647e9;       // 0x7fffffff -> 0x41DFFFFFFFC00000 as double.
    t.b_long_hi = 0x000000ff;  // 0xFF00FF00FF -> 0x426FE01FE01FE000 as double.
    t.b_long_lo = 0x00ff00ff;
898
    Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
899 900
    USE(dummy);

901 902
    CHECK_EQ(static_cast<int32_t>(0x41DFFFFF), t.dbl_exp);
    CHECK_EQ(static_cast<int32_t>(0xFFC00000), t.dbl_mant);
903
    CHECK_EQ(0, t.long_hi);
904
    CHECK_EQ(static_cast<int32_t>(0x7fffffff), t.long_lo);
905 906 907 908
    CHECK_EQ(2.147483647e9, t.a_converted);

    // 0xFF00FF00FF -> 1.095233372415e12.
    CHECK_EQ(1.095233372415e12, t.b);
909
    CHECK_EQ(static_cast<int64_t>(0xFF00FF00FF), t.b_long_as_int64);
910 911 912 913 914
  }
}


TEST(MIPS11) {
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
  // Do not run test on MIPS64r6, as these instructions are removed.
  if (kArchVariant != kMips64r6) {
    // Test LWL, LWR, SWL and SWR instructions.
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);

    typedef struct {
      int32_t reg_init;
      int32_t mem_init;
      int32_t lwl_0;
      int32_t lwl_1;
      int32_t lwl_2;
      int32_t lwl_3;
      int32_t lwr_0;
      int32_t lwr_1;
      int32_t lwr_2;
      int32_t lwr_3;
      int32_t swl_0;
      int32_t swl_1;
      int32_t swl_2;
      int32_t swl_3;
      int32_t swr_0;
      int32_t swr_1;
      int32_t swr_2;
      int32_t swr_3;
    } T;
    T t;

    Assembler assm(isolate, NULL, 0);

    // Test all combinations of LWL and vAddr.
947 948 949
    __ lw(a4, MemOperand(a0, offsetof(T, reg_init)));
    __ lwl(a4, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a4, MemOperand(a0, offsetof(T, lwl_0)));
950

951 952 953
    __ lw(a5, MemOperand(a0, offsetof(T, reg_init)));
    __ lwl(a5, MemOperand(a0, offsetof(T, mem_init) + 1));
    __ sw(a5, MemOperand(a0, offsetof(T, lwl_1)));
954

955 956 957
    __ lw(a6, MemOperand(a0, offsetof(T, reg_init)));
    __ lwl(a6, MemOperand(a0, offsetof(T, mem_init) + 2));
    __ sw(a6, MemOperand(a0, offsetof(T, lwl_2)));
958

959 960 961
    __ lw(a7, MemOperand(a0, offsetof(T, reg_init)));
    __ lwl(a7, MemOperand(a0, offsetof(T, mem_init) + 3));
    __ sw(a7, MemOperand(a0, offsetof(T, lwl_3)));
962 963

    // Test all combinations of LWR and vAddr.
964 965 966
    __ lw(a4, MemOperand(a0, offsetof(T, reg_init)));
    __ lwr(a4, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a4, MemOperand(a0, offsetof(T, lwr_0)));
967

968 969 970
    __ lw(a5, MemOperand(a0, offsetof(T, reg_init)));
    __ lwr(a5, MemOperand(a0, offsetof(T, mem_init) + 1));
    __ sw(a5, MemOperand(a0, offsetof(T, lwr_1)));
971

972 973 974
    __ lw(a6, MemOperand(a0, offsetof(T, reg_init)));
    __ lwr(a6, MemOperand(a0, offsetof(T, mem_init) + 2));
    __ sw(a6, MemOperand(a0, offsetof(T, lwr_2)) );
975

976 977 978
    __ lw(a7, MemOperand(a0, offsetof(T, reg_init)));
    __ lwr(a7, MemOperand(a0, offsetof(T, mem_init) + 3));
    __ sw(a7, MemOperand(a0, offsetof(T, lwr_3)) );
979 980

    // Test all combinations of SWL and vAddr.
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    __ lw(a4, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a4, MemOperand(a0, offsetof(T, swl_0)));
    __ lw(a4, MemOperand(a0, offsetof(T, reg_init)));
    __ swl(a4, MemOperand(a0, offsetof(T, swl_0)));

    __ lw(a5, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a5, MemOperand(a0, offsetof(T, swl_1)));
    __ lw(a5, MemOperand(a0, offsetof(T, reg_init)));
    __ swl(a5, MemOperand(a0, offsetof(T, swl_1) + 1));

    __ lw(a6, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a6, MemOperand(a0, offsetof(T, swl_2)));
    __ lw(a6, MemOperand(a0, offsetof(T, reg_init)));
    __ swl(a6, MemOperand(a0, offsetof(T, swl_2) + 2));

    __ lw(a7, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a7, MemOperand(a0, offsetof(T, swl_3)));
    __ lw(a7, MemOperand(a0, offsetof(T, reg_init)));
    __ swl(a7, MemOperand(a0, offsetof(T, swl_3) + 3));
1000 1001

    // Test all combinations of SWR and vAddr.
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    __ lw(a4, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a4, MemOperand(a0, offsetof(T, swr_0)));
    __ lw(a4, MemOperand(a0, offsetof(T, reg_init)));
    __ swr(a4, MemOperand(a0, offsetof(T, swr_0)));

    __ lw(a5, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a5, MemOperand(a0, offsetof(T, swr_1)));
    __ lw(a5, MemOperand(a0, offsetof(T, reg_init)));
    __ swr(a5, MemOperand(a0, offsetof(T, swr_1) + 1));

    __ lw(a6, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a6, MemOperand(a0, offsetof(T, swr_2)));
    __ lw(a6, MemOperand(a0, offsetof(T, reg_init)));
    __ swr(a6, MemOperand(a0, offsetof(T, swr_2) + 2));

    __ lw(a7, MemOperand(a0, offsetof(T, mem_init)));
    __ sw(a7, MemOperand(a0, offsetof(T, swr_3)));
    __ lw(a7, MemOperand(a0, offsetof(T, reg_init)));
    __ swr(a7, MemOperand(a0, offsetof(T, swr_3) + 3));
1021

1022 1023
    __ jr(ra);
    __ nop();
1024

1025 1026 1027 1028 1029 1030 1031
    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
    t.reg_init = 0xaabbccdd;
    t.mem_init = 0x11223344;
1032

1033
    Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
1034
    USE(dummy);
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    if (kArchEndian == kLittle) {
      CHECK_EQ(static_cast<int32_t>(0x44bbccdd), t.lwl_0);
      CHECK_EQ(static_cast<int32_t>(0x3344ccdd), t.lwl_1);
      CHECK_EQ(static_cast<int32_t>(0x223344dd), t.lwl_2);
      CHECK_EQ(static_cast<int32_t>(0x11223344), t.lwl_3);

      CHECK_EQ(static_cast<int32_t>(0x11223344), t.lwr_0);
      CHECK_EQ(static_cast<int32_t>(0xaa112233), t.lwr_1);
      CHECK_EQ(static_cast<int32_t>(0xaabb1122), t.lwr_2);
      CHECK_EQ(static_cast<int32_t>(0xaabbcc11), t.lwr_3);

      CHECK_EQ(static_cast<int32_t>(0x112233aa), t.swl_0);
      CHECK_EQ(static_cast<int32_t>(0x1122aabb), t.swl_1);
      CHECK_EQ(static_cast<int32_t>(0x11aabbcc), t.swl_2);
      CHECK_EQ(static_cast<int32_t>(0xaabbccdd), t.swl_3);

      CHECK_EQ(static_cast<int32_t>(0xaabbccdd), t.swr_0);
      CHECK_EQ(static_cast<int32_t>(0xbbccdd44), t.swr_1);
      CHECK_EQ(static_cast<int32_t>(0xccdd3344), t.swr_2);
      CHECK_EQ(static_cast<int32_t>(0xdd223344), t.swr_3);
    } else {
      CHECK_EQ(static_cast<int32_t>(0x11223344), t.lwl_0);
      CHECK_EQ(static_cast<int32_t>(0x223344dd), t.lwl_1);
      CHECK_EQ(static_cast<int32_t>(0x3344ccdd), t.lwl_2);
      CHECK_EQ(static_cast<int32_t>(0x44bbccdd), t.lwl_3);

      CHECK_EQ(static_cast<int32_t>(0xaabbcc11), t.lwr_0);
      CHECK_EQ(static_cast<int32_t>(0xaabb1122), t.lwr_1);
      CHECK_EQ(static_cast<int32_t>(0xaa112233), t.lwr_2);
      CHECK_EQ(static_cast<int32_t>(0x11223344), t.lwr_3);

      CHECK_EQ(static_cast<int32_t>(0xaabbccdd), t.swl_0);
      CHECK_EQ(static_cast<int32_t>(0x11aabbcc), t.swl_1);
      CHECK_EQ(static_cast<int32_t>(0x1122aabb), t.swl_2);
      CHECK_EQ(static_cast<int32_t>(0x112233aa), t.swl_3);

      CHECK_EQ(static_cast<int32_t>(0xdd223344), t.swr_0);
      CHECK_EQ(static_cast<int32_t>(0xccdd3344), t.swr_1);
      CHECK_EQ(static_cast<int32_t>(0xbbccdd44), t.swr_2);
      CHECK_EQ(static_cast<int32_t>(0xaabbccdd), t.swr_3);
    }
1077
  }
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
}


TEST(MIPS12) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  typedef struct {
      int32_t  x;
      int32_t  y;
      int32_t  y1;
      int32_t  y2;
      int32_t  y3;
      int32_t  y4;
  } T;
  T t;

1096
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
1097 1098 1099

  __ mov(t2, fp);  // Save frame pointer.
  __ mov(fp, a0);  // Access struct T by fp.
1100 1101
  __ lw(a4, MemOperand(a0, offsetof(T, y)));
  __ lw(a7, MemOperand(a0, offsetof(T, y4)));
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118

  __ addu(a5, a4, a7);
  __ subu(t0, a4, a7);
  __ nop();
  __ push(a4);  // These instructions disappear after opt.
  __ Pop();
  __ addu(a4, a4, a4);
  __ nop();
  __ Pop();     // These instructions disappear after opt.
  __ push(a7);
  __ nop();
  __ push(a7);  // These instructions disappear after opt.
  __ pop(a7);
  __ nop();
  __ push(a7);
  __ pop(t0);
  __ nop();
1119 1120
  __ sw(a4, MemOperand(fp, offsetof(T, y)));
  __ lw(a4, MemOperand(fp, offsetof(T, y)));
1121
  __ nop();
1122 1123
  __ sw(a4, MemOperand(fp, offsetof(T, y)));
  __ lw(a5, MemOperand(fp, offsetof(T, y)));
1124 1125
  __ nop();
  __ push(a5);
1126
  __ lw(a5, MemOperand(fp, offsetof(T, y)));
1127 1128 1129
  __ pop(a5);
  __ nop();
  __ push(a5);
1130
  __ lw(a6, MemOperand(fp, offsetof(T, y)));
1131 1132 1133
  __ pop(a5);
  __ nop();
  __ push(a5);
1134
  __ lw(a6, MemOperand(fp, offsetof(T, y)));
1135 1136 1137
  __ pop(a6);
  __ nop();
  __ push(a6);
1138
  __ lw(a6, MemOperand(fp, offsetof(T, y)));
1139 1140 1141
  __ pop(a5);
  __ nop();
  __ push(a5);
1142
  __ lw(a6, MemOperand(fp, offsetof(T, y)));
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  __ pop(a7);
  __ nop();

  __ mov(fp, t2);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  t.x = 1;
  t.y = 2;
  t.y1 = 3;
  t.y2 = 4;
  t.y3 = 0XBABA;
  t.y4 = 0xDEDA;

1162
  Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
  USE(dummy);

  CHECK_EQ(3, t.y1);
}


TEST(MIPS13) {
  // Test Cvt_d_uw and Trunc_uw_d macros.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  typedef struct {
    double cvt_big_out;
    double cvt_small_out;
    uint32_t trunc_big_out;
    uint32_t trunc_small_out;
    uint32_t cvt_big_in;
    uint32_t cvt_small_in;
  } T;
  T t;

1185
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
1186

1187
  __ sw(a4, MemOperand(a0, offsetof(T, cvt_small_in)));
1188
  __ Cvt_d_uw(f10, a4);
1189
  __ sdc1(f10, MemOperand(a0, offsetof(T, cvt_small_out)));
1190

1191
  __ Trunc_uw_d(f10, f10, f4);
1192
  __ swc1(f10, MemOperand(a0, offsetof(T, trunc_small_out)));
1193

1194
  __ sw(a4, MemOperand(a0, offsetof(T, cvt_big_in)));
1195
  __ Cvt_d_uw(f8, a4);
1196
  __ sdc1(f8, MemOperand(a0, offsetof(T, cvt_big_out)));
1197

1198
  __ Trunc_uw_d(f8, f8, f4);
1199
  __ swc1(f8, MemOperand(a0, offsetof(T, trunc_big_out)));
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());

  t.cvt_big_in = 0xFFFFFFFF;
  t.cvt_small_in  = 333;

1213
  Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
  USE(dummy);

  CHECK_EQ(t.cvt_big_out, static_cast<double>(t.cvt_big_in));
  CHECK_EQ(t.cvt_small_out, static_cast<double>(t.cvt_small_in));

  CHECK_EQ(static_cast<int>(t.trunc_big_out), static_cast<int>(t.cvt_big_in));
  CHECK_EQ(static_cast<int>(t.trunc_small_out),
           static_cast<int>(t.cvt_small_in));
}


TEST(MIPS14) {
  // Test round, floor, ceil, trunc, cvt.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

#define ROUND_STRUCT_ELEMENT(x) \
1232
  uint32_t x##_isNaN2008; \
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
  int32_t x##_up_out; \
  int32_t x##_down_out; \
  int32_t neg_##x##_up_out; \
  int32_t neg_##x##_down_out; \
  uint32_t x##_err1_out; \
  uint32_t x##_err2_out; \
  uint32_t x##_err3_out; \
  uint32_t x##_err4_out; \
  int32_t x##_invalid_result;

  typedef struct {
    double round_up_in;
    double round_down_in;
    double neg_round_up_in;
    double neg_round_down_in;
    double err1_in;
    double err2_in;
    double err3_in;
    double err4_in;

    ROUND_STRUCT_ELEMENT(round)
    ROUND_STRUCT_ELEMENT(floor)
    ROUND_STRUCT_ELEMENT(ceil)
    ROUND_STRUCT_ELEMENT(trunc)
    ROUND_STRUCT_ELEMENT(cvt)
  } T;
  T t;

#undef ROUND_STRUCT_ELEMENT

1263
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
1264 1265 1266 1267 1268 1269

  // Save FCSR.
  __ cfc1(a1, FCSR);
  // Disable FPU exceptions.
  __ ctc1(zero_reg, FCSR);
#define RUN_ROUND_TEST(x) \
1270 1271
  __ cfc1(t0, FCSR);\
  __ sw(t0, MemOperand(a0, offsetof(T, x##_isNaN2008))); \
1272
  __ ldc1(f0, MemOperand(a0, offsetof(T, round_up_in))); \
1273
  __ x##_w_d(f0, f0); \
1274
  __ swc1(f0, MemOperand(a0, offsetof(T, x##_up_out))); \
1275
  \
1276
  __ ldc1(f0, MemOperand(a0, offsetof(T, round_down_in))); \
1277
  __ x##_w_d(f0, f0); \
1278
  __ swc1(f0, MemOperand(a0, offsetof(T, x##_down_out))); \
1279
  \
1280
  __ ldc1(f0, MemOperand(a0, offsetof(T, neg_round_up_in))); \
1281
  __ x##_w_d(f0, f0); \
1282
  __ swc1(f0, MemOperand(a0, offsetof(T, neg_##x##_up_out))); \
1283
  \
1284
  __ ldc1(f0, MemOperand(a0, offsetof(T, neg_round_down_in))); \
1285
  __ x##_w_d(f0, f0); \
1286
  __ swc1(f0, MemOperand(a0, offsetof(T, neg_##x##_down_out))); \
1287
  \
1288
  __ ldc1(f0, MemOperand(a0, offsetof(T, err1_in))); \
1289 1290 1291
  __ ctc1(zero_reg, FCSR); \
  __ x##_w_d(f0, f0); \
  __ cfc1(a2, FCSR); \
1292
  __ sw(a2, MemOperand(a0, offsetof(T, x##_err1_out))); \
1293
  \
1294
  __ ldc1(f0, MemOperand(a0, offsetof(T, err2_in))); \
1295 1296 1297
  __ ctc1(zero_reg, FCSR); \
  __ x##_w_d(f0, f0); \
  __ cfc1(a2, FCSR); \
1298
  __ sw(a2, MemOperand(a0, offsetof(T, x##_err2_out))); \
1299
  \
1300
  __ ldc1(f0, MemOperand(a0, offsetof(T, err3_in))); \
1301 1302 1303
  __ ctc1(zero_reg, FCSR); \
  __ x##_w_d(f0, f0); \
  __ cfc1(a2, FCSR); \
1304
  __ sw(a2, MemOperand(a0, offsetof(T, x##_err3_out))); \
1305
  \
1306
  __ ldc1(f0, MemOperand(a0, offsetof(T, err4_in))); \
1307 1308 1309
  __ ctc1(zero_reg, FCSR); \
  __ x##_w_d(f0, f0); \
  __ cfc1(a2, FCSR); \
1310 1311
  __ sw(a2, MemOperand(a0, offsetof(T, x##_err4_out))); \
  __ swc1(f0, MemOperand(a0, offsetof(T, x##_invalid_result)));
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339

  RUN_ROUND_TEST(round)
  RUN_ROUND_TEST(floor)
  RUN_ROUND_TEST(ceil)
  RUN_ROUND_TEST(trunc)
  RUN_ROUND_TEST(cvt)

  // Restore FCSR.
  __ ctc1(a1, FCSR);

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());

  t.round_up_in = 123.51;
  t.round_down_in = 123.49;
  t.neg_round_up_in = -123.5;
  t.neg_round_down_in = -123.49;
  t.err1_in = 123.51;
  t.err2_in = 1;
  t.err3_in = static_cast<double>(1) + 0xFFFFFFFF;
  t.err4_in = NAN;

1340
  Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
1341 1342 1343
  USE(dummy);

#define GET_FPU_ERR(x) (static_cast<int>(x & kFCSRFlagMask))
1344
#define CHECK_NAN2008(x) (x & kFCSRNaN2008FlagMask)
1345 1346 1347 1348 1349
#define CHECK_ROUND_RESULT(type) \
  CHECK(GET_FPU_ERR(t.type##_err1_out) & kFCSRInexactFlagMask); \
  CHECK_EQ(0, GET_FPU_ERR(t.type##_err2_out)); \
  CHECK(GET_FPU_ERR(t.type##_err3_out) & kFCSRInvalidOpFlagMask); \
  CHECK(GET_FPU_ERR(t.type##_err4_out) & kFCSRInvalidOpFlagMask); \
1350 1351 1352 1353 1354
  if (CHECK_NAN2008(t.type##_isNaN2008) && kArchVariant == kMips64r6) { \
    CHECK_EQ(static_cast<int32_t>(0), t.type##_invalid_result);\
  } else { \
    CHECK_EQ(static_cast<int32_t>(kFPUInvalidResult), t.type##_invalid_result);\
  }
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387

  CHECK_ROUND_RESULT(round);
  CHECK_ROUND_RESULT(floor);
  CHECK_ROUND_RESULT(ceil);
  CHECK_ROUND_RESULT(cvt);
}


TEST(MIPS15) {
  // Test chaining of label usages within instructions (issue 1644).
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  Assembler assm(isolate, NULL, 0);

  Label target;
  __ beq(v0, v1, &target);
  __ nop();
  __ bne(v0, v1, &target);
  __ nop();
  __ bind(&target);
  __ nop();
}


// ----- mips64 tests -----------------------------------------------

TEST(MIPS16) {
  // Test 64-bit memory loads and stores.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

1388
  struct T {
1389 1390 1391 1392 1393 1394
    int64_t r1;
    int64_t r2;
    int64_t r3;
    int64_t r4;
    int64_t r5;
    int64_t r6;
1395 1396 1397 1398 1399 1400
    int64_t r7;
    int64_t r8;
    int64_t r9;
    int64_t r10;
    int64_t r11;
    int64_t r12;
1401 1402
    uint32_t ui;
    int32_t si;
1403
  };
1404 1405 1406 1407 1408 1409
  T t;

  Assembler assm(isolate, NULL, 0);
  Label L, C;

  // Basic 32-bit word load/store, with un-signed data.
1410 1411
  __ lw(a4, MemOperand(a0, offsetof(T, ui)));
  __ sw(a4, MemOperand(a0, offsetof(T, r1)));
1412 1413

  // Check that the data got zero-extended into 64-bit a4.
1414
  __ sd(a4, MemOperand(a0, offsetof(T, r2)));
1415 1416

  // Basic 32-bit word load/store, with SIGNED data.
1417 1418
  __ lw(a5, MemOperand(a0, offsetof(T, si)));
  __ sw(a5, MemOperand(a0, offsetof(T, r3)));
1419 1420

  // Check that the data got sign-extended into 64-bit a4.
1421
  __ sd(a5, MemOperand(a0, offsetof(T, r4)));
1422 1423

  // 32-bit UNSIGNED word load/store, with SIGNED data.
1424 1425
  __ lwu(a6, MemOperand(a0, offsetof(T, si)));
  __ sw(a6, MemOperand(a0, offsetof(T, r5)));
1426 1427

  // Check that the data got zero-extended into 64-bit a4.
1428
  __ sd(a6, MemOperand(a0, offsetof(T, r6)));
1429 1430

  // lh with positive data.
1431
  __ lh(a5, MemOperand(a0, offsetof(T, ui)));
1432
  __ sw(a5, MemOperand(a0, offsetof(T, r7)));
1433 1434

  // lh with negative data.
1435
  __ lh(a6, MemOperand(a0, offsetof(T, si)));
1436
  __ sw(a6, MemOperand(a0, offsetof(T, r8)));
1437 1438

  // lhu with negative data.
1439
  __ lhu(a7, MemOperand(a0, offsetof(T, si)));
1440
  __ sw(a7, MemOperand(a0, offsetof(T, r9)));
1441 1442

  // lb with negative data.
1443
  __ lb(t0, MemOperand(a0, offsetof(T, si)));
1444
  __ sw(t0, MemOperand(a0, offsetof(T, r10)));
1445

1446 1447 1448 1449 1450
  // sh writes only 1/2 of word.
  __ lw(a4, MemOperand(a0, offsetof(T, ui)));
  __ sh(a4, MemOperand(a0, offsetof(T, r11)));
  __ lw(a4, MemOperand(a0, offsetof(T, si)));
  __ sh(a4, MemOperand(a0, offsetof(T, r12)));
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  t.ui = 0x44332211;
  t.si = 0x99aabbcc;
1462 1463 1464 1465
  t.r1 = 0x5555555555555555;
  t.r2 = 0x5555555555555555;
  t.r3 = 0x5555555555555555;
  t.r4 = 0x5555555555555555;
1466
  t.r5 = 0x5555555555555555;
1467 1468 1469 1470 1471 1472 1473 1474
  t.r6 = 0x5555555555555555;
  t.r7 = 0x5555555555555555;
  t.r8 = 0x5555555555555555;
  t.r9 = 0x5555555555555555;
  t.r10 = 0x5555555555555555;
  t.r11 = 0x5555555555555555;
  t.r12 = 0x5555555555555555;

1475
  Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
1476 1477
  USE(dummy);

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
  if (kArchEndian == kLittle) {
    // Unsigned data, 32 & 64
    CHECK_EQ(static_cast<int64_t>(0x5555555544332211L), t.r1);  // lw, sw.
    CHECK_EQ(static_cast<int64_t>(0x0000000044332211L), t.r2);  // sd.

    // Signed data, 32 & 64.
    CHECK_EQ(static_cast<int64_t>(0x5555555599aabbccL), t.r3);  // lw, sw.
    CHECK_EQ(static_cast<int64_t>(0xffffffff99aabbccL), t.r4);  // sd.

    // Signed data, 32 & 64.
    CHECK_EQ(static_cast<int64_t>(0x5555555599aabbccL), t.r5);  // lwu, sw.
    CHECK_EQ(static_cast<int64_t>(0x0000000099aabbccL), t.r6);  // sd.

    // lh with unsigned and signed data.
    CHECK_EQ(static_cast<int64_t>(0x5555555500002211L), t.r7);  // lh, sw.
    CHECK_EQ(static_cast<int64_t>(0x55555555ffffbbccL), t.r8);  // lh, sw.
1494

1495 1496
    // lhu with signed data.
    CHECK_EQ(static_cast<int64_t>(0x555555550000bbccL), t.r9);  // lhu, sw.
1497

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
    // lb with signed data.
    CHECK_EQ(static_cast<int64_t>(0x55555555ffffffccL), t.r10);  // lb, sw.

    // sh with unsigned and signed data.
    CHECK_EQ(static_cast<int64_t>(0x5555555555552211L), t.r11);  // lw, sh.
    CHECK_EQ(static_cast<int64_t>(0x555555555555bbccL), t.r12);  // lw, sh.
  } else {
    // Unsigned data, 32 & 64
    CHECK_EQ(static_cast<int64_t>(0x4433221155555555L), t.r1);  // lw, sw.
    CHECK_EQ(static_cast<int64_t>(0x0000000044332211L), t.r2);  // sd.

    // Signed data, 32 & 64.
    CHECK_EQ(static_cast<int64_t>(0x99aabbcc55555555L), t.r3);  // lw, sw.
    CHECK_EQ(static_cast<int64_t>(0xffffffff99aabbccL), t.r4);  // sd.

    // Signed data, 32 & 64.
    CHECK_EQ(static_cast<int64_t>(0x99aabbcc55555555L), t.r5);  // lwu, sw.
    CHECK_EQ(static_cast<int64_t>(0x0000000099aabbccL), t.r6);  // sd.

    // lh with unsigned and signed data.
    CHECK_EQ(static_cast<int64_t>(0x0000443355555555L), t.r7);  // lh, sw.
    CHECK_EQ(static_cast<int64_t>(0xffff99aa55555555L), t.r8);  // lh, sw.

    // lhu with signed data.
    CHECK_EQ(static_cast<int64_t>(0x000099aa55555555L), t.r9);  // lhu, sw.

    // lb with signed data.
    CHECK_EQ(static_cast<int64_t>(0xffffff9955555555L), t.r10);  // lb, sw.

    // sh with unsigned and signed data.
    CHECK_EQ(static_cast<int64_t>(0x2211555555555555L), t.r11);  // lw, sh.
    CHECK_EQ(static_cast<int64_t>(0xbbcc555555555555L), t.r12);  // lw, sh.
  }
1531 1532
}

1533

1534
// ----------------------mips64r6 specific tests----------------------
1535
TEST(seleqz_selnez) {
1536 1537 1538 1539
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
1540 1541
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
1542 1543 1544 1545 1546 1547

    typedef struct test {
      int a;
      int b;
      int c;
      int d;
1548 1549 1550 1551
      double e;
      double f;
      double g;
      double h;
1552 1553 1554 1555
      float i;
      float j;
      float k;
      float l;
1556 1557 1558 1559 1560
    } Test;

    Test test;
    // Integer part of test.
    __ addiu(t1, zero_reg, 1);                      // t1 = 1
1561
    __ seleqz(t3, t1, zero_reg);                    // t3 = 1
1562
    __ sw(t3, MemOperand(a0, offsetof(Test, a)));  // a = 1
1563
    __ seleqz(t2, t1, t1);                          // t2 = 0
1564
    __ sw(t2, MemOperand(a0, offsetof(Test, b)));  // b = 0
1565
    __ selnez(t3, t1, zero_reg);                    // t3 = 1;
1566
    __ sw(t3, MemOperand(a0, offsetof(Test, c)));  // c = 0
1567
    __ selnez(t3, t1, t1);                          // t3 = 1
1568
    __ sw(t3, MemOperand(a0, offsetof(Test, d)));  // d = 1
1569
    // Floating point part of test.
1570 1571 1572 1573
    __ ldc1(f0, MemOperand(a0, offsetof(Test, e)) );  // src
    __ ldc1(f2, MemOperand(a0, offsetof(Test, f)) );  // test
    __ lwc1(f8, MemOperand(a0, offsetof(Test, i)) );  // src
    __ lwc1(f10, MemOperand(a0, offsetof(Test, j)) );  // test
1574 1575 1576 1577
    __ seleqz_d(f4, f0, f2);
    __ selnez_d(f6, f0, f2);
    __ seleqz_s(f12, f8, f10);
    __ selnez_s(f14, f8, f10);
1578 1579 1580 1581
    __ sdc1(f4, MemOperand(a0, offsetof(Test, g)) );  // src
    __ sdc1(f6, MemOperand(a0, offsetof(Test, h)) );  // src
    __ swc1(f12, MemOperand(a0, offsetof(Test, k)) );  // src
    __ swc1(f14, MemOperand(a0, offsetof(Test, l)) );  // src
1582 1583 1584 1585 1586 1587 1588 1589
    __ jr(ra);
    __ nop();
    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());

1590
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
1591 1592 1593 1594 1595 1596

    CHECK_EQ(test.a, 1);
    CHECK_EQ(test.b, 0);
    CHECK_EQ(test.c, 0);
    CHECK_EQ(test.d, 1);

1597 1598 1599
    const int test_size = 3;
    const int input_size = 5;

1600
    double inputs_D[input_size] = {0.0, 65.2, -70.32,
1601
      18446744073709551621.0, -18446744073709551621.0};
1602
    double outputs_D[input_size] = {0.0, 65.2, -70.32,
1603
      18446744073709551621.0, -18446744073709551621.0};
1604
    double tests_D[test_size*2] = {2.8, 2.9, -2.8, -2.9,
1605
      18446744073709551616.0, 18446744073709555712.0};
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
    float inputs_S[input_size] = {0.0, 65.2, -70.32,
      18446744073709551621.0, -18446744073709551621.0};
    float outputs_S[input_size] = {0.0, 65.2, -70.32,
      18446744073709551621.0, -18446744073709551621.0};
    float tests_S[test_size*2] = {2.9, 2.8, -2.9, -2.8,
      18446744073709551616.0, 18446746272732807168.0};
    for (int j=0; j < test_size; j+=2) {
      for (int i=0; i < input_size; i++) {
        test.e = inputs_D[i];
        test.f = tests_D[j];
        test.i = inputs_S[i];
        test.j = tests_S[j];
1618
        (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
1619
        CHECK_EQ(test.g, outputs_D[i]);
1620
        CHECK_EQ(test.h, 0);
1621 1622
        CHECK_EQ(test.k, outputs_S[i]);
        CHECK_EQ(test.l, 0);
1623

1624 1625
        test.f = tests_D[j+1];
        test.j = tests_S[j+1];
1626
        (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
1627
        CHECK_EQ(test.g, 0);
1628 1629 1630
        CHECK_EQ(test.h, outputs_D[i]);
        CHECK_EQ(test.k, 0);
        CHECK_EQ(test.l, outputs_S[i]);
1631 1632 1633 1634 1635 1636
      }
    }
  }
}


1637 1638

TEST(min_max) {
1639 1640 1641 1642
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
1643
    MacroAssembler assm(isolate, nullptr, 0,
1644
                        v8::internal::CodeObjectRequired::kYes);
1645

1646
    struct TestFloat {
1647 1648 1649 1650
      double a;
      double b;
      double c;
      double d;
1651 1652 1653 1654
      float e;
      float f;
      float g;
      float h;
1655
    };
1656 1657

    TestFloat test;
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
    const double dnan = std::numeric_limits<double>::quiet_NaN();
    const double dinf = std::numeric_limits<double>::infinity();
    const double dminf = -std::numeric_limits<double>::infinity();
    const float fnan = std::numeric_limits<float>::quiet_NaN();
    const float finf = std::numeric_limits<float>::infinity();
    const float fminf = std::numeric_limits<float>::infinity();
    const int kTableLength = 13;
    double inputsa[kTableLength] = {2.0,  3.0,  dnan, 3.0,   -0.0, 0.0, dinf,
                                    dnan, 42.0, dinf, dminf, dinf, dnan};
    double inputsb[kTableLength] = {3.0,  2.0,  3.0,  dnan, 0.0,   -0.0, dnan,
                                    dinf, dinf, 42.0, dinf, dminf, dnan};
    double outputsdmin[kTableLength] = {2.0,   2.0,   3.0,  3.0,  -0.0,
                                        -0.0,  dinf,  dinf, 42.0, 42.0,
                                        dminf, dminf, dnan};
    double outputsdmax[kTableLength] = {3.0,  3.0,  3.0,  3.0,  0.0,  0.0, dinf,
                                        dinf, dinf, dinf, dinf, dinf, dnan};

    float inputse[kTableLength] = {2.0,  3.0,  fnan, 3.0,   -0.0, 0.0, finf,
                                   fnan, 42.0, finf, fminf, finf, fnan};
1677
    float inputsf[kTableLength] = {3.0,  2.0,  3.0,  fnan, 0.0,   -0.0, fnan,
1678 1679 1680 1681 1682 1683
                                   finf, finf, 42.0, finf, fminf, fnan};
    float outputsfmin[kTableLength] = {2.0,   2.0,   3.0,  3.0,  -0.0,
                                       -0.0,  finf,  finf, 42.0, 42.0,
                                       fminf, fminf, fnan};
    float outputsfmax[kTableLength] = {3.0,  3.0,  3.0,  3.0,  0.0,  0.0, finf,
                                       finf, finf, finf, finf, finf, fnan};
1684

1685 1686 1687 1688
    __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, a)));
    __ ldc1(f8, MemOperand(a0, offsetof(TestFloat, b)));
    __ lwc1(f2, MemOperand(a0, offsetof(TestFloat, e)));
    __ lwc1(f6, MemOperand(a0, offsetof(TestFloat, f)));
1689 1690
    __ min_d(f10, f4, f8);
    __ max_d(f12, f4, f8);
1691 1692
    __ min_s(f14, f2, f6);
    __ max_s(f16, f2, f6);
1693 1694 1695 1696
    __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, c)));
    __ sdc1(f12, MemOperand(a0, offsetof(TestFloat, d)));
    __ swc1(f14, MemOperand(a0, offsetof(TestFloat, g)));
    __ swc1(f16, MemOperand(a0, offsetof(TestFloat, h)));
1697 1698 1699 1700 1701 1702 1703 1704
    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
1705
    for (int i = 4; i < kTableLength; i++) {
1706 1707 1708 1709
      test.a = inputsa[i];
      test.b = inputsb[i];
      test.e = inputse[i];
      test.f = inputsf[i];
1710

1711 1712 1713 1714 1715 1716
      CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0);

      CHECK_EQ(0, memcmp(&test.c, &outputsdmin[i], sizeof(test.c)));
      CHECK_EQ(0, memcmp(&test.d, &outputsdmax[i], sizeof(test.d)));
      CHECK_EQ(0, memcmp(&test.g, &outputsfmin[i], sizeof(test.g)));
      CHECK_EQ(0, memcmp(&test.h, &outputsfmax[i], sizeof(test.h)));
1717
    }
1718 1719 1720 1721
  }
}


1722
TEST(rint_d)  {
1723
  if (kArchVariant == kMips64r6) {
1724
    const int kTableLength = 30;
1725 1726 1727
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
1728 1729
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
1730 1731 1732 1733 1734 1735 1736 1737

    typedef struct test_float {
      double a;
      double b;
      int fcsr;
    }TestFloat;

    TestFloat test;
1738
    double inputs[kTableLength] = {18446744073709551617.0,
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
      4503599627370496.0, -4503599627370496.0,
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E147,
      1.7976931348623157E+308, 6.27463370218383111104242366943E-307,
      309485009821345068724781056.89,
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
1750
    double outputs_RN[kTableLength] = {18446744073709551617.0,
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
      4503599627370496.0, -4503599627370496.0,
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E147,
      1.7976931348623157E308, 0,
      309485009821345068724781057.0,
      2.0, 3.0, 2.0, 3.0, 4.0, 4.0,
      -2.0, -3.0, -2.0, -3.0, -4.0, -4.0,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
1762
    double outputs_RZ[kTableLength] = {18446744073709551617.0,
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
      4503599627370496.0, -4503599627370496.0,
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E147,
      1.7976931348623157E308, 0,
      309485009821345068724781057.0,
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
1774
    double outputs_RP[kTableLength] = {18446744073709551617.0,
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
      4503599627370496.0, -4503599627370496.0,
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E147,
      1.7976931348623157E308, 1,
      309485009821345068724781057.0,
      3.0, 3.0, 3.0, 4.0, 4.0, 4.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
1786
    double outputs_RM[kTableLength] = {18446744073709551617.0,
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
      4503599627370496.0, -4503599627370496.0,
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E147,
      1.7976931348623157E308, 0,
      309485009821345068724781057.0,
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -3.0, -3.0, -3.0, -4.0, -4.0, -4.0,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
    int fcsr_inputs[4] =
      {kRoundToNearest, kRoundToZero, kRoundToPlusInf, kRoundToMinusInf};
    double* outputs[4] = {outputs_RN, outputs_RZ, outputs_RP, outputs_RM};
1801 1802
    __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, a)) );
    __ lw(t0, MemOperand(a0, offsetof(TestFloat, fcsr)) );
1803 1804
    __ ctc1(t0, FCSR);
    __ rint_d(f8, f4);
1805
    __ sdc1(f8, MemOperand(a0, offsetof(TestFloat, b)) );
1806 1807 1808 1809 1810 1811 1812 1813 1814
    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());

1815
    for (int j = 0; j < 4; j++) {
1816
      test.fcsr = fcsr_inputs[j];
1817
      for (int i = 0; i < kTableLength; i++) {
1818
        test.a = inputs[i];
1819
        (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
1820 1821 1822 1823 1824 1825 1826
        CHECK_EQ(test.b, outputs[j][i]);
      }
    }
  }
}


1827 1828 1829 1830 1831
TEST(sel) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
1832 1833
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
1834

1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
    typedef struct test {
      double dd;
      double ds;
      double dt;
      float fd;
      float fs;
      float ft;
    } Test;

    Test test;
1845 1846 1847 1848 1849 1850
    __ ldc1(f0, MemOperand(a0, offsetof(Test, dd)) );  // test
    __ ldc1(f2, MemOperand(a0, offsetof(Test, ds)) );  // src1
    __ ldc1(f4, MemOperand(a0, offsetof(Test, dt)) );  // src2
    __ lwc1(f6, MemOperand(a0, offsetof(Test, fd)) );  // test
    __ lwc1(f8, MemOperand(a0, offsetof(Test, fs)) );  // src1
    __ lwc1(f10, MemOperand(a0, offsetof(Test, ft)) );  // src2
1851 1852
    __ sel_d(f0, f2, f4);
    __ sel_s(f6, f8, f10);
1853 1854
    __ sdc1(f0, MemOperand(a0, offsetof(Test, dd)) );
    __ swc1(f6, MemOperand(a0, offsetof(Test, fd)) );
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
    __ jr(ra);
    __ nop();
    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());

    const int test_size = 3;
    const int input_size = 5;

    double inputs_dt[input_size] = {0.0, 65.2, -70.32,
      18446744073709551621.0, -18446744073709551621.0};
    double inputs_ds[input_size] = {0.1, 69.88, -91.325,
      18446744073709551625.0, -18446744073709551625.0};
    float inputs_ft[input_size] = {0.0, 65.2, -70.32,
      18446744073709551621.0, -18446744073709551621.0};
    float inputs_fs[input_size] = {0.1, 69.88, -91.325,
      18446744073709551625.0, -18446744073709551625.0};
    double tests_D[test_size*2] = {2.8, 2.9, -2.8, -2.9,
      18446744073709551616.0, 18446744073709555712.0};
    float tests_S[test_size*2] = {2.9, 2.8, -2.9, -2.8,
      18446744073709551616.0, 18446746272732807168.0};
    for (int j=0; j < test_size; j+=2) {
      for (int i=0; i < input_size; i++) {
        test.dt = inputs_dt[i];
        test.dd = tests_D[j];
        test.ds = inputs_ds[i];
        test.ft = inputs_ft[i];
        test.fd = tests_S[j];
        test.fs = inputs_fs[i];
1886
        (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
1887 1888 1889 1890 1891
        CHECK_EQ(test.dd, inputs_ds[i]);
        CHECK_EQ(test.fd, inputs_fs[i]);

        test.dd = tests_D[j+1];
        test.fd = tests_S[j+1];
1892
        (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
1893 1894 1895
        CHECK_EQ(test.dd, inputs_dt[i]);
        CHECK_EQ(test.fd, inputs_ft[i]);
      }
1896 1897 1898 1899 1900
    }
  }
}


1901
TEST(rint_s)  {
1902
  if (kArchVariant == kMips64r6) {
1903
    const int kTableLength = 30;
1904 1905 1906
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
1907 1908
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
1909 1910

    typedef struct test_float {
1911 1912
      float a;
      float b;
1913 1914 1915 1916
      int fcsr;
    }TestFloat;

    TestFloat test;
1917
    float inputs[kTableLength] = {18446744073709551617.0,
1918
      4503599627370496.0, -4503599627370496.0,
1919 1920
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E37,
      1.7976931348623157E+38, 6.27463370218383111104242366943E-37,
1921 1922 1923 1924 1925 1926 1927 1928
      309485009821345068724781056.89,
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
1929
    float outputs_RN[kTableLength] = {18446744073709551617.0,
1930
      4503599627370496.0, -4503599627370496.0,
1931 1932
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E37,
      1.7976931348623157E38, 0,
1933 1934 1935 1936 1937 1938 1939 1940
      309485009821345068724781057.0,
      2.0, 3.0, 2.0, 3.0, 4.0, 4.0,
      -2.0, -3.0, -2.0, -3.0, -4.0, -4.0,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
1941
    float outputs_RZ[kTableLength] = {18446744073709551617.0,
1942
      4503599627370496.0, -4503599627370496.0,
1943 1944
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E37,
      1.7976931348623157E38, 0,
1945 1946 1947 1948 1949 1950 1951 1952
      309485009821345068724781057.0,
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
1953
    float outputs_RP[kTableLength] = {18446744073709551617.0,
1954
      4503599627370496.0, -4503599627370496.0,
1955 1956
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E37,
      1.7976931348623157E38, 1,
1957 1958 1959 1960 1961 1962 1963 1964
      309485009821345068724781057.0,
      3.0, 3.0, 3.0, 4.0, 4.0, 4.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
1965
    float outputs_RM[kTableLength] = {18446744073709551617.0,
1966
      4503599627370496.0, -4503599627370496.0,
1967 1968
      1.26782468584154733584017312973E30, 1.44860108245951772690707170478E37,
      1.7976931348623157E38, 0,
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
      309485009821345068724781057.0,
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -3.0, -3.0, -3.0, -4.0, -4.0, -4.0,
      37778931862957161709568.0, 37778931862957161709569.0,
      37778931862957161709580.0, 37778931862957161709581.0,
      37778931862957161709582.0, 37778931862957161709583.0,
      37778931862957161709584.0, 37778931862957161709585.0,
      37778931862957161709586.0, 37778931862957161709587.0};
    int fcsr_inputs[4] =
      {kRoundToNearest, kRoundToZero, kRoundToPlusInf, kRoundToMinusInf};
1979
    float* outputs[4] = {outputs_RN, outputs_RZ, outputs_RP, outputs_RM};
1980 1981
    __ lwc1(f4, MemOperand(a0, offsetof(TestFloat, a)) );
    __ lw(t0, MemOperand(a0, offsetof(TestFloat, fcsr)) );
1982 1983
    __ cfc1(t1, FCSR);
    __ ctc1(t0, FCSR);
1984
    __ rint_s(f8, f4);
1985
    __ swc1(f8, MemOperand(a0, offsetof(TestFloat, b)) );
1986 1987 1988 1989 1990 1991 1992 1993 1994
    __ ctc1(t1, FCSR);
    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
1995 1996

    for (int j = 0; j < 4; j++) {
1997
      test.fcsr = fcsr_inputs[j];
1998
      for (int i = 0; i < kTableLength; i++) {
1999
        test.a = inputs[i];
2000
        (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2001 2002 2003 2004 2005 2006 2007
        CHECK_EQ(test.b, outputs[j][i]);
      }
    }
  }
}


2008 2009
TEST(mina_maxa) {
  if (kArchVariant == kMips64r6) {
2010
    const int kTableLength = 23;
2011 2012 2013
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
2014
    MacroAssembler assm(isolate, nullptr, 0,
2015
                        v8::internal::CodeObjectRequired::kYes);
2016 2017 2018 2019 2020 2021 2022 2023
    const double dnan = std::numeric_limits<double>::quiet_NaN();
    const double dinf = std::numeric_limits<double>::infinity();
    const double dminf = -std::numeric_limits<double>::infinity();
    const float fnan = std::numeric_limits<float>::quiet_NaN();
    const float finf = std::numeric_limits<float>::infinity();
    const float fminf = std::numeric_limits<float>::infinity();

    struct TestFloat {
2024 2025 2026 2027 2028 2029 2030 2031
      double a;
      double b;
      double resd;
      double resd1;
      float c;
      float d;
      float resf;
      float resf1;
2032
    };
2033 2034

    TestFloat test;
2035
    double inputsa[kTableLength] = {
2036 2037
        5.3,  4.8, 6.1,  9.8, 9.8,  9.8,  -10.0, -8.9, -9.8,  -10.0, -8.9, -9.8,
        dnan, 3.0, -0.0, 0.0, dinf, dnan, 42.0,  dinf, dminf, dinf,  dnan};
2038
    double inputsb[kTableLength] = {
2039 2040
        4.8, 5.3,  6.1, -10.0, -8.9, -9.8, 9.8,  9.8,  9.8,  -9.8,  -11.2, -9.8,
        3.0, dnan, 0.0, -0.0,  dnan, dinf, dinf, 42.0, dinf, dminf, dnan};
2041
    double resd[kTableLength] = {
2042 2043
        4.8, 4.8, 6.1,  9.8,  -8.9, -9.8, 9.8,  -8.9, -9.8,  -9.8,  -8.9, -9.8,
        3.0, 3.0, -0.0, -0.0, dinf, dinf, 42.0, 42.0, dminf, dminf, dnan};
2044
    double resd1[kTableLength] = {
2045 2046
        5.3, 5.3, 6.1, -10.0, 9.8,  9.8,  -10.0, 9.8,  9.8,  -10.0, -11.2, -9.8,
        3.0, 3.0, 0.0, 0.0,   dinf, dinf, dinf,  dinf, dinf, dinf,  dnan};
2047
    float inputsc[kTableLength] = {
2048 2049 2050 2051 2052 2053
        5.3,  4.8, 6.1,  9.8, 9.8,  9.8,  -10.0, -8.9, -9.8,  -10.0, -8.9, -9.8,
        fnan, 3.0, -0.0, 0.0, finf, fnan, 42.0,  finf, fminf, finf,  fnan};
    float inputsd[kTableLength] = {4.8,  5.3,  6.1,  -10.0, -8.9,  -9.8,
                                   9.8,  9.8,  9.8,  -9.8,  -11.2, -9.8,
                                   3.0,  fnan, -0.0, 0.0,   fnan,  finf,
                                   finf, 42.0, finf, fminf, fnan};
2054
    float resf[kTableLength] = {
2055 2056
        4.8, 4.8, 6.1,  9.8,  -8.9, -9.8, 9.8,  -8.9, -9.8,  -9.8,  -8.9, -9.8,
        3.0, 3.0, -0.0, -0.0, finf, finf, 42.0, 42.0, fminf, fminf, fnan};
2057
    float resf1[kTableLength] = {
2058 2059
        5.3, 5.3, 6.1, -10.0, 9.8,  9.8,  -10.0, 9.8,  9.8,  -10.0, -11.2, -9.8,
        3.0, 3.0, 0.0, 0.0,   finf, finf, finf,  finf, finf, finf,  fnan};
2060

2061 2062 2063 2064
    __ ldc1(f2, MemOperand(a0, offsetof(TestFloat, a)) );
    __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, b)) );
    __ lwc1(f8, MemOperand(a0, offsetof(TestFloat, c)) );
    __ lwc1(f10, MemOperand(a0, offsetof(TestFloat, d)) );
2065 2066 2067 2068
    __ mina_d(f6, f2, f4);
    __ mina_s(f12, f8, f10);
    __ maxa_d(f14, f2, f4);
    __ maxa_s(f16, f8, f10);
2069 2070 2071 2072
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, resf)) );
    __ sdc1(f6, MemOperand(a0, offsetof(TestFloat, resd)) );
    __ swc1(f16, MemOperand(a0, offsetof(TestFloat, resf1)) );
    __ sdc1(f14, MemOperand(a0, offsetof(TestFloat, resd1)) );
2073 2074 2075 2076 2077 2078 2079 2080
    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
2081
    for (int i = 0; i < kTableLength; i++) {
2082 2083 2084 2085
      test.a = inputsa[i];
      test.b = inputsb[i];
      test.c = inputsc[i];
      test.d = inputsd[i];
2086
      (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2087

2088 2089 2090 2091 2092 2093
      if (i < kTableLength - 1) {
        CHECK_EQ(test.resd, resd[i]);
        CHECK_EQ(test.resf, resf[i]);
        CHECK_EQ(test.resd1, resd1[i]);
        CHECK_EQ(test.resf1, resf1[i]);
      } else {
2094 2095 2096 2097
        CHECK(std::isnan(test.resd));
        CHECK(std::isnan(test.resf));
        CHECK(std::isnan(test.resd1));
        CHECK(std::isnan(test.resf1));
2098
      }
2099 2100 2101 2102 2103 2104
    }
  }
}



2105
// ----------------------mips64r2 specific tests----------------------
2106 2107 2108 2109 2110
TEST(trunc_l) {
  if (kArchVariant == kMips64r2) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
2111 2112
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
2113 2114
    const double dFPU64InvalidResult = static_cast<double>(kFPU64InvalidResult);
    typedef struct test_float {
2115
      uint32_t isNaN2008;
2116 2117 2118 2119 2120
      double a;
      float b;
      int64_t c;  // a trunc result
      int64_t d;  // b trunc result
    }Test;
2121 2122
    const int kTableLength = 15;
    double inputs_D[kTableLength] = {
2123 2124 2125 2126 2127 2128
        2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
        -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
        2147483648.0,
        std::numeric_limits<double>::quiet_NaN(),
        std::numeric_limits<double>::infinity()
        };
2129
    float inputs_S[kTableLength] = {
2130 2131 2132 2133 2134 2135
        2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
        -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
        2147483648.0,
        std::numeric_limits<float>::quiet_NaN(),
        std::numeric_limits<float>::infinity()
        };
2136
    double outputs[kTableLength] = {
2137 2138 2139 2140
        2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
        -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
        2147483648.0, dFPU64InvalidResult,
        dFPU64InvalidResult};
2141 2142 2143 2144 2145
    double outputsNaN2008[kTableLength] = {
        2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
        -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
        2147483648.0, dFPU64InvalidResult,
        dFPU64InvalidResult};
2146

2147 2148
    __ cfc1(t1, FCSR);
    __ sw(t1, MemOperand(a0, offsetof(Test, isNaN2008)));
2149 2150
    __ ldc1(f4, MemOperand(a0, offsetof(Test, a)) );
    __ lwc1(f6, MemOperand(a0, offsetof(Test, b)) );
2151 2152
    __ trunc_l_d(f8, f4);
    __ trunc_l_s(f10, f6);
2153 2154
    __ sdc1(f8, MemOperand(a0, offsetof(Test, c)) );
    __ sdc1(f10, MemOperand(a0, offsetof(Test, d)) );
2155 2156 2157 2158 2159 2160 2161 2162
    __ jr(ra);
    __ nop();
    Test test;
    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
2163
    for (int i = 0; i < kTableLength; i++) {
2164 2165
      test.a = inputs_D[i];
      test.b = inputs_S[i];
2166
      (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2167 2168 2169 2170 2171 2172
      if ((test.isNaN2008 & kFCSRNaN2008FlagMask) &&
              kArchVariant == kMips64r6) {
        CHECK_EQ(test.c, outputsNaN2008[i]);
      } else {
        CHECK_EQ(test.c, outputs[i]);
      }
2173 2174 2175 2176 2177 2178 2179 2180
      CHECK_EQ(test.d, test.c);
    }
  }
}


TEST(movz_movn) {
  if (kArchVariant == kMips64r2) {
2181
    const int kTableLength = 4;
2182 2183 2184
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
2185 2186
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202

    typedef struct test_float {
      int64_t rt;
      double a;
      double b;
      double bold;
      double b1;
      double bold1;
      float c;
      float d;
      float dold;
      float d1;
      float dold1;
    }TestFloat;

    TestFloat test;
2203
    double inputs_D[kTableLength] = {
2204 2205
      5.3, -5.3, 5.3, -2.9
    };
2206
    double inputs_S[kTableLength] = {
2207 2208 2209
      4.8, 4.8, -4.8, -0.29
    };

2210
    float outputs_S[kTableLength] = {
2211 2212
      4.8, 4.8, -4.8, -0.29
    };
2213
    double outputs_D[kTableLength] = {
2214 2215 2216
      5.3, -5.3, 5.3, -2.9
    };

2217 2218
    __ ldc1(f2, MemOperand(a0, offsetof(TestFloat, a)) );
    __ lwc1(f6, MemOperand(a0, offsetof(TestFloat, c)) );
2219
    __ ld(t0, MemOperand(a0, offsetof(TestFloat, rt)));
2220 2221 2222 2223
    __ Move(f12, 0.0);
    __ Move(f10, 0.0);
    __ Move(f16, 0.0);
    __ Move(f14, 0.0);
2224 2225 2226 2227
    __ sdc1(f12, MemOperand(a0, offsetof(TestFloat, bold)) );
    __ swc1(f10, MemOperand(a0, offsetof(TestFloat, dold)) );
    __ sdc1(f16, MemOperand(a0, offsetof(TestFloat, bold1)) );
    __ swc1(f14, MemOperand(a0, offsetof(TestFloat, dold1)) );
2228 2229 2230 2231
    __ movz_s(f10, f6, t0);
    __ movz_d(f12, f2, t0);
    __ movn_s(f14, f6, t0);
    __ movn_d(f16, f2, t0);
2232 2233 2234 2235
    __ swc1(f10, MemOperand(a0, offsetof(TestFloat, d)) );
    __ sdc1(f12, MemOperand(a0, offsetof(TestFloat, b)) );
    __ swc1(f14, MemOperand(a0, offsetof(TestFloat, d1)) );
    __ sdc1(f16, MemOperand(a0, offsetof(TestFloat, b1)) );
2236 2237 2238 2239 2240 2241 2242 2243
    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
2244
    for (int i = 0; i < kTableLength; i++) {
2245 2246 2247 2248
      test.a = inputs_D[i];
      test.c = inputs_S[i];

      test.rt = 1;
2249
      (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2250 2251 2252 2253 2254 2255
      CHECK_EQ(test.b, test.bold);
      CHECK_EQ(test.d, test.dold);
      CHECK_EQ(test.b1, outputs_D[i]);
      CHECK_EQ(test.d1, outputs_S[i]);

      test.rt = 0;
2256
      (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
      CHECK_EQ(test.b, outputs_D[i]);
      CHECK_EQ(test.d, outputs_S[i]);
      CHECK_EQ(test.b1, test.bold1);
      CHECK_EQ(test.d1, test.dold1);
    }
  }
}


TEST(movt_movd) {
  if (kArchVariant == kMips64r2) {
2268
    const int kTableLength = 4;
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    typedef struct test_float {
      double srcd;
      double dstd;
      double dstdold;
      double dstd1;
      double dstdold1;
      float srcf;
      float dstf;
      float dstfold;
      float dstf1;
      float dstfold1;
      int32_t cc;
      int32_t fcsr;
    }TestFloat;

    TestFloat test;
2287
    double inputs_D[kTableLength] = {
2288 2289
      5.3, -5.3, 20.8, -2.9
    };
2290
    double inputs_S[kTableLength] = {
2291 2292 2293
      4.88, 4.8, -4.8, -0.29
    };

2294
    float outputs_S[kTableLength] = {
2295 2296
      4.88, 4.8, -4.8, -0.29
    };
2297
    double outputs_D[kTableLength] = {
2298 2299 2300 2301
      5.3, -5.3, 20.8, -2.9
    };
    int condition_flags[8] = {0, 1, 2, 3, 4, 5, 6, 7};

2302
    for (int i = 0; i < kTableLength; i++) {
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
      test.srcd = inputs_D[i];
      test.srcf = inputs_S[i];

      for (int j = 0; j< 8; j++) {
        test.cc = condition_flags[j];
        if (test.cc == 0) {
          test.fcsr = 1 << 23;
        } else {
          test.fcsr = 1 << (24+condition_flags[j]);
        }
        HandleScope scope(isolate);
2314 2315
        MacroAssembler assm(isolate, NULL, 0,
                            v8::internal::CodeObjectRequired::kYes);
2316 2317 2318
        __ ldc1(f2, MemOperand(a0, offsetof(TestFloat, srcd)) );
        __ lwc1(f4, MemOperand(a0, offsetof(TestFloat, srcf)) );
        __ lw(t1, MemOperand(a0, offsetof(TestFloat, fcsr)) );
2319 2320 2321 2322 2323
        __ cfc1(t0, FCSR);
        __ ctc1(t1, FCSR);
        __ li(t2, 0x0);
        __ mtc1(t2, f12);
        __ mtc1(t2, f10);
2324 2325
        __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, dstdold)) );
        __ swc1(f12, MemOperand(a0, offsetof(TestFloat, dstfold)) );
2326 2327
        __ movt_s(f12, f4, test.cc);
        __ movt_d(f10, f2, test.cc);
2328 2329 2330 2331
        __ swc1(f12, MemOperand(a0, offsetof(TestFloat, dstf)) );
        __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, dstd)) );
        __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, dstdold1)) );
        __ swc1(f12, MemOperand(a0, offsetof(TestFloat, dstfold1)) );
2332 2333
        __ movf_s(f12, f4, test.cc);
        __ movf_d(f10, f2, test.cc);
2334 2335
        __ swc1(f12, MemOperand(a0, offsetof(TestFloat, dstf1)) );
        __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, dstd1)) );
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
        __ ctc1(t0, FCSR);
        __ jr(ra);
        __ nop();

        CodeDesc desc;
        assm.GetCode(&desc);
        Handle<Code> code = isolate->factory()->NewCode(
            desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
        F3 f = FUNCTION_CAST<F3>(code->entry());

2346
        (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2347 2348 2349 2350 2351
        CHECK_EQ(test.dstf, outputs_S[i]);
        CHECK_EQ(test.dstd, outputs_D[i]);
        CHECK_EQ(test.dstf1, test.dstfold1);
        CHECK_EQ(test.dstd1, test.dstdold1);
        test.fcsr = 0;
2352
        (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
        CHECK_EQ(test.dstf, test.dstfold);
        CHECK_EQ(test.dstd, test.dstdold);
        CHECK_EQ(test.dstf1, outputs_S[i]);
        CHECK_EQ(test.dstd1, outputs_D[i]);
      }
    }
  }
}



// ----------------------tests for all archs--------------------------
TEST(cvt_w_d) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
2369
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
2370 2371 2372 2373 2374 2375

  typedef struct test_float {
    double a;
    int32_t b;
    int fcsr;
  }Test;
2376 2377
  const int kTableLength = 24;
  double inputs[kTableLength] = {
2378 2379 2380 2381 2382 2383 2384
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      2147483637.0, 2147483638.0, 2147483639.0,
      2147483640.0, 2147483641.0, 2147483642.0,
      2147483643.0, 2147483644.0, 2147483645.0,
      2147483646.0, 2147483647.0, 2147483653.0
      };
2385
  double outputs_RN[kTableLength] = {
2386 2387 2388 2389 2390 2391
      2.0, 3.0, 2.0, 3.0, 4.0, 4.0,
      -2.0, -3.0, -2.0, -3.0, -4.0, -4.0,
      2147483637.0, 2147483638.0, 2147483639.0,
      2147483640.0, 2147483641.0, 2147483642.0,
      2147483643.0, 2147483644.0, 2147483645.0,
      2147483646.0, 2147483647.0, kFPUInvalidResult};
2392
  double outputs_RZ[kTableLength] = {
2393 2394 2395 2396 2397 2398
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      2147483637.0, 2147483638.0, 2147483639.0,
      2147483640.0, 2147483641.0, 2147483642.0,
      2147483643.0, 2147483644.0, 2147483645.0,
      2147483646.0, 2147483647.0, kFPUInvalidResult};
2399
  double outputs_RP[kTableLength] = {
2400 2401 2402 2403 2404 2405
      3.0, 3.0, 3.0, 4.0, 4.0, 4.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      2147483637.0, 2147483638.0, 2147483639.0,
      2147483640.0, 2147483641.0, 2147483642.0,
      2147483643.0, 2147483644.0, 2147483645.0,
      2147483646.0, 2147483647.0, kFPUInvalidResult};
2406
  double outputs_RM[kTableLength] = {
2407 2408 2409 2410 2411 2412 2413 2414 2415
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -3.0, -3.0, -3.0, -4.0, -4.0, -4.0,
      2147483637.0, 2147483638.0, 2147483639.0,
      2147483640.0, 2147483641.0, 2147483642.0,
      2147483643.0, 2147483644.0, 2147483645.0,
      2147483646.0, 2147483647.0, kFPUInvalidResult};
  int fcsr_inputs[4] =
      {kRoundToNearest, kRoundToZero, kRoundToPlusInf, kRoundToMinusInf};
  double* outputs[4] = {outputs_RN, outputs_RZ, outputs_RP, outputs_RM};
2416 2417
  __ ldc1(f4, MemOperand(a0, offsetof(Test, a)) );
  __ lw(t0, MemOperand(a0, offsetof(Test, fcsr)) );
2418 2419 2420
  __ cfc1(t1, FCSR);
  __ ctc1(t0, FCSR);
  __ cvt_w_d(f8, f4);
2421
  __ swc1(f8, MemOperand(a0, offsetof(Test, b)) );
2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
  __ ctc1(t1, FCSR);
  __ jr(ra);
  __ nop();
  Test test;
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  for (int j = 0; j < 4; j++) {
    test.fcsr = fcsr_inputs[j];
2433
    for (int i = 0; i < kTableLength; i++) {
2434
      test.a = inputs[i];
2435
      (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
      CHECK_EQ(test.b, outputs[j][i]);
    }
  }
}


TEST(trunc_w) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
2446
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
2447 2448

  typedef struct test_float {
2449
    uint32_t isNaN2008;
2450 2451 2452 2453 2454
    double a;
    float b;
    int32_t c;  // a trunc result
    int32_t d;  // b trunc result
  }Test;
2455 2456
  const int kTableLength = 15;
  double inputs_D[kTableLength] = {
2457 2458 2459 2460 2461 2462
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      2147483648.0,
      std::numeric_limits<double>::quiet_NaN(),
      std::numeric_limits<double>::infinity()
      };
2463
  float inputs_S[kTableLength] = {
2464 2465 2466 2467 2468 2469
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      2147483648.0,
      std::numeric_limits<float>::quiet_NaN(),
      std::numeric_limits<float>::infinity()
      };
2470
  double outputs[kTableLength] = {
2471 2472 2473 2474
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      kFPUInvalidResult, kFPUInvalidResult,
      kFPUInvalidResult};
2475 2476 2477 2478 2479 2480
  double outputsNaN2008[kTableLength] = {
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      kFPUInvalidResult,
      0,
      kFPUInvalidResult};
2481

2482 2483
  __ cfc1(t1, FCSR);
  __ sw(t1, MemOperand(a0, offsetof(Test, isNaN2008)));
2484 2485
  __ ldc1(f4, MemOperand(a0, offsetof(Test, a)) );
  __ lwc1(f6, MemOperand(a0, offsetof(Test, b)) );
2486 2487
  __ trunc_w_d(f8, f4);
  __ trunc_w_s(f10, f6);
2488 2489
  __ swc1(f8, MemOperand(a0, offsetof(Test, c)) );
  __ swc1(f10, MemOperand(a0, offsetof(Test, d)) );
2490 2491 2492 2493 2494 2495 2496 2497
  __ jr(ra);
  __ nop();
  Test test;
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
2498
  for (int i = 0; i < kTableLength; i++) {
2499 2500
    test.a = inputs_D[i];
    test.b = inputs_S[i];
2501
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2502 2503 2504 2505 2506
    if ((test.isNaN2008 & kFCSRNaN2008FlagMask) && kArchVariant == kMips64r6) {
      CHECK_EQ(test.c, outputsNaN2008[i]);
    } else {
      CHECK_EQ(test.c, outputs[i]);
    }
2507 2508 2509 2510 2511 2512 2513 2514 2515
    CHECK_EQ(test.d, test.c);
  }
}


TEST(round_w) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
2516
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
2517 2518

  typedef struct test_float {
2519
    uint32_t isNaN2008;
2520 2521 2522 2523 2524
    double a;
    float b;
    int32_t c;  // a trunc result
    int32_t d;  // b trunc result
  }Test;
2525 2526
  const int kTableLength = 15;
  double inputs_D[kTableLength] = {
2527 2528 2529 2530 2531 2532
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      2147483648.0,
      std::numeric_limits<double>::quiet_NaN(),
      std::numeric_limits<double>::infinity()
      };
2533
  float inputs_S[kTableLength] = {
2534 2535 2536 2537 2538 2539
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      2147483648.0,
      std::numeric_limits<float>::quiet_NaN(),
      std::numeric_limits<float>::infinity()
      };
2540
  double outputs[kTableLength] = {
2541 2542 2543 2544
      2.0, 3.0, 2.0, 3.0, 4.0, 4.0,
      -2.0, -3.0, -2.0, -3.0, -4.0, -4.0,
      kFPUInvalidResult, kFPUInvalidResult,
      kFPUInvalidResult};
2545 2546 2547 2548 2549
  double outputsNaN2008[kTableLength] = {
      2.0, 3.0, 2.0, 3.0, 4.0, 4.0,
      -2.0, -3.0, -2.0, -3.0, -4.0, -4.0,
      kFPUInvalidResult, 0,
      kFPUInvalidResult};
2550

2551 2552
  __ cfc1(t1, FCSR);
  __ sw(t1, MemOperand(a0, offsetof(Test, isNaN2008)));
2553 2554
  __ ldc1(f4, MemOperand(a0, offsetof(Test, a)) );
  __ lwc1(f6, MemOperand(a0, offsetof(Test, b)) );
2555 2556
  __ round_w_d(f8, f4);
  __ round_w_s(f10, f6);
2557 2558
  __ swc1(f8, MemOperand(a0, offsetof(Test, c)) );
  __ swc1(f10, MemOperand(a0, offsetof(Test, d)) );
2559 2560 2561 2562 2563 2564 2565 2566
  __ jr(ra);
  __ nop();
  Test test;
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
2567
  for (int i = 0; i < kTableLength; i++) {
2568 2569
    test.a = inputs_D[i];
    test.b = inputs_S[i];
2570
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2571 2572 2573 2574 2575
    if ((test.isNaN2008 & kFCSRNaN2008FlagMask) && kArchVariant == kMips64r6) {
      CHECK_EQ(test.c, outputsNaN2008[i]);
    } else {
      CHECK_EQ(test.c, outputs[i]);
    }
2576 2577 2578 2579 2580 2581 2582 2583 2584
    CHECK_EQ(test.d, test.c);
  }
}


TEST(round_l) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
2585 2586
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
2587 2588
    const double dFPU64InvalidResult = static_cast<double>(kFPU64InvalidResult);
    typedef struct test_float {
2589
      uint32_t isNaN2008;
2590 2591 2592 2593 2594
      double a;
      float b;
      int64_t c;
      int64_t d;
    }Test;
2595 2596
    const int kTableLength = 15;
    double inputs_D[kTableLength] = {
2597 2598 2599 2600 2601 2602
        2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
        -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
        2147483648.0,
        std::numeric_limits<double>::quiet_NaN(),
        std::numeric_limits<double>::infinity()
        };
2603
    float inputs_S[kTableLength] = {
2604 2605 2606 2607 2608 2609
        2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
        -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
        2147483648.0,
        std::numeric_limits<float>::quiet_NaN(),
        std::numeric_limits<float>::infinity()
        };
2610
    double outputs[kTableLength] = {
2611 2612 2613 2614
        2.0, 3.0, 2.0, 3.0, 4.0, 4.0,
        -2.0, -3.0, -2.0, -3.0, -4.0, -4.0,
        2147483648.0, dFPU64InvalidResult,
        dFPU64InvalidResult};
2615 2616 2617 2618 2619 2620
    double outputsNaN2008[kTableLength] = {
        2.0, 3.0, 2.0, 3.0, 4.0, 4.0,
        -2.0, -3.0, -2.0, -3.0, -4.0, -4.0,
        2147483648.0,
        0,
        dFPU64InvalidResult};
2621

2622 2623
    __ cfc1(t1, FCSR);
    __ sw(t1, MemOperand(a0, offsetof(Test, isNaN2008)));
2624 2625
    __ ldc1(f4, MemOperand(a0, offsetof(Test, a)) );
    __ lwc1(f6, MemOperand(a0, offsetof(Test, b)) );
2626 2627
    __ round_l_d(f8, f4);
    __ round_l_s(f10, f6);
2628 2629
    __ sdc1(f8, MemOperand(a0, offsetof(Test, c)) );
    __ sdc1(f10, MemOperand(a0, offsetof(Test, d)) );
2630 2631 2632 2633 2634 2635 2636 2637
    __ jr(ra);
    __ nop();
    Test test;
    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
2638
    for (int i = 0; i < kTableLength; i++) {
2639 2640
      test.a = inputs_D[i];
      test.b = inputs_S[i];
2641
      (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2642 2643 2644 2645 2646 2647
      if ((test.isNaN2008 & kFCSRNaN2008FlagMask) &&
              kArchVariant == kMips64r6) {
        CHECK_EQ(test.c, outputsNaN2008[i]);
      } else {
        CHECK_EQ(test.c, outputs[i]);
      }
2648 2649 2650 2651 2652 2653
      CHECK_EQ(test.d, test.c);
    }
}


TEST(sub) {
2654
  const int kTableLength = 12;
2655 2656 2657
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
2658
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669

  typedef struct test_float {
    float a;
    float b;
    float resultS;
    double c;
    double d;
    double resultD;
  }TestFloat;

  TestFloat test;
2670
  double inputfs_D[kTableLength] = {
2671 2672 2673
    5.3, 4.8, 2.9, -5.3, -4.8, -2.9,
    5.3, 4.8, 2.9, -5.3, -4.8, -2.9
  };
2674
  double inputft_D[kTableLength] = {
2675 2676 2677
    4.8, 5.3, 2.9, 4.8, 5.3, 2.9,
    -4.8, -5.3, -2.9, -4.8, -5.3, -2.9
  };
2678
  double outputs_D[kTableLength] = {
2679 2680 2681
    0.5, -0.5, 0.0, -10.1, -10.1, -5.8,
    10.1, 10.1, 5.8, -0.5, 0.5, 0.0
  };
2682
  float inputfs_S[kTableLength] = {
2683 2684 2685
    5.3, 4.8, 2.9, -5.3, -4.8, -2.9,
    5.3, 4.8, 2.9, -5.3, -4.8, -2.9
  };
2686
  float inputft_S[kTableLength] = {
2687 2688 2689
    4.8, 5.3, 2.9, 4.8, 5.3, 2.9,
    -4.8, -5.3, -2.9, -4.8, -5.3, -2.9
  };
2690
  float outputs_S[kTableLength] = {
2691 2692 2693
    0.5, -0.5, 0.0, -10.1, -10.1, -5.8,
    10.1, 10.1, 5.8, -0.5, 0.5, 0.0
  };
2694 2695 2696 2697
  __ lwc1(f2, MemOperand(a0, offsetof(TestFloat, a)) );
  __ lwc1(f4, MemOperand(a0, offsetof(TestFloat, b)) );
  __ ldc1(f8, MemOperand(a0, offsetof(TestFloat, c)) );
  __ ldc1(f10, MemOperand(a0, offsetof(TestFloat, d)) );
2698 2699
  __ sub_s(f6, f2, f4);
  __ sub_d(f12, f8, f10);
2700 2701
  __ swc1(f6, MemOperand(a0, offsetof(TestFloat, resultS)) );
  __ sdc1(f12, MemOperand(a0, offsetof(TestFloat, resultD)) );
2702 2703 2704 2705 2706 2707 2708 2709
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
2710
  for (int i = 0; i < kTableLength; i++) {
2711 2712 2713 2714
    test.a = inputfs_S[i];
    test.b = inputft_S[i];
    test.c = inputfs_D[i];
    test.d = inputft_D[i];
2715
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2716 2717 2718 2719 2720 2721 2722
    CHECK_EQ(test.resultS, outputs_S[i]);
    CHECK_EQ(test.resultD, outputs_D[i]);
  }
}


TEST(sqrt_rsqrt_recip) {
2723
  const int kTableLength = 4;
2724 2725 2726 2727 2728 2729 2730
  const double deltaDouble = 2E-15;
  const float deltaFloat = 2E-7;
  const float sqrt2_s = sqrt(2);
  const double sqrt2_d = sqrt(2);
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
2731
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744

  typedef struct test_float {
    float a;
    float resultS;
    float resultS1;
    float resultS2;
    double c;
    double resultD;
    double resultD1;
    double resultD2;
  }TestFloat;
  TestFloat test;

2745
  double inputs_D[kTableLength] = {
2746 2747 2748
    0.0L, 4.0L, 2.0L, 4e-28L
  };

2749
  double outputs_D[kTableLength] = {
2750 2751
    0.0L, 2.0L, sqrt2_d, 2e-14L
  };
2752
  float inputs_S[kTableLength] = {
2753 2754 2755
    0.0, 4.0, 2.0, 4e-28
  };

2756
  float outputs_S[kTableLength] = {
2757 2758 2759 2760
    0.0, 2.0, sqrt2_s, 2e-14
  };


2761 2762
  __ lwc1(f2, MemOperand(a0, offsetof(TestFloat, a)) );
  __ ldc1(f8, MemOperand(a0, offsetof(TestFloat, c)) );
2763 2764 2765 2766 2767
  __ sqrt_s(f6, f2);
  __ sqrt_d(f12, f8);
  __ rsqrt_d(f14, f8);
  __ rsqrt_s(f16, f2);
  __ recip_d(f18, f8);
2768
  __ recip_s(f4, f2);
2769 2770 2771 2772
  __ swc1(f6, MemOperand(a0, offsetof(TestFloat, resultS)) );
  __ sdc1(f12, MemOperand(a0, offsetof(TestFloat, resultD)) );
  __ swc1(f16, MemOperand(a0, offsetof(TestFloat, resultS1)) );
  __ sdc1(f14, MemOperand(a0, offsetof(TestFloat, resultD1)) );
2773
  __ swc1(f4, MemOperand(a0, offsetof(TestFloat, resultS2)) );
2774
  __ sdc1(f18, MemOperand(a0, offsetof(TestFloat, resultD2)) );
2775 2776 2777 2778 2779 2780 2781 2782 2783
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());

2784
  for (int i = 0; i < kTableLength; i++) {
2785 2786 2787 2788 2789
    float f1;
    double d1;
    test.a = inputs_S[i];
    test.c = inputs_D[i];

2790
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818

    CHECK_EQ(test.resultS, outputs_S[i]);
    CHECK_EQ(test.resultD, outputs_D[i]);

    if (i != 0) {
      f1 = test.resultS1 - 1.0F/outputs_S[i];
      f1 = (f1 < 0) ? f1 : -f1;
      CHECK(f1 <= deltaFloat);
      d1 = test.resultD1 - 1.0L/outputs_D[i];
      d1 = (d1 < 0) ? d1 : -d1;
      CHECK(d1 <= deltaDouble);
      f1 = test.resultS2 - 1.0F/inputs_S[i];
      f1 = (f1 < 0) ? f1 : -f1;
      CHECK(f1 <= deltaFloat);
      d1 = test.resultD2 - 1.0L/inputs_D[i];
      d1 = (d1 < 0) ? d1 : -d1;
      CHECK(d1 <= deltaDouble);
    } else {
      CHECK_EQ(test.resultS1, 1.0F/outputs_S[i]);
      CHECK_EQ(test.resultD1, 1.0L/outputs_D[i]);
      CHECK_EQ(test.resultS2, 1.0F/inputs_S[i]);
      CHECK_EQ(test.resultD2, 1.0L/inputs_D[i]);
    }
  }
}


TEST(neg) {
2819
  const int kTableLength = 2;
2820 2821 2822
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
2823
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
2824 2825 2826 2827 2828 2829 2830 2831 2832

  typedef struct test_float {
    float a;
    float resultS;
    double c;
    double resultD;
  }TestFloat;

  TestFloat test;
2833
  double inputs_D[kTableLength] = {
2834 2835 2836
    4.0, -2.0
  };

2837
  double outputs_D[kTableLength] = {
2838 2839
    -4.0, 2.0
  };
2840
  float inputs_S[kTableLength] = {
2841 2842 2843
    4.0, -2.0
  };

2844
  float outputs_S[kTableLength] = {
2845 2846
    -4.0, 2.0
  };
2847 2848
  __ lwc1(f2, MemOperand(a0, offsetof(TestFloat, a)) );
  __ ldc1(f8, MemOperand(a0, offsetof(TestFloat, c)) );
2849 2850
  __ neg_s(f6, f2);
  __ neg_d(f12, f8);
2851 2852
  __ swc1(f6, MemOperand(a0, offsetof(TestFloat, resultS)) );
  __ sdc1(f12, MemOperand(a0, offsetof(TestFloat, resultD)) );
2853 2854 2855 2856 2857 2858 2859 2860
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
2861
  for (int i = 0; i < kTableLength; i++) {
2862 2863
    test.a = inputs_S[i];
    test.c = inputs_D[i];
2864
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2865 2866 2867 2868 2869 2870 2871 2872
    CHECK_EQ(test.resultS, outputs_S[i]);
    CHECK_EQ(test.resultD, outputs_D[i]);
  }
}



TEST(mul) {
2873
  const int kTableLength = 4;
2874 2875 2876
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
2877
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888

  typedef struct test_float {
    float a;
    float b;
    float resultS;
    double c;
    double d;
    double resultD;
  }TestFloat;

  TestFloat test;
2889
  double inputfs_D[kTableLength] = {
2890 2891
    5.3, -5.3, 5.3, -2.9
  };
2892
  double inputft_D[kTableLength] = {
2893 2894 2895
    4.8, 4.8, -4.8, -0.29
  };

2896
  float inputfs_S[kTableLength] = {
2897 2898
    5.3, -5.3, 5.3, -2.9
  };
2899
  float inputft_S[kTableLength] = {
2900 2901 2902
    4.8, 4.8, -4.8, -0.29
  };

2903 2904 2905 2906
  __ lwc1(f2, MemOperand(a0, offsetof(TestFloat, a)) );
  __ lwc1(f4, MemOperand(a0, offsetof(TestFloat, b)) );
  __ ldc1(f6, MemOperand(a0, offsetof(TestFloat, c)) );
  __ ldc1(f8, MemOperand(a0, offsetof(TestFloat, d)) );
2907 2908
  __ mul_s(f10, f2, f4);
  __ mul_d(f12, f6, f8);
2909 2910
  __ swc1(f10, MemOperand(a0, offsetof(TestFloat, resultS)) );
  __ sdc1(f12, MemOperand(a0, offsetof(TestFloat, resultD)) );
2911 2912 2913 2914 2915 2916 2917 2918
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
2919
  for (int i = 0; i < kTableLength; i++) {
2920 2921 2922 2923
    test.a = inputfs_S[i];
    test.b = inputft_S[i];
    test.c = inputfs_D[i];
    test.d = inputft_D[i];
2924
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2925 2926 2927 2928 2929 2930 2931
    CHECK_EQ(test.resultS, inputfs_S[i]*inputft_S[i]);
    CHECK_EQ(test.resultD, inputfs_D[i]*inputft_D[i]);
  }
}


TEST(mov) {
2932
  const int kTableLength = 4;
2933 2934 2935
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
2936
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
2937 2938 2939 2940 2941 2942 2943 2944 2945

  typedef struct test_float {
    double a;
    double b;
    float c;
    float d;
  }TestFloat;

  TestFloat test;
2946
  double inputs_D[kTableLength] = {
2947 2948
    5.3, -5.3, 5.3, -2.9
  };
2949
  double inputs_S[kTableLength] = {
2950 2951 2952
    4.8, 4.8, -4.8, -0.29
  };

2953
  float outputs_S[kTableLength] = {
2954 2955
    4.8, 4.8, -4.8, -0.29
  };
2956
  double outputs_D[kTableLength] = {
2957 2958 2959
    5.3, -5.3, 5.3, -2.9
  };

2960
  __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, a)) );
2961
  __ lwc1(f6, MemOperand(a0, offsetof(TestFloat, c)) );
2962 2963 2964 2965
  __ mov_s(f8, f6);
  __ mov_d(f10, f4);
  __ swc1(f8, MemOperand(a0, offsetof(TestFloat, d)) );
  __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, b)) );
2966 2967 2968 2969 2970 2971 2972 2973
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
2974
  for (int i = 0; i < kTableLength; i++) {
2975 2976 2977
    test.a = inputs_D[i];
    test.c = inputs_S[i];

2978
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
    CHECK_EQ(test.b, outputs_D[i]);
    CHECK_EQ(test.d, outputs_S[i]);
  }
}


TEST(floor_w) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
2989
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
2990 2991

  typedef struct test_float {
2992
    uint32_t isNaN2008;
2993 2994 2995 2996 2997
    double a;
    float b;
    int32_t c;  // a floor result
    int32_t d;  // b floor result
  }Test;
2998 2999
  const int kTableLength = 15;
  double inputs_D[kTableLength] = {
3000 3001 3002 3003 3004 3005
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      2147483648.0,
      std::numeric_limits<double>::quiet_NaN(),
      std::numeric_limits<double>::infinity()
      };
3006
  float inputs_S[kTableLength] = {
3007 3008 3009 3010 3011 3012
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      2147483648.0,
      std::numeric_limits<float>::quiet_NaN(),
      std::numeric_limits<float>::infinity()
      };
3013
  double outputs[kTableLength] = {
3014 3015 3016 3017
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -3.0, -3.0, -3.0, -4.0, -4.0, -4.0,
      kFPUInvalidResult, kFPUInvalidResult,
      kFPUInvalidResult};
3018 3019 3020 3021 3022 3023
  double outputsNaN2008[kTableLength] = {
      2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
      -3.0, -3.0, -3.0, -4.0, -4.0, -4.0,
      kFPUInvalidResult,
      0,
      kFPUInvalidResult};
3024

3025 3026
  __ cfc1(t1, FCSR);
  __ sw(t1, MemOperand(a0, offsetof(Test, isNaN2008)));
3027 3028
  __ ldc1(f4, MemOperand(a0, offsetof(Test, a)) );
  __ lwc1(f6, MemOperand(a0, offsetof(Test, b)) );
3029 3030
  __ floor_w_d(f8, f4);
  __ floor_w_s(f10, f6);
3031 3032
  __ swc1(f8, MemOperand(a0, offsetof(Test, c)) );
  __ swc1(f10, MemOperand(a0, offsetof(Test, d)) );
3033 3034 3035 3036 3037 3038 3039 3040
  __ jr(ra);
  __ nop();
  Test test;
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
3041
  for (int i = 0; i < kTableLength; i++) {
3042 3043
    test.a = inputs_D[i];
    test.b = inputs_S[i];
3044
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3045 3046 3047 3048 3049
    if ((test.isNaN2008 & kFCSRNaN2008FlagMask) && kArchVariant == kMips64r6) {
      CHECK_EQ(test.c, outputsNaN2008[i]);
    } else {
      CHECK_EQ(test.c, outputs[i]);
    }
3050 3051 3052 3053 3054 3055 3056 3057 3058
    CHECK_EQ(test.d, test.c);
  }
}


TEST(floor_l) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
3059 3060
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
3061 3062
    const double dFPU64InvalidResult = static_cast<double>(kFPU64InvalidResult);
    typedef struct test_float {
3063
      uint32_t isNaN2008;
3064 3065 3066 3067 3068
      double a;
      float b;
      int64_t c;
      int64_t d;
    }Test;
3069 3070
    const int kTableLength = 15;
    double inputs_D[kTableLength] = {
3071 3072 3073 3074 3075 3076
        2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
        -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
        2147483648.0,
        std::numeric_limits<double>::quiet_NaN(),
        std::numeric_limits<double>::infinity()
        };
3077
    float inputs_S[kTableLength] = {
3078 3079 3080 3081 3082 3083
        2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
        -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
        2147483648.0,
        std::numeric_limits<float>::quiet_NaN(),
        std::numeric_limits<float>::infinity()
        };
3084
    double outputs[kTableLength] = {
3085 3086 3087 3088
        2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
        -3.0, -3.0, -3.0, -4.0, -4.0, -4.0,
        2147483648.0, dFPU64InvalidResult,
        dFPU64InvalidResult};
3089 3090 3091 3092 3093 3094
    double outputsNaN2008[kTableLength] = {
        2.0, 2.0, 2.0, 3.0, 3.0, 3.0,
        -3.0, -3.0, -3.0, -4.0, -4.0, -4.0,
        2147483648.0,
        0,
        dFPU64InvalidResult};
3095

3096 3097
    __ cfc1(t1, FCSR);
    __ sw(t1, MemOperand(a0, offsetof(Test, isNaN2008)));
3098 3099
    __ ldc1(f4, MemOperand(a0, offsetof(Test, a)) );
    __ lwc1(f6, MemOperand(a0, offsetof(Test, b)) );
3100 3101
    __ floor_l_d(f8, f4);
    __ floor_l_s(f10, f6);
3102 3103
    __ sdc1(f8, MemOperand(a0, offsetof(Test, c)) );
    __ sdc1(f10, MemOperand(a0, offsetof(Test, d)) );
3104 3105 3106 3107 3108 3109 3110 3111
    __ jr(ra);
    __ nop();
    Test test;
    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
3112
    for (int i = 0; i < kTableLength; i++) {
3113 3114
      test.a = inputs_D[i];
      test.b = inputs_S[i];
3115
      (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3116 3117 3118 3119 3120 3121
      if ((test.isNaN2008 & kFCSRNaN2008FlagMask) &&
              kArchVariant == kMips64r6) {
        CHECK_EQ(test.c, outputsNaN2008[i]);
      } else {
        CHECK_EQ(test.c, outputs[i]);
      }
3122 3123 3124 3125 3126 3127 3128 3129 3130
      CHECK_EQ(test.d, test.c);
    }
}


TEST(ceil_w) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
3131
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
3132 3133

  typedef struct test_float {
3134
    uint32_t isNaN2008;
3135 3136 3137 3138 3139
    double a;
    float b;
    int32_t c;  // a floor result
    int32_t d;  // b floor result
  }Test;
3140 3141
  const int kTableLength = 15;
  double inputs_D[kTableLength] = {
3142 3143 3144 3145 3146 3147
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      2147483648.0,
      std::numeric_limits<double>::quiet_NaN(),
      std::numeric_limits<double>::infinity()
      };
3148
  float inputs_S[kTableLength] = {
3149 3150 3151 3152 3153 3154
      2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
      -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
      2147483648.0,
      std::numeric_limits<float>::quiet_NaN(),
      std::numeric_limits<float>::infinity()
      };
3155
  double outputs[kTableLength] = {
3156 3157 3158 3159
      3.0, 3.0, 3.0, 4.0, 4.0, 4.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      kFPUInvalidResult, kFPUInvalidResult,
      kFPUInvalidResult};
3160 3161 3162 3163 3164 3165
  double outputsNaN2008[kTableLength] = {
      3.0, 3.0, 3.0, 4.0, 4.0, 4.0,
      -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
      kFPUInvalidResult,
      0,
      kFPUInvalidResult};
3166

3167 3168
  __ cfc1(t1, FCSR);
  __ sw(t1, MemOperand(a0, offsetof(Test, isNaN2008)));
3169 3170
  __ ldc1(f4, MemOperand(a0, offsetof(Test, a)) );
  __ lwc1(f6, MemOperand(a0, offsetof(Test, b)) );
3171 3172
  __ ceil_w_d(f8, f4);
  __ ceil_w_s(f10, f6);
3173 3174
  __ swc1(f8, MemOperand(a0, offsetof(Test, c)) );
  __ swc1(f10, MemOperand(a0, offsetof(Test, d)) );
3175 3176 3177 3178 3179 3180 3181 3182
  __ jr(ra);
  __ nop();
  Test test;
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
3183
  for (int i = 0; i < kTableLength; i++) {
3184 3185
    test.a = inputs_D[i];
    test.b = inputs_S[i];
3186
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3187 3188 3189 3190 3191
    if ((test.isNaN2008 & kFCSRNaN2008FlagMask) && kArchVariant == kMips64r6) {
      CHECK_EQ(test.c, outputsNaN2008[i]);
    } else {
      CHECK_EQ(test.c, outputs[i]);
    }
3192 3193 3194 3195 3196 3197 3198 3199 3200
    CHECK_EQ(test.d, test.c);
  }
}


TEST(ceil_l) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
3201 3202
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
3203 3204
    const double dFPU64InvalidResult = static_cast<double>(kFPU64InvalidResult);
    typedef struct test_float {
3205
      uint32_t isNaN2008;
3206 3207 3208 3209 3210
      double a;
      float b;
      int64_t c;
      int64_t d;
    }Test;
3211 3212
    const int kTableLength = 15;
    double inputs_D[kTableLength] = {
3213 3214 3215 3216 3217 3218
        2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
        -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
        2147483648.0,
        std::numeric_limits<double>::quiet_NaN(),
        std::numeric_limits<double>::infinity()
        };
3219
    float inputs_S[kTableLength] = {
3220 3221 3222 3223 3224 3225
        2.1, 2.6, 2.5, 3.1, 3.6, 3.5,
        -2.1, -2.6, -2.5, -3.1, -3.6, -3.5,
        2147483648.0,
        std::numeric_limits<float>::quiet_NaN(),
        std::numeric_limits<float>::infinity()
        };
3226
    double outputs[kTableLength] = {
3227 3228 3229 3230
        3.0, 3.0, 3.0, 4.0, 4.0, 4.0,
        -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
        2147483648.0, dFPU64InvalidResult,
        dFPU64InvalidResult};
3231 3232 3233 3234 3235 3236
    double outputsNaN2008[kTableLength] = {
        3.0, 3.0, 3.0, 4.0, 4.0, 4.0,
        -2.0, -2.0, -2.0, -3.0, -3.0, -3.0,
        2147483648.0,
        0,
        dFPU64InvalidResult};
3237

3238 3239
    __ cfc1(t1, FCSR);
    __ sw(t1, MemOperand(a0, offsetof(Test, isNaN2008)));
3240 3241
    __ ldc1(f4, MemOperand(a0, offsetof(Test, a)) );
    __ lwc1(f6, MemOperand(a0, offsetof(Test, b)) );
3242 3243
    __ ceil_l_d(f8, f4);
    __ ceil_l_s(f10, f6);
3244 3245
    __ sdc1(f8, MemOperand(a0, offsetof(Test, c)) );
    __ sdc1(f10, MemOperand(a0, offsetof(Test, d)) );
3246 3247 3248 3249 3250 3251 3252 3253
    __ jr(ra);
    __ nop();
    Test test;
    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
3254
    for (int i = 0; i < kTableLength; i++) {
3255 3256
      test.a = inputs_D[i];
      test.b = inputs_S[i];
3257
      (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3258 3259 3260 3261 3262 3263
      if ((test.isNaN2008 & kFCSRNaN2008FlagMask) &&
              kArchVariant == kMips64r6) {
        CHECK_EQ(test.c, outputsNaN2008[i]);
      } else {
        CHECK_EQ(test.c, outputs[i]);
      }
3264 3265 3266
      CHECK_EQ(test.d, test.c);
    }
}
3267 3268


3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
TEST(jump_tables1) {
  // Test jump tables with forward jumps.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  Assembler assm(isolate, nullptr, 0);

  const int kNumCases = 512;
  int values[kNumCases];
  isolate->random_number_generator()->NextBytes(values, sizeof(values));
  Label labels[kNumCases];

  __ daddiu(sp, sp, -8);
  __ sd(ra, MemOperand(sp));
3283
  __ Align(8);
3284 3285 3286

  Label done;
  {
3287
    __ BlockTrampolinePoolFor(kNumCases * 2 + 6);
3288
    PredictableCodeSizeScope predictable(
3289
        &assm, (kNumCases * 2 + 6) * Assembler::kInstrSize);
3290 3291 3292
    Label here;

    __ bal(&here);
3293
    __ dsll(at, a0, 3);  // In delay slot.
3294 3295
    __ bind(&here);
    __ daddu(at, at, ra);
3296
    __ ld(at, MemOperand(at, 4 * Assembler::kInstrSize));
3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
    __ jr(at);
    __ nop();
    for (int i = 0; i < kNumCases; ++i) {
      __ dd(&labels[i]);
    }
  }

  for (int i = 0; i < kNumCases; ++i) {
    __ bind(&labels[i]);
    __ lui(v0, (values[i] >> 16) & 0xffff);
    __ ori(v0, v0, values[i] & 0xffff);
    __ b(&done);
    __ nop();
  }

  __ bind(&done);
  __ ld(ra, MemOperand(sp));
  __ daddiu(sp, sp, 8);
  __ jr(ra);
  __ nop();

3318 3319
  CHECK_EQ(assm.UnboundLabelsCount(), 0);

3320 3321 3322 3323 3324 3325 3326 3327 3328
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F1 f = FUNCTION_CAST<F1>(code->entry());
  for (int i = 0; i < kNumCases; ++i) {
3329
    int64_t res = reinterpret_cast<int64_t>(
3330
        CALL_GENERATED_CODE(isolate, f, i, 0, 0, 0, 0));
3331
    ::printf("f(%d) = %" PRId64 "\n", i, res);
3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363
    CHECK_EQ(values[i], static_cast<int>(res));
  }
}


TEST(jump_tables2) {
  // Test jump tables with backward jumps.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  Assembler assm(isolate, nullptr, 0);

  const int kNumCases = 512;
  int values[kNumCases];
  isolate->random_number_generator()->NextBytes(values, sizeof(values));
  Label labels[kNumCases];

  __ daddiu(sp, sp, -8);
  __ sd(ra, MemOperand(sp));

  Label done, dispatch;
  __ b(&dispatch);
  __ nop();

  for (int i = 0; i < kNumCases; ++i) {
    __ bind(&labels[i]);
    __ lui(v0, (values[i] >> 16) & 0xffff);
    __ ori(v0, v0, values[i] & 0xffff);
    __ b(&done);
    __ nop();
  }

3364
  __ Align(8);
3365 3366
  __ bind(&dispatch);
  {
3367
    __ BlockTrampolinePoolFor(kNumCases * 2 + 6);
3368
    PredictableCodeSizeScope predictable(
3369
        &assm, (kNumCases * 2 + 6) * Assembler::kInstrSize);
3370 3371 3372
    Label here;

    __ bal(&here);
3373
    __ dsll(at, a0, 3);  // In delay slot.
3374 3375
    __ bind(&here);
    __ daddu(at, at, ra);
3376
    __ ld(at, MemOperand(at, 4 * Assembler::kInstrSize));
3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398
    __ jr(at);
    __ nop();
    for (int i = 0; i < kNumCases; ++i) {
      __ dd(&labels[i]);
    }
  }

  __ bind(&done);
  __ ld(ra, MemOperand(sp));
  __ daddiu(sp, sp, 8);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
  code->Print(std::cout);
#endif
  F1 f = FUNCTION_CAST<F1>(code->entry());
  for (int i = 0; i < kNumCases; ++i) {
3399
    int64_t res = reinterpret_cast<int64_t>(
3400
        CALL_GENERATED_CODE(isolate, f, i, 0, 0, 0, 0));
3401
    ::printf("f(%d) = %" PRId64 "\n", i, res);
3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
    CHECK_EQ(values[i], res);
  }
}


TEST(jump_tables3) {
  // Test jump tables with backward jumps and embedded heap objects.
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  Assembler assm(isolate, nullptr, 0);

  const int kNumCases = 512;
  Handle<Object> values[kNumCases];
  for (int i = 0; i < kNumCases; ++i) {
    double value = isolate->random_number_generator()->NextDouble();
    values[i] = isolate->factory()->NewHeapNumber(value, IMMUTABLE, TENURED);
  }
  Label labels[kNumCases];
  Object* obj;
  int64_t imm64;

  __ daddiu(sp, sp, -8);
  __ sd(ra, MemOperand(sp));

  Label done, dispatch;
  __ b(&dispatch);
3429
  __ nop();
3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443


  for (int i = 0; i < kNumCases; ++i) {
    __ bind(&labels[i]);
    obj = *values[i];
    imm64 = reinterpret_cast<intptr_t>(obj);
    __ lui(v0, (imm64 >> 32) & kImm16Mask);
    __ ori(v0, v0, (imm64 >> 16) & kImm16Mask);
    __ dsll(v0, v0, 16);
    __ ori(v0, v0, imm64 & kImm16Mask);
    __ b(&done);
    __ nop();
  }

3444
  __ Align(8);
3445 3446
  __ bind(&dispatch);
  {
3447
    __ BlockTrampolinePoolFor(kNumCases * 2 + 6);
3448
    PredictableCodeSizeScope predictable(
3449
        &assm, (kNumCases * 2 + 6) * Assembler::kInstrSize);
3450 3451 3452
    Label here;

    __ bal(&here);
3453
    __ dsll(at, a0, 3);  // In delay slot.
3454 3455
    __ bind(&here);
    __ daddu(at, at, ra);
3456
    __ ld(at, MemOperand(at, 4 * Assembler::kInstrSize));
3457 3458 3459 3460 3461 3462 3463 3464 3465
    __ jr(at);
    __ nop();
    for (int i = 0; i < kNumCases; ++i) {
      __ dd(&labels[i]);
    }
  }

  __ bind(&done);
  __ ld(ra, MemOperand(sp));
3466
  __ daddiu(sp, sp, 8);
3467 3468 3469 3470 3471 3472 3473 3474
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
#ifdef OBJECT_PRINT
3475
  code->Print(std::cout);
3476 3477 3478
#endif
  F1 f = FUNCTION_CAST<F1>(code->entry());
  for (int i = 0; i < kNumCases; ++i) {
3479 3480
    Handle<Object> result(
        CALL_GENERATED_CODE(isolate, f, i, 0, 0, 0, 0), isolate);
3481 3482 3483 3484 3485 3486 3487 3488 3489 3490
#ifdef OBJECT_PRINT
    ::printf("f(%d) = ", i);
    result->Print(std::cout);
    ::printf("\n");
#endif
    CHECK(values[i].is_identical_to(result));
  }
}


3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509
TEST(BITSWAP) {
  // Test BITSWAP
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);

    typedef struct {
      int64_t r1;
      int64_t r2;
      int64_t r3;
      int64_t r4;
      int64_t r5;
      int64_t r6;
    } T;
    T t;

    Assembler assm(isolate, NULL, 0);

3510
    __ ld(a4, MemOperand(a0, offsetof(T, r1)));
3511 3512
    __ nop();
    __ bitswap(a6, a4);
3513
    __ sd(a6, MemOperand(a0, offsetof(T, r1)));
3514

3515
    __ ld(a4, MemOperand(a0, offsetof(T, r2)));
3516 3517
    __ nop();
    __ bitswap(a6, a4);
3518
    __ sd(a6, MemOperand(a0, offsetof(T, r2)));
3519

3520
    __ ld(a4, MemOperand(a0, offsetof(T, r3)));
3521 3522
    __ nop();
    __ bitswap(a6, a4);
3523
    __ sd(a6, MemOperand(a0, offsetof(T, r3)));
3524

3525
    __ ld(a4, MemOperand(a0, offsetof(T, r4)));
3526 3527
    __ nop();
    __ bitswap(a6, a4);
3528
    __ sd(a6, MemOperand(a0, offsetof(T, r4)));
3529

3530
    __ ld(a4, MemOperand(a0, offsetof(T, r5)));
3531 3532
    __ nop();
    __ dbitswap(a6, a4);
3533
    __ sd(a6, MemOperand(a0, offsetof(T, r5)));
3534

3535
    __ ld(a4, MemOperand(a0, offsetof(T, r6)));
3536 3537
    __ nop();
    __ dbitswap(a6, a4);
3538
    __ sd(a6, MemOperand(a0, offsetof(T, r6)));
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553

    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
    t.r1 = 0x00102100781A15C3;
    t.r2 = 0x001021008B71FCDE;
    t.r3 = 0xFF8017FF781A15C3;
    t.r4 = 0xFF8017FF8B71FCDE;
    t.r5 = 0x10C021098B71FCDE;
    t.r6 = 0xFB8017FF781A15C3;
3554
    Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598
    USE(dummy);

    CHECK_EQ(static_cast<int64_t>(0x000000001E58A8C3L), t.r1);
    CHECK_EQ(static_cast<int64_t>(0xFFFFFFFFD18E3F7BL), t.r2);
    CHECK_EQ(static_cast<int64_t>(0x000000001E58A8C3L), t.r3);
    CHECK_EQ(static_cast<int64_t>(0xFFFFFFFFD18E3F7BL), t.r4);
    CHECK_EQ(static_cast<int64_t>(0x08038490D18E3F7BL), t.r5);
    CHECK_EQ(static_cast<int64_t>(0xDF01E8FF1E58A8C3L), t.r6);
  }
}


TEST(class_fmt) {
  if (kArchVariant == kMips64r6) {
    // Test CLASS.fmt instruction.
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);

    typedef struct {
      double dSignalingNan;
      double dQuietNan;
      double dNegInf;
      double dNegNorm;
      double dNegSubnorm;
      double dNegZero;
      double dPosInf;
      double dPosNorm;
      double dPosSubnorm;
      double dPosZero;
      float  fSignalingNan;
      float  fQuietNan;
      float  fNegInf;
      float  fNegNorm;
      float  fNegSubnorm;
      float  fNegZero;
      float  fPosInf;
      float  fPosNorm;
      float  fPosSubnorm;
      float  fPosZero;  } T;
    T t;

    // Create a function that accepts &t, and loads, manipulates, and stores
    // the doubles t.a ... t.f.
3599 3600
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
3601

3602
    __ ldc1(f4, MemOperand(a0, offsetof(T, dSignalingNan)));
3603
    __ class_d(f6, f4);
3604
    __ sdc1(f6, MemOperand(a0, offsetof(T, dSignalingNan)));
3605

3606
    __ ldc1(f4, MemOperand(a0, offsetof(T, dQuietNan)));
3607
    __ class_d(f6, f4);
3608
    __ sdc1(f6, MemOperand(a0, offsetof(T, dQuietNan)));
3609

3610
    __ ldc1(f4, MemOperand(a0, offsetof(T, dNegInf)));
3611
    __ class_d(f6, f4);
3612
    __ sdc1(f6, MemOperand(a0, offsetof(T, dNegInf)));
3613

3614
    __ ldc1(f4, MemOperand(a0, offsetof(T, dNegNorm)));
3615
    __ class_d(f6, f4);
3616
    __ sdc1(f6, MemOperand(a0, offsetof(T, dNegNorm)));
3617

3618
    __ ldc1(f4, MemOperand(a0, offsetof(T, dNegSubnorm)));
3619
    __ class_d(f6, f4);
3620
    __ sdc1(f6, MemOperand(a0, offsetof(T, dNegSubnorm)));
3621

3622
    __ ldc1(f4, MemOperand(a0, offsetof(T, dNegZero)));
3623
    __ class_d(f6, f4);
3624
    __ sdc1(f6, MemOperand(a0, offsetof(T, dNegZero)));
3625

3626
    __ ldc1(f4, MemOperand(a0, offsetof(T, dPosInf)));
3627
    __ class_d(f6, f4);
3628
    __ sdc1(f6, MemOperand(a0, offsetof(T, dPosInf)));
3629

3630
    __ ldc1(f4, MemOperand(a0, offsetof(T, dPosNorm)));
3631
    __ class_d(f6, f4);
3632
    __ sdc1(f6, MemOperand(a0, offsetof(T, dPosNorm)));
3633

3634
    __ ldc1(f4, MemOperand(a0, offsetof(T, dPosSubnorm)));
3635
    __ class_d(f6, f4);
3636
    __ sdc1(f6, MemOperand(a0, offsetof(T, dPosSubnorm)));
3637

3638
    __ ldc1(f4, MemOperand(a0, offsetof(T, dPosZero)));
3639
    __ class_d(f6, f4);
3640
    __ sdc1(f6, MemOperand(a0, offsetof(T, dPosZero)));
3641 3642

    // Testing instruction CLASS.S
3643
    __ lwc1(f4, MemOperand(a0, offsetof(T, fSignalingNan)));
3644
    __ class_s(f6, f4);
3645
    __ swc1(f6, MemOperand(a0, offsetof(T, fSignalingNan)));
3646

3647
    __ lwc1(f4, MemOperand(a0, offsetof(T, fQuietNan)));
3648
    __ class_s(f6, f4);
3649
    __ swc1(f6, MemOperand(a0, offsetof(T, fQuietNan)));
3650

3651
    __ lwc1(f4, MemOperand(a0, offsetof(T, fNegInf)));
3652
    __ class_s(f6, f4);
3653
    __ swc1(f6, MemOperand(a0, offsetof(T, fNegInf)));
3654

3655
    __ lwc1(f4, MemOperand(a0, offsetof(T, fNegNorm)));
3656
    __ class_s(f6, f4);
3657
    __ swc1(f6, MemOperand(a0, offsetof(T, fNegNorm)));
3658

3659
    __ lwc1(f4, MemOperand(a0, offsetof(T, fNegSubnorm)));
3660
    __ class_s(f6, f4);
3661
    __ swc1(f6, MemOperand(a0, offsetof(T, fNegSubnorm)));
3662

3663
    __ lwc1(f4, MemOperand(a0, offsetof(T, fNegZero)));
3664
    __ class_s(f6, f4);
3665
    __ swc1(f6, MemOperand(a0, offsetof(T, fNegZero)));
3666

3667
    __ lwc1(f4, MemOperand(a0, offsetof(T, fPosInf)));
3668
    __ class_s(f6, f4);
3669
    __ swc1(f6, MemOperand(a0, offsetof(T, fPosInf)));
3670

3671
    __ lwc1(f4, MemOperand(a0, offsetof(T, fPosNorm)));
3672
    __ class_s(f6, f4);
3673
    __ swc1(f6, MemOperand(a0, offsetof(T, fPosNorm)));
3674

3675
    __ lwc1(f4, MemOperand(a0, offsetof(T, fPosSubnorm)));
3676
    __ class_s(f6, f4);
3677
    __ swc1(f6, MemOperand(a0, offsetof(T, fPosSubnorm)));
3678

3679
    __ lwc1(f4, MemOperand(a0, offsetof(T, fPosZero)));
3680
    __ class_s(f6, f4);
3681
    __ swc1(f6, MemOperand(a0, offsetof(T, fPosZero)));
3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715

    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());

    // Double test values.
    t.dSignalingNan =  std::numeric_limits<double>::signaling_NaN();
    t.dQuietNan = std::numeric_limits<double>::quiet_NaN();
    t.dNegInf       = -1.0 / 0.0;
    t.dNegNorm      = -5.0;
    t.dNegSubnorm   = -DBL_MIN / 2.0;
    t.dNegZero      = -0.0;
    t.dPosInf       = 2.0 / 0.0;
    t.dPosNorm      = 275.35;
    t.dPosSubnorm   = DBL_MIN / 2.0;
    t.dPosZero      = +0.0;
    // Float test values

    t.fSignalingNan = std::numeric_limits<float>::signaling_NaN();
    t.fQuietNan     = std::numeric_limits<float>::quiet_NaN();
    t.fNegInf       = -0.5/0.0;
    t.fNegNorm      = -FLT_MIN;
    t.fNegSubnorm   = -FLT_MIN / 1.5;
    t.fNegZero      = -0.0;
    t.fPosInf       = 100000.0 / 0.0;
    t.fPosNorm      = FLT_MAX;
    t.fPosSubnorm   = FLT_MIN / 20.0;
    t.fPosZero      = +0.0;

3716
    Object* dummy = CALL_GENERATED_CODE(isolate, f, &t, 0, 0, 0, 0);
3717 3718
    USE(dummy);
    // Expected double results.
3719 3720
    CHECK_EQ(bit_cast<int64_t>(t.dSignalingNan), 0x001);
    CHECK_EQ(bit_cast<int64_t>(t.dQuietNan),     0x002);
3721 3722 3723 3724 3725 3726 3727 3728
    CHECK_EQ(bit_cast<int64_t>(t.dNegInf),       0x004);
    CHECK_EQ(bit_cast<int64_t>(t.dNegNorm),      0x008);
    CHECK_EQ(bit_cast<int64_t>(t.dNegSubnorm),   0x010);
    CHECK_EQ(bit_cast<int64_t>(t.dNegZero),      0x020);
    CHECK_EQ(bit_cast<int64_t>(t.dPosInf),       0x040);
    CHECK_EQ(bit_cast<int64_t>(t.dPosNorm),      0x080);
    CHECK_EQ(bit_cast<int64_t>(t.dPosSubnorm),   0x100);
    CHECK_EQ(bit_cast<int64_t>(t.dPosZero),      0x200);
3729 3730

    // Expected float results.
3731 3732
    CHECK_EQ(bit_cast<int32_t>(t.fSignalingNan), 0x001);
    CHECK_EQ(bit_cast<int32_t>(t.fQuietNan),     0x002);
3733 3734 3735 3736 3737 3738 3739 3740
    CHECK_EQ(bit_cast<int32_t>(t.fNegInf),       0x004);
    CHECK_EQ(bit_cast<int32_t>(t.fNegNorm),      0x008);
    CHECK_EQ(bit_cast<int32_t>(t.fNegSubnorm),   0x010);
    CHECK_EQ(bit_cast<int32_t>(t.fNegZero),      0x020);
    CHECK_EQ(bit_cast<int32_t>(t.fPosInf),       0x040);
    CHECK_EQ(bit_cast<int32_t>(t.fPosNorm),      0x080);
    CHECK_EQ(bit_cast<int32_t>(t.fPosSubnorm),   0x100);
    CHECK_EQ(bit_cast<int32_t>(t.fPosZero),      0x200);
3741 3742 3743 3744 3745 3746 3747 3748
  }
}


TEST(ABS) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
3749
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761

  typedef struct test_float {
    int64_t fir;
    double a;
    float b;
    double fcsr;
  } TestFloat;

  TestFloat test;

  // Save FIR.
  __ cfc1(a1, FCSR);
3762
  __ sd(a1, MemOperand(a0, offsetof(TestFloat, fcsr)));
3763 3764 3765
  // Disable FPU exceptions.
  __ ctc1(zero_reg, FCSR);

3766
  __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, a)));
3767
  __ abs_d(f10, f4);
3768
  __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, a)));
3769

3770
  __ lwc1(f4, MemOperand(a0, offsetof(TestFloat, b)));
3771
  __ abs_s(f10, f4);
3772
  __ swc1(f10, MemOperand(a0, offsetof(TestFloat, b)));
3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786

  // Restore FCSR.
  __ ctc1(a1, FCSR);

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  test.a = -2.0;
  test.b = -2.0;
3787
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3788 3789 3790 3791 3792
  CHECK_EQ(test.a, 2.0);
  CHECK_EQ(test.b, 2.0);

  test.a = 2.0;
  test.b = 2.0;
3793
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3794 3795 3796 3797 3798 3799
  CHECK_EQ(test.a, 2.0);
  CHECK_EQ(test.b, 2.0);

  // Testing biggest positive number
  test.a = std::numeric_limits<double>::max();
  test.b = std::numeric_limits<float>::max();
3800
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3801 3802 3803 3804
  CHECK_EQ(test.a, std::numeric_limits<double>::max());
  CHECK_EQ(test.b, std::numeric_limits<float>::max());

  // Testing smallest negative number
3805 3806
  test.a = -std::numeric_limits<double>::max();  // lowest()
  test.b = -std::numeric_limits<float>::max();   // lowest()
3807
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3808 3809 3810 3811 3812 3813
  CHECK_EQ(test.a, std::numeric_limits<double>::max());
  CHECK_EQ(test.b, std::numeric_limits<float>::max());

  // Testing smallest positive number
  test.a = -std::numeric_limits<double>::min();
  test.b = -std::numeric_limits<float>::min();
3814
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3815 3816 3817 3818 3819 3820 3821 3822
  CHECK_EQ(test.a, std::numeric_limits<double>::min());
  CHECK_EQ(test.b, std::numeric_limits<float>::min());

  // Testing infinity
  test.a = -std::numeric_limits<double>::max()
          / std::numeric_limits<double>::min();
  test.b = -std::numeric_limits<float>::max()
          / std::numeric_limits<float>::min();
3823
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3824 3825 3826 3827 3828 3829 3830
  CHECK_EQ(test.a, std::numeric_limits<double>::max()
                 / std::numeric_limits<double>::min());
  CHECK_EQ(test.b, std::numeric_limits<float>::max()
                 / std::numeric_limits<float>::min());

  test.a = std::numeric_limits<double>::quiet_NaN();
  test.b = std::numeric_limits<float>::quiet_NaN();
3831
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3832 3833 3834 3835 3836
  CHECK_EQ(std::isnan(test.a), true);
  CHECK_EQ(std::isnan(test.b), true);

  test.a = std::numeric_limits<double>::signaling_NaN();
  test.b = std::numeric_limits<float>::signaling_NaN();
3837
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3838 3839 3840 3841 3842 3843 3844 3845 3846
  CHECK_EQ(std::isnan(test.a), true);
  CHECK_EQ(std::isnan(test.b), true);
}


TEST(ADD_FMT) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
3847
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859

  typedef struct test_float {
    double a;
    double b;
    double c;
    float fa;
    float fb;
    float fc;
  } TestFloat;

  TestFloat test;

3860 3861
  __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, a)));
  __ ldc1(f8, MemOperand(a0, offsetof(TestFloat, b)));
3862
  __ add_d(f10, f8, f4);
3863
  __ sdc1(f10, MemOperand(a0, offsetof(TestFloat, c)));
3864

3865 3866
  __ lwc1(f4, MemOperand(a0, offsetof(TestFloat, fa)));
  __ lwc1(f8, MemOperand(a0, offsetof(TestFloat, fb)));
3867
  __ add_s(f10, f8, f4);
3868
  __ swc1(f10, MemOperand(a0, offsetof(TestFloat, fc)));
3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());
  test.a = 2.0;
  test.b = 3.0;
  test.fa = 2.0;
  test.fb = 3.0;
3882
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3883 3884 3885 3886
  CHECK_EQ(test.c, 5.0);
  CHECK_EQ(test.fc, 5.0);

  test.a = std::numeric_limits<double>::max();
3887
  test.b = -std::numeric_limits<double>::max();  // lowest()
3888
  test.fa = std::numeric_limits<float>::max();
3889
  test.fb = -std::numeric_limits<float>::max();  // lowest()
3890
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3891 3892 3893 3894 3895 3896 3897
  CHECK_EQ(test.c, 0.0);
  CHECK_EQ(test.fc, 0.0);

  test.a = std::numeric_limits<double>::max();
  test.b = std::numeric_limits<double>::max();
  test.fa = std::numeric_limits<float>::max();
  test.fb = std::numeric_limits<float>::max();
3898
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3899 3900 3901 3902 3903 3904 3905
  CHECK_EQ(std::isfinite(test.c), false);
  CHECK_EQ(std::isfinite(test.fc), false);

  test.a = 5.0;
  test.b = std::numeric_limits<double>::signaling_NaN();
  test.fa = 5.0;
  test.fb = std::numeric_limits<float>::signaling_NaN();
3906
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
3907 3908 3909 3910 3911 3912 3913 3914 3915 3916
  CHECK_EQ(std::isnan(test.c), true);
  CHECK_EQ(std::isnan(test.fc), true);
}


TEST(C_COND_FMT) {
  if (kArchVariant == kMips64r2) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
3917 3918
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946

    typedef struct test_float {
      double dOp1;
      double dOp2;
      uint32_t dF;
      uint32_t dUn;
      uint32_t dEq;
      uint32_t dUeq;
      uint32_t dOlt;
      uint32_t dUlt;
      uint32_t dOle;
      uint32_t dUle;
      float fOp1;
      float fOp2;
      uint32_t fF;
      uint32_t fUn;
      uint32_t fEq;
      uint32_t fUeq;
      uint32_t fOlt;
      uint32_t fUlt;
      uint32_t fOle;
      uint32_t fUle;
    } TestFloat;

    TestFloat test;

    __ li(t1, 1);

3947 3948
    __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, dOp1)));
    __ ldc1(f6, MemOperand(a0, offsetof(TestFloat, dOp2)));
3949

3950 3951
    __ lwc1(f14, MemOperand(a0, offsetof(TestFloat, fOp1)));
    __ lwc1(f16, MemOperand(a0, offsetof(TestFloat, fOp2)));
3952 3953 3954 3955 3956 3957 3958

    __ mov(t2, zero_reg);
    __ mov(t3, zero_reg);
    __ c_d(F, f4, f6, 0);
    __ c_s(F, f14, f16, 2);
    __ movt(t2, t1, 0);
    __ movt(t3, t1, 2);
3959 3960
    __ sw(t2, MemOperand(a0, offsetof(TestFloat, dF)) );
    __ sw(t3, MemOperand(a0, offsetof(TestFloat, fF)) );
3961 3962 3963 3964 3965 3966 3967

    __ mov(t2, zero_reg);
    __ mov(t3, zero_reg);
    __ c_d(UN, f4, f6, 2);
    __ c_s(UN, f14, f16, 4);
    __ movt(t2, t1, 2);
    __ movt(t3, t1, 4);
3968 3969
    __ sw(t2, MemOperand(a0, offsetof(TestFloat, dUn)) );
    __ sw(t3, MemOperand(a0, offsetof(TestFloat, fUn)) );
3970 3971 3972 3973 3974 3975 3976

    __ mov(t2, zero_reg);
    __ mov(t3, zero_reg);
    __ c_d(EQ, f4, f6, 4);
    __ c_s(EQ, f14, f16, 6);
    __ movt(t2, t1, 4);
    __ movt(t3, t1, 6);
3977 3978
    __ sw(t2, MemOperand(a0, offsetof(TestFloat, dEq)) );
    __ sw(t3, MemOperand(a0, offsetof(TestFloat, fEq)) );
3979 3980 3981 3982 3983 3984 3985

    __ mov(t2, zero_reg);
    __ mov(t3, zero_reg);
    __ c_d(UEQ, f4, f6, 6);
    __ c_s(UEQ, f14, f16, 0);
    __ movt(t2, t1, 6);
    __ movt(t3, t1, 0);
3986 3987
    __ sw(t2, MemOperand(a0, offsetof(TestFloat, dUeq)) );
    __ sw(t3, MemOperand(a0, offsetof(TestFloat, fUeq)) );
3988 3989 3990 3991 3992 3993 3994

    __ mov(t2, zero_reg);
    __ mov(t3, zero_reg);
    __ c_d(OLT, f4, f6, 0);
    __ c_s(OLT, f14, f16, 2);
    __ movt(t2, t1, 0);
    __ movt(t3, t1, 2);
3995 3996
    __ sw(t2, MemOperand(a0, offsetof(TestFloat, dOlt)) );
    __ sw(t3, MemOperand(a0, offsetof(TestFloat, fOlt)) );
3997 3998 3999 4000 4001 4002 4003

    __ mov(t2, zero_reg);
    __ mov(t3, zero_reg);
    __ c_d(ULT, f4, f6, 2);
    __ c_s(ULT, f14, f16, 4);
    __ movt(t2, t1, 2);
    __ movt(t3, t1, 4);
4004 4005
    __ sw(t2, MemOperand(a0, offsetof(TestFloat, dUlt)) );
    __ sw(t3, MemOperand(a0, offsetof(TestFloat, fUlt)) );
4006 4007 4008 4009 4010 4011 4012

    __ mov(t2, zero_reg);
    __ mov(t3, zero_reg);
    __ c_d(OLE, f4, f6, 4);
    __ c_s(OLE, f14, f16, 6);
    __ movt(t2, t1, 4);
    __ movt(t3, t1, 6);
4013 4014
    __ sw(t2, MemOperand(a0, offsetof(TestFloat, dOle)) );
    __ sw(t3, MemOperand(a0, offsetof(TestFloat, fOle)) );
4015 4016 4017 4018 4019 4020 4021

    __ mov(t2, zero_reg);
    __ mov(t3, zero_reg);
    __ c_d(ULE, f4, f6, 6);
    __ c_s(ULE, f14, f16, 0);
    __ movt(t2, t1, 6);
    __ movt(t3, t1, 0);
4022 4023
    __ sw(t2, MemOperand(a0, offsetof(TestFloat, dUle)) );
    __ sw(t3, MemOperand(a0, offsetof(TestFloat, fUle)) );
4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036

    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
    test.dOp1 = 2.0;
    test.dOp2 = 3.0;
    test.fOp1 = 2.0;
    test.fOp2 = 3.0;
4037
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053
    CHECK_EQ(test.dF, 0U);
    CHECK_EQ(test.dUn, 0U);
    CHECK_EQ(test.dEq, 0U);
    CHECK_EQ(test.dUeq, 0U);
    CHECK_EQ(test.dOlt, 1U);
    CHECK_EQ(test.dUlt, 1U);
    CHECK_EQ(test.dOle, 1U);
    CHECK_EQ(test.dUle, 1U);
    CHECK_EQ(test.fF, 0U);
    CHECK_EQ(test.fUn, 0U);
    CHECK_EQ(test.fEq, 0U);
    CHECK_EQ(test.fUeq, 0U);
    CHECK_EQ(test.fOlt, 1U);
    CHECK_EQ(test.fUlt, 1U);
    CHECK_EQ(test.fOle, 1U);
    CHECK_EQ(test.fUle, 1U);
4054 4055 4056 4057

    test.dOp1 = std::numeric_limits<double>::max();
    test.dOp2 = std::numeric_limits<double>::min();
    test.fOp1 = std::numeric_limits<float>::min();
4058
    test.fOp2 = -std::numeric_limits<float>::max();  // lowest()
4059
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075
    CHECK_EQ(test.dF, 0U);
    CHECK_EQ(test.dUn, 0U);
    CHECK_EQ(test.dEq, 0U);
    CHECK_EQ(test.dUeq, 0U);
    CHECK_EQ(test.dOlt, 0U);
    CHECK_EQ(test.dUlt, 0U);
    CHECK_EQ(test.dOle, 0U);
    CHECK_EQ(test.dUle, 0U);
    CHECK_EQ(test.fF, 0U);
    CHECK_EQ(test.fUn, 0U);
    CHECK_EQ(test.fEq, 0U);
    CHECK_EQ(test.fUeq, 0U);
    CHECK_EQ(test.fOlt, 0U);
    CHECK_EQ(test.fUlt, 0U);
    CHECK_EQ(test.fOle, 0U);
    CHECK_EQ(test.fUle, 0U);
4076

4077 4078
    test.dOp1 = -std::numeric_limits<double>::max();  // lowest()
    test.dOp2 = -std::numeric_limits<double>::max();  // lowest()
4079 4080
    test.fOp1 = std::numeric_limits<float>::max();
    test.fOp2 = std::numeric_limits<float>::max();
4081
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097
    CHECK_EQ(test.dF, 0U);
    CHECK_EQ(test.dUn, 0U);
    CHECK_EQ(test.dEq, 1U);
    CHECK_EQ(test.dUeq, 1U);
    CHECK_EQ(test.dOlt, 0U);
    CHECK_EQ(test.dUlt, 0U);
    CHECK_EQ(test.dOle, 1U);
    CHECK_EQ(test.dUle, 1U);
    CHECK_EQ(test.fF, 0U);
    CHECK_EQ(test.fUn, 0U);
    CHECK_EQ(test.fEq, 1U);
    CHECK_EQ(test.fUeq, 1U);
    CHECK_EQ(test.fOlt, 0U);
    CHECK_EQ(test.fUlt, 0U);
    CHECK_EQ(test.fOle, 1U);
    CHECK_EQ(test.fUle, 1U);
4098 4099 4100 4101 4102

    test.dOp1 = std::numeric_limits<double>::quiet_NaN();
    test.dOp2 = 0.0;
    test.fOp1 = std::numeric_limits<float>::quiet_NaN();
    test.fOp2 = 0.0;
4103
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119
    CHECK_EQ(test.dF, 0U);
    CHECK_EQ(test.dUn, 1U);
    CHECK_EQ(test.dEq, 0U);
    CHECK_EQ(test.dUeq, 1U);
    CHECK_EQ(test.dOlt, 0U);
    CHECK_EQ(test.dUlt, 1U);
    CHECK_EQ(test.dOle, 0U);
    CHECK_EQ(test.dUle, 1U);
    CHECK_EQ(test.fF, 0U);
    CHECK_EQ(test.fUn, 1U);
    CHECK_EQ(test.fEq, 0U);
    CHECK_EQ(test.fUeq, 1U);
    CHECK_EQ(test.fOlt, 0U);
    CHECK_EQ(test.fUlt, 1U);
    CHECK_EQ(test.fOle, 0U);
    CHECK_EQ(test.fUle, 1U);
4120 4121 4122 4123 4124 4125 4126 4127 4128
  }
}


TEST(CMP_COND_FMT) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();
    Isolate* isolate = CcTest::i_isolate();
    HandleScope scope(isolate);
4129 4130
    MacroAssembler assm(isolate, NULL, 0,
                        v8::internal::CodeObjectRequired::kYes);
4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164

    typedef struct test_float {
      double dOp1;
      double dOp2;
      double dF;
      double dUn;
      double dEq;
      double dUeq;
      double dOlt;
      double dUlt;
      double dOle;
      double dUle;
      double dOr;
      double dUne;
      double dNe;
      float fOp1;
      float fOp2;
      float fF;
      float fUn;
      float fEq;
      float fUeq;
      float fOlt;
      float fUlt;
      float fOle;
      float fUle;
      float fOr;
      float fUne;
      float fNe;
    } TestFloat;

    TestFloat test;

    __ li(t1, 1);

4165 4166
    __ ldc1(f4, MemOperand(a0, offsetof(TestFloat, dOp1)));
    __ ldc1(f6, MemOperand(a0, offsetof(TestFloat, dOp2)));
4167

4168 4169
    __ lwc1(f14, MemOperand(a0, offsetof(TestFloat, fOp1)));
    __ lwc1(f16, MemOperand(a0, offsetof(TestFloat, fOp2)));
4170 4171 4172

    __ cmp_d(F, f2, f4, f6);
    __ cmp_s(F, f12, f14, f16);
4173 4174
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dF)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fF)) );
4175 4176 4177

    __ cmp_d(UN, f2, f4, f6);
    __ cmp_s(UN, f12, f14, f16);
4178 4179
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dUn)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fUn)) );
4180 4181 4182

    __ cmp_d(EQ, f2, f4, f6);
    __ cmp_s(EQ, f12, f14, f16);
4183 4184
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dEq)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fEq)) );
4185 4186 4187

    __ cmp_d(UEQ, f2, f4, f6);
    __ cmp_s(UEQ, f12, f14, f16);
4188 4189
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dUeq)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fUeq)) );
4190 4191 4192

    __ cmp_d(LT, f2, f4, f6);
    __ cmp_s(LT, f12, f14, f16);
4193 4194
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dOlt)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fOlt)) );
4195 4196 4197

    __ cmp_d(ULT, f2, f4, f6);
    __ cmp_s(ULT, f12, f14, f16);
4198 4199
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dUlt)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fUlt)) );
4200 4201 4202

    __ cmp_d(LE, f2, f4, f6);
    __ cmp_s(LE, f12, f14, f16);
4203 4204
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dOle)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fOle)) );
4205 4206 4207

    __ cmp_d(ULE, f2, f4, f6);
    __ cmp_s(ULE, f12, f14, f16);
4208 4209
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dUle)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fUle)) );
4210 4211 4212

    __ cmp_d(ORD, f2, f4, f6);
    __ cmp_s(ORD, f12, f14, f16);
4213 4214
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dOr)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fOr)) );
4215 4216 4217

    __ cmp_d(UNE, f2, f4, f6);
    __ cmp_s(UNE, f12, f14, f16);
4218 4219
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dUne)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fUne)) );
4220 4221 4222

    __ cmp_d(NE, f2, f4, f6);
    __ cmp_s(NE, f12, f14, f16);
4223 4224
    __ sdc1(f2, MemOperand(a0, offsetof(TestFloat, dNe)) );
    __ swc1(f12, MemOperand(a0, offsetof(TestFloat, fNe)) );
4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242

    __ jr(ra);
    __ nop();

    CodeDesc desc;
    assm.GetCode(&desc);
    Handle<Code> code = isolate->factory()->NewCode(
        desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    F3 f = FUNCTION_CAST<F3>(code->entry());
    uint64_t dTrue  = 0xFFFFFFFFFFFFFFFF;
    uint64_t dFalse = 0x0000000000000000;
    uint32_t fTrue  = 0xFFFFFFFF;
    uint32_t fFalse = 0x00000000;

    test.dOp1 = 2.0;
    test.dOp2 = 3.0;
    test.fOp1 = 2.0;
    test.fOp2 = 3.0;
4243
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266
    CHECK_EQ(bit_cast<uint64_t>(test.dF), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUn), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dEq), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUeq), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dOlt), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dUlt), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dOle), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dUle), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dOr), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dUne), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dNe), dTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fF), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUn), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fEq), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUeq), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fOlt), fTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fUlt), fTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fOle), fTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fUle), fTrue);

    test.dOp1 = std::numeric_limits<double>::max();
    test.dOp2 = std::numeric_limits<double>::min();
    test.fOp1 = std::numeric_limits<float>::min();
4267
    test.fOp2 = -std::numeric_limits<float>::max();  // lowest()
4268
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288
    CHECK_EQ(bit_cast<uint64_t>(test.dF), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUn), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dEq), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUeq), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dOlt), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUlt), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dOle), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUle), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dOr), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dUne), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dNe), dTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fF), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUn), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fEq), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUeq), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fOlt), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUlt), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fOle), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUle), fFalse);

4289 4290
    test.dOp1 = -std::numeric_limits<double>::max();  // lowest()
    test.dOp2 = -std::numeric_limits<double>::max();  // lowest()
4291 4292
    test.fOp1 = std::numeric_limits<float>::max();
    test.fOp2 = std::numeric_limits<float>::max();
4293
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317
    CHECK_EQ(bit_cast<uint64_t>(test.dF), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUn), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dEq), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dUeq), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dOlt), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUlt), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dOle), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dUle), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dOr), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dUne), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dNe), dFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fF), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUn), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fEq), fTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fUeq), fTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fOlt), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUlt), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fOle), fTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fUle), fTrue);

    test.dOp1 = std::numeric_limits<double>::quiet_NaN();
    test.dOp2 = 0.0;
    test.fOp1 = std::numeric_limits<float>::quiet_NaN();
    test.fOp2 = 0.0;
4318
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345
    CHECK_EQ(bit_cast<uint64_t>(test.dF), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUn), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dEq), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUeq), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dOlt), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUlt), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dOle), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUle), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dOr), dFalse);
    CHECK_EQ(bit_cast<uint64_t>(test.dUne), dTrue);
    CHECK_EQ(bit_cast<uint64_t>(test.dNe), dFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fF), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUn), fTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fEq), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUeq), fTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fOlt), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUlt), fTrue);
    CHECK_EQ(bit_cast<uint32_t>(test.fOle), fFalse);
    CHECK_EQ(bit_cast<uint32_t>(test.fUle), fTrue);
  }
}


TEST(CVT) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
4346
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381

  typedef struct test_float {
    float    cvt_d_s_in;
    double   cvt_d_s_out;
    int32_t  cvt_d_w_in;
    double   cvt_d_w_out;
    int64_t  cvt_d_l_in;
    double   cvt_d_l_out;

    float    cvt_l_s_in;
    int64_t  cvt_l_s_out;
    double   cvt_l_d_in;
    int64_t  cvt_l_d_out;

    double   cvt_s_d_in;
    float    cvt_s_d_out;
    int32_t  cvt_s_w_in;
    float    cvt_s_w_out;
    int64_t  cvt_s_l_in;
    float    cvt_s_l_out;

    float    cvt_w_s_in;
    int32_t  cvt_w_s_out;
    double   cvt_w_d_in;
    int32_t  cvt_w_d_out;
  } TestFloat;

  TestFloat test;

  // Save FCSR.
  __ cfc1(a1, FCSR);
  // Disable FPU exceptions.
  __ ctc1(zero_reg, FCSR);

#define GENERATE_CVT_TEST(x, y, z) \
4382
  __ y##c1(f0, MemOperand(a0, offsetof(TestFloat, x##_in))); \
4383 4384
  __ x(f0, f0); \
  __ nop(); \
4385
  __ z##c1(f0, MemOperand(a0, offsetof(TestFloat, x##_out)));
4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423

  GENERATE_CVT_TEST(cvt_d_s, lw, sd)
  GENERATE_CVT_TEST(cvt_d_w, lw, sd)
  GENERATE_CVT_TEST(cvt_d_l, ld, sd)

  GENERATE_CVT_TEST(cvt_l_s, lw, sd)
  GENERATE_CVT_TEST(cvt_l_d, ld, sd)

  GENERATE_CVT_TEST(cvt_s_d, ld, sw)
  GENERATE_CVT_TEST(cvt_s_w, lw, sw)
  GENERATE_CVT_TEST(cvt_s_l, ld, sw)

  GENERATE_CVT_TEST(cvt_w_s, lw, sw)
  GENERATE_CVT_TEST(cvt_w_d, ld, sw)

  // Restore FCSR.
  __ ctc1(a1, FCSR);

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());

  test.cvt_d_s_in = -0.51;
  test.cvt_d_w_in = -1;
  test.cvt_d_l_in = -1;
  test.cvt_l_s_in = -0.51;
  test.cvt_l_d_in = -0.51;
  test.cvt_s_d_in = -0.51;
  test.cvt_s_w_in = -1;
  test.cvt_s_l_in = -1;
  test.cvt_w_s_in = -0.51;
  test.cvt_w_d_in = -0.51;

4424
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447
  CHECK_EQ(test.cvt_d_s_out, static_cast<double>(test.cvt_d_s_in));
  CHECK_EQ(test.cvt_d_w_out, static_cast<double>(test.cvt_d_w_in));
  CHECK_EQ(test.cvt_d_l_out, static_cast<double>(test.cvt_d_l_in));
  CHECK_EQ(test.cvt_l_s_out, -1);
  CHECK_EQ(test.cvt_l_d_out, -1);
  CHECK_EQ(test.cvt_s_d_out, static_cast<float>(test.cvt_s_d_in));
  CHECK_EQ(test.cvt_s_w_out, static_cast<float>(test.cvt_s_w_in));
  CHECK_EQ(test.cvt_s_l_out, static_cast<float>(test.cvt_s_l_in));
  CHECK_EQ(test.cvt_w_s_out, -1);
  CHECK_EQ(test.cvt_w_d_out, -1);


  test.cvt_d_s_in = 0.49;
  test.cvt_d_w_in = 1;
  test.cvt_d_l_in = 1;
  test.cvt_l_s_in = 0.49;
  test.cvt_l_d_in = 0.49;
  test.cvt_s_d_in = 0.49;
  test.cvt_s_w_in = 1;
  test.cvt_s_l_in = 1;
  test.cvt_w_s_in = 0.49;
  test.cvt_w_d_in = 0.49;

4448
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470
  CHECK_EQ(test.cvt_d_s_out, static_cast<double>(test.cvt_d_s_in));
  CHECK_EQ(test.cvt_d_w_out, static_cast<double>(test.cvt_d_w_in));
  CHECK_EQ(test.cvt_d_l_out, static_cast<double>(test.cvt_d_l_in));
  CHECK_EQ(test.cvt_l_s_out, 0);
  CHECK_EQ(test.cvt_l_d_out, 0);
  CHECK_EQ(test.cvt_s_d_out, static_cast<float>(test.cvt_s_d_in));
  CHECK_EQ(test.cvt_s_w_out, static_cast<float>(test.cvt_s_w_in));
  CHECK_EQ(test.cvt_s_l_out, static_cast<float>(test.cvt_s_l_in));
  CHECK_EQ(test.cvt_w_s_out, 0);
  CHECK_EQ(test.cvt_w_d_out, 0);

  test.cvt_d_s_in = std::numeric_limits<float>::max();
  test.cvt_d_w_in = std::numeric_limits<int32_t>::max();
  test.cvt_d_l_in = std::numeric_limits<int64_t>::max();
  test.cvt_l_s_in = std::numeric_limits<float>::max();
  test.cvt_l_d_in = std::numeric_limits<double>::max();
  test.cvt_s_d_in = std::numeric_limits<double>::max();
  test.cvt_s_w_in = std::numeric_limits<int32_t>::max();
  test.cvt_s_l_in = std::numeric_limits<int64_t>::max();
  test.cvt_w_s_in = std::numeric_limits<float>::max();
  test.cvt_w_d_in = std::numeric_limits<double>::max();

4471
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483
  CHECK_EQ(test.cvt_d_s_out, static_cast<double>(test.cvt_d_s_in));
  CHECK_EQ(test.cvt_d_w_out, static_cast<double>(test.cvt_d_w_in));
  CHECK_EQ(test.cvt_d_l_out, static_cast<double>(test.cvt_d_l_in));
  CHECK_EQ(test.cvt_l_s_out, std::numeric_limits<int64_t>::max());
  CHECK_EQ(test.cvt_l_d_out, std::numeric_limits<int64_t>::max());
  CHECK_EQ(test.cvt_s_d_out, static_cast<float>(test.cvt_s_d_in));
  CHECK_EQ(test.cvt_s_w_out, static_cast<float>(test.cvt_s_w_in));
  CHECK_EQ(test.cvt_s_l_out, static_cast<float>(test.cvt_s_l_in));
  CHECK_EQ(test.cvt_w_s_out, std::numeric_limits<int32_t>::max());
  CHECK_EQ(test.cvt_w_d_out, std::numeric_limits<int32_t>::max());


4484 4485 4486 4487 4488 4489 4490 4491 4492 4493
  test.cvt_d_s_in = -std::numeric_limits<float>::max();   // lowest()
  test.cvt_d_w_in = std::numeric_limits<int32_t>::min();  // lowest()
  test.cvt_d_l_in = std::numeric_limits<int64_t>::min();  // lowest()
  test.cvt_l_s_in = -std::numeric_limits<float>::max();   // lowest()
  test.cvt_l_d_in = -std::numeric_limits<double>::max();  // lowest()
  test.cvt_s_d_in = -std::numeric_limits<double>::max();  // lowest()
  test.cvt_s_w_in = std::numeric_limits<int32_t>::min();  // lowest()
  test.cvt_s_l_in = std::numeric_limits<int64_t>::min();  // lowest()
  test.cvt_w_s_in = -std::numeric_limits<float>::max();   // lowest()
  test.cvt_w_d_in = -std::numeric_limits<double>::max();  // lowest()
4494

4495
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
  CHECK_EQ(test.cvt_d_s_out, static_cast<double>(test.cvt_d_s_in));
  CHECK_EQ(test.cvt_d_w_out, static_cast<double>(test.cvt_d_w_in));
  CHECK_EQ(test.cvt_d_l_out, static_cast<double>(test.cvt_d_l_in));
  // The returned value when converting from fixed-point to float-point
  // is not consistent between board, simulator and specification
  // in this test case, therefore modifying the test
  CHECK(test.cvt_l_s_out == std::numeric_limits<int64_t>::min() ||
       test.cvt_l_s_out == std::numeric_limits<int64_t>::max());
  CHECK(test.cvt_l_d_out == std::numeric_limits<int64_t>::min() ||
        test.cvt_l_d_out == std::numeric_limits<int64_t>::max());
  CHECK_EQ(test.cvt_s_d_out, static_cast<float>(test.cvt_s_d_in));
  CHECK_EQ(test.cvt_s_w_out, static_cast<float>(test.cvt_s_w_in));
  CHECK_EQ(test.cvt_s_l_out, static_cast<float>(test.cvt_s_l_in));
  CHECK(test.cvt_w_s_out == std::numeric_limits<int32_t>::min() ||
        test.cvt_w_s_out == std::numeric_limits<int32_t>::max());
  CHECK(test.cvt_w_d_out == std::numeric_limits<int32_t>::min() ||
        test.cvt_w_d_out == std::numeric_limits<int32_t>::max());


  test.cvt_d_s_in = std::numeric_limits<float>::min();
  test.cvt_d_w_in = std::numeric_limits<int32_t>::min();
  test.cvt_d_l_in = std::numeric_limits<int64_t>::min();
  test.cvt_l_s_in = std::numeric_limits<float>::min();
  test.cvt_l_d_in = std::numeric_limits<double>::min();
  test.cvt_s_d_in = std::numeric_limits<double>::min();
  test.cvt_s_w_in = std::numeric_limits<int32_t>::min();
  test.cvt_s_l_in = std::numeric_limits<int64_t>::min();
  test.cvt_w_s_in = std::numeric_limits<float>::min();
  test.cvt_w_d_in = std::numeric_limits<double>::min();

4526
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543
  CHECK_EQ(test.cvt_d_s_out, static_cast<double>(test.cvt_d_s_in));
  CHECK_EQ(test.cvt_d_w_out, static_cast<double>(test.cvt_d_w_in));
  CHECK_EQ(test.cvt_d_l_out, static_cast<double>(test.cvt_d_l_in));
  CHECK_EQ(test.cvt_l_s_out, 0);
  CHECK_EQ(test.cvt_l_d_out, 0);
  CHECK_EQ(test.cvt_s_d_out, static_cast<float>(test.cvt_s_d_in));
  CHECK_EQ(test.cvt_s_w_out, static_cast<float>(test.cvt_s_w_in));
  CHECK_EQ(test.cvt_s_l_out, static_cast<float>(test.cvt_s_l_in));
  CHECK_EQ(test.cvt_w_s_out, 0);
  CHECK_EQ(test.cvt_w_d_out, 0);
}


TEST(DIV_FMT) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
4544
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561

  typedef struct test {
    double dOp1;
    double dOp2;
    double dRes;
    float  fOp1;
    float  fOp2;
    float  fRes;
  } Test;

  Test test;

  // Save FCSR.
  __ cfc1(a1, FCSR);
  // Disable FPU exceptions.
  __ ctc1(zero_reg, FCSR);

4562 4563
  __ ldc1(f4, MemOperand(a0, offsetof(Test, dOp1)) );
  __ ldc1(f2, MemOperand(a0, offsetof(Test, dOp2)) );
4564 4565
  __ nop();
  __ div_d(f6, f4, f2);
4566
  __ sdc1(f6, MemOperand(a0, offsetof(Test, dRes)) );
4567

4568 4569
  __ lwc1(f4, MemOperand(a0, offsetof(Test, fOp1)) );
  __ lwc1(f2, MemOperand(a0, offsetof(Test, fOp2)) );
4570 4571
  __ nop();
  __ div_s(f6, f4, f2);
4572
  __ swc1(f6, MemOperand(a0, offsetof(Test, fRes)) );
4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584

    // Restore FCSR.
  __ ctc1(a1, FCSR);

  __ jr(ra);
  __ nop();
  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F3 f = FUNCTION_CAST<F3>(code->entry());

4585
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625

  const int test_size = 3;

  double dOp1[test_size] = {
    5.0,
    DBL_MAX,
    DBL_MAX,
  };
  double dOp2[test_size] = {
    2.0,
    2.0,
    -DBL_MAX,
  };
  double dRes[test_size] = {
    2.5,
    DBL_MAX / 2.0,
    -1.0,
  };
  float fOp1[test_size] = {
    5.0,
    FLT_MAX,
    FLT_MAX,
  };
  float fOp2[test_size] = {
    2.0,
    2.0,
    -FLT_MAX,
  };
  float fRes[test_size] = {
    2.5,
    FLT_MAX / 2.0,
    -1.0,
  };

  for (int i = 0; i < test_size; i++) {
    test.dOp1 = dOp1[i];
    test.dOp2 = dOp2[i];
    test.fOp1 = fOp1[i];
    test.fOp2 = fOp2[i];

4626
    (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4627 4628 4629 4630 4631 4632 4633 4634 4635
    CHECK_EQ(test.dRes, dRes[i]);
    CHECK_EQ(test.fRes, fRes[i]);
  }

  test.dOp1 = DBL_MAX;
  test.dOp2 = -0.0;
  test.fOp1 = FLT_MAX;
  test.fOp2 = -0.0;

4636
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4637 4638 4639 4640 4641 4642 4643 4644
  CHECK_EQ(false, std::isfinite(test.dRes));
  CHECK_EQ(false, std::isfinite(test.fRes));

  test.dOp1 = 0.0;
  test.dOp2 = -0.0;
  test.fOp1 = 0.0;
  test.fOp2 = -0.0;

4645
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4646 4647 4648 4649 4650 4651 4652 4653
  CHECK_EQ(true, std::isnan(test.dRes));
  CHECK_EQ(true, std::isnan(test.fRes));

  test.dOp1 = std::numeric_limits<double>::quiet_NaN();
  test.dOp2 = -5.0;
  test.fOp1 = std::numeric_limits<float>::quiet_NaN();
  test.fOp2 = -5.0;

4654
  (CALL_GENERATED_CODE(isolate, f, &test, 0, 0, 0, 0));
4655 4656 4657 4658 4659
  CHECK_EQ(true, std::isnan(test.dRes));
  CHECK_EQ(true, std::isnan(test.fRes));
}


4660 4661 4662 4663
uint64_t run_align(uint64_t rs_value, uint64_t rt_value, uint8_t bp) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

4664
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
4665 4666 4667 4668 4669 4670 4671 4672 4673 4674

  __ align(v0, a0, a1, bp);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

4675
  F4 f = FUNCTION_CAST<F4>(code->entry());
4676

4677 4678
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, rs_value, rt_value, 0, 0, 0));
4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716

  return res;
}


TEST(r6_align) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseAlign {
      uint64_t  rs_value;
      uint64_t  rt_value;
      uint8_t   bp;
      uint64_t  expected_res;
    };

    struct TestCaseAlign tc[] = {
      // rs_value,    rt_value,    bp, expected_res
      {  0x11223344,  0xaabbccdd,   0, 0xffffffffaabbccdd },
      {  0x11223344,  0xaabbccdd,   1, 0xffffffffbbccdd11 },
      {  0x11223344,  0xaabbccdd,   2, 0xffffffffccdd1122 },
      {  0x11223344,  0xaabbccdd,   3, 0xffffffffdd112233 },
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseAlign);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      CHECK_EQ(tc[i].expected_res, run_align(tc[i].rs_value,
                                              tc[i].rt_value,
                                              tc[i].bp));
    }
  }
}


uint64_t run_dalign(uint64_t rs_value, uint64_t rt_value, uint8_t bp) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

4717
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728

  __ dalign(v0, a0, a1, bp);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F4 f = FUNCTION_CAST<F4>(code->entry());
4729 4730
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, rs_value, rt_value, 0, 0, 0));
4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774

  return res;
}


TEST(r6_dalign) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseDalign {
      uint64_t  rs_value;
      uint64_t  rt_value;
      uint8_t   bp;
      uint64_t  expected_res;
    };

    struct TestCaseDalign tc[] = {
      // rs_value,           rt_value,            bp, expected_res
      { 0x1122334455667700,  0xaabbccddeeff8899,   0, 0xaabbccddeeff8899 },
      { 0x1122334455667700,  0xaabbccddeeff8899,   1, 0xbbccddeeff889911 },
      { 0x1122334455667700,  0xaabbccddeeff8899,   2, 0xccddeeff88991122 },
      { 0x1122334455667700,  0xaabbccddeeff8899,   3, 0xddeeff8899112233 },
      { 0x1122334455667700,  0xaabbccddeeff8899,   4, 0xeeff889911223344 },
      { 0x1122334455667700,  0xaabbccddeeff8899,   5, 0xff88991122334455 },
      { 0x1122334455667700,  0xaabbccddeeff8899,   6, 0x8899112233445566 },
      { 0x1122334455667700,  0xaabbccddeeff8899,   7, 0x9911223344556677 }
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseDalign);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      CHECK_EQ(tc[i].expected_res, run_dalign(tc[i].rs_value,
                                              tc[i].rt_value,
                                              tc[i].bp));
    }
  }
}


uint64_t PC;  // The program counter.

uint64_t run_aluipc(int16_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

4775
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788

  __ aluipc(v0, offset);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());
  PC = (uint64_t) f;  // Set the program counter.

4789 4790
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828

  return res;
}


TEST(r6_aluipc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseAluipc {
      int16_t   offset;
    };

    struct TestCaseAluipc tc[] = {
      // offset
      { -32768 },   // 0x8000
      {     -1 },   // 0xFFFF
      {      0 },
      {      1 },
      {  32767 },   // 0x7FFF
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseAluipc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      PC = 0;
      uint64_t res = run_aluipc(tc[i].offset);
      // Now, the program_counter (PC) is set.
      uint64_t expected_res = ~0x0FFFF & (PC + (tc[i].offset << 16));
      CHECK_EQ(expected_res, res);
    }
  }
}


uint64_t run_auipc(int16_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

4829
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842

  __ auipc(v0, offset);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());
  PC = (uint64_t) f;  // Set the program counter.

4843 4844
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878

  return res;
}


TEST(r6_auipc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseAuipc {
      int16_t   offset;
    };

    struct TestCaseAuipc tc[] = {
      // offset
      { -32768 },   // 0x8000
      {     -1 },   // 0xFFFF
      {      0 },
      {      1 },
      {  32767 },   // 0x7FFF
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseAuipc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      PC = 0;
      uint64_t res = run_auipc(tc[i].offset);
      // Now, the program_counter (PC) is set.
      uint64_t expected_res = PC + (tc[i].offset << 16);
      CHECK_EQ(expected_res, res);
    }
  }
}


4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059
uint64_t run_aui(uint64_t rs, uint16_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);

  __ li(t0, rs);
  __ aui(v0, t0, offset);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

  uint64_t res =
    reinterpret_cast<uint64_t>
    (CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));

  return res;
}


uint64_t run_daui(uint64_t rs, uint16_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);

  __ li(t0, rs);
  __ daui(v0, t0, offset);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

  uint64_t res =
    reinterpret_cast<uint64_t>
    (CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));

  return res;
}


uint64_t run_dahi(uint64_t rs, uint16_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);

  __ li(v0, rs);
  __ dahi(v0, offset);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

  uint64_t res =
    reinterpret_cast<uint64_t>
    (CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));

  return res;
}


uint64_t run_dati(uint64_t rs, uint16_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);

  __ li(v0, rs);
  __ dati(v0, offset);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

  uint64_t res =
    reinterpret_cast<uint64_t>
    (CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));

  return res;
}


TEST(r6_aui_family) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseAui {
      uint64_t   rs;
      uint16_t   offset;
      uint64_t   ref_res;
    };

    // AUI test cases.
    struct TestCaseAui aui_tc[] = {
      {0xfffeffff, 0x1, 0xffffffffffffffff},
      {0xffffffff, 0x0, 0xffffffffffffffff},
      {0, 0xffff, 0xffffffffffff0000},
      {0x0008ffff, 0xfff7, 0xffffffffffffffff},
      {32767, 32767, 0x000000007fff7fff},
      {0x00000000ffffffff, 0x1, 0x000000000000ffff},
      {0xffffffff, 0xffff, 0xfffffffffffeffff},
    };

    size_t nr_test_cases = sizeof(aui_tc) / sizeof(TestCaseAui);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_aui(aui_tc[i].rs, aui_tc[i].offset);
      CHECK_EQ(aui_tc[i].ref_res, res);
    }

    // DAUI test cases.
    struct TestCaseAui daui_tc[] = {
      {0xfffffffffffeffff, 0x1, 0xffffffffffffffff},
      {0xffffffffffffffff, 0x0, 0xffffffffffffffff},
      {0, 0xffff, 0xffffffffffff0000},
      {0x0008ffff, 0xfff7, 0xffffffffffffffff},
      {32767, 32767, 0x000000007fff7fff},
      {0x00000000ffffffff, 0x1, 0x000000010000ffff},
      {0xffffffff, 0xffff, 0x00000000fffeffff},
    };

    nr_test_cases = sizeof(daui_tc) / sizeof(TestCaseAui);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_daui(daui_tc[i].rs, daui_tc[i].offset);
      CHECK_EQ(daui_tc[i].ref_res, res);
    }

    // DATI test cases.
    struct TestCaseAui dati_tc[] = {
      {0xfffffffffffeffff, 0x1, 0x0000fffffffeffff},
      {0xffffffffffffffff, 0x0, 0xffffffffffffffff},
      {0, 0xffff, 0xffff000000000000},
      {0x0008ffff, 0xfff7, 0xfff700000008ffff},
      {32767, 32767, 0x7fff000000007fff},
      {0x00000000ffffffff, 0x1, 0x00010000ffffffff},
      {0xffffffffffff, 0xffff, 0xffffffffffffffff},
    };

    nr_test_cases = sizeof(dati_tc) / sizeof(TestCaseAui);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_dati(dati_tc[i].rs, dati_tc[i].offset);
      CHECK_EQ(dati_tc[i].ref_res, res);
    }

    // DAHI test cases.
    struct TestCaseAui dahi_tc[] = {
      {0xfffffffeffffffff, 0x1, 0xffffffffffffffff},
      {0xffffffffffffffff, 0x0, 0xffffffffffffffff},
      {0, 0xffff, 0xffffffff00000000},
    };

    nr_test_cases = sizeof(dahi_tc) / sizeof(TestCaseAui);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_dahi(dahi_tc[i].rs, dahi_tc[i].offset);
      CHECK_EQ(dahi_tc[i].ref_res, res);
    }
  }
}


5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090
uint64_t run_li_macro(uint64_t rs, LiFlags mode) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);

  __ li(a0, rs, mode);
  __ mov(v0, a0);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));

  return res;
}


TEST(li_macro) {
  CcTest::InitializeVM();

  uint64_t inputs[] = {
      0x0000000000000000, 0x000000000000ffff, 0x00000000ffffffff,
      0x0000ffffffffffff, 0xffffffffffffffff, 0xffff000000000000,
      0xffffffff00000000, 0xffffffffffff0000, 0xffff0000ffff0000,
5091 5092
      0x0000ffffffff0000, 0x0000ffff0000ffff, 0x00007fffffffffff,
      0x7fffffffffffffff, 0x000000007fffffff, 0x00007fff7fffffff,
5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108
  };

  size_t nr_test_cases = sizeof(inputs) / sizeof(inputs[0]);
  for (size_t i = 0; i < nr_test_cases; ++i) {
    uint64_t res = run_li_macro(inputs[i], OPTIMIZE_SIZE);
    CHECK_EQ(inputs[i], res);
    res = run_li_macro(inputs[i], CONSTANT_SIZE);
    CHECK_EQ(inputs[i], res);
    if (is_int48(inputs[i])) {
      res = run_li_macro(inputs[i], ADDRESS_LOAD);
      CHECK_EQ(inputs[i], res);
    }
  }
}


5109 5110 5111 5112
uint64_t run_lwpc(int offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5113
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147

  // 256k instructions; 2^8k
  // addiu t3, a4, 0xffff;  (0x250fffff)
  // ...
  // addiu t0, a4, 0x0000;  (0x250c0000)
  uint32_t addiu_start_1 = 0x25000000;
  for (int32_t i = 0xfffff; i >= 0xc0000; --i) {
    uint32_t addiu_new = addiu_start_1 + i;
    __ dd(addiu_new);
  }

  __ lwpc(t8, offset);         // offset 0; 0xef080000 (t8 register)
  __ mov(v0, t8);

  // 256k instructions; 2^8k
  // addiu a4, a4, 0x0000;  (0x25080000)
  // ...
  // addiu a7, a4, 0xffff;  (0x250bffff)
  uint32_t addiu_start_2 = 0x25000000;
  for (int32_t i = 0x80000; i <= 0xbffff; ++i) {
    uint32_t addiu_new = addiu_start_2 + i;
    __ dd(addiu_new);
  }

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

5148 5149
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188

  return res;
}


TEST(r6_lwpc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseLwpc {
      int       offset;
      uint64_t  expected_res;
    };

    struct TestCaseLwpc tc[] = {
      // offset,   expected_res
      { -262144,   0x250fffff         },   // offset 0x40000
      {      -4,   0x250c0003         },
      {      -1,   0x250c0000         },
      {       0,   0xffffffffef080000 },
      {       1,   0x03001025         },   // mov(v0, t8)
      {       2,   0x25080000         },
      {       4,   0x25080002         },
      {  262143,   0x250bfffd         },   // offset 0x3ffff
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseLwpc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_lwpc(tc[i].offset);
      CHECK_EQ(tc[i].expected_res, res);
    }
  }
}


uint64_t run_lwupc(int offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5189
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223

  // 256k instructions; 2^8k
  // addiu t3, a4, 0xffff;  (0x250fffff)
  // ...
  // addiu t0, a4, 0x0000;  (0x250c0000)
  uint32_t addiu_start_1 = 0x25000000;
  for (int32_t i = 0xfffff; i >= 0xc0000; --i) {
    uint32_t addiu_new = addiu_start_1 + i;
    __ dd(addiu_new);
  }

  __ lwupc(t8, offset);         // offset 0; 0xef080000 (t8 register)
  __ mov(v0, t8);

  // 256k instructions; 2^8k
  // addiu a4, a4, 0x0000;  (0x25080000)
  // ...
  // addiu a7, a4, 0xffff;  (0x250bffff)
  uint32_t addiu_start_2 = 0x25000000;
  for (int32_t i = 0x80000; i <= 0xbffff; ++i) {
    uint32_t addiu_new = addiu_start_2 + i;
    __ dd(addiu_new);
  }

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

5224 5225
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264

  return res;
}


TEST(r6_lwupc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseLwupc {
      int       offset;
      uint64_t  expected_res;
    };

    struct TestCaseLwupc tc[] = {
      // offset,    expected_res
      { -262144,    0x250fffff },   // offset 0x40000
      {      -4,    0x250c0003 },
      {      -1,    0x250c0000 },
      {       0,    0xef100000 },
      {       1,    0x03001025 },   // mov(v0, t8)
      {       2,    0x25080000 },
      {       4,    0x25080002 },
      {  262143,    0x250bfffd },   // offset 0x3ffff
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseLwupc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_lwupc(tc[i].offset);
      CHECK_EQ(tc[i].expected_res, res);
    }
  }
}


uint64_t run_jic(int16_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5265
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307

  Label get_program_counter, stop_execution;
  __ push(ra);
  __ li(v0, 0);
  __ li(t1, 0x66);

  __ addiu(v0, v0, 0x1);        // <-- offset = -32
  __ addiu(v0, v0, 0x2);
  __ addiu(v0, v0, 0x10);
  __ addiu(v0, v0, 0x20);
  __ beq(v0, t1, &stop_execution);
  __ nop();

  __ bal(&get_program_counter);  // t0 <- program counter
  __ nop();
  __ jic(t0, offset);

  __ addiu(v0, v0, 0x100);
  __ addiu(v0, v0, 0x200);
  __ addiu(v0, v0, 0x1000);
  __ addiu(v0, v0, 0x2000);   // <--- offset = 16
  __ pop(ra);
  __ jr(ra);
  __ nop();

  __ bind(&get_program_counter);
  __ mov(t0, ra);
  __ jr(ra);
  __ nop();

  __ bind(&stop_execution);
  __ pop(ra);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

5308 5309
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345

  return res;
}


TEST(r6_jic) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseJic {
      // As rt will be used t0 register which will have value of
      // the program counter for the jic instruction.
      int16_t   offset;
      uint32_t  expected_res;
    };

    struct TestCaseJic tc[] = {
      // offset,   expected_result
      {      16,            0x2033 },
      {       4,            0x3333 },
      {     -32,              0x66 },
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseJic);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_jic(tc[i].offset);
      CHECK_EQ(tc[i].expected_res, res);
    }
  }
}


uint64_t run_beqzc(int32_t value, int32_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5346
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379

  Label stop_execution;
  __ li(v0, 0);
  __ li(t1, 0x66);

  __ addiu(v0, v0, 0x1);        // <-- offset = -8
  __ addiu(v0, v0, 0x2);
  __ addiu(v0, v0, 0x10);
  __ addiu(v0, v0, 0x20);
  __ beq(v0, t1, &stop_execution);
  __ nop();

  __ beqzc(a0, offset);

  __ addiu(v0, v0,    0x1);
  __ addiu(v0, v0,  0x100);
  __ addiu(v0, v0,  0x200);
  __ addiu(v0, v0, 0x1000);
  __ addiu(v0, v0, 0x2000);   // <--- offset = 4
  __ jr(ra);
  __ nop();

  __ bind(&stop_execution);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

5380 5381
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, value, 0, 0, 0, 0));
5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418

  return res;
}


TEST(r6_beqzc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseBeqzc {
      uint32_t  value;
      int32_t   offset;
      uint32_t  expected_res;
    };

    struct TestCaseBeqzc tc[] = {
      //    value,    offset,   expected_res
      {       0x0,        -8,           0x66 },
      {       0x0,         0,         0x3334 },
      {       0x0,         1,         0x3333 },
      {     0xabc,         1,         0x3334 },
      {       0x0,         4,         0x2033 },
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseBeqzc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_beqzc(tc[i].value, tc[i].offset);
      CHECK_EQ(tc[i].expected_res, res);
    }
  }
}


uint64_t run_jialc(int16_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5419
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473

  Label main_block, get_program_counter;
  __ push(ra);
  __ li(v0, 0);
  __ beq(v0, v0, &main_block);
  __ nop();

  // Block 1
  __ addiu(v0, v0, 0x1);        // <-- offset = -40
  __ addiu(v0, v0, 0x2);
  __ jr(ra);
  __ nop();

  // Block 2
  __ addiu(v0, v0, 0x10);        // <-- offset = -24
  __ addiu(v0, v0, 0x20);
  __ jr(ra);
  __ nop();

  // Block 3 (Main)
  __ bind(&main_block);
  __ bal(&get_program_counter);  // t0 <- program counter
  __ nop();
  __ jialc(t0, offset);
  __ addiu(v0, v0, 0x4);
  __ pop(ra);
  __ jr(ra);
  __ nop();

  // Block 4
  __ addiu(v0, v0, 0x100);      // <-- offset = 20
  __ addiu(v0, v0, 0x200);
  __ jr(ra);
  __ nop();

  // Block 5
  __ addiu(v0, v0, 0x1000);     // <--- offset = 36
  __ addiu(v0, v0, 0x2000);
  __ jr(ra);
  __ nop();

  __ bind(&get_program_counter);
  __ mov(t0, ra);
  __ jr(ra);
  __ nop();


  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

5474 5475
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512

  return res;
}


TEST(r6_jialc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseJialc {
      // As rt will be used t0 register which will have value of
      // the program counter for the jialc instruction.
      int16_t   offset;
      uint32_t  expected_res;
    };

    struct TestCaseJialc tc[] = {
      // offset,   expected_res
      {     -40,            0x7 },
      {     -24,           0x34 },
      {      20,          0x304 },
      {      36,         0x3004 }
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseJialc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_jialc(tc[i].offset);
      CHECK_EQ(tc[i].expected_res, res);
    }
  }
}


uint64_t run_addiupc(int32_t imm19) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5513
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526

  __ addiupc(v0, imm19);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());
  PC = (uint64_t) f;  // Set the program counter.

5527 5528
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566

  return res;
}


TEST(r6_addiupc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseAddiupc {
      int32_t   imm19;
    };

    struct TestCaseAddiupc tc[] = {
      //  imm19
      { -262144 },   // 0x40000
      {      -1 },   // 0x7FFFF
      {       0 },
      {       1 },   // 0x00001
      {  262143 }    // 0x3FFFF
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseAddiupc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      PC = 0;
      uint64_t res = run_addiupc(tc[i].imm19);
      // Now, the program_counter (PC) is set.
      uint64_t expected_res = PC + (tc[i].imm19 << 2);
      CHECK_EQ(expected_res, res);
    }
  }
}


uint64_t run_ldpc(int offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5567
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601

  // 256k instructions; 2 * 2^7k = 2^8k
  // addiu t3, a4, 0xffff;  (0x250fffff)
  // ...
  // addiu t0, a4, 0x0000;  (0x250c0000)
  uint32_t addiu_start_1 = 0x25000000;
  for (int32_t i = 0xfffff; i >= 0xc0000; --i) {
    uint32_t addiu_new = addiu_start_1 + i;
    __ dd(addiu_new);
  }

  __ ldpc(t8, offset);         // offset 0; 0xef080000 (t8 register)
  __ mov(v0, t8);

  // 256k instructions; 2 * 2^7k = 2^8k
  // addiu a4, a4, 0x0000;  (0x25080000)
  // ...
  // addiu a7, a4, 0xffff;  (0x250bffff)
  uint32_t addiu_start_2 = 0x25000000;
  for (int32_t i = 0x80000; i <= 0xbffff; ++i) {
    uint32_t addiu_new = addiu_start_2 + i;
    __ dd(addiu_new);
  }

  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

5602 5603
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617

  return res;
}


TEST(r6_ldpc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseLdpc {
      int       offset;
      uint64_t  expected_res;
    };

5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633
    auto doubleword = [](uint32_t word2, uint32_t word1) {
      if (kArchEndian == kLittle)
        return (static_cast<uint64_t>(word2) << 32) + word1;
      else
        return (static_cast<uint64_t>(word1) << 32) + word2;
    };

    TestCaseLdpc tc[] = {
        // offset,  expected_res
        {-131072, doubleword(0x250ffffe, 0x250fffff)},
        {-4, doubleword(0x250c0006, 0x250c0007)},
        {-1, doubleword(0x250c0000, 0x250c0001)},
        {0, doubleword(0x03001025, 0xef180000)},
        {1, doubleword(0x25080001, 0x25080000)},
        {4, doubleword(0x25080007, 0x25080006)},
        {131071, doubleword(0x250bfffd, 0x250bfffc)},
5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseLdpc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      uint64_t res = run_ldpc(tc[i].offset);
      CHECK_EQ(tc[i].expected_res, res);
    }
  }
}


int64_t run_bc(int32_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5649
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5650 5651 5652 5653 5654 5655 5656 5657

  Label continue_1, stop_execution;
  __ push(ra);
  __ li(v0, 0);
  __ li(t8, 0);
  __ li(t9, 2);   // Condition for the stopping execution.

  for (int32_t i = -100; i <= -11; ++i) {
5658
    __ addiu(v0, v0, 1);
5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676
  }

  __ addiu(t8, t8, 1);              // -10

  __ beq(t8, t9, &stop_execution);  // -9
  __ nop();                         // -8
  __ beq(t8, t8, &continue_1);      // -7
  __ nop();                         // -6

  __ bind(&stop_execution);
  __ pop(ra);                       // -5, -4
  __ jr(ra);                        // -3
  __ nop();                         // -2

  __ bind(&continue_1);
  __ bc(offset);                    // -1

  for (int32_t i = 0; i <= 99; ++i) {
5677
    __ addiu(v0, v0, 1);
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690
  }

  __ pop(ra);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

5691 5692
  int64_t res = reinterpret_cast<int64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728

  return res;
}


TEST(r6_bc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseBc {
      int32_t   offset;
      int64_t   expected_res;
    };

    struct TestCaseBc tc[] = {
      //    offset,   expected_result
      {       -100,   (abs(-100) - 10) * 2      },
      {        -11,   (abs(-100) - 10 + 1)      },
      {          0,   (abs(-100) - 10 + 1 + 99) },
      {          1,   (abs(-100) - 10 + 99)     },
      {         99,   (abs(-100) - 10 + 1)      },
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseBc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      int64_t res = run_bc(tc[i].offset);
      CHECK_EQ(tc[i].expected_res, res);
    }
  }
}


int64_t run_balc(int32_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5729
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772

  Label continue_1, stop_execution;
  __ push(ra);
  __ li(v0, 0);
  __ li(t8, 0);
  __ li(t9, 2);   // Condition for stopping execution.

  __ beq(t8, t8, &continue_1);
  __ nop();

  uint32_t instruction_addiu = 0x24420001;  // addiu v0, v0, 1
  for (int32_t i = -117; i <= -57; ++i) {
    __ dd(instruction_addiu);
  }
  __ jr(ra);                        // -56
  __ nop();                         // -55

  for (int32_t i = -54; i <= -4; ++i) {
    __ dd(instruction_addiu);
  }
  __ jr(ra);                        // -3
  __ nop();                         // -2

  __ bind(&continue_1);
  __ balc(offset);                    // -1

  __ pop(ra);                         // 0, 1
  __ jr(ra);                          // 2
  __ nop();                           // 3

  for (int32_t i = 4; i <= 44; ++i) {
    __ dd(instruction_addiu);
  }
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

5773 5774
  int64_t res = reinterpret_cast<int64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805

  return res;
}


TEST(r6_balc) {
  if (kArchVariant == kMips64r6) {
    CcTest::InitializeVM();

    struct TestCaseBalc {
      int32_t   offset;
      int64_t   expected_res;
    };

    struct TestCaseBalc tc[] = {
      //  offset,   expected_result
      {     -117,   61  },
      {      -54,   51  },
      {        0,   0   },
      {        4,   41  },
    };

    size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseBalc);
    for (size_t i = 0; i < nr_test_cases; ++i) {
      int64_t res = run_balc(tc[i].offset);
      CHECK_EQ(tc[i].expected_res, res);
    }
  }
}


5806 5807 5808 5809
uint64_t run_dsll(uint64_t rt_value, uint16_t sa_value) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5810
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5811 5812 5813 5814 5815 5816 5817 5818 5819 5820

  __ dsll(v0, a0, sa_value);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

5821
  F4 f = FUNCTION_CAST<F4>(code->entry());
5822

5823 5824
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, rt_value, 0, 0, 0, 0));
5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857

  return res;
}


TEST(dsll) {
  CcTest::InitializeVM();

  struct TestCaseDsll {
    uint64_t  rt_value;
    uint16_t  sa_value;
    uint64_t  expected_res;
  };

  struct TestCaseDsll tc[] = {
    // rt_value,           sa_value, expected_res
    {  0xffffffffffffffff,    0,      0xffffffffffffffff },
    {  0xffffffffffffffff,   16,      0xffffffffffff0000 },
    {  0xffffffffffffffff,   31,      0xffffffff80000000 },
  };

  size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseDsll);
  for (size_t i = 0; i < nr_test_cases; ++i) {
    CHECK_EQ(tc[i].expected_res,
            run_dsll(tc[i].rt_value, tc[i].sa_value));
  }
}


uint64_t run_bal(int16_t offset) {
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5858
  MacroAssembler assm(isolate, NULL, 0, v8::internal::CodeObjectRequired::kYes);
5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878

  __ mov(t0, ra);
  __ bal(offset);       // Equivalent for "BGEZAL zero_reg, offset".
  __ nop();

  __ mov(ra, t0);
  __ jr(ra);
  __ nop();

  __ li(v0, 1);
  __ jr(ra);
  __ nop();

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());

  F2 f = FUNCTION_CAST<F2>(code->entry());

5879 5880
  uint64_t res = reinterpret_cast<uint64_t>(
      CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905

  return res;
}


TEST(bal) {
  CcTest::InitializeVM();

  struct TestCaseBal {
    int16_t  offset;
    uint64_t  expected_res;
  };

  struct TestCaseBal tc[] = {
    // offset, expected_res
    {       4,      1 },
  };

  size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseBal);
  for (size_t i = 0; i < nr_test_cases; ++i) {
    CHECK_EQ(tc[i].expected_res, run_bal(tc[i].offset));
  }
}


5906 5907 5908 5909 5910 5911 5912 5913
TEST(Trampoline) {
  // Private member of Assembler class.
  static const int kMaxBranchOffset = (1 << (18 - 1)) - 1;

  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  HandleScope scope(isolate);

5914 5915
  MacroAssembler assm(isolate, nullptr, 0,
                      v8::internal::CodeObjectRequired::kYes);
5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931
  Label done;
  size_t nr_calls = kMaxBranchOffset / (2 * Instruction::kInstrSize) + 2;

  for (size_t i = 0; i < nr_calls; ++i) {
    __ BranchShort(&done, eq, a0, Operand(a1));
  }
  __ bind(&done);
  __ Ret(USE_DELAY_SLOT);
  __ mov(v0, zero_reg);

  CodeDesc desc;
  assm.GetCode(&desc);
  Handle<Code> code = isolate->factory()->NewCode(
      desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
  F2 f = FUNCTION_CAST<F2>(code->entry());

5932 5933
  int64_t res = reinterpret_cast<int64_t>(
      CALL_GENERATED_CODE(isolate, f, 42, 42, 0, 0, 0));
5934 5935 5936 5937
  CHECK_EQ(res, 0);
}


5938
#undef __