Skip to content

[test] Accelerate: Don’t hide utility functions in an if #available block #29138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions test/stdlib/Accelerate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -529,51 +529,51 @@ if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) {
expectTrue(elementsAlmostEqual(result, legacyResult))
expectTrue(elementsAlmostEqual(result, returnedResult))
}

//===----------------------------------------------------------------------===//
//
// Array almost equal.
//
//===----------------------------------------------------------------------===//

func elementsAlmostEqual<T: FloatingPoint>(_ lhs: [T], _ rhs: [T]) -> Bool {
var returnValue = true
zip(lhs, rhs).forEach {
if !isAlmostEqual($0.0, $0.1) {
returnValue = false
return
}
}
return returnValue
}

func isAlmostEqual<T: FloatingPoint>(_ lhs: T,
_ rhs: T,
tolerance: T = T.ulpOfOne.squareRoot()) -> Bool {
assert(tolerance >= .ulpOfOne && tolerance < 1, "tolerance should be in [.ulpOfOne, 1).")
guard lhs.isFinite && rhs.isFinite else {
return rescaledAlmostEqual(lhs, rhs, tolerance: tolerance)
}
let scale = max(abs(lhs), abs(rhs), .leastNormalMagnitude)
return abs(lhs - rhs) < scale*tolerance
}

func rescaledAlmostEqual<T: FloatingPoint>(_ lhs: T,
_ rhs: T,
tolerance: T) -> Bool {
if lhs.isNaN || rhs.isNaN { return false }
if lhs.isInfinite {
if rhs.isInfinite { return lhs == rhs }
let scaledLhs = T(sign: lhs.sign,
exponent: T.greatestFiniteMagnitude.exponent,
significand: 1)
let scaledRhs = T(sign: .plus,
exponent: -1,
significand: rhs)
return isAlmostEqual(scaledLhs, scaledRhs, tolerance: tolerance)
}
return rescaledAlmostEqual(rhs, lhs, tolerance: tolerance)
}

//===----------------------------------------------------------------------===//
//
// Array almost equal.
//
//===----------------------------------------------------------------------===//

func elementsAlmostEqual<T: FloatingPoint>(_ lhs: [T], _ rhs: [T]) -> Bool {
var returnValue = true
zip(lhs, rhs).forEach {
if !isAlmostEqual($0.0, $0.1) {
returnValue = false
return
}
}
return returnValue
}

func isAlmostEqual<T: FloatingPoint>(_ lhs: T,
_ rhs: T,
tolerance: T = T.ulpOfOne.squareRoot()) -> Bool {
assert(tolerance >= .ulpOfOne && tolerance < 1, "tolerance should be in [.ulpOfOne, 1).")
guard lhs.isFinite && rhs.isFinite else {
return rescaledAlmostEqual(lhs, rhs, tolerance: tolerance)
}
let scale = max(abs(lhs), abs(rhs), .leastNormalMagnitude)
return abs(lhs - rhs) < scale*tolerance
}

func rescaledAlmostEqual<T: FloatingPoint>(_ lhs: T,
_ rhs: T,
tolerance: T) -> Bool {
if lhs.isNaN || rhs.isNaN { return false }
if lhs.isInfinite {
if rhs.isInfinite { return lhs == rhs }
let scaledLhs = T(sign: lhs.sign,
exponent: T.greatestFiniteMagnitude.exponent,
significand: 1)
let scaledRhs = T(sign: .plus,
exponent: -1,
significand: rhs)
return isAlmostEqual(scaledLhs, scaledRhs, tolerance: tolerance)
}
return rescaledAlmostEqual(rhs, lhs, tolerance: tolerance)
}

runAllTests()
Expand Down