number-comparison-truncations.js 4.39 KB
Newer Older
1 2 3 4
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --allow-natives-syntax --opt --turbo-inlining
6 7 8 9 10 11 12 13 14

// Test that SpeculativeNumberEqual[SignedSmall] properly passes the
// kIdentifyZeros truncation.
(function() {
  function foo(x, y) {
    if (x * y === 0) return 0;
    return 1;
  }

15
  %PrepareFunctionForOptimization(foo);
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  assertEquals(0, foo(0, 1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(0, foo(0, 1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  assertOptimized(foo);
  // Even if x*y produces -0 now, it should stay optimized.
  assertEquals(0, foo(-3, 0));
  assertEquals(0, foo(0, -3));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberEqual[Number] properly passes the
// kIdentifyZeros truncation.
(function() {
  // Produce a SpeculativeNumberEqual with Number feedback.
  function bar(x, y) { return x === y; }
35
  %EnsureFeedbackVectorForFunction(bar);
36 37 38 39 40 41 42 43
  bar(0.1, 0.5);
  bar(-0, 100);

  function foo(x, y) {
    if (bar(x * y, 0)) return 0;
    return 1;
  }

44
  %PrepareFunctionForOptimization(foo);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  assertEquals(0, foo(0, 1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(0, foo(0, 1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  assertOptimized(foo);
  // Even if x*y produces -0 now, it should stay optimized.
  assertEquals(0, foo(-3, 0));
  assertEquals(0, foo(0, -3));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberLessThan[SignedSmall] properly passes the
// kIdentifyZeros truncation.
(function() {
  function foo(x, y) {
    if (x * y < 0) return 0;
    return 1;
  }

67
  %PrepareFunctionForOptimization(foo);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  assertEquals(0, foo(1, -1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(0, foo(1, -1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  assertOptimized(foo);
  // Even if x*y produces -0 now, it should stay optimized.
  assertEquals(1, foo(-3, 0));
  assertEquals(1, foo(0, -3));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberLessThan[Number] properly passes the
// kIdentifyZeros truncation.
(function() {
  // Produce a SpeculativeNumberLessThan with Number feedback.
  function bar(x, y) { return x < y; }
87
  %EnsureFeedbackVectorForFunction(bar);
88 89 90 91 92 93 94 95
  bar(0.1, 0.5);
  bar(-0, 100);

  function foo(x, y) {
    if (bar(x * y, 0)) return 0;
    return 1;
  }

96
  %PrepareFunctionForOptimization(foo);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  assertEquals(0, foo(1, -1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(0, foo(1, -1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  assertOptimized(foo);
  // Even if x*y produces -0 now, it should stay optimized.
  assertEquals(1, foo(-3, 0));
  assertEquals(1, foo(0, -3));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberLessThanOrEqual[SignedSmall] properly passes the
// kIdentifyZeros truncation.
(function() {
  function foo(x, y) {
    if (x * y <= 0) return 0;
    return 1;
  }

119
  %PrepareFunctionForOptimization(foo);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  assertEquals(0, foo(0, 1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(0, foo(0, 1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  assertOptimized(foo);
  // Even if x*y produces -0 now, it should stay optimized.
  assertEquals(0, foo(-3, 0));
  assertEquals(0, foo(0, -3));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberLessThanOrEqual[Number] properly passes the
// kIdentifyZeros truncation.
(function() {
  // Produce a SpeculativeNumberLessThanOrEqual with Number feedback.
  function bar(x, y) { return x <= y; }
139
  %EnsureFeedbackVectorForFunction(bar);
140 141 142 143 144 145 146 147
  bar(0.1, 0.5);
  bar(-0, 100);

  function foo(x, y) {
    if (bar(x * y, 0)) return 0;
    return 1;
  }

148
  %PrepareFunctionForOptimization(foo);
149 150 151 152 153 154 155 156 157 158 159 160 161
  assertEquals(0, foo(0, 1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(0, foo(0, 1));
  assertEquals(1, foo(1, 1));
  assertEquals(1, foo(1, 2));
  assertOptimized(foo);
  // Even if x*y produces -0 now, it should stay optimized.
  assertEquals(0, foo(-3, 0));
  assertEquals(0, foo(0, -3));
  assertOptimized(foo);
})();