3
3
// RUN: %S/../../utils/line-directive %t/FloatingPoint.swift -- %target-run %t/a.out
4
4
// REQUIRES: executable_test
5
5
6
+ %{
7
+ from gyb_stdlib_unittest_support import TRACE, stackTrace, trace
8
+ }%
9
+
6
10
import Swift
7
11
import StdlibUnittest
8
12
9
-
10
13
#if arch(i386) || arch(x86_64)
11
14
12
15
struct Float80Bits : Equatable, CustomStringConvertible {
@@ -19,7 +22,7 @@ struct Float80Bits : Equatable, CustomStringConvertible {
19
22
}
20
23
21
24
var description: String {
22
- return "(\(String(signAndExponent, radix: 16)) \(String(significand, radix: 16)))) "
25
+ return "(\(String(signAndExponent, radix: 16)) \(String(significand, radix: 16)))"
23
26
}
24
27
}
25
28
@@ -51,6 +54,29 @@ extension Float80 {
51
54
52
55
#endif
53
56
57
+ % for (FloatTy, BitPatternTy) in [
58
+ % ('Float', 'UInt32'),
59
+ % ('Double', 'UInt64'),
60
+ % ('Float80', 'Float80Bits')
61
+ % ]:
62
+ % if FloatTy == 'Float80':
63
+ #if arch(i386) || arch(x86_64)
64
+ % end
65
+ func expectBitwiseEqual(
66
+ _ expected: ${FloatTy}, _ actual: ${FloatTy}, ${TRACE}
67
+ ) {
68
+ expectEqual(expected.bitPattern, actual.bitPattern, ${trace})
69
+ }
70
+ func expectBitwiseEqual(
71
+ bitPattern expected: ${BitPatternTy}, _ actual: ${FloatTy}, ${TRACE}
72
+ ) {
73
+ expectBitwiseEqual(${FloatTy}(bitPattern: expected), actual, ${trace})
74
+ }
75
+ % if FloatTy == 'Float80':
76
+ #endif
77
+ % end
78
+ % end
79
+
54
80
var FloatingPoint = TestSuite("FloatingPoint")
55
81
56
82
func positiveOne<T: IntegerLiteralConvertible>() -> T {
@@ -65,6 +91,29 @@ FloatingPoint.test("Float/IntegerLiteralConvertible") {
65
91
expectEqual(negativeOne(), -1.0 as Float)
66
92
}
67
93
94
+ FloatingPoint.test("Float/FloatLiteralConvertible") {
95
+ let x: Float = -0.0
96
+ expectBitwiseEqual(bitPattern: 0x8000_0000, x)
97
+ }
98
+
99
+ FloatingPoint.test("Float/staticProperties") {
100
+ typealias Ty = Float
101
+ // From the FloatingPoint protocol.
102
+ expectEqual(2, Ty.radix)
103
+ expectBitwiseEqual(bitPattern: 0x7fc0_0000, Ty.nan)
104
+ expectBitwiseEqual(bitPattern: 0x7fa0_0000, Ty.signalingNaN)
105
+ expectBitwiseEqual(bitPattern: 0x7f80_0000, Ty.infinity)
106
+ expectBitwiseEqual(0x1.ffff_fe__p127, Ty.greatestFiniteMagnitude)
107
+ expectBitwiseEqual(0x1.921f_b6__p1, Ty.pi)
108
+ expectBitwiseEqual(0x1.0p-23, Ty.ulpOfOne)
109
+ expectBitwiseEqual(0x1.0p-126, Ty.leastNormalMagnitude)
110
+ expectBitwiseEqual(0x1.0p-149, Ty.leastNonzeroMagnitude)
111
+
112
+ // From the BinaryFloatingPoint protocol.
113
+ expectEqual(8, Ty.exponentBitCount)
114
+ expectEqual(23, Ty.significandBitCount)
115
+ }
116
+
68
117
// Tests the float and int conversions work correctly. Each case is special.
69
118
FloatingPoint.test("Float/UInt8") {
70
119
expectEqual(UInt8.min, UInt8(Float(UInt8.min)))
@@ -115,6 +164,29 @@ FloatingPoint.test("Double/IntegerLiteralConvertible") {
115
164
expectEqual(negativeOne(), -1.0 as Double)
116
165
}
117
166
167
+ FloatingPoint.test("Double/FloatLiteralConvertible") {
168
+ let x: Double = -0.0
169
+ expectBitwiseEqual(bitPattern: 0x8000_0000_0000_0000, x)
170
+ }
171
+
172
+ FloatingPoint.test("Double/staticProperties") {
173
+ typealias Ty = Double
174
+ // From the FloatingPoint protocol.
175
+ expectEqual(2, Ty.radix)
176
+ expectBitwiseEqual(bitPattern: 0x7ff8_0000_0000_0000, Ty.nan)
177
+ expectBitwiseEqual(bitPattern: 0x7ff4_0000_0000_0000, Ty.signalingNaN)
178
+ expectBitwiseEqual(bitPattern: 0x7ff0_0000_0000_0000, Ty.infinity)
179
+ expectBitwiseEqual(0x1.ffff_ffff_ffff_f__p1023, Ty.greatestFiniteMagnitude)
180
+ expectBitwiseEqual(0x1.921f_b544_42d1_8__p1, Ty.pi)
181
+ expectBitwiseEqual(0x1.0p-52, Ty.ulpOfOne)
182
+ expectBitwiseEqual(0x1.0p-1022, Ty.leastNormalMagnitude)
183
+ expectBitwiseEqual(0x1.0p-1074, Ty.leastNonzeroMagnitude)
184
+
185
+ // From the BinaryFloatingPoint protocol.
186
+ expectEqual(11, Ty.exponentBitCount)
187
+ expectEqual(52, Ty.significandBitCount)
188
+ }
189
+
118
190
FloatingPoint.test("Double/UInt8") {
119
191
expectEqual(UInt8.min, UInt8(Double(UInt8.min)))
120
192
expectEqual(UInt8.max, UInt8(Double(UInt8.max)))
@@ -174,37 +246,35 @@ FloatingPoint.test("Double/HashValueZero") {
174
246
let floatNextUpDownTests: [(Float, Float)] = [
175
247
(.nan, .nan),
176
248
(.greatestFiniteMagnitude, .infinity),
177
- (-.infinity, -.greatestFiniteMagnitude),
178
- (0x1.fffffep-1, 1.0), (1.0, 0x1.000002p+0),
179
- (-0x1p-149, -0.0), (0.0, 0x1p-149),
180
- (0x1.effffep-1, 0.96875), (0.96875, 0x1.f00002p-1),
249
+ (0x1.ffff_fe__p-1, 1.0), (1.0, 0x1.0000_02__p+0),
250
+ (0.0, .leastNonzeroMagnitude),
251
+ (0x1.efff_fe__p-1, 0x1.fp-1), (0x1.fp-1, 0x1.f000_02__p-1),
181
252
]
182
253
183
254
FloatingPoint.test("Float.nextUp, .nextDown")
184
255
.forEach(in: floatNextUpDownTests) {
185
256
(prev, succ) in
186
- expectEqual (succ.bitPattern , prev.nextUp.bitPattern )
187
- expectEqual (prev.bitPattern , succ.nextDown.bitPattern )
188
- expectEqual(( -succ).bitPattern , (-prev).nextDown.bitPattern )
189
- expectEqual(( -prev).bitPattern , (-succ).nextUp.bitPattern)
257
+ expectBitwiseEqual (succ, prev.nextUp)
258
+ expectBitwiseEqual (prev, succ.nextDown)
259
+ expectBitwiseEqual( -succ, (-prev).nextDown)
260
+ expectBitwiseEqual( -prev, (-succ).nextUp)
190
261
}
191
262
192
263
let doubleNextUpDownTests: [(Double, Double)] = [
193
264
(.nan, .nan),
194
265
(.greatestFiniteMagnitude, .infinity),
195
- (-.infinity, -.greatestFiniteMagnitude),
196
- (0x1.fffffffffffffp-1, 1.0), (1.0, 0x1.0000000000001p+0),
197
- (-0x1p-1074, -0.0), (0.0, 0x1p-1074),
198
- (0x1.effffffffffffp-1, 0.96875), (0.96875, 0x1.f000000000001p-1),
266
+ (0x1.ffff_ffff_ffff_fp-1, 1.0), (1.0, 0x1.0000_0000_0000_1p+0),
267
+ (0.0, .leastNonzeroMagnitude),
268
+ (0x1.efff_ffff_ffff_fp-1, 0x1.fp-1), (0x1.fp-1, 0x1.f000_0000_0000_1p-1),
199
269
]
200
270
201
271
FloatingPoint.test("Double.nextUp, .nextDown")
202
272
.forEach(in: doubleNextUpDownTests) {
203
273
(prev, succ) in
204
- expectEqual (succ.bitPattern , prev.nextUp.bitPattern )
205
- expectEqual (prev.bitPattern , succ.nextDown.bitPattern )
206
- expectEqual(( -succ).bitPattern , (-prev).nextDown.bitPattern )
207
- expectEqual(( -prev).bitPattern , (-succ).nextUp.bitPattern)
274
+ expectBitwiseEqual (succ, prev.nextUp)
275
+ expectBitwiseEqual (prev, succ.nextDown)
276
+ expectBitwiseEqual( -succ, (-prev).nextDown)
277
+ expectBitwiseEqual( -prev, (-succ).nextUp)
208
278
}
209
279
210
280
#if arch(i386) || arch(x86_64)
@@ -214,13 +284,55 @@ FloatingPoint.test("Float80/IntegerLiteralConvertible") {
214
284
expectEqual(negativeOne(), -1.0 as Float80)
215
285
}
216
286
287
+ FloatingPoint.test("Float80/FloatLiteralConvertible") {
288
+ let x: Float80 = -0.0
289
+ expectBitwiseEqual(bitPattern: Float80Bits(0x8000, 0), x)
290
+ }
291
+
292
+ FloatingPoint.test("Float80/staticProperties") {
293
+ typealias Ty = Double
294
+ // From the FloatingPoint protocol.
295
+ expectEqual(2, Ty.radix)
296
+ expectBitwiseEqual(bitPattern: 0x7ff8_0000_0000_0000, Ty.nan)
297
+ expectBitwiseEqual(bitPattern: 0x7ff4_0000_0000_0000, Ty.signalingNaN)
298
+ expectBitwiseEqual(bitPattern: 0x7ff0_0000_0000_0000, Ty.infinity)
299
+ expectBitwiseEqual(0x1.ffff_ffff_ffff_f__p1023, Ty.greatestFiniteMagnitude)
300
+ expectBitwiseEqual(0x1.921f_b544_42d1_8__p1, Ty.pi)
301
+ expectBitwiseEqual(0x1.0p-52, Ty.ulpOfOne)
302
+ expectBitwiseEqual(0x1.0p-1022, Ty.leastNormalMagnitude)
303
+ expectBitwiseEqual(0x1.0p-1074, Ty.leastNonzeroMagnitude)
304
+
305
+ // From the BinaryFloatingPoint protocol.
306
+ expectEqual(11, Ty.exponentBitCount)
307
+ expectEqual(52, Ty.significandBitCount)
308
+ }
309
+
217
310
FloatingPoint.test("Float80/HashValueZero") {
218
311
let zero: Float80 = getFloat80(0.0)
219
312
let negativeZero: Float80 = getFloat80(-0.0)
220
313
expectNotEqual(zero.bitPattern, negativeZero.bitPattern)
221
314
expectEqual(zero.hashValue, negativeZero.hashValue)
222
315
}
223
316
317
+ let float80NextUpDownTests: [(Float80, Float80)] = [
318
+ (.nan, .nan),
319
+ (.greatestFiniteMagnitude, .infinity),
320
+ (0x1.ffff_ffff_ffff_fffep-1, 1.0), (1.0, 0x1.0000_0000_0000_0002p+0),
321
+ (0.0, .leastNonzeroMagnitude),
322
+ (0x1.efff_ffff_ffff_fffep-1, 0x1.fp-1),
323
+ (0x1.fp-1, 0x1.f000_0000_0000_0002p-1),
324
+ ]
325
+
326
+ FloatingPoint.test("Float80.nextUp, .nextDown")
327
+ .forEach(in: float80NextUpDownTests) {
328
+ (prev, succ) in
329
+
330
+ expectBitwiseEqual(succ, prev.nextUp)
331
+ expectBitwiseEqual(prev, succ.nextDown)
332
+ expectBitwiseEqual(-succ, (-prev).nextDown)
333
+ expectBitwiseEqual(-prev, (-succ).nextUp)
334
+ }
335
+
224
336
#endif
225
337
226
338
% for FloatSelf in ['Float32', 'Float64']:
0 commit comments