From fad5027804908a17b20c06bca66f065eb0891020 Mon Sep 17 00:00:00 2001 From: Nevyn Bengtsson Date: Thu, 20 Nov 2025 12:48:34 +0100 Subject: [PATCH 1/4] Import Foundation for replaceOccurrences This method is in Foundation, not in stdlib, so needs to be explicitly imported to be able to build on Linux. --- Sources/SIMDToolsMacros/SIMDToolsMacro.swift | 1 + 1 file changed, 1 insertion(+) 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 From c0b5f7818efd8509c618c2b441d63d835972031f Mon Sep 17 00:00:00 2001 From: Nevyn Bengtsson Date: Thu, 20 Nov 2025 12:57:04 +0100 Subject: [PATCH 2/4] On Linux, don't use Apple-specific intrinsics in Angle they're apple specific extensions. Use glibc instead. --- Sources/SIMDTools/Angle.swift | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/Sources/SIMDTools/Angle.swift b/Sources/SIMDTools/Angle.swift index c5baa01..5d85346 100644 --- a/Sources/SIMDTools/Angle.swift +++ b/Sources/SIMDTools/Angle.swift @@ -1,4 +1,7 @@ import simd +#if canImport(Glibc) +import Glibc +#endif /// A floating point value that represents an angle @@ -11,6 +14,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 +216,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 +232,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 +239,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 } From 70cf651e17764ba69af96233a2181fe57831feec Mon Sep 17 00:00:00 2001 From: Nevyn Bengtsson Date: Tue, 25 Nov 2025 12:10:15 +0100 Subject: [PATCH 3/4] Avoid importing Glibc unless needed --- Sources/SIMDTools/Angle.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/SIMDTools/Angle.swift b/Sources/SIMDTools/Angle.swift index 5d85346..dde29d0 100644 --- a/Sources/SIMDTools/Angle.swift +++ b/Sources/SIMDTools/Angle.swift @@ -1,5 +1,6 @@ import simd -#if canImport(Glibc) +#if !canImport(Darwin) +// If we don't have Apple-specific intrinsics, use Glibc instead import Glibc #endif From a681c41496fa7d280d7cbef55ce732561c32cd68 Mon Sep 17 00:00:00 2001 From: Nevyn Bengtsson Date: Fri, 5 Dec 2025 14:29:37 +0100 Subject: [PATCH 4/4] Depend on kvSIMD on Linux I was able to build this before because Swift 6.1 would just pick up kvSIMD from higher up in the dependency chain, which was wrong. In Swift 6.2, we need to explicitly depend on kvSIMD to have a chance to build. --- Package.swift | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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"]