bounds-checks-elimination.js 2.73 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
// Copyright 2014 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.

// Flags: --allow-natives-syntax --array-bounds-checks-elimination

var a = []
for (var i = 0; i < 9; i++) a[i] = i + 1;

function test(f, arg1, arg2, expected) {
  assertEquals(expected, f(arg1));
  f(arg2);
  %OptimizeFunctionOnNextCall(f);
  assertEquals(expected, f(arg1));
}

test(function f0() {
  return a[7] * a[6] * a[5] * a[4] * a[3] * a[2] * a[1] * a[0];
}, 0, 1, 40320);

test(function f1() {
  return a[7] * a[6] * a[5] * a[4] * a[10] * a[2] * a[1] * a[0];
}, 0, 1, NaN);

test(function f2() {
  return a[0] * a[1] * a[2] * a[3] * a[4] * a[5] * a[6] * a[7];
}, 0, 1, 40320);

test(function f3() {
  return a[3] * a[0] * a[6] * a[7] * a[5] * a[1] * a[4] * a[2];
}, 0, 1, 40320);

test(function f4(b) {
  return a[b+3] * a[0] * a[b+6] * a[7] * a[b+5] * a[1] * a[b+4] * a[2];
}, 0, 1, 40320);

test(function f5(b) {
  return a[b+1] * a[0] * a[b+4] * a[7] * a[b+3] * a[1] * a[b+2] * a[2];
}, 2, 3, 40320);

test(function f6(b) {
  var c;
  if (b) c = a[3] * a[0] * a[6] * a[7];
  return c * a[5] * a[1] * a[4] * a[2];
}, true, false, 40320);

test(function f7(b) {
  var c = a[7];
  if (b) c *= a[3] * a[0] * a[6];
  return c * a[5] * a[1] * a[4] * a[2];
}, true, false, 40320);

test(function f8(b) {
  var c = a[7];
  if (b) c *= a[3] * a[0] * a[6];
  return c * a[5] * a[10] * a[4] * a[2];
}, true, false, NaN);

test(function f9(b) {
  var c = a[1];
  if (b) {
    c *= a[3] * a[0] * a[6];
  } else {
    c = a[6] * a[5] * a[4];
  }
  return c * a[5] * a[7] * a[4] * a[2];
}, true, false, 40320);

test(function fa(b) {
  var c = a[1];
  if (b) {
    c = a[6] * a[b+5] * a[4];
  } else {
    c *= a[b+3] * a[0] * a[b+6];
  }
  return c * a[5] * a[b+7] * a[4] * a[2];
}, 0, 1, 40320);

test(function fb(b) {
  var c = a[b-3];
  if (b != 4) {
    c = a[6] * a[b+1] * a[4];
  } else {
    c *= a[b-1] * a[0] * a[b+2];
  }
  return c * a[5] * a[b+3] * a[4] * a[b-2];
}, 4, 3, 40320);

test(function fc(b) {
  var c = a[b-3];
  if (b != 4) {
    c = a[6] * a[b+1] * a[4];
  } else {
    c *= a[b-1] * a[0] * a[b+2];
  }
  return c * a[5] * a[b+3] * a[4] * a[b-2];
}, 6, 3, NaN);

test(function fd(b) {
  var c = a[b-3];
  if (b != 4) {
    c = a[6] * a[b+1] * a[4];
  } else {
    c *= a[b-1] * a[0] * a[b+2];
  }
  return c * a[5] * a[b+3] * a[4] * a[b-2];
}, 1, 4, NaN);

test(function fe(b) {
  var c = 1;
  for (var i = 1; i < b-1; i++) {
    c *= a[i-1] * a[i] * a[i+1];
  }
  return c;
}, 8, 4, (40320 / 8 / 7) * (40320 / 8) * (40320 / 2));

test(function ff(b) {
  var c = 0;
  for (var i = 0; i < b; i++) {
    c += a[3] * a[0] * a[6] * a[7] * a[5] * a[1] * a[4] * a[2];
  }
  return c;
}, 100, 4, 40320 * 100);