summaryrefslogtreecommitdiff
path: root/deps/v8/test/js-perf-test/BigInt/divide.js
blob: 2fcf18b9de862a0b43ebfe10bc33e685fb1119ad (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
// Copyright 2022 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 random_dividends = []
let random_divisors = [];

// 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('Divide-One', [1000], [
  new Benchmark('Divide-One', true, false, 0, TestDivideOne)
]);


new BenchmarkSuite('Divide-Small', [1000], [
  new Benchmark('Divide-Small', true, false, 0, TestDivideSmall,
    SetUpTestDivideSmall)
]);


new BenchmarkSuite('Divide-Small-Truncated', [1000], [
  new Benchmark('Divide-Small-Truncated', true, false, 0, TestDivideSmallTruncated)
]);


new BenchmarkSuite('Divide-Random', [10000], [
  new Benchmark('Divide-Random', true, false, 0, TestDivideRandom,
    SetUpTestDivideRandom)
]);


function TestDivideOne() {
  let sum = 0n;

  for (let i = 0n; i < TEST_ITERATIONS; ++i) {
    sum += i / 1n;
  }

  return sum;
}


function SetUpTestDivideSmall() {
  random_dividends = [];
  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    const bigint = RandomBigIntWithBits(64);
    random_dividends.push(Math.random() < 0.5 ? -bigint : bigint);
  }

  random_divisors = [];
  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    const bigint = RandomBigIntWithBits(32);
    random_divisors.push(Math.random() < 0.5 ? -bigint : bigint);
  }
}


function TestDivideSmall() {
  let sum = 0n;

  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    sum += random_dividends[i] / random_divisors[i];
  }

  return sum;
}


function TestDivideSmallTruncated() {
  let sum = 0n;

  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    sum += BigInt.asIntN(64, random_dividends[i] / random_divisors[i]);
  }

  return sum;
}


function SetUpTestDivideRandom() {
  random_dividends = [];
  // 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_dividends.push(Math.random() < 0.5 ? -bigint : bigint);
  }

  random_divisors = [];
  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    // Avoid divide-by-zero
    const bits = Math.floor(1 + Math.random() * (max_in_4bits - 1)) * 4;
    const bigint = RandomBigIntWithBits(bits);
    random_divisors.push(Math.random() < 0.5 ? -bigint : bigint);
  }
}


function TestDivideRandom() {
  let sum = 0n;

  for (let i = 0; i < TEST_ITERATIONS; ++i) {
    sum += random_dividends[i] / random_divisors[i];
  }

  return sum;
}