-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathFloatingPointPrinting.swift
221 lines (198 loc) · 6.62 KB
/
FloatingPointPrinting.swift
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
215
216
217
218
219
220
221
//===--- FloatingPointPrinting.swift -----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 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 verifies the performance of generating a text description
// from a binary floating-point value.
import TestsUtils
public let benchmarks = [
BenchmarkInfo(
name: "FloatingPointPrinting_Float_description_small",
runFunction: run_FloatingPointPrinting_Float_description_small,
tags: [.validation, .api, .runtime, .String],
legacyFactor: 108),
BenchmarkInfo(
name: "FloatingPointPrinting_Double_description_small",
runFunction: run_FloatingPointPrinting_Double_description_small,
tags: [.validation, .api, .runtime, .String],
legacyFactor: 100),
BenchmarkInfo(
name: "FloatingPointPrinting_Float80_description_small",
runFunction: run_FloatingPointPrinting_Float80_description_small,
tags: [.validation, .api, .runtime, .String],
legacyFactor: 108),
BenchmarkInfo(
name: "FloatingPointPrinting_Float_description_uniform",
runFunction: run_FloatingPointPrinting_Float_description_uniform,
tags: [.validation, .api, .runtime, .String],
legacyFactor: 100),
BenchmarkInfo(
name: "FloatingPointPrinting_Double_description_uniform",
runFunction: run_FloatingPointPrinting_Double_description_uniform,
tags: [.validation, .api, .runtime, .String],
legacyFactor: 100),
BenchmarkInfo(
name: "FloatingPointPrinting_Float80_description_uniform",
runFunction: run_FloatingPointPrinting_Float80_description_uniform,
tags: [.validation, .api, .runtime, .String],
legacyFactor: 100),
BenchmarkInfo(
name: "FloatingPointPrinting_Float_interpolated",
runFunction: run_FloatingPointPrinting_Float_interpolated,
tags: [.validation, .api, .runtime, .String],
legacyFactor: 200),
BenchmarkInfo(
name: "FloatingPointPrinting_Double_interpolated",
runFunction: run_FloatingPointPrinting_Double_interpolated,
tags: [.validation, .api, .runtime, .String],
legacyFactor: 200),
BenchmarkInfo(
name: "FloatingPointPrinting_Float80_interpolated",
runFunction: run_FloatingPointPrinting_Float80_interpolated,
tags: [.validation, .api, .runtime, .String],
legacyFactor: 200)
]
// Generate descriptions for 100,000 values around 1.0.
//
// Note that some formatting algorithms behave very
// differently for values around 1.0 than they do for
// less-common extreme values. Having a "small" test
// and a "uniform" test exercises both cases.
//
// Dividing integers 1...100000 by 101 (a prime) yields floating-point
// values from about 1e-2 to about 1e3, each with plenty of digits after
// the decimal:
@inline(never)
public func run_FloatingPointPrinting_Float_description_small(_ n: Int) {
let count = 1_000
for _ in 0..<n {
for i in 1...count {
let f = Float(i) / 101.0
blackHole(f.description)
}
}
}
@inline(never)
public func run_FloatingPointPrinting_Double_description_small(_ n: Int) {
let count = 1_000
for _ in 0..<n {
for i in 1...count {
let f = Double(i) / 101.0
blackHole(f.description)
}
}
}
@inline(never)
public func run_FloatingPointPrinting_Float80_description_small(_ n: Int) {
#if canImport(Darwin) || os(Linux)
// On Darwin, long double is Float80 on x86, and Double otherwise.
// On Linux, Float80 is at aleast available on x86.
#if arch(x86_64) || arch(i386)
let count = 1_000
for _ in 0..<n {
for i in 1...count {
let f = Float80(i) / 101.0
blackHole(f.description)
}
}
#endif // x86
#endif // Darwin/Linux
}
// Generate descriptions for 100,000 values spread evenly across
// the full range of the type:
@inline(never)
public func run_FloatingPointPrinting_Float_description_uniform(_ n: Int) {
let count = 1_000
let step = UInt32.max / UInt32(count)
for _ in 0..<n {
for i in 0..<count {
let raw = UInt32(i) * step
let f = Float(bitPattern: raw)
blackHole(f.description)
}
}
}
@inline(never)
public func run_FloatingPointPrinting_Double_description_uniform(_ n: Int) {
let count = 1_000
let step = UInt64.max / UInt64(count)
for _ in 0..<n {
for i in 0..<count {
let raw = UInt64(i) * step
let f = Double(bitPattern: raw)
blackHole(f.description)
}
}
}
@inline(never)
public func run_FloatingPointPrinting_Float80_description_uniform(_ n: Int) {
#if canImport(Darwin) || os(Linux)
// On Darwin, long double is Float80 on x86, and Double otherwise.
// On Linux, Float80 is at aleast available on x86.
#if arch(x86_64) || arch(i386)
let count = 1_000
let step = UInt64.max / UInt64(count)
for _ in 0..<n {
for i in 0..<count {
let fraction = UInt64(i) * step
let exponent = UInt(i) % 32768
let f = Float80(sign: .plus, exponentBitPattern: exponent, significandBitPattern: fraction)
blackHole(f.description)
}
}
#endif // x86
#endif // Darwin/Linux
}
// The "interpolated" tests verify that any storage optimizations used while
// producing the formatted numeric strings don't pessimize later use of the
// result.
@inline(never)
public func run_FloatingPointPrinting_Float_interpolated(_ n: Int) {
let count = 500
let step = UInt32.max / UInt32(count)
for _ in 0..<n {
for i in 0..<count {
let raw = UInt32(i) * step
let f = Float(bitPattern: raw)
blackHole("and the actual result was \(f)")
}
}
}
@inline(never)
public func run_FloatingPointPrinting_Double_interpolated(_ n: Int) {
let count = 500
let step = UInt64.max / UInt64(count)
for _ in 0..<n {
for i in 0..<count {
let raw = UInt64(i) * step
let f = Double(bitPattern: raw)
blackHole("and the actual result was \(f)")
}
}
}
@inline(never)
public func run_FloatingPointPrinting_Float80_interpolated(_ n: Int) {
#if canImport(Darwin) || os(Linux)
// On Darwin, long double is Float80 on x86, and Double otherwise.
// On Linux, Float80 is at aleast available on x86.
#if arch(x86_64) || arch(i386)
let count = 500
let step = UInt64.max / UInt64(count)
for _ in 0..<n {
for i in 0..<count {
let fraction = UInt64(i) * step
let exponent = UInt(i) % 32768
let f = Float80(sign: .plus, exponentBitPattern: exponent, significandBitPattern: fraction)
blackHole("and the actual result was \(f)")
}
}
#endif // x86
#endif // Darwin/Linux
}