diff --git a/Foundation/CGFloat.swift b/Foundation/CGFloat.swift index 44003f6fdc..930478cfd1 100644 --- a/Foundation/CGFloat.swift +++ b/Foundation/CGFloat.swift @@ -145,6 +145,19 @@ public struct CGFloat { public var native: NativeType } +extension CGFloat : SignedNumeric { + // FIXME(integers): implement + public init?(exactly source: T) { + fatalError() + } + + @_transparent + public var magnitude: CGFloat { + return CGFloat(Swift.abs(native)) + } + +} + extension CGFloat : BinaryFloatingPoint { public typealias RawSignificand = UInt @@ -278,34 +291,29 @@ extension CGFloat : BinaryFloatingPoint { return CGFloat(native.nextUp) } - @_transparent - public var magnitude: CGFloat { - return CGFloat(Swift.abs(native)) - } - @_transparent public mutating func negate() { native.negate() } @_transparent - public mutating func add(_ other: CGFloat) { - native.add(other.native) + public static func +=(_ lhs: inout CGFloat, _ rhs: CGFloat) { + lhs.native += rhs.native } @_transparent - public mutating func subtract(_ other: CGFloat) { - native.subtract(other.native) + public static func -=(_ lhs: inout CGFloat, _ rhs: CGFloat) { + lhs.native -= rhs.native } @_transparent - public mutating func multiply(by other: CGFloat) { - native.multiply(by: other.native) + public static func *=(_ lhs: inout CGFloat, _ rhs: CGFloat) { + lhs.native *= rhs.native } @_transparent - public mutating func divide(by other: CGFloat) { - native.divide(by: other.native) + public static func /=(_ lhs: inout CGFloat, _ rhs: CGFloat) { + lhs.native /= rhs.native } @_transparent @@ -564,45 +572,36 @@ extension Float { // tweaking the overload resolution rules, or by removing the other // definitions in the standard lib, or both. -@_transparent -public func +(lhs: CGFloat, rhs: CGFloat) -> CGFloat { - return lhs.adding(rhs) -} - -@_transparent -public func -(lhs: CGFloat, rhs: CGFloat) -> CGFloat { - return lhs.subtracting(rhs) -} - -@_transparent -public func *(lhs: CGFloat, rhs: CGFloat) -> CGFloat { - return lhs.multiplied(by: rhs) -} - -@_transparent -public func /(lhs: CGFloat, rhs: CGFloat) -> CGFloat { - return lhs.divided(by: rhs) -} +extension CGFloat { + @_transparent + public static func +(lhs: CGFloat, rhs: CGFloat) -> CGFloat { + var lhs = lhs + lhs += rhs + return lhs + } -@_transparent -public func +=(lhs: inout CGFloat, rhs: CGFloat) { - lhs.add(rhs) -} + @_transparent + public static func -(lhs: CGFloat, rhs: CGFloat) -> CGFloat { + var lhs = lhs + lhs -= rhs + return lhs + } -@_transparent -public func -=(lhs: inout CGFloat, rhs: CGFloat) { - lhs.subtract(rhs) -} + @_transparent + public static func *(lhs: CGFloat, rhs: CGFloat) -> CGFloat { + var lhs = lhs + lhs *= rhs + return lhs + } -@_transparent -public func *=(lhs: inout CGFloat, rhs: CGFloat) { - lhs.multiply(by: rhs) + @_transparent + public static func /(lhs: CGFloat, rhs: CGFloat) -> CGFloat { + var lhs = lhs + lhs /= rhs + return lhs + } } -@_transparent -public func /=(lhs: inout CGFloat, rhs: CGFloat) { - lhs.divide(by: rhs) -} //===----------------------------------------------------------------------===// // Strideable Conformance diff --git a/Foundation/NSDecimal.swift b/Foundation/NSDecimal.swift index 02a7fd2cab..8987c1a43c 100644 --- a/Foundation/NSDecimal.swift +++ b/Foundation/NSDecimal.swift @@ -166,25 +166,6 @@ extension Decimal { if !self.isFinite { return Decimal.nan } return Decimal(_exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)) } - public mutating func add(_ other: Decimal) { - var rhs = other - _ = NSDecimalAdd(&self, &self, &rhs, .plain) - } - public mutating func subtract(_ other: Decimal) { - var rhs = other - _ = NSDecimalSubtract(&self, &self, &rhs, .plain) - } - public mutating func multiply(by other: Decimal) { - var rhs = other - _ = NSDecimalMultiply(&self, &self, &rhs, .plain) - } - public mutating func divide(by other: Decimal) { - var rhs = other - _ = NSDecimalDivide(&self, &self, &rhs, .plain) - } - public mutating func negate() { - _isNegative = _isNegative == 0 ? 1 : 0 - } public func isEqual(to other: Decimal) -> Bool { return self.compare(to: other) == .orderedSame } @@ -216,26 +197,6 @@ extension Decimal { public var nextDown: Decimal { return self - Decimal(_exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000)) } - public static func +(lhs: Decimal, rhs: Decimal) -> Decimal { - var answer = lhs - answer.add(rhs) - return answer; - } - public static func -(lhs: Decimal, rhs: Decimal) -> Decimal { - var answer = lhs - answer.subtract(rhs) - return answer; - } - public static func /(lhs: Decimal, rhs: Decimal) -> Decimal { - var answer = lhs - answer.divide(by: rhs) - return answer; - } - public static func *(lhs: Decimal, rhs: Decimal) -> Decimal { - var answer = lhs - answer.multiply(by: rhs) - return answer; - } } extension Decimal : Hashable, Comparable { @@ -320,7 +281,63 @@ extension Decimal : ExpressibleByIntegerLiteral { } } -extension Decimal : SignedNumber { +extension Decimal : SignedNumeric { + public var magnitude: Decimal { + return Decimal(_exponent: _exponent, _length: _length, _isNegative: 0, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa) + } + + // FIXME(integers): implement properly + public init?(exactly source: T) { + fatalError() + } + + public static func +=(_ lhs: inout Decimal, _ rhs: Decimal) { + var rhs = rhs + _ = NSDecimalAdd(&lhs, &lhs, &rhs, .plain) + } + + public static func -=(_ lhs: inout Decimal, _ rhs: Decimal) { + var rhs = rhs + _ = NSDecimalSubtract(&lhs, &lhs, &rhs, .plain) + } + + public static func *=(_ lhs: inout Decimal, _ rhs: Decimal) { + var rhs = rhs + _ = NSDecimalMultiply(&lhs, &lhs, &rhs, .plain) + } + + public static func /=(_ lhs: inout Decimal, _ rhs: Decimal) { + var rhs = rhs + _ = NSDecimalDivide(&lhs, &lhs, &rhs, .plain) + } + + public static func +(lhs: Decimal, rhs: Decimal) -> Decimal { + var answer = lhs + answer += rhs + return answer; + } + + public static func -(lhs: Decimal, rhs: Decimal) -> Decimal { + var answer = lhs + answer -= rhs + return answer; + } + + public static func /(lhs: Decimal, rhs: Decimal) -> Decimal { + var answer = lhs + answer /= rhs + return answer; + } + + public static func *(lhs: Decimal, rhs: Decimal) -> Decimal { + var answer = lhs + answer *= rhs + return answer; + } + + public mutating func negate() { + _isNegative = _isNegative == 0 ? 1 : 0 + } } extension Decimal : Strideable { @@ -332,12 +349,6 @@ extension Decimal : Strideable { } } -extension Decimal : AbsoluteValuable { - public static func abs(_ x: Decimal) -> Decimal { - return Decimal(_exponent: x._exponent, _length: x._length, _isNegative: 0, _isCompact: x._isCompact, _reserved: 0, _mantissa: x._mantissa) - } -} - extension Decimal { public typealias RoundingMode = NSDecimalNumber.RoundingMode public typealias CalculationError = NSDecimalNumber.CalculationError diff --git a/Foundation/NSError.swift b/Foundation/NSError.swift index 5c675ded4b..ffc19ceb9e 100644 --- a/Foundation/NSError.swift +++ b/Foundation/NSError.swift @@ -372,13 +372,13 @@ public protocol __BridgedNSError : Error { // Allow two bridged NSError types to be compared. extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: SignedInteger { public static func ==(lhs: Self, rhs: Self) -> Bool { - return lhs.rawValue.toIntMax() == rhs.rawValue.toIntMax() + return lhs.rawValue == rhs.rawValue } } public extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: SignedInteger { public var _domain: String { return Self._nsErrorDomain } - public var _code: Int { return Int(rawValue.toIntMax()) } + public var _code: Int { return Int(rawValue) } public init?(rawValue: RawValue) { self = unsafeBitCast(rawValue, to: Self.self) @@ -398,14 +398,14 @@ public extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: S // Allow two bridged NSError types to be compared. extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: UnsignedInteger { public static func ==(lhs: Self, rhs: Self) -> Bool { - return lhs.rawValue.toUIntMax() == rhs.rawValue.toUIntMax() + return lhs.rawValue == rhs.rawValue } } public extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: UnsignedInteger { public var _domain: String { return Self._nsErrorDomain } public var _code: Int { - return Int(bitPattern: UInt(rawValue.toUIntMax())) + return Int(bitPattern: UInt(rawValue)) } public init?(rawValue: RawValue) { diff --git a/Foundation/NSScanner.swift b/Foundation/NSScanner.swift index 9cb3b58404..e991a7a792 100644 --- a/Foundation/NSScanner.swift +++ b/Foundation/NSScanner.swift @@ -219,7 +219,8 @@ internal protocol _BitShiftable { static func <<(lhs: Self, rhs: Self) -> Self } -internal protocol _IntegerLike : Integer, _BitShiftable { +// FIXME(integers): replace this protocol with just a FixedWidthInteger +internal protocol _IntegerLike : FixedWidthInteger, _BitShiftable { init(_ value: Int) static var max: Self { get } static var min: Self { get } diff --git a/Foundation/ProgressFraction.swift b/Foundation/ProgressFraction.swift index 8fcf3de049..7dac53aaa6 100644 --- a/Foundation/ProgressFraction.swift +++ b/Foundation/ProgressFraction.swift @@ -54,7 +54,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible { return _ProgressFraction(completed: simplified.0, total: simplified.1) } - static private func _math(lhs: _ProgressFraction, rhs: _ProgressFraction, whichOperator: (_ lhs : Double, _ rhs : Double) -> Double, whichOverflow : (_ lhs: Int64, _ rhs: Int64) -> (Int64, overflow: Bool)) -> _ProgressFraction { + static private func _math(lhs: _ProgressFraction, rhs: _ProgressFraction, whichOperator: (_ lhs : Double, _ rhs : Double) -> Double, whichOverflow : (_ lhs: Int64, _ rhs: Int64) -> (Int64, overflow: ArithmeticOverflow)) -> _ProgressFraction { // Mathematically, it is nonsense to add or subtract something with a denominator of 0. However, for the purposes of implementing Progress' fractions, we just assume that a zero-denominator fraction is "weightless" and return the other value. We still need to check for the case where they are both nonsense though. precondition(!(lhs.total == 0 && rhs.total == 0), "Attempt to add or subtract invalid fraction") guard lhs.total != 0 else { @@ -71,7 +71,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible { if let lcm = _leastCommonMultiple(lhs.total, rhs.total) { let result = whichOverflow(lhs.completed * (lcm / lhs.total), rhs.completed * (lcm / rhs.total)) - if result.overflow { + if result.overflow == .overflow { return _ProgressFraction(double: whichOperator(lhs.fractionCompleted, rhs.fractionCompleted), overflow: true) } else { return _ProgressFraction(completed: result.0, total: lcm) @@ -83,7 +83,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible { if let lcm = _leastCommonMultiple(lhsSimplified.total, rhsSimplified.total) { let result = whichOverflow(lhsSimplified.completed * (lcm / lhsSimplified.total), rhsSimplified.completed * (lcm / rhsSimplified.total)) - if result.overflow { + if result.overflow == .overflow { // Use original lhs/rhs here return _ProgressFraction(double: whichOperator(lhs.fractionCompleted, rhs.fractionCompleted), overflow: true) } else { @@ -97,11 +97,11 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible { } static internal func +(lhs: _ProgressFraction, rhs: _ProgressFraction) -> _ProgressFraction { - return _math(lhs: lhs, rhs: rhs, whichOperator: +, whichOverflow: Int64.addWithOverflow) + return _math(lhs: lhs, rhs: rhs, whichOperator: +, whichOverflow: { $0.addingReportingOverflow($1) }) } static internal func -(lhs: _ProgressFraction, rhs: _ProgressFraction) -> _ProgressFraction { - return _math(lhs: lhs, rhs: rhs, whichOperator: -, whichOverflow: Int64.subtractWithOverflow) + return _math(lhs: lhs, rhs: rhs, whichOperator: -, whichOverflow: { $0.subtractingReportingOverflow($1) }) } static internal func *(lhs: _ProgressFraction, rhs: _ProgressFraction) -> _ProgressFraction { @@ -110,18 +110,18 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible { return _ProgressFraction(double: rhs.fractionCompleted * rhs.fractionCompleted, overflow: true) } - let newCompleted = Int64.multiplyWithOverflow(lhs.completed, rhs.completed) - let newTotal = Int64.multiplyWithOverflow(lhs.total, rhs.total) + let newCompleted = lhs.completed.multipliedReportingOverflow(by: rhs.completed) + let newTotal = lhs.total.multipliedReportingOverflow(by: rhs.total) - if newCompleted.overflow || newTotal.overflow { + if newCompleted.overflow == .overflow || newTotal.overflow == .overflow { // Try simplifying, then do it again let lhsSimplified = lhs.simplified() let rhsSimplified = rhs.simplified() - let newCompletedSimplified = Int64.multiplyWithOverflow(lhsSimplified.completed, rhsSimplified.completed) - let newTotalSimplified = Int64.multiplyWithOverflow(lhsSimplified.total, rhsSimplified.total) + let newCompletedSimplified = lhsSimplified.completed.multipliedReportingOverflow(by: rhsSimplified.completed) + let newTotalSimplified = lhsSimplified.total.multipliedReportingOverflow(by: rhsSimplified.total) - if newCompletedSimplified.overflow || newTotalSimplified.overflow { + if newCompletedSimplified.overflow == .overflow || newTotalSimplified.overflow == .overflow { // Still overflow return _ProgressFraction(double: lhs.fractionCompleted * rhs.fractionCompleted, overflow: true) } else { @@ -138,14 +138,14 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible { return _ProgressFraction(double: lhs.fractionCompleted / Double(rhs), overflow: true) } - let newTotal = Int64.multiplyWithOverflow(lhs.total, rhs) + let newTotal = lhs.total.multipliedReportingOverflow(by: rhs) - if newTotal.overflow { + if newTotal.overflow == .overflow { let simplified = lhs.simplified() - let newTotalSimplified = Int64.multiplyWithOverflow(simplified.total, rhs) + let newTotalSimplified = simplified.total.multipliedReportingOverflow(by: rhs) - if newTotalSimplified.overflow { + if newTotalSimplified.overflow == .overflow { // Still overflow return _ProgressFraction(double: lhs.fractionCompleted / Double(rhs), overflow: true) } else { @@ -175,10 +175,10 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible { return false } else { // Cross-multiply - let left = Int64.multiplyWithOverflow(lhs.completed, rhs.total) - let right = Int64.multiplyWithOverflow(lhs.total, rhs.completed) + let left = lhs.completed.multipliedReportingOverflow(by: rhs.total) + let right = lhs.total.multipliedReportingOverflow(by: rhs.completed) - if !left.overflow && !right.overflow { + if left.overflow == .none && right.overflow == .none { if left.0 == right.0 { return true } @@ -187,10 +187,10 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible { let lhsSimplified = lhs.simplified() let rhsSimplified = rhs.simplified() - let leftSimplified = Int64.multiplyWithOverflow(lhsSimplified.completed, rhsSimplified.total) - let rightSimplified = Int64.multiplyWithOverflow(lhsSimplified.total, rhsSimplified.completed) + let leftSimplified = lhsSimplified.completed.multipliedReportingOverflow(by: rhsSimplified.total) + let rightSimplified = lhsSimplified.total.multipliedReportingOverflow(by: rhsSimplified.completed) - if !leftSimplified.overflow && !rightSimplified.overflow { + if leftSimplified.overflow == .none && rightSimplified.overflow == .none { if leftSimplified.0 == rightSimplified.0 { return true } @@ -260,8 +260,8 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible { private static func _leastCommonMultiple(_ a : Int64, _ b : Int64) -> Int64? { // This division always results in an integer value because gcd(a,b) is a divisor of a. // lcm(a,b) == (|a|/gcd(a,b))*b == (|b|/gcd(a,b))*a - let result = Int64.multiplyWithOverflow((a / _greatestCommonDivisor(a, b)), b) - if result.overflow { + let result = (a / _greatestCommonDivisor(a, b)).multipliedReportingOverflow(by: b) + if result.overflow == .overflow { return nil } else { return result.0