Skip to content

Commit 60b7c57

Browse files
committed
[benchmark] Add mock floating-point types for conversion benchmarks
1 parent 38e724b commit 60b7c57

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

benchmark/single-source/FloatingPointConversion.swift

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,120 @@ public let FloatingPointConversion = [
2121
name: "ConvertFloatingPoint.GenericDoubleToDouble",
2222
runFunction: run_ConvertFloatingPoint_GenericDoubleToDouble,
2323
tags: [.validation, .api]),
24+
BenchmarkInfo(
25+
name: "ConvertFloatingPoint.MockFloat64ToDouble",
26+
runFunction: run_ConvertFloatingPoint_MockFloat64ToDouble,
27+
tags: [.validation, .api]),
2428
]
2529

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

153+
let mockFloat64s = doubles.map { MockFloat64($0) }
154+
41155
@inline(__always)
42156
func convert<
43157
T: BinaryFloatingPoint, U: BinaryFloatingPoint
@@ -64,3 +178,13 @@ public func run_ConvertFloatingPoint_GenericDoubleToDouble(_ N: Int) {
64178
}
65179
}
66180
}
181+
182+
@inline(never)
183+
public func run_ConvertFloatingPoint_MockFloat64ToDouble(_ N: Int) {
184+
for _ in 0..<(N * 100) {
185+
for element in mockFloat64s {
186+
let f = Double(identity(element))
187+
blackHole(f)
188+
}
189+
}
190+
}

0 commit comments

Comments
 (0)