Skip to content

Commit bb954f9

Browse files
authored
Merge pull request #33975 from xwu/benchmark-floating-point-conversion-exactly
[benchmark] Add another test to floating point conversion benchmark
2 parents 734b8a2 + cb96bfb commit bb954f9

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

benchmark/single-source/FloatingPointConversion.swift

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ public let FloatingPointConversion = [
1818
runFunction: run_ConvertFloatingPoint_MockFloat64ToDouble,
1919
tags: [.validation, .api],
2020
setUpFunction: { blackHole(mockFloat64s) }),
21+
BenchmarkInfo(
22+
name: "ConvertFloatingPoint.MockFloat64Exactly",
23+
runFunction: run_ConvertFloatingPoint_MockFloat64Exactly,
24+
tags: [.validation, .api],
25+
setUpFunction: { blackHole(mockFloat64s) }),
26+
BenchmarkInfo(
27+
name: "ConvertFloatingPoint.MockFloat64Exactly2",
28+
runFunction: run_ConvertFloatingPoint_MockFloat64Exactly2,
29+
tags: [.validation, .api],
30+
setUpFunction: { blackHole(mockFloat64s) }),
2131
BenchmarkInfo(
2232
name: "ConvertFloatingPoint.MockFloat64ToInt64",
2333
runFunction: run_ConvertFloatingPoint_MockFloat64ToInt64,
@@ -130,6 +140,11 @@ struct MockFloat64: MockBinaryFloatingPoint {
130140
init(_ _value: Double) { self._value = _value }
131141
}
132142

143+
struct MockFloat32: MockBinaryFloatingPoint {
144+
var _value: Float
145+
init(_ _value: Float) { self._value = _value }
146+
}
147+
133148
let doubles = [
134149
1.8547832857295, 26.321549267719135, 98.9544480962058, 73.70286973782363,
135150
82.04918555938816, 76.38902969312758, 46.35647857011161, 64.0821426030317,
@@ -147,20 +162,40 @@ let doubles = [
147162

148163
let mockFloat64s = doubles.map { MockFloat64($0) }
149164

165+
// See also: test/SILOptimizer/floating_point_conversion.swift
166+
167+
@inline(never)
168+
public func run_ConvertFloatingPoint_MockFloat64ToDouble(_ N: Int) {
169+
for _ in 0..<(N * 100) {
170+
for element in mockFloat64s {
171+
let f = Double(identity(element))
172+
blackHole(f)
173+
}
174+
}
175+
}
176+
150177
@inline(__always)
151178
func convert<
152179
T: BinaryFloatingPoint, U: BinaryFloatingPoint
153-
>(_ value: T, to: U.Type) -> U {
154-
U(value)
180+
>(exactly value: T, to: U.Type) -> U? {
181+
U(exactly: value)
155182
}
156183

157-
// See also: test/SILOptimizer/floating_point_conversion.swift
184+
@inline(never)
185+
public func run_ConvertFloatingPoint_MockFloat64Exactly(_ N: Int) {
186+
for _ in 0..<(N * 25) {
187+
for element in mockFloat64s {
188+
let f = convert(exactly: identity(element), to: Double.self)
189+
blackHole(f)
190+
}
191+
}
192+
}
158193

159194
@inline(never)
160-
public func run_ConvertFloatingPoint_MockFloat64ToDouble(_ N: Int) {
161-
for _ in 0..<(N * 100) {
195+
public func run_ConvertFloatingPoint_MockFloat64Exactly2(_ N: Int) {
196+
for _ in 0..<(N * 25) {
162197
for element in mockFloat64s {
163-
let f = Double(identity(element))
198+
let f = convert(exactly: identity(element), to: MockFloat32.self)
164199
blackHole(f)
165200
}
166201
}

0 commit comments

Comments
 (0)