/
FloatingPointTypes.swift.gyb
1070 lines (951 loc) · 33.1 KB
/
FloatingPointTypes.swift.gyb
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- FloatingPointTypes.swift.gyb -------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
import SwiftShims
%{
from SwiftFloatingPointTypes import all_floating_point_types
#
# Utility code for later in this template
#
# Bit counts for all int types
allIntBits = [8, 16, 32, 64, 'Int']
# Number of bits in the Builtin.Word type
word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
# Number of bits in integer literals.
builtinIntLiteralBits = 2048
def allInts():
for bits in allIntBits:
for signed in False, True:
yield bits, signed
def baseIntName(name):
return 'Int' if name == 'Int' else 'Int' + str(name)
def builtinIntName(name):
return 'Int' + str(word_bits) if name == 'Int' else 'Int' + str(name)
def intName(name, signed):
return ('' if signed else 'U') + baseIntName(name)
}%
// TODO: remove once integer proposal is available ----------------------------
%for bits in [32,64]:
extension UInt${bits} {
var signBitIndex: Int {
return ${bits-1} - Int(Int${bits}(Builtin.int_ctlz_Int${bits}(self._value, false._value)))
}
var countTrailingZeros: Int {
return Int(Int${bits}(Builtin.int_cttz_Int${bits}(self._value, false._value)))
}
}
%end
% for self_type in all_floating_point_types():
%{
Self = self_type.stdlib_name
bits = self_type.bits
cFuncSuffix = self_type.cFuncSuffix
SignificandSize = self_type.significand_size
SignificandBitCount = self_type.significand_bits
ExponentBitCount = self_type.exponent_bits
RawSignificand = 'UInt' + str(SignificandSize)
if Self == 'Float':
SelfDocComment = '''\
/// A single-precision, floating-point value type.'''
elif Self == 'Double':
SelfDocComment = '''\
/// A double-precision, floating-point value type.'''
elif Self == 'Float80':
SelfDocComment = '''\
/// An extended-precision, floating-point value type.'''
else:
raise ValueError('Unhandled float type.')
}%
% if bits == 80:
#if !os(Windows) && (arch(i386) || arch(x86_64))
% end
${SelfDocComment}
@_fixed_layout
public struct ${Self} {
public // @testable
var _value: Builtin.FPIEEE${bits}
/// Creates a value initialized to zero.
@_transparent public
init() {
let zero: Int64 = 0
self._value = Builtin.sitofp_Int64_FPIEEE${bits}(zero._value)
}
@_transparent
public // @testable
init(_bits v: Builtin.FPIEEE${bits}) {
self._value = v
}
}
extension ${Self} : CustomStringConvertible {
/// A textual representation of the value.
public var description: String {
return _float${bits}ToString(self, debug: false)
}
}
extension ${Self} : CustomDebugStringConvertible {
/// A textual representation of the value, suitable for debugging.
public var debugDescription: String {
return _float${bits}ToString(self, debug: true)
}
}
extension ${Self}: BinaryFloatingPoint {
public typealias Exponent = Int
public typealias RawSignificand = ${RawSignificand}
public static var exponentBitCount: Int {
return ${ExponentBitCount}
}
%if bits == 80:
/// The available number of fractional significand bits.
///
/// `Float80.significandBitCount` is 63, even though 64 bits are used to
/// store the significand in the memory representation of a `Float80`
/// instance. Unlike other floating-point types, the `Float80` type
/// explicitly stores the leading integral significand bit.
%end
public static var significandBitCount: Int {
return ${SignificandBitCount}
}
// Implementation details.
@_versioned
static var _infinityExponent: UInt {
@inline(__always) get { return 1 << UInt(exponentBitCount) - 1 }
}
static var _exponentBias: UInt {
@inline(__always) get { return _infinityExponent >> 1 }
}
static var _significandMask: ${RawSignificand} {
@inline(__always) get {
return 1 << ${RawSignificand}(significandBitCount) - 1
}
}
@_versioned
static var _quietNaNMask: ${RawSignificand} {
@inline(__always) get {
return 1 << ${RawSignificand}(significandBitCount - 1)
}
}
%if bits != 80:
// Conversions to/from integer encoding. These are not part of the
// BinaryFloatingPoint prototype because there's no guarantee that an
// integer type of the same size actually exists (e.g. Float80).
//
// If we want them in a protocol at some future point, that protocol should
// be "InterchangeFloatingPoint" or "PortableFloatingPoint" or similar, and
// apply to IEEE 754 "interchange types".
/// The bit pattern of the value's encoding.
///
/// The bit pattern matches the binary interchange format defined by the
/// [IEEE 754 specification][spec].
///
/// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
///
/// - SeeAlso: `init(bitPattern:)`
public var bitPattern: UInt${bits} {
return UInt${bits}(Builtin.bitcast_FPIEEE${bits}_Int${bits}(_value))
}
/// Creates a new value with the given bit pattern.
///
/// The value passed as `bitPattern` is interpreted in the binary interchange
/// format defined by the [IEEE 754 specification][spec].
///
/// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
///
/// - Parameter bitPattern: The integer encoding of a `${Self}` instance.
///
/// - SeeAlso: `bitPattern`
public init(bitPattern: UInt${bits}) {
self.init(_bits: Builtin.bitcast_Int${bits}_FPIEEE${bits}(bitPattern._value))
}
public var sign: FloatingPointSign {
let shift = ${Self}.significandBitCount + ${Self}.exponentBitCount
return FloatingPointSign(rawValue: Int(bitPattern >> ${RawSignificand}(shift)))!
}
@available(*, unavailable, renamed: "sign")
public var isSignMinus: Bool { Builtin.unreachable() }
public var exponentBitPattern: UInt {
return UInt(bitPattern >> UInt${bits}(${Self}.significandBitCount)) &
${Self}._infinityExponent
}
public var significandBitPattern: ${RawSignificand} {
return ${RawSignificand}(bitPattern) & ${Self}._significandMask
}
public init(sign: FloatingPointSign,
exponentBitPattern: UInt,
significandBitPattern: ${RawSignificand}) {
let signShift = ${Self}.significandBitCount + ${Self}.exponentBitCount
let sign = UInt${bits}(sign == .minus ? 1 : 0)
let exponent = UInt${bits}(
exponentBitPattern & ${Self}._infinityExponent)
let significand = UInt${bits}(
significandBitPattern & ${Self}._significandMask)
self.init(bitPattern:
sign << UInt${bits}(signShift) |
exponent << UInt${bits}(${Self}.significandBitCount) |
significand)
}
public var isCanonical: Bool {
return true
}
%else:
// Internal implementation details of x86 Float80
struct _Float80Representation {
var explicitSignificand: UInt64
var signAndExponent: UInt16
var _padding: (UInt16, UInt16, UInt16) = (0, 0, 0)
var sign: FloatingPointSign {
return FloatingPointSign(rawValue: Int(signAndExponent >> 15))!
}
var exponentBitPattern: UInt { return UInt(signAndExponent) & 0x7fff }
init(explicitSignificand: UInt64, signAndExponent: UInt16) {
self.explicitSignificand = explicitSignificand
self.signAndExponent = signAndExponent
}
}
var _representation: _Float80Representation {
return unsafeBitCast(self, to: _Float80Representation.self)
}
public var sign: FloatingPointSign {
return _representation.sign
}
static var _explicitBitMask: UInt64 {
@inline(__always) get { return 1 << 63 }
}
public var exponentBitPattern: UInt {
let provisional = _representation.exponentBitPattern
if provisional == 0 {
if _representation.explicitSignificand >= Float80._explicitBitMask {
// Pseudo-denormals have an exponent of 0 but the leading bit of the
// significand field is set. These are noncanonical encodings of the
// same significand with an exponent of 1.
return 1
}
// Exponent is zero, leading bit of significand is clear, so this is
// a canonical zero or subnormal number.
return 0
}
if _representation.explicitSignificand < Float80._explicitBitMask {
// If the exponent is not-zero but the leading bit of the significand
// is clear, then we have an invalid operand (unnormal, pseudo-inf, or
// pseudo-NaN). All of these are noncanonical encodings of NaN.
return Float80._infinityExponent
}
// We have a canonical number, so the provisional exponent is correct.
return provisional
}
public var significandBitPattern: UInt64 {
if _representation.exponentBitPattern > 0 &&
_representation.explicitSignificand < Float80._explicitBitMask {
// If the exponent is nonzero and the leading bit of the significand
// is clear, then we have an invalid operand (unnormal, pseudo-inf, or
// pseudo-NaN). All of these are noncanonical encodings of qNaN.
return _representation.explicitSignificand | Float80._quietNaNMask
}
// Otherwise we always get the "right" significand by simply clearing the
// integral bit.
return _representation.explicitSignificand & Float80._significandMask
}
public init(sign: FloatingPointSign,
exponentBitPattern: UInt,
significandBitPattern: UInt64) {
let signBit = UInt16(sign == .minus ? 0x8000 : 0)
let exponent = UInt16(exponentBitPattern)
var significand = significandBitPattern
if exponent != 0 { significand |= Float80._explicitBitMask }
let rep = _Float80Representation(explicitSignificand: significand,
signAndExponent: signBit|exponent)
self = unsafeBitCast(rep, to: Float80.self)
}
public var isCanonical: Bool {
if exponentBitPattern == 0 {
// If exponent field is zero, canonical numbers have the explicit
// significand bit clear.
return _representation.explicitSignificand < Float80._explicitBitMask
}
// If exponent is nonzero, canonical values have the explicit significand
// bit set.
return _representation.explicitSignificand >= Float80._explicitBitMask
}
%end
public static var infinity: ${Self} {
return ${Self}(sign: .plus,
exponentBitPattern: _infinityExponent,
significandBitPattern: 0)
}
public static var nan: ${Self} {
return ${Self}(nan: 0, signaling: false)
}
public static var signalingNaN: ${Self} {
return ${Self}(nan: 0, signaling: true)
}
@available(*, unavailable, renamed: "nan")
public static var quietNaN: ${Self} { Builtin.unreachable()}
public static var greatestFiniteMagnitude: ${Self} {
return ${Self}(sign: .plus,
exponentBitPattern: _infinityExponent - 1,
significandBitPattern: _significandMask)
}
public static var pi: ${Self} {
%if bits == 32:
// Note: this is not the correctly rounded (to nearest) value of pi,
// because pi would round *up* in Float precision, which can result
// in angles in the wrong quadrant if users aren't careful. This is
// not a problem for Double or Float80, as pi rounds down in both of
// those formats.
return Float(0x1.921fb4p1)
%elif bits == 64:
return Double(0x1.921fb54442d18p1)
%elif bits == 80:
return Float80(0x1.921fb54442d1846ap1)
%end
}
public var ulp: ${Self} {
if !isFinite { return ${Self}.nan }
if exponentBitPattern > UInt(${Self}.significandBitCount) {
// self is large enough that self.ulp is normal, so we just compute its
// exponent and construct it with a significand of zero.
let ulpExponent = exponentBitPattern - UInt(${Self}.significandBitCount)
return ${Self}(sign: .plus,
exponentBitPattern: ulpExponent,
significandBitPattern: 0)
}
if exponentBitPattern >= 1 {
// self is normal but ulp is subnormal.
let ulpShift = ${RawSignificand}(exponentBitPattern - 1)
return ${Self}(sign: .plus,
exponentBitPattern: 0,
significandBitPattern: 1 << ulpShift)
}
return ${Self}(sign: .plus,
exponentBitPattern: 0,
significandBitPattern: 1)
}
public static var leastNormalMagnitude: ${Self} {
return ${Self}(sign: .plus,
exponentBitPattern: 1,
significandBitPattern: 0)
}
public static var leastNonzeroMagnitude: ${Self} {
#if arch(arm)
return leastNormalMagnitude
#else
return ${Self}(sign: .plus,
exponentBitPattern: 0,
significandBitPattern: 1)
#endif
}
public var exponent: Int {
if !isFinite { return .max }
if isZero { return .min }
let provisional = Int(exponentBitPattern) - Int(${Self}._exponentBias)
if isNormal { return provisional }
let shift = ${Self}.significandBitCount - significandBitPattern.signBitIndex
return provisional + 1 - Int(shift)
}
public var significand: ${Self} {
if isNaN { return self }
if isNormal {
return ${Self}(sign: .plus,
exponentBitPattern: ${Self}._exponentBias,
significandBitPattern: significandBitPattern)
}
if isSubnormal {
let shift = ${Self}.significandBitCount - significandBitPattern.signBitIndex
return ${Self}(sign: .plus,
exponentBitPattern: ${Self}._exponentBias,
significandBitPattern: significandBitPattern << ${RawSignificand}(shift))
}
// zero or infinity.
return ${Self}(sign: .plus,
exponentBitPattern: exponentBitPattern,
significandBitPattern: 0)
}
public init(sign: FloatingPointSign, exponent: Int, significand: ${Self}) {
var result = significand
if sign == .minus { result = -result }
if significand.isFinite && !significand.isZero {
var clamped = exponent
let leastNormalExponent = 1 - Int(${Self}._exponentBias)
let greatestFiniteExponent = Int(${Self}._exponentBias)
if clamped < leastNormalExponent {
clamped = max(clamped, 3*leastNormalExponent)
while clamped < leastNormalExponent {
result *= ${Self}.leastNormalMagnitude
clamped -= leastNormalExponent
}
}
else if clamped > greatestFiniteExponent {
clamped = min(clamped, 3*greatestFiniteExponent)
let step = ${Self}(sign: .plus,
exponentBitPattern: ${Self}._infinityExponent - 1,
significandBitPattern: 0)
while clamped > greatestFiniteExponent {
result *= step
clamped -= greatestFiniteExponent
}
}
let scale = ${Self}(sign: .plus,
exponentBitPattern: UInt(Int(${Self}._exponentBias) + clamped),
significandBitPattern: 0)
result = result * scale
}
self = result
}
/// Creates a NaN ("not a number") value with the specified payload.
///
/// NaN values compare not equal to every value, including themselves. Most
/// operations with a NaN operand produce a NaN result. Don't use the
/// equal-to operator (`==`) to test whether a value is NaN. Instead, use
/// the value's `isNaN` property.
///
/// let x = ${Self}(nan: 0, signaling: false)
/// print(x == .nan)
/// // Prints "false"
/// print(x.isNaN)
/// // Prints "true"
///
/// - Parameters:
/// - payload: The payload to use for the new NaN value.
/// - signaling: Pass `true` to create a signaling NaN or `false` to create
/// a quiet NaN.
public init(nan payload: RawSignificand, signaling: Bool) {
// We use significandBitCount - 2 bits for NaN payload.
_precondition(payload < (${Self}._quietNaNMask >> 1),
"NaN payload is not encodable.")
var significand = payload
significand |= ${Self}._quietNaNMask >> (signaling ? 1 : 0)
self.init(sign: .plus,
exponentBitPattern: ${Self}._infinityExponent,
significandBitPattern: significand)
}
public var nextUp: ${Self} {
if isNaN { return self }
if sign == .minus {
#if arch(arm)
// On arm, subnormals are flushed to zero.
if (exponentBitPattern == 1 && significandBitPattern == 0) ||
(exponentBitPattern == 0 && significandBitPattern != 0) {
return ${Self}(sign: .minus,
exponentBitPattern: 0,
significandBitPattern: 0)
}
#endif
if significandBitPattern == 0 {
if exponentBitPattern == 0 {
return .leastNonzeroMagnitude
}
return ${Self}(sign: .minus,
exponentBitPattern: exponentBitPattern - 1,
significandBitPattern: ${Self}._significandMask)
}
return ${Self}(sign: .minus,
exponentBitPattern: exponentBitPattern,
significandBitPattern: significandBitPattern - 1)
}
if isInfinite { return self }
if significandBitPattern == ${Self}._significandMask {
return ${Self}(sign: .plus,
exponentBitPattern: exponentBitPattern + 1,
significandBitPattern: 0)
}
#if arch(arm)
// On arm, subnormals are skipped.
if exponentBitPattern == 0 {
return .leastNonzeroMagnitude
}
#endif
return ${Self}(sign: .plus,
exponentBitPattern: exponentBitPattern,
significandBitPattern: significandBitPattern + 1)
}
@_transparent
public mutating func round(_ rule: FloatingPointRoundingRule) {
switch rule {
case .toNearestOrAwayFromZero:
_value = Builtin.int_round_FPIEEE${bits}(_value)
case .toNearestOrEven:
_value = Builtin.int_rint_FPIEEE${bits}(_value)
case .towardZero:
_value = Builtin.int_trunc_FPIEEE${bits}(_value)
case .awayFromZero:
if sign == .minus {
_value = Builtin.int_floor_FPIEEE${bits}(_value)
}
else {
_value = Builtin.int_ceil_FPIEEE${bits}(_value)
}
case .up:
_value = Builtin.int_ceil_FPIEEE${bits}(_value)
case .down:
_value = Builtin.int_floor_FPIEEE${bits}(_value)
}
}
@_transparent
public mutating func negate() {
_value = Builtin.fneg_FPIEEE${bits}(self._value)
}
@_transparent
public mutating func add(_ other: ${Self}) {
_value = Builtin.fadd_FPIEEE${bits}(self._value, other._value)
}
@_transparent
public mutating func subtract(_ other: ${Self}) {
_value = Builtin.fsub_FPIEEE${bits}(self._value, other._value)
}
@_transparent
public mutating func multiply(by other: ${Self}) {
_value = Builtin.fmul_FPIEEE${bits}(self._value, other._value)
}
@_transparent
public mutating func divide(by other: ${Self}) {
_value = Builtin.fdiv_FPIEEE${bits}(self._value, other._value)
}
@_transparent
public mutating func formRemainder(dividingBy other: ${Self}) {
%if bits == 80:
var other = other
_swift_stdlib_remainderl(&self, &other)
%else:
self = _swift_stdlib_remainder${cFuncSuffix}(self, other)
%end
}
@_transparent
public mutating func formTruncatingRemainder(dividingBy other: ${Self}) {
_value = Builtin.frem_FPIEEE${bits}(self._value, other._value)
}
@_transparent
public mutating func formSquareRoot( ) {
%if bits == 80:
_swift_stdlib_squareRootl(&self)
%else:
self = _swift_stdlib_squareRoot${cFuncSuffix}(self)
%end
}
@_transparent
public mutating func addProduct(_ lhs: ${Self}, _ rhs: ${Self}) {
_value = Builtin.int_fma_FPIEEE${bits}(lhs._value, rhs._value, _value)
}
@_transparent
public func isEqual(to other: ${Self}) -> Bool {
return Bool(Builtin.fcmp_oeq_FPIEEE${bits}(self._value, other._value))
}
@_transparent
public func isLess(than other: ${Self}) -> Bool {
return Bool(Builtin.fcmp_olt_FPIEEE${bits}(self._value, other._value))
}
@_transparent
public func isLessThanOrEqualTo(_ other: ${Self}) -> Bool {
return Bool(Builtin.fcmp_ole_FPIEEE${bits}(self._value, other._value))
}
@_transparent
public var isNormal: Bool {
return exponentBitPattern > 0 && isFinite
}
@_transparent
public var isFinite: Bool {
return exponentBitPattern < ${Self}._infinityExponent
}
@_transparent
public var isZero: Bool {
return exponentBitPattern == 0 && significandBitPattern == 0
}
@_transparent
public var isSubnormal: Bool {
return exponentBitPattern == 0 && significandBitPattern != 0
}
@_transparent
public var isInfinite: Bool {
return !isFinite && significandBitPattern == 0
}
@_transparent
public var isNaN: Bool {
return !isFinite && significandBitPattern != 0
}
@_transparent
public var isSignalingNaN: Bool {
return isNaN && (significandBitPattern & ${Self}._quietNaNMask) == 0
}
public var binade: ${Self} {
if !isFinite { return .nan }
if exponentBitPattern != 0 {
return ${Self}(sign: sign, exponentBitPattern: exponentBitPattern,
significandBitPattern: 0)
}
if significandBitPattern == 0 { return self }
// For subnormals, we isolate the leading significand bit.
let index = significandBitPattern.signBitIndex
return ${Self}(sign: sign, exponentBitPattern: 0,
significandBitPattern: 1 << RawSignificand(index))
}
public var significandWidth: Int {
let trailingZeros = significandBitPattern.countTrailingZeros
if isNormal {
guard significandBitPattern != 0 else { return 0 }
return ${Self}.significandBitCount - trailingZeros
}
if isSubnormal {
return significandBitPattern.signBitIndex - trailingZeros
}
return -1
}
/// Creates a new value from the given floating-point literal.
///
/// Do not call this initializer directly. It is used by the compiler when
/// you create a new `${Self}` instance by using a floating-point literal.
/// Instead, create a new value by using a literal.
///
/// In this example, the assignment to the `x` constant calls this
/// initializer behind the scenes.
///
/// let x: ${Self} = 21.25
/// // x == 21.25
///
/// - Parameter value: The new floating-point value.
@_transparent
public init(floatLiteral value: ${Self}) {
self = value
}
}
extension ${Self} : _ExpressibleByBuiltinIntegerLiteral, ExpressibleByIntegerLiteral {
@_transparent
public
init(_builtinIntegerLiteral value: Builtin.Int${builtinIntLiteralBits}){
self = ${Self}(_bits: Builtin.itofp_with_overflow_Int${builtinIntLiteralBits}_FPIEEE${bits}(value))
}
/// Creates a new value from the given integer literal.
///
/// Do not call this initializer directly. It is used by the compiler when
/// you create a new `${Self}` instance by using an integer literal.
/// Instead, create a new value by using a literal.
///
/// In this example, the assignment to the `x` constant calls this
/// initializer behind the scenes.
///
/// let x: ${Self} = 100
/// // x == 100.0
///
/// - Parameter value: The new value.
@_transparent
public init(integerLiteral value: Int64) {
self = ${Self}(_bits: Builtin.sitofp_Int64_FPIEEE${bits}(value._value))
}
}
#if !os(Windows) && (arch(i386) || arch(x86_64))
% builtinFloatLiteralBits = 80
extension ${Self} : _ExpressibleByBuiltinFloatLiteral {
@_transparent
public
init(_builtinFloatLiteral value: Builtin.FPIEEE${builtinFloatLiteralBits}) {
% if bits == builtinFloatLiteralBits:
self = ${Self}(_bits: value)
% elif bits < builtinFloatLiteralBits:
self = ${Self}(_bits: Builtin.fptrunc_FPIEEE${builtinFloatLiteralBits}_FPIEEE${bits}(value))
% else:
// FIXME: This is actually losing precision <rdar://problem/14073102>.
self = ${Self}(Builtin.fpext_FPIEEE${builtinFloatLiteralBits}_FPIEEE${bits}(value))
% end
}
}
#else
% builtinFloatLiteralBits = 64
extension ${Self} : _ExpressibleByBuiltinFloatLiteral {
@_transparent
public
init(_builtinFloatLiteral value: Builtin.FPIEEE${builtinFloatLiteralBits}) {
% if bits == builtinFloatLiteralBits:
self = ${Self}(_bits: value)
% elif bits < builtinFloatLiteralBits:
self = ${Self}(_bits: Builtin.fptrunc_FPIEEE${builtinFloatLiteralBits}_FPIEEE${bits}(value))
% else:
// FIXME: This is actually losing precision <rdar://problem/14073102>.
self = ${Self}(Builtin.fpext_FPIEEE${builtinFloatLiteralBits}_FPIEEE${bits}(value))
% end
}
}
#endif
extension ${Self} : Hashable {
/// The number's hash value.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
public var hashValue: Int {
if isZero {
// To satisfy the axiom that equality implies hash equality, we need to
// finesse the hash value of -0.0 to match +0.0.
return 0
} else {
%if bits <= word_bits:
return Int(bitPattern: UInt(bitPattern))
%elif bits == 64: # Double -> 32-bit Int
return Int(truncatingBitPattern: bitPattern >> 32) ^
Int(truncatingBitPattern: bitPattern)
%elif word_bits == 32: # Float80 -> 32-bit Int
return Int(truncatingBitPattern: significandBitPattern >> 32) ^
Int(truncatingBitPattern: significandBitPattern) ^
Int(_representation.signAndExponent)
%else: # Float80 -> 64-bit Int
return Int(bitPattern: UInt(significandBitPattern)) ^
Int(_representation.signAndExponent)
%end
}
}
}
extension ${Self} : Arithmetic {
/// The magnitude of this value.
///
/// For any value `x`, `x.magnitude.sign` is `.plus`. If `x` is not NaN,
/// `x.magnitude` is the absolute value of `x`.
///
/// The global `abs(_:)` function provides more familiar syntax when you need
/// to find an absolute value. In addition, because `abs(_:)` always returns
/// a value of the same type, even in a generic context, using the function
/// instead of the `magnitude` property is encouraged.
///
/// let targetDistance: ${Self} = 5.25
/// let throwDistance: ${Self} = 5.5
///
/// let margin = targetDistance - throwDistance
/// // margin == -0.25
/// // margin.magnitude == 0.25
///
/// // Use 'abs(_:)' instead of 'magnitude'
/// print("Missed the target by \(abs(margin)) meters.")
/// // Prints "Missed the target by 0.25 meters."
///
/// - SeeAlso: `abs(_:)`
@_transparent
public var magnitude: ${Self} {
return ${Self}(_bits: Builtin.int_fabs_FPIEEE${bits}(_value))
}
}
@_transparent
public prefix func + (x: ${Self}) -> ${Self} {
return x
}
@_transparent
public prefix func - (x: ${Self}) -> ${Self} {
return ${Self}(_bits: Builtin.fneg_FPIEEE${bits}(x._value))
}
//===----------------------------------------------------------------------===//
// Explicit conversions between types.
//===----------------------------------------------------------------------===//
// Construction from integers.
extension ${Self} {
% for (srcBits, srcSigned) in allInts():
% That = intName(srcBits, srcSigned)
% ThatBuiltinName = builtinIntName(srcBits)
% sign = 's' if srcSigned else 'u'
@_transparent
public init(_ v: ${That}) {
_value = Builtin.${sign}itofp_${ThatBuiltinName}_FPIEEE${bits}(v._value)
}
% end
}
// Construction from other floating point numbers.
extension ${Self} {
% for src_type in all_floating_point_types():
% srcBits = src_type.bits
% That = src_type.stdlib_name
% if srcBits == 80:
#if !os(Windows) && (arch(i386) || arch(x86_64))
% end
% if srcBits == bits:
/// Creates a new instance initialized to the given value.
///
/// The value of `other` is represented exactly by the new instance. A NaN
/// passed as `other` results in another NaN, with a signaling NaN value
/// converted to quiet NaN.
% else:
/// Creates a new instance that approximates the given value.
///
/// The value of `other` is rounded to a representable value, if necessary.
/// A NaN passed as `other` results in another NaN, with a signaling NaN
/// value converted to quiet NaN.
% end
///
/// let x: ${That} = 21.25
/// let y = ${Self}(x)
/// // y == 21.25
///
/// let z = ${Self}(${That}.nan)
/// // z.isNaN == true
///
/// - Parameter other: The value to use for the new instance.
@_transparent
public init(_ other: ${That}) {
% if srcBits > bits:
_value = Builtin.fptrunc_FPIEEE${srcBits}_FPIEEE${bits}(other._value)
% elif srcBits < bits:
_value = Builtin.fpext_FPIEEE${srcBits}_FPIEEE${bits}(other._value)
% else:
_value = other._value
% end
}
/// Creates a new instance initialized to the given value, if it can be
/// represented without rounding.
///
/// If `other` can't be represented as an instance of `${Self}` without
/// rounding, the result of this initializer is `nil`. In particular,
/// passing NaN as `other` always results in `nil`.
///
/// let x: ${That} = 21.25
/// let y = ${Self}(exactly: x)
/// // y == Optional.some(21.25)
///
/// let z = ${Self}(exactly: ${That}.nan)
/// // z == nil
///
/// - Parameter other: The value to use for the new instance.
@inline(__always)
public init?(exactly other: ${That}) {
self.init(other)
// Converting the infinity value is considered value preserving.
// In other cases, check that we can round-trip and get the same value.
// NaN always fails.
if ${That}(self) != other {
return nil
}
}
% if srcBits == 80:
#endif
% end
% end
}
//===----------------------------------------------------------------------===//
// Standard Operator Table
//===----------------------------------------------------------------------===//
// TODO: These should not be necessary, since they're already provided by
// <T: FloatingPoint>, but in practice they are currently needed to
// disambiguate overloads. We should find a way to remove them, either by
// tweaking the overload resolution rules, or by removing the other
// definitions in the standard lib, or both.
@_transparent
public func + (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
return lhs.adding(rhs)
}
@_transparent
public func - (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
return lhs.subtracting(rhs)
}
@_transparent
public func * (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
return lhs.multiplied(by: rhs)
}
@_transparent
public func / (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
return lhs.divided(by: rhs)
}
@_transparent
public func += (lhs: inout ${Self}, rhs: ${Self}) {
lhs.add(rhs)
}
@_transparent
public func -= (lhs: inout ${Self}, rhs: ${Self}) {
lhs.subtract(rhs)
}
@_transparent
public func *= (lhs: inout ${Self}, rhs: ${Self}) {
lhs.multiply(by: rhs)
}
@_transparent
public func /= (lhs: inout ${Self}, rhs: ${Self}) {
lhs.divide(by: rhs)
}
//===----------------------------------------------------------------------===//
// Strideable Conformance
//===----------------------------------------------------------------------===//
extension ${Self} : Strideable {
/// Returns the distance from this value to the specified value.
///
/// For two values `x` and `y`, the result of `x.distance(to: y)` is equal to
/// `y - x`---a distance `d` such that `x.advanced(by: d)` approximates `y`.
/// For example:
///
/// let x = 21.5
/// let d = x.distance(to: 15.0)
/// // d == -6.5
///
/// print(x.advanced(by: d))
/// // Prints "15.0"
///
/// - Parameter other: A value to calculate the distance to.
/// - Returns: The distance between this value and `other`.
@_transparent
public func distance(to other: ${Self}) -> ${Self} {
return other - self
}
/// Returns a new value advanced by the given distance.
///
/// For two values `x` and `d`, the result of a `x.advanced(by: d)` is equal
/// to `x + d`---a new value `y` such that `x.distance(to: y)` approximates