Further benchmark improvments, workaround hypot bug. (#28)

Added benchmark version that defeats hoisting part of the divide computation outside of the workloop (but kept the old ones as well, because *both* are good representitives of what we want to track). Also found a bug in macOS hypot very close to the overflow boundary for the naive algorithm, and added a workaround (also reported the bug).
This commit is contained in:
Stephen Canon authored and GitHub committed 2019-11-02 21:40:00 -04:00
1 parent e3b632a3c4
commit a2353c82df
3 files changed
+56 -11

No files matched your search

+4
View File
@@ -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)
@@ -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 (<rdar://problem/56844150>) 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) }
@@ -26,12 +26,12 @@ extension Complex where RealType == Double {
}
}
let wellScaledDoubles: [Complex<Double>] = (0 ..< 1000).map { _ in
let wellScaledDoubles: [Complex<Double>] = (0 ..< 1024).map { _ in
Complex(length: Double.random(in: 0.5 ..< 2.0),
phase: Double.random(in: -.pi ..< .pi))!
}
let poorlyScaledDoubles: [Complex<Double>] = (0 ..< 1000).map { _ in
let poorlyScaledDoubles: [Complex<Double>] = (0 ..< 1024).map { _ in
Complex(
length: Double(sign: .plus,
exponent: .random(in: -970 ... 1023),
@@ -42,7 +42,7 @@ let poorlyScaledDoubles: [Complex<Double>] = (0 ..< 1000).map { _ in
final class ArithmeticBenchmarkTests: XCTestCase {
func testDivision() {
func testDivisionByConstant() {
let divisor = wellScaledDoubles[0]
var sum = Complex<Double>.zero
measure {
@@ -64,7 +64,7 @@ final class ArithmeticBenchmarkTests: XCTestCase {
print(sum)
}
func testDivisionC() {
func testDivisionByConstantC() {
let divisor = wellScaledDoubles[0].ctype
var sum = Complex<Double>.zero
measure {
@@ -101,24 +101,50 @@ final class ArithmeticBenchmarkTests: XCTestCase {
print(sum)
}
func testDivisionPoorScaling() {
let divisor = poorlyScaledDoubles[0]
func testDivision() {
var sum = Complex<Double>.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<Double>.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<Double>.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<Double>.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))
}
}
}