summaryrefslogtreecommitdiff
path: root/deps/v8/test/js-perf-test/BigInt/subtract.js
blob: ee7c8cd12660e96fc22e09f4e6b8a83e4d24201b (plain)
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
124
// Copyright 2019 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.

"use strict";

d8.file.execute('bigint-util.js');

let initial_diff = 0n;
let a = 0n;
let random_bigints = [];

// This dummy ensures that the feedback for benchmark.run() in the Measure
// function from base.js is not monomorphic, thereby preventing the benchmarks
// below from being inlined. This ensures consistent behavior and comparable
// results.
new BenchmarkSuite('Prevent-Inline-Dummy', [10000], [
  new Benchmark('Prevent-Inline-Dummy', true, false, 0, () => {})
]);


new BenchmarkSuite('Subtract-Zero', [1000], [
  new Benchmark('Subtract-Zero', true, false, 0, TestSubtractZero,
    SetUpTestSubtractZero)
]);


BITS_CASES.forEach((d) => {
  new BenchmarkSuite(`Subtract-SameSign-${d}`, [1000], [
    new Benchmark(`Subtract-SameSign-${d}`, true, false, 0,
      TestSubtractSameSign, () => SetUpTestSubtractSameSign(d))
  ]);
});


BITS_CASES.forEach((d) => {
  new BenchmarkSuite(`Subtract-DifferentSign-${d}`, [1000], [
    new Benchmark(`Subtract-DifferentSign-${d}`, true, false, 0,
      TestSubtractDifferentSign, () => SetUpTestSubtractDifferentSign(d))
  ]);
});


new BenchmarkSuite('Subtract-Random', [1000], [
  new Benchmark('Subtract-Random', true, false, 0, TestSubtractRandom,
    SetUpTestSubtractRandom)
]);


function SetUpTestSubtractSameSign(bits) {
  // Subtract a small random negative value from the maximal negative number to
  // make sure the result stays negative.
  initial_diff = -MaxBigIntWithBits(bits);
  a = -SmallRandomBigIntWithBits(bits);
}


function TestSubtractSameSign() {
  let diff = initial_diff;

  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    diff = diff - a;
  }

  return diff;
}


function SetUpTestSubtractDifferentSign(bits) {
  // Subtract a small random negative value from a small positive value so that
  // the differnce stays positive but does not grow in digits.
  initial_diff = SmallRandomBigIntWithBits(bits);
  a = -SmallRandomBigIntWithBits(bits);
}


function TestSubtractDifferentSign() {
  let diff = initial_diff;

  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    diff = diff - a;
  }

  return diff;
}


function SetUpTestSubtractRandom() {
  random_bigints = [];
  // RandomBigIntWithBits needs multiples of 4 bits.
  const max_in_4bits = RANDOM_BIGINTS_MAX_BITS / 4;
  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    const bits = Math.floor(Math.random() * max_in_4bits) * 4;
    const bigint = RandomBigIntWithBits(bits);
    random_bigints.push(Math.random() < 0.5 ? -bigint : bigint);
  }
}


function TestSubtractRandom() {
  let diff = 0n;

  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    diff = diff - random_bigints[i];
  }

  return diff;
}


function SetUpTestSubtractZero() {
  initial_diff = 42n;
}


function TestSubtractZero() {
  let diff = initial_diff;

  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    diff = diff - 0n;
  }

  return diff;
}