OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. Use of this | 1 // Copyright 2014 the V8 project authors. All rights reserved. Use of this |
2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
3 // LICENSE file. | 3 // LICENSE file. |
4 | 4 |
5 #include <cmath> | 5 #include <cmath> |
6 #include <functional> | 6 #include <functional> |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "src/base/bits.h" | 9 #include "src/base/bits.h" |
10 #include "src/base/utils/random-number-generator.h" | 10 #include "src/base/utils/random-number-generator.h" |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 Node* a = Int64Input(&m, j); | 409 Node* a = Int64Input(&m, j); |
410 Node* b = Int64Input(&m, k); | 410 Node* b = Int64Input(&m, k); |
411 m.Return(m.AddNode(kOps[i], a, b)); | 411 m.Return(m.AddNode(kOps[i], a, b)); |
412 m.GenerateCode(); | 412 m.GenerateCode(); |
413 } | 413 } |
414 } | 414 } |
415 } | 415 } |
416 } | 416 } |
417 | 417 |
418 | 418 |
| 419 TEST(RunInt64AddWithOverflowP) { |
| 420 int64_t actual_val = -1; |
| 421 RawMachineAssemblerTester<int32_t> m; |
| 422 Int64BinopTester bt(&m); |
| 423 Node* add = m.Int64AddWithOverflow(bt.param0, bt.param1); |
| 424 Node* val = m.Projection(0, add); |
| 425 Node* ovf = m.Projection(1, add); |
| 426 m.StoreToPointer(&actual_val, MachineRepresentation::kWord64, val); |
| 427 bt.AddReturn(ovf); |
| 428 FOR_INT64_INPUTS(i) { |
| 429 FOR_INT64_INPUTS(j) { |
| 430 int64_t expected_val; |
| 431 int expected_ovf = bits::SignedAddOverflow64(*i, *j, &expected_val); |
| 432 CHECK_EQ(expected_ovf, bt.call(*i, *j)); |
| 433 CHECK_EQ(expected_val, actual_val); |
| 434 } |
| 435 } |
| 436 } |
| 437 |
| 438 |
| 439 TEST(RunInt64AddWithOverflowImm) { |
| 440 int64_t actual_val = -1, expected_val = 0; |
| 441 FOR_INT64_INPUTS(i) { |
| 442 { |
| 443 RawMachineAssemblerTester<int32_t> m(MachineType::Int64()); |
| 444 Node* add = m.Int64AddWithOverflow(m.Int64Constant(*i), m.Parameter(0)); |
| 445 Node* val = m.Projection(0, add); |
| 446 Node* ovf = m.Projection(1, add); |
| 447 m.StoreToPointer(&actual_val, MachineRepresentation::kWord64, val); |
| 448 m.Return(ovf); |
| 449 FOR_INT64_INPUTS(j) { |
| 450 int expected_ovf = bits::SignedAddOverflow64(*i, *j, &expected_val); |
| 451 CHECK_EQ(expected_ovf, m.Call(*j)); |
| 452 CHECK_EQ(expected_val, actual_val); |
| 453 } |
| 454 } |
| 455 { |
| 456 RawMachineAssemblerTester<int32_t> m(MachineType::Int64()); |
| 457 Node* add = m.Int64AddWithOverflow(m.Parameter(0), m.Int64Constant(*i)); |
| 458 Node* val = m.Projection(0, add); |
| 459 Node* ovf = m.Projection(1, add); |
| 460 m.StoreToPointer(&actual_val, MachineRepresentation::kWord64, val); |
| 461 m.Return(ovf); |
| 462 FOR_INT64_INPUTS(j) { |
| 463 int expected_ovf = bits::SignedAddOverflow64(*i, *j, &expected_val); |
| 464 CHECK_EQ(expected_ovf, m.Call(*j)); |
| 465 CHECK_EQ(expected_val, actual_val); |
| 466 } |
| 467 } |
| 468 FOR_INT64_INPUTS(j) { |
| 469 RawMachineAssemblerTester<int32_t> m; |
| 470 Node* add = |
| 471 m.Int64AddWithOverflow(m.Int64Constant(*i), m.Int64Constant(*j)); |
| 472 Node* val = m.Projection(0, add); |
| 473 Node* ovf = m.Projection(1, add); |
| 474 m.StoreToPointer(&actual_val, MachineRepresentation::kWord64, val); |
| 475 m.Return(ovf); |
| 476 int expected_ovf = bits::SignedAddOverflow64(*i, *j, &expected_val); |
| 477 CHECK_EQ(expected_ovf, m.Call()); |
| 478 CHECK_EQ(expected_val, actual_val); |
| 479 } |
| 480 } |
| 481 } |
| 482 |
| 483 |
| 484 TEST(RunInt64AddWithOverflowInBranchP) { |
| 485 int constant = 911777; |
| 486 RawMachineLabel blocka, blockb; |
| 487 RawMachineAssemblerTester<int32_t> m; |
| 488 Int64BinopTester bt(&m); |
| 489 Node* add = m.Int64AddWithOverflow(bt.param0, bt.param1); |
| 490 Node* ovf = m.Projection(1, add); |
| 491 m.Branch(ovf, &blocka, &blockb); |
| 492 m.Bind(&blocka); |
| 493 bt.AddReturn(m.Int64Constant(constant)); |
| 494 m.Bind(&blockb); |
| 495 Node* val = m.Projection(0, add); |
| 496 Node* truncated = m.TruncateInt64ToInt32(val); |
| 497 bt.AddReturn(truncated); |
| 498 FOR_INT64_INPUTS(i) { |
| 499 FOR_INT64_INPUTS(j) { |
| 500 int32_t expected = constant; |
| 501 int64_t result; |
| 502 if (!bits::SignedAddOverflow64(*i, *j, &result)) { |
| 503 expected = static_cast<int32_t>(result); |
| 504 } |
| 505 CHECK_EQ(expected, bt.call(*i, *j)); |
| 506 } |
| 507 } |
| 508 } |
| 509 |
| 510 |
| 511 TEST(RunInt64SubWithOverflowP) { |
| 512 int64_t actual_val = -1; |
| 513 RawMachineAssemblerTester<int32_t> m; |
| 514 Int64BinopTester bt(&m); |
| 515 Node* add = m.Int64SubWithOverflow(bt.param0, bt.param1); |
| 516 Node* val = m.Projection(0, add); |
| 517 Node* ovf = m.Projection(1, add); |
| 518 m.StoreToPointer(&actual_val, MachineRepresentation::kWord64, val); |
| 519 bt.AddReturn(ovf); |
| 520 FOR_INT64_INPUTS(i) { |
| 521 FOR_INT64_INPUTS(j) { |
| 522 int64_t expected_val; |
| 523 int expected_ovf = bits::SignedSubOverflow64(*i, *j, &expected_val); |
| 524 CHECK_EQ(expected_ovf, bt.call(*i, *j)); |
| 525 CHECK_EQ(expected_val, actual_val); |
| 526 } |
| 527 } |
| 528 } |
| 529 |
| 530 |
| 531 TEST(RunInt64SubWithOverflowImm) { |
| 532 int64_t actual_val = -1, expected_val = 0; |
| 533 FOR_INT64_INPUTS(i) { |
| 534 { |
| 535 RawMachineAssemblerTester<int32_t> m(MachineType::Int64()); |
| 536 Node* add = m.Int64SubWithOverflow(m.Int64Constant(*i), m.Parameter(0)); |
| 537 Node* val = m.Projection(0, add); |
| 538 Node* ovf = m.Projection(1, add); |
| 539 m.StoreToPointer(&actual_val, MachineRepresentation::kWord64, val); |
| 540 m.Return(ovf); |
| 541 FOR_INT64_INPUTS(j) { |
| 542 int expected_ovf = bits::SignedSubOverflow64(*i, *j, &expected_val); |
| 543 CHECK_EQ(expected_ovf, m.Call(*j)); |
| 544 CHECK_EQ(expected_val, actual_val); |
| 545 } |
| 546 } |
| 547 { |
| 548 RawMachineAssemblerTester<int32_t> m(MachineType::Int64()); |
| 549 Node* add = m.Int64SubWithOverflow(m.Parameter(0), m.Int64Constant(*i)); |
| 550 Node* val = m.Projection(0, add); |
| 551 Node* ovf = m.Projection(1, add); |
| 552 m.StoreToPointer(&actual_val, MachineRepresentation::kWord64, val); |
| 553 m.Return(ovf); |
| 554 FOR_INT64_INPUTS(j) { |
| 555 int expected_ovf = bits::SignedSubOverflow64(*j, *i, &expected_val); |
| 556 CHECK_EQ(expected_ovf, m.Call(*j)); |
| 557 CHECK_EQ(expected_val, actual_val); |
| 558 } |
| 559 } |
| 560 FOR_INT64_INPUTS(j) { |
| 561 RawMachineAssemblerTester<int32_t> m; |
| 562 Node* add = |
| 563 m.Int64SubWithOverflow(m.Int64Constant(*i), m.Int64Constant(*j)); |
| 564 Node* val = m.Projection(0, add); |
| 565 Node* ovf = m.Projection(1, add); |
| 566 m.StoreToPointer(&actual_val, MachineRepresentation::kWord64, val); |
| 567 m.Return(ovf); |
| 568 int expected_ovf = bits::SignedSubOverflow64(*i, *j, &expected_val); |
| 569 CHECK_EQ(expected_ovf, m.Call()); |
| 570 CHECK_EQ(expected_val, actual_val); |
| 571 } |
| 572 } |
| 573 } |
| 574 |
| 575 |
| 576 TEST(RunInt64SubWithOverflowInBranchP) { |
| 577 int constant = 911999; |
| 578 RawMachineLabel blocka, blockb; |
| 579 RawMachineAssemblerTester<int32_t> m; |
| 580 Int64BinopTester bt(&m); |
| 581 Node* sub = m.Int64SubWithOverflow(bt.param0, bt.param1); |
| 582 Node* ovf = m.Projection(1, sub); |
| 583 m.Branch(ovf, &blocka, &blockb); |
| 584 m.Bind(&blocka); |
| 585 bt.AddReturn(m.Int64Constant(constant)); |
| 586 m.Bind(&blockb); |
| 587 Node* val = m.Projection(0, sub); |
| 588 Node* truncated = m.TruncateInt64ToInt32(val); |
| 589 bt.AddReturn(truncated); |
| 590 FOR_INT64_INPUTS(i) { |
| 591 FOR_INT64_INPUTS(j) { |
| 592 int32_t expected = constant; |
| 593 int64_t result; |
| 594 if (!bits::SignedSubOverflow64(*i, *j, &result)) { |
| 595 expected = static_cast<int32_t>(result); |
| 596 } |
| 597 CHECK_EQ(expected, static_cast<int32_t>(bt.call(*i, *j))); |
| 598 } |
| 599 } |
| 600 } |
| 601 |
| 602 |
419 // TODO(titzer): add tests that run 64-bit integer operations. | 603 // TODO(titzer): add tests that run 64-bit integer operations. |
420 #endif // V8_TARGET_ARCH_64_BIT | 604 #endif // V8_TARGET_ARCH_64_BIT |
421 | 605 |
422 | 606 |
423 TEST(RunGoto) { | 607 TEST(RunGoto) { |
424 RawMachineAssemblerTester<int32_t> m; | 608 RawMachineAssemblerTester<int32_t> m; |
425 int constant = 99999; | 609 int constant = 99999; |
426 | 610 |
427 RawMachineLabel next; | 611 RawMachineLabel next; |
428 m.Goto(&next); | 612 m.Goto(&next); |
(...skipping 5451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5880 Node* call = r.AddNode(r.common()->Call(desc), phi); | 6064 Node* call = r.AddNode(r.common()->Call(desc), phi); |
5881 r.Return(call); | 6065 r.Return(call); |
5882 | 6066 |
5883 CHECK_EQ(33, r.Call(1)); | 6067 CHECK_EQ(33, r.Call(1)); |
5884 CHECK_EQ(44, r.Call(0)); | 6068 CHECK_EQ(44, r.Call(0)); |
5885 } | 6069 } |
5886 | 6070 |
5887 } // namespace compiler | 6071 } // namespace compiler |
5888 } // namespace internal | 6072 } // namespace internal |
5889 } // namespace v8 | 6073 } // namespace v8 |
OLD | NEW |