From 65aa6b5045d8dd59bc8512b87ab4ec25a18dd82d Mon Sep 17 00:00:00 2001 From: pcbeard Date: Wed, 22 Sep 2021 00:27:52 -0700 Subject: [PATCH] Use Foundation versions of math functions (#6) * Use Foundation versions of math functions If neither Darwin, nor Glibc are available, use Foundation to import math functions. Use dynamic library target on Windows since ar tool doesn't seem to exist. * Fix test_asinh() by adding accuracy parameter * Reduce accuracy for testQuatFromEulerAngles_0_(n)90_0 * Remove WASM CI * Use Swift 5.5 in on Windows CI * Update Windows CI * Update ci-windows.yml * Update CI trigger Co-authored-by: Christian Treffs --- .github/workflows/ci-linux.yml | 1 + .github/workflows/ci-macos.yml | 1 + .github/workflows/ci-wasm.yml | 17 ------ .github/workflows/ci-windows.yml | 61 +++++++++++-------- .github/workflows/markdown-link-check.yml | 7 ++- Package.swift | 12 +++- README.md | 1 - Sources/FirebladeMath/Functions/abs.swift | 8 +++ Sources/FirebladeMath/Functions/acos.swift | 12 ++++ Sources/FirebladeMath/Functions/acosh.swift | 12 ++++ Sources/FirebladeMath/Functions/asin.swift | 12 ++++ Sources/FirebladeMath/Functions/asinh.swift | 13 +++- Sources/FirebladeMath/Functions/atan.swift | 12 ++++ Sources/FirebladeMath/Functions/atan2.swift | 12 ++++ Sources/FirebladeMath/Functions/atanh.swift | 12 ++++ Sources/FirebladeMath/Functions/ceil.swift | 12 ++++ .../FirebladeMath/Functions/copysign.swift | 12 ++++ Sources/FirebladeMath/Functions/cos.swift | 12 ++++ Sources/FirebladeMath/Functions/cosh.swift | 12 ++++ Sources/FirebladeMath/Functions/exp.swift | 12 ++++ Sources/FirebladeMath/Functions/exp2.swift | 12 ++++ Sources/FirebladeMath/Functions/floor.swift | 12 ++++ Sources/FirebladeMath/Functions/hypot.swift | 12 ++++ Sources/FirebladeMath/Functions/log.swift | 12 ++++ Sources/FirebladeMath/Functions/log10.swift | 12 ++++ Sources/FirebladeMath/Functions/log2.swift | 12 ++++ Sources/FirebladeMath/Functions/max.swift | 8 +++ Sources/FirebladeMath/Functions/min.swift | 12 ++++ Sources/FirebladeMath/Functions/mod.swift | 12 ++++ Sources/FirebladeMath/Functions/pow.swift | 12 ++++ Sources/FirebladeMath/Functions/pow2.swift | 12 ++++ Sources/FirebladeMath/Functions/sin.swift | 12 ++++ Sources/FirebladeMath/Functions/sinh.swift | 12 ++++ Sources/FirebladeMath/Functions/sqrt.swift | 12 ++++ Sources/FirebladeMath/Functions/tan.swift | 12 ++++ Sources/FirebladeMath/Functions/tanh.swift | 12 ++++ Tests/FirebladeMathTests/FunctionTests.swift | 2 +- .../Quat4f+EulerTests.swift | 8 +-- 38 files changed, 397 insertions(+), 54 deletions(-) delete mode 100644 .github/workflows/ci-wasm.yml diff --git a/.github/workflows/ci-linux.yml b/.github/workflows/ci-linux.yml index 097bcc3..c01b339 100644 --- a/.github/workflows/ci-linux.yml +++ b/.github/workflows/ci-linux.yml @@ -5,6 +5,7 @@ on: branches: [ master ] pull_request: branches: [ master ] + workflow_dispatch: jobs: linux-test-build-release: diff --git a/.github/workflows/ci-macos.yml b/.github/workflows/ci-macos.yml index aac12d8..c2cfaae 100644 --- a/.github/workflows/ci-macos.yml +++ b/.github/workflows/ci-macos.yml @@ -5,6 +5,7 @@ on: branches: [ master ] pull_request: branches: [ master ] + workflow_dispatch: jobs: macos-test-build-release-xcode: diff --git a/.github/workflows/ci-wasm.yml b/.github/workflows/ci-wasm.yml deleted file mode 100644 index babbaba..0000000 --- a/.github/workflows/ci-wasm.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: WASM - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - wasm-build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - name: Build - uses: swiftwasm/swiftwasm-action@main - with: - shell-action: swift build --triple wasm32-unknown-wasi diff --git a/.github/workflows/ci-windows.yml b/.github/workflows/ci-windows.yml index 3b75fcd..9d89653 100644 --- a/.github/workflows/ci-windows.yml +++ b/.github/workflows/ci-windows.yml @@ -5,34 +5,43 @@ on: branches: [ master ] pull_request: branches: [ master ] + workflow_dispatch: jobs: windows-test-build-release: runs-on: windows-latest - env: - SNAPSHOT: "https://swift.org/builds/swift-5.4.1-release/windows10/swift-5.4.1-RELEASE/swift-5.4.1-RELEASE-windows10.exe" + strategy: + matrix: + include: + - tag: 5.5-RELEASE + branch: swift-5.5-release steps: - - uses: actions/checkout@master - - uses: seanmiddleditch/gha-setup-vsdevenv@master - - - name: Install Swift - run: | - Install-Binary -Url "${env:SNAPSHOT}" -Name "installer.exe" -ArgumentList ("-q") - - name: Set Environment Variables - run: | - echo "SDKROOT=C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append - echo "DEVELOPER_DIR=C:\Library\Developer" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append - - name: Adjust Paths - run: | - echo "C:\Library\Swift-development\bin;C:\Library\icu-67\usr\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append - echo "C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append - - name: Install Supporting Files - run: | - Copy-Item "$env:SDKROOT\usr\share\ucrt.modulemap" -destination "$env:UniversalCRTSdkDir\Include\$env:UCRTVersion\ucrt\module.modulemap" - Copy-Item "$env:SDKROOT\usr\share\visualc.modulemap" -destination "$env:VCToolsInstallDir\include\module.modulemap" - Copy-Item "$env:SDKROOT\usr\share\visualc.apinotes" -destination "$env:VCToolsInstallDir\include\visualc.apinotes" - Copy-Item "$env:SDKROOT\usr\share\winsdk.modulemap" -destination "$env:UniversalCRTSdkDir\Include\$env:UCRTVersion\um\module.modulemap" - - name: Test - run: swift test -v --skip-update --parallel --enable-test-discovery - - name: Build Release - run: swift build -c release \ No newline at end of file + - uses: actions/checkout@master + with: + fetch-depth: 1 + + - uses: seanmiddleditch/gha-setup-vsdevenv@master + + - name: Install Swift ${{ matrix.tag }} + run: | + Install-Binary -Url "https://swift.org/builds/${{ matrix.branch }}/windows10/swift-${{ matrix.tag }}/swift-${{ matrix.tag }}-windows10.exe" -Name "installer.exe" -ArgumentList ("-q") + - name: Set Environment Variables + run: | + echo "SDKROOT=C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append + echo "DEVELOPER_DIR=C:\Library\Developer" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append + - name: Adjust Paths + run: | + echo "C:\Library\Swift-development\bin;C:\Library\icu-67\usr\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append + echo "C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append + - name: Install Supporting Files + run: | + Copy-Item "$env:SDKROOT\usr\share\ucrt.modulemap" -destination "$env:UniversalCRTSdkDir\Include\$env:UCRTVersion\ucrt\module.modulemap" + Copy-Item "$env:SDKROOT\usr\share\visualc.modulemap" -destination "$env:VCToolsInstallDir\include\module.modulemap" + Copy-Item "$env:SDKROOT\usr\share\visualc.apinotes" -destination "$env:VCToolsInstallDir\include\visualc.apinotes" + Copy-Item "$env:SDKROOT\usr\share\winsdk.modulemap" -destination "$env:UniversalCRTSdkDir\Include\$env:UCRTVersion\um\module.modulemap" + + - name: Test + run: swift test -v --skip-update --parallel --enable-test-discovery + + - name: Build Release + run: swift build -c release diff --git a/.github/workflows/markdown-link-check.yml b/.github/workflows/markdown-link-check.yml index 52e8f7e..21f9351 100644 --- a/.github/workflows/markdown-link-check.yml +++ b/.github/workflows/markdown-link-check.yml @@ -1,6 +1,11 @@ name: Check markdown links -on: push +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + workflow_dispatch: jobs: markdown-link-check: diff --git a/Package.swift b/Package.swift index 94bfe9f..7638b36 100644 --- a/Package.swift +++ b/Package.swift @@ -13,10 +13,16 @@ swiftSettings.append(.define("FRB_MATH_USE_SIMD")) #if canImport(Darwin) swiftSettings.append(.define("FRB_MATH_DARWIN")) +#elseif canImport(Glibc) +swiftSettings.append(.define("FRB_MATH_GLIBC")) +#elseif canImport(Foundation) +swiftSettings.append(.define("FRB_MATH_FOUNDATION")) #endif -#if canImport(Glibc) -swiftSettings.append(.define("FRB_MATH_GLIBC")) +#if os(Windows) +let libraryType : Product.Library.LibraryType = .dynamic +#else +let libraryType : Product.Library.LibraryType = .static #endif let package = Package( @@ -24,7 +30,7 @@ let package = Package( products: [ .library( name: "FirebladeMath", - type: .static, + type: libraryType, targets: ["FirebladeMath"]) ], targets: [ diff --git a/README.md b/README.md index 65384b2..b9e3db8 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ [![macOS](https://github.com/fireblade-engine/math/actions/workflows/ci-macos.yml/badge.svg)](https://github.com/fireblade-engine/math/actions/workflows/ci-macos.yml) [![Linux](https://github.com/fireblade-engine/math/actions/workflows/ci-linux.yml/badge.svg)](https://github.com/fireblade-engine/math/actions/workflows/ci-linux.yml) [![Windows](https://github.com/fireblade-engine/math/actions/workflows/ci-windows.yml/badge.svg)](https://github.com/fireblade-engine/math/actions/workflows/ci-windows.yml) -[![WASM](https://github.com/fireblade-engine/math/actions/workflows/ci-wasm.yml/badge.svg)](https://github.com/fireblade-engine/math/actions/workflows/ci-wasm.yml) [![swift-version-compatibility](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Ffireblade-engine%2Fmath%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/fireblade-engine/math) [![platform-compatilibilty](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Ffireblade-engine%2Fmath%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/fireblade-engine/math) diff --git a/Sources/FirebladeMath/Functions/abs.swift b/Sources/FirebladeMath/Functions/abs.swift index 1caa31b..28a961f 100644 --- a/Sources/FirebladeMath/Functions/abs.swift +++ b/Sources/FirebladeMath/Functions/abs.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -26,4 +30,8 @@ public func abs(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.fabs(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.fabs(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/acos.swift b/Sources/FirebladeMath/Functions/acos.swift index 43e5765..0aa99c1 100644 --- a/Sources/FirebladeMath/Functions/acos.swift +++ b/Sources/FirebladeMath/Functions/acos.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func acos(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.acosf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.acosf(float) + #endif } /// Computes the principal value of the arc cosine of arg. @@ -36,4 +44,8 @@ public func acos(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.acos(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.acos(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/acosh.swift b/Sources/FirebladeMath/Functions/acosh.swift index 7bd12c4..9ac217a 100644 --- a/Sources/FirebladeMath/Functions/acosh.swift +++ b/Sources/FirebladeMath/Functions/acosh.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func acosh(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.acosh(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.acosh(double) + #endif } /// Computes the inverse hyperbolic cosine of arg. @@ -36,4 +44,8 @@ public func acosh(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.acoshf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.acoshf(float) + #endif } diff --git a/Sources/FirebladeMath/Functions/asin.swift b/Sources/FirebladeMath/Functions/asin.swift index a1ed9d7..e22e199 100644 --- a/Sources/FirebladeMath/Functions/asin.swift +++ b/Sources/FirebladeMath/Functions/asin.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func asin(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.asin(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.asin(double) + #endif } /// Computes the principal values of the arc sine of arg. @@ -36,4 +44,8 @@ public func asin(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.asinf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.asinf(float) + #endif } diff --git a/Sources/FirebladeMath/Functions/asinh.swift b/Sources/FirebladeMath/Functions/asinh.swift index 90e63e8..3b5b721 100644 --- a/Sources/FirebladeMath/Functions/asinh.swift +++ b/Sources/FirebladeMath/Functions/asinh.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func asinh(_ double: Double) -> Double { return Glibc.asinh(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.asinh(double) + #endif } /// Computes the inverse hyperbolic sine of arg. @@ -33,7 +41,10 @@ public func asinh(_ float: Float) -> Float { #endif #if FRB_MATH_GLIBC - return Glibc.asinhf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.asinhf(float) + #endif } diff --git a/Sources/FirebladeMath/Functions/atan.swift b/Sources/FirebladeMath/Functions/atan.swift index 4b313db..361cb59 100644 --- a/Sources/FirebladeMath/Functions/atan.swift +++ b/Sources/FirebladeMath/Functions/atan.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -19,6 +23,10 @@ public func atan(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.atan(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.atan(double) + #endif } /// Computes the principal value of the arc tangent of arg. @@ -34,4 +42,8 @@ public func atan(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.atanf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.atanf(float) + #endif } diff --git a/Sources/FirebladeMath/Functions/atan2.swift b/Sources/FirebladeMath/Functions/atan2.swift index 3b58744..43e5c22 100644 --- a/Sources/FirebladeMath/Functions/atan2.swift +++ b/Sources/FirebladeMath/Functions/atan2.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -18,6 +22,10 @@ public func atan2(_ y: Double, _ x: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.atan2(y, x) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.atan2(y, x) + #endif } /// The atan2() function computes the principal value of the arc tangent of y/x, @@ -32,4 +40,8 @@ public func atan2(_ y: Float, _ x: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.atan2f(y, x) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.atan2f(y, x) + #endif } diff --git a/Sources/FirebladeMath/Functions/atanh.swift b/Sources/FirebladeMath/Functions/atanh.swift index 606c16e..afbd444 100644 --- a/Sources/FirebladeMath/Functions/atanh.swift +++ b/Sources/FirebladeMath/Functions/atanh.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -21,6 +25,10 @@ public func atanh(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.atanhf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.atanhf(float) + #endif } /// Computes the inverse hyperbolic tangent of arg. @@ -38,4 +46,8 @@ public func atanh(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.atanh(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.atanh(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/ceil.swift b/Sources/FirebladeMath/Functions/ceil.swift index 5f14c94..1358ec5 100644 --- a/Sources/FirebladeMath/Functions/ceil.swift +++ b/Sources/FirebladeMath/Functions/ceil.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -18,6 +22,10 @@ public func ceil(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.ceilf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.ceilf(float) + #endif } /// Computes the smallest integer value not less than arg. @@ -32,4 +40,8 @@ public func ceil(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.ceil(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.ceil(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/copysign.swift b/Sources/FirebladeMath/Functions/copysign.swift index 9d62a5b..dc48de6 100644 --- a/Sources/FirebladeMath/Functions/copysign.swift +++ b/Sources/FirebladeMath/Functions/copysign.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -23,6 +27,10 @@ public func copysign(_ x: Double, _ y: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.copysign(x, y) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.copysign(x, y) + #endif } /// Composes a floating point value with the magnitude of x and the sign of y. @@ -42,4 +50,8 @@ public func copysign(_ x: Float, _ y: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.copysignf(x, y) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.copysignf(x, y) + #endif } diff --git a/Sources/FirebladeMath/Functions/cos.swift b/Sources/FirebladeMath/Functions/cos.swift index 5f82039..c8a46fb 100644 --- a/Sources/FirebladeMath/Functions/cos.swift +++ b/Sources/FirebladeMath/Functions/cos.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func cos(_ angleRad: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.cosf(angleRad) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.cosf(angleRad) + #endif } /// Computes the cosine of arg (measured in radians). @@ -36,4 +44,8 @@ public func cos(_ angleRad: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.cos(angleRad) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.cos(angleRad) + #endif } diff --git a/Sources/FirebladeMath/Functions/cosh.swift b/Sources/FirebladeMath/Functions/cosh.swift index 4521153..191b198 100644 --- a/Sources/FirebladeMath/Functions/cosh.swift +++ b/Sources/FirebladeMath/Functions/cosh.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -19,6 +23,10 @@ public func cosh(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.coshf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.coshf(float) + #endif } /// Computes the hyperbolic cosine of arg. @@ -34,4 +42,8 @@ public func cosh(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.cosh(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.cosh(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/exp.swift b/Sources/FirebladeMath/Functions/exp.swift index aa62631..cfd2150 100644 --- a/Sources/FirebladeMath/Functions/exp.swift +++ b/Sources/FirebladeMath/Functions/exp.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func exp(_ power: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.expf(power) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.expf(power) + #endif } /// Computes the e (Euler's number, 2.7182818) raised to the given power arg. @@ -36,4 +44,8 @@ public func exp(_ power: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.exp(power) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.exp(power) + #endif } diff --git a/Sources/FirebladeMath/Functions/exp2.swift b/Sources/FirebladeMath/Functions/exp2.swift index 9324ab4..f88f253 100644 --- a/Sources/FirebladeMath/Functions/exp2.swift +++ b/Sources/FirebladeMath/Functions/exp2.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func exp2(_ power: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.exp2f(power) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.exp2f(power) + #endif } /// Computes 2 raised to the given power n. @@ -36,4 +44,8 @@ public func exp2(_ power: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.exp2(power) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.exp2(power) + #endif } diff --git a/Sources/FirebladeMath/Functions/floor.swift b/Sources/FirebladeMath/Functions/floor.swift index 51b7efe..10c89bd 100644 --- a/Sources/FirebladeMath/Functions/floor.swift +++ b/Sources/FirebladeMath/Functions/floor.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -18,6 +22,10 @@ public func floor(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.floorf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.floorf(float) + #endif } /// Computes the largest integer value not greater than arg. @@ -32,4 +40,8 @@ public func floor(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.floor(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.floor(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/hypot.swift b/Sources/FirebladeMath/Functions/hypot.swift index effcf19..437d7cc 100644 --- a/Sources/FirebladeMath/Functions/hypot.swift +++ b/Sources/FirebladeMath/Functions/hypot.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -17,6 +21,10 @@ public func hypot(_ x: Float, _ y: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.hypotf(x, y) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.hypotf(x, y) + #endif } /// Returns the hypotenuse of a right-angled triangle whose legs are x and y. @@ -30,4 +38,8 @@ public func hypot(_ x: Double, _ y: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.hypot(x, y) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.hypot(x, y) + #endif } diff --git a/Sources/FirebladeMath/Functions/log.swift b/Sources/FirebladeMath/Functions/log.swift index 3626b8f..98f2fd4 100644 --- a/Sources/FirebladeMath/Functions/log.swift +++ b/Sources/FirebladeMath/Functions/log.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func log(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.logf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.logf(float) + #endif } /// Computes the natural (base e) logarithm of arg. @@ -36,4 +44,8 @@ public func log(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.log(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.log(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/log10.swift b/Sources/FirebladeMath/Functions/log10.swift index ea9d325..c35ef82 100644 --- a/Sources/FirebladeMath/Functions/log10.swift +++ b/Sources/FirebladeMath/Functions/log10.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func log10(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.log10f(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.log10f(float) + #endif } /// Computes the common (base-10) logarithm of arg. @@ -36,4 +44,8 @@ public func log10(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.log10(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.log10(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/log2.swift b/Sources/FirebladeMath/Functions/log2.swift index 87ab025..e43be3f 100644 --- a/Sources/FirebladeMath/Functions/log2.swift +++ b/Sources/FirebladeMath/Functions/log2.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func log2(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.log2f(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.log2f(float) + #endif } /// Computes the base 2 logarithm of arg. @@ -36,4 +44,8 @@ public func log2(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.log2(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.log2(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/max.swift b/Sources/FirebladeMath/Functions/max.swift index e2432bc..dfe45a7 100644 --- a/Sources/FirebladeMath/Functions/max.swift +++ b/Sources/FirebladeMath/Functions/max.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func max(_ x: Float, _ y: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.fmaxf(x, y) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.fmaxf(x, y) + #endif } /// Returns the larger of two floating point arguments, treating NaNs as missing data (between a NaN and a numeric value, the numeric value is chosen). diff --git a/Sources/FirebladeMath/Functions/min.swift b/Sources/FirebladeMath/Functions/min.swift index 9ac45a7..dbb55e8 100644 --- a/Sources/FirebladeMath/Functions/min.swift +++ b/Sources/FirebladeMath/Functions/min.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func min(_ x: Float, _ y: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.fminf(x, y) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.fminf(x, y) + #endif } /// Returns the smaller of two floating point arguments, treating NaNs as missing data (between a NaN and a numeric value, the numeric value is chosen). @@ -36,4 +44,8 @@ public func min(_ x: Double, _ y: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.fmin(x, y) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.fmin(x, y) + #endif } diff --git a/Sources/FirebladeMath/Functions/mod.swift b/Sources/FirebladeMath/Functions/mod.swift index 04582a8..42f82bd 100644 --- a/Sources/FirebladeMath/Functions/mod.swift +++ b/Sources/FirebladeMath/Functions/mod.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -23,6 +27,10 @@ public func mod(_ x: Float, _ y: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.fmodf(x, y) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.fmodf(x, y) + #endif } /// Computes the floating-point remainder of the division operation x/y. @@ -42,4 +50,8 @@ public func mod(_ x: Double, _ y: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.fmod(x, y) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.fmod(x, y) + #endif } diff --git a/Sources/FirebladeMath/Functions/pow.swift b/Sources/FirebladeMath/Functions/pow.swift index 2415278..2ff0d1c 100644 --- a/Sources/FirebladeMath/Functions/pow.swift +++ b/Sources/FirebladeMath/Functions/pow.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -23,6 +27,10 @@ public func pow(_ base: Float, _ exponent: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.powf(base, exponent) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.powf(base, exponent) + #endif } /// Computes the value of base raised to the power exponent. @@ -42,4 +50,8 @@ public func pow(_ base: Double, _ exponent: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.pow(base, exponent) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.pow(base, exponent) + #endif } diff --git a/Sources/FirebladeMath/Functions/pow2.swift b/Sources/FirebladeMath/Functions/pow2.swift index e4371f8..a1155cc 100644 --- a/Sources/FirebladeMath/Functions/pow2.swift +++ b/Sources/FirebladeMath/Functions/pow2.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -14,6 +18,10 @@ public func pow2(_ exponent: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.powf(2, exponent) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.powf(2, exponent) + #endif } /// Computes the value of base 2 raised to the power exponent. @@ -25,4 +33,8 @@ public func pow2(_ exponent: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.pow(2, exponent) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.pow(2, exponent) + #endif } diff --git a/Sources/FirebladeMath/Functions/sin.swift b/Sources/FirebladeMath/Functions/sin.swift index 5fc7e78..83d9ce0 100644 --- a/Sources/FirebladeMath/Functions/sin.swift +++ b/Sources/FirebladeMath/Functions/sin.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func sin(_ angleRad: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.sinf(angleRad) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.sinf(angleRad) + #endif } /// Computes the sine of arg (measured in radians). @@ -36,4 +44,8 @@ public func sin(_ angleRad: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.sin(angleRad) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.sin(angleRad) + #endif } diff --git a/Sources/FirebladeMath/Functions/sinh.swift b/Sources/FirebladeMath/Functions/sinh.swift index dc682cc..6a42abf 100644 --- a/Sources/FirebladeMath/Functions/sinh.swift +++ b/Sources/FirebladeMath/Functions/sinh.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func sinh(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.sinhf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.sinhf(float) + #endif } /// Computes hyperbolic sine of arg. @@ -36,4 +44,8 @@ public func sinh(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.sinh(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.sinh(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/sqrt.swift b/Sources/FirebladeMath/Functions/sqrt.swift index 0cdfdc3..55247e4 100644 --- a/Sources/FirebladeMath/Functions/sqrt.swift +++ b/Sources/FirebladeMath/Functions/sqrt.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func sqrt(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.sqrtf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.sqrtf(float) + #endif } /// Computes square root of arg. @@ -36,4 +44,8 @@ public func sqrt(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.sqrt(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.sqrt(double) + #endif } diff --git a/Sources/FirebladeMath/Functions/tan.swift b/Sources/FirebladeMath/Functions/tan.swift index cd8ee3f..151a73b 100644 --- a/Sources/FirebladeMath/Functions/tan.swift +++ b/Sources/FirebladeMath/Functions/tan.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -20,6 +24,10 @@ public func tan(_ angleRad: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.tanf(angleRad) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.tanf(angleRad) + #endif } /// Computes the tangent of arg (measured in radians). @@ -36,4 +44,8 @@ public func tan(_ angleRad: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.tan(angleRad) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.tan(angleRad) + #endif } diff --git a/Sources/FirebladeMath/Functions/tanh.swift b/Sources/FirebladeMath/Functions/tanh.swift index f2b3974..0199406 100644 --- a/Sources/FirebladeMath/Functions/tanh.swift +++ b/Sources/FirebladeMath/Functions/tanh.swift @@ -1,3 +1,7 @@ +#if FRB_MATH_FOUNDATION +import Foundation +#endif + #if FRB_MATH_DARWIN import Darwin.C.math #endif @@ -19,6 +23,10 @@ public func tanh(_ float: Float) -> Float { #if FRB_MATH_GLIBC return Glibc.tanhf(float) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.tanhf(float) + #endif } /// Computes the hyperbolic tangent of arg. @@ -34,4 +42,8 @@ public func tanh(_ double: Double) -> Double { #if FRB_MATH_GLIBC return Glibc.tanh(double) #endif + + #if FRB_MATH_FOUNDATION + return Foundation.tanh(double) + #endif } diff --git a/Tests/FirebladeMathTests/FunctionTests.swift b/Tests/FirebladeMathTests/FunctionTests.swift index f0efba0..efe1dd0 100644 --- a/Tests/FirebladeMathTests/FunctionTests.swift +++ b/Tests/FirebladeMathTests/FunctionTests.swift @@ -28,7 +28,7 @@ final class FunctionTests: XCTestCase { XCTAssertEqual(FirebladeMath.asin(Float(0.5)), 0.5235988) } func test_asinh() { - XCTAssertEqual(FirebladeMath.asinh(Double(123)), 5.505348060078276) + XCTAssertEqual(FirebladeMath.asinh(Double(123)), 5.505348060078276, accuracy: 1e-14) XCTAssertEqual(FirebladeMath.asinh(Float(123)), 5.505348) } func test_atan() { diff --git a/Tests/FirebladeMathTests/Quat4f+EulerTests.swift b/Tests/FirebladeMathTests/Quat4f+EulerTests.swift index 295bcd1..be999cc 100644 --- a/Tests/FirebladeMathTests/Quat4f+EulerTests.swift +++ b/Tests/FirebladeMathTests/Quat4f+EulerTests.swift @@ -71,9 +71,9 @@ class Quat4f_EulerTests: XCTestCase { XCTAssertEqual(quat.z, 0, accuracy: 1e-7) XCTAssertEqual(quat.w, 0, accuracy: 1e-7) // gimbal lock - XCTAssertEqual(quat.pitch, .pi) + XCTAssertEqual(quat.pitch, .pi, accuracy: 1e-6) XCTAssertEqual(quat.yaw, 0, accuracy: 1e-6) - XCTAssertEqual(quat.roll, .pi) + XCTAssertEqual(quat.roll, .pi, accuracy: 1e-6) } func testQuatFromEulerAngles_0_n90_0() { @@ -88,9 +88,9 @@ class Quat4f_EulerTests: XCTestCase { XCTAssertEqual(quat.z, 0, accuracy: 1e-7) XCTAssertEqual(quat.w, 0, accuracy: 1e-7) // gimbal lock - XCTAssertEqual(quat.pitch, .pi) + XCTAssertEqual(quat.pitch, .pi, accuracy: 1e-6) XCTAssertEqual(quat.yaw, 0, accuracy: 1e-6) - XCTAssertEqual(quat.roll, .pi) + XCTAssertEqual(quat.roll, .pi, accuracy: 1e-6) } func testQuatFromEulerAngles_0_0_90() {