Skip to content

Commit 87e1a13

Browse files
authored
Merge pull request #33821 from xwu/more-fp-conversion-benchmarks
2 parents 923d6ed + 44a7038 commit 87e1a13

File tree

1 file changed

+124
-2
lines changed

1 file changed

+124
-2
lines changed

benchmark/single-source/FloatingPointConversion.swift

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,123 @@ public let FloatingPointConversion = [
1616
BenchmarkInfo(
1717
name: "ConvertFloatingPoint.ConcreteDoubleToDouble",
1818
runFunction: run_ConvertFloatingPoint_ConcreteDoubleToDouble,
19-
tags: [.validation, .api]),
19+
tags: [.validation, .api],
20+
setUpFunction: { blackHole(doubles) }),
2021
BenchmarkInfo(
2122
name: "ConvertFloatingPoint.GenericDoubleToDouble",
2223
runFunction: run_ConvertFloatingPoint_GenericDoubleToDouble,
23-
tags: [.validation, .api]),
24+
tags: [.validation, .api],
25+
setUpFunction: { blackHole(doubles) }),
26+
BenchmarkInfo(
27+
name: "ConvertFloatingPoint.MockFloat64ToDouble",
28+
runFunction: run_ConvertFloatingPoint_MockFloat64ToDouble,
29+
tags: [.validation, .api],
30+
setUpFunction: { blackHole(mockFloat64s) }),
2431
]
2532

33+
protocol MockBinaryFloatingPoint: BinaryFloatingPoint {
34+
associatedtype _Value: BinaryFloatingPoint
35+
var _value: _Value { get set }
36+
init(_ _value: _Value)
37+
}
38+
39+
extension MockBinaryFloatingPoint {
40+
static var exponentBitCount: Int { _Value.exponentBitCount }
41+
static var greatestFiniteMagnitude: Self {
42+
Self(_Value.greatestFiniteMagnitude)
43+
}
44+
static var infinity: Self { Self(_Value.infinity) }
45+
static var leastNonzeroMagnitude: Self { Self(_Value.leastNonzeroMagnitude) }
46+
static var leastNormalMagnitude: Self { Self(_Value.leastNormalMagnitude) }
47+
static var nan: Self { Self(_Value.nan) }
48+
static var pi: Self { Self(_Value.pi) }
49+
static var signalingNaN: Self { Self(_Value.signalingNaN) }
50+
static var significandBitCount: Int { _Value.significandBitCount }
51+
52+
static func + (lhs: Self, rhs: Self) -> Self { Self(lhs._value + rhs._value) }
53+
static func += (lhs: inout Self, rhs: Self) { lhs._value += rhs._value }
54+
static func - (lhs: Self, rhs: Self) -> Self { Self(lhs._value - rhs._value) }
55+
static func -= (lhs: inout Self, rhs: Self) { lhs._value -= rhs._value }
56+
static func * (lhs: Self, rhs: Self) -> Self { Self(lhs._value * rhs._value) }
57+
static func *= (lhs: inout Self, rhs: Self) { lhs._value *= rhs._value }
58+
static func / (lhs: Self, rhs: Self) -> Self { Self(lhs._value / rhs._value) }
59+
static func /= (lhs: inout Self, rhs: Self) { lhs._value /= rhs._value }
60+
61+
init(_ value: Int) { self.init(_Value(value)) }
62+
init(_ value: Float) { self.init(_Value(value)) }
63+
init(_ value: Double) { self.init(_Value(value)) }
64+
init(_ value: Float80) { self.init(_Value(value)) }
65+
init(integerLiteral value: _Value.IntegerLiteralType) {
66+
self.init(_Value(integerLiteral: value))
67+
}
68+
init(floatLiteral value: _Value.FloatLiteralType) {
69+
self.init(_Value(floatLiteral: value))
70+
}
71+
init(sign: FloatingPointSign, exponent: _Value.Exponent, significand: Self) {
72+
self.init(
73+
_Value(sign: sign, exponent: exponent, significand: significand._value))
74+
}
75+
init(
76+
sign: FloatingPointSign,
77+
exponentBitPattern: _Value.RawExponent,
78+
significandBitPattern: _Value.RawSignificand
79+
) {
80+
self.init(
81+
_Value(
82+
sign: sign,
83+
exponentBitPattern: exponentBitPattern,
84+
significandBitPattern: significandBitPattern))
85+
}
86+
87+
var binade: Self { Self(_value.binade) }
88+
var exponent: _Value.Exponent { _value.exponent }
89+
var exponentBitPattern: _Value.RawExponent { _value.exponentBitPattern }
90+
var isCanonical: Bool { _value.isCanonical }
91+
var isFinite: Bool { _value.isFinite }
92+
var isInfinite: Bool { _value.isInfinite }
93+
var isNaN: Bool { _value.isNaN }
94+
var isNormal: Bool { _value.isNormal }
95+
var isSignalingNaN: Bool { _value.isSignalingNaN }
96+
var isSubnormal: Bool { _value.isSubnormal }
97+
var isZero: Bool { _value.isZero }
98+
var magnitude: Self { Self(_value.magnitude) }
99+
var nextDown: Self { Self(_value.nextDown) }
100+
var nextUp: Self { Self(_value.nextUp) }
101+
var sign: FloatingPointSign { _value.sign }
102+
var significand: Self { Self(_value.significand) }
103+
var significandBitPattern: _Value.RawSignificand {
104+
_value.significandBitPattern
105+
}
106+
var significandWidth: Int { _value.significandWidth }
107+
var ulp: Self { Self(_value.ulp) }
108+
109+
mutating func addProduct(_ lhs: Self, _ rhs: Self) {
110+
_value.addProduct(lhs._value, rhs._value)
111+
}
112+
func advanced(by n: _Value.Stride) -> Self { Self(_value.advanced(by: n)) }
113+
func distance(to other: Self) -> _Value.Stride {
114+
_value.distance(to: other._value)
115+
}
116+
mutating func formRemainder(dividingBy other: Self) {
117+
_value.formRemainder(dividingBy: other._value)
118+
}
119+
mutating func formSquareRoot() { _value.formSquareRoot() }
120+
mutating func formTruncatingRemainder(dividingBy other: Self) {
121+
_value.formTruncatingRemainder(dividingBy: other._value)
122+
}
123+
func isEqual(to other: Self) -> Bool { _value.isEqual(to: other._value) }
124+
func isLess(than other: Self) -> Bool { _value.isLess(than: other._value) }
125+
func isLessThanOrEqualTo(_ other: Self) -> Bool {
126+
_value.isLessThanOrEqualTo(other._value)
127+
}
128+
mutating func round(_ rule: FloatingPointRoundingRule) { _value.round(rule) }
129+
}
130+
131+
struct MockFloat64: MockBinaryFloatingPoint {
132+
var _value: Double
133+
init(_ _value: Double) { self._value = _value }
134+
}
135+
26136
let doubles = [
27137
1.8547832857295, 26.321549267719135, 98.9544480962058, 73.70286973782363,
28138
82.04918555938816, 76.38902969312758, 46.35647857011161, 64.0821426030317,
@@ -38,6 +148,8 @@ let doubles = [
38148
64.79777579583545, 45.25948795832151, 94.31492354198335, 52.31096166433902,
39149
]
40150

151+
let mockFloat64s = doubles.map { MockFloat64($0) }
152+
41153
@inline(__always)
42154
func convert<
43155
T: BinaryFloatingPoint, U: BinaryFloatingPoint
@@ -64,3 +176,13 @@ public func run_ConvertFloatingPoint_GenericDoubleToDouble(_ N: Int) {
64176
}
65177
}
66178
}
179+
180+
@inline(never)
181+
public func run_ConvertFloatingPoint_MockFloat64ToDouble(_ N: Int) {
182+
for _ in 0..<(N * 100) {
183+
for element in mockFloat64s {
184+
let f = Double(identity(element))
185+
blackHole(f)
186+
}
187+
}
188+
}

0 commit comments

Comments
 (0)