Skip to content

[benchmark] Add DoubleWidth division benchmark #14043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ set(SWIFT_BENCH_MODULES
single-source/DictionaryRemove
single-source/DictionarySubscriptDefault
single-source/DictionarySwap
single-source/DoubleWidthDivision
single-source/DropFirst
single-source/DropLast
single-source/DropWhile
Expand Down
56 changes: 56 additions & 0 deletions benchmark/single-source/DoubleWidthDivision.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===--- DoubleWidthDivision.swift ----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// This test checks performance of division using DoubleWidth.

import Foundation
import TestsUtils

public let DoubleWidthDivision = BenchmarkInfo(
name: "DoubleWidthDivision",
runFunction: run_DoubleWidthDivision,
tags: [.validation, .algorithm]
)

private typealias Int128 = DoubleWidth<Int64>
private typealias Int256 = DoubleWidth<Int128>
private typealias Int512 = DoubleWidth<Int256>
private typealias Int1024 = DoubleWidth<Int512>

@inline(never)
public func run_DoubleWidthDivision(_ N: Int) {
var sum = 0
for _ in 1...N {
let (q, r) =
(Int128(Int64.max) * 16)
.quotientAndRemainder(dividingBy: numericCast(getInt(16)))
sum += Int(q * r)

let (q1, r1) =
(40 as Int128).dividingFullWidth(
(high: numericCast(getInt(0)), low: numericCast(getInt(240))))
sum += Int(q1 * r1)

let x =
DoubleWidth<DoubleWidth<DoubleWidth<Int8>>>(
Int64.max / numericCast(getInt(4)))
let y = DoubleWidth<DoubleWidth<Int8>>(Int32.max)
let (q2, r2) = y.dividingFullWidth((x.high, x.low))
sum += Int(q2 - r2)

let xx = Int1024(x)
let yy = Int512(y)
let (q3, r3) = yy.dividingFullWidth((xx.high, xx.low))
sum -= Int(q3 - r3)
}
CheckResults(sum == 0)
}
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import DictionaryLiteral
import DictionaryRemove
import DictionarySubscriptDefault
import DictionarySwap
import DoubleWidthDivision
import DropFirst
import DropLast
import DropWhile
Expand Down Expand Up @@ -191,6 +192,7 @@ registerBenchmark(DictionaryLiteral)
registerBenchmark(DictionaryRemove)
registerBenchmark(DictionarySubscriptDefault)
registerBenchmark(DictionarySwap)
registerBenchmark(DoubleWidthDivision)
registerBenchmark(DropFirst)
registerBenchmark(DropLast)
registerBenchmark(DropWhile)
Expand Down