diff --git a/Package.swift b/Package.swift index 3490cd7..41c9fd0 100644 --- a/Package.swift +++ b/Package.swift @@ -3,6 +3,12 @@ import PackageDescription import CompilerPluginSupport +#if canImport(simd) +let simdAvailable = true +#else +let simdAvailable = false +#endif + let package = Package( name: "simd-tools", platforms: [ @@ -20,7 +26,9 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-syntax.git", .upToNextMajor(from: "600.0.1")), - ], + ] + (simdAvailable ? [] : [ + .package(url: "https://github.com/keyvariable/kvSIMD.swift.git", from: "1.1.0"), + ]), targets: [ .macro( name: "SIMDToolsMacros", @@ -29,7 +37,12 @@ let package = Package( .product(name: "SwiftCompilerPlugin", package: "swift-syntax") ] ), - .target(name: "SIMDTools", dependencies: ["SIMDToolsMacros"]), + .target( + name: "SIMDTools", + dependencies: ["SIMDToolsMacros"] + (simdAvailable ? [] : [ + .product(name: "kvSIMD", package: "kvSIMD.swift"), + ]) + ), .testTarget( name: "SIMDToolsTests", dependencies: ["SIMDTools"] diff --git a/Sources/SIMDTools/Angle.swift b/Sources/SIMDTools/Angle.swift index c5baa01..dde29d0 100644 --- a/Sources/SIMDTools/Angle.swift +++ b/Sources/SIMDTools/Angle.swift @@ -1,4 +1,8 @@ import simd +#if !canImport(Darwin) +// If we don't have Apple-specific intrinsics, use Glibc instead +import Glibc +#endif /// A floating point value that represents an angle @@ -11,6 +15,11 @@ public struct Angle { public var radians: Float32 { degrees * Float32.pi / 180.0 } + + @inline(__always) + public var piFactor: Float { + degrees / 180.0 + } /// Creates an instance using the value in radians /// - Parameter radians: The angle value in radians @@ -208,7 +217,13 @@ extension Angle: Comparable { /// - sina: A reference to a variable to store the sine of the angle /// - cosa: A reference to a variable to store the cosine of the angle public func sincos(_ a: Angle, _ sina: inout Float, _ cosa: inout Float) { - __sincospif(a.degrees / 180.0, &sina, &cosa) + #if canImport(Darwin) + __sincospif(a.piFactor, &sina, &cosa) + #else + let r = a.radians + sina = sinf(r) + cosa = cosf(r) + #endif } /// Computes the sine and cosine of the given angle @@ -218,7 +233,6 @@ public func sincos(_ a: Angle) -> (sin: Float, cos: Float) { var s: Float = 0.0 var c: Float = 0.0 sincos(a, &s, &c) - return (sin: s, cos: c) } @@ -226,19 +240,31 @@ public func sincos(_ a: Angle) -> (sin: Float, cos: Float) { /// - Parameter a: The angle /// - Returns: The sine of the angle public func sin(_ a: Angle) -> Float { - __sinpif(a.degrees / 180.0) + #if canImport(Darwin) + return __sinpif(a.piFactor) + #else + return sinf(a.radians) + #endif } /// Computes the cosine of the given angle /// - Parameter a: The angle /// - Returns: The cosine of the angle public func cos(_ a: Angle) -> Float { - __cospif(a.degrees / 180.0) + #if canImport(Darwin) + return __cospif(a.piFactor) + #else + return cosf(a.radians) + #endif } /// Computes the tangent of the given angle /// - Parameter a: The angle /// - Returns: The tangent of the angle public func tan(_ a: Angle) -> Float { - __tanpif(a.degrees / 180.0) + #if canImport(Darwin) + return __tanpif(a.piFactor) + #else + return tanf(a.radians) + #endif } diff --git a/Sources/SIMDToolsMacros/SIMDToolsMacro.swift b/Sources/SIMDToolsMacros/SIMDToolsMacro.swift index 5b4552b..d65ca34 100644 --- a/Sources/SIMDToolsMacros/SIMDToolsMacro.swift +++ b/Sources/SIMDToolsMacros/SIMDToolsMacro.swift @@ -2,6 +2,7 @@ import SwiftCompilerPlugin import SwiftSyntax import SwiftSyntaxBuilder import SwiftSyntaxMacros +import Foundation enum MacroExpansionError: Error, CustomStringConvertible { case invalidArguments