Commit d98e684f authored by Qifan Pan's avatar Qifan Pan Committed by V8 LUCI CQ

[turbofan] More benchmarks for BigInt operations

Benchmark BitwiseAnd, Divide, and Exponentiate. Add explicitly truncated
cases to Multiply.

Bug: v8:9407
Change-Id: I89b2a624c0a60034fbe875958461a5f2ab826ade
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3829467
Commit-Queue: Qifan Pan <panq@google.com>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#82514}
parent 9eba38fe
// 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_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('BitwiseAnd-Zero', [1000], [
new Benchmark('BitwiseAnd-Zero', true, false, 0, TestBitwiseAndZero)
]);
new BenchmarkSuite('BitwiseAnd-Small', [1000], [
new Benchmark('BitwiseAnd-Small', true, false, 0, TestBitwiseAnd,
SetUpTestBitwiseAndSmall)
]);
new BenchmarkSuite('BitwiseAnd-Small-Truncated', [1000], [
new Benchmark('BitwiseAnd-Small-Truncated', true, false, 0,
TestBitwiseAndTruncated, SetUpTestBitwiseAndSmall)
]);
new BenchmarkSuite('BitwiseAnd-Random', [1000], [
new Benchmark('BitwiseAnd-Random', true, false, 0, TestBitwiseAnd,
SetUpTestBitwiseAndRandom)
]);
function TestBitwiseAndZero() {
let result = 0n;
for (let i = 0n; i < TEST_ITERATIONS; ++i) {
result += 0n & i;
}
return result;
}
function SetUpTestBitwiseAndSmall() {
random_bigints = [];
for (let i = 0; i < TEST_ITERATIONS; ++i) {
const bigint = RandomBigIntWithBits(64);
random_bigints.push(Math.random() < 0.5 ? -bigint : bigint);
}
}
function SetUpTestBitwiseAndRandom() {
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 TestBitwiseAnd() {
let result = 0n;
for (let i = 0; i < TEST_ITERATIONS - 1; ++i) {
result += random_bigints[i] & random_bigints[i + 1];
}
return result;
}
function TestBitwiseAndTruncated() {
let result = 0n;
for (let i = 0; i < TEST_ITERATIONS - 1; ++i) {
// Truncated explicitly
result += BigInt.asIntN(64, random_bigints[i] & random_bigints[i + 1]);
}
return result;
}
// 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;
}
// 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_exponents = [];
// 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('Exponentiate-Base-Two', [10000], [
new Benchmark('Exponentiate-Base-Two', true, false, 0,
TestExponentiateBaseTwo, SetUpTestExponentiateBaseTwo)
]);
function SetUpTestExponentiateBaseTwo() {
random_exponents = [];
// Restrict the maximum length of exponents to 20 bits so that the durations
// are reasonable and BigIntTooBig exceptions can be avoided.
const max_in_4bits = 20 / 4;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
const bits = Math.floor(Math.random() * max_in_4bits) * 4;
const bigint = RandomBigIntWithBits(bits);
// Exponents are non-negative.
random_exponents.push(bigint);
}
}
function TestExponentiateBaseTwo() {
let sum = 0n;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
sum += 2n ** random_exponents[i];
}
return sum;
}
......@@ -27,6 +27,12 @@ new BenchmarkSuite('Multiply-Small', [1000], [
]);
new BenchmarkSuite('Multiply-Small-Truncated', [1000], [
new Benchmark('Multiply-Small-Truncated', true, false, 0,
TestMultiplySmallTruncated)
]);
new BenchmarkSuite('Multiply-Random', [10000], [
new Benchmark('Multiply-Random', true, false, 0, TestMultiplyRandom,
SetUpTestMultiplyRandom)
......@@ -55,6 +61,17 @@ function TestMultiplySmall() {
}
function TestMultiplySmallTruncated() {
let sum = 0n;
for (let i = 0n; i < TEST_ITERATIONS; ++i) {
sum += BigInt.asIntN(64, i * (i + 1n));
}
return sum;
}
function SetUpTestMultiplyRandom() {
random_bigints = [];
// RandomBigIntWithBits needs multiples of 4 bits.
......@@ -71,7 +88,7 @@ function TestMultiplyRandom() {
let sum = 0n;
for (let i = 0; i < TEST_ITERATIONS - 1; ++i) {
sum = random_bigints[i] * random_bigints[i + 1];
sum += random_bigints[i] * random_bigints[i + 1];
}
return sum;
......
......@@ -169,9 +169,49 @@
"tests": [
{ "name": "Multiply-Zero" },
{ "name": "Multiply-Small"},
{ "name": "Multiply-Small-Truncated" },
{ "name": "Multiply-Random" }
]
},
{
"name": "Divide",
"main": "run.js",
"flags": ["--allow-natives-syntax"],
"resources": ["divide.js", "bigint-util.js"],
"test_flags": ["divide"],
"results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$",
"tests": [
{ "name": "Divide-One" },
{ "name": "Divide-Small"},
{ "name": "Divide-Small-Truncated" },
{ "name": "Divide-Random" }
]
},
{
"name": "BitwiseAnd",
"main": "run.js",
"flags": ["--allow-natives-syntax"],
"resources": ["bitwise-and.js", "bigint-util.js"],
"test_flags": ["bitwise-and"],
"results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$",
"tests": [
{ "name": "BitwiseAnd-Zero" },
{ "name": "BitwiseAnd-Small" },
{ "name": "BitwiseAnd-Small-Truncated" },
{ "name": "BitwiseAnd-Random" }
]
},
{
"name": "Exponentiate",
"main": "run.js",
"flags": ["--allow-natives-syntax"],
"resources": ["exponentiate.js", "bigint-util.js"],
"test_flags": ["exponentiate"],
"results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$",
"tests": [
{ "name": "Exponentiate-Base-Two" }
]
},
{
"name": "AsUintN",
"main": "run.js",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment