mirror of
https://github.com/apple/swift-numerics
synced 2026-11-26 04:57:53 +00:00
Move imaginary helper functions into a distinct file
This commit is contained in:
1 parent
211b94fb16
commit
e9dfc766be
3 files changed
+81
-39
No files matched your search
@@ -0,0 +1,77 @@
|
||||
//===--- ImaginaryHelper.swift --------------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2019-2021 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// Provides common vector operations on SIMD3 to ease the use of the quaternions
|
||||
// imaginary/vector components internally to the module, and in tests.
|
||||
extension SIMD3 where Scalar: FloatingPoint {
|
||||
/// Returns a vector with infinity in all lanes
|
||||
@usableFromInline @inline(__always)
|
||||
internal static var infinity: Self {
|
||||
SIMD3(repeating: .infinity)
|
||||
}
|
||||
|
||||
/// Returns a vector with nan in all lanes
|
||||
@usableFromInline @inline(__always)
|
||||
internal static var nan: Self {
|
||||
SIMD3(repeating: .nan)
|
||||
}
|
||||
|
||||
/// True if all values of this instance are finite
|
||||
@usableFromInline @inline(__always)
|
||||
internal var isFinite: Bool {
|
||||
x.isFinite && y.isFinite && z.isFinite
|
||||
}
|
||||
|
||||
/// The ∞-norm of the value (`max(abs(x), abs(y), abs(z))`).
|
||||
@usableFromInline @inline(__always)
|
||||
internal var magnitude: Scalar {
|
||||
max()
|
||||
}
|
||||
|
||||
/// The Euclidean norm (a.k.a. 2-norm, `sqrt(x*x + y*y + z*z)`).
|
||||
@usableFromInline @inline(__always)
|
||||
internal var length: Scalar {
|
||||
let naive = lengthSquared
|
||||
guard naive.isNormal else { return carefulLength }
|
||||
return naive.squareRoot()
|
||||
}
|
||||
|
||||
// Implementation detail of `length`, moving slow path off of the
|
||||
// inline function.
|
||||
@usableFromInline
|
||||
internal var carefulLength: Scalar {
|
||||
guard isFinite else { return .infinity }
|
||||
guard !magnitude.isZero else { return .zero }
|
||||
// Unscale the vector, calculate its length and rescale the result
|
||||
return (self / magnitude).length * magnitude
|
||||
}
|
||||
|
||||
/// Returns the squared length of this instance.
|
||||
@usableFromInline @inline(__always)
|
||||
internal var lengthSquared: Scalar {
|
||||
dot(self)
|
||||
}
|
||||
|
||||
/// Returns the scalar/dot product of this vector with `other`.
|
||||
@usableFromInline @inline(__always)
|
||||
internal func dot(_ other: SIMD3<Scalar>) -> Scalar {
|
||||
(self * other).sum()
|
||||
}
|
||||
|
||||
/// Returns the vector/cross product of this vector with `other`.
|
||||
@usableFromInline @inline(__always)
|
||||
internal func cross(_ other: SIMD3<Scalar>) -> SIMD3<Scalar> {
|
||||
let yzx = SIMD3<Int>(1,2,0)
|
||||
let zxy = SIMD3<Int>(2,0,1)
|
||||
return (self[yzx] * other[zxy]) - (self[zxy] * other[yzx])
|
||||
}
|
||||
}
|
||||
@@ -381,7 +381,7 @@ extension Quaternion {
|
||||
/// [wiki]: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Using_quaternion_as_rotations
|
||||
@inlinable
|
||||
public func act(on vector: SIMD3<RealType>) -> SIMD3<RealType> {
|
||||
guard vector.isFinite else { return SIMD3(repeating: .infinity) }
|
||||
guard vector.isFinite else { return .infinity }
|
||||
guard vector != .zero else { return .zero }
|
||||
|
||||
// The following expression have been split up so the type-checker
|
||||
@@ -452,36 +452,3 @@ extension Quaternion {
|
||||
self.init(real: .cos(halfAngle), imaginary: unitAxis * .sin(halfAngle))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SIMD Helper
|
||||
//
|
||||
// Provides common vector operations on SIMD3 to ease the use of "imaginary"
|
||||
// and *(x,y,z)* axis representations internally to the module.
|
||||
extension SIMD3 where Scalar: FloatingPoint {
|
||||
|
||||
/// True if all values of this instance are finite
|
||||
@usableFromInline @inline(__always)
|
||||
internal var isFinite: Bool {
|
||||
x.isFinite && y.isFinite && z.isFinite
|
||||
}
|
||||
|
||||
/// Returns the squared length of this instance.
|
||||
@usableFromInline @inline(__always)
|
||||
internal var lengthSquared: Scalar {
|
||||
dot(self)
|
||||
}
|
||||
|
||||
/// Returns the scalar/dot product of this vector with `other`.
|
||||
@usableFromInline @inline(__always)
|
||||
internal func dot(_ other: SIMD3<Scalar>) -> Scalar {
|
||||
(self * other).sum()
|
||||
}
|
||||
|
||||
/// Returns the vector/cross product of this vector with `other`.
|
||||
@usableFromInline @inline(__always)
|
||||
internal func cross(_ other: SIMD3<Scalar>) -> SIMD3<Scalar> {
|
||||
let yzx = SIMD3<Int>(1,2,0)
|
||||
let zxy = SIMD3<Int>(2,0,1)
|
||||
return (self[yzx] * other[zxy]) - (self[zxy] * other[yzx])
|
||||
}
|
||||
}
|
||||
@@ -306,9 +306,9 @@ final class TransformationTests: XCTestCase {
|
||||
let q = Quaternion(angle: .pi, axis: SIMD3(1,0,0))
|
||||
XCTAssertEqual(q.act(on: .zero), .zero)
|
||||
XCTAssertEqual(q.act(on: -.zero), .zero)
|
||||
XCTAssertEqual(q.act(on: .nan ), SIMD3(repeating: .infinity))
|
||||
XCTAssertEqual(q.act(on: .infinity), SIMD3(repeating: .infinity))
|
||||
XCTAssertEqual(q.act(on: -.infinity), SIMD3(repeating: .infinity))
|
||||
XCTAssertEqual(q.act(on: .nan ), .infinity)
|
||||
XCTAssertEqual(q.act(on: .infinity), .infinity)
|
||||
XCTAssertEqual(q.act(on: -.infinity), .infinity)
|
||||
}
|
||||
|
||||
func testActOnVectorEdgeCase() {
|
||||
@@ -369,8 +369,6 @@ final class TransformationTests: XCTestCase {
|
||||
|
||||
// MARK: - Helper
|
||||
extension SIMD3 where Scalar: FloatingPoint {
|
||||
fileprivate static var infinity: Self { SIMD3(.infinity,0,0) }
|
||||
fileprivate static var nan: Self { SIMD3(.nan,0,0) }
|
||||
fileprivate var isNaN: Bool { x.isNaN && y.isNaN && z.isNaN }
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user