From a35f5ba889a6a0e40292fcabf9072673225c2a77 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Wed, 8 Apr 2020 15:10:52 -0400 Subject: [PATCH 01/13] Add Real conformance for Float16. --- Sources/RealModule/Float16+Real.swift | 165 ++++++++++++++++++++++++++ Tests/RealTests/RealTests.swift | 7 ++ 2 files changed, 172 insertions(+) create mode 100644 Sources/RealModule/Float16+Real.swift diff --git a/Sources/RealModule/Float16+Real.swift b/Sources/RealModule/Float16+Real.swift new file mode 100644 index 0000000..cb3d5bb --- /dev/null +++ b/Sources/RealModule/Float16+Real.swift @@ -0,0 +1,165 @@ +//===--- Float16+Real.swift -----------------------------------*- swift -*-===// +// +// This source file is part of the Swift Numerics open source project +// +// Copyright (c) 2020 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) +extension Float16: Real { + @_transparent + public static func cos(_ x: Float16) -> Float16 { + Float16(.cos(Float(x))) + } + + @_transparent + public static func sin(_ x: Float16) -> Float16 { + Float16(.sin(Float(x))) + } + + @_transparent + public static func tan(_ x: Float16) -> Float16 { + Float16(.tan(Float(x))) + } + + @_transparent + public static func acos(_ x: Float16) -> Float16 { + Float16(.acos(Float(x))) + } + + @_transparent + public static func asin(_ x: Float16) -> Float16 { + Float16(.asin(Float(x))) + } + + @_transparent + public static func atan(_ x: Float16) -> Float16 { + Float16(.atan(Float(x))) + } + + @_transparent + public static func cosh(_ x: Float16) -> Float16 { + Float16(.cosh(Float(x))) + } + + @_transparent + public static func sinh(_ x: Float16) -> Float16 { + Float16(.sinh(Float(x))) + } + + @_transparent + public static func tanh(_ x: Float16) -> Float16 { + Float16(.tanh(Float(x))) + } + + @_transparent + public static func acosh(_ x: Float16) -> Float16 { + Float16(.acosh(Float(x))) + } + + @_transparent + public static func asinh(_ x: Float16) -> Float16 { + Float16(.asinh(Float(x))) + } + + @_transparent + public static func atanh(_ x: Float16) -> Float16 { + Float16(.atanh(Float(x))) + } + + @_transparent + public static func exp(_ x: Float16) -> Float16 { + Float16(.exp(Float(x))) + } + + @_transparent + public static func expMinusOne(_ x: Float16) -> Float16 { + Float16(.expMinusOne(Float(x))) + } + + @_transparent + public static func log(_ x: Float16) -> Float16 { + Float16(.log(Float(x))) + } + + @_transparent + public static func log(onePlus x: Float16) -> Float16 { + Float16(.log(onePlus: Float(x))) + } + + @_transparent + public static func erf(_ x: Float16) -> Float16 { + Float16(.erf(Float(x))) + } + + @_transparent + public static func erfc(_ x: Float16) -> Float16 { + Float16(.erfc(Float(x))) + } + + @_transparent + public static func exp2(_ x: Float16) -> Float16 { + Float16(.exp2(Float(x))) + } + + @_transparent + public static func exp10(_ x: Float16) -> Float16 { + Float16(.exp10(Float(x))) + } + + @_transparent + public static func hypot(_ x: Float16, _ y: Float16) -> Float16 { + if x.isInfinite || y.isInfinite { return .infinity } + let xf = Float(x) + let yf = Float(y) + return Float16(.sqrt(xf*xf + yf*yf)) + } + + @_transparent + public static func gamma(_ x: Float16) -> Float16 { + Float16(.gamma(Float(x))) + } + + @_transparent + public static func log2(_ x: Float16) -> Float16 { + Float16(.log2(Float(x))) + } + + @_transparent + public static func log10(_ x: Float16) -> Float16 { + Float16(.log10(Float(x))) + } + + @_transparent + public static func pow(_ x: Float16, _ y: Float16) -> Float16 { + Float16(.pow(Float(x), Float(y))) + } + + @_transparent + public static func pow(_ x: Float16, _ n: Int) -> Float16 { + // TODO: this can be improved because the range of n that don't overflow + // for all x != 1 is pretty small for Float16. + Float16(.pow(Float(x), n)) + } + + @_transparent + public static func root(_ x: Float16, _ n: Int) -> Float16 { + Float16(.root(Float(x), n)) + } + + @_transparent + public static func atan2(y: Float16, x: Float16) -> Float16 { + Float16(.atan2(y: Float(y), x: Float(x))) + } + + #if !os(Windows) + @_transparent + public static func logGamma(_ x: Float16) -> Float16 { + Float16(.logGamma(Float(x))) + } + #endif +} diff --git a/Tests/RealTests/RealTests.swift b/Tests/RealTests/RealTests.swift index 60afb5d..0e10f41 100644 --- a/Tests/RealTests/RealTests.swift +++ b/Tests/RealTests/RealTests.swift @@ -59,6 +59,13 @@ internal extension Real where Self: BinaryFloatingPoint { final class ElementaryFunctionChecks: XCTestCase { + func testFloat16() { + if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) { + Float16.elementaryFunctionChecks() + Float16.realFunctionChecks() + } + } + func testFloat() { Float.elementaryFunctionChecks() Float.realFunctionChecks() From 9e1645a49510122887897943c295e3f591dbef87 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Fri, 10 Apr 2020 10:08:59 -0400 Subject: [PATCH 02/13] Additional test coverage for pow with integer exponents. These cases expose a bug in the current implementation; because the split of the exponent into high and low parts may produce numbers with different signs, if both parts overflow or underflow, we can end up with 0 * infinity = NaN, when the result should actually be either zero or infinity. --- Tests/RealTests/IntegerExponentTests.swift | 71 ++++++++++++++-------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/Tests/RealTests/IntegerExponentTests.swift b/Tests/RealTests/IntegerExponentTests.swift index 0d8997b..fcf0011 100644 --- a/Tests/RealTests/IntegerExponentTests.swift +++ b/Tests/RealTests/IntegerExponentTests.swift @@ -12,7 +12,8 @@ import XCTest import RealModule -internal extension Real where Self: BinaryFloatingPoint { +internal extension Real where Self: BinaryFloatingPoint, + Self.RawSignificand: FixedWidthInteger { static func testIntegerExponentCommon() { // If x is -1, then the result is ±1 with sign chosen by parity of n. // Simply converting n to Real will flip parity when n is large, so @@ -27,12 +28,55 @@ internal extension Real where Self: BinaryFloatingPoint { XCTAssertEqual(Self.pow(-1, Int.max), -1) XCTAssertEqual(Self.pow(-1, -Int.max), -1) XCTAssertEqual(Self.pow(-1, Int.min), 1) + // Generate some random test values that definitely overflow or + // underflow; we want to be sure that we get the right ±0 or ±∞ + // result. + for _ in 0 ..< 10 { + let x = Self.random(in: 2 ..< 4) + let n = Int.random(in: 1 - Int(Self.leastNonzeroMagnitude.exponent) ..< .max) + let even = n & -2 + let odd = even | 1 + assertClose( .infinity, Self.pow(x, even)) + assertClose( .infinity, Self.pow(x, odd)) + assertClose( 0.0, Self.pow(1/x, even)) + assertClose( 0.0, Self.pow(1/x, odd)) + assertClose( .infinity, Self.pow(-x, even)) + assertClose(-.infinity, Self.pow(-x, odd)) + assertClose( 0.0, Self.pow(-1/x, even)) + assertClose(-0.0, Self.pow(-1/x, odd)) + assertClose( 0.0, Self.pow(x, -even)) + assertClose( 0.0, Self.pow(x, -odd)) + assertClose( .infinity, Self.pow(1/x, -even)) + assertClose( .infinity, Self.pow(1/x, -odd)) + assertClose( 0.0, Self.pow(-x, -even)) + assertClose(-0.0, Self.pow(-x, -odd)) + assertClose( .infinity, Self.pow(-1/x, -even)) + assertClose(-.infinity, Self.pow(-1/x, -odd)) + } + } + + static func testIntegerExponentDoubleAndSmaller() { + // max/min exponents, these always saturate, but this will reveal + // errors in some implementations that one could try. + let u = Self(1).nextUp + let d = Self(1).nextDown + assertClose( .infinity, Self.pow(-u, Int.max - 1)) + assertClose( 0.0, Self.pow(-d, Int.max - 1)) + assertClose( 0.0, Self.pow(-u, -Int.max + 1)) + assertClose( .infinity, Self.pow(-d, -Int.max + 1)) + assertClose(-.infinity, Self.pow(-u, Int.max)) + assertClose(-0.0, Self.pow(-d, Int.max)) + assertClose(-0.0, Self.pow(-u, -Int.max)) + assertClose(-.infinity, Self.pow(-d, -Int.max)) + assertClose( 0.0, Self.pow(-u, Int.min)) + assertClose( .infinity, Self.pow(-d, Int.min)) } } extension Float { static func testIntegerExponent() { testIntegerExponentCommon() + testIntegerExponentDoubleAndSmaller() let u = Float(1).nextUp let d = Float(1).nextDown // Smallest exponents not exactly representable as Float. @@ -52,24 +96,13 @@ extension Float { assertClose(-7.0064924138100205091278464932003585e-46, Float.pow(-d, 1744361943)) assertClose( 7.0064919961905290625123586120258840e-46, Float.pow(-d, 1744361944)) assertClose(-7.0064915785710625079583096856510544e-46, Float.pow(-d, 1744361945)) - // max/min exponents, these always saturate, but this will reveal - // errors in some implementations that one could try. - assertClose( .infinity, Self.pow(-u, Int.max - 1)) - assertClose( 0.0, Self.pow(-d, Int.max - 1)) - assertClose( 0.0, Self.pow(-u, -Int.max + 1)) - assertClose( .infinity, Self.pow(-d, -Int.max + 1)) - assertClose(-.infinity, Self.pow(-u, Int.max)) - assertClose(-0.0, Self.pow(-d, Int.max)) - assertClose(-0.0, Self.pow(-u, -Int.max)) - assertClose(-.infinity, Self.pow(-d, -Int.max)) - assertClose( 0.0, Self.pow(-u, Int.min)) - assertClose( .infinity, Self.pow(-d, Int.min)) } } extension Double { static func testIntegerExponent() { testIntegerExponentCommon() + testIntegerExponentDoubleAndSmaller() // Following tests only make sense (and are only necessary) on 64b platforms. #if arch(arm64) || arch(x86_64) let u: Double = 1.nextUp @@ -93,18 +126,6 @@ extension Double { assertClose(-2.4703282292062332640976590913373022e-324, Double.pow(-d, 6711563375777760775)) assertClose( 2.4703282292062329898361312467121758e-324, Double.pow(-d, 6711563375777760776)) assertClose(-2.4703282292062327155746034020870799e-324, Double.pow(-d, 6711563375777760777)) - // max/min exponents, these always saturate, but this will reveal - // errors in some implementations that one could try. - assertClose( .infinity, Self.pow(-u, Int.max - 1)) - assertClose( 0.0, Self.pow(-d, Int.max - 1)) - assertClose( 0.0, Self.pow(-u, -Int.max + 1)) - assertClose( .infinity, Self.pow(-d, -Int.max + 1)) - assertClose(-.infinity, Self.pow(-u, Int.max)) - assertClose(-0.0, Self.pow(-d, Int.max)) - assertClose(-0.0, Self.pow(-u, -Int.max)) - assertClose(-.infinity, Self.pow(-d, -Int.max)) - assertClose( 0.0, Self.pow(-u, Int.min)) - assertClose( .infinity, Self.pow(-d, Int.min)) #endif } } From 75e4116989cd7e9480b7c11a39fe9f21f509c5fd Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Fri, 10 Apr 2020 10:09:34 -0400 Subject: [PATCH 03/13] Additional test coverage for Float16 power with integer exponents. --- Tests/RealTests/IntegerExponentTests.swift | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Tests/RealTests/IntegerExponentTests.swift b/Tests/RealTests/IntegerExponentTests.swift index fcf0011..7fde6a3 100644 --- a/Tests/RealTests/IntegerExponentTests.swift +++ b/Tests/RealTests/IntegerExponentTests.swift @@ -130,7 +130,40 @@ extension Double { } } +@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) +extension Float16 { + static func testIntegerExponent() { + testIntegerExponentCommon() + testIntegerExponentDoubleAndSmaller() + let u = Float16(1).nextUp + let d = Float16(1).nextDown + // Smallest exponents not exactly representable as Float16. + assertClose(-7.3890572722436554354625993393835304, Float16.pow(-u, 0x801)) + assertClose(-0.3676100238077049750885141244927184, Float16.pow(-d, 0x801)) + // Exponents close to overflow boundary. + assertClose( 65403.86633107, Float16.pow(-u, 11360)) + assertClose(-65467.73729429, Float16.pow(-u, 11361)) + assertClose( 65531.67063149, Float16.pow(-u, 11362)) + assertClose( 65487.96799785, Float16.pow(-d, -22706)) + assertClose(-65519.96016590, Float16.pow(-d, -22707)) + assertClose( 65551.96796276, Float16.pow(-d, -22708)) + // Exponents close to underflow boundary. + assertClose( 5.966876499900e-8, Float16.pow(-u, -17042)) + assertClose(-5.961055156973e-8, Float16.pow(-u, -17043)) + assertClose( 5.955239493405e-8, Float16.pow(-u, -17044)) + assertClose( 5.964109628044e-8, Float16.pow(-d, 34060)) + assertClose(-5.961197465140e-8, Float16.pow(-d, 34061)) + assertClose( 5.958286724190e-8, Float16.pow(-d, 34062)) + } +} + final class IntegerExponentTests: XCTestCase { + + @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) + func testFloat16() { + Float16.testIntegerExponent() + } + func testFloat() { Float.testIntegerExponent() } From ee082efdaf1e2aa15b113ffdd51010af0dc152a5 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Fri, 10 Apr 2020 11:46:22 -0400 Subject: [PATCH 04/13] Fix bug for very large exponents pow(Float/Double, Int). Just-added tests expose this issue, where the split of n into high/low parts could produce parts with different signs. If both x**high and x**low then overflow and underflow, the resulting computation would be 0*inf (or inf*0) and produce NaN instead of either zero or infinity. --- Sources/RealModule/Double+Real.swift | 42 ++++++++++++++++++++++------ Sources/RealModule/Float+Real.swift | 21 ++++++++++---- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/Sources/RealModule/Double+Real.swift b/Sources/RealModule/Double+Real.swift index 05e44e9..38d07aa 100644 --- a/Sources/RealModule/Double+Real.swift +++ b/Sources/RealModule/Double+Real.swift @@ -158,16 +158,40 @@ extension Double: Real { // If n is exactly representable as Double, we can just call pow: // Note that all calls on a 32b platform go down this path. if let y = Double(exactly: n) { return libm_pow(x, y) } - // Otherwise, n is too large to losslessly represent as Double, so we - // just split it into two parts, high and low. This is always exact, - // so the only source of error is pow itself and the multiplication. + // n is not representable in Double, so we will split it into two parts, + // low and high, such that (high + low) = n, and use the identity: // - // mask constant is spelled in this funny way because if we just anded - // with the hex value, we'd get a compile error on 32b platforms, even - // though this whole branch is dead code on 32b. - let mask = Int(truncatingIfNeeded: 0x1f_ffff_ffff_ffff as UInt64) - let low = n & mask - let high = n - low + // x**(high + low) = x**high * x**low. + // + // We put the high-order 32 bits into high, and the remaining 32 bits + // in low. + // + // The exact split isn't important; all we need is that both pieces get + // less than 53 bits (so that they are exact) and that they both have + // the same sign as n. + // + // This second point is a little bit subtle--why is + // it necessary? Consider what would happen if we took x = 2 and + // n = Int.min + Int(UInt32.max), and simply naively split n without + // taking care with the sign. We would end up computing: + // + // 2**n = 2**Int.min * 2**UInt32.max + // + // The first exponent is negative, the second positive, so the first term + // underflows to zero, and the second overflows to infinity, so the final + // result is NaN, when it should be zero. In order to avoid this + // situation, we make sure that high contains n rounded *towards zero*, + // rather than using simple two's-complement truncation (which rounds + // down). + let mask = Int(truncatingIfNeeded: UInt32.max) + let round = n < 0 ? mask : 0 + // The addition and subtraction below cannot actually overflow (proof: + // round is positive if n is negative, and zero otherwise, so n + round + // is guaranteed to be representable, and n and high have the same sign, + // so n - high is also representable), but it's hard to tell the compiler + // that, so I'm using wrapping operations instead. + let high = (n &+ round) & ~mask + let low = n &- high return libm_pow(x, Double(low)) * libm_pow(x, Double(high)) } diff --git a/Sources/RealModule/Float+Real.swift b/Sources/RealModule/Float+Real.swift index b5df21f..83a0ab4 100644 --- a/Sources/RealModule/Float+Real.swift +++ b/Sources/RealModule/Float+Real.swift @@ -150,12 +150,21 @@ extension Float: Real { // The range of "interesting" n is -1488522191 ... 1744361944; outside // of this range, all x != 1 overflow or underflow, so only the parity // of x matters. We don't really care about the specific range at all, - // only that the bounds fit exactly into two Floats. Mask the low 24 - // bits of n, get pow with that exponent (this contains the parity), - // then get pow with the rest (this may round, but if it does we've - // saturated anyway, so it doesn't matter). - let low = n & 0xffffff - let high = n - low + // only that the bounds fit exactly into two Floats. + // + // We do, however, need to be careful that high and low both have the + // same sign as n (consult the Double implementation for details of why + // this matters), so we need to be a little bit careful constructing + // them. + // + // Unlike the Double implementation, when n is very large, high will + // get rounded here; that's OK because it does not change the sign or + // parity, which are the only two bits that matter for such large + // exponents in Float. + let mask = Int(truncatingIfNeeded: 0xffffff) + let round = n < 0 ? mask : 0 + let high = (n &+ round) & ~mask + let low = n &- high return libm_powf(x, Float(low)) * libm_powf(x, Float(high)) } From 59444343c4cbdc5573931d0bf3a6871b1e8b3ea2 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Fri, 10 Apr 2020 16:16:25 -0400 Subject: [PATCH 05/13] Somewhat better implementation of pow(x,n) for Float16. --- Sources/RealModule/Float16+Real.swift | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sources/RealModule/Float16+Real.swift b/Sources/RealModule/Float16+Real.swift index cb3d5bb..965795e 100644 --- a/Sources/RealModule/Float16+Real.swift +++ b/Sources/RealModule/Float16+Real.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +import _NumericsShims + @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) extension Float16: Real { @_transparent @@ -141,9 +143,14 @@ extension Float16: Real { @_transparent public static func pow(_ x: Float16, _ n: Int) -> Float16 { - // TODO: this can be improved because the range of n that don't overflow - // for all x != 1 is pretty small for Float16. - Float16(.pow(Float(x), n)) + // Float16 is simpler than Float or Double, because the range of + // "interesting" exponents is pretty small; anything outside of + // -22707 ... 34061 simply overflows or underflows for every + // x that isn't zero or one. This whole range is representable + // as Float, so we can just use powf as long as we're a little + // bit (get it?) careful to preserve parity. + let clamped = min(max(n, -0x10000), 0x10000) | (n & 1) + return Float16(libm_powf(Float(x), Float(clamped))) } @_transparent From a5cc950a86d005946b8d33a1995ecfcd0150c7fe Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Wed, 22 Apr 2020 16:41:30 -0400 Subject: [PATCH 06/13] Merge pull request #111 from rxwei/complex-derivatives Make 'Complex' differentiable. --- Sources/ComplexModule/Differentiable.swift | 94 ++++++++++++++++++++ Tests/ComplexTests/DifferentiableTests.swift | 74 +++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 Sources/ComplexModule/Differentiable.swift create mode 100644 Tests/ComplexTests/DifferentiableTests.swift diff --git a/Sources/ComplexModule/Differentiable.swift b/Sources/ComplexModule/Differentiable.swift new file mode 100644 index 0000000..75883f7 --- /dev/null +++ b/Sources/ComplexModule/Differentiable.swift @@ -0,0 +1,94 @@ +//===--- Differentiable.swift ---------------------------------*- swift -*-===// +// +// This source file is part of the Swift Numerics open source project +// +// Copyright (c) 2019 - 2020 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +#if canImport(_Differentiation) +import _Differentiation + +extension Complex: Differentiable +where RealType: Differentiable, RealType.TangentVector == RealType { + public typealias TangentVector = Self +} + +extension Complex +where RealType: Differentiable, RealType.TangentVector == RealType { + @derivative(of: init(_:_:)) + @usableFromInline + static func _derivativeInit( + _ real: RealType, + _ imaginary: RealType + ) -> (value: Complex, pullback: (Complex) -> (RealType, RealType)) { + (value: .init(real, imaginary), pullback: { v in + (v.real, v.imaginary) + }) + } + + @derivative(of: real) + @usableFromInline + func _derivativeReal() -> (value: RealType, pullback: (RealType) -> Complex) { + (value: real, pullback: { v in + Complex(v, .zero) + }) + } + + @derivative(of: imaginary) + @usableFromInline + func _derivativeImaginary() -> ( + value: RealType, + pullback: (RealType) -> Complex + ) { + (value: real, pullback: { v in + Complex(.zero, v) + }) + } + + @derivative(of: +) + @usableFromInline + static func _derivativeAdd(lhs: Complex, rhs: Complex) + -> (value: Complex, pullback: (Complex) -> (Complex, Complex)) + { + (lhs + rhs, { v in (v, v) }) + } + + @derivative(of: -) + @usableFromInline + static func _derivativeSubtract(lhs: Complex, rhs: Complex) + -> (value: Complex, pullback: (Complex) -> (Complex, Complex)) + { + (lhs - rhs, { v in (v, -v) }) + } + + @derivative(of: *) + @usableFromInline + static func _derivativeMultiply(lhs: Complex, rhs: Complex) + -> (value: Complex, pullback: (Complex) -> (Complex, Complex)) + { + (lhs * rhs, { v in (rhs * v, lhs * v) }) + } + + @derivative(of: /) + @usableFromInline + static func _derivativeDivide(lhs: Complex, rhs: Complex) + -> (value: Complex, pullback: (Complex) -> (Complex, Complex)) + { + (lhs / rhs, { v in (v / rhs, -lhs / (rhs * rhs) * v) }) + } + + @derivative(of: conjugate) + @usableFromInline + func _derivativeConjugate() -> ( + value: Complex, + pullback: (Complex) -> Complex + ) { + (conjugate, { v in v.conjugate }) + } +} + +#endif diff --git a/Tests/ComplexTests/DifferentiableTests.swift b/Tests/ComplexTests/DifferentiableTests.swift new file mode 100644 index 0000000..0dc09df --- /dev/null +++ b/Tests/ComplexTests/DifferentiableTests.swift @@ -0,0 +1,74 @@ +//===--- DifferentiableTests.swift ----------------------------*- swift -*-===// +// +// This source file is part of the Swift Numerics open source project +// +// Copyright (c) 2019 - 2020 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +#if canImport(_Differentiation) + +import XCTest +import ComplexModule +import _Differentiation + +final class DifferentiableTests: XCTestCase { + func testComponentGetter() { + XCTAssertEqual( + gradient(at: Complex(5, 5)) { $0.real * 2 }, + Complex(2, 0)) + XCTAssertEqual( + gradient(at: Complex(5, 5)) { $0.imaginary * 2 }, + Complex(0, 2)) + XCTAssertEqual( + gradient(at: Complex(5, 5)) { $0.real * 5 + $0.imaginary * 2 }, + Complex(5, 2)) + } + + func testInitializer() { + let φ1 = pullback(at: 4, -3) { r, i in Complex(r, i) } + let tan1 = φ1(Complex(-1, 2)) + XCTAssertEqual(tan1.0, -1) + XCTAssertEqual(tan1.1, 2) + + let φ2 = pullback(at: 4, -3) { r, i in Complex(r * r, i + i) } + let tan2 = φ2(Complex(-1, 1)) + XCTAssertEqual(tan2.0, -8) + XCTAssertEqual(tan2.1, 2) + } + + func testConjugate() { + let φ = pullback(at: Complex(20, -4)) { x in x.conjugate } + XCTAssertEqual(φ(Complex(1, 0)), Complex(1, 0)) + XCTAssertEqual(φ(Complex(0, 1)), Complex(0, -1)) + XCTAssertEqual(φ(Complex(-1, 1)), Complex(-1, -1)) + } + + func testArithmetics() { + let φAdd = pullback(at: Complex(2, 3)) { x in + x + Complex(5, 6) + } + XCTAssertEqual(φAdd(Complex(1, 1)), Complex(1, 1)) + + let φSubtract = pullback(at: Complex(2, 3)) { x in + Complex(5, 6) - x + } + XCTAssertEqual(φSubtract(Complex(1, 1)), Complex(-1, -1)) + + let φMultiply = pullback(at: Complex(2, 3)) { x in x * x } + XCTAssertEqual(φMultiply(Complex(1, 0)), Complex(4, 6)) + XCTAssertEqual(φMultiply(Complex(0, 1)), Complex(-6, 4)) + XCTAssertEqual(φMultiply(Complex(1, 1)), Complex(-2, 10)) + + let φDivide = pullback(at: Complex(20, -4)) { x in + x / Complex(2, 2) + } + XCTAssertEqual(φDivide(Complex(1, 0)), Complex(0.25, -0.25)) + XCTAssertEqual(φDivide(Complex(0, 1)), Complex(0.25, 0.25)) + } +} + +#endif From 93b8e9f0dfc860e8536d84c798b64c64970ac1a6 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Mon, 29 Jun 2020 14:50:37 -0400 Subject: [PATCH 07/13] Float16 is unavailable on macOS 11. --- Sources/RealModule/Float16+Real.swift | 4 +- .../_NumericsShims/include/_NumericsShims.h | 7 ++- Tests/RealTests/IntegerExponentTests.swift | 59 ++++++++++--------- Tests/RealTests/RealTests.swift | 4 +- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/Sources/RealModule/Float16+Real.swift b/Sources/RealModule/Float16+Real.swift index 965795e..c64a047 100644 --- a/Sources/RealModule/Float16+Real.swift +++ b/Sources/RealModule/Float16+Real.swift @@ -11,7 +11,9 @@ import _NumericsShims -@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) +@available(iOS 14.0, tvOS 14.0, watchOS 7.0, *) +@available(macOS, unavailable) +@available(macCatalyst, unavailable) extension Float16: Real { @_transparent public static func cos(_ x: Float16) -> Float16 { diff --git a/Sources/_NumericsShims/include/_NumericsShims.h b/Sources/_NumericsShims/include/_NumericsShims.h index 4e1b40b..dfe0fe8 100644 --- a/Sources/_NumericsShims/include/_NumericsShims.h +++ b/Sources/_NumericsShims/include/_NumericsShims.h @@ -2,7 +2,7 @@ // // This source file is part of the Swift Numerics open source project // -// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +// Copyright (c) 2019-2020 Apple Inc. and the Swift Numerics project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -383,7 +383,9 @@ HEADER_SHIM long double libm_lgammal(long double x, int *signp) { #endif // MARK: - shims to import C complex operations for timing purposes - +// Clang doesn't provide complex arithmetic on Windows (because MSVC +// doesn't), so we can't define these there, or we'll get link errors. +#if !defined _WIN32 typedef struct { double real; double imag; } CComplex; HEADER_SHIM CComplex libm_cdiv(CComplex z, CComplex w) { @@ -399,3 +401,4 @@ HEADER_SHIM CComplex libm_cmul(CComplex z, CComplex w) { double _Complex c = a*b; return (CComplex){ __real__ c, __imag__ c }; } +#endif // !defined _WIN32 diff --git a/Tests/RealTests/IntegerExponentTests.swift b/Tests/RealTests/IntegerExponentTests.swift index 7fde6a3..804ffcc 100644 --- a/Tests/RealTests/IntegerExponentTests.swift +++ b/Tests/RealTests/IntegerExponentTests.swift @@ -73,6 +73,34 @@ internal extension Real where Self: BinaryFloatingPoint, } } +@available(iOS 14.0, watchOS 14.0, tvOS 7.0, *) +@available(macOS, unavailable) +extension Float16 { + static func testIntegerExponent() { + testIntegerExponentCommon() + testIntegerExponentDoubleAndSmaller() + let u = Float16(1).nextUp + let d = Float16(1).nextDown + // Smallest exponents not exactly representable as Float16. + assertClose(-7.3890572722436554354625993393835304, Float16.pow(-u, 0x801)) + assertClose(-0.3676100238077049750885141244927184, Float16.pow(-d, 0x801)) + // Exponents close to overflow boundary. + assertClose( 65403.86633107, Float16.pow(-u, 11360)) + assertClose(-65467.73729429, Float16.pow(-u, 11361)) + assertClose( 65531.67063149, Float16.pow(-u, 11362)) + assertClose( 65487.96799785, Float16.pow(-d, -22706)) + assertClose(-65519.96016590, Float16.pow(-d, -22707)) + assertClose( 65551.96796276, Float16.pow(-d, -22708)) + // Exponents close to underflow boundary. + assertClose( 5.966876499900e-8, Float16.pow(-u, -17042)) + assertClose(-5.961055156973e-8, Float16.pow(-u, -17043)) + assertClose( 5.955239493405e-8, Float16.pow(-u, -17044)) + assertClose( 5.964109628044e-8, Float16.pow(-d, 34060)) + assertClose(-5.961197465140e-8, Float16.pow(-d, 34061)) + assertClose( 5.958286724190e-8, Float16.pow(-d, 34062)) + } +} + extension Float { static func testIntegerExponent() { testIntegerExponentCommon() @@ -130,36 +158,11 @@ extension Double { } } -@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) -extension Float16 { - static func testIntegerExponent() { - testIntegerExponentCommon() - testIntegerExponentDoubleAndSmaller() - let u = Float16(1).nextUp - let d = Float16(1).nextDown - // Smallest exponents not exactly representable as Float16. - assertClose(-7.3890572722436554354625993393835304, Float16.pow(-u, 0x801)) - assertClose(-0.3676100238077049750885141244927184, Float16.pow(-d, 0x801)) - // Exponents close to overflow boundary. - assertClose( 65403.86633107, Float16.pow(-u, 11360)) - assertClose(-65467.73729429, Float16.pow(-u, 11361)) - assertClose( 65531.67063149, Float16.pow(-u, 11362)) - assertClose( 65487.96799785, Float16.pow(-d, -22706)) - assertClose(-65519.96016590, Float16.pow(-d, -22707)) - assertClose( 65551.96796276, Float16.pow(-d, -22708)) - // Exponents close to underflow boundary. - assertClose( 5.966876499900e-8, Float16.pow(-u, -17042)) - assertClose(-5.961055156973e-8, Float16.pow(-u, -17043)) - assertClose( 5.955239493405e-8, Float16.pow(-u, -17044)) - assertClose( 5.964109628044e-8, Float16.pow(-d, 34060)) - assertClose(-5.961197465140e-8, Float16.pow(-d, 34061)) - assertClose( 5.958286724190e-8, Float16.pow(-d, 34062)) - } -} - final class IntegerExponentTests: XCTestCase { - @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) + @available(iOS 14.0, watchOS 14.0, tvOS 7.0, *) + @available(macOS, unavailable) + @available(macCatalyst, unavailable) func testFloat16() { Float16.testIntegerExponent() } diff --git a/Tests/RealTests/RealTests.swift b/Tests/RealTests/RealTests.swift index 0e10f41..bfb96dd 100644 --- a/Tests/RealTests/RealTests.swift +++ b/Tests/RealTests/RealTests.swift @@ -59,12 +59,14 @@ internal extension Real where Self: BinaryFloatingPoint { final class ElementaryFunctionChecks: XCTestCase { + #if !os(macOS) func testFloat16() { - if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) { + if #available(iOS 14.0, watchOS 14.0, tvOS 7.0, *) { Float16.elementaryFunctionChecks() Float16.realFunctionChecks() } } + #endif func testFloat() { Float.elementaryFunctionChecks() From e97070e36cdc378c9ba9dd6a97ba6975a43b268f Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Mon, 29 Jun 2020 14:52:30 -0400 Subject: [PATCH 08/13] file -> filePath, too. --- Tests/RealTests/RealTestSupport.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/RealTests/RealTestSupport.swift b/Tests/RealTests/RealTestSupport.swift index 8296c43..103e10a 100644 --- a/Tests/RealTests/RealTestSupport.swift +++ b/Tests/RealTests/RealTestSupport.swift @@ -23,7 +23,7 @@ func assertClose( _ expected: TestLiteralType, _ observed: T, allowedError: T = 16, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line ) -> T where T: BinaryFloatingPoint { // Shortcut relative-error check if we got the sign wrong; it's OK to @@ -74,7 +74,7 @@ func assertClose( _ observed: T, allowedError: T = 16, worstError: inout T, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line ) where T: BinaryFloatingPoint { worstError = max(worstError, assertClose( From 40b5a03063e690ec413e46e0df41a9d2c96c4677 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Fri, 3 Jul 2020 09:45:25 -0400 Subject: [PATCH 09/13] Merge pull request #105 from compnerd/cmake build: add initial CMake based build system --- CMakeLists.txt | 33 ++++++++ Sources/CMakeLists.txt | 13 +++ Sources/ComplexModule/CMakeLists.txt | 21 +++++ Sources/Numerics/CMakeLists.txt | 20 +++++ Sources/RealModule/CMakeLists.txt | 25 ++++++ Sources/_NumericsShims/CMakeLists.txt | 14 ++++ .../_NumericsShims/include/module.modulemap | 3 + Tests/CMakeLists.txt | 24 ++++++ Tests/ComplexTests/CMakeLists.txt | 21 +++++ Tests/RealTests/CMakeLists.txt | 19 +++++ Tests/WindowsMain.swift | 76 +++++++++++++++++ cmake/modules/SwiftSupport.cmake | 84 +++++++++++++++++++ 12 files changed, 353 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 Sources/CMakeLists.txt create mode 100644 Sources/ComplexModule/CMakeLists.txt create mode 100644 Sources/Numerics/CMakeLists.txt create mode 100644 Sources/RealModule/CMakeLists.txt create mode 100644 Sources/_NumericsShims/CMakeLists.txt create mode 100644 Sources/_NumericsShims/include/module.modulemap create mode 100644 Tests/CMakeLists.txt create mode 100644 Tests/ComplexTests/CMakeLists.txt create mode 100644 Tests/RealTests/CMakeLists.txt create mode 100644 Tests/WindowsMain.swift create mode 100644 cmake/modules/SwiftSupport.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e0e8058 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,33 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +cmake_minimum_required(VERSION 3.16) +project(swift-numerics + LANGUAGES Swift) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules) + +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift) + +include(CTest) +include(SwiftSupport) + +add_subdirectory(Sources) +if(BUILD_TESTING) + add_subdirectory(Tests) +endif() + +get_property(SWIFT_NUMERICS_EXPORTS GLOBAL PROPERTY SWIFT_NUMERICS_EXPORTS) +export(TARGETS ${SWIFT_NUMERICS_EXPORTS} + NAMESPACE SwiftNumerics:: + FILE swift-numerics-config.cmake + EXPORT_LINK_INTERFACE_LIBRARIES) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt new file mode 100644 index 0000000..4107e69 --- /dev/null +++ b/Sources/CMakeLists.txt @@ -0,0 +1,13 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_subdirectory(_NumericsShims) +add_subdirectory(ComplexModule) +add_subdirectory(Numerics) +add_subdirectory(RealModule) diff --git a/Sources/ComplexModule/CMakeLists.txt b/Sources/ComplexModule/CMakeLists.txt new file mode 100644 index 0000000..7ddfbd1 --- /dev/null +++ b/Sources/ComplexModule/CMakeLists.txt @@ -0,0 +1,21 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(ComplexModule + Arithmetic.swift + Complex.swift + Differentiable.swift) +set_target_properties(ComplexModule PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) +target_link_libraries(ComplexModule PUBLIC + RealModule) + + +_install_target(ComplexModule) +set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS ComplexModule) diff --git a/Sources/Numerics/CMakeLists.txt b/Sources/Numerics/CMakeLists.txt new file mode 100644 index 0000000..9eea6ae --- /dev/null +++ b/Sources/Numerics/CMakeLists.txt @@ -0,0 +1,20 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(Numerics + Numerics.swift) +set_target_properties(Numerics PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) +target_link_libraries(Numerics PUBLIC + ComplexModule + RealModule) + + +_install_target(Numerics) +set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS Numerics) diff --git a/Sources/RealModule/CMakeLists.txt b/Sources/RealModule/CMakeLists.txt new file mode 100644 index 0000000..37a8b61 --- /dev/null +++ b/Sources/RealModule/CMakeLists.txt @@ -0,0 +1,25 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(RealModule + AlgebraicField.swift + Double+Real.swift + ElementaryFunctions.swift + Float+Real.swift + Float80+Real.swift + Real.swift + RealFunctions.swift) +set_target_properties(RealModule PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) +target_link_libraries(RealModule PUBLIC + _NumericsShims) + + +_install_target(RealModule) +set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS RealModule) diff --git a/Sources/_NumericsShims/CMakeLists.txt b/Sources/_NumericsShims/CMakeLists.txt new file mode 100644 index 0000000..92ed523 --- /dev/null +++ b/Sources/_NumericsShims/CMakeLists.txt @@ -0,0 +1,14 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(_NumericsShims INTERFACE) +target_include_directories(_NumericsShims INTERFACE + include) + +set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS _NumericsShims) diff --git a/Sources/_NumericsShims/include/module.modulemap b/Sources/_NumericsShims/include/module.modulemap new file mode 100644 index 0000000..077fb43 --- /dev/null +++ b/Sources/_NumericsShims/include/module.modulemap @@ -0,0 +1,3 @@ +module _NumericsShims { + header "_NumericsShims.h" +} diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt new file mode 100644 index 0000000..b879f0e --- /dev/null +++ b/Tests/CMakeLists.txt @@ -0,0 +1,24 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +find_package(dispatch CONFIG QUIET) +find_package(Foundation CONFIG QUIET) +find_package(XCTest CONFIG QUIET) + +add_subdirectory(ComplexTests) +add_subdirectory(RealTests) + +add_executable(SwiftNumericsTestRunner + WindowsMain.swift) +target_link_libraries(SwiftNumericsTestRunner PRIVATE + ComplexTests + RealTests) + +add_test(NAME SwiftNumericsTestRunner + COMMAND SwiftNumericsTestRunner) diff --git a/Tests/ComplexTests/CMakeLists.txt b/Tests/ComplexTests/CMakeLists.txt new file mode 100644 index 0000000..88502cf --- /dev/null +++ b/Tests/ComplexTests/CMakeLists.txt @@ -0,0 +1,21 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(ComplexTests + ArithmeticTests.swift + DifferentiableTests.swift + PropertyTests.swift) +set_target_properties(ComplexTests PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) +target_compile_options(ComplexTests PRIVATE + -enable-testing) +target_link_libraries(ComplexTests PUBLIC + $<$>:Foundation> + ComplexModule + XCTest) diff --git a/Tests/RealTests/CMakeLists.txt b/Tests/RealTests/CMakeLists.txt new file mode 100644 index 0000000..53c9a4f --- /dev/null +++ b/Tests/RealTests/CMakeLists.txt @@ -0,0 +1,19 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(RealTests + IntegerExponentTests.swift + RealTests.swift + RealTestSupport.swift) +target_compile_options(RealTests PRIVATE + -enable-testing) +target_link_libraries(RealTests PUBLIC + $<$>:Foundation> + RealModule + XCTest) diff --git a/Tests/WindowsMain.swift b/Tests/WindowsMain.swift new file mode 100644 index 0000000..9414d23 --- /dev/null +++ b/Tests/WindowsMain.swift @@ -0,0 +1,76 @@ +//===--- WindowsMain.swift ------------------------------------*- swift -*-===// +// +// This source file is part of the Swift Numerics open source project +// +// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +#if os(Windows) +import XCTest + +@testable +import RealTests + +@testable +import ComplexTests + +extension ElementaryFunctionChecks { + static var all = testCase([ + ("testFloat", ElementaryFunctionChecks.testFloat), + ("testDouble", ElementaryFunctionChecks.testDouble), + ]) +} + +extension IntegerExponentTests { + static var all = testCase([ + ("testFloat", IntegerExponentTests.testFloat), + ("testDouble", IntegerExponentTests.testDouble), + ]) +} + +extension ArithmeticTests { + static var all = testCase([ + ("testPolar", ArithmeticTests.testPolar), + ("testBaudinSmith", ArithmeticTests.testBaudinSmith), + ("testDivisionByZero", ArithmeticTests.testDivisionByZero), + ]) +} + +#if canImport(_Differentiation) + extension DifferentiableTests { + static var all = testCase([ + ("testComponentGetter", DifferentiableTests.testComponentGetter), + ("testConjugate", DifferentiableTests.testConjugate), + ("testArithmetics", DifferentiableTests.testArithmetics), + ]) + } +#endif + +extension PropertyTests { + static var all = testCase([ + ("testProperties", PropertyTests.testProperties), + ("testEquatableHashable", PropertyTests.testEquatableHashable), + ("testCodable", PropertyTests.testCodable), + ]) +} + +var testCases = [ + ElementaryFunctionChecks.all, + IntegerExponentTests.all, + ArithmeticTests.all, + PropertyTests.all, +] + +#if canImport(_Differentiation) +testCases += [ + DifferentiableTests.all +] +#endif + +XCTMain(testCases) + +#endif diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake new file mode 100644 index 0000000..ec268ed --- /dev/null +++ b/cmake/modules/SwiftSupport.cmake @@ -0,0 +1,84 @@ +#[[ +This source file is part of the Swift Numerics open source project + +Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +# Returns the architecture name in a variable +# +# Usage: +# get_swift_host_arch(result_var_name) +# +# Sets ${result_var_name} with the converted architecture name derived from +# CMAKE_SYSTEM_PROCESSOR. +function(get_swift_host_arch result_var_name) + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") + set("${result_var_name}" "x86_64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64") + set("${result_var_name}" "aarch64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64") + set("${result_var_name}" "powerpc64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le") + set("${result_var_name}" "powerpc64le" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x") + set("${result_var_name}" "s390x" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l") + set("${result_var_name}" "armv6" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l") + set("${result_var_name}" "armv7" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a") + set("${result_var_name}" "armv7" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64") + set("${result_var_name}" "x86_64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64") + set("${result_var_name}" "itanium" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86") + set("${result_var_name}" "i686" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686") + set("${result_var_name}" "i686" PARENT_SCOPE) + else() + message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}") + endif() +endfunction() + +# Returns the os name in a variable +# +# Usage: +# get_swift_host_os(result_var_name) +# +# +# Sets ${result_var_name} with the converted OS name derived from +# CMAKE_SYSTEM_NAME. +function(get_swift_host_os result_var_name) + if(CMAKE_SYSTEM_NAME STREQUAL Darwin) + set(${result_var_name} macosx PARENT_SCOPE) + else() + string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc) + set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE) + endif() +endfunction() + +function(_install_target module) + get_swift_host_arch(swift_arch) + get_swift_host_os(swift_os) + install(TARGETS ${module} + ARCHIVE DESTINATION lib/swift$<$>:_static>/${swift_os} + LIBRARY DESTINATION lib/swift$<$>:_static>/${swift_os} + RUNTIME DESTINATION bin) + if(CMAKE_SYSTEM_NAME STREQUAL Darwin) + install(FILES $/${module}.swiftdoc + DESTINATION lib/swift$<$>:_static>/${swift_os}/${mmodule}.swiftmodule + RENAME ${swift_arch}.swiftdoc) + install(FILES $/${module}.swiftmodule + DESTINATION lib/swift$<$>:_static>/${swift_os}/${mmodule}.swiftmodule + RENAME ${swift_arch}.swiftmodule) + else() + install(FILES + $/${module}.swiftdoc + $/${module}.swiftmodule + DESTINATION lib/swift$<$>:_static>/${swift_os}/${swift_arch}) + endif() +endfunction() From 4cd11553dbe034f22261799cdb90df5fafc4f0b8 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Mon, 27 Jul 2020 09:44:42 -0400 Subject: [PATCH 10/13] Add Float16 tests for Approximate Equality. --- Tests/RealTests/ApproximateEqualityTests.swift | 9 +++++++++ Tests/RealTests/RealTestSupport.swift | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/Tests/RealTests/ApproximateEqualityTests.swift b/Tests/RealTests/ApproximateEqualityTests.swift index d4d83fd..9bbfc5a 100644 --- a/Tests/RealTests/ApproximateEqualityTests.swift +++ b/Tests/RealTests/ApproximateEqualityTests.swift @@ -110,6 +110,15 @@ final class ElementaryFunctionTests: XCTestCase { } } + @available(iOS 14.0, watchOS 14.0, tvOS 7.0, *) + @available(macOS, unavailable) + @available(macCatalyst, unavailable) + func testFloat16() { + testSpecials(Float16.self) + testDefaults(Float16.self) + testRandom(Float16.self) + } + func testFloat() { testSpecials(Float.self) testDefaults(Float.self) diff --git a/Tests/RealTests/RealTestSupport.swift b/Tests/RealTests/RealTestSupport.swift index b61328d..f43ab07 100644 --- a/Tests/RealTests/RealTestSupport.swift +++ b/Tests/RealTests/RealTestSupport.swift @@ -86,6 +86,11 @@ internal protocol FixedWidthFloatingPoint: BinaryFloatingPoint where Exponent: FixedWidthInteger, RawSignificand: FixedWidthInteger { } +@available(iOS 14.0, watchOS 14.0, tvOS 7.0, *) +@available(macOS, unavailable) +@available(macCatalyst, unavailable) +extension Float16: FixedWidthFloatingPoint { } + extension Float: FixedWidthFloatingPoint { } extension Double: FixedWidthFloatingPoint { } #if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android) From 66ac02125c33f8519dc1dd803e69becd007078bd Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Mon, 27 Jul 2020 10:06:08 -0400 Subject: [PATCH 11/13] Don't test approximate equality for Float80 on platforms that don't have it. --- Tests/RealTests/ApproximateEqualityTests.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/RealTests/ApproximateEqualityTests.swift b/Tests/RealTests/ApproximateEqualityTests.swift index 9bbfc5a..a4daee6 100644 --- a/Tests/RealTests/ApproximateEqualityTests.swift +++ b/Tests/RealTests/ApproximateEqualityTests.swift @@ -131,9 +131,11 @@ final class ElementaryFunctionTests: XCTestCase { testRandom(Double.self) } + #if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android) func testFloat80() { testSpecials(Float80.self) testDefaults(Float80.self) testRandom(Float80.self) } + #endif } From 336740bd6d88ebd532b5ed7cec1b4fe9d17c89ad Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Mon, 27 Jul 2020 11:53:02 -0400 Subject: [PATCH 12/13] Also mark Complex test unavailable where needed. --- Tests/ComplexTests/ApproximateEqualityTests.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/ComplexTests/ApproximateEqualityTests.swift b/Tests/ComplexTests/ApproximateEqualityTests.swift index 1b0e24e..8705486 100644 --- a/Tests/ComplexTests/ApproximateEqualityTests.swift +++ b/Tests/ComplexTests/ApproximateEqualityTests.swift @@ -48,7 +48,9 @@ final class ElementaryFunctionTests: XCTestCase { testSpecials(Double.self) } + #if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android) func testFloat80() { testSpecials(Float80.self) } + #endif } From 4771ef24d20325fe4ab16d093efa050519c2e73b Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Mon, 27 Jul 2020 11:53:28 -0400 Subject: [PATCH 13/13] Remove float16 tests for approx equality for now. --- Tests/RealTests/ApproximateEqualityTests.swift | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Tests/RealTests/ApproximateEqualityTests.swift b/Tests/RealTests/ApproximateEqualityTests.swift index a4daee6..ec44b37 100644 --- a/Tests/RealTests/ApproximateEqualityTests.swift +++ b/Tests/RealTests/ApproximateEqualityTests.swift @@ -110,15 +110,6 @@ final class ElementaryFunctionTests: XCTestCase { } } - @available(iOS 14.0, watchOS 14.0, tvOS 7.0, *) - @available(macOS, unavailable) - @available(macCatalyst, unavailable) - func testFloat16() { - testSpecials(Float16.self) - testDefaults(Float16.self) - testRandom(Float16.self) - } - func testFloat() { testSpecials(Float.self) testDefaults(Float.self)