Skip to content
Open
Show file tree
Hide file tree
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
17 changes: 15 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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",
Expand All @@ -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"]
Expand Down
36 changes: 31 additions & 5 deletions Sources/SIMDTools/Angle.swift
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -218,27 +233,38 @@ 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)
}

/// Computes the sine of the given angle
/// - 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
}
1 change: 1 addition & 0 deletions Sources/SIMDToolsMacros/SIMDToolsMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import Foundation

enum MacroExpansionError: Error, CustomStringConvertible {
case invalidArguments
Expand Down