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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
//===--- BinaryFloatingPointConversionFromBinaryInteger.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 generic binary floating-point conversion from
// a binary integer.
import TestsUtils
#if swift(>=4.2)
public let benchmarks = [
BenchmarkInfo(
name: "BinaryFloatingPointConversionFromBinaryInteger",
runFunction: run_BinaryFloatingPointConversionFromBinaryInteger,
tags: [.validation, .algorithm]
),
]
#else
public let benchmarks: [BenchmarkInfo] = []
#endif
struct MockBinaryInteger<T : BinaryInteger> {
var _value: T
init(_ value: T) {
_value = value
}
}
extension MockBinaryInteger : CustomStringConvertible {
var description: String {
return _value.description
}
}
extension MockBinaryInteger : ExpressibleByIntegerLiteral {
init(integerLiteral value: T.IntegerLiteralType) {
_value = T(integerLiteral: value)
}
}
extension MockBinaryInteger : Comparable {
static func < (lhs: MockBinaryInteger<T>, rhs: MockBinaryInteger<T>) -> Bool {
return lhs._value < rhs._value
}
static func == (
lhs: MockBinaryInteger<T>, rhs: MockBinaryInteger<T>
) -> Bool {
return lhs._value == rhs._value
}
}
extension MockBinaryInteger : Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(_value)
}
}
extension MockBinaryInteger : BinaryInteger {
static var isSigned: Bool {
return T.isSigned
}
init<Source>(_ source: Source) where Source : BinaryFloatingPoint {
_value = T(source)
}
init?<Source>(exactly source: Source) where Source : BinaryFloatingPoint {
guard let result = T(exactly: source) else { return nil }
_value = result
}
init<Source>(_ source: Source) where Source : BinaryInteger {
_value = T(source)
}
init?<Source>(exactly source: Source) where Source : BinaryInteger {
guard let result = T(exactly: source) else { return nil }
_value = result
}
init<Source>(truncatingIfNeeded source: Source) where Source : BinaryInteger {
_value = T(truncatingIfNeeded: source)
}
init<Source>(clamping source: Source) where Source : BinaryInteger {
_value = T(clamping: source)
}
var magnitude: MockBinaryInteger<T.Magnitude> {
return MockBinaryInteger<T.Magnitude>(_value.magnitude)
}
var words: T.Words {
return _value.words
}
var bitWidth: Int {
return _value.bitWidth
}
var trailingZeroBitCount: Int {
return _value.trailingZeroBitCount
}
func isMultiple(of other: MockBinaryInteger<T>) -> Bool {
return _value.isMultiple(of: other._value)
}
static func + (
lhs: MockBinaryInteger<T>, rhs: MockBinaryInteger<T>
) -> MockBinaryInteger<T> {
return MockBinaryInteger(lhs._value + rhs._value)
}
static func += (lhs: inout MockBinaryInteger<T>, rhs: MockBinaryInteger<T>) {
lhs._value += rhs._value
}
static func - (
lhs: MockBinaryInteger<T>, rhs: MockBinaryInteger<T>
) -> MockBinaryInteger<T> {
return MockBinaryInteger(lhs._value - rhs._value)
}
static func -= (lhs: inout MockBinaryInteger<T>, rhs: MockBinaryInteger<T>) {
lhs._value -= rhs._value
}
static func * (
lhs: MockBinaryInteger<T>, rhs: MockBinaryInteger<T>
) -> MockBinaryInteger<T> {
return MockBinaryInteger(lhs._value * rhs._value)
}
static func *= (lhs: inout MockBinaryInteger<T>, rhs: MockBinaryInteger<T>) {
lhs._value *= rhs._value
}
static func / (
lhs: MockBinaryInteger<T>, rhs: MockBinaryInteger<T>
) -> MockBinaryInteger<T> {
return MockBinaryInteger(lhs._value / rhs._value)
}
static func /= (lhs: inout MockBinaryInteger<T>, rhs: MockBinaryInteger<T>) {
lhs._value /= rhs._value
}
static func % (
lhs: MockBinaryInteger<T>, rhs: MockBinaryInteger<T>
) -> MockBinaryInteger<T> {
return MockBinaryInteger(lhs._value % rhs._value)
}
static func %= (lhs: inout MockBinaryInteger<T>, rhs: MockBinaryInteger<T>) {
lhs._value %= rhs._value
}
static func &= (lhs: inout MockBinaryInteger<T>, rhs: MockBinaryInteger<T>) {
lhs._value &= rhs._value
}
static func |= (lhs: inout MockBinaryInteger<T>, rhs: MockBinaryInteger<T>) {
lhs._value |= rhs._value
}
static func ^= (lhs: inout MockBinaryInteger<T>, rhs: MockBinaryInteger<T>) {
lhs._value ^= rhs._value
}
static prefix func ~ (x: MockBinaryInteger<T>) -> MockBinaryInteger<T> {
return MockBinaryInteger(~x._value)
}
static func >>= <RHS>(
lhs: inout MockBinaryInteger<T>, rhs: RHS
) where RHS : BinaryInteger {
lhs._value >>= rhs
}
static func <<= <RHS>(
lhs: inout MockBinaryInteger<T>, rhs: RHS
) where RHS : BinaryInteger {
lhs._value <<= rhs
}
}
#if swift(>=4.2)
@inline(never)
public func run_BinaryFloatingPointConversionFromBinaryInteger(_ n: Int) {
var xs = [Double]()
xs.reserveCapacity(n)
for _ in 1...n {
var x = 0 as Double
for i in 0..<2000 {
x += Double._convert(from: MockBinaryInteger(getInt(i))).value
}
xs.append(x)
}
check(xs[getInt(0)] == 1999000)
}
#endif
|