Skip to content

Commit 2ad5fa6

Browse files
authored
fix linux build
1 parent 338957b commit 2ad5fa6

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

Package.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import PackageDescription
44
import CompilerPluginSupport
55

6+
#if canImport(simd)
7+
let simdAvailable = true
8+
#else
9+
let simdAvailable = false
10+
#endif
11+
612
let package = Package(
713
name: "simd-tools",
814
platforms: [
@@ -20,7 +26,9 @@ let package = Package(
2026
],
2127
dependencies: [
2228
.package(url: "https://github.com/apple/swift-syntax.git", .upToNextMajor(from: "600.0.1")),
23-
],
29+
] + (simdAvailable ? [] : [
30+
.package(url: "https://github.com/keyvariable/kvSIMD.swift.git", from: "1.1.0"),
31+
]),
2432
targets: [
2533
.macro(
2634
name: "SIMDToolsMacros",
@@ -29,7 +37,12 @@ let package = Package(
2937
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
3038
]
3139
),
32-
.target(name: "SIMDTools", dependencies: ["SIMDToolsMacros"]),
40+
.target(
41+
name: "SIMDTools",
42+
dependencies: ["SIMDToolsMacros"] + (simdAvailable ? [] : [
43+
.product(name: "kvSIMD", package: "kvSIMD.swift"),
44+
])
45+
),
3346
.testTarget(
3447
name: "SIMDToolsTests",
3548
dependencies: ["SIMDTools"]

Sources/SIMDTools/Angle.swift

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import simd
2+
#if !canImport(Darwin)
3+
// If we don't have Apple-specific intrinsics, use Glibc instead
4+
import Glibc
5+
#endif
26

37
/// A floating point value that represents an angle
48

@@ -11,6 +15,11 @@ public struct Angle {
1115
public var radians: Float32 {
1216
degrees * Float32.pi / 180.0
1317
}
18+
19+
@inline(__always)
20+
public var piFactor: Float {
21+
degrees / 180.0
22+
}
1423

1524
/// Creates an instance using the value in radians
1625
/// - Parameter radians: The angle value in radians
@@ -208,7 +217,13 @@ extension Angle: Comparable {
208217
/// - sina: A reference to a variable to store the sine of the angle
209218
/// - cosa: A reference to a variable to store the cosine of the angle
210219
public func sincos(_ a: Angle, _ sina: inout Float, _ cosa: inout Float) {
211-
__sincospif(a.degrees / 180.0, &sina, &cosa)
220+
#if canImport(Darwin)
221+
__sincospif(a.piFactor, &sina, &cosa)
222+
#else
223+
let r = a.radians
224+
sina = sinf(r)
225+
cosa = cosf(r)
226+
#endif
212227
}
213228

214229
/// Computes the sine and cosine of the given angle
@@ -218,27 +233,38 @@ public func sincos(_ a: Angle) -> (sin: Float, cos: Float) {
218233
var s: Float = 0.0
219234
var c: Float = 0.0
220235
sincos(a, &s, &c)
221-
222236
return (sin: s, cos: c)
223237
}
224238

225239
/// Computes the sine of the given angle
226240
/// - Parameter a: The angle
227241
/// - Returns: The sine of the angle
228242
public func sin(_ a: Angle) -> Float {
229-
__sinpif(a.degrees / 180.0)
243+
#if canImport(Darwin)
244+
return __sinpif(a.piFactor)
245+
#else
246+
return sinf(a.radians)
247+
#endif
230248
}
231249

232250
/// Computes the cosine of the given angle
233251
/// - Parameter a: The angle
234252
/// - Returns: The cosine of the angle
235253
public func cos(_ a: Angle) -> Float {
236-
__cospif(a.degrees / 180.0)
254+
#if canImport(Darwin)
255+
return __cospif(a.piFactor)
256+
#else
257+
return cosf(a.radians)
258+
#endif
237259
}
238260

239261
/// Computes the tangent of the given angle
240262
/// - Parameter a: The angle
241263
/// - Returns: The tangent of the angle
242264
public func tan(_ a: Angle) -> Float {
243-
__tanpif(a.degrees / 180.0)
265+
#if canImport(Darwin)
266+
return __tanpif(a.piFactor)
267+
#else
268+
return tanf(a.radians)
269+
#endif
244270
}

Sources/SIMDToolsMacros/SIMDToolsMacro.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import SwiftCompilerPlugin
22
import SwiftSyntax
33
import SwiftSyntaxBuilder
44
import SwiftSyntaxMacros
5+
import Foundation
56

67
enum MacroExpansionError: Error, CustomStringConvertible {
78
case invalidArguments

0 commit comments

Comments
 (0)