|
| 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 | +} |
0 commit comments