Skip to content

Commit ea38221

Browse files
authored
Merge pull request #14043 from xwu/benchmark-doublewidth-division
[benchmark] Add DoubleWidth division benchmark
2 parents dc0be0e + bc0e806 commit ea38221

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ set(SWIFT_BENCH_MODULES
6565
single-source/DictionaryRemove
6666
single-source/DictionarySubscriptDefault
6767
single-source/DictionarySwap
68+
single-source/DoubleWidthDivision
6869
single-source/DropFirst
6970
single-source/DropLast
7071
single-source/DropWhile
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===--- DoubleWidthDivision.swift ----------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// This test checks performance of division using DoubleWidth.
14+
15+
import Foundation
16+
import TestsUtils
17+
18+
public let DoubleWidthDivision = BenchmarkInfo(
19+
name: "DoubleWidthDivision",
20+
runFunction: run_DoubleWidthDivision,
21+
tags: [.validation, .algorithm]
22+
)
23+
24+
private typealias Int128 = DoubleWidth<Int64>
25+
private typealias Int256 = DoubleWidth<Int128>
26+
private typealias Int512 = DoubleWidth<Int256>
27+
private typealias Int1024 = DoubleWidth<Int512>
28+
29+
@inline(never)
30+
public func run_DoubleWidthDivision(_ N: Int) {
31+
var sum = 0
32+
for _ in 1...N {
33+
let (q, r) =
34+
(Int128(Int64.max) * 16)
35+
.quotientAndRemainder(dividingBy: numericCast(getInt(16)))
36+
sum += Int(q * r)
37+
38+
let (q1, r1) =
39+
(40 as Int128).dividingFullWidth(
40+
(high: numericCast(getInt(0)), low: numericCast(getInt(240))))
41+
sum += Int(q1 * r1)
42+
43+
let x =
44+
DoubleWidth<DoubleWidth<DoubleWidth<Int8>>>(
45+
Int64.max / numericCast(getInt(4)))
46+
let y = DoubleWidth<DoubleWidth<Int8>>(Int32.max)
47+
let (q2, r2) = y.dividingFullWidth((x.high, x.low))
48+
sum += Int(q2 - r2)
49+
50+
let xx = Int1024(x)
51+
let yy = Int512(y)
52+
let (q3, r3) = yy.dividingFullWidth((xx.high, xx.low))
53+
sum -= Int(q3 - r3)
54+
}
55+
CheckResults(sum == 0)
56+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import DictionaryLiteral
5252
import DictionaryRemove
5353
import DictionarySubscriptDefault
5454
import DictionarySwap
55+
import DoubleWidthDivision
5556
import DropFirst
5657
import DropLast
5758
import DropWhile
@@ -193,6 +194,7 @@ registerBenchmark(DictionaryLiteral)
193194
registerBenchmark(DictionaryRemove)
194195
registerBenchmark(DictionarySubscriptDefault)
195196
registerBenchmark(DictionarySwap)
197+
registerBenchmark(DoubleWidthDivision)
196198
registerBenchmark(DropFirst)
197199
registerBenchmark(DropLast)
198200
registerBenchmark(DropWhile)

0 commit comments

Comments
 (0)