|
| 1 | +//===--- ImaginaryHelper.swift --------------------------------*- swift -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2019-2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// Provides common vector operations on SIMD3 to ease the use of the quaternions |
| 14 | +// imaginary/vector components internally to the module, and in tests. |
| 15 | +extension SIMD3 where Scalar: FloatingPoint { |
| 16 | + /// Returns a vector with infinity in all lanes |
| 17 | + @usableFromInline @inline(__always) |
| 18 | + internal static var infinity: Self { |
| 19 | + SIMD3(repeating: .infinity) |
| 20 | + } |
| 21 | + |
| 22 | + /// Returns a vector with nan in all lanes |
| 23 | + @usableFromInline @inline(__always) |
| 24 | + internal static var nan: Self { |
| 25 | + SIMD3(repeating: .nan) |
| 26 | + } |
| 27 | + |
| 28 | + /// True if all values of this instance are finite |
| 29 | + @usableFromInline @inline(__always) |
| 30 | + internal var isFinite: Bool { |
| 31 | + x.isFinite && y.isFinite && z.isFinite |
| 32 | + } |
| 33 | + |
| 34 | + /// The ∞-norm of the value (`max(abs(x), abs(y), abs(z))`). |
| 35 | + @usableFromInline @inline(__always) |
| 36 | + internal var magnitude: Scalar { |
| 37 | + max() |
| 38 | + } |
| 39 | + |
| 40 | + /// The Euclidean norm (a.k.a. 2-norm, `sqrt(x*x + y*y + z*z)`). |
| 41 | + @usableFromInline @inline(__always) |
| 42 | + internal var length: Scalar { |
| 43 | + let naive = lengthSquared |
| 44 | + guard naive.isNormal else { return carefulLength } |
| 45 | + return naive.squareRoot() |
| 46 | + } |
| 47 | + |
| 48 | + // Implementation detail of `length`, moving slow path off of the |
| 49 | + // inline function. |
| 50 | + @usableFromInline |
| 51 | + internal var carefulLength: Scalar { |
| 52 | + guard isFinite else { return .infinity } |
| 53 | + guard !magnitude.isZero else { return .zero } |
| 54 | + // Unscale the vector, calculate its length and rescale the result |
| 55 | + return (self / magnitude).length * magnitude |
| 56 | + } |
| 57 | + |
| 58 | + /// Returns the squared length of this instance. |
| 59 | + @usableFromInline @inline(__always) |
| 60 | + internal var lengthSquared: Scalar { |
| 61 | + dot(self) |
| 62 | + } |
| 63 | + |
| 64 | + /// Returns the scalar/dot product of this vector with `other`. |
| 65 | + @usableFromInline @inline(__always) |
| 66 | + internal func dot(_ other: SIMD3<Scalar>) -> Scalar { |
| 67 | + (self * other).sum() |
| 68 | + } |
| 69 | + |
| 70 | + /// Returns the vector/cross product of this vector with `other`. |
| 71 | + @usableFromInline @inline(__always) |
| 72 | + internal func cross(_ other: SIMD3<Scalar>) -> SIMD3<Scalar> { |
| 73 | + let yzx = SIMD3<Int>(1,2,0) |
| 74 | + let zxy = SIMD3<Int>(2,0,1) |
| 75 | + return (self[yzx] * other[zxy]) - (self[zxy] * other[yzx]) |
| 76 | + } |
| 77 | +} |
0 commit comments