Skip to content

Commit e4571bc

Browse files
committed
[NFC] Adopt Darwin overlay implementation of Decimal.exponent and Decimal.quietNaN, make minor formatting changes to synchronize
1 parent 598b2f8 commit e4571bc

File tree

1 file changed

+90
-30
lines changed

1 file changed

+90
-30
lines changed

Foundation/Decimal.swift

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public struct Decimal {
9595
}
9696
}
9797

98+
extension Decimal {
99+
public typealias RoundingMode = NSDecimalNumber.RoundingMode
100+
public typealias CalculationError = NSDecimalNumber.CalculationError
101+
}
102+
98103
extension Decimal {
99104
public static let leastFiniteMagnitude = Decimal(
100105
_exponent: 127,
@@ -104,6 +109,7 @@ extension Decimal {
104109
_reserved: 0,
105110
_mantissa: (0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff)
106111
)
112+
107113
public static let greatestFiniteMagnitude = Decimal(
108114
_exponent: 127,
109115
_length: 8,
@@ -112,6 +118,7 @@ extension Decimal {
112118
_reserved: 0,
113119
_mantissa: (0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff)
114120
)
121+
115122
public static let leastNormalMagnitude = Decimal(
116123
_exponent: -127,
117124
_length: 1,
@@ -120,6 +127,7 @@ extension Decimal {
120127
_reserved: 0,
121128
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
122129
)
130+
123131
public static let leastNonzeroMagnitude = Decimal(
124132
_exponent: -127,
125133
_length: 1,
@@ -128,6 +136,7 @@ extension Decimal {
128136
_reserved: 0,
129137
_mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)
130138
)
139+
131140
public static let pi = Decimal(
132141
_exponent: -38,
133142
_length: 8,
@@ -136,62 +145,94 @@ extension Decimal {
136145
_reserved: 0,
137146
_mantissa: (0x6623, 0x7d57, 0x16e7, 0xad0d, 0xaf52, 0x4641, 0xdfa7, 0xec58)
138147
)
148+
139149
public var exponent: Int {
140-
get {
141-
return Int(self.__exponent)
142-
}
150+
return Int(_exponent)
143151
}
152+
144153
public var significand: Decimal {
145-
get {
146-
return Decimal(_exponent: 0, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa)
147-
}
154+
return Decimal(
155+
_exponent: 0, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact,
156+
_reserved: 0, _mantissa: _mantissa)
148157
}
158+
149159
public init(sign: FloatingPointSign, exponent: Int, significand: Decimal) {
150-
self.init(_exponent: Int32(exponent) + significand._exponent, _length: significand._length, _isNegative: sign == .plus ? 0 : 1, _isCompact: significand._isCompact, _reserved: 0, _mantissa: significand._mantissa)
160+
self.init(
161+
_exponent: Int32(exponent) + significand._exponent,
162+
_length: significand._length,
163+
_isNegative: sign == .plus ? 0 : 1,
164+
_isCompact: significand._isCompact,
165+
_reserved: 0,
166+
_mantissa: significand._mantissa)
151167
}
168+
152169
public init(signOf: Decimal, magnitudeOf magnitude: Decimal) {
153-
self.init(_exponent: magnitude._exponent, _length: magnitude._length, _isNegative: signOf._isNegative, _isCompact: magnitude._isCompact, _reserved: 0, _mantissa: magnitude._mantissa)
170+
self.init(
171+
_exponent: magnitude._exponent,
172+
_length: magnitude._length,
173+
_isNegative: signOf._isNegative,
174+
_isCompact: magnitude._isCompact,
175+
_reserved: 0,
176+
_mantissa: magnitude._mantissa)
154177
}
178+
155179
public var sign: FloatingPointSign {
156180
return _isNegative == 0 ? FloatingPointSign.plus : FloatingPointSign.minus
157181
}
182+
158183
public static var radix: Int {
159184
return 10
160185
}
186+
161187
public var ulp: Decimal {
162188
if !self.isFinite { return Decimal.nan }
163-
return Decimal(_exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
189+
return Decimal(
190+
_exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1,
191+
_reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
164192
}
193+
165194
public func isEqual(to other: Decimal) -> Bool {
166195
return self.compare(to: other) == .orderedSame
167196
}
197+
168198
public func isLess(than other: Decimal) -> Bool {
169199
return self.compare(to: other) == .orderedAscending
170200
}
201+
171202
public func isLessThanOrEqualTo(_ other: Decimal) -> Bool {
172203
let comparison = self.compare(to: other)
173204
return comparison == .orderedAscending || comparison == .orderedSame
174205
}
206+
175207
public func isTotallyOrdered(belowOrEqualTo other: Decimal) -> Bool {
176-
// Notes: Decimal does not have -0 or infinities to worry about
208+
// Note: Decimal does not have -0 or infinities to worry about
177209
if self.isNaN {
178210
return false
179-
} else if self < other {
211+
}
212+
if self < other {
180213
return true
181-
} else if other < self {
214+
}
215+
if other < self {
182216
return false
183217
}
184-
// fall through to == behavior
218+
// Fall through to == behavior
185219
return true
186220
}
221+
187222
public var isCanonical: Bool {
188223
return true
189224
}
225+
190226
public var nextUp: Decimal {
191-
return self + Decimal(_exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
227+
return self + Decimal(
228+
_exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1,
229+
_reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
192230
}
231+
193232
public var nextDown: Decimal {
194-
return self - Decimal(_exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
233+
return self - Decimal(
234+
_exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1,
235+
_reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
195236
}
196237
}
197238

@@ -587,27 +628,36 @@ extension Decimal : Strideable {
587628
}
588629
}
589630

631+
// The methods in this extension exist to match the protocol requirements of
632+
// FloatingPoint, even if we can't conform directly.
633+
//
634+
// If it becomes clear that conformance is truly impossible, we can deprecate
635+
// some of the methods (e.g. `isEqual(to:)` in favor of operators).
590636
extension Decimal {
591-
public typealias RoundingMode = NSDecimalNumber.RoundingMode
592-
public typealias CalculationError = NSDecimalNumber.CalculationError
593637
public init(_ value: UInt8) {
594638
self.init(UInt64(value))
595639
}
640+
596641
public init(_ value: Int8) {
597642
self.init(Int64(value))
598643
}
644+
599645
public init(_ value: UInt16) {
600646
self.init(UInt64(value))
601647
}
648+
602649
public init(_ value: Int16) {
603650
self.init(Int64(value))
604651
}
652+
605653
public init(_ value: UInt32) {
606654
self.init(UInt64(value))
607655
}
656+
608657
public init(_ value: Int32) {
609658
self.init(Int64(value))
610659
}
660+
611661
public init(_ value: Double) {
612662
precondition(!value.isInfinite, "Decimal does not yet fully adopt FloatingPoint")
613663
if value.isNaN {
@@ -629,26 +679,27 @@ extension Decimal {
629679
}
630680
var mantissa = UInt64(val)
631681

632-
var i = Int32(0)
633-
// this is a bit ugly but it is the closest approximation of the C initializer that can be expressed here.
682+
var i: Int32 = 0
683+
// This is a bit ugly but it is the closest approximation of the C
684+
// initializer that can be expressed here.
634685
while mantissa != 0 && i < NSDecimalMaxSize {
635686
switch i {
636687
case 0:
637-
_mantissa.0 = UInt16(truncatingIfNeeded:mantissa)
688+
_mantissa.0 = UInt16(truncatingIfNeeded: mantissa)
638689
case 1:
639-
_mantissa.1 = UInt16(truncatingIfNeeded:mantissa)
690+
_mantissa.1 = UInt16(truncatingIfNeeded: mantissa)
640691
case 2:
641-
_mantissa.2 = UInt16(truncatingIfNeeded:mantissa)
692+
_mantissa.2 = UInt16(truncatingIfNeeded: mantissa)
642693
case 3:
643-
_mantissa.3 = UInt16(truncatingIfNeeded:mantissa)
694+
_mantissa.3 = UInt16(truncatingIfNeeded: mantissa)
644695
case 4:
645-
_mantissa.4 = UInt16(truncatingIfNeeded:mantissa)
696+
_mantissa.4 = UInt16(truncatingIfNeeded: mantissa)
646697
case 5:
647-
_mantissa.5 = UInt16(truncatingIfNeeded:mantissa)
698+
_mantissa.5 = UInt16(truncatingIfNeeded: mantissa)
648699
case 6:
649-
_mantissa.6 = UInt16(truncatingIfNeeded:mantissa)
700+
_mantissa.6 = UInt16(truncatingIfNeeded: mantissa)
650701
case 7:
651-
_mantissa.7 = UInt16(truncatingIfNeeded:mantissa)
702+
_mantissa.7 = UInt16(truncatingIfNeeded: mantissa)
652703
default:
653704
fatalError("initialization overflow")
654705
}
@@ -716,10 +767,11 @@ extension Decimal {
716767
}
717768

718769
public static var quietNaN: Decimal {
719-
var quiet = Decimal()
720-
quiet._isNegative = 1
721-
return quiet
770+
return Decimal(
771+
_exponent: 0, _length: 0, _isNegative: 1, _isCompact: 0,
772+
_reserved: 0, _mantissa: (0, 0, 0, 0, 0, 0, 0, 0))
722773
}
774+
723775
public var floatingPointClass: FloatingPointClassification {
724776
if _length == 0 && _isNegative == 1 {
725777
return .quietNaN
@@ -732,27 +784,35 @@ extension Decimal {
732784
return .positiveNormal
733785
}
734786
}
787+
735788
public var isSignMinus: Bool {
736789
return _isNegative != 0
737790
}
791+
738792
public var isNormal: Bool {
739793
return !isZero && !isInfinite && !isNaN
740794
}
795+
741796
public var isFinite: Bool {
742797
return !isNaN
743798
}
799+
744800
public var isZero: Bool {
745801
return _length == 0 && _isNegative == 0
746802
}
803+
747804
public var isSubnormal: Bool {
748805
return false
749806
}
807+
750808
public var isInfinite: Bool {
751809
return false
752810
}
811+
753812
public var isNaN: Bool {
754813
return _length == 0 && _isNegative == 1
755814
}
815+
756816
public var isSignaling: Bool {
757817
return false
758818
}

0 commit comments

Comments
 (0)