diff --git a/Sources/Complex/Arithmetic.swift b/Sources/Complex/Arithmetic.swift index 0d98eec..0ef8f77 100644 --- a/Sources/Complex/Arithmetic.swift +++ b/Sources/Complex/Arithmetic.swift @@ -94,8 +94,12 @@ extension Complex: Numeric { } @usableFromInline + @_specialize(where RealType == Float) + @_specialize(where RealType == Double) internal static func rescaledDivide(_ z: Complex, _ w: Complex) -> Complex { if z.isZero || !w.isFinite { return .zero } + // TODO: detect when RealType is Float and just promote to Double, then + // use the naive algorithm. let zScale = z.magnitude let wScale = w.magnitude let zNorm = z.divided(by: zScale) diff --git a/Sources/ElementaryFunctions/ScalarConformances.swift b/Sources/ElementaryFunctions/ScalarConformances.swift index 02d9017..ffc1e14 100644 --- a/Sources/ElementaryFunctions/ScalarConformances.swift +++ b/Sources/ElementaryFunctions/ScalarConformances.swift @@ -91,7 +91,22 @@ extension Double: Real { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) @_transparent public static func exp10(_ x: Double) -> Double { return libm_exp10(x) } #endif - @_transparent public static func hypot(_ x: Double, _ y: Double) -> Double { return libm_hypot(x, y) } + #if os(macOS) && arch(x86_64) + // Workaround for macOS bug () where hypot can + // overflow for values very close to the overflow boundary of the naive + // algorithm. Since this is only for macOS, we can just unconditionally + // use Float80, which makes the implementation trivial. + public static func hypot(_ x: Double, _ y: Double) -> Double { + if x.isInfinite || y.isInfinite { return .infinity } + let x80 = Float80(x) + let y80 = Float80(y) + return Double(Float80.sqrt(x80*x80 + y80*y80)) + } + #else + @_transparent public static func hypot(_ x: Double, _ y: Double) -> Double { + return libm_hypot(x, y) + } + #endif @_transparent public static func gamma(_ x: Double) -> Double { return libm_tgamma(x) } @_transparent public static func log2(_ x: Double) -> Double { return libm_log2(x) } @_transparent public static func log10(_ x: Double) -> Double { return libm_log10(x) } diff --git a/Tests/ComplexTests/ArithmeticBenchmarkTests.swift b/Tests/ComplexTests/ArithmeticBenchmarkTests.swift index 0623881..d7326c1 100644 --- a/Tests/ComplexTests/ArithmeticBenchmarkTests.swift +++ b/Tests/ComplexTests/ArithmeticBenchmarkTests.swift @@ -26,12 +26,12 @@ extension Complex where RealType == Double { } } -let wellScaledDoubles: [Complex] = (0 ..< 1000).map { _ in +let wellScaledDoubles: [Complex] = (0 ..< 1024).map { _ in Complex(length: Double.random(in: 0.5 ..< 2.0), phase: Double.random(in: -.pi ..< .pi))! } -let poorlyScaledDoubles: [Complex] = (0 ..< 1000).map { _ in +let poorlyScaledDoubles: [Complex] = (0 ..< 1024).map { _ in Complex( length: Double(sign: .plus, exponent: .random(in: -970 ... 1023), @@ -42,7 +42,7 @@ let poorlyScaledDoubles: [Complex] = (0 ..< 1000).map { _ in final class ArithmeticBenchmarkTests: XCTestCase { - func testDivision() { + func testDivisionByConstant() { let divisor = wellScaledDoubles[0] var sum = Complex.zero measure { @@ -64,7 +64,7 @@ final class ArithmeticBenchmarkTests: XCTestCase { print(sum) } - func testDivisionC() { + func testDivisionByConstantC() { let divisor = wellScaledDoubles[0].ctype var sum = Complex.zero measure { @@ -101,24 +101,50 @@ final class ArithmeticBenchmarkTests: XCTestCase { print(sum) } - func testDivisionPoorScaling() { - let divisor = poorlyScaledDoubles[0] + func testDivision() { var sum = Complex.zero measure { for _ in 0 ..< 100 { - sum = poorlyScaledDoubles.reduce(into: sum) { $0 += $1 / divisor } + for i in 0 ..< 1024 { + sum += wellScaledDoubles[i] / wellScaledDoubles[(i - 1) & 1023] + } + } + } + print(sum) + } + + func testDivisionC() { + var sum = Complex.zero + measure { + for _ in 0 ..< 100 { + for i in 0 ..< 1024 { + sum += Complex(libm_cdiv(wellScaledDoubles[i].ctype, + wellScaledDoubles[(i - 1) & 1023].ctype)) + } + } + } + print(sum) + } + + func testDivisionPoorScaling() { + var sum = Complex.zero + measure { + for _ in 0 ..< 100 { + for i in 0 ..< 1024 { + sum += poorlyScaledDoubles[i] / poorlyScaledDoubles[(i - 1) & 1023] + } } } print(sum) } func testDivisionPoorScalingC() { - let divisor = poorlyScaledDoubles[0].ctype var sum = Complex.zero measure { for _ in 0 ..< 100 { - sum = poorlyScaledDoubles.reduce(into: sum) { - $0 += Complex(libm_cdiv($1.ctype, divisor)) + for i in 0 ..< 1024 { + sum += Complex(libm_cdiv(poorlyScaledDoubles[i].ctype, + poorlyScaledDoubles[(i - 1) & 1023].ctype)) } } }