test-assembler-x64.cc 70.4 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 2009 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 <cstdlib>
#include <iostream>
30

31
#include "src/v8.h"
32

33
#include "src/base/platform/platform.h"
34
#include "src/base/utils/random-number-generator.h"
35
#include "src/double.h"
36
#include "src/heap/factory.h"
37
#include "src/macro-assembler.h"
38
#include "src/objects-inl.h"
39
#include "src/ostreams.h"
40
#include "src/simulator.h"
41
#include "test/cctest/cctest.h"
42
#include "test/common/assembler-tester.h"
43

44 45
namespace v8 {
namespace internal {
46

47 48 49
// Test the x64 assembler by compiling some simple functions into
// a buffer and executing them.  These tests do not initialize the
// V8 library, create a context, or use any V8 objects.
50 51
// The AMD64 calling convention is used, with the first six arguments
// in RDI, RSI, RDX, RCX, R8, and R9, and floating point arguments in
52 53
// the XMM registers.  The return value is in RAX.
// This calling convention is used on Linux, with GCC, and on Mac OS,
54 55
// with GCC.  A different convention is used on 64-bit windows,
// where the first four integer arguments are passed in RCX, RDX, R8 and R9.
56

57 58 59 60 61 62
typedef int(F0)();
typedef int(F1)(int64_t x);
typedef int(F2)(int64_t x, int64_t y);
typedef unsigned(F3)(double x);
typedef uint64_t(F4)(uint64_t* x, uint64_t* y);
typedef uint64_t(F5)(uint64_t x);
63

64
#ifdef _WIN64
65 66
static const Register arg1 = rcx;
static const Register arg2 = rdx;
67
#else
68 69
static const Register arg1 = rdi;
static const Register arg2 = rsi;
70 71
#endif

72
#define __ masm.
73

74
TEST(AssemblerX64ReturnOperation) {
75
  CcTest::InitializeVM();
76 77
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
78
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
79 80

  // Assemble a simple function that copies argument 2 and returns it.
81
  __ movq(rax, arg2);
82 83 84 85
  __ nop();
  __ ret(0);

  CodeDesc desc;
86
  masm.GetCode(CcTest::i_isolate(), &desc);
87
  MakeAssemblerBufferExecutable(buffer, allocated);
88
  // Call the function from C++.
89
  auto f = GeneratedCode<F2>::FromBuffer(CcTest::i_isolate(), buffer);
90
  int result = f.Call(3, 2);
91 92 93
  CHECK_EQ(2, result);
}

94

95
TEST(AssemblerX64StackOperations) {
96
  CcTest::InitializeVM();
97 98
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
99
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
100 101 102 103

  // Assemble a simple function that copies argument 2 and returns it.
  // We compile without stack frame pointers, so the gdb debugger shows
  // incorrect stack frames when debugging this function (which has them).
104
  __ pushq(rbp);
105
  __ movq(rbp, rsp);
106 107 108 109 110 111 112
  __ pushq(arg2);  // Value at (rbp - 8)
  __ pushq(arg2);  // Value at (rbp - 16)
  __ pushq(arg1);  // Value at (rbp - 24)
  __ popq(rax);
  __ popq(rax);
  __ popq(rax);
  __ popq(rbp);
113 114 115 116
  __ nop();
  __ ret(0);

  CodeDesc desc;
117
  masm.GetCode(CcTest::i_isolate(), &desc);
118
  MakeAssemblerBufferExecutable(buffer, allocated);
119
  // Call the function from C++.
120
  auto f = GeneratedCode<F2>::FromBuffer(CcTest::i_isolate(), buffer);
121
  int result = f.Call(3, 2);
122 123 124
  CHECK_EQ(2, result);
}

125

126
TEST(AssemblerX64ArithmeticOperations) {
127
  CcTest::InitializeVM();
128 129
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
130
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
131

132
  // Assemble a simple function that adds arguments returning the sum.
133 134
  __ movq(rax, arg2);
  __ addq(rax, arg1);
135 136 137
  __ ret(0);

  CodeDesc desc;
138
  masm.GetCode(CcTest::i_isolate(), &desc);
139
  MakeAssemblerBufferExecutable(buffer, allocated);
140
  // Call the function from C++.
141
  auto f = GeneratedCode<F2>::FromBuffer(CcTest::i_isolate(), buffer);
142
  int result = f.Call(3, 2);
143 144 145
  CHECK_EQ(5, result);
}

146

147
TEST(AssemblerX64CmpbOperation) {
148
  CcTest::InitializeVM();
149 150
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
151
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
152 153 154 155 156 157 158 159 160 161 162 163 164 165

  // Assemble a function that compare argument byte returing 1 if equal else 0.
  // On Windows, it compares rcx with rdx which does not require REX prefix;
  // on Linux, it compares rdi with rsi which requires REX prefix.

  Label done;
  __ movq(rax, Immediate(1));
  __ cmpb(arg1, arg2);
  __ j(equal, &done);
  __ movq(rax, Immediate(0));
  __ bind(&done);
  __ ret(0);

  CodeDesc desc;
166
  masm.GetCode(CcTest::i_isolate(), &desc);
167
  MakeAssemblerBufferExecutable(buffer, allocated);
168
  // Call the function from C++.
169
  auto f = GeneratedCode<F2>::FromBuffer(CcTest::i_isolate(), buffer);
170
  int result = f.Call(0x1002, 0x2002);
171
  CHECK_EQ(1, result);
172
  result = f.Call(0x1002, 0x2003);
173 174 175
  CHECK_EQ(0, result);
}

176
TEST(AssemblerX64ImulOperation) {
177
  CcTest::InitializeVM();
178 179
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
180
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
181 182 183

  // Assemble a simple function that multiplies arguments returning the high
  // word.
184
  __ movq(rax, arg2);
185
  __ imulq(arg1);
186 187 188 189
  __ movq(rax, rdx);
  __ ret(0);

  CodeDesc desc;
190
  masm.GetCode(CcTest::i_isolate(), &desc);
191
  MakeAssemblerBufferExecutable(buffer, allocated);
192
  // Call the function from C++.
193
  auto f = GeneratedCode<F2>::FromBuffer(CcTest::i_isolate(), buffer);
194
  int result = f.Call(3, 2);
195
  CHECK_EQ(0, result);
196
  result = f.Call(0x100000000l, 0x100000000l);
197
  CHECK_EQ(1, result);
198
  result = f.Call(-0x100000000l, 0x100000000l);
199 200 201
  CHECK_EQ(-1, result);
}

202
TEST(AssemblerX64testbwqOperation) {
203 204
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
205 206
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
207
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

  __ pushq(rbx);
  __ pushq(rdi);
  __ pushq(rsi);
  __ pushq(r12);
  __ pushq(r13);
  __ pushq(r14);
  __ pushq(r15);

  // Assemble a simple function that tests testb and testw
  Label bad;
  Label done;

  // Test immediate testb and testw
  __ movq(rax, Immediate(2));
  __ movq(rbx, Immediate(4));
  __ movq(rcx, Immediate(8));
  __ movq(rdx, Immediate(16));
  __ movq(rsi, Immediate(32));
  __ movq(rdi, Immediate(64));
  __ movq(r10, Immediate(128));
  __ movq(r11, Immediate(0));
  __ movq(r12, Immediate(0));
  __ movq(r13, Immediate(0));
  __ testb(rax, Immediate(2));
  __ j(zero, &bad);
  __ testb(rbx, Immediate(4));
  __ j(zero, &bad);
  __ testb(rcx, Immediate(8));
  __ j(zero, &bad);
  __ testb(rdx, Immediate(16));
  __ j(zero, &bad);
  __ testb(rsi, Immediate(32));
  __ j(zero, &bad);
  __ testb(rdi, Immediate(64));
  __ j(zero, &bad);
  __ testb(r10, Immediate(128));
  __ j(zero, &bad);
  __ testw(rax, Immediate(2));
  __ j(zero, &bad);
  __ testw(rbx, Immediate(4));
  __ j(zero, &bad);
  __ testw(rcx, Immediate(8));
  __ j(zero, &bad);
  __ testw(rdx, Immediate(16));
  __ j(zero, &bad);
  __ testw(rsi, Immediate(32));
  __ j(zero, &bad);
  __ testw(rdi, Immediate(64));
  __ j(zero, &bad);
  __ testw(r10, Immediate(128));
  __ j(zero, &bad);

  // Test reg, reg testb and testw
  __ movq(rax, Immediate(2));
  __ movq(rbx, Immediate(2));
  __ testb(rax, rbx);
  __ j(zero, &bad);
  __ movq(rbx, Immediate(4));
  __ movq(rax, Immediate(4));
  __ testb(rbx, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(8));
  __ testb(rcx, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(16));
  __ testb(rdx, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(32));
  __ testb(rsi, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(64));
  __ testb(rdi, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(128));
  __ testb(r10, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(2));
  __ movq(rbx, Immediate(2));
  __ testw(rax, rbx);
  __ j(zero, &bad);
  __ movq(rbx, Immediate(4));
  __ movq(rax, Immediate(4));
  __ testw(rbx, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(8));
  __ testw(rcx, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(16));
  __ testw(rdx, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(32));
  __ testw(rsi, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(64));
  __ testw(rdi, rax);
  __ j(zero, &bad);
  __ movq(rax, Immediate(128));
  __ testw(r10, rax);
  __ j(zero, &bad);

  // Test diffrrent extended register coding combinations.
  __ movq(rax, Immediate(5));
  __ movq(r11, Immediate(5));
  __ testb(r11, rax);
  __ j(zero, &bad);
  __ testb(rax, r11);
  __ j(zero, &bad);
  __ testw(r11, rax);
  __ j(zero, &bad);
  __ testw(rax, r11);
  __ j(zero, &bad);
  __ movq(r11, Immediate(3));
  __ movq(r12, Immediate(3));
  __ movq(rdi, Immediate(3));
  __ testb(r12, rdi);
  __ j(zero, &bad);
  __ testb(rdi, r12);
  __ j(zero, &bad);
  __ testb(r12, r11);
  __ j(zero, &bad);
  __ testb(r11, r12);
  __ j(zero, &bad);
  __ testw(r12, r11);
  __ j(zero, &bad);
  __ testw(r11, r12);
  __ j(zero, &bad);

336 337 338 339 340 341
  // Test sign-extended imediate tests
  __ movq(r11, Immediate(2));
  __ shlq(r11, Immediate(32));
  __ testq(r11, Immediate(-1));
  __ j(zero, &bad);

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  // All tests passed
  __ movq(rax, Immediate(1));
  __ jmp(&done);

  __ bind(&bad);
  __ movq(rax, Immediate(0));
  __ bind(&done);

  __ popq(r15);
  __ popq(r14);
  __ popq(r13);
  __ popq(r12);
  __ popq(rsi);
  __ popq(rdi);
  __ popq(rbx);

  __ ret(0);

  CodeDesc desc;
361
  masm.GetCode(CcTest::i_isolate(), &desc);
362
  MakeAssemblerBufferExecutable(buffer, allocated);
363
  // Call the function from C++.
364
  auto f = GeneratedCode<F2>::FromBuffer(CcTest::i_isolate(), buffer);
365
  int result = f.Call(0, 0);
366 367
  CHECK_EQ(1, result);
}
368

369
TEST(AssemblerX64XchglOperations) {
370
  CcTest::InitializeVM();
371 372
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
373
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
374 375

  __ movq(rax, Operand(arg1, 0));
svenpanne@chromium.org's avatar
svenpanne@chromium.org committed
376 377
  __ movq(r11, Operand(arg2, 0));
  __ xchgl(rax, r11);
378
  __ movq(Operand(arg1, 0), rax);
svenpanne@chromium.org's avatar
svenpanne@chromium.org committed
379
  __ movq(Operand(arg2, 0), r11);
380 381 382
  __ ret(0);

  CodeDesc desc;
383
  masm.GetCode(CcTest::i_isolate(), &desc);
384
  MakeAssemblerBufferExecutable(buffer, allocated);
385
  // Call the function from C++.
386 387
  uint64_t left = V8_2PART_UINT64_C(0x10000000, 20000000);
  uint64_t right = V8_2PART_UINT64_C(0x30000000, 40000000);
388
  auto f = GeneratedCode<F4>::FromBuffer(CcTest::i_isolate(), buffer);
389
  uint64_t result = f.Call(&left, &right);
390 391 392 393 394 395 396
  CHECK_EQ(V8_2PART_UINT64_C(0x00000000, 40000000), left);
  CHECK_EQ(V8_2PART_UINT64_C(0x00000000, 20000000), right);
  USE(result);
}


TEST(AssemblerX64OrlOperations) {
397
  CcTest::InitializeVM();
398 399
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
400
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
401 402 403 404 405 406

  __ movq(rax, Operand(arg2, 0));
  __ orl(Operand(arg1, 0), rax);
  __ ret(0);

  CodeDesc desc;
407
  masm.GetCode(CcTest::i_isolate(), &desc);
408
  MakeAssemblerBufferExecutable(buffer, allocated);
409
  // Call the function from C++.
410 411
  uint64_t left = V8_2PART_UINT64_C(0x10000000, 20000000);
  uint64_t right = V8_2PART_UINT64_C(0x30000000, 40000000);
412
  auto f = GeneratedCode<F4>::FromBuffer(CcTest::i_isolate(), buffer);
413
  uint64_t result = f.Call(&left, &right);
414 415 416 417 418 419
  CHECK_EQ(V8_2PART_UINT64_C(0x10000000, 60000000), left);
  USE(result);
}


TEST(AssemblerX64RollOperations) {
420
  CcTest::InitializeVM();
421 422
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
423
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
424 425 426 427 428 429

  __ movq(rax, arg1);
  __ roll(rax, Immediate(1));
  __ ret(0);

  CodeDesc desc;
430
  masm.GetCode(CcTest::i_isolate(), &desc);
431
  MakeAssemblerBufferExecutable(buffer, allocated);
432
  // Call the function from C++.
433
  uint64_t src = V8_2PART_UINT64_C(0x10000000, C0000000);
434
  auto f = GeneratedCode<F5>::FromBuffer(CcTest::i_isolate(), buffer);
435
  uint64_t result = f.Call(src);
436 437 438 439 440
  CHECK_EQ(V8_2PART_UINT64_C(0x00000000, 80000001), result);
}


TEST(AssemblerX64SublOperations) {
441
  CcTest::InitializeVM();
442 443
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
444
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
445 446 447 448 449 450

  __ movq(rax, Operand(arg2, 0));
  __ subl(Operand(arg1, 0), rax);
  __ ret(0);

  CodeDesc desc;
451
  masm.GetCode(CcTest::i_isolate(), &desc);
452
  MakeAssemblerBufferExecutable(buffer, allocated);
453
  // Call the function from C++.
454 455
  uint64_t left = V8_2PART_UINT64_C(0x10000000, 20000000);
  uint64_t right = V8_2PART_UINT64_C(0x30000000, 40000000);
456
  auto f = GeneratedCode<F4>::FromBuffer(CcTest::i_isolate(), buffer);
457
  uint64_t result = f.Call(&left, &right);
458
  CHECK_EQ(V8_2PART_UINT64_C(0x10000000, E0000000), left);
459 460 461 462 463
  USE(result);
}


TEST(AssemblerX64TestlOperations) {
464
  CcTest::InitializeVM();
465 466
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
467
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
468 469 470 471

  // Set rax with the ZF flag of the testl instruction.
  Label done;
  __ movq(rax, Immediate(1));
svenpanne@chromium.org's avatar
svenpanne@chromium.org committed
472 473
  __ movq(r11, Operand(arg2, 0));
  __ testl(Operand(arg1, 0), r11);
474 475 476 477 478 479
  __ j(zero, &done, Label::kNear);
  __ movq(rax, Immediate(0));
  __ bind(&done);
  __ ret(0);

  CodeDesc desc;
480
  masm.GetCode(CcTest::i_isolate(), &desc);
481
  MakeAssemblerBufferExecutable(buffer, allocated);
482
  // Call the function from C++.
483 484
  uint64_t left = V8_2PART_UINT64_C(0x10000000, 20000000);
  uint64_t right = V8_2PART_UINT64_C(0x30000000, 00000000);
485
  auto f = GeneratedCode<F4>::FromBuffer(CcTest::i_isolate(), buffer);
486
  uint64_t result = f.Call(&left, &right);
487
  CHECK_EQ(1u, result);
488 489
}

490
TEST(AssemblerX64TestwOperations) {
491
  typedef uint16_t(F)(uint16_t * x);
492
  CcTest::InitializeVM();
493 494
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
495
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
496 497 498 499

  // Set rax with the ZF flag of the testl instruction.
  Label done;
  __ movq(rax, Immediate(1));
500
  __ testw(Operand(arg1, 0), Immediate(0xF0F0));
501 502 503 504 505 506
  __ j(not_zero, &done, Label::kNear);
  __ movq(rax, Immediate(0));
  __ bind(&done);
  __ ret(0);

  CodeDesc desc;
507
  masm.GetCode(CcTest::i_isolate(), &desc);
508
  MakeAssemblerBufferExecutable(buffer, allocated);
509 510
  // Call the function from C++.
  uint16_t operand = 0x8000;
511
  auto f = GeneratedCode<F>::FromBuffer(CcTest::i_isolate(), buffer);
512
  uint16_t result = f.Call(&operand);
513 514
  CHECK_EQ(1u, result);
}
515 516

TEST(AssemblerX64XorlOperations) {
517
  CcTest::InitializeVM();
518 519
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
520
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
521 522 523 524 525 526

  __ movq(rax, Operand(arg2, 0));
  __ xorl(Operand(arg1, 0), rax);
  __ ret(0);

  CodeDesc desc;
527
  masm.GetCode(CcTest::i_isolate(), &desc);
528
  MakeAssemblerBufferExecutable(buffer, allocated);
529
  // Call the function from C++.
530 531
  uint64_t left = V8_2PART_UINT64_C(0x10000000, 20000000);
  uint64_t right = V8_2PART_UINT64_C(0x30000000, 60000000);
532
  auto f = GeneratedCode<F4>::FromBuffer(CcTest::i_isolate(), buffer);
533
  uint64_t result = f.Call(&left, &right);
534 535 536 537 538
  CHECK_EQ(V8_2PART_UINT64_C(0x10000000, 40000000), left);
  USE(result);
}


539
TEST(AssemblerX64MemoryOperands) {
540
  CcTest::InitializeVM();
541 542
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
543
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
544 545

  // Assemble a simple function that copies argument 2 and returns it.
546
  __ pushq(rbp);
547
  __ movq(rbp, rsp);
548

549 550 551
  __ pushq(arg2);  // Value at (rbp - 8)
  __ pushq(arg2);  // Value at (rbp - 16)
  __ pushq(arg1);  // Value at (rbp - 24)
552

553
  const int kStackElementSize = 8;
554
  __ movq(rax, Operand(rbp, -3 * kStackElementSize));
555 556 557 558
  __ popq(arg2);
  __ popq(arg2);
  __ popq(arg2);
  __ popq(rbp);
559 560 561 562
  __ nop();
  __ ret(0);

  CodeDesc desc;
563
  masm.GetCode(CcTest::i_isolate(), &desc);
564
  MakeAssemblerBufferExecutable(buffer, allocated);
565
  // Call the function from C++.
566
  auto f = GeneratedCode<F2>::FromBuffer(CcTest::i_isolate(), buffer);
567
  int result = f.Call(3, 2);
568
  CHECK_EQ(3, result);
569 570
}

571

572
TEST(AssemblerX64ControlFlow) {
573
  CcTest::InitializeVM();
574 575
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
576
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
577

578
  // Assemble a simple function that copies argument 1 and returns it.
579
  __ pushq(rbp);
580

581
  __ movq(rbp, rsp);
582
  __ movq(rax, arg1);
583 584
  Label target;
  __ jmp(&target);
585
  __ movq(rax, arg2);
586
  __ bind(&target);
587
  __ popq(rbp);
588 589 590
  __ ret(0);

  CodeDesc desc;
591
  masm.GetCode(CcTest::i_isolate(), &desc);
592
  MakeAssemblerBufferExecutable(buffer, allocated);
593
  // Call the function from C++.
594
  auto f = GeneratedCode<F2>::FromBuffer(CcTest::i_isolate(), buffer);
595
  int result = f.Call(3, 2);
596 597 598
  CHECK_EQ(3, result);
}

599

600
TEST(AssemblerX64LoopImmediates) {
601
  CcTest::InitializeVM();
602 603
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
604
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
605

606 607 608 609 610 611 612
  // Assemble two loops using rax as counter, and verify the ending counts.
  Label Fail;
  __ movq(rax, Immediate(-3));
  Label Loop1_test;
  Label Loop1_body;
  __ jmp(&Loop1_test);
  __ bind(&Loop1_body);
613
  __ addq(rax, Immediate(7));
614
  __ bind(&Loop1_test);
615
  __ cmpq(rax, Immediate(20));
616 617
  __ j(less_equal, &Loop1_body);
  // Did the loop terminate with the expected value?
618
  __ cmpq(rax, Immediate(25));
619 620 621 622 623 624 625
  __ j(not_equal, &Fail);

  Label Loop2_test;
  Label Loop2_body;
  __ movq(rax, Immediate(0x11FEED00));
  __ jmp(&Loop2_test);
  __ bind(&Loop2_body);
626
  __ addq(rax, Immediate(-0x1100));
627
  __ bind(&Loop2_test);
628
  __ cmpq(rax, Immediate(0x11FE8000));
629 630
  __ j(greater, &Loop2_body);
  // Did the loop terminate with the expected value?
631
  __ cmpq(rax, Immediate(0x11FE7600));
632 633 634 635 636 637 638 639 640
  __ j(not_equal, &Fail);

  __ movq(rax, Immediate(1));
  __ ret(0);
  __ bind(&Fail);
  __ movq(rax, Immediate(0));
  __ ret(0);

  CodeDesc desc;
641
  masm.GetCode(CcTest::i_isolate(), &desc);
642
  MakeAssemblerBufferExecutable(buffer, allocated);
643
  // Call the function from C++.
644
  auto f = GeneratedCode<F0>::FromBuffer(CcTest::i_isolate(), buffer);
645
  int result = f.Call();
646 647
  CHECK_EQ(1, result);
}
648

649 650

TEST(OperandRegisterDependency) {
651
  int offsets[4] = {0, 1, 0xFED, 0xBEEFCAD};
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
  for (int i = 0; i < 4; i++) {
    int offset = offsets[i];
    CHECK(Operand(rax, offset).AddressUsesRegister(rax));
    CHECK(!Operand(rax, offset).AddressUsesRegister(r8));
    CHECK(!Operand(rax, offset).AddressUsesRegister(rcx));

    CHECK(Operand(rax, rax, times_1, offset).AddressUsesRegister(rax));
    CHECK(!Operand(rax, rax, times_1, offset).AddressUsesRegister(r8));
    CHECK(!Operand(rax, rax, times_1, offset).AddressUsesRegister(rcx));

    CHECK(Operand(rax, rcx, times_1, offset).AddressUsesRegister(rax));
    CHECK(Operand(rax, rcx, times_1, offset).AddressUsesRegister(rcx));
    CHECK(!Operand(rax, rcx, times_1, offset).AddressUsesRegister(r8));
    CHECK(!Operand(rax, rcx, times_1, offset).AddressUsesRegister(r9));
    CHECK(!Operand(rax, rcx, times_1, offset).AddressUsesRegister(rdx));
    CHECK(!Operand(rax, rcx, times_1, offset).AddressUsesRegister(rsp));

    CHECK(Operand(rsp, offset).AddressUsesRegister(rsp));
    CHECK(!Operand(rsp, offset).AddressUsesRegister(rax));
671
    CHECK(!Operand(rsp, offset).AddressUsesRegister(r15));
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

    CHECK(Operand(rbp, offset).AddressUsesRegister(rbp));
    CHECK(!Operand(rbp, offset).AddressUsesRegister(rax));
    CHECK(!Operand(rbp, offset).AddressUsesRegister(r13));

    CHECK(Operand(rbp, rax, times_1, offset).AddressUsesRegister(rbp));
    CHECK(Operand(rbp, rax, times_1, offset).AddressUsesRegister(rax));
    CHECK(!Operand(rbp, rax, times_1, offset).AddressUsesRegister(rcx));
    CHECK(!Operand(rbp, rax, times_1, offset).AddressUsesRegister(r13));
    CHECK(!Operand(rbp, rax, times_1, offset).AddressUsesRegister(r8));
    CHECK(!Operand(rbp, rax, times_1, offset).AddressUsesRegister(rsp));

    CHECK(Operand(rsp, rbp, times_1, offset).AddressUsesRegister(rsp));
    CHECK(Operand(rsp, rbp, times_1, offset).AddressUsesRegister(rbp));
    CHECK(!Operand(rsp, rbp, times_1, offset).AddressUsesRegister(rax));
687
    CHECK(!Operand(rsp, rbp, times_1, offset).AddressUsesRegister(r15));
688 689 690 691
    CHECK(!Operand(rsp, rbp, times_1, offset).AddressUsesRegister(r13));
  }
}

692 693 694

TEST(AssemblerX64LabelChaining) {
  // Test chaining of label usages within instructions (issue 1644).
695 696
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
697
  Assembler masm(AssemblerOptions{}, nullptr, 0);
698 699 700 701 702 703 704 705

  Label target;
  __ j(equal, &target);
  __ j(not_equal, &target);
  __ bind(&target);
  __ nop();
}

706 707

TEST(AssemblerMultiByteNop) {
708 709
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
710
  byte buffer[1024];
711
  Isolate* isolate = CcTest::i_isolate();
712
  Assembler masm(AssemblerOptions{}, buffer, sizeof(buffer));
713 714 715 716 717
  __ pushq(rbx);
  __ pushq(rcx);
  __ pushq(rdx);
  __ pushq(rdi);
  __ pushq(rsi);
718 719 720 721 722 723 724
  __ movq(rax, Immediate(1));
  __ movq(rbx, Immediate(2));
  __ movq(rcx, Immediate(3));
  __ movq(rdx, Immediate(4));
  __ movq(rdi, Immediate(5));
  __ movq(rsi, Immediate(6));
  for (int i = 0; i < 16; i++) {
725
    int before = masm.pc_offset();
726
    __ Nop(i);
727
    CHECK_EQ(masm.pc_offset() - before, i);
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
  }

  Label fail;
  __ cmpq(rax, Immediate(1));
  __ j(not_equal, &fail);
  __ cmpq(rbx, Immediate(2));
  __ j(not_equal, &fail);
  __ cmpq(rcx, Immediate(3));
  __ j(not_equal, &fail);
  __ cmpq(rdx, Immediate(4));
  __ j(not_equal, &fail);
  __ cmpq(rdi, Immediate(5));
  __ j(not_equal, &fail);
  __ cmpq(rsi, Immediate(6));
  __ j(not_equal, &fail);
  __ movq(rax, Immediate(42));
744 745 746 747 748
  __ popq(rsi);
  __ popq(rdi);
  __ popq(rdx);
  __ popq(rcx);
  __ popq(rbx);
749 750 751
  __ ret(0);
  __ bind(&fail);
  __ movq(rax, Immediate(13));
752 753 754 755 756
  __ popq(rsi);
  __ popq(rdi);
  __ popq(rdx);
  __ popq(rcx);
  __ popq(rbx);
757 758 759
  __ ret(0);

  CodeDesc desc;
760
  masm.GetCode(isolate, &desc);
761 762
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
763

764 765
  auto f = GeneratedCode<F0>::FromCode(*code);
  int res = f.Call();
766 767 768 769
  CHECK_EQ(42, res);
}


770
#ifdef __GNUC__
771
#define ELEMENT_COUNT 4u
772 773 774

void DoSSE2(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::HandleScope scope(CcTest::isolate());
775
  v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
776
  byte buffer[1024];
777 778 779 780 781

  CHECK(args[0]->IsArray());
  v8::Local<v8::Array> vec = v8::Local<v8::Array>::Cast(args[0]);
  CHECK_EQ(ELEMENT_COUNT, vec->Length());

782
  Isolate* isolate = CcTest::i_isolate();
783
  Assembler masm(AssemblerOptions{}, buffer, sizeof(buffer));
784 785

  // Remove return address from the stack for fix stack frame alignment.
786
  __ popq(rcx);
787 788

  // Store input vector on the stack.
789
  for (unsigned i = 0; i < ELEMENT_COUNT; i++) {
790 791 792 793
    __ movl(rax, Immediate(vec->Get(context, i)
                               .ToLocalChecked()
                               ->Int32Value(context)
                               .FromJust()));
794
    __ shlq(rax, Immediate(0x20));
795 796 797 798
    __ orq(rax, Immediate(vec->Get(context, ++i)
                              .ToLocalChecked()
                              ->Int32Value(context)
                              .FromJust()));
799
    __ pushq(rax);
800 801 802 803 804 805 806 807 808 809 810
  }

  // Read vector into a xmm register.
  __ xorps(xmm0, xmm0);
  __ movdqa(xmm0, Operand(rsp, 0));
  // Create mask and store it in the return register.
  __ movmskps(rax, xmm0);

  // Remove unused data from the stack.
  __ addq(rsp, Immediate(ELEMENT_COUNT * sizeof(int32_t)));
  // Restore return address.
811
  __ pushq(rcx);
812 813 814 815

  __ ret(0);

  CodeDesc desc;
816
  masm.GetCode(isolate, &desc);
817 818
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
819

820 821
  auto f = GeneratedCode<F0>::FromCode(*code);
  int res = f.Call();
822
  args.GetReturnValue().Set(v8::Integer::New(CcTest::isolate(), res));
823 824
}

825

826
TEST(StackAlignmentForSSE2) {
827
  CcTest::InitializeVM();
828
  CHECK_EQ(0, v8::base::OS::ActivationFrameAlignment() % 16);
829

830
  v8::Isolate* isolate = CcTest::isolate();
831
  v8::HandleScope handle_scope(isolate);
832
  v8::Local<v8::ObjectTemplate> global_template =
833
      v8::ObjectTemplate::New(isolate);
834 835
  global_template->Set(v8_str("do_sse2"),
                       v8::FunctionTemplate::New(isolate, DoSSE2));
836

837
  LocalContext env(nullptr, global_template);
838 839 840 841 842 843
  CompileRun(
      "function foo(vec) {"
      "  return do_sse2(vec);"
      "}");

  v8::Local<v8::Object> global_object = env->Global();
844 845
  v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
      global_object->Get(env.local(), v8_str("foo")).ToLocalChecked());
846 847

  int32_t vec[ELEMENT_COUNT] = { -1, 1, 1, 1 };
848
  v8::Local<v8::Array> v8_vec = v8::Array::New(isolate, ELEMENT_COUNT);
849
  for (unsigned i = 0; i < ELEMENT_COUNT; i++) {
850
    v8_vec->Set(env.local(), i, v8_num(vec[i])).FromJust();
851 852 853
  }

  v8::Local<v8::Value> args[] = { v8_vec };
854 855
  v8::Local<v8::Value> result =
      foo->Call(env.local(), global_object, 1, args).ToLocalChecked();
856 857

  // The mask should be 0b1000.
858
  CHECK_EQ(8, result->Int32Value(env.local()).FromJust());
859 860 861
}

#undef ELEMENT_COUNT
862
#endif  // __GNUC__
863 864


865 866 867 868 869 870 871
TEST(AssemblerX64Extractps) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(SSE4_1)) return;

  v8::HandleScope scope(CcTest::isolate());
  byte buffer[256];
  Isolate* isolate = CcTest::i_isolate();
872
  Assembler masm(AssemblerOptions{}, buffer, sizeof(buffer));
873 874
  {
    CpuFeatureScope fscope2(&masm, SSE4_1);
875 876 877 878 879
    __ extractps(rax, xmm0, 0x1);
    __ ret(0);
  }

  CodeDesc desc;
880
  masm.GetCode(isolate, &desc);
881 882
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
883
#ifdef OBJECT_PRINT
884
  StdoutStream os;
885
  code->Print(os);
886 887
#endif

888
  auto f = GeneratedCode<F3>::FromCode(*code);
889
  uint64_t value1 = V8_2PART_UINT64_C(0x12345678, 87654321);
890
  CHECK_EQ(0x12345678u, f.Call(uint64_to_double(value1)));
891
  uint64_t value2 = V8_2PART_UINT64_C(0x87654321, 12345678);
892
  CHECK_EQ(0x87654321u, f.Call(uint64_to_double(value2)));
893 894
}

895
typedef int(F6)(float x, float y);
896 897 898 899 900 901
TEST(AssemblerX64SSE) {
  CcTest::InitializeVM();

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[256];
902
  MacroAssembler masm(isolate, buffer, sizeof(buffer),
903
                      v8::internal::CodeObjectRequired::kYes);
904 905 906 907 908 909 910 911 912 913 914 915 916
  {
    __ shufps(xmm0, xmm0, 0x0);  // brocast first argument
    __ shufps(xmm1, xmm1, 0x0);  // brocast second argument
    __ movaps(xmm2, xmm1);
    __ addps(xmm2, xmm0);
    __ mulps(xmm2, xmm1);
    __ subps(xmm2, xmm0);
    __ divps(xmm2, xmm1);
    __ cvttss2si(rax, xmm2);
    __ ret(0);
  }

  CodeDesc desc;
917
  masm.GetCode(isolate, &desc);
918 919
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
920
#ifdef OBJECT_PRINT
921
  StdoutStream os;
922
  code->Print(os);
923 924
#endif

925 926
  auto f = GeneratedCode<F6>::FromCode(*code);
  CHECK_EQ(2, f.Call(1.0, 2.0));
927
}
928

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
TEST(AssemblerX64SSE3) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(SSE3)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[256];
  MacroAssembler masm(isolate, buffer, sizeof(buffer),
                      v8::internal::CodeObjectRequired::kYes);
  {
    CpuFeatureScope fscope(&masm, SSE3);
    __ shufps(xmm0, xmm0, 0x0);  // brocast first argument
    __ shufps(xmm1, xmm1, 0x0);  // brocast second argument
    __ haddps(xmm1, xmm0);
    __ cvttss2si(rax, xmm1);
    __ ret(0);
  }

  CodeDesc desc;
  masm.GetCode(isolate, &desc);
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
#ifdef OBJECT_PRINT
952
  StdoutStream os;
953
  code->Print(os);
954 955
#endif

956 957
  auto f = GeneratedCode<F6>::FromCode(*code);
  CHECK_EQ(4, f.Call(1.0, 2.0));
958
}
959

960
typedef int(F7)(double x, double y, double z);
961 962 963 964 965 966 967
TEST(AssemblerX64FMA_sd) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(FMA3)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[1024];
968
  MacroAssembler masm(isolate, buffer, sizeof(buffer),
969
                      v8::internal::CodeObjectRequired::kYes);
970
  {
971
    CpuFeatureScope fscope(&masm, FMA3);
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 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
    Label exit;
    // argument in xmm0, xmm1 and xmm2
    // xmm0 * xmm1 + xmm2
    __ movaps(xmm3, xmm0);
    __ mulsd(xmm3, xmm1);
    __ addsd(xmm3, xmm2);  // Expected result in xmm3

    __ subq(rsp, Immediate(kDoubleSize));  // For memory operand
    // vfmadd132sd
    __ movl(rax, Immediate(1));  // Test number
    __ movaps(xmm8, xmm0);
    __ vfmadd132sd(xmm8, xmm2, xmm1);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd213sd
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ vfmadd213sd(xmm8, xmm0, xmm2);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd231sd
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ vfmadd231sd(xmm8, xmm0, xmm1);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);

    // vfmadd132sd
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ movsd(Operand(rsp, 0), xmm1);
    __ vfmadd132sd(xmm8, xmm2, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd213sd
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ movsd(Operand(rsp, 0), xmm2);
    __ vfmadd213sd(xmm8, xmm0, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd231sd
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ movsd(Operand(rsp, 0), xmm1);
    __ vfmadd231sd(xmm8, xmm0, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);

    // xmm0 * xmm1 - xmm2
    __ movaps(xmm3, xmm0);
    __ mulsd(xmm3, xmm1);
    __ subsd(xmm3, xmm2);  // Expected result in xmm3

    // vfmsub132sd
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ vfmsub132sd(xmm8, xmm2, xmm1);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd213sd
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ vfmsub213sd(xmm8, xmm0, xmm2);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmsub231sd
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ vfmsub231sd(xmm8, xmm0, xmm1);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);

    // vfmsub132sd
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ movsd(Operand(rsp, 0), xmm1);
    __ vfmsub132sd(xmm8, xmm2, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmsub213sd
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ movsd(Operand(rsp, 0), xmm2);
    __ vfmsub213sd(xmm8, xmm0, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmsub231sd
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ movsd(Operand(rsp, 0), xmm1);
    __ vfmsub231sd(xmm8, xmm0, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);


    // - xmm0 * xmm1 + xmm2
    __ movaps(xmm3, xmm0);
    __ mulsd(xmm3, xmm1);
1071
    __ Move(xmm4, static_cast<uint64_t>(1) << 63);
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    __ xorpd(xmm3, xmm4);
    __ addsd(xmm3, xmm2);  // Expected result in xmm3

    // vfnmadd132sd
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ vfnmadd132sd(xmm8, xmm2, xmm1);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd213sd
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ vfnmadd213sd(xmm8, xmm0, xmm2);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmadd231sd
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ vfnmadd231sd(xmm8, xmm0, xmm1);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);

    // vfnmadd132sd
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ movsd(Operand(rsp, 0), xmm1);
    __ vfnmadd132sd(xmm8, xmm2, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmadd213sd
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ movsd(Operand(rsp, 0), xmm2);
    __ vfnmadd213sd(xmm8, xmm0, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmadd231sd
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ movsd(Operand(rsp, 0), xmm1);
    __ vfnmadd231sd(xmm8, xmm0, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);


    // - xmm0 * xmm1 - xmm2
    __ movaps(xmm3, xmm0);
    __ mulsd(xmm3, xmm1);
1120
    __ Move(xmm4, static_cast<uint64_t>(1) << 63);
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
    __ xorpd(xmm3, xmm4);
    __ subsd(xmm3, xmm2);  // Expected result in xmm3

    // vfnmsub132sd
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ vfnmsub132sd(xmm8, xmm2, xmm1);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmsub213sd
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ vfnmsub213sd(xmm8, xmm0, xmm2);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmsub231sd
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ vfnmsub231sd(xmm8, xmm0, xmm1);
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);

    // vfnmsub132sd
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ movsd(Operand(rsp, 0), xmm1);
    __ vfnmsub132sd(xmm8, xmm2, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmsub213sd
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ movsd(Operand(rsp, 0), xmm2);
    __ vfnmsub213sd(xmm8, xmm0, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmsub231sd
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ movsd(Operand(rsp, 0), xmm1);
    __ vfnmsub231sd(xmm8, xmm0, Operand(rsp, 0));
    __ ucomisd(xmm8, xmm3);
    __ j(not_equal, &exit);


    __ xorl(rax, rax);
    __ bind(&exit);
    __ addq(rsp, Immediate(kDoubleSize));
    __ ret(0);
  }

  CodeDesc desc;
1173
  masm.GetCode(isolate, &desc);
1174 1175
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
1176
#ifdef OBJECT_PRINT
1177
  StdoutStream os;
1178
  code->Print(os);
1179 1180
#endif

1181 1182 1183
  auto f = GeneratedCode<F7>::FromCode(*code);
  CHECK_EQ(
      0, f.Call(0.000092662107262076, -2.460774966188315, -1.0958787393627414));
1184 1185
}

1186
typedef int(F8)(float x, float y, float z);
1187 1188 1189 1190 1191 1192 1193
TEST(AssemblerX64FMA_ss) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(FMA3)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[1024];
1194
  MacroAssembler masm(isolate, buffer, sizeof(buffer),
1195
                      v8::internal::CodeObjectRequired::kYes);
1196
  {
1197
    CpuFeatureScope fscope(&masm, FMA3);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 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 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    Label exit;
    // arguments in xmm0, xmm1 and xmm2
    // xmm0 * xmm1 + xmm2
    __ movaps(xmm3, xmm0);
    __ mulss(xmm3, xmm1);
    __ addss(xmm3, xmm2);  // Expected result in xmm3

    __ subq(rsp, Immediate(kDoubleSize));  // For memory operand
    // vfmadd132ss
    __ movl(rax, Immediate(1));  // Test number
    __ movaps(xmm8, xmm0);
    __ vfmadd132ss(xmm8, xmm2, xmm1);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd213ss
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ vfmadd213ss(xmm8, xmm0, xmm2);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd231ss
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ vfmadd231ss(xmm8, xmm0, xmm1);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);

    // vfmadd132ss
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ movss(Operand(rsp, 0), xmm1);
    __ vfmadd132ss(xmm8, xmm2, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd213ss
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ movss(Operand(rsp, 0), xmm2);
    __ vfmadd213ss(xmm8, xmm0, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd231ss
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ movss(Operand(rsp, 0), xmm1);
    __ vfmadd231ss(xmm8, xmm0, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);

    // xmm0 * xmm1 - xmm2
    __ movaps(xmm3, xmm0);
    __ mulss(xmm3, xmm1);
    __ subss(xmm3, xmm2);  // Expected result in xmm3

    // vfmsub132ss
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ vfmsub132ss(xmm8, xmm2, xmm1);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd213ss
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ vfmsub213ss(xmm8, xmm0, xmm2);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmsub231ss
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ vfmsub231ss(xmm8, xmm0, xmm1);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);

    // vfmsub132ss
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ movss(Operand(rsp, 0), xmm1);
    __ vfmsub132ss(xmm8, xmm2, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmsub213ss
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ movss(Operand(rsp, 0), xmm2);
    __ vfmsub213ss(xmm8, xmm0, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmsub231ss
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ movss(Operand(rsp, 0), xmm1);
    __ vfmsub231ss(xmm8, xmm0, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);


    // - xmm0 * xmm1 + xmm2
    __ movaps(xmm3, xmm0);
    __ mulss(xmm3, xmm1);
1297
    __ Move(xmm4, static_cast<uint32_t>(1) << 31);
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 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 1340 1341 1342 1343 1344 1345
    __ xorps(xmm3, xmm4);
    __ addss(xmm3, xmm2);  // Expected result in xmm3

    // vfnmadd132ss
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ vfnmadd132ss(xmm8, xmm2, xmm1);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmadd213ss
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ vfnmadd213ss(xmm8, xmm0, xmm2);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmadd231ss
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ vfnmadd231ss(xmm8, xmm0, xmm1);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);

    // vfnmadd132ss
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ movss(Operand(rsp, 0), xmm1);
    __ vfnmadd132ss(xmm8, xmm2, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmadd213ss
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ movss(Operand(rsp, 0), xmm2);
    __ vfnmadd213ss(xmm8, xmm0, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmadd231ss
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ movss(Operand(rsp, 0), xmm1);
    __ vfnmadd231ss(xmm8, xmm0, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);


    // - xmm0 * xmm1 - xmm2
    __ movaps(xmm3, xmm0);
    __ mulss(xmm3, xmm1);
1346
    __ Move(xmm4, static_cast<uint32_t>(1) << 31);
1347 1348 1349 1350 1351 1352 1353 1354 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 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
    __ xorps(xmm3, xmm4);
    __ subss(xmm3, xmm2);  // Expected result in xmm3

    // vfnmsub132ss
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ vfnmsub132ss(xmm8, xmm2, xmm1);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfmsub213ss
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ vfnmsub213ss(xmm8, xmm0, xmm2);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmsub231ss
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ vfnmsub231ss(xmm8, xmm0, xmm1);
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);

    // vfnmsub132ss
    __ incq(rax);
    __ movaps(xmm8, xmm0);
    __ movss(Operand(rsp, 0), xmm1);
    __ vfnmsub132ss(xmm8, xmm2, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmsub213ss
    __ incq(rax);
    __ movaps(xmm8, xmm1);
    __ movss(Operand(rsp, 0), xmm2);
    __ vfnmsub213ss(xmm8, xmm0, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);
    // vfnmsub231ss
    __ incq(rax);
    __ movaps(xmm8, xmm2);
    __ movss(Operand(rsp, 0), xmm1);
    __ vfnmsub231ss(xmm8, xmm0, Operand(rsp, 0));
    __ ucomiss(xmm8, xmm3);
    __ j(not_equal, &exit);


    __ xorl(rax, rax);
    __ bind(&exit);
    __ addq(rsp, Immediate(kDoubleSize));
    __ ret(0);
  }

  CodeDesc desc;
1399
  masm.GetCode(isolate, &desc);
1400 1401
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
1402
#ifdef OBJECT_PRINT
1403
  StdoutStream os;
1404
  code->Print(os);
1405 1406
#endif

1407 1408
  auto f = GeneratedCode<F8>::FromCode(*code);
  CHECK_EQ(0, f.Call(9.26621069e-05f, -2.4607749f, -1.09587872f));
1409
}
1410 1411


1412 1413 1414 1415 1416 1417
TEST(AssemblerX64SSE_ss) {
  CcTest::InitializeVM();

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[1024];
1418
  Assembler masm(AssemblerOptions{}, buffer, sizeof(buffer));
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
  {
    Label exit;
    // arguments in xmm0, xmm1 and xmm2
    __ movl(rax, Immediate(0));

    __ movaps(xmm3, xmm0);
    __ maxss(xmm3, xmm1);
    __ ucomiss(xmm3, xmm1);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(1));

    __ movaps(xmm3, xmm1);
    __ minss(xmm3, xmm2);
    __ ucomiss(xmm3, xmm1);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(2));

    __ movaps(xmm3, xmm2);
    __ subss(xmm3, xmm1);
    __ ucomiss(xmm3, xmm0);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(3));

    __ movaps(xmm3, xmm0);
    __ addss(xmm3, xmm1);
    __ ucomiss(xmm3, xmm2);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(4));

    __ movaps(xmm3, xmm0);
    __ mulss(xmm3, xmm1);
    __ ucomiss(xmm3, xmm1);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(5));

    __ movaps(xmm3, xmm0);
    __ divss(xmm3, xmm1);
    __ mulss(xmm3, xmm2);
    __ mulss(xmm3, xmm1);
    __ ucomiss(xmm3, xmm2);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(6));

    // result in eax
    __ bind(&exit);
    __ ret(0);
  }

  CodeDesc desc;
1474
  masm.GetCode(isolate, &desc);
1475 1476
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
1477
#ifdef OBJECT_PRINT
1478
  StdoutStream os;
1479
  code->Print(os);
1480 1481
#endif

1482 1483
  auto f = GeneratedCode<F8>::FromCode(*code);
  int res = f.Call(1.0f, 2.0f, 3.0f);
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
  PrintF("f(1,2,3) = %d\n", res);
  CHECK_EQ(6, res);
}


TEST(AssemblerX64AVX_ss) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(AVX)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[1024];
1496
  Assembler masm(AssemblerOptions{}, buffer, sizeof(buffer));
1497
  {
1498
    CpuFeatureScope avx_scope(&masm, AVX);
1499 1500
    Label exit;
    // arguments in xmm0, xmm1 and xmm2
1501
    __ subq(rsp, Immediate(kDoubleSize * 2));  // For memory operand
1502

1503
    __ movl(rdx, Immediate(0xC2F64000));  // -123.125
1504 1505 1506
    __ vmovd(xmm4, rdx);
    __ vmovss(Operand(rsp, 0), xmm4);
    __ vmovss(xmm5, Operand(rsp, 0));
1507 1508
    __ vmovaps(xmm6, xmm5);
    __ vmovd(rcx, xmm6);
1509 1510 1511 1512 1513
    __ cmpl(rcx, rdx);
    __ movl(rax, Immediate(9));
    __ j(not_equal, &exit);

    __ movl(rax, Immediate(0));
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
    __ vmaxss(xmm3, xmm0, xmm1);
    __ vucomiss(xmm3, xmm1);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(1));

    __ vminss(xmm3, xmm1, xmm2);
    __ vucomiss(xmm3, xmm1);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(2));

    __ vsubss(xmm3, xmm2, xmm1);
    __ vucomiss(xmm3, xmm0);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(3));

    __ vaddss(xmm3, xmm0, xmm1);
    __ vucomiss(xmm3, xmm2);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(4));

    __ vmulss(xmm3, xmm0, xmm1);
    __ vucomiss(xmm3, xmm1);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(5));

    __ vdivss(xmm3, xmm0, xmm1);
    __ vmulss(xmm3, xmm3, xmm2);
    __ vmulss(xmm3, xmm3, xmm1);
    __ vucomiss(xmm3, xmm2);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(6));

    // result in eax
    __ bind(&exit);
1554
    __ addq(rsp, Immediate(kDoubleSize * 2));
1555 1556 1557 1558
    __ ret(0);
  }

  CodeDesc desc;
1559
  masm.GetCode(isolate, &desc);
1560 1561
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
1562
#ifdef OBJECT_PRINT
1563
  StdoutStream os;
1564
  code->Print(os);
1565 1566
#endif

1567 1568
  auto f = GeneratedCode<F8>::FromCode(*code);
  int res = f.Call(1.0f, 2.0f, 3.0f);
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
  PrintF("f(1,2,3) = %d\n", res);
  CHECK_EQ(6, res);
}


TEST(AssemblerX64AVX_sd) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(AVX)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[1024];
1581
  Assembler masm(AssemblerOptions{}, buffer, sizeof(buffer));
1582
  {
1583
    CpuFeatureScope avx_scope(&masm, AVX);
1584 1585
    Label exit;
    // arguments in xmm0, xmm1 and xmm2
1586
    __ subq(rsp, Immediate(kDoubleSize * 2));  // For memory operand
1587 1588
    __ movl(rax, Immediate(0));

1589 1590 1591
    __ vmaxsd(xmm4, xmm0, xmm1);
    __ vmovsd(Operand(rsp, kDoubleSize), xmm4);
    __ vmovsd(xmm5, Operand(rsp, kDoubleSize));
1592
    __ vmovsd(xmm6, xmm6, xmm5);
1593
    __ vmovapd(xmm3, xmm6);
1594

1595 1596
    // Test vcvtss2sd & vcvtsd2ss
    __ movl(rax, Immediate(9));
1597
    __ movq(rdx, uint64_t{0x426D1A0000000000});
1598 1599 1600 1601
    __ movq(Operand(rsp, 0), rdx);
    __ vcvtsd2ss(xmm6, xmm6, Operand(rsp, 0));
    __ vcvtss2sd(xmm7, xmm6, xmm6);
    __ vcvtsd2ss(xmm8, xmm7, xmm7);
1602
    __ vmovss(Operand(rsp, 0), xmm8);
1603 1604 1605 1606 1607
    __ vcvtss2sd(xmm9, xmm8, Operand(rsp, 0));
    __ vmovq(rcx, xmm9);
    __ cmpq(rcx, rdx);
    __ j(not_equal, &exit);

1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
    // Test vcvttsd2si
    __ movl(rax, Immediate(10));
    __ movl(rdx, Immediate(123));
    __ vcvtlsi2sd(xmm6, xmm6, rdx);
    __ vcvttsd2si(rcx, xmm6);
    __ cmpl(rcx, rdx);
    __ j(not_equal, &exit);
    __ xorl(rcx, rcx);
    __ vmovsd(Operand(rsp, 0), xmm6);
    __ vcvttsd2si(rcx, Operand(rsp, 0));
    __ cmpl(rcx, rdx);
    __ j(not_equal, &exit);

    // Test vcvttsd2siq
    __ movl(rax, Immediate(11));
1623
    __ movq(rdx, uint64_t{0x426D1A94A2000000});  // 1.0e12
1624 1625
    __ vmovq(xmm6, rdx);
    __ vcvttsd2siq(rcx, xmm6);
1626
    __ movq(rdx, uint64_t{1000000000000});
1627 1628 1629 1630 1631 1632 1633 1634
    __ cmpq(rcx, rdx);
    __ j(not_equal, &exit);
    __ xorq(rcx, rcx);
    __ vmovsd(Operand(rsp, 0), xmm6);
    __ vcvttsd2siq(rcx, Operand(rsp, 0));
    __ cmpq(rcx, rdx);
    __ j(not_equal, &exit);

1635 1636
    // Test vmovmskpd
    __ movl(rax, Immediate(12));
1637
    __ movq(rdx, uint64_t{0x426D1A94A2000000});  // 1.0e12
1638
    __ vmovq(xmm6, rdx);
1639
    __ movq(rdx, uint64_t{0xC26D1A94A2000000});  // -1.0e12
1640 1641 1642 1643 1644 1645
    __ vmovq(xmm7, rdx);
    __ shufps(xmm6, xmm7, 0x44);
    __ vmovmskpd(rdx, xmm6);
    __ cmpl(rdx, Immediate(2));
    __ j(not_equal, &exit);

1646
    // Test vpcmpeqd
1647
    __ movq(rdx, uint64_t{0x0123456789ABCDEF});
1648
    __ movq(rcx, uint64_t{0x0123456788888888});
1649 1650 1651 1652
    __ vmovq(xmm6, rdx);
    __ vmovq(xmm7, rcx);
    __ vpcmpeqd(xmm8, xmm6, xmm7);
    __ vmovq(rdx, xmm8);
1653
    __ movq(rcx, uint64_t{0xFFFFFFFF00000000});
1654 1655 1656 1657 1658 1659
    __ cmpq(rcx, rdx);
    __ movl(rax, Immediate(13));
    __ j(not_equal, &exit);

    // Test vpsllq, vpsrlq
    __ movl(rax, Immediate(13));
1660
    __ movq(rdx, uint64_t{0x0123456789ABCDEF});
1661 1662 1663
    __ vmovq(xmm6, rdx);
    __ vpsrlq(xmm7, xmm6, 4);
    __ vmovq(rdx, xmm7);
1664
    __ movq(rcx, uint64_t{0x00123456789ABCDE});
1665 1666 1667 1668
    __ cmpq(rdx, rcx);
    __ j(not_equal, &exit);
    __ vpsllq(xmm7, xmm6, 12);
    __ vmovq(rdx, xmm7);
1669
    __ movq(rcx, uint64_t{0x3456789ABCDEF000});
1670 1671 1672
    __ cmpq(rdx, rcx);
    __ j(not_equal, &exit);

1673 1674
    // Test vandpd, vorpd, vxorpd
    __ movl(rax, Immediate(14));
1675 1676
    __ movl(rdx, Immediate(0x00FF00FF));
    __ movl(rcx, Immediate(0x0F0F0F0F));
1677 1678 1679 1680
    __ vmovd(xmm4, rdx);
    __ vmovd(xmm5, rcx);
    __ vandpd(xmm6, xmm4, xmm5);
    __ vmovd(rdx, xmm6);
1681
    __ cmpl(rdx, Immediate(0x000F000F));
1682 1683 1684
    __ j(not_equal, &exit);
    __ vorpd(xmm6, xmm4, xmm5);
    __ vmovd(rdx, xmm6);
1685
    __ cmpl(rdx, Immediate(0x0FFF0FFF));
1686 1687 1688
    __ j(not_equal, &exit);
    __ vxorpd(xmm6, xmm4, xmm5);
    __ vmovd(rdx, xmm6);
1689
    __ cmpl(rdx, Immediate(0x0FF00FF0));
1690 1691
    __ j(not_equal, &exit);

1692 1693
    // Test vsqrtsd
    __ movl(rax, Immediate(15));
1694
    __ movq(rdx, uint64_t{0x4004000000000000});  // 2.5
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
    __ vmovq(xmm4, rdx);
    __ vmulsd(xmm5, xmm4, xmm4);
    __ vmovsd(Operand(rsp, 0), xmm5);
    __ vsqrtsd(xmm6, xmm5, xmm5);
    __ vmovq(rcx, xmm6);
    __ cmpq(rcx, rdx);
    __ j(not_equal, &exit);
    __ vsqrtsd(xmm7, xmm7, Operand(rsp, 0));
    __ vmovq(rcx, xmm7);
    __ cmpq(rcx, rdx);
    __ j(not_equal, &exit);

1707 1708
    // Test vroundsd
    __ movl(rax, Immediate(16));
1709
    __ movq(rdx, uint64_t{0x4002000000000000});  // 2.25
1710 1711
    __ vmovq(xmm4, rdx);
    __ vroundsd(xmm5, xmm4, xmm4, kRoundUp);
1712
    __ movq(rcx, uint64_t{0x4008000000000000});  // 3.0
1713 1714 1715 1716
    __ vmovq(xmm6, rcx);
    __ vucomisd(xmm5, xmm6);
    __ j(not_equal, &exit);

1717 1718
    // Test vcvtlsi2sd
    __ movl(rax, Immediate(17));
1719
    __ movl(rdx, Immediate(6));
1720
    __ movq(rcx, uint64_t{0x4018000000000000});  // 6.0
1721
    __ vmovq(xmm5, rcx);
1722
    __ vcvtlsi2sd(xmm6, xmm6, rdx);
1723 1724 1725
    __ vucomisd(xmm5, xmm6);
    __ j(not_equal, &exit);
    __ movl(Operand(rsp, 0), rdx);
1726
    __ vcvtlsi2sd(xmm7, xmm7, Operand(rsp, 0));
1727 1728 1729 1730 1731
    __ vucomisd(xmm5, xmm6);
    __ j(not_equal, &exit);

    // Test vcvtqsi2sd
    __ movl(rax, Immediate(18));
1732 1733
    __ movq(rdx, uint64_t{0x2000000000000000});  // 2 << 0x3C
    __ movq(rcx, uint64_t{0x43C0000000000000});
1734 1735 1736 1737
    __ vmovq(xmm5, rcx);
    __ vcvtqsi2sd(xmm6, xmm6, rdx);
    __ vucomisd(xmm5, xmm6);
    __ j(not_equal, &exit);
1738

1739 1740
    // Test vcvtsd2si
    __ movl(rax, Immediate(19));
1741
    __ movq(rdx, uint64_t{0x4018000000000000});  // 6.0
1742 1743 1744 1745 1746
    __ vmovq(xmm5, rdx);
    __ vcvtsd2si(rcx, xmm5);
    __ cmpl(rcx, Immediate(6));
    __ j(not_equal, &exit);

1747
    __ movq(rdx, uint64_t{0x3FF0000000000000});  // 1.0
1748 1749 1750 1751 1752
    __ vmovq(xmm7, rdx);
    __ vmulsd(xmm1, xmm1, xmm7);
    __ movq(Operand(rsp, 0), rdx);
    __ vmovq(xmm6, Operand(rsp, 0));
    __ vmulsd(xmm1, xmm1, xmm6);
1753

1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
    __ vucomisd(xmm3, xmm1);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(1));

    __ vminsd(xmm3, xmm1, xmm2);
    __ vucomisd(xmm3, xmm1);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(2));

    __ vsubsd(xmm3, xmm2, xmm1);
    __ vucomisd(xmm3, xmm0);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(3));

    __ vaddsd(xmm3, xmm0, xmm1);
    __ vucomisd(xmm3, xmm2);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(4));

    __ vmulsd(xmm3, xmm0, xmm1);
    __ vucomisd(xmm3, xmm1);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(5));

    __ vdivsd(xmm3, xmm0, xmm1);
    __ vmulsd(xmm3, xmm3, xmm2);
    __ vmulsd(xmm3, xmm3, xmm1);
    __ vucomisd(xmm3, xmm2);
    __ j(parity_even, &exit);
    __ j(not_equal, &exit);
    __ movl(rax, Immediate(6));

    // result in eax
    __ bind(&exit);
1793
    __ addq(rsp, Immediate(kDoubleSize * 2));
1794 1795 1796 1797
    __ ret(0);
  }

  CodeDesc desc;
1798
  masm.GetCode(isolate, &desc);
1799 1800
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
1801
#ifdef OBJECT_PRINT
1802
  StdoutStream os;
1803
  code->Print(os);
1804 1805
#endif

1806 1807
  auto f = GeneratedCode<F7>::FromCode(*code);
  int res = f.Call(1.0, 2.0, 3.0);
1808 1809 1810 1811 1812
  PrintF("f(1,2,3) = %d\n", res);
  CHECK_EQ(6, res);
}


1813 1814 1815 1816 1817 1818 1819
TEST(AssemblerX64BMI1) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(BMI1)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[1024];
1820
  MacroAssembler masm(isolate, buffer, sizeof(buffer),
1821
                      v8::internal::CodeObjectRequired::kYes);
1822
  {
1823
    CpuFeatureScope fscope(&masm, BMI1);
1824 1825
    Label exit;

1826
    __ movq(rcx, uint64_t{0x1122334455667788});     // source operand
1827 1828 1829
    __ pushq(rcx);                                  // For memory operand

    // andn
1830
    __ movq(rdx, uint64_t{0x1000000020000000});
1831 1832 1833

    __ movl(rax, Immediate(1));  // Test number
    __ andnq(r8, rdx, rcx);
1834
    __ movq(r9, uint64_t{0x0122334455667788});  // expected result
1835 1836 1837 1838 1839
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ andnq(r8, rdx, Operand(rsp, 0));
1840
    __ movq(r9, uint64_t{0x0122334455667788});  // expected result
1841 1842 1843 1844 1845
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ andnl(r8, rdx, rcx);
1846
    __ movq(r9, uint64_t{0x0000000055667788});  // expected result
1847 1848 1849 1850 1851
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ andnl(r8, rdx, Operand(rsp, 0));
1852
    __ movq(r9, uint64_t{0x0000000055667788});  // expected result
1853 1854 1855 1856
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // bextr
1857
    __ movq(rdx, uint64_t{0x0000000000002808});
1858 1859 1860

    __ incq(rax);
    __ bextrq(r8, rcx, rdx);
1861
    __ movq(r9, uint64_t{0x0000003344556677});  // expected result
1862 1863 1864 1865 1866
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ bextrq(r8, Operand(rsp, 0), rdx);
1867
    __ movq(r9, uint64_t{0x0000003344556677});  // expected result
1868 1869 1870 1871 1872
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ bextrl(r8, rcx, rdx);
1873
    __ movq(r9, uint64_t{0x0000000000556677});  // expected result
1874 1875 1876 1877 1878
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ bextrl(r8, Operand(rsp, 0), rdx);
1879
    __ movq(r9, uint64_t{0x0000000000556677});  // expected result
1880 1881 1882 1883 1884 1885
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // blsi
    __ incq(rax);
    __ blsiq(r8, rcx);
1886
    __ movq(r9, uint64_t{0x0000000000000008});  // expected result
1887 1888 1889 1890 1891
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ blsiq(r8, Operand(rsp, 0));
1892
    __ movq(r9, uint64_t{0x0000000000000008});  // expected result
1893 1894 1895 1896 1897
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ blsil(r8, rcx);
1898
    __ movq(r9, uint64_t{0x0000000000000008});  // expected result
1899 1900 1901 1902 1903
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ blsil(r8, Operand(rsp, 0));
1904
    __ movq(r9, uint64_t{0x0000000000000008});  // expected result
1905 1906 1907 1908 1909 1910
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // blsmsk
    __ incq(rax);
    __ blsmskq(r8, rcx);
1911
    __ movq(r9, uint64_t{0x000000000000000F});  // expected result
1912 1913 1914 1915 1916
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ blsmskq(r8, Operand(rsp, 0));
1917
    __ movq(r9, uint64_t{0x000000000000000F});  // expected result
1918 1919 1920 1921 1922
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ blsmskl(r8, rcx);
1923
    __ movq(r9, uint64_t{0x000000000000000F});  // expected result
1924 1925 1926 1927 1928
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ blsmskl(r8, Operand(rsp, 0));
1929
    __ movq(r9, uint64_t{0x000000000000000F});  // expected result
1930 1931 1932 1933 1934 1935
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // blsr
    __ incq(rax);
    __ blsrq(r8, rcx);
1936
    __ movq(r9, uint64_t{0x1122334455667780});  // expected result
1937 1938 1939 1940 1941
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ blsrq(r8, Operand(rsp, 0));
1942
    __ movq(r9, uint64_t{0x1122334455667780});  // expected result
1943 1944 1945 1946 1947
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ blsrl(r8, rcx);
1948
    __ movq(r9, uint64_t{0x0000000055667780});  // expected result
1949 1950 1951 1952 1953
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ blsrl(r8, Operand(rsp, 0));
1954
    __ movq(r9, uint64_t{0x0000000055667780});  // expected result
1955 1956 1957 1958 1959 1960
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // tzcnt
    __ incq(rax);
    __ tzcntq(r8, rcx);
1961
    __ movq(r9, uint64_t{0x0000000000000003});  // expected result
1962 1963 1964 1965 1966
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ tzcntq(r8, Operand(rsp, 0));
1967
    __ movq(r9, uint64_t{0x0000000000000003});  // expected result
1968 1969 1970 1971 1972
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ tzcntl(r8, rcx);
1973
    __ movq(r9, uint64_t{0x0000000000000003});  // expected result
1974 1975 1976 1977 1978
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ tzcntl(r8, Operand(rsp, 0));
1979
    __ movq(r9, uint64_t{0x0000000000000003});  // expected result
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ xorl(rax, rax);
    __ bind(&exit);
    __ popq(rcx);
    __ ret(0);
  }

  CodeDesc desc;
1990
  masm.GetCode(isolate, &desc);
1991 1992
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
1993
#ifdef OBJECT_PRINT
1994
  StdoutStream os;
1995
  code->Print(os);
1996 1997
#endif

1998 1999
  auto f = GeneratedCode<F0>::FromCode(*code);
  CHECK_EQ(0, f.Call());
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
}


TEST(AssemblerX64LZCNT) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(LZCNT)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[256];
2010
  MacroAssembler masm(isolate, buffer, sizeof(buffer),
2011
                      v8::internal::CodeObjectRequired::kYes);
2012
  {
2013
    CpuFeatureScope fscope(&masm, LZCNT);
2014 2015
    Label exit;

2016
    __ movq(rcx, uint64_t{0x1122334455667788});     // source operand
2017 2018 2019 2020
    __ pushq(rcx);                                  // For memory operand

    __ movl(rax, Immediate(1));  // Test number
    __ lzcntq(r8, rcx);
2021
    __ movq(r9, uint64_t{0x0000000000000003});  // expected result
2022 2023 2024 2025 2026
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ lzcntq(r8, Operand(rsp, 0));
2027
    __ movq(r9, uint64_t{0x0000000000000003});  // expected result
2028 2029 2030 2031 2032
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ lzcntl(r8, rcx);
2033
    __ movq(r9, uint64_t{0x0000000000000001});  // expected result
2034 2035 2036 2037 2038
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ lzcntl(r8, Operand(rsp, 0));
2039
    __ movq(r9, uint64_t{0x0000000000000001});  // expected result
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ xorl(rax, rax);
    __ bind(&exit);
    __ popq(rcx);
    __ ret(0);
  }

  CodeDesc desc;
2050
  masm.GetCode(isolate, &desc);
2051 2052
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
2053
#ifdef OBJECT_PRINT
2054
  StdoutStream os;
2055
  code->Print(os);
2056 2057
#endif

2058 2059
  auto f = GeneratedCode<F0>::FromCode(*code);
  CHECK_EQ(0, f.Call());
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
}


TEST(AssemblerX64POPCNT) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(POPCNT)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[256];
2070
  MacroAssembler masm(isolate, buffer, sizeof(buffer),
2071
                      v8::internal::CodeObjectRequired::kYes);
2072
  {
2073
    CpuFeatureScope fscope(&masm, POPCNT);
2074 2075
    Label exit;

2076
    __ movq(rcx, uint64_t{0x1111111111111100});     // source operand
2077 2078 2079 2080
    __ pushq(rcx);                                  // For memory operand

    __ movl(rax, Immediate(1));  // Test number
    __ popcntq(r8, rcx);
2081
    __ movq(r9, uint64_t{0x000000000000000E});  // expected result
2082 2083 2084 2085 2086
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ popcntq(r8, Operand(rsp, 0));
2087
    __ movq(r9, uint64_t{0x000000000000000E});  // expected result
2088 2089 2090 2091 2092
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ popcntl(r8, rcx);
2093
    __ movq(r9, uint64_t{0x0000000000000006});  // expected result
2094 2095 2096 2097 2098
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ popcntl(r8, Operand(rsp, 0));
2099
    __ movq(r9, uint64_t{0x0000000000000006});  // expected result
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ xorl(rax, rax);
    __ bind(&exit);
    __ popq(rcx);
    __ ret(0);
  }

  CodeDesc desc;
2110
  masm.GetCode(isolate, &desc);
2111 2112
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
2113
#ifdef OBJECT_PRINT
2114
  StdoutStream os;
2115
  code->Print(os);
2116 2117
#endif

2118 2119
  auto f = GeneratedCode<F0>::FromCode(*code);
  CHECK_EQ(0, f.Call());
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
}


TEST(AssemblerX64BMI2) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(BMI2)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[2048];
2130
  MacroAssembler masm(isolate, buffer, sizeof(buffer),
2131
                      v8::internal::CodeObjectRequired::kYes);
2132
  {
2133
    CpuFeatureScope fscope(&masm, BMI2);
2134 2135
    Label exit;
    __ pushq(rbx);                                  // save rbx
2136
    __ movq(rcx, uint64_t{0x1122334455667788});     // source operand
2137 2138 2139
    __ pushq(rcx);                                  // For memory operand

    // bzhi
2140
    __ movq(rdx, uint64_t{0x0000000000000009});
2141 2142 2143

    __ movl(rax, Immediate(1));  // Test number
    __ bzhiq(r8, rcx, rdx);
2144
    __ movq(r9, uint64_t{0x0000000000000188});  // expected result
2145 2146 2147 2148 2149
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ bzhiq(r8, Operand(rsp, 0), rdx);
2150
    __ movq(r9, uint64_t{0x0000000000000188});  // expected result
2151 2152 2153 2154 2155
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ bzhil(r8, rcx, rdx);
2156
    __ movq(r9, uint64_t{0x0000000000000188});  // expected result
2157 2158 2159 2160 2161
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ bzhil(r8, Operand(rsp, 0), rdx);
2162
    __ movq(r9, uint64_t{0x0000000000000188});  // expected result
2163 2164 2165 2166
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // mulx
2167
    __ movq(rdx, uint64_t{0x0000000000001000});
2168 2169 2170

    __ incq(rax);
    __ mulxq(r8, r9, rcx);
2171
    __ movq(rbx, uint64_t{0x0000000000000112});  // expected result
2172 2173
    __ cmpq(r8, rbx);
    __ j(not_equal, &exit);
2174
    __ movq(rbx, uint64_t{0x2334455667788000});  // expected result
2175 2176 2177 2178 2179
    __ cmpq(r9, rbx);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ mulxq(r8, r9, Operand(rsp, 0));
2180
    __ movq(rbx, uint64_t{0x0000000000000112});  // expected result
2181 2182
    __ cmpq(r8, rbx);
    __ j(not_equal, &exit);
2183
    __ movq(rbx, uint64_t{0x2334455667788000});  // expected result
2184 2185 2186 2187 2188
    __ cmpq(r9, rbx);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ mulxl(r8, r9, rcx);
2189
    __ movq(rbx, uint64_t{0x0000000000000556});  // expected result
2190 2191
    __ cmpq(r8, rbx);
    __ j(not_equal, &exit);
2192
    __ movq(rbx, uint64_t{0x0000000067788000});  // expected result
2193 2194 2195 2196 2197
    __ cmpq(r9, rbx);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ mulxl(r8, r9, Operand(rsp, 0));
2198
    __ movq(rbx, uint64_t{0x0000000000000556});  // expected result
2199 2200
    __ cmpq(r8, rbx);
    __ j(not_equal, &exit);
2201
    __ movq(rbx, uint64_t{0x0000000067788000});  // expected result
2202 2203 2204 2205
    __ cmpq(r9, rbx);
    __ j(not_equal, &exit);

    // pdep
2206
    __ movq(rdx, uint64_t{0xFFFFFFFFFFFFFFF0});
2207 2208 2209

    __ incq(rax);
    __ pdepq(r8, rdx, rcx);
2210
    __ movq(r9, uint64_t{0x1122334455667400});  // expected result
2211 2212 2213 2214 2215
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ pdepq(r8, rdx, Operand(rsp, 0));
2216
    __ movq(r9, uint64_t{0x1122334455667400});  // expected result
2217 2218 2219 2220 2221
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ pdepl(r8, rdx, rcx);
2222
    __ movq(r9, uint64_t{0x0000000055667400});  // expected result
2223 2224 2225 2226 2227
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ pdepl(r8, rdx, Operand(rsp, 0));
2228
    __ movq(r9, uint64_t{0x0000000055667400});  // expected result
2229 2230 2231 2232
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // pext
2233
    __ movq(rdx, uint64_t{0xFFFFFFFFFFFFFFF0});
2234 2235 2236

    __ incq(rax);
    __ pextq(r8, rdx, rcx);
2237
    __ movq(r9, uint64_t{0x0000000003FFFFFE});  // expected result
2238 2239 2240 2241 2242
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ pextq(r8, rdx, Operand(rsp, 0));
2243
    __ movq(r9, uint64_t{0x0000000003FFFFFE});  // expected result
2244 2245 2246 2247 2248
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ pextl(r8, rdx, rcx);
2249
    __ movq(r9, uint64_t{0x000000000000FFFE});  // expected result
2250 2251 2252 2253 2254
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ pextl(r8, rdx, Operand(rsp, 0));
2255
    __ movq(r9, uint64_t{0x000000000000FFFE});  // expected result
2256 2257 2258 2259
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // sarx
2260
    __ movq(rdx, uint64_t{0x0000000000000004});
2261 2262 2263

    __ incq(rax);
    __ sarxq(r8, rcx, rdx);
2264
    __ movq(r9, uint64_t{0x0112233445566778});  // expected result
2265 2266 2267 2268 2269
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ sarxq(r8, Operand(rsp, 0), rdx);
2270
    __ movq(r9, uint64_t{0x0112233445566778});  // expected result
2271 2272 2273 2274 2275
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ sarxl(r8, rcx, rdx);
2276
    __ movq(r9, uint64_t{0x0000000005566778});  // expected result
2277 2278 2279 2280 2281
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ sarxl(r8, Operand(rsp, 0), rdx);
2282
    __ movq(r9, uint64_t{0x0000000005566778});  // expected result
2283 2284 2285 2286
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // shlx
2287
    __ movq(rdx, uint64_t{0x0000000000000004});
2288 2289 2290

    __ incq(rax);
    __ shlxq(r8, rcx, rdx);
2291
    __ movq(r9, uint64_t{0x1223344556677880});  // expected result
2292 2293 2294 2295 2296
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ shlxq(r8, Operand(rsp, 0), rdx);
2297
    __ movq(r9, uint64_t{0x1223344556677880});  // expected result
2298 2299 2300 2301 2302
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ shlxl(r8, rcx, rdx);
2303
    __ movq(r9, uint64_t{0x0000000056677880});  // expected result
2304 2305 2306 2307 2308
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ shlxl(r8, Operand(rsp, 0), rdx);
2309
    __ movq(r9, uint64_t{0x0000000056677880});  // expected result
2310 2311 2312 2313
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // shrx
2314
    __ movq(rdx, uint64_t{0x0000000000000004});
2315 2316 2317

    __ incq(rax);
    __ shrxq(r8, rcx, rdx);
2318
    __ movq(r9, uint64_t{0x0112233445566778});  // expected result
2319 2320 2321 2322 2323
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ shrxq(r8, Operand(rsp, 0), rdx);
2324
    __ movq(r9, uint64_t{0x0112233445566778});  // expected result
2325 2326 2327 2328 2329
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ shrxl(r8, rcx, rdx);
2330
    __ movq(r9, uint64_t{0x0000000005566778});  // expected result
2331 2332 2333 2334 2335
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ shrxl(r8, Operand(rsp, 0), rdx);
2336
    __ movq(r9, uint64_t{0x0000000005566778});  // expected result
2337 2338 2339 2340 2341 2342
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    // rorx
    __ incq(rax);
    __ rorxq(r8, rcx, 0x4);
2343
    __ movq(r9, uint64_t{0x8112233445566778});  // expected result
2344 2345 2346 2347 2348
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ rorxq(r8, Operand(rsp, 0), 0x4);
2349
    __ movq(r9, uint64_t{0x8112233445566778});  // expected result
2350 2351 2352 2353 2354
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ rorxl(r8, rcx, 0x4);
2355
    __ movq(r9, uint64_t{0x0000000085566778});  // expected result
2356 2357 2358 2359 2360
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ incq(rax);
    __ rorxl(r8, Operand(rsp, 0), 0x4);
2361
    __ movq(r9, uint64_t{0x0000000085566778});  // expected result
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
    __ cmpq(r8, r9);
    __ j(not_equal, &exit);

    __ xorl(rax, rax);
    __ bind(&exit);
    __ popq(rcx);
    __ popq(rbx);
    __ ret(0);
  }

  CodeDesc desc;
2373
  masm.GetCode(isolate, &desc);
2374 2375
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
2376
#ifdef OBJECT_PRINT
2377
  StdoutStream os;
2378
  code->Print(os);
2379 2380
#endif

2381 2382
  auto f = GeneratedCode<F0>::FromCode(*code);
  CHECK_EQ(0, f.Call());
2383 2384 2385
}


2386 2387 2388 2389 2390
TEST(AssemblerX64JumpTables1) {
  // Test jump tables with forward jumps.
  CcTest::InitializeVM();
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
2391
  MacroAssembler masm(isolate, nullptr, 0,
2392
                      v8::internal::CodeObjectRequired::kYes);
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417

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

  Label done, table;
  __ leaq(arg2, Operand(&table));
  __ jmp(Operand(arg2, arg1, times_8, 0));
  __ ud2();
  __ bind(&table);
  for (int i = 0; i < kNumCases; ++i) {
    __ dq(&labels[i]);
  }

  for (int i = 0; i < kNumCases; ++i) {
    __ bind(&labels[i]);
    __ movq(rax, Immediate(values[i]));
    __ jmp(&done);
  }

  __ bind(&done);
  __ ret(0);

  CodeDesc desc;
2418
  masm.GetCode(isolate, &desc);
2419 2420
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
2421
#ifdef OBJECT_PRINT
2422
  code->Print(std::cout);
2423 2424
#endif

2425
  auto f = GeneratedCode<F1>::FromCode(*code);
2426
  for (int i = 0; i < kNumCases; ++i) {
2427
    int res = f.Call(i);
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
    PrintF("f(%d) = %d\n", i, res);
    CHECK_EQ(values[i], res);
  }
}


TEST(AssemblerX64JumpTables2) {
  // Test jump tables with backwards jumps.
  CcTest::InitializeVM();
  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
2439
  MacroAssembler masm(isolate, nullptr, 0,
2440
                      v8::internal::CodeObjectRequired::kYes);
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466

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

  Label done, table;
  __ leaq(arg2, Operand(&table));
  __ jmp(Operand(arg2, arg1, times_8, 0));
  __ ud2();

  for (int i = 0; i < kNumCases; ++i) {
    __ bind(&labels[i]);
    __ movq(rax, Immediate(values[i]));
    __ jmp(&done);
  }

  __ bind(&done);
  __ ret(0);

  __ bind(&table);
  for (int i = 0; i < kNumCases; ++i) {
    __ dq(&labels[i]);
  }

  CodeDesc desc;
2467
  masm.GetCode(isolate, &desc);
2468 2469
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
2470
#ifdef OBJECT_PRINT
2471
  code->Print(std::cout);
2472 2473
#endif

2474
  auto f = GeneratedCode<F1>::FromCode(*code);
2475
  for (int i = 0; i < kNumCases; ++i) {
2476
    int res = f.Call(i);
2477 2478 2479 2480 2481
    PrintF("f(%d) = %d\n", i, res);
    CHECK_EQ(values[i], res);
  }
}

2482 2483
TEST(AssemblerX64PslldWithXmm15) {
  CcTest::InitializeVM();
2484 2485
  size_t allocated;
  byte* buffer = AllocateAssemblerBuffer(&allocated);
2486
  Assembler masm(AssemblerOptions{}, buffer, static_cast<int>(allocated));
2487 2488 2489 2490 2491 2492 2493

  __ movq(xmm15, arg1);
  __ pslld(xmm15, 1);
  __ movq(rax, xmm15);
  __ ret(0);

  CodeDesc desc;
2494
  masm.GetCode(CcTest::i_isolate(), &desc);
2495
  MakeAssemblerBufferExecutable(buffer, allocated);
2496
  auto f = GeneratedCode<F5>::FromBuffer(CcTest::i_isolate(), buffer);
2497
  uint64_t result = f.Call(uint64_t{0x1122334455667788});
2498
  CHECK_EQ(uint64_t{0x22446688AACCEF10}, result);
2499 2500
}

2501
typedef float(F9)(float x, float y);
2502 2503 2504 2505 2506 2507 2508
TEST(AssemblerX64vmovups) {
  CcTest::InitializeVM();
  if (!CpuFeatures::IsSupported(AVX)) return;

  Isolate* isolate = reinterpret_cast<Isolate*>(CcTest::isolate());
  HandleScope scope(isolate);
  v8::internal::byte buffer[256];
2509
  MacroAssembler masm(isolate, buffer, sizeof(buffer),
2510 2511
                      v8::internal::CodeObjectRequired::kYes);
  {
2512
    CpuFeatureScope avx_scope(&masm, AVX);
2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
    __ shufps(xmm0, xmm0, 0x0);  // brocast first argument
    __ shufps(xmm1, xmm1, 0x0);  // brocast second argument
    // copy xmm1 to xmm0 through the stack to test the "vmovups reg, mem".
    __ subq(rsp, Immediate(kSimd128Size));
    __ vmovups(Operand(rsp, 0), xmm1);
    __ vmovups(xmm0, Operand(rsp, 0));
    __ addq(rsp, Immediate(kSimd128Size));

    __ ret(0);
  }

  CodeDesc desc;
2525
  masm.GetCode(isolate, &desc);
2526 2527
  Handle<Code> code =
      isolate->factory()->NewCode(desc, Code::STUB, Handle<Code>());
2528
#ifdef OBJECT_PRINT
2529
  StdoutStream os;
2530
  code->Print(os);
2531 2532
#endif

2533 2534
  auto f = GeneratedCode<F9>::FromCode(*code);
  CHECK_EQ(-1.5, f.Call(1.5, -1.5));
2535 2536
}

2537
#undef __
2538 2539 2540

}  // namespace internal
}  // namespace v8