├── .gitattributes ├── .github └── FUNDING.yml ├── .gitignore ├── CONTRIBUTING.md ├── Directory.Build.props ├── Directory.Build.targets ├── LICENSE ├── MathSharp.code-workspace ├── MathSharp.pub ├── MathSharp.sln ├── MathSharp.sln.DotSettings ├── MathSharp.snk ├── README.md ├── THIRD-PARTY-NOTICES.txt ├── assets ├── Benchmarks │ ├── AnchoredScaleBenchmark-barplot.png │ ├── AnchoredScaleBenchmark-report-github.md │ ├── MatrixEqualityBenchmark-barplot.png │ ├── MatrixEqualityBenchmark-report-github.md │ ├── MatrixTransposeBenchmark-barplot.png │ ├── MatrixTransposeBenchmark-report-github.md │ ├── SineWaveBenchmark-barplot.png │ ├── SineWaveBenchmark-report-github.md │ ├── VectorAdditionBenchmark-barplot.png │ └── VectorAdditionBenchmark-report-github.md ├── buymecoffee.png └── logo.png ├── build.cmd ├── build.sh ├── design-docs ├── CODEOWNERS ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── docs ├── Comparisons-And-Selections.md ├── Introduction.md ├── Storage.md ├── docfx.json └── material │ ├── partials │ └── head.tmpl.partial │ └── styles │ └── main.css ├── pack.cmd ├── pack.sh ├── restore.cmd ├── restore.sh ├── samples ├── Directory.Build.props ├── Directory.Build.targets └── MathSharp.Interactive │ ├── Benchmarks │ ├── FloatBenchmark.cs │ ├── MatrixTests │ │ └── Single │ │ │ ├── MatrixAdditionBenchmark.cs │ │ │ ├── MatrixEqualityBenchmark.cs │ │ │ ├── MatrixIsIdentityBenchmark.cs │ │ │ ├── MatrixScalarMultiplicationBenchmark.cs │ │ │ └── MatrixTransposeBenchmark.cs │ └── Vector │ │ └── Single │ │ ├── AnchoredScaleBenchmark.cs │ │ ├── CameraClassBenchmark.cs │ │ ├── SawToothWaveBenchmark.cs │ │ ├── SelectionBenchmark.cs │ │ ├── SinBenchmark.cs │ │ ├── SinCosBenchmark.cs │ │ ├── SineWaveBenchmark.cs │ │ └── VectorAdditionBenchmark.cs │ ├── MathSharp.Interactive.csproj │ ├── Program.cs │ └── TrigTest.cs ├── scripts ├── azure-pipelines.yml ├── build.ps1 ├── build.sh ├── cibuild.cmd ├── cibuild.sh ├── old │ ├── azure-pipelines.yml │ ├── build.ps1 │ └── build.sh ├── raw_hwvector_types_D.cs ├── raw_hwvector_types_S.cs └── type_generator.py ├── sources ├── Directory.Build.props ├── Directory.Build.targets └── MathSharp │ ├── .editorconfig │ ├── .gitignore │ ├── Attributes │ ├── AlignedAttribute.cs │ ├── HfaAttribute.cs │ └── HvaAttribute.cs │ ├── Constants │ ├── ScalarDoubleConstants.cs │ └── ScalarSingleConstants.cs │ ├── MathSharp.csproj │ ├── MathSharp.csproj.DotSettings │ ├── Matrix │ ├── MatrixSingle.cs │ ├── MatrixSingle │ │ ├── BasicMaths.cs │ │ ├── Conversions.cs │ │ └── MatrixOperations.cs │ └── MatrixStorageTypes.cs │ ├── Quaternion │ ├── Quaternion.cs │ ├── QuaternionConstants.cs │ └── QuaternionStorageTypes.cs │ ├── Utils │ ├── Helpers.Common.cs │ ├── Helpers.Vector4D.cs │ ├── Helpers.Vector4F.cs │ ├── ShuffleValues.cs │ └── ThrowHelper.cs │ └── Vector │ ├── Options.cs │ ├── Shared │ ├── BitComparisons.cs │ ├── BitOperations.T.cs │ ├── ComparisonResults.cs │ ├── IsSupported.cs │ └── ToString.cs │ ├── SoftwareFallbacks │ ├── BitOperations.SoftwareFallbacks.cs │ └── BitOperations.T.SoftwareFallbacks.cs │ ├── Vector.cs │ ├── VectorDimensions.cs │ ├── VectorFloatingPoint │ ├── VectorConstants.cs │ ├── VectorDouble │ │ ├── BasicMaths.cs │ │ ├── Casts.cs │ │ ├── Comparisons.cs │ │ ├── Conversions.cs │ │ ├── FusedOperations.cs │ │ ├── Logical.cs │ │ ├── SoftwareFallbacks │ │ │ ├── BasicMaths.SoftwareFallbacks.cs │ │ │ ├── Comparisons.SoftwareFallbacks.cs │ │ │ └── VectorOperations.SoftwareFallbacks.cs │ │ ├── Trigonometry.cs │ │ └── VectorOperations.cs │ └── VectorSingle │ │ ├── BasicMaths.cs │ │ ├── Casts.cs │ │ ├── Comparisons.cs │ │ ├── Conversions.SystemNumerics.cs │ │ ├── Conversions.cs │ │ ├── FusedOperations.cs │ │ ├── Logical.cs │ │ ├── SoftwareFallbacks │ │ ├── BasicMaths.SoftwareFallbacks.cs │ │ ├── Comparisons.SoftwareFallbacks.cs │ │ └── VectorOperations.SoftwareFallbacks.cs │ │ ├── Trigonometry.cs │ │ └── VectorOperations.cs │ ├── VectorStorageType.Double.cs │ └── VectorStorageTypes.Single.cs ├── test.cmd ├── test.sh ├── tests ├── Directory.Build.props ├── Directory.Build.targets └── MathSharp.UnitTests │ ├── Helpers │ └── TestHelpers.cs │ ├── MathSharp.UnitTests.csproj │ ├── MathSharp.UnitTests.csproj.DotSettings │ └── VectorTests │ ├── VectorDouble │ ├── BasicMathsTests │ │ ├── Abs.cs │ │ ├── Add.cs │ │ ├── Clamp.cs │ │ ├── Divide.cs │ │ ├── HorizontalAdd.cs │ │ ├── Max.cs │ │ ├── Min.cs │ │ ├── Multiply.cs │ │ ├── Negate.cs │ │ ├── Remainder.cs │ │ ├── Sqrt.cs │ │ └── Subtract.cs │ ├── BitOperationsTests │ │ ├── And.cs │ │ ├── AndNot.cs │ │ ├── Not.cs │ │ ├── Or.cs │ │ └── Xor.cs │ ├── ComparisonTests │ │ ├── Equality.cs │ │ ├── GreaterThan.cs │ │ ├── GreaterThanOrEqualTo.cs │ │ ├── Inequality.cs │ │ ├── LessThan.cs │ │ └── LessThanOrEqualTo.cs │ ├── ConversionTests │ │ ├── LoadTests.cs │ │ └── StoreTests.cs │ └── VectorOperations │ │ ├── CrossProduct.cs │ │ ├── Distance.cs │ │ ├── DistanceSquared.cs │ │ ├── DotProduct.cs │ │ ├── Length.cs │ │ ├── LengthSquared.cs │ │ ├── Lerp.cs │ │ ├── Normalize.cs │ │ └── Reflect.cs │ └── VectorSingle │ ├── BasicMathsTests │ ├── Abs.cs │ ├── Add.cs │ ├── Clamp.cs │ ├── CopySign.cs │ ├── Divide.cs │ ├── ExtractSign.cs │ ├── Floor.cs │ ├── HorizontalAdd.cs │ ├── InBounds.cs │ ├── IsFinite.cs │ ├── IsInfinite.cs │ ├── IsNaN.cs │ ├── IsNotNaN.cs │ ├── IsNotZero.cs │ ├── IsZero.cs │ ├── Max.cs │ ├── Min.cs │ ├── Multiply.cs │ ├── Negate.cs │ ├── Reciprocal.cs │ ├── ReciprocalApprox.cs │ ├── ReciprocalSqrt.cs │ ├── ReciprocalSqrtApprox.cs │ ├── Remainder.cs │ ├── Round.cs │ ├── Sqrt.cs │ ├── Subtract.cs │ └── Truncate.cs │ ├── BitOperationsTests │ ├── And.cs │ ├── AndNot.cs │ ├── Not.cs │ ├── Or.cs │ └── Xor.cs │ ├── ComparisonTests │ ├── Equal.cs │ ├── GreaterThan.cs │ ├── GreaterThanOrEqualTo.cs │ ├── LessThan.cs │ ├── LessThanOrEqualTo.cs │ └── NotEqual.cs │ ├── DataSets.cs │ └── VectorOperations │ ├── CrossProduct.cs │ ├── Distance.cs │ ├── DistanceSquared.cs │ ├── DotProduct.cs │ ├── Length.cs │ ├── LengthSquared.cs │ ├── Lerp.cs │ ├── Normalize.cs │ ├── NormalizeApprox.cs │ └── Reflect.cs └── vendor.yml /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | ############################################################################### 5 | # Set explicit file behavior to: 6 | # treat as binary 7 | ############################################################################### 8 | *.snk binary 9 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | custom: [paypal.me/johnhkelly, buymeacoffee.com/johnhk, ko-fi.com/johnhk] 4 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/CONTRIBUTING.md -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | 15 | $(MSBuildThisFileDirectory)artifacts/ 16 | $(MathSharpProjectCategory)/$(MSBuildProjectName) 17 | https://github.com/johnkellyoxford/MathSharp 18 | 19 | 20 | 21 | 22 | true 23 | $(BaseArtifactsPath)obj/$(BaseArtifactsPathSuffix)/ 24 | embedded 25 | false 26 | enable 27 | true 28 | true 29 | true 30 | false 31 | 32 | 33 | 34 | true 35 | 36 | 37 | 38 | 39 | John Kelly and Contributors 40 | $(BaseArtifactsPath)bin/$(BaseArtifactsPathSuffix)/ 41 | John Kelly 42 | $(BaseArtifactsPath)pkg/$(BaseArtifactsPathSuffix)/$(Configuration)/ 43 | MathSharp 44 | 2.0.0 45 | pre 46 | 47 | 48 | 49 | 50 | Copyright (C) John Kelly and Contributors 51 | MathSharp is a vector and matrix library written in C# using hardware intrinsics. Thanks to hardware acceleration, MathSharp is significantly faster than most mathematics libraries out there. 52 | strict 53 | false 54 | true 55 | preview 56 | en-GB 57 | true 58 | MIT 59 | $(RepositoryUrl) 60 | true 61 | false 62 | git 63 | 64 | https://api.nuget.org/v3/index.json; 65 | 66 | true 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /Directory.Build.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | 15 | $(DefineConstants);$(OS) 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 John Kelly 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MathSharp.code-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "folders": [ 3 | { 4 | "path": "." 5 | } 6 | ] 7 | } -------------------------------------------------------------------------------- /MathSharp.pub: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/MathSharp.pub -------------------------------------------------------------------------------- /MathSharp.sln.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | True 3 | True 4 | True 5 | True 6 | True 7 | True 8 | True 9 | True 10 | True 11 | True 12 | True 13 | True 14 | True 15 | True 16 | True -------------------------------------------------------------------------------- /MathSharp.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/MathSharp.snk -------------------------------------------------------------------------------- /THIRD-PARTY-NOTICES.txt: -------------------------------------------------------------------------------- 1 | -- new-repo template - https://github.com/tannergooding/new-repo 2 | # The MIT License (MIT) 3 | 4 | Copyright © Tanner Gooding and Contributors 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | this software and associated documentation files (the "Software"), to deal in 8 | the Software without restriction, including without limitation the rights to 9 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | the Software, and to permit persons to whom the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | -- DirectXMath (used for algorithms) - https://github.com/microsoft/DirectXMath 24 | 25 | The MIT License (MIT) 26 | 27 | Copyright (c) 2011-2019 Microsoft Corp 28 | 29 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 30 | software and associated documentation files (the "Software"), to deal in the Software 31 | without restriction, including without limitation the rights to use, copy, modify, 32 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 33 | permit persons to whom the Software is furnished to do so, subject to the following 34 | conditions: 35 | 36 | The above copyright notice and this permission notice shall be included in all copies 37 | or substantial portions of the Software. 38 | 39 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 40 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 41 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 42 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 43 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 44 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 45 | 46 | -- corefx (used for algorithms) - https://github.com/dotnet/corefx 47 | 48 | The MIT License (MIT) 49 | 50 | Copyright (c) .NET Foundation and Contributors 51 | 52 | All rights reserved. 53 | 54 | Permission is hereby granted, free of charge, to any person obtaining a copy 55 | of this software and associated documentation files (the "Software"), to deal 56 | in the Software without restriction, including without limitation the rights 57 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 58 | copies of the Software, and to permit persons to whom the Software is 59 | furnished to do so, subject to the following conditions: 60 | 61 | The above copyright notice and this permission notice shall be included in all 62 | copies or substantial portions of the Software. 63 | 64 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 65 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 66 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 67 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 68 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 69 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 70 | SOFTWARE. -------------------------------------------------------------------------------- /assets/Benchmarks/AnchoredScaleBenchmark-barplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/assets/Benchmarks/AnchoredScaleBenchmark-barplot.png -------------------------------------------------------------------------------- /assets/Benchmarks/AnchoredScaleBenchmark-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362 4 | Intel Core i3-8350K CPU 4.00GHz (Coffee Lake), 1 CPU, 4 logical and 4 physical cores 5 | .NET Core SDK=3.0.100 6 | [Host] : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 7 | Core : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 8 | 9 | Job=Core Runtime=Core 10 | 11 | ``` 12 | | Method | Mean | Error | StdDev | Rank | 13 | |--------------- |-----------:|----------:|----------:|-----:| 14 | | MathSharp | 0.8542 ns | 0.0084 ns | 0.0079 ns | 1 | 15 | | SystemNumerics | 2.0281 ns | 0.0123 ns | 0.0115 ns | 2 | 16 | | OpenTk | 37.4250 ns | 0.1585 ns | 0.1483 ns | 3 | 17 | -------------------------------------------------------------------------------- /assets/Benchmarks/MatrixEqualityBenchmark-barplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/assets/Benchmarks/MatrixEqualityBenchmark-barplot.png -------------------------------------------------------------------------------- /assets/Benchmarks/MatrixEqualityBenchmark-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362 4 | Intel Core i3-8350K CPU 4.00GHz (Coffee Lake), 1 CPU, 4 logical and 4 physical cores 5 | .NET Core SDK=3.0.100 6 | [Host] : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 7 | DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 8 | 9 | 10 | ``` 11 | | Method | Mean | Error | StdDev | 12 | |--------------- |----------:|----------:|----------:| 13 | | OpenTk | 10.984 ns | 0.1087 ns | 0.1017 ns | 14 | | SystemNumerics | 3.895 ns | 0.0185 ns | 0.0164 ns | 15 | | MathSharp | 1.622 ns | 0.0109 ns | 0.0102 ns | 16 | -------------------------------------------------------------------------------- /assets/Benchmarks/MatrixTransposeBenchmark-barplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/assets/Benchmarks/MatrixTransposeBenchmark-barplot.png -------------------------------------------------------------------------------- /assets/Benchmarks/MatrixTransposeBenchmark-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362 4 | Intel Core i3-8350K CPU 4.00GHz (Coffee Lake), 1 CPU, 4 logical and 4 physical cores 5 | .NET Core SDK=3.0.100 6 | [Host] : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 7 | DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 8 | 9 | 10 | ``` 11 | | Method | Mean | Error | StdDev | 12 | |--------------- |----------:|----------:|----------:| 13 | | MathSharp | 1.070 ns | 0.0122 ns | 0.0114 ns | 14 | | SystemNumerics | 3.744 ns | 0.0153 ns | 0.0143 ns | 15 | | OpenTk | 15.875 ns | 0.3438 ns | 0.5146 ns | 16 | -------------------------------------------------------------------------------- /assets/Benchmarks/SineWaveBenchmark-barplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/assets/Benchmarks/SineWaveBenchmark-barplot.png -------------------------------------------------------------------------------- /assets/Benchmarks/SineWaveBenchmark-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362 4 | Intel Core i3-8350K CPU 4.00GHz (Coffee Lake), 1 CPU, 4 logical and 4 physical cores 5 | .NET Core SDK=3.0.100 6 | [Host] : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 7 | DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 8 | 9 | 10 | ``` 11 | | Method | Mean | Error | StdDev | 12 | |------------ |----------:|----------:|----------:| 13 | | SystemMathF | 412.30 us | 1.6319 us | 1.5265 us | 14 | | MathSharp | 80.37 us | 0.6465 us | 0.6048 us | 15 | -------------------------------------------------------------------------------- /assets/Benchmarks/VectorAdditionBenchmark-barplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/assets/Benchmarks/VectorAdditionBenchmark-barplot.png -------------------------------------------------------------------------------- /assets/Benchmarks/VectorAdditionBenchmark-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362 4 | Intel Core i3-8350K CPU 4.00GHz (Coffee Lake), 1 CPU, 4 logical and 4 physical cores 5 | .NET Core SDK=3.0.100 6 | [Host] : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 7 | DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT 8 | 9 | 10 | ``` 11 | | Method | Mean | Error | StdDev | 12 | |--------------- |----------:|----------:|----------:| 13 | | OpenTk | 6.5341 ns | 0.0392 ns | 0.0367 ns | 14 | | SystemNumerics | 0.0510 ns | 0.0080 ns | 0.0075 ns | 15 | | MathSharp | 0.0426 ns | 0.0043 ns | 0.0040 ns | 16 | -------------------------------------------------------------------------------- /assets/buymecoffee.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/assets/buymecoffee.png -------------------------------------------------------------------------------- /assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-h-k/MathSharp/69672de21f0fdf71c52fd73df7f7c2b8b9527c3e/assets/logo.png -------------------------------------------------------------------------------- /build.cmd: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | powershell.exe -NoLogo -NoProfile -ExecutionPolicy ByPass -Command "& """%~dp0scripts\build.ps1""" -build %*" 3 | EXIT /B %ERRORLEVEL% 4 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SOURCE="${BASH_SOURCE[0]}" 4 | while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink 5 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 6 | SOURCE="$(readlink "$SOURCE")" 7 | [[ $SOURCE != /* ]] && SOURCE="$ScriptRoot/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located 8 | done 9 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 10 | 11 | . "$ScriptRoot/scripts/build.sh" --build "$@" 12 | -------------------------------------------------------------------------------- /design-docs/CODEOWNERS: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Default owner for all files in the repository 3 | ############################################################################### 4 | * @johnkellyoxford 5 | -------------------------------------------------------------------------------- /design-docs/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of 9 | experience, nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or reject 41 | comments, commits, code, wiki edits, issues, and other contributions that are 42 | not aligned to this Code of Conduct, or to ban temporarily or permanently any 43 | contributor for other behaviors that they deem inappropriate, threatening, 44 | offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at [INSERT EMAIL ADDRESS]. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an 62 | incident. Further details of specific enforcement policies may be posted 63 | separately. 64 | 65 | Project maintainers who do not follow or enforce the Code of Conduct in good 66 | faith may face temporary or permanent repercussions as determined by other 67 | members of the project's leadership. 68 | 69 | ## Attribution 70 | 71 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 72 | version 1.4, available at 73 | https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 74 | 75 | [homepage]: https://www.contributor-covenant.org 76 | -------------------------------------------------------------------------------- /design-docs/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to $NewRepo 2 | 3 | The following is a set of guidelines for contributing to MathSharp. 4 | 5 | ## Table of Contents 6 | 7 | * [Code of Conduct](#code-of-conduct) 8 | * [License](#license) 9 | * [What should I know?](#what-should-i-know) 10 | * [Pull Requests](#pull-requests) 11 | * [Issues](#issues) 12 | * [Questions](#questions) 13 | 14 | ### Code of Conduct 15 | 16 | $NewRepo and everyone contributing (this includes issues, pull requests, the 17 | wiki, etc) must abide by the [CODE_OF_CONDUCT](docs/CODE_OF_CONDUCT.md). 18 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 19 | reported by contacting the project team at $Email. 20 | 21 | ### License 22 | 23 | Copyright © $Name and Contributors. Licensed under the MIT License 24 | (MIT). See [LICENSE](LICENSE.md) in the repository root for more information. 25 | 26 | ### What should I know? 27 | 28 | Most of the basics of the project, such as what it is about, and its goals are 29 | covered in our [README](docs/README.md). 30 | 31 | ### Pull Requests 32 | 33 | All pull requests should follow our 34 | [PULL_REQUEST_TEMPLATE](docs/PULL_REQUEST_TEMPALTE.md). It is additionally 35 | recommended that an issue be opened, discussed, and approved first to ensure 36 | that the change will be accepted. Any pull requests not following pull request 37 | template will be requested to be updated. Any pull requests opened without a 38 | corresponding issue may be delayed or be required to undergo further changes 39 | before being accepted. 40 | 41 | ### Issues 42 | 43 | All issues should follow our [ISSUE_TEMPLATE](ISSUE_TEMPLATE.md). It is 44 | additionaly recommended to prefix your issue with an appriopriate "tag" such as 45 | `[QUESTION]`, `[BUG]`, `[REGRESSION]`, `[IDEA]`, or `[PROPOSAL]`. 46 | 47 | ### Questions 48 | 49 | Feel free to open an issue prefixed with `[QUESTION]`. 50 | -------------------------------------------------------------------------------- /design-docs/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | # Issue Template 2 | 3 | Please remove everything except for the template best matching your issue type 4 | below. Then ensure you fill in the template, to the best of your abilities 5 | before submitting. 6 | 7 | ## Table of Contents 8 | 9 | * [Code of Conduct](#code-of-conduct) 10 | * [Question](#question) 11 | * [Bug](#bug) 12 | * [Regression](#regression) 13 | * [Idea](#idea) 14 | * [Proposal](#proposal) 15 | 16 | ### Code of Conduct 17 | 18 | $NewRepo and everyone contributing (this includes issues, pull requests, the 19 | wiki, etc) must abide by the [CODE_OF_CONDUCT](docs/CODE_OF_CONDUCT.md). 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 21 | reported by contacting the project team at $Email. 22 | 23 | ### Question 24 | 25 | Questions are straightforward, just prefix your issue title with `[QUESTION]` 26 | and state what you desire to be answered. 27 | 28 | ### Bug 29 | 30 | Bugs need to be both reproducible or we need some other mechanism of being able 31 | to debug and determine the root cause of failure. Please prefix your issue title 32 | with `[BUG]` and fill out the following sections. 33 | 34 | #### Description (optional) 35 | 36 | This section should contain an expanded description of the bug, if the title is 37 | not sufficient. 38 | 39 | #### Reproduction Steps 40 | 41 | This section should contain an ordered list of steps that can be used to 42 | reproduce the bug. Ideally, you would include as much information about your 43 | environment as possible to help use in determining the best fix for the issue. 44 | 45 | #### Expected Behavior 46 | 47 | This section should contain a description of the behavior that is expected. 48 | 49 | #### Actual Behavior 50 | 51 | This section should contain a description of the actual behavior being observed. 52 | 53 | ### Regression 54 | 55 | Regressions are similar to bugs but represent functionality that worked in a 56 | prior release but is no longer functioning as expected. Please prefix your issue 57 | title with `[REGRESSION]` and fill out the same sections as for a [Bug](#bug). 58 | 59 | ### Idea 60 | 61 | Requests would ideally always be well thought and come with a full proposal; 62 | however, this is not always possible and sometimes you want input from others 63 | before actually proposing something. Please prefix your issue title with 64 | `[IDEA]` and give a brief summary of what you want. 65 | 66 | ### Proposals 67 | 68 | Proposals are generally thought out and discussed ideas that are ready to be 69 | proposed for inclusion in the project. Please prefix your issue title with 70 | `[PROPOSAL]` and fill out the following sections. 71 | 72 | #### Description (optional) 73 | 74 | This section should contain an expanded description of the proposal, if the 75 | title is not sufficient. 76 | 77 | #### Rationale 78 | 79 | This section should contain the reasoning behind the proposal. It should include 80 | things like "Why do we need this", "How will it improve a developers quality of 81 | life", etc. 82 | 83 | #### Proposed API 84 | 85 | This section should contain the APIs being proposed along with a brief 86 | description of what each API does. 87 | 88 | #### Drawbacks 89 | 90 | This should contain any drawbacks for the proposal that came up during the 91 | discussions. 92 | 93 | #### Alternatives 94 | 95 | This should contain any alternative proposals that came up during the 96 | discussions. 97 | 98 | #### Other thoughts 99 | 100 | This should contain any additional thoughts or footnotes on the proposal. 101 | 102 | #### Discussions (optional) 103 | 104 | This section should contain a link to all discussions related to the proposal 105 | that have been made thus far. 106 | -------------------------------------------------------------------------------- /design-docs/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | # Pull Request Template 2 | 3 | Please remove everything except for the template best matching your pull request 4 | type below. Then ensure you fill in the template, to the best of your abilities 5 | before submitting. 6 | 7 | ## Table of Contents 8 | 9 | * [Code of Conduct](#code-of-conduct) 10 | * [Test or Documentation Addition](#test-or-documentation-addition) 11 | * [Bug Fix](#bug-fix) 12 | * [Proposal Implementation](#proposal-implementation) 13 | 14 | ### Code of Conduct 15 | 16 | $NewRepo and everyone contributing (this includes issues, pull requests, the 17 | wiki, etc) must abide by the [CODE_OF_CONDUCT](docs/CODE_OF_CONDUCT.md). 18 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 19 | reported by contacting the project team at $Email. 20 | 21 | ## Test or Documentation Addition 22 | 23 | These types of additions should just have a brief summary of what they are 24 | doing. They do not need an attached issue. They should be prefixed with `[TEST]` 25 | or `[DOCS]`. 26 | 27 | ## Bug Fix 28 | 29 | Bug fixes should have a brief summary of the change as well as a link to the 30 | issue they are resolving. They should be prefixed with `[BUG]` and should 31 | include a regression test to ensure the bug is not introduced again in the 32 | future. 33 | 34 | ## Proposal Implementation 35 | 36 | Proposal implementations should also have a brief summary of the change and a 37 | link to the proposal they are implementing. They should be prefixed with 38 | `[IMPL]`. Ideally, they will also come with basic unit, functional, or 39 | integration tests that validate they work as intended. 40 | -------------------------------------------------------------------------------- /docs/Storage.md: -------------------------------------------------------------------------------- 1 | # Storage 2 | 3 | MathSharp operations are designed to occur with locals and arguments, which are located on the stack or SIMD registers, allowing them to be fast and efficient. Because of this, 4 | the types worked with are generally larger than you would expect or have specific alignment requirements, and don't distinguish between different dimensions. Generally, you should use other types 5 | for fields and members of types, and then use SIMD types for passing parameters or returns. This will result in the best performance and the minimum number of conversions between storage and SIMD types. 6 | 7 | By default, MathSharp supports using the `System.Numerics` types as storage types, and provides `Load()` extension methods in `Vector, Matrix, Quaternion`, and `Store(out T store)` methods too. 8 | Writing your own storage types is trivial. Use the `FromVector` and `ToVector` methods to convert the types. As an example, here is a custom Vector3 storage type. 9 | 10 | ```cs 11 | public struct MyVec3 12 | { 13 | public float X, Y, Z; 14 | 15 | public Vector128 Load() => Vector.FromVector3D(in x); 16 | } 17 | 18 | public static class MyVec3Extensions 19 | { 20 | public static void Store(this Vector128 vector, out MyVec3 vec3) => Vector.ToVector3D(vector, vec3); 21 | } 22 | ``` 23 | 24 | You can use the aligned variants of these methods (e.g `FromVector3DAligned`) if you can guarantee the passed value is always 16 byte aligned for `float`s, and 32 byte aligned for `double`s. 25 | 26 | Passing by `in` is very important, because it ensures you are actually passing a reference. We don't take `ref` because we want to allow readonly members to be passed. 27 | The load and from methods also have pointer overloads. Where these can be called without pinning, they should be used for potential performance benefits. -------------------------------------------------------------------------------- /docs/docfx.json: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": [ 3 | { 4 | "src": [ 5 | { 6 | "files": [ 7 | "**.csproj" 8 | ], 9 | "src": "..\\sources\\MathSharp" 10 | } 11 | ], 12 | "dest": "api", 13 | "disableGitFeatures": false, 14 | "disableDefaultFilter": false 15 | } 16 | ], 17 | "build": { 18 | "content": [ 19 | { 20 | "files": [ 21 | "api/**.yml", 22 | "api/index.md" 23 | ] 24 | }, 25 | { 26 | "files": [ 27 | "articles/**.md", 28 | "articles/**/toc.yml", 29 | "toc.yml", 30 | "*.md" 31 | ] 32 | } 33 | ], 34 | "resource": [ 35 | { 36 | "files": [ 37 | "images/**" 38 | ] 39 | } 40 | ], 41 | "overwrite": [ 42 | { 43 | "files": [ 44 | "apidoc/**.md" 45 | ], 46 | "exclude": [ 47 | "obj/**", 48 | "_site/**" 49 | ] 50 | } 51 | ], 52 | "dest": "_site", 53 | "globalMetadataFiles": [], 54 | "fileMetadataFiles": [], 55 | "template": [ 56 | "default", 57 | "templates/material" 58 | ], 59 | "postProcessors": [], 60 | "markdownEngineName": "markdig", 61 | "noLangKeyword": false, 62 | "keepFileLink": false, 63 | "cleanupCacheHistory": false, 64 | "disableGitFeatures": false 65 | } 66 | } -------------------------------------------------------------------------------- /docs/material/partials/head.tmpl.partial: -------------------------------------------------------------------------------- 1 | {{!Copyright (c) Oscar Vasquez. All rights reserved. Licensed under the MIT license. See LICENSE file in the project root for full license information.}} 2 | 3 | 4 | 5 | 6 | {{#title}}{{title}}{{/title}}{{^title}}{{>partials/title}}{{/title}} {{#_appTitle}}| {{_appTitle}} {{/_appTitle}} 7 | 8 | 9 | 10 | {{#_description}}{{/_description}} 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | {{#_noindex}}{{/_noindex}} 19 | {{#_enableSearch}}{{/_enableSearch}} 20 | {{#_enableNewTab}}{{/_enableNewTab}} 21 | -------------------------------------------------------------------------------- /pack.cmd: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | powershell.exe -NoLogo -NoProfile -ExecutionPolicy ByPass -Command "& """%~dp0scripts\build.ps1""" -pack %*" 3 | EXIT /B %ERRORLEVEL% 4 | -------------------------------------------------------------------------------- /pack.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SOURCE="${BASH_SOURCE[0]}" 4 | while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink 5 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 6 | SOURCE="$(readlink "$SOURCE")" 7 | [[ $SOURCE != /* ]] && SOURCE="$ScriptRoot/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located 8 | done 9 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 10 | 11 | . "$ScriptRoot/scripts/build.sh" --pack "$@" 12 | -------------------------------------------------------------------------------- /restore.cmd: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | powershell.exe -NoLogo -NoProfile -ExecutionPolicy ByPass -Command "& """%~dp0scripts\build.ps1""" -restore %*" 3 | EXIT /B %ERRORLEVEL% 4 | -------------------------------------------------------------------------------- /restore.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SOURCE="${BASH_SOURCE[0]}" 4 | while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink 5 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 6 | SOURCE="$(readlink "$SOURCE")" 7 | [[ $SOURCE != /* ]] && SOURCE="$ScriptRoot/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located 8 | done 9 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 10 | 11 | . "$ScriptRoot/scripts/build.sh" --restore "$@" 12 | -------------------------------------------------------------------------------- /samples/Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | $(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.props 15 | $(NoWarn);CS1591 16 | Exe 17 | samples 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /samples/Directory.Build.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | $(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.targets 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/FloatBenchmark.cs: -------------------------------------------------------------------------------- 1 | namespace MathSharp.Interactive.Benchmarks 2 | { 3 | public abstract class FloatBenchmark 4 | { 5 | public const int Count = 1024; 6 | 7 | 8 | } 9 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/MatrixTests/Single/MatrixAdditionBenchmark.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Runtime.CompilerServices; 3 | using BenchmarkDotNet.Attributes; 4 | using OpenTK; 5 | 6 | namespace MathSharp.Interactive.Benchmarks.MatrixTests.Single 7 | { 8 | public class MatrixAdditionBenchmark 9 | { 10 | private Matrix4 _openTk1; 11 | private Matrix4 _openTk2; 12 | 13 | private MatrixSingle _mathSharp1; 14 | private MatrixSingle _mathSharp2; 15 | 16 | 17 | 18 | [GlobalSetup] 19 | public unsafe void Setup() 20 | { 21 | Trace.Assert(Unsafe.SizeOf() == Unsafe.SizeOf()); 22 | var openTk1 = new Matrix4(); 23 | var openTk2 = new Matrix4(); 24 | 25 | float* p1 = (float*) &openTk1; 26 | float* p2 = (float*) &openTk2; 27 | for (var i = 0; i < 4 * 4; i++) 28 | { 29 | p1[i] = i; 30 | p2[i] = 1f / i; 31 | } 32 | 33 | _openTk1 = openTk1; 34 | _openTk2 = openTk2; 35 | 36 | _mathSharp1 = Unsafe.As(ref openTk1); 37 | _mathSharp2 = Unsafe.As(ref openTk2); 38 | } 39 | 40 | [Benchmark] 41 | public MatrixSingle MathSharp() 42 | { 43 | return Matrix.Add(_mathSharp1, _mathSharp2); 44 | } 45 | 46 | [Benchmark] 47 | public Matrix4 OpenTk() 48 | { 49 | return _openTk1 + _openTk2; 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/MatrixTests/Single/MatrixEqualityBenchmark.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using BenchmarkDotNet.Attributes; 3 | using OpenTK; 4 | 5 | namespace MathSharp.Interactive.Benchmarks.MatrixTests.Single 6 | { 7 | [RPlotExporter] 8 | public class MatrixEqualityBenchmark 9 | { 10 | private static readonly MatrixSingle M1; 11 | private static readonly MatrixSingle M2; 12 | 13 | private static readonly Matrix4 OpenTk1; 14 | private static readonly Matrix4 OpenTk2; 15 | 16 | private static readonly Matrix4x4 SysNum1; 17 | private static readonly Matrix4x4 SysNum2; 18 | 19 | [Benchmark] 20 | public bool OpenTk() 21 | { 22 | return OpenTk1 == OpenTk2; 23 | } 24 | 25 | [Benchmark] 26 | public bool SystemNumerics() 27 | { 28 | return SysNum1 == SysNum2; 29 | } 30 | 31 | [Benchmark] 32 | public bool MathSharp() 33 | { 34 | return Matrix.CompareEqual(M1, M2); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/MatrixTests/Single/MatrixIsIdentityBenchmark.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | 3 | namespace MathSharp.Interactive.Benchmarks.MatrixTests.Single 4 | { 5 | public class MatrixIsIdentityBenchmark 6 | { 7 | private readonly MatrixSingle _matrix = default; 8 | 9 | [Benchmark] 10 | public bool Normal() 11 | { 12 | return Matrix.IsIdentity(_matrix); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/MatrixTests/Single/MatrixScalarMultiplicationBenchmark.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Runtime.CompilerServices; 3 | using BenchmarkDotNet.Attributes; 4 | using OpenTK; 5 | 6 | namespace MathSharp.Interactive.Benchmarks.MatrixTests.Single 7 | { 8 | public class MatrixScalarMultiplicationBenchmark 9 | { 10 | private Matrix4 _openTk1; 11 | private MatrixSingle _mathSharp1; 12 | 13 | [GlobalSetup] 14 | public unsafe void Setup() 15 | { 16 | Trace.Assert(Unsafe.SizeOf() == Unsafe.SizeOf()); 17 | var openTk1 = new Matrix4(); 18 | var openTk2 = new Matrix4(); 19 | 20 | float* p1 = (float*)&openTk1; 21 | float* p2 = (float*)&openTk2; 22 | for (var i = 0; i < 4 * 4; i++) 23 | { 24 | p1[i] = i; 25 | p2[i] = 1f / i; 26 | } 27 | 28 | _openTk1 = openTk1; 29 | 30 | _mathSharp1 = Unsafe.As(ref openTk1); 31 | } 32 | 33 | [Benchmark] 34 | public MatrixSingle MathSharp() 35 | { 36 | return Matrix.ScalarMultiply(_mathSharp1, 2f); 37 | } 38 | 39 | [Benchmark] 40 | public Matrix4 OpenTk() 41 | { 42 | return _openTk1 * 2f; 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/MatrixTests/Single/MatrixTransposeBenchmark.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using BenchmarkDotNet.Attributes; 3 | using OpenTK; 4 | 5 | namespace MathSharp.Interactive.Benchmarks.MatrixTests.Single 6 | { 7 | [RPlotExporter] 8 | public class MatrixTransposeBenchmark 9 | { 10 | private Matrix4 _openTk1; 11 | private MatrixSingle _mathSharp1; 12 | private Matrix4x4 _sys1; 13 | 14 | [Benchmark] 15 | public MatrixSingle MathSharp() 16 | { 17 | return Matrix.Transpose(_mathSharp1); 18 | } 19 | 20 | [Benchmark] 21 | public Matrix4x4 SystemNumerics() 22 | { 23 | return Matrix4x4.Transpose(_sys1); 24 | } 25 | 26 | [Benchmark] 27 | public Matrix4 OpenTk() 28 | { 29 | return Matrix4.Transpose(_openTk1); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/Vector/Single/CameraClassBenchmark.cs: -------------------------------------------------------------------------------- 1 |  using System; 2 | using System.Runtime.Intrinsics; 3 | using BenchmarkDotNet.Attributes; 4 | 5 | namespace MathSharp.Interactive.Benchmarks.Vector.Single 6 | { 7 | using Vector = MathSharp.Vector; 8 | using Vector3 = System.Numerics.Vector3; 9 | 10 | public class CameraClassBenchmark 11 | { 12 | private float _pitch; 13 | private float _yaw; 14 | private Vector128 _unitY; 15 | private Vector128 _unitYWrapper; 16 | 17 | private Vector128 _mathSharpFront; 18 | private Vector128 _mathSharpWrapperFront; 19 | private Vector3 _openTkFront; 20 | 21 | [GlobalSetup] 22 | public void Setup() 23 | { 24 | _pitch = 11f; 25 | _yaw = 0.008888f; 26 | _unitY = Vector128.Create(0f, 1f, 0f, 0f); 27 | _unitYWrapper = _unitY; 28 | float x = MathF.Cos(_pitch) * MathF.Cos(_yaw); 29 | float y = MathF.Sin(_pitch); 30 | float z = MathF.Cos(_pitch) * MathF.Sin(_yaw); 31 | _mathSharpFront = Vector128.Create(x, y, z, 0); 32 | _mathSharpWrapperFront = _mathSharpFront; 33 | _openTkFront = new Vector3(x, y, z); 34 | } 35 | 36 | [Benchmark] 37 | public Vector128 MathSharp() 38 | { 39 | Vector128 front = _mathSharpFront; 40 | front = Vector.Normalize3D(front); 41 | Vector128 right = Vector.Normalize3D(Vector.Cross3D(_unitY, front)); 42 | 43 | Vector128 up = Vector.Normalize3D(Vector.Cross3D(front, right)); 44 | return up; 45 | } 46 | 47 | public void DotAndCross(Vector3 left0, Vector3 left1, Vector3 right0, Vector3 right1, out Vector3 result) 48 | { 49 | var leftDot = new Vector3(Vector3.Dot(left0, left1)); 50 | var rightDot = new Vector3(Vector3.Dot(right0, right1)); 51 | result = Vector3.Cross(leftDot, rightDot); 52 | } 53 | 54 | public void DotAndCross(in Vector3 left0, in Vector3 left1, in Vector3 right0, in Vector3 right1, out Vector3 result) 55 | { 56 | var leftDot = Vector.Dot3D(left0.Load(), left1.Load()); 57 | var rightDot = Vector.Dot3D(right0.Load(), right1.Load()); 58 | 59 | Vector.Store(Vector.Cross3D(leftDot, rightDot), out result); 60 | } 61 | 62 | [Benchmark] 63 | public Vector3 OpenTkMath() 64 | { 65 | Vector3 front = _openTkFront; 66 | front = Vector3.Normalize(front); 67 | Vector3 right = Vector3.Normalize(Vector3.Cross(Vector3.UnitY, front)); 68 | 69 | Vector3 up = Vector3.Normalize(Vector3.Cross(front, right)); 70 | return up; 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/Vector/Single/SelectionBenchmark.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Numerics; 5 | using System.Runtime.CompilerServices; 6 | using System.Runtime.Intrinsics; 7 | using System.Runtime.Intrinsics.X86; 8 | using System.Text; 9 | 10 | using MVector = MathSharp.Vector; 11 | 12 | namespace MathSharp.Interactive.Benchmarks.Vector.Single 13 | { 14 | public class SelectionBenchmark 15 | { 16 | private Vector4 _value = new Vector4(99, -5, 6, -2.3f); 17 | private Vector4 _multiply = new Vector4(99, 7, 0.1f, -2.3f); 18 | 19 | 20 | private Vector128 _value1 = Vector128.Create(99, -5, 6, -2.3f); 21 | private Vector128 _multiply1 = Vector128.Create(99, 7, 0.1f, -2.3f); 22 | 23 | [Benchmark] 24 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 25 | public Vector4 SysNumeric() => Foo(_value, _multiply); 26 | 27 | [Benchmark] 28 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 29 | public Vector128 MathSharp() => Foo(_value1, _multiply1); 30 | 31 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 32 | public Vector4 Foo(Vector4 left, Vector4 right) 33 | { 34 | var mul = left * right; 35 | var sqrt = Vector4.SquareRoot(mul); 36 | // No way to vectorise this :( 37 | var x = left.X == right.X ? left.X * right.X : sqrt.X; 38 | var y = left.Y == right.Y ? left.Y * right.Y : sqrt.Y; 39 | var z = left.Z == right.Z ? left.Z * right.Z : sqrt.Z; 40 | var w = left.W == right.W ? left.W * right.W : sqrt.W; 41 | return new Vector4(x, y, z, w); 42 | } 43 | 44 | //[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] 45 | //public static Vector128 MulNegAndAbs_MathSharp(Vector128 value, Vector128 multiply) 46 | //{ 47 | // var mul = Sse.Multiply(Sse.Xor(value, Vector128.Create(int.MinValue).AsSingle()), multiply); 48 | // mul = Sse.Max(mul, value); 49 | // return Sse.And(mul, Vector128.Create(~int.MinValue).AsSingle()); 50 | //} 51 | 52 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 53 | public Vector128 Foo(Vector128 left, Vector128 right) 54 | { 55 | var comp = MVector.CompareEqual(left, right); 56 | var mul = MVector.Multiply(left, right); 57 | var sqrt = MVector.Sqrt(mul); 58 | 59 | return MVector.Select(comp, mul, sqrt); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/Vector/Single/SinBenchmark.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using System.Runtime.Intrinsics; 3 | using BenchmarkDotNet.Attributes; 4 | 5 | namespace MathSharp.Interactive.Benchmarks.Vector.Single 6 | { 7 | using Vector = MathSharp.Vector; 8 | 9 | public class SinBenchmark 10 | { 11 | private static readonly Vector4 MathFVector = new Vector4(1f, 2f, 3f, 4f); 12 | private static readonly Vector128 MathSharpVector = Vector128.Create(1f, 2f, 3f, 4f); 13 | 14 | [Benchmark] 15 | public Vector128 MathF() 16 | { 17 | return Vector128.Create( 18 | System.MathF.Sin(MathFVector.X), 19 | System.MathF.Sin(MathFVector.W), 20 | System.MathF.Sin(MathFVector.Z), 21 | System.MathF.Sin(MathFVector.W) 22 | ); 23 | } 24 | 25 | [Benchmark] 26 | public Vector128 MathSharp() 27 | { 28 | return Vector.Sin(MathSharpVector); 29 | } 30 | 31 | [Benchmark] 32 | public Vector128 MathSharp_Approx() 33 | { 34 | return Vector.SinApprox(MathSharpVector); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/Vector/Single/SinCosBenchmark.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using System.Runtime.Intrinsics; 3 | using BenchmarkDotNet.Attributes; 4 | 5 | namespace MathSharp.Interactive.Benchmarks.Vector.Single 6 | { 7 | using Vector = MathSharp.Vector; 8 | 9 | public class SinCosBenchmark 10 | { 11 | private static readonly Vector4 MathFVector = new Vector4(1f, 2f, 3f, 4f); 12 | private static readonly Vector128 MathSharpVector = Vector128.Create(1f, 2f, 3f, 4f); 13 | private static Vector128 _sin; 14 | private static Vector128 _cos; 15 | 16 | [Benchmark] 17 | public void MathF() 18 | { 19 | _sin = Vector128.Create( 20 | System.MathF.Sin(MathFVector.X), 21 | System.MathF.Sin(MathFVector.W), 22 | System.MathF.Sin(MathFVector.Z), 23 | System.MathF.Sin(MathFVector.W) 24 | ); 25 | 26 | _cos = Vector128.Create( 27 | System.MathF.Cos(MathFVector.X), 28 | System.MathF.Cos(MathFVector.W), 29 | System.MathF.Cos(MathFVector.Z), 30 | System.MathF.Cos(MathFVector.W) 31 | ); 32 | } 33 | 34 | [Benchmark] 35 | public void MathSharp() 36 | { 37 | Vector.SinCos(MathSharpVector, out _sin, out _cos); 38 | } 39 | 40 | [Benchmark] 41 | public void MathSharp_Approx() 42 | { 43 | Vector.SinCosApprox(MathSharpVector, out _sin, out _cos); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/Vector/Single/SineWaveBenchmark.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using OpenTK; 3 | using System; 4 | using System.Runtime.CompilerServices; 5 | using System.Runtime.InteropServices; 6 | using System.Runtime.Intrinsics; 7 | using static MathSharp.Vector; 8 | 9 | #nullable disable 10 | 11 | namespace MathSharp.Interactive.Benchmarks.Vector.Single 12 | { 13 | [RPlotExporter] 14 | public class SineWaveBenchmark 15 | { 16 | private const bool UseSinApprox = true; 17 | 18 | public const int SampleRate = 44100; 19 | private const float Tau = MathF.PI * 2; 20 | //The wave's frequency, A4. 21 | private const float Frequency = 440; 22 | 23 | public float[] AudioBufferNormal; 24 | public float[] AudioBufferVectorized; 25 | 26 | 27 | 28 | [GlobalSetup] 29 | public unsafe void Setup() 30 | { 31 | //We're going to generate a second of a pure sine. 32 | AudioBufferNormal = new float[SampleRate]; 33 | AudioBufferVectorized = new float[SampleRate]; 34 | 35 | Constants = (Vector128*)Marshal.AllocHGlobal(sizeof(Vector128) * 4); 36 | Constants[0] = Vector128.Create(0f, 1f, 2f, 3f); 37 | Constants[1] = Vector128.Create((float)SampleRate); 38 | Constants[2] = Vector128.Create(4f); 39 | Constants[3] = Vector128.Create(Tau * Frequency); 40 | } 41 | 42 | [Benchmark] 43 | public void SystemMathF() 44 | { 45 | var length = AudioBufferNormal.Length; 46 | for (int i = 0; i < length; i++) 47 | AudioBufferNormal[i] = MathF.Sin(Tau * Frequency * ((float)i / SampleRate)); 48 | } 49 | 50 | private unsafe Vector128* Constants; 51 | 52 | [Benchmark] 53 | public unsafe void MathSharp() 54 | { 55 | var length = SampleRate; 56 | var vecLength = SampleRate & ~3; 57 | 58 | var samplePoints = Constants[0]; 59 | var sampleRate = Constants[1]; 60 | var sampleIterator = Constants[2]; 61 | var sine = Constants[3]; 62 | 63 | var i = 0; 64 | 65 | //fixed (float* ptr = AudioBufferVectorized) // OLD 66 | fixed (float* ptr = &AudioBufferVectorized[0]) // NEW 67 | { 68 | while (i < vecLength) 69 | { 70 | Vector128 vector = Divide(samplePoints, sampleRate); 71 | vector = Multiply(vector, sine); 72 | 73 | vector = Sin(vector); 74 | vector.ToVector4D(&ptr[i]); 75 | 76 | i += 4; 77 | samplePoints = Add(samplePoints, sampleIterator); 78 | } 79 | 80 | if (i == length) return; 81 | 82 | var onesIterator = Vector128.CreateScalarUnsafe(1.0f); 83 | while (i < length) 84 | { 85 | Vector128 vector = Divide(samplePoints, sampleRate); 86 | vector = Multiply(vector, sine); 87 | 88 | vector = Sin(vector); 89 | 90 | Unsafe.Write(&ptr[i], vector.ToScalar()); 91 | 92 | i++; 93 | samplePoints = Add(samplePoints, onesIterator); 94 | } 95 | } 96 | 97 | } 98 | 99 | [GlobalCleanup] 100 | public unsafe void Cleanup() 101 | { 102 | Marshal.FreeHGlobal((IntPtr)Constants); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Benchmarks/Vector/Single/VectorAdditionBenchmark.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.Intrinsics; 2 | using BenchmarkDotNet.Attributes; 3 | using OpenTK; 4 | 5 | namespace MathSharp.Interactive.Benchmarks.Vector.Single 6 | { 7 | using SysVector4 = System.Numerics.Vector4; 8 | using Vector = MathSharp.Vector; 9 | 10 | [RPlotExporter] 11 | public class VectorAdditionBenchmark 12 | { 13 | private Vector4 _openTk1; 14 | private Vector4 _openTk2; 15 | 16 | private SysVector4 _sysNumerics1; 17 | private SysVector4 _sysNumerics2; 18 | 19 | private Vector128 _mathSharp1; 20 | private Vector128 _mathSharp2; 21 | 22 | [Benchmark] 23 | public Vector4 OpenTk() => _openTk1 + _openTk2; 24 | 25 | [Benchmark] 26 | public SysVector4 SystemNumerics() => _sysNumerics1 + _sysNumerics2; 27 | 28 | [Benchmark] 29 | public Vector128 MathSharp() => Vector.Add(_mathSharp1, _mathSharp2); 30 | } 31 | } -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/MathSharp.Interactive.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 1573;1591;NU1701;CS0649;CS1591; 5 | Exe 6 | netcoreapp3.1 7 | 8.0 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /samples/MathSharp.Interactive/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Globalization; 3 | using System.Linq; 4 | using System.Runtime.CompilerServices; 5 | using System.Runtime.InteropServices; 6 | using System.Runtime.Intrinsics; 7 | using System.Runtime.Intrinsics.X86; 8 | using MathSharp.StorageTypes; 9 | using MathSharp; 10 | 11 | using MathSharp.Interactive.Benchmarks.Vector.Single; 12 | 13 | using Vector4 = System.Numerics.Vector4; 14 | ======= 15 | using BenchmarkDotNet.Attributes; 16 | using System.Reflection; 17 | using BenchmarkDotNet.Running; 18 | 19 | namespace MathSharp.Interactive 20 | { 21 | internal unsafe class Program 22 | { 23 | public static void Main(string[] args) 24 | { 25 | BenchmarkRunner.Run(); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /scripts/azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | trigger: 2 | - master 3 | - dev 4 | 5 | pr: 6 | - master 7 | - dev 8 | 9 | jobs: 10 | - job: windows_debug_x86 11 | pool: 12 | name: Hosted 13 | demands: Cmd 14 | steps: 15 | - task: BatchScript@1 16 | displayName: 'Run scripts/cibuild.cmd' 17 | inputs: 18 | filename: scripts/cibuild.cmd 19 | arguments: '-configuration Debug -architecture x86' 20 | 21 | - job: windows_release_x86 22 | pool: 23 | name: Hosted 24 | demands: Cmd 25 | steps: 26 | - task: BatchScript@1 27 | displayName: 'Run scripts/cibuild.cmd' 28 | inputs: 29 | filename: scripts/cibuild.cmd 30 | arguments: '-configuration Release -architecture x86' 31 | 32 | - job: windows_debug_x64 33 | pool: 34 | name: Hosted 35 | demands: Cmd 36 | steps: 37 | - task: BatchScript@1 38 | displayName: 'Run scripts/cibuild.cmd' 39 | inputs: 40 | filename: scripts/cibuild.cmd 41 | arguments: '-configuration Debug -architecture x64' 42 | 43 | - job: windows_release_x64 44 | pool: 45 | name: Hosted 46 | demands: Cmd 47 | steps: 48 | - task: BatchScript@1 49 | displayName: 'Run scripts/cibuild.cmd' 50 | inputs: 51 | filename: scripts/cibuild.cmd 52 | arguments: '-configuration Release -architecture x64' 53 | 54 | - job: ubuntu_1604_debug_x64 55 | pool: 56 | name: Hosted Ubuntu 1604 57 | steps: 58 | - task: Bash@3 59 | displayName: 'Run scripts/cibuild.sh' 60 | inputs: 61 | targetType: filePath 62 | filePath: ./scripts/cibuild.sh 63 | arguments: '--configuration Debug --architecture x64' 64 | 65 | - job: ubuntu_1604_release_x64 66 | pool: 67 | name: Hosted Ubuntu 1604 68 | steps: 69 | - task: Bash@3 70 | displayName: 'Run scripts/cibuild.sh' 71 | inputs: 72 | targetType: filePath 73 | filePath: ./scripts/cibuild.sh 74 | arguments: '--configuration Release --architecture x64' 75 | 76 | - job: macOS_debug_x64 77 | pool: 78 | name: Hosted macOS 79 | steps: 80 | - task: Bash@3 81 | displayName: 'Run scripts/cibuild.sh' 82 | inputs: 83 | targetType: filePath 84 | filePath: ./scripts/cibuild.sh 85 | arguments: '--configuration Debug --architecture x64' 86 | 87 | - job: macOS_release_x64 88 | pool: 89 | name: Hosted macOS 90 | steps: 91 | - task: Bash@3 92 | displayName: 'Run scripts/cibuild.sh' 93 | inputs: 94 | targetType: filePath 95 | filePath: ./scripts/cibuild.sh 96 | arguments: '--configuration Release --architecture x64' 97 | -------------------------------------------------------------------------------- /scripts/cibuild.cmd: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | powershell.exe -NoLogo -NoProfile -ExecutionPolicy ByPass -Command "& """%~dp0build.ps1""" -ci '-p:BuildDocFx=false'" %*" 3 | EXIT /B %ERRORLEVEL% 4 | -------------------------------------------------------------------------------- /scripts/cibuild.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SOURCE="${BASH_SOURCE[0]}" 4 | while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink 5 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 6 | SOURCE="$(readlink "$SOURCE")" 7 | [[ $SOURCE != /* ]] && SOURCE="$ScriptRoot/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located 8 | done 9 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 10 | 11 | . "$ScriptRoot/build.sh" --ci -p:BuildDocFx=false "$@" -------------------------------------------------------------------------------- /scripts/old/azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | # Starter pipeline 2 | # Start with a minimal pipeline that you can customize to build and deploy your code. 3 | # Add steps that build, run tests, deploy, and more: 4 | # https://aka.ms/yaml 5 | 6 | trigger: 7 | - master 8 | - dev 9 | 10 | pr: 11 | - master 12 | - dev 13 | 14 | pool: 15 | vmImage: 'ubuntu-latest' 16 | 17 | variables: 18 | buildConfiguration: 'Release' 19 | 20 | steps: 21 | - task: DotNetCoreInstaller@0 22 | inputs: 23 | version: '3.0.0' 24 | - script: dotnet build --configuration $(buildConfiguration) 25 | displayName: 'dotnet build $(buildConfiguration)' 26 | - script: dotnet test --configuration $(buildConfiguration) -------------------------------------------------------------------------------- /scripts/old/build.ps1: -------------------------------------------------------------------------------- 1 | [CmdletBinding(PositionalBinding=$false)] 2 | Param( 3 | [ValidateSet("Debug", "Release")] [string]$configuration = "Debug", # default Debug 4 | [switch]$clean, 5 | [switch]$restore, 6 | [switch]$skipbuild, 7 | [switch]$help, 8 | [switch]$skiptests, 9 | [switch]$ci, 10 | [switch]$package, 11 | [ValidateSet("Any CPU", "x86", "x64", "ARM", "ARM64")] [string]$arch = "Any CPU", # default Any CPU, but not self contained 12 | [ValidateSet("quiet", "minimal", "normal", "detailed", "diagnostic")] [string]$verbosity = "minimal", 13 | [Parameter(ValueFromRemainingArguments=$true)] [string[]]$msbuildargs 14 | ) 15 | 16 | $RepoRoot = Join-Path -Path $PSScriptRoot -ChildPath ".." 17 | $solution = Join-Path -Path $RepoRoot -ChildPath "MathSharp.sln" 18 | $packageproject = [IO.Path]::Combine($RepoRoot, "sources", "MathSharp") 19 | 20 | function Write-Help() { 21 | Write-Host -Object "Switches and options: " 22 | Write-Host -Object " -clean Clean the solution" 23 | Write-Host -Object " -configuration [Debug|Release] Use Debug or Release mode when building" 24 | Write-Host -Object " -restore Restore all dependencies" 25 | Write-Host -Object " -skiptests Skip execution of tests - faster" 26 | Write-Host -Object " -help Print this text" 27 | Write-Host -Object " -arch [Any CPU|x86|x64|ARM|ARM64] Select the architecture to build for" 28 | Write-Host -Object "\n" 29 | Write-Host -Object "All other arguments are passed through to MSBuild" 30 | } 31 | 32 | function New-Library() { 33 | & dotnet build --no-restore -c "$configuration" -v "$verbosity" "$solution" $msbuildargs 34 | 35 | if ($LastExitCode -ne 0) { 36 | throw "'New-Library'' failed for '$solution'" 37 | } 38 | } 39 | 40 | function Test-Library() { 41 | & dotnet test --no-build --no-restore -c "$configuration" -v "$verbosity" "$solution" $msbuildargs 42 | 43 | if ($LastExitCode -ne 0) { 44 | throw "'Test-Library' failed for '$solution'" 45 | } 46 | } 47 | 48 | function Restore-Dependencies() { 49 | & dotnet restore -v "$verbosity" "$solution" $msbuildargs 50 | if ($LastExitCode -ne 0) { 51 | throw "'Restore-Dependencies' failed for '$solution'" 52 | } 53 | } 54 | 55 | function Clear-Artifacts() { 56 | & dotnet clean -c "$configuration" -v "$verbosity" "$solution" 57 | if ($LastExitCode -ne 0) { 58 | throw "'Clear-Artifacts' failed for '$solution'" 59 | } 60 | } 61 | 62 | function New-NugetPackage() { 63 | & dotnet pack -c "$configuration" -v "$verbosity" --no-build --no-restore "$packageproject" $msbuildargs 64 | 65 | if ($LastExitCode -ne 0) { 66 | throw "'New-NugetPackage' failed for '$solution'" 67 | } 68 | } 69 | 70 | Write-Output -Object "Build started..." 71 | try { 72 | if ($ci) { 73 | $restore=$true 74 | $skipbuild=$false 75 | $skiptests=$false 76 | $package=$true 77 | } 78 | 79 | if ($help) { 80 | Write-Help 81 | exit 0 82 | } 83 | 84 | if ($clean) { 85 | Clear-Artifacts 86 | } 87 | 88 | if ($restore) { 89 | Restore-Dependencies 90 | } 91 | 92 | if (!$skipbuild) { 93 | New-Library 94 | } 95 | 96 | if (!$skiptests) { 97 | Test-Library 98 | } 99 | 100 | if ($package) { 101 | New-NugetPackage 102 | } 103 | } 104 | catch { 105 | Write-Host -Object $_ 106 | Write-Host -Object $_.Exception 107 | Write-Host -Object $_.ScriptStackTrace 108 | exit 1 109 | } 110 | 111 | -------------------------------------------------------------------------------- /scripts/old/build.sh: -------------------------------------------------------------------------------- 1 | function Help { 2 | echo "Switches and options: " 3 | echo " -clean Clean the solution" 4 | echo " -configuration [Debug|Release] Use Debug or Release mode when building" 5 | echo " -restore Restore all dependencies" 6 | echo " -skiptests Skip execution of tests - faster" 7 | echo " -help Print this text" 8 | echo " -arch [Any CPU|x86|x64|ARM|ARM64] Select the architecture to build for" 9 | echo "\n" 10 | echo "All other arguments are passed through to MSBuild" 11 | } -------------------------------------------------------------------------------- /sources/Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | $(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.props 15 | sources 16 | true 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /sources/Directory.Build.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | $(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.targets 15 | 16 | 17 | 18 | 19 | 20 | $(IntermediateOutputPath)$(MSBuildProjectName).InternalsVisibleTo$(DefaultLanguageSourceExtension) 21 | 22 | 23 | 24 | 25 | false 26 | 27 | 28 | 29 | 33 | 34 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /sources/MathSharp/.gitignore: -------------------------------------------------------------------------------- 1 | ############### 2 | # folder # 3 | ############### 4 | /**/DROP/ 5 | /**/TEMP/ 6 | /**/packages/ 7 | /**/bin/ 8 | /**/obj/ 9 | _site 10 | -------------------------------------------------------------------------------- /sources/MathSharp/Attributes/AlignedAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MathSharp.Attributes 4 | { 5 | /// 6 | /// Indicates a type should be aligned on a certain boundary 7 | /// 8 | public class AlignedAttribute : Attribute 9 | { 10 | /// 11 | /// The ideal alignment for the type 12 | /// 13 | public int Alignment; 14 | 15 | /// 16 | /// Initializes a new instance of the class with a specified alignment "" 17 | /// 18 | /// The ideal alignment for the type, a power of 2 19 | public AlignedAttribute(int alignment) 20 | { 21 | Alignment = alignment; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sources/MathSharp/Attributes/HfaAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MathSharp.Attributes 4 | { 5 | // TODO for analyzer 6 | /// 7 | /// Indicates a type is desired to be a Homogeneous Float Aggregate type, 8 | /// which consists of up to and including 4 identical floating point types, either 9 | /// or 10 | /// 11 | [AttributeUsage(AttributeTargets.Struct)] 12 | public class HfaAttribute : Attribute 13 | { 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /sources/MathSharp/Attributes/HvaAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Intrinsics; 3 | 4 | namespace MathSharp.Attributes 5 | { 6 | // TODO for analyzer 7 | /// 8 | /// Indicates a type is desired to be a Homogeneous Vector Aggregate type, 9 | /// which consists of up to and including 4 identical vector types, 10 | /// where the vector types are , , and 11 | /// 12 | [AttributeUsage(AttributeTargets.Struct)] 13 | public class HvaAttribute : Attribute 14 | { 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sources/MathSharp/Constants/ScalarDoubleConstants.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MathSharp.Constants 4 | { 5 | /// 6 | /// constants used frequently in maths 7 | /// 8 | public static class ScalarDoubleConstants 9 | { 10 | /// 11 | /// Represents the ratio of the circumference of a circle to its diameter, specified by the constant, π 12 | /// 13 | public const double Pi = Math.PI; // for symmetry 14 | 15 | /// 16 | /// π multiplied by 2 17 | /// 18 | public const double Pi2 = Pi * 2d; 19 | 20 | /// 21 | /// The reciprocal of π, 1 divided by π 22 | /// 23 | public const double OneDivPi = 1d / Pi; 24 | 25 | /// 26 | /// The reciprocal of 2π, 1 divided by 2π 27 | /// 28 | public const double OneDiv2Pi = 1d / Pi2; 29 | 30 | /// 31 | /// π divided by 2 32 | /// 33 | public const double PiDiv2 = Pi / 2d; 34 | 35 | /// 36 | /// π divided by 4 37 | /// 38 | public const double PiDiv4 = Pi / 4d; 39 | 40 | /// 41 | /// 3π divided by 4 42 | /// 43 | public const double ThreePiDiv4 = 3d * Pi / 4d; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /sources/MathSharp/Constants/ScalarSingleConstants.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MathSharp.Constants 4 | { 5 | /// 6 | /// constants used frequently in maths 7 | /// 8 | public static class ScalarSingleConstants 9 | { 10 | /// 11 | /// Represents the ratio of the circumference of a circle to its diameter, specified by the constant, π 12 | /// 13 | public const float Pi = MathF.PI; // for symmetry 14 | 15 | /// 16 | /// π multiplied by 2 17 | /// 18 | public const float Pi2 = Pi * 2f; 19 | 20 | /// 21 | /// The reciprocal of π, 1 divided by π 22 | /// 23 | public const float OneDivPi = 1f / Pi; 24 | 25 | /// 26 | /// The reciprocal of 2π, 1 divided by 2π 27 | /// 28 | public const float OneDiv2Pi = 1f / Pi2; 29 | 30 | /// 31 | /// π divided by 2 32 | /// 33 | public const float PiDiv2 = Pi / 2f; 34 | 35 | /// 36 | /// π divided by 4 37 | /// 38 | public const float PiDiv4 = Pi / 4f; 39 | 40 | /// 41 | /// 3π divided by 4 42 | /// 43 | public const float ThreePiDiv4 = 3f * Pi / 4f; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /sources/MathSharp/MathSharp.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1 5 | false 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /sources/MathSharp/Matrix/MatrixSingle/Conversions.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable InconsistentNaming 2 | // ReSharper disable InvokeAsExtensionMethod 3 | 4 | namespace MathSharp 5 | { 6 | public static unsafe partial class Matrix 7 | { 8 | public static MatrixSingle FromMatrix4x4(float* p) 9 | { 10 | var v0 = Vector.FromVector4D(&p[0]); 11 | var v1 = Vector.FromVector4D(&p[4]); 12 | var v2 = Vector.FromVector4D(&p[8]); 13 | var v3 = Vector.FromVector4D(&p[12]); 14 | 15 | return new MatrixSingle(v0, v1, v2, v3); 16 | } 17 | 18 | public static void ToMatrix4x4(MatrixSingle matrix, float* destination) 19 | { 20 | Vector.ToVector4D(matrix._v0, &destination[0]); 21 | Vector.ToVector4D(matrix._v1, &destination[4]); 22 | Vector.ToVector4D(matrix._v2, &destination[8]); 23 | Vector.ToVector4D(matrix._v3, &destination[12]); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /sources/MathSharp/Quaternion/QuaternionConstants.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.Intrinsics; 2 | // ReSharper disable InconsistentNaming 3 | 4 | namespace MathSharp.Quaternion 5 | { 6 | public static partial class Quaternion 7 | { 8 | public static class SingleConstants 9 | { 10 | public static readonly Vector128 Identity = Vector128.Create(0f, 0f, 0f, 1f); 11 | 12 | public static readonly Vector128 MatrixIdentityRow0 = Vector128.Create(1f, 0f, 0f, 0f); 13 | 14 | public static readonly Vector128 SlerpEpsilon = Vector128.Create(1e-6f); 15 | public static readonly Vector128 OneMinusSlerpEpsilon = Vector128.Create(1 - 1e-6f); 16 | 17 | public static readonly Vector128 SignMaskXYZ = Vector128.Create(int.MinValue, int.MinValue, int.MinValue, 0).AsSingle(); 18 | public static readonly Vector128 SignMaskX = Vector128.Create(int.MinValue, 0, 0, 0).AsSingle(); 19 | } 20 | 21 | public static class DoubleConstants 22 | { 23 | public static readonly Vector256 Identity = Vector256.Create(0d, 0d, 0d, 1d); 24 | 25 | public static readonly Vector256 SlerpEpsilon = Vector256.Create(1e-6d); 26 | public static readonly Vector256 OneMinusSlerpEpsilon = Vector256.Create(1 - 1e-6d); 27 | 28 | 29 | public static readonly Vector256 SignMaskXYZ = Vector256.Create(long.MinValue, long.MinValue, long.MinValue, 0).AsDouble(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /sources/MathSharp/Quaternion/QuaternionStorageTypes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Intrinsics; 3 | using MathSharp.StorageTypes; 4 | 5 | namespace MathSharp.Quaternion 6 | { 7 | public readonly struct QuaternionF : IEquatable 8 | { 9 | public readonly float X, Y, Z, W; 10 | 11 | public QuaternionF(QuaternionF xyzw) => this = xyzw; 12 | 13 | public QuaternionF(float xyzw) : this(xyzw, xyzw, xyzw, xyzw) { } 14 | 15 | public QuaternionF(Vector2F xy, Vector2F zw) : this(xy.X, xy.Y, zw.X, zw.Y) { } 16 | 17 | public QuaternionF(Vector2F xy, float z, float w) : this(xy.X, xy.Y, z, w) { } 18 | 19 | public QuaternionF(Vector3F xyz, float w) : this(xyz.X, xyz.Y, xyz.Z, w) { } 20 | 21 | public QuaternionF(float x, float y, float z, float w) 22 | { 23 | X = x; 24 | Y = y; 25 | Z = z; 26 | W = w; 27 | } 28 | 29 | public override bool Equals(object? obj) 30 | => obj is QuaternionF other && Equals(other); 31 | 32 | public override int GetHashCode() => HashCode.Combine(X, Y, Z, W); 33 | 34 | public bool Equals(QuaternionF other) => X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z); 35 | 36 | public override unsafe string? ToString() 37 | { 38 | fixed (QuaternionF* p = &this) 39 | { 40 | return Vector.ToString(QuaternionExtensions.ToVector128(p), elemCount: 4); 41 | } 42 | } 43 | } 44 | 45 | public static unsafe partial class QuaternionExtensions 46 | { 47 | public static Vector128 ToVector128(QuaternionF* p) => Vector.FromVector4D((float*)p); 48 | 49 | public static void StoreToQuaternion(Vector128 quaternion, QuaternionF* destination) => Vector.ToVector4D(quaternion, &destination->X); 50 | 51 | public static void StoreToQuaternion(Vector128 quaternion, out QuaternionF destination) 52 | { 53 | fixed (QuaternionF* p = &destination) 54 | { 55 | StoreToQuaternion(quaternion, p); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /sources/MathSharp/Utils/Helpers.Common.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | namespace MathSharp.Utils 4 | { 5 | internal static partial class Helpers 6 | { 7 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 8 | public static float AsMaskSingle(bool val) => val ? AllBitsSetSingle : NoBitsSetSingle; 9 | 10 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 11 | public static double AsMaskDouble(bool val) => val ? AllBitsSetDouble : NoBitsSetDouble; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /sources/MathSharp/Utils/Helpers.Vector4D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Intrinsics; 3 | 4 | namespace MathSharp.Utils 5 | { 6 | internal static partial class Helpers 7 | { 8 | public static readonly double NoBitsSetDouble = 0d; 9 | public static readonly double AllBitsSetDouble = BitConverter.Int64BitsToDouble(-1); 10 | 11 | public static void GetLowHigh(Vector256 vector, out Vector128 low, out Vector128 high) where T : struct 12 | { 13 | low = vector.GetLower(); 14 | high = vector.GetUpper(); 15 | } 16 | 17 | public static Vector256 FromLowHigh(Vector128 low, Vector128 high) where T : struct 18 | => low.ToVector256Unsafe().WithUpper(high); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /sources/MathSharp/Utils/Helpers.Vector4F.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.Intrinsics; 4 | 5 | namespace MathSharp.Utils 6 | { 7 | internal static partial class Helpers 8 | { 9 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 10 | public static Vector256 DuplicateToVector256(Vector128 vector) 11 | { 12 | return Vector256.Create(vector, vector); 13 | } 14 | 15 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 16 | public static T X(Vector128 vector) where T : struct => vector.GetElement(0); 17 | 18 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 19 | public static T Y(Vector128 vector) where T : struct => vector.GetElement(1); 20 | 21 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 22 | public static T Z(Vector128 vector) where T : struct => vector.GetElement(2); 23 | 24 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 25 | public static T W(Vector128 vector) where T : struct => vector.GetElement(3); 26 | 27 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 28 | public static T X(Vector256 vector) where T : struct => vector.GetElement(0); 29 | 30 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 31 | public static T Y(Vector256 vector) where T : struct => vector.GetElement(1); 32 | 33 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 34 | public static T Z(Vector256 vector) where T : struct => vector.GetElement(2); 35 | 36 | [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] 37 | public static T W(Vector256 vector) where T : struct => vector.GetElement(3); 38 | 39 | public static readonly float NoBitsSetSingle = 0f; 40 | public static readonly float AllBitsSetSingle = BitConverter.Int32BitsToSingle(-1); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /sources/MathSharp/Utils/ThrowHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.CompilerServices; 3 | 4 | namespace MathSharp.Utils 5 | { 6 | internal static class ThrowHelper 7 | { 8 | [MethodImpl(MethodImplOptions.NoInlining)] 9 | public static void ThrowPlatformNotSupportedException(string message) 10 | => throw new PlatformNotSupportedException(message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/Options.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | namespace MathSharp 4 | { 5 | public static class Options 6 | { 7 | // Used to allow things like changing (x + y) * z to FMA(x, y, z) 8 | // or 1 / n to ReciprocalApprox(n) 9 | /// 10 | /// Indicates whether math used in methods is strict and IEEE compliant. 11 | /// 12 | public static bool StrictMath 13 | { 14 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 15 | get => false; 16 | } 17 | 18 | // Used to allow things like Max(a, b) discarding NaNs 19 | // ReSharper disable once InconsistentNaming 20 | /// 21 | /// Indicates whether behaviour around +inf, -inf, and NaN must be respected as IEEE dictates. 22 | /// 23 | public static bool StrictNonFiniteBehaviour 24 | { 25 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 26 | get => false; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/SoftwareFallbacks/BitOperations.T.SoftwareFallbacks.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | using System.Runtime.Intrinsics; 3 | 4 | namespace MathSharp 5 | { 6 | internal static partial class SoftwareFallbacks 7 | { 8 | #region Vector128 9 | 10 | [MethodImpl(MaxOpt)] 11 | public static Vector128 Or_Software(Vector128 left, Vector128 right) where T : struct 12 | => Or_Software(left.AsUInt64(), right.AsUInt64()).As(); 13 | 14 | [MethodImpl(MaxOpt)] 15 | public static Vector128 And_Software(Vector128 left, Vector128 right) where T : struct 16 | => And_Software(left.AsUInt64(), right.AsUInt64()).As(); 17 | 18 | [MethodImpl(MaxOpt)] 19 | public static Vector128 Xor_Software(Vector128 left, Vector128 right) where T : struct 20 | => Xor_Software(left.AsUInt64(), right.AsUInt64()).As(); 21 | 22 | [MethodImpl(MaxOpt)] 23 | public static Vector128 Not_Software(Vector128 vector) where T : struct 24 | => Not_Software(vector.AsUInt64()).As(); 25 | 26 | [MethodImpl(MaxOpt)] 27 | public static Vector128 AndNot_Software(Vector128 left, Vector128 right) where T : struct 28 | => AndNot_Software(left.AsUInt64(), right.AsUInt64()).As(); 29 | 30 | #endregion 31 | 32 | #region Vector256 33 | 34 | [MethodImpl(MaxOpt)] 35 | public static Vector256 Or_Software(Vector256 left, Vector256 right) where T : struct 36 | => Or_Software(left.AsUInt64(), right.AsUInt64()).As(); 37 | 38 | [MethodImpl(MaxOpt)] 39 | public static Vector256 And_Software(Vector256 left, Vector256 right) where T : struct 40 | => And_Software(left.AsUInt64(), right.AsUInt64()).As(); 41 | 42 | [MethodImpl(MaxOpt)] 43 | public static Vector256 Xor_Software(Vector256 left, Vector256 right) where T : struct 44 | => Xor_Software(left.AsUInt64(), right.AsUInt64()).As(); 45 | 46 | [MethodImpl(MaxOpt)] 47 | public static Vector256 Not_Software(Vector256 vector) where T : struct 48 | => Not_Software(vector.AsUInt64()).As(); 49 | 50 | [MethodImpl(MaxOpt)] 51 | public static Vector256 AndNot_Software(Vector256 left, Vector256 right) where T : struct 52 | => AndNot_Software(left.AsUInt64(), right.AsUInt64()).As(); 53 | 54 | #endregion 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/Vector.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | namespace MathSharp 4 | { 5 | public static partial class Vector 6 | { 7 | private const MethodImplOptions MaxOpt = 8 | MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/VectorDimensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics.CodeAnalysis; 2 | 3 | namespace MathSharp 4 | { 5 | [SuppressMessage("ReSharper", "InconsistentNaming")] 6 | public enum VectorDimensions 7 | { 8 | V1D = 1, 9 | V2D = 2, 10 | V3D = 3, 11 | V4D = 4 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/VectorFloatingPoint/VectorDouble/Casts.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.Intrinsics; 2 | using System.Runtime.Intrinsics.X86; 3 | using static MathSharp.Utils.Helpers; 4 | 5 | namespace MathSharp 6 | { 7 | public static partial class Vector 8 | { 9 | // Used by trig. Low quality because it narrows to Int32. 10 | internal static Vector256 ConvertToInt64(Vector256 vector) 11 | { 12 | if (Avx2.IsSupported) 13 | { 14 | return Avx2.ConvertToVector256Int64(Avx.ConvertToVector128Int32WithTruncation(vector)); 15 | } 16 | 17 | return SoftwareFallback(vector); 18 | 19 | static Vector256 SoftwareFallback(Vector256 vector) 20 | { 21 | return Vector256.Create( 22 | (long)X(vector), 23 | (long)Y(vector), 24 | (long)Z(vector), 25 | (long)W(vector) 26 | ); 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/VectorFloatingPoint/VectorDouble/Comparisons.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | using System.Runtime.Intrinsics; 3 | using System.Runtime.Intrinsics.X86; 4 | using static MathSharp.SoftwareFallbacks; 5 | 6 | namespace MathSharp 7 | { 8 | 9 | 10 | 11 | public static partial class Vector 12 | { 13 | [MethodImpl(MaxOpt)] 14 | public static Vector256 CompareEqual(Vector256 left, Vector256 right) 15 | { 16 | if (Avx.IsSupported) 17 | { 18 | return Avx.Compare(left, right, FloatComparisonMode.OrderedEqualNonSignaling); 19 | } 20 | 21 | return CompareEqual_Software(left, right); 22 | } 23 | 24 | 25 | [MethodImpl(MaxOpt)] 26 | public static Vector256 CompareNotEqual(Vector256 left, Vector256 right) 27 | { 28 | if (Avx.IsSupported) 29 | { 30 | return Avx.Compare(left, right, FloatComparisonMode.UnorderedNotEqualNonSignaling); 31 | } 32 | 33 | return CompareNotEqual_Software(left, right); 34 | } 35 | 36 | 37 | [MethodImpl(MaxOpt)] 38 | public static Vector256 CompareGreaterThan(Vector256 left, Vector256 right) 39 | { 40 | if (Avx.IsSupported) 41 | { 42 | return Avx.Compare(left, right, FloatComparisonMode.UnorderedNotLessThanOrEqualNonSignaling); 43 | } 44 | 45 | return CompareGreaterThan_Software(left, right); 46 | } 47 | 48 | 49 | [MethodImpl(MaxOpt)] 50 | public static Vector256 CompareLessThan(Vector256 left, Vector256 right) 51 | { 52 | if (Avx.IsSupported) 53 | { 54 | return Avx.Compare(left, right, FloatComparisonMode.OrderedLessThanSignaling); 55 | } 56 | 57 | return CompareLessThan_Software(left, right); 58 | } 59 | 60 | 61 | [MethodImpl(MaxOpt)] 62 | public static Vector256 CompareGreaterThanOrEqual(Vector256 left, Vector256 right) 63 | { 64 | if (Avx.IsSupported) 65 | { 66 | return Avx.Compare(left, right, FloatComparisonMode.UnorderedNotLessThanSignaling); 67 | } 68 | 69 | return CompareGreaterThanOrEqual_Software(left, right); 70 | } 71 | 72 | 73 | [MethodImpl(MaxOpt)] 74 | public static Vector256 CompareLessThanOrEqual(Vector256 left, Vector256 right) 75 | { 76 | if (Avx.IsSupported) 77 | { 78 | return Avx.Compare(left, right, FloatComparisonMode.OrderedLessThanOrEqualSignaling); 79 | } 80 | 81 | return CompareLessThanOrEqual_Software(left, right); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/VectorFloatingPoint/VectorDouble/Logical.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | using System.Runtime.Intrinsics; 3 | using System.Runtime.Intrinsics.X86; 4 | using MathSharp.Utils; 5 | using static MathSharp.SoftwareFallbacks; 6 | using static MathSharp.Utils.Helpers; 7 | 8 | namespace MathSharp 9 | { 10 | 11 | 12 | 13 | public static partial class Vector 14 | { 15 | [MethodImpl(MaxOpt)] 16 | public static Vector256 Shuffle(Vector256 vector, byte control) 17 | { 18 | if (Avx2.IsSupported) 19 | { 20 | return Avx2.Permute4x64(vector, control); 21 | } 22 | 23 | return Shuffle(vector, vector, control); 24 | } 25 | 26 | [MethodImpl(MaxOpt)] 27 | public static Vector256 FillWithX(Vector256 vector) 28 | => Shuffle(vector, ShuffleValues.X); 29 | 30 | [MethodImpl(MaxOpt)] 31 | public static Vector256 FillWithY(Vector256 vector) 32 | => Shuffle(vector, ShuffleValues.Y); 33 | 34 | [MethodImpl(MaxOpt)] 35 | public static Vector256 FillWithZ(Vector256 vector) 36 | => Shuffle(vector, ShuffleValues.Z); 37 | 38 | [MethodImpl(MaxOpt)] 39 | public static Vector256 FillWithW(Vector256 vector) 40 | => Shuffle(vector, ShuffleValues.W); 41 | 42 | [MethodImpl(MaxOpt)] 43 | public static Vector256 Shuffle(Vector256 left, Vector256 right, byte control) 44 | { 45 | //There is a way to do Permute4x64 with a few AVX instructions but haven't figured it out yet 46 | //if (Avx.IsSupported) 47 | //{ 48 | // return Avx.Shuffle(left, right, control); 49 | //} 50 | 51 | return Shuffle_Software(left, right, control); 52 | } 53 | 54 | [MethodImpl(MaxOpt)] 55 | public static byte MoveMask(Vector256 vector) 56 | { 57 | if (Avx.IsSupported) 58 | { 59 | return (byte)Avx.MoveMask(vector); 60 | } 61 | 62 | return SoftwareFallback(vector); 63 | 64 | static byte SoftwareFallback(Vector256 vector) 65 | { 66 | double s0 = X(vector); 67 | double s1 = Y(vector); 68 | double s2 = Z(vector); 69 | double s3 = W(vector); 70 | 71 | long e0 = Unsafe.As(ref s0); 72 | long e1 = Unsafe.As(ref s1); 73 | long e2 = Unsafe.As(ref s2); 74 | long e3 = Unsafe.As(ref s3); 75 | 76 | return (byte)(SignAsByteBool(e0) | (SignAsByteBool(e1) << 1) | (SignAsByteBool(e2) << 2) | (SignAsByteBool(e3) << 3)); 77 | } 78 | 79 | static byte SignAsByteBool(long i) => i < 0 ? (byte)1 : (byte)0; 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/VectorFloatingPoint/VectorSingle/Casts.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.Intrinsics; 3 | using System.Runtime.Intrinsics.X86; 4 | using static MathSharp.Utils.Helpers; 5 | 6 | namespace MathSharp 7 | { 8 | public static partial class Vector 9 | { 10 | /// 11 | /// Converts each element of a to the equivalent . 12 | /// Equivalent to a per-element C#-style cast 13 | /// 14 | /// The vector of s 15 | /// A vector that contains each element of casted to a 16 | public static Vector128 ConvertToSingle(Vector128 vector) 17 | { 18 | if (Sse2.IsSupported) 19 | { 20 | return Sse2.ConvertToVector128Single(vector); 21 | } 22 | 23 | return SoftwareFallback(vector); 24 | 25 | static Vector128 SoftwareFallback(Vector128 vector) 26 | { 27 | return Vector128.Create( 28 | (float)X(vector), 29 | (float)Y(vector), 30 | (float)Z(vector), 31 | (float)W(vector) 32 | ); 33 | } 34 | } 35 | 36 | /// 37 | /// Converts each element of a to the equivalent . 38 | /// Equivalent to a per-element C#-style cast 39 | /// 40 | /// The vector of s 41 | /// A vector that contains each element of casted to a 42 | public static Vector128 ConvertToInt32(Vector128 vector) 43 | { 44 | if (Sse2.IsSupported) 45 | { 46 | return Sse2.ConvertToVector128Int32WithTruncation(vector); 47 | } 48 | 49 | return SoftwareFallback(vector); 50 | 51 | static Vector128 SoftwareFallback(Vector128 vector) 52 | { 53 | return Vector128.Create( 54 | (int)X(vector), 55 | (int)Y(vector), 56 | (int)Z(vector), 57 | (int)W(vector) 58 | ); 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/VectorFloatingPoint/VectorSingle/Comparisons.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Diagnostics.CodeAnalysis; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using System.Runtime.Intrinsics.X86; 6 | using static MathSharp.SoftwareFallbacks; 7 | using static MathSharp.Utils.Helpers; 8 | 9 | namespace MathSharp 10 | { 11 | public static partial class Vector 12 | { 13 | [MethodImpl(MaxOpt)] 14 | public static Vector128 CompareEqual(Vector128 left, Vector128 right) 15 | { 16 | if (Sse.IsSupported) 17 | { 18 | return Sse.CompareEqual(left, right); 19 | } 20 | 21 | return CompareEqual_Software(left, right); 22 | } 23 | 24 | [MethodImpl(MaxOpt)] 25 | public static Vector256 CompareEqual(Vector256 left, Vector256 right) 26 | { 27 | if (Avx.IsSupported) 28 | { 29 | return Avx.Compare(left, right, FloatComparisonMode.UnorderedEqualNonSignaling); 30 | } 31 | 32 | return FromLowHigh(CompareEqual(left.GetLower(), right.GetLower()), 33 | CompareEqual(left.GetUpper(), right.GetUpper())); 34 | } 35 | 36 | [MethodImpl(MaxOpt)] 37 | public static Vector128 CompareNotEqual(Vector128 left, Vector128 right) 38 | { 39 | if (Sse.IsSupported) 40 | { 41 | return Sse.CompareNotEqual(left, right); 42 | } 43 | 44 | return CompareNotEqual_Software(left, right); 45 | } 46 | 47 | 48 | [MethodImpl(MaxOpt)] 49 | public static Vector128 CompareGreaterThan(Vector128 left, Vector128 right) 50 | { 51 | if (Sse.IsSupported) 52 | { 53 | return Sse.CompareGreaterThan(left, right); 54 | } 55 | 56 | return CompareGreaterThan_Software(left, right); 57 | } 58 | 59 | 60 | [MethodImpl(MaxOpt)] 61 | public static Vector128 CompareLessThan(Vector128 left, Vector128 right) 62 | { 63 | if (Sse.IsSupported) 64 | { 65 | return Sse.CompareLessThan(left, right); 66 | } 67 | 68 | return CompareLessThan_Software(left, right); 69 | } 70 | 71 | 72 | [MethodImpl(MaxOpt)] 73 | public static Vector128 CompareGreaterThanOrEqual(Vector128 left, Vector128 right) 74 | { 75 | if (Sse.IsSupported) 76 | { 77 | return Sse.CompareGreaterThanOrEqual(left, right); 78 | } 79 | 80 | return CompareGreaterThanOrEqual_Software(left, right); 81 | } 82 | 83 | 84 | [MethodImpl(MaxOpt)] 85 | public static Vector128 CompareLessThanOrEqual(Vector128 left, Vector128 right) 86 | { 87 | if (Sse.IsSupported) 88 | { 89 | return Sse.CompareLessThanOrEqual(left, right); 90 | } 91 | 92 | return CompareLessThanOrEqual_Software(left, right); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/VectorFloatingPoint/VectorSingle/Conversions.SystemNumerics.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.Intrinsics; 4 | 5 | namespace MathSharp 6 | { 7 | public static unsafe partial class Vector 8 | { 9 | [MethodImpl(MaxOpt)] 10 | public static Vector128 Load(this in Vector4 vector) 11 | { 12 | return FromVector4D(in vector.X); 13 | } 14 | 15 | [MethodImpl(MaxOpt)] 16 | public static Vector128 Load(this in Vector3 vector) 17 | { 18 | return FromVector3DAligned(in vector.X); // Vector3 is special cased to be 16 bytes on the stack to allow this 19 | } 20 | 21 | [MethodImpl(MaxOpt)] 22 | public static Vector128 Load(this in Vector2 vector) 23 | { 24 | return FromVector2D(in vector.X); 25 | } 26 | 27 | public static void Store(this Vector128 vector, out Vector4 destination) 28 | { 29 | fixed (void* p = &destination) 30 | { 31 | ToVector4D(vector, (float*)p); 32 | } 33 | } 34 | 35 | public static void Store(this Vector128 vector, out Vector3 destination) 36 | { 37 | fixed (void* p = &destination) 38 | { 39 | ToVector3D(vector, (float*)p); 40 | } 41 | } 42 | 43 | public static void Store(this Vector128 vector, out Vector2 destination) 44 | { 45 | fixed (void* p = &destination) 46 | { 47 | ToVector2D(vector, (float*)p); 48 | } 49 | } 50 | 51 | public static void Store(this Vector128 vector, out float destination) 52 | { 53 | StoreScalar(vector, out destination); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /sources/MathSharp/Vector/VectorFloatingPoint/VectorSingle/Logical.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | using System.Runtime.Intrinsics; 3 | using System.Runtime.Intrinsics.X86; 4 | using MathSharp.Utils; 5 | using static MathSharp.SoftwareFallbacks; 6 | using static MathSharp.Utils.Helpers; 7 | 8 | namespace MathSharp 9 | { 10 | public static partial class Vector 11 | { 12 | [MethodImpl(MaxOpt)] 13 | public static float GetX(Vector128 vector) => vector.GetElement(0); 14 | 15 | [MethodImpl(MaxOpt)] 16 | public static float GetY(Vector128 vector) => vector.GetElement(1); 17 | 18 | [MethodImpl(MaxOpt)] 19 | public static float GetZ(Vector128 vector) => vector.GetElement(2); 20 | 21 | [MethodImpl(MaxOpt)] 22 | public static float GetW(Vector128 vector) => vector.GetElement(3); 23 | 24 | [MethodImpl(MaxOpt)] 25 | public static Vector128 Shuffle(Vector128 vector, byte control) 26 | { 27 | if (Avx.IsSupported) 28 | { 29 | return Avx.Permute(vector, control); 30 | } 31 | 32 | return Shuffle(vector, vector, control); 33 | } 34 | 35 | [MethodImpl(MaxOpt)] 36 | public static Vector128 FillWithX(Vector128 vector) 37 | => Shuffle(vector, ShuffleValues.XXXX); 38 | 39 | [MethodImpl(MaxOpt)] 40 | public static Vector128 FillWithY(Vector128 vector) 41 | => Shuffle(vector, ShuffleValues.YYYY); 42 | 43 | [MethodImpl(MaxOpt)] 44 | public static Vector128 FillWithZ(Vector128 vector) 45 | => Shuffle(vector, ShuffleValues.ZZZZ); 46 | 47 | [MethodImpl(MaxOpt)] 48 | public static Vector128 FillWithW(Vector128 vector) 49 | => Shuffle(vector, ShuffleValues.WWWW); 50 | 51 | [MethodImpl(MaxOpt)] 52 | public static Vector128 Shuffle(Vector128 left, Vector128 right, byte control) 53 | { 54 | if (Sse.IsSupported) 55 | { 56 | return Sse.Shuffle(left, right, control); 57 | } 58 | 59 | return Shuffle_Software(left, right, control); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /test.cmd: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | powershell.exe -NoLogo -NoProfile -ExecutionPolicy ByPass -Command "& """%~dp0scripts\build.ps1""" -test %*" 3 | EXIT /B %ERRORLEVEL% 4 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SOURCE="${BASH_SOURCE[0]}" 4 | while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink 5 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 6 | SOURCE="$(readlink "$SOURCE")" 7 | [[ $SOURCE != /* ]] && SOURCE="$ScriptRoot/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located 8 | done 9 | ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 10 | 11 | . "$ScriptRoot/scripts/build.sh" --test "$@" 12 | -------------------------------------------------------------------------------- /tests/Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | $(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.props 15 | tests 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /tests/Directory.Build.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | $(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.targets 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/MathSharp.UnitTests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1 5 | false 6 | 7 | 8 | 9 | NU1701;1702;xUnit1019 10 | 11 | 12 | 13 | NU1701;1702;xUnit1019 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/MathSharp.UnitTests.csproj.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | True -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Abs.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 7 | { 8 | public class Abs 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), new Vector4d(0d) }, 14 | new object[] { Vector256.Create(1d), new Vector4d(1d) }, 15 | new object[] { Vector256.Create(-1d), new Vector4d(1d) }, 16 | new object[] { Vector256.Create(-1d, 0d, -1d, 0d), new Vector4d(1d, 0d, 1d, 0d) }, 17 | new object[] { Vector256.Create(double.NegativeInfinity), new Vector4d(double.PositiveInfinity) }, 18 | new object[] { Vector256.Create(double.NaN, -111d, -0.0000001d, 0.0000001d), new Vector4d(double.NaN, 111d, 0.0000001d, 0.0000001d) }, 19 | }; 20 | 21 | [Theory] 22 | [MemberData(nameof(Data))] 23 | public void Abs_Theory(Vector256 vector, Vector4d expected) 24 | { 25 | Vector256 result = Vector.Abs(vector); 26 | 27 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Add.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 7 | { 8 | public class Add 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new Vector4d(0d + 0d) }, 14 | new object[] { Vector256.Create(1d), Vector256.Create(1d), new Vector4d(1d + 1d) }, 15 | new object[] { Vector256.Create(-1d), Vector256.Create(1d), new Vector4d(-1d + 1d) }, 16 | new object[] { Vector256.Create(-1d, 0d, -1d, 0d), Vector256.Create(1d, 10d, 1d, 10d), new Vector4d(-1d + 1d, 0d + 10d, -1d + 1d, 0d + 10d) }, 17 | new object[] { Vector256.Create(double.NegativeInfinity), Vector256.Create(double.PositiveInfinity), new Vector4d(double.NegativeInfinity + double.PositiveInfinity) }, 18 | new object[] { Vector256.Create(double.MinValue), Vector256.Create(double.MaxValue), new Vector4d(double.MinValue + double.MaxValue) }, 19 | }; 20 | 21 | [Theory] 22 | [MemberData(nameof(Data))] 23 | public void Add_Theory(Vector256 left, Vector256 right, Vector4d expected) 24 | { 25 | Vector256 result = Vector.Add(left, right); 26 | 27 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Clamp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using OpenTK; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 8 | { 9 | public class Clamp 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector256.Create(0d), Vector256.Create(0d), Vector256.Create(0d), new Vector4d(Math.Clamp(0d, 0d, 0d)) }, 15 | new object[] { Vector256.Create(1d), Vector256.Create(0d), Vector256.Create(1d), new Vector4d(Math.Clamp(1d, 0d, 1d)) }, 16 | new object[] { Vector256.Create(1d), Vector256.Create(10d), Vector256.Create(110d), new Vector4d(Math.Clamp(1d, 10d, 110d)) }, 17 | new object[] { Vector256.Create(double.NegativeInfinity), Vector256.Create(0d), Vector256.Create(1d), new Vector4d(Math.Clamp(double.NegativeInfinity, 0d, 1d)) }, 18 | new object[] { Vector256.Create(double.PositiveInfinity), Vector256.Create(0d), Vector256.Create(100d), new Vector4d(Math.Clamp(double.PositiveInfinity, 0d, 100d)) }, 19 | }; 20 | 21 | [Theory] 22 | [MemberData(nameof(Data))] 23 | public void Clamp_Theory(Vector256 vector, Vector256 low, Vector256 high, Vector4d expected) 24 | { 25 | Vector256 result = Vector.Clamp(vector, low, high); 26 | 27 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Divide.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 7 | { 8 | public class Divide 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new Vector4d(0d / 0d) }, 14 | new object[] { Vector256.Create(1d), Vector256.Create(1d), new Vector4d(1d / 1d) }, 15 | new object[] { Vector256.Create(-1d), Vector256.Create(1d), new Vector4d(-1d / 1d) }, 16 | new object[] { Vector256.Create(-1d, 0d, -1d, 0d), Vector256.Create(1d, 10d, 1d, 10d), new Vector4d(-1d / 1d, 0d / 10d, -1d / 1d, 0d / 10d) }, 17 | new object[] { Vector256.Create(double.NegativeInfinity), Vector256.Create(double.PositiveInfinity), new Vector4d(double.NegativeInfinity / double.PositiveInfinity) }, 18 | new object[] { Vector256.Create(double.MinValue), Vector256.Create(double.MaxValue), new Vector4d(double.MinValue / double.MaxValue) }, 19 | }; 20 | 21 | [Theory] 22 | [MemberData(nameof(Data))] 23 | public void Divide_Theory(Vector256 left, Vector256 right, Vector4d expected) 24 | { 25 | Vector256 result = Vector.Divide(left, right); 26 | 27 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/HorizontalAdd.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 7 | { 8 | public class HorizontalAdd 9 | { 10 | public static IEnumerable Data 11 | { 12 | get 13 | { 14 | static Vector4d HorizontalAdd(Vector4d left, Vector4d right) 15 | { 16 | return new Vector4d(left.X + left.Y, right.X + right.Y, left.Z + left.W, right.Z + right.W); 17 | } 18 | 19 | static Vector4d HorizontalAddD(double left, double right) 20 | { 21 | return HorizontalAdd(new Vector4d(left), new Vector4d(right)); 22 | } 23 | 24 | return new[] 25 | { 26 | new object[] 27 | { 28 | Vector256.Create(0d), Vector256.Create(0d), 29 | HorizontalAddD(0d, 0d) 30 | }, 31 | new object[] 32 | { 33 | Vector256.Create(1d), Vector256.Create(1d), 34 | HorizontalAddD(1d, 1d) 35 | }, 36 | new object[] 37 | { 38 | Vector256.Create(10d, -10d, double.MinValue, double.MaxValue), Vector256.Create(0.0005d, 0.1d, 2343242d, -123123123123d), 39 | HorizontalAdd(new Vector4d(10d, -10d, double.MinValue, double.MaxValue), new Vector4d(0.0005d, 0.1d, 2343242d, -123123123123d)) 40 | }, 41 | }; 42 | } 43 | } 44 | 45 | 46 | [Theory] 47 | [MemberData(nameof(Data))] 48 | public void HorizontalAdd_Theory(Vector256 left, Vector256 right, Vector4d expected) 49 | { 50 | Vector256 result = Vector.HorizontalAdd(left, right); 51 | 52 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Max.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using OpenTK; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 8 | { 9 | public class Max 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector256.Create(0d), Vector256.Create(1d), new Vector4d(Math.Max(0d, 1d)) }, 15 | new object[] { Vector256.Create(1d), Vector256.Create(-1d), new Vector4d(Math.Max(1d, 0d)) }, 16 | new object[] { Vector256.Create(0.99999999999999999999999999999999999999999d), Vector256.Create(1d), new Vector4d(Math.Max(0.99999999999999999999999999999999999999999d, 1d)) }, 17 | new object[] { Vector256.Create(double.NegativeInfinity), Vector256.Create(double.PositiveInfinity), new Vector4d(Math.Max(double.NegativeInfinity, double.PositiveInfinity)) }, 18 | new object[] { Vector256.Create(double.NaN), Vector256.Create(0d), new Vector4d(0d) }, 19 | }; 20 | 21 | [Theory] 22 | [MemberData(nameof(Data))] 23 | public void Max_Theory(Vector256 left, Vector256 right, Vector4d expected) 24 | { 25 | Vector256 result = Vector.Max(left, right); 26 | 27 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Min.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using OpenTK; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 8 | { 9 | public class Min 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector256.Create(0d), Vector256.Create(1d), new Vector4d(Math.Min(0d, 1d)) }, 15 | new object[] { Vector256.Create(1d), Vector256.Create(-1d), new Vector4d(Math.Min(1d, -1d)) }, 16 | new object[] { Vector256.Create(0.99999999999999999999999999999999999999999d), Vector256.Create(1d), new Vector4d(Math.Min(0.99999999999999999999999999999999999999999d, 1d)) }, 17 | new object[] { Vector256.Create(double.NegativeInfinity), Vector256.Create(double.PositiveInfinity), new Vector4d(Math.Min(double.NegativeInfinity, double.PositiveInfinity)) }, 18 | new object[] { Vector256.Create(double.NaN), Vector256.Create(0d), new Vector4d(0d) }, 19 | }; 20 | 21 | [Theory] 22 | [MemberData(nameof(Data))] 23 | public void Min_Theory(Vector256 left, Vector256 right, Vector4d expected) 24 | { 25 | Vector256 result = Vector.Min(left, right); 26 | 27 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Multiply.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 7 | { 8 | public class Multiply 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new Vector4d(0d * 0d) }, 14 | new object[] { Vector256.Create(1d), Vector256.Create(1d), new Vector4d(1d * 1d) }, 15 | new object[] { Vector256.Create(-1d), Vector256.Create(1d), new Vector4d(-1d * 1d) }, 16 | new object[] { Vector256.Create(-1d, 0d, -1d, 0d), Vector256.Create(1d, 10d, 1d, 10d), new Vector4d(-1d * 1d, 0d * 10d, -1d * 1d, 0d * 10d) }, 17 | new object[] { Vector256.Create(double.NegativeInfinity), Vector256.Create(double.PositiveInfinity), new Vector4d(double.NegativeInfinity * double.PositiveInfinity) }, 18 | new object[] { Vector256.Create(double.MinValue), Vector256.Create(double.MaxValue), new Vector4d(double.MinValue * double.MaxValue) }, 19 | }; 20 | 21 | [Theory] 22 | [MemberData(nameof(Data))] 23 | public void Multiply_Theory(Vector256 left, Vector256 right, Vector4d expected) 24 | { 25 | Vector256 result = Vector.Multiply(left, right); 26 | 27 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Negate.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 7 | { 8 | public class Negate 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(1d), new Vector4d(-1d) }, 14 | new object[] { Vector256.Create(-1d), new Vector4d(1d) }, 15 | new object[] { Vector256.Create(-1d, double.NegativeInfinity, -1d, -0.00000000000000000001d), new Vector4d(1d, double.PositiveInfinity, 1d, 0.00000000000000000001d) }, 16 | new object[] { Vector256.Create(double.NegativeInfinity), new Vector4d(double.PositiveInfinity) }, 17 | }; 18 | 19 | [Theory] 20 | [MemberData(nameof(Data))] 21 | public void Negate_Theory(Vector256 vector, Vector4d expected) 22 | { 23 | Vector256 result = Vector.Negate(vector); 24 | 25 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 26 | } 27 | 28 | [Fact] 29 | public void Negate_NegateZero_Passes() 30 | { 31 | Vector256 result = Vector.Negate(Vector256.Create(0d)); 32 | 33 | Vector256 expected = Vector256.Create(long.MinValue); 34 | 35 | Assert.True(result.AsInt64().Equals(expected), $"Expected {expected}, got {result}"); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Remainder.cs: -------------------------------------------------------------------------------- 1 | using OpenTK; 2 | using System.Collections.Generic; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 6 | { 7 | public class Remainder 8 | { 9 | public static IEnumerable Data => 10 | new[] 11 | { 12 | new object[] { new Vector4d(10.4f), new Vector4d(2f) }, 13 | new object[] { new Vector4d(-10.4f), new Vector4d(2f) }, 14 | new object[] { new Vector4d(float.NaN, float.MinValue, float.MaxValue, float.PositiveInfinity), new Vector4d(3f) }, 15 | new object[] { new Vector4d(float.NaN, float.MinValue, float.MaxValue, float.PositiveInfinity), new Vector4d(2.4f) }, 16 | new object[] { new Vector4d(23.45f), new Vector4d(219f) }, 17 | new object[] { new Vector4d(12), new Vector4d(float.NaN) }, 18 | new object[] { new Vector4d(-10000000f), new Vector4d(2.2f) }, 19 | new object[] { new Vector4d(10000000f), new Vector4d(2.2f) }, 20 | new object[] { new Vector4d(float.NegativeInfinity), new Vector4d(3f) } 21 | }; 22 | 23 | [Theory] 24 | [MemberData(nameof(Data))] 25 | public unsafe void Remainder_Theory(Vector4d left, Vector4d right) 26 | { 27 | var l = Vector.FromVector4D(&left.X); 28 | var r = Vector.FromVector4D(&right.X); 29 | 30 | var remainder = Vector.Remainder(l, r); 31 | var expected = new Vector4d(left.X % right.X, left.Y % right.Y, left.Z % right.Z, left.W % right.W); 32 | 33 | TestHelpers.AreEqual(expected, remainder); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Sqrt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using OpenTK; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 8 | { 9 | public class Sqrt 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector256.Create(0d), new Vector4d(Math.Sqrt(0d)) }, 15 | new object[] { Vector256.Create(1d), new Vector4d(Math.Sqrt(1d)) }, 16 | new object[] { Vector256.Create(-1d), new Vector4d(Math.Sqrt(-1)) }, 17 | new object[] { Vector256.Create(1d, 4d, 9d, 16d), new Vector4d(Math.Sqrt(1d), Math.Sqrt(4d), Math.Sqrt(9d), Math.Sqrt(16d)) }, 18 | new object[] { Vector256.Create(0.5d, 111d, -0.0000001d, 0.0000001d), new Vector4d(Math.Sqrt(0.5d), Math.Sqrt(111d), Math.Sqrt(-0.0000001d), Math.Sqrt(0.0000001d)) }, 19 | }; 20 | 21 | [Theory] 22 | [MemberData(nameof(Data))] 23 | public void Sqrt_Theory(Vector256 vector, Vector4d expected) 24 | { 25 | Vector256 result = Vector.Sqrt(vector); 26 | 27 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BasicMathsTests/Subtract.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BasicMathsTests 7 | { 8 | public class Subtract 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new Vector4d(0d - 0d) }, 14 | new object[] { Vector256.Create(1d), Vector256.Create(1d), new Vector4d(1d - 1d) }, 15 | new object[] { Vector256.Create(-1d), Vector256.Create(1d), new Vector4d(-1d - 1d) }, 16 | new object[] { Vector256.Create(-1d, 0d, -1d, 0d), Vector256.Create(1d, 10d, 1d, 10d), new Vector4d(-1d - 1d, 0d - 10d, -1d - 1d, 0d - 10d) }, 17 | new object[] { Vector256.Create(double.NegativeInfinity), Vector256.Create(double.PositiveInfinity), new Vector4d(double.NegativeInfinity - double.PositiveInfinity) }, 18 | new object[] { Vector256.Create(double.MinValue), Vector256.Create(double.MaxValue), new Vector4d(double.MinValue - double.MaxValue) }, 19 | }; 20 | 21 | [Theory] 22 | [MemberData(nameof(Data))] 23 | public void Subtract_Theory(Vector256 left, Vector256 right, Vector4d expected) 24 | { 25 | Vector256 result = Vector.Subtract(left, right); 26 | 27 | Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BitOperationsTests/And.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BitOperationsTests 7 | { 8 | public class AndTests 9 | { 10 | public static readonly double AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1)); 11 | 12 | public static IEnumerable Data 13 | { 14 | get 15 | { 16 | static double AndF(double a, double b) 17 | { 18 | ulong and = Unsafe.As(ref a) & Unsafe.As(ref b); 19 | return Unsafe.As(ref and); 20 | } 21 | 22 | return new[] 23 | { 24 | new object[] { Vector256.Create(0d), Vector256.Create(0d), Vector256.Create(0d) }, 25 | new object[] { Vector256.Create(AllBitsSet), Vector256.Create(AllBitsSet), Vector256.Create(AllBitsSet) }, 26 | new object[] { Vector256.Create(235434d, -123d, 0, double.MaxValue), Vector256.Create(235434d, -123d, 0, double.MaxValue), Vector256.Create(235434d, -123d, 0, double.MaxValue) }, 27 | new object[] 28 | { 29 | Vector256.Create(0d, double.MinValue, double.PositiveInfinity, 1414123d), Vector256.Create(double.NaN, -0.00000000023434d, double.NegativeInfinity, 0), 30 | Vector256.Create(AndF(0d, double.NaN), AndF(double.MinValue, -0.00000000023434d), AndF(double.PositiveInfinity, double.NegativeInfinity), AndF(1414123d, 0d)) 31 | } 32 | }; 33 | } 34 | } 35 | 36 | [Theory] 37 | // TODO wtf 38 | #pragma warning disable xUnit1019 39 | [MemberData(nameof(Data))] 40 | #pragma warning enable xUnit1019 41 | public void And_Theory(Vector256 left, Vector256 right, Vector256 expected) 42 | { 43 | Vector256 vector = Vector.And(left, right); 44 | 45 | Assert.True(TestHelpers.AreEqual(expected, vector)); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BitOperationsTests/AndNot.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BitOperationsTests 8 | { 9 | public class AndNotTests 10 | { 11 | public static readonly double AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1)); 12 | 13 | public static IEnumerable Data 14 | { 15 | get 16 | { 17 | static double AndNotF(double a, double b) 18 | { 19 | ulong andNot = ~Unsafe.As(ref a) & Unsafe.As(ref b); 20 | return Unsafe.As(ref andNot); 21 | } 22 | 23 | return new[] 24 | { 25 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new Vector4d(0d) }, 26 | new object[] { Vector256.Create(AllBitsSet), Vector256.Create(AllBitsSet), new Vector4d(0d) }, 27 | new object[] { Vector256.Create(235434d, -123d, 0, double.MaxValue), Vector256.Create(235434d, -123d, 0, double.MaxValue), new Vector4d(0d) }, 28 | new object[] 29 | { 30 | Vector256.Create(0d, double.MinValue, double.PositiveInfinity, 1414123d), Vector256.Create(double.NaN, -0.00000000023434d, double.NegativeInfinity, 0), 31 | new Vector4d(AndNotF(0d, double.NaN), AndNotF(double.MinValue, -0.00000000023434d), AndNotF(double.PositiveInfinity, double.NegativeInfinity), AndNotF(1414123d, 0d)) 32 | } 33 | }; 34 | } 35 | } 36 | 37 | [Theory] 38 | // TODO wtf 39 | #pragma warning disable xUnit1019 40 | [MemberData(nameof(Data))] 41 | #pragma warning enable xUnit1019 42 | public void AndNot_Theory(Vector256 left, Vector256 right, Vector4d expected) 43 | { 44 | Vector256 vector = Vector.AndNot(left, right); 45 | 46 | Assert.True(TestHelpers.AreEqual(expected, vector)); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BitOperationsTests/Not.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BitOperationsTests 8 | { 9 | public class NotTests 10 | { 11 | public static readonly double AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1L)); 12 | 13 | public static IEnumerable Data 14 | { 15 | get 16 | { 17 | static double NotF(double a) 18 | { 19 | ulong not = ~Unsafe.As(ref a); 20 | return Unsafe.As(ref not); 21 | } 22 | 23 | return new[] 24 | { 25 | new object[] { Vector256.Create(0d), new Vector4d(AllBitsSet) }, 26 | new object[] { Vector256.Create(AllBitsSet), new Vector4d(0d) }, 27 | new object[] { Vector256.Create(235434d, -123d, 0, double.MaxValue), new Vector4d(NotF(235434d), NotF(-123d), NotF(0d), NotF(double.MaxValue)) } 28 | }; 29 | } 30 | } 31 | 32 | [Theory] 33 | // TODO wtf 34 | #pragma warning disable xUnit1019 35 | [MemberData(nameof(Data))] 36 | #pragma warning enable xUnit1019 37 | public void Not_Theory(Vector256 value, Vector4d expected) 38 | { 39 | Vector256 vector = Vector.Not(value); 40 | 41 | Assert.True(TestHelpers.AreEqual(expected, vector)); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BitOperationsTests/Or.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BitOperationsTests 8 | { 9 | public class OrTests 10 | { 11 | public static readonly double AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1)); 12 | 13 | public static IEnumerable Data 14 | { 15 | get 16 | { 17 | static double OrF(double a, double b) 18 | { 19 | ulong or = Unsafe.As(ref a) | Unsafe.As(ref b); 20 | return Unsafe.As(ref or); 21 | } 22 | 23 | return new[] 24 | { 25 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new Vector4d(0d) }, 26 | new object[] { Vector256.Create(AllBitsSet), Vector256.Create(AllBitsSet), new Vector4d(AllBitsSet) }, 27 | new object[] { Vector256.Create(235434d, -123d, 0, double.MaxValue), Vector256.Create(235434d, -123d, 0, double.MaxValue), new Vector4d(235434d, -123d, 0, double.MaxValue) }, 28 | new object[] 29 | { 30 | Vector256.Create(0d, double.MinValue, double.PositiveInfinity, 1414123d), Vector256.Create(double.NaN, -0.00000000023434d, double.NegativeInfinity, 0), 31 | new Vector4d(OrF(0d, double.NaN), OrF(double.MinValue, -0.00000000023434d), OrF(double.PositiveInfinity, double.NegativeInfinity), OrF(1414123d, 0d)) 32 | } 33 | }; 34 | } 35 | } 36 | 37 | [Theory] 38 | // TODO wtf 39 | #pragma warning disable xUnit1019 40 | [MemberData(nameof(Data))] 41 | #pragma warning enable xUnit1019 42 | public void Or_Theory(Vector256 left, Vector256 right, Vector4d expected) 43 | { 44 | Vector256 vector = Vector.Or(left, right); 45 | 46 | Assert.True(TestHelpers.AreEqual(expected, vector)); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/BitOperationsTests/Xor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.BitOperationsTests 8 | { 9 | public class XorTests 10 | { 11 | public static readonly double AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1)); 12 | 13 | public static IEnumerable Data 14 | { 15 | get 16 | { 17 | static double XorF(double a, double b) 18 | { 19 | ulong xor = Unsafe.As(ref a) ^ Unsafe.As(ref b); 20 | return Unsafe.As(ref xor); 21 | } 22 | 23 | return new[] 24 | { 25 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new Vector4d(0d) }, 26 | new object[] { Vector256.Create(AllBitsSet), Vector256.Create(AllBitsSet), new Vector4d(0d) }, 27 | new object[] { Vector256.Create(235434d, -123d, 0, double.MaxValue), Vector256.Create(235434d, -123d, 0, double.MaxValue), new Vector4d(0d) }, 28 | new object[] 29 | { 30 | Vector256.Create(0d, double.MinValue, double.PositiveInfinity, 1414123d), Vector256.Create(double.NaN, -0.00000000023434d, double.NegativeInfinity, 0), 31 | new Vector4d(XorF(0d, double.NaN), XorF(double.MinValue, -0.00000000023434d), XorF(double.PositiveInfinity, double.NegativeInfinity), XorF(1414123d, 0d)) 32 | } 33 | }; 34 | } 35 | } 36 | 37 | [Theory] 38 | // TODO wtf 39 | #pragma warning disable xUnit1019 40 | [MemberData(nameof(Data))] 41 | #pragma warning enable xUnit1019 42 | public void Xor_Theory(Vector256 left, Vector256 right, Vector4d expected) 43 | { 44 | Vector256 vector = Vector.Xor(left, right); 45 | 46 | Assert.True(TestHelpers.AreEqual(expected, vector)); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/ComparisonTests/Equality.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.ComparisonTests 7 | { 8 | public class Equality 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new []{ true, true, true, true } }, 14 | new object[] { Vector256.Create(0d), Vector256.Create(1d), new []{ false, false, false, false } }, 15 | new object[] { Vector256.Create(0d, 1d, 0d, 1d), Vector256.Create(0d), new []{ true, false, true, false } }, 16 | new object[] { Vector256.Create(double.NaN), Vector256.Create(0d), new []{ false, false, false, false } }, 17 | new object[] 18 | { 19 | Vector256.Create(0d, double.MaxValue, double.MinValue, double.PositiveInfinity), 20 | Vector256.Create(double.Epsilon, double.MaxValue - 1E+301d, double.MinValue + 1E+301d, double.NegativeInfinity), 21 | new []{ false, false, false, false } 22 | } 23 | }; 24 | 25 | [Theory] 26 | [MemberData(nameof(Data))] 27 | public static void Equality_Theory(Vector256 left, Vector256 right, bool[] expected) 28 | { 29 | Vector256 result = Vector.CompareEqual(left, right).AsInt64(); 30 | 31 | Assert.True(AreAllEqual(expected, result)); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/ComparisonTests/GreaterThan.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.ComparisonTests 8 | { 9 | public class GreaterThan 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new []{ false, false, false, false } }, 15 | new object[] { Vector256.Create(0d), Vector256.Create(1d), new []{ false, false, false, false } }, 16 | new object[] { Vector256.Create(1d), Vector256.Create(0d), new []{ true, true, true, true } }, 17 | new object[] { Vector256.Create(1d, -1d, 1d, -1d), Vector256.Create(0d), new []{ true, false, true, false } }, 18 | new object[] { Vector256.Create(double.NaN), Vector256.Create(0d), new []{ true, true, true, true } }, 19 | new object[] 20 | { 21 | Vector256.Create(0d, double.MaxValue, double.MinValue, double.PositiveInfinity), 22 | Vector256.Create(double.Epsilon, double.MaxValue - 1E+301d, double.MinValue + 1E+301d, double.NegativeInfinity), 23 | new []{ false, true, false, true } 24 | }, 25 | new object[] 26 | { 27 | Vector256.Create(1d, 10000000d, 0.000001d, -1), 28 | Vector256.Create(1d, 10000000d, 0.000001d, -1), 29 | new []{ false, false, false, false } 30 | } 31 | }; 32 | 33 | [Theory] 34 | [MemberData(nameof(Data))] 35 | public static void GreaterThan_Theory(Vector256 left, Vector256 right, bool[] expected) 36 | { 37 | Vector256 result1 = Vector.CompareGreaterThan(left, right).AsInt64(); 38 | 39 | Assert.True(AreAllEqual(expected, result1), $"Expected {string.Join(' ', expected.Select(b => b.ToString()))}, got {result1}"); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/ComparisonTests/GreaterThanOrEqualTo.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.ComparisonTests 7 | { 8 | public class GreaterThanOrEqualTo 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new []{ true, true, true, true } }, 14 | new object[] { Vector256.Create(0d), Vector256.Create(1d), new []{ false, false, false, false } }, 15 | new object[] { Vector256.Create(1d), Vector256.Create(0d), new []{ true, true, true, true } }, 16 | new object[] { Vector256.Create(1d, -1d, 1d, -1d), Vector256.Create(0d), new []{ true, false, true, false } }, 17 | new object[] { Vector256.Create(double.NaN), Vector256.Create(0d), new []{ true, true, true, true } }, 18 | new object[] 19 | { 20 | Vector256.Create(0d, double.MaxValue, double.MinValue, double.PositiveInfinity), 21 | Vector256.Create(double.Epsilon, double.MaxValue - 1E+301d, double.MinValue + 1E+301d, double.NegativeInfinity), 22 | new []{ false, true, false, true } 23 | }, 24 | new object[] 25 | { 26 | Vector256.Create(1d, 10000000d, 0.000001d, -1), 27 | Vector256.Create(1d, 10000000d, 0.000001d, -1), 28 | new []{ true, true, true, true } 29 | } 30 | }; 31 | 32 | [Theory] 33 | [MemberData(nameof(Data))] 34 | public static void GreaterThanOrEqualTo_Theory(Vector256 left, Vector256 right, bool[] expected) 35 | { 36 | Vector256 result1 = Vector.CompareGreaterThanOrEqual(left, right).AsInt64(); 37 | 38 | Assert.True((bool) AreAllEqual(expected, result1)); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/ComparisonTests/Inequality.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.ComparisonTests 7 | { 8 | public class Inequality 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new []{ true, true, true, true } }, 14 | new object[] { Vector256.Create(0d), Vector256.Create(1d), new []{ false, false, false, false } }, 15 | new object[] { Vector256.Create(0d, 1d, 0d, 1d), Vector256.Create(0d), new []{ true, false, true, false } }, 16 | new object[] { Vector256.Create(double.NaN), Vector256.Create(0d), new []{ false, false, false, false } }, 17 | new object[] 18 | { 19 | Vector256.Create(0d, double.MaxValue, double.MinValue, double.PositiveInfinity), 20 | Vector256.Create(double.Epsilon, double.MaxValue - 1E+301d, double.MinValue + 1E+301d, double.NegativeInfinity), 21 | new []{ false, false, false, false } 22 | } 23 | }; 24 | 25 | [Theory] 26 | [MemberData(nameof(Data))] 27 | public static void Inequality_Theory(Vector256 left, Vector256 right, bool[] expected) 28 | { 29 | Vector256 result = Vector.CompareNotEqual(left, right).AsInt64(); 30 | 31 | Assert.True(AreAllNotEqual(expected, result)); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/ComparisonTests/LessThan.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.ComparisonTests 7 | { 8 | public class LessThan 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new []{ true, true, true, true } }, 14 | new object[] { Vector256.Create(0d), Vector256.Create(1d), new []{ false, false, false, false } }, 15 | new object[] { Vector256.Create(1d), Vector256.Create(0d), new []{ true, true, true, true } }, 16 | new object[] { Vector256.Create(1d, -1d, 1d, -1d), Vector256.Create(0d), new []{ true, false, true, false } }, 17 | new object[] { Vector256.Create(double.NaN), Vector256.Create(0d), new []{ true, true, true, true } }, 18 | new object[] 19 | { 20 | Vector256.Create(0d, double.MaxValue, double.MinValue, double.PositiveInfinity), 21 | Vector256.Create(double.Epsilon, double.MaxValue - 1E+301d, double.MinValue + 1E+301d, double.NegativeInfinity), 22 | new []{ false, true, false, true } 23 | }, 24 | new object[] 25 | { 26 | Vector256.Create(1d, 10000000d, 0.000001d, -1), 27 | Vector256.Create(1d, 10000000d, 0.000001d, -1), 28 | new []{ true, true, true, true } 29 | } 30 | }; 31 | 32 | [Theory] 33 | [MemberData(nameof(Data))] 34 | public static void LessThan_Theory(Vector256 left, Vector256 right, bool[] expected) 35 | { 36 | Vector256 result1 = Vector.CompareLessThan(left, right).AsInt64(); 37 | 38 | Assert.True(AreAllNotEqual(expected, result1)); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/ComparisonTests/LessThanOrEqualTo.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.ComparisonTests 7 | { 8 | public class LessThanOrEqualTo 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector256.Create(0d), Vector256.Create(0d), new []{ false, false, false, false } }, 14 | new object[] { Vector256.Create(0d), Vector256.Create(1d), new []{ false, false, false, false } }, 15 | new object[] { Vector256.Create(1d), Vector256.Create(0d), new []{ true, true, true, true } }, 16 | new object[] { Vector256.Create(1d, -1d, 1d, -1d), Vector256.Create(0d), new []{ true, false, true, false } }, 17 | new object[] { Vector256.Create(double.NaN), Vector256.Create(0d), new []{ true, true, true, true } }, 18 | new object[] 19 | { 20 | Vector256.Create(0d, double.MaxValue, double.MinValue, double.PositiveInfinity), 21 | Vector256.Create(double.Epsilon, double.MaxValue - 1E+301d, double.MinValue + 1E+301d, double.NegativeInfinity), 22 | new []{ false, true, false, true } 23 | }, 24 | new object[] 25 | { 26 | Vector256.Create(1d, 10000000d, 0.000001d, -1), 27 | Vector256.Create(1d, 10000000d, 0.000001d, -1), 28 | new []{ false, false, false, false } 29 | } 30 | }; 31 | 32 | [Theory] 33 | [MemberData(nameof(Data))] 34 | public static void LessThanOrEqualTo_Theory(Vector256 left, Vector256 right, bool[] expected) 35 | { 36 | Vector256 result1 = Vector.CompareLessThanOrEqual(left, right).AsInt64(); 37 | 38 | Assert.True((bool) AreAllNotEqual(expected, result1)); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/ConversionTests/LoadTests.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | // ReSharper disable ConvertToConstant.Local 4 | 5 | //namespace MathSharp.UnitTests.VectorTests.VectorDouble.ConversionTests 6 | //{ 7 | // public class LoadTests 8 | // { 9 | // [Fact] 10 | // public void Load_StackVector4d_Passes() 11 | // { 12 | // var vector = new Vector4d(1, 2, 3, 4); 13 | 14 | // Vector256 loaded = vector.ToVector128(); 15 | // Assert.True(MathSharp.UnitTests.TestHelpers.AreEqual(vector, loaded)); 16 | // } 17 | 18 | // [Fact] 19 | // public void Load_StackVector3d_Passes() 20 | // { 21 | // var vector = new Vector3d(1, 2, 3); 22 | 23 | // Vector256 loaded = vector.ToVector128(); 24 | // Assert.True(MathSharp.UnitTests.TestHelpers.AreEqual(vector, loaded)); 25 | // } 26 | 27 | // [Fact] 28 | // public void Load_StackVector3dWithScalarW_Passes() 29 | // { 30 | // var vector = new Vector3d(1, 2, 3); 31 | // var scalar = 4324d; 32 | 33 | // Vector256 loaded = vector.ToVector128(scalar); 34 | // Assert.True(MathSharp.UnitTests.TestHelpers.AreEqual(new Vector4d(vector, scalar), loaded)); 35 | // } 36 | 37 | // [Fact] 38 | // public void Load_StackVector2d_Passes() 39 | // { 40 | // var vector = new Vector2d(1, 2); 41 | 42 | // Vector256 loaded = vector.ToVector128(); 43 | // Assert.True(MathSharp.UnitTests.TestHelpers.AreEqual(vector, loaded)); 44 | // } 45 | 46 | // [Fact] 47 | // public void Load_StackVector2dBroadcasted_Passes() 48 | // { 49 | // var vector = new Vector2d(1, 2); 50 | 51 | // Vector256 loaded = vector.LoadBroadcast(); 52 | // Assert.True(MathSharp.UnitTests.TestHelpers.AreEqual(new Vector4d(vector, vector.X, vector.Y), loaded)); 53 | // } 54 | 55 | // [Fact] 56 | // public void Load_StackScalar_Passes() 57 | // { 58 | // var vector = 55d; 59 | 60 | // Vector256 loaded = vector.LoadScalar(); 61 | // Assert.True(MathSharp.UnitTests.TestHelpers.AreEqual(vector, loaded)); 62 | // } 63 | 64 | // [Fact] 65 | // public void Load_StackScalarBroadcasted_Passes() 66 | // { 67 | // var vector = 55d; 68 | 69 | // Vector256 loaded = vector.LoadScalarBroadcast(); 70 | // Assert.True(MathSharp.UnitTests.TestHelpers.AreEqual(new Vector4d(vector), loaded)); 71 | // } 72 | // } 73 | //} -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/ConversionTests/StoreTests.cs: -------------------------------------------------------------------------------- 1 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.ConversionTests 2 | { 3 | public class StoreTests 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorDouble/VectorOperations/Normalize.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using OpenTK; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorDouble.VectorOperations 8 | { 9 | public class Normalize 10 | { 11 | public static IEnumerable Data(VectorDimensions dimension) 12 | { 13 | var objs = new[] 14 | { 15 | new object[] {Vector256.Create(0d), default(Vector4d)}, 16 | new object[] {Vector256.Create(1d), default(Vector4d)}, 17 | new object[] {Vector256.Create(-1d), default(Vector4d)}, 18 | new object[] {Vector256.Create(-1d), default(Vector4d)}, 19 | new object[] {Vector256.Create(1d, 2d, 3d, 4d), default(Vector4d)}, 20 | new object[] {Vector256.Create(double.PositiveInfinity), default(Vector4d)}, 21 | new object[] {Vector256.Create(double.PositiveInfinity), default(Vector4d)}, 22 | new object[] {Vector256.Create(double.NaN), default(Vector4d)}, 23 | new object[] {Vector256.Create(double.MaxValue, double.MinValue, double.NaN, 0), default(Vector4d)}, 24 | }; 25 | 26 | foreach (object[] set in objs) 27 | { 28 | switch (dimension) 29 | { 30 | case VectorDimensions.V2D: 31 | { 32 | Vector2d v1 = TestHelpers.ByValToSlowVector2d(((Vector256)set[0])); 33 | set[1] = Vector2d.Normalize(v1); 34 | break; 35 | } 36 | 37 | case VectorDimensions.V3D: 38 | { 39 | Vector3d v1 = TestHelpers.ByValToSlowVector3d(((Vector256)set[0])); 40 | set[1] = Vector3d.Normalize(v1); 41 | break; 42 | } 43 | 44 | case VectorDimensions.V4D: 45 | { 46 | Vector4d v1 = TestHelpers.ByValToSlowVector4d(((Vector256)set[0])); 47 | set[1] = Vector4d.Normalize(v1); 48 | break; 49 | } 50 | 51 | default: 52 | throw new ArgumentException(nameof(dimension)); 53 | } 54 | } 55 | 56 | return objs; 57 | } 58 | 59 | [Theory] 60 | [MemberData(nameof(Data), VectorDimensions.V2D)] 61 | public static void Normalize2D_Theory(Vector256 vector, Vector2d expected) 62 | { 63 | Vector256 result = Vector.Normalize2D(vector); 64 | 65 | Assert.True(TestHelpers.AreApproxEqual(expected, result)); 66 | } 67 | 68 | [Theory] 69 | [MemberData(nameof(Data), VectorDimensions.V3D)] 70 | public static void Normalize3D_Theory(Vector256 vector, Vector3d expected) 71 | { 72 | Vector256 result = Vector.Normalize3D(vector); 73 | 74 | Assert.True(TestHelpers.AreApproxEqual(expected, result)); 75 | } 76 | 77 | [Theory] 78 | [MemberData(nameof(Data), VectorDimensions.V4D)] 79 | public static void Normalize4D_Theory(Vector256 vector, Vector4d expected) 80 | { 81 | Vector256 result = Vector.Normalize4D(vector); 82 | 83 | Assert.True(TestHelpers.AreApproxEqual(expected, result, 0.00001d), $"Expected: {expected}, got {result}"); 84 | } 85 | } 86 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Abs.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 8 | { 9 | public class Abs 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector128.Create(0f), new Vector4(0f) }, 15 | new object[] { Vector128.Create(1f), new Vector4(1f) }, 16 | new object[] { Vector128.Create(-1f), new Vector4(1f) }, 17 | new object[] { Vector128.Create(-1f, 0f, -1f, 0f), new Vector4(1f, 0f, 1f, 0f) }, 18 | new object[] { Vector128.Create(float.NegativeInfinity), new Vector4(float.PositiveInfinity) }, 19 | new object[] { Vector128.Create(float.NaN, -111f, -0.0000001f, 0.0000001f), new Vector4(float.NaN, 111f, 0.0000001f, 0.0000001f) }, 20 | }; 21 | 22 | [Theory] 23 | [MemberData(nameof(Data))] 24 | public void Abs_Theory(Vector128 vector, Vector4 expected) 25 | { 26 | Vector128 result = Vector.Abs(vector); 27 | 28 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Add.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 8 | { 9 | public class Add 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new Vector4(0f + 0f) }, 15 | new object[] { Vector128.Create(1f), Vector128.Create(1f), new Vector4(1f + 1f) }, 16 | new object[] { Vector128.Create(-1f), Vector128.Create(1f), new Vector4(-1f + 1f) }, 17 | new object[] { Vector128.Create(-1f, 0f, -1f, 0f), Vector128.Create(1f, 10f, 1f, 10f), new Vector4(-1f + 1f, 0f + 10f, -1f + 1f, 0f + 10f) }, 18 | new object[] { Vector128.Create(float.NegativeInfinity), Vector128.Create(float.PositiveInfinity), new Vector4(float.NegativeInfinity + float.PositiveInfinity) }, 19 | new object[] { Vector128.Create(float.MinValue), Vector128.Create(float.MaxValue), new Vector4(float.MinValue + float.MaxValue) }, 20 | }; 21 | 22 | [Theory] 23 | [MemberData(nameof(Data))] 24 | public void Add_Theory(Vector128 left, Vector128 right, Vector4 expected) 25 | { 26 | Vector128 result = Vector.Add(left, right); 27 | 28 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Clamp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using OpenTK; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | using static MathSharp.UnitTests.TestHelpers; 7 | 8 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 9 | { 10 | public class Clamp 11 | { 12 | public static IEnumerable Data => 13 | new[] 14 | { 15 | new object[] { Vector128.Create(0f), Vector128.Create(0f), Vector128.Create(0f), new Vector4(Math.Clamp(0f, 0f, 0f)) }, 16 | new object[] { Vector128.Create(1f), Vector128.Create(0f), Vector128.Create(1f), new Vector4(Math.Clamp(1f, 0f, 1f)) }, 17 | new object[] { Vector128.Create(1f), Vector128.Create(10f), Vector128.Create(110f), new Vector4(Math.Clamp(1f, 10f, 110f)) }, 18 | new object[] { Vector128.Create(float.NegativeInfinity), Vector128.Create(0f), Vector128.Create(1f), new Vector4(Math.Clamp(float.NegativeInfinity, 0f, 1f)) }, 19 | new object[] { Vector128.Create(float.PositiveInfinity), Vector128.Create(0f), Vector128.Create(100f), new Vector4(Math.Clamp(float.PositiveInfinity, 0f, 100f)) }, 20 | }; 21 | 22 | [Theory] 23 | [MemberData(nameof(Data))] 24 | public void Clamp_Theory(Vector128 vector, Vector128 low, Vector128 high, Vector4 expected) 25 | { 26 | Vector128 result = Vector.Clamp(vector, low, high); 27 | 28 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/CopySign.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 8 | { 9 | public class CopySign 10 | { 11 | public static IEnumerable Data => DataSets.Single.CreateBinaryDataSet(MathF.CopySign); 12 | 13 | [Theory] 14 | [MemberData(nameof(Data))] 15 | public static void CopySign_Theory(Vector128 a, Vector128 b, Vector128 expected) 16 | { 17 | a = Vector.CopySign(a, b); 18 | 19 | Assert.True(PerElemCheck(a, expected, (f, exp) => f.Equals(exp))); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Divide.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using OpenTK; 4 | using Xunit; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 8 | { 9 | public class Divide 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new Vector4(0f / 0f) }, 15 | new object[] { Vector128.Create(1f), Vector128.Create(1f), new Vector4(1f / 1f) }, 16 | new object[] { Vector128.Create(-1f), Vector128.Create(1f), new Vector4(-1f / 1f) }, 17 | new object[] { Vector128.Create(-1f, 0f, -1f, 0f), Vector128.Create(1f, 10f, 1f, 10f), new Vector4(-1f / 1f, 0f / 10f, -1f / 1f, 0f / 10f) }, 18 | new object[] { Vector128.Create(float.NegativeInfinity), Vector128.Create(float.PositiveInfinity), new Vector4(float.NegativeInfinity / float.PositiveInfinity) }, 19 | new object[] { Vector128.Create(float.MinValue), Vector128.Create(float.MaxValue), new Vector4(float.MinValue / float.MaxValue) }, 20 | }; 21 | 22 | [Theory] 23 | [MemberData(nameof(Data))] 24 | public void Divide_Theory(Vector128 left, Vector128 right, Vector4 expected) 25 | { 26 | Vector128 result = Vector.Divide(left, right); 27 | 28 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/ExtractSign.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 7 | { 8 | public class ExtractSign 9 | { 10 | public static IEnumerable Data => DataSets.Single.CreateUnaryDataSet(x => IsNegative(x) ? -0f : 0f); 11 | 12 | [Theory] 13 | [MemberData(nameof(Data))] 14 | public static void ExtractSign_Theory(Vector128 a, Vector128 expected) 15 | { 16 | a = Vector.ExtractSign(a); 17 | 18 | Assert.True(PerElemCheck(a, expected, (f1, f2) => IsNegative(f1) == IsNegative(f2))); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Floor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 7 | { 8 | public class Floor 9 | { 10 | public static IEnumerable Data { get; } = DataSets.Single.CreateRoundingDataSet(MathF.Floor); 11 | 12 | [Theory] 13 | [MemberData(nameof(Data))] 14 | public static void Floor_Theory(Vector128 value, Vector128 expected) 15 | { 16 | value = Vector.Floor(value); 17 | 18 | Assert.True(TestHelpers.AreEqual(value, expected)); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/HorizontalAdd.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 8 | { 9 | public class HorizontalAdd 10 | { 11 | public static IEnumerable Data 12 | { 13 | get 14 | { 15 | static Vector4 HorizontalAdd(Vector4 left, Vector4 right) 16 | { 17 | return new Vector4(left.X + left.Y, left.Z + left.W, right.X + right.Y, right.Z + right.W); 18 | } 19 | 20 | static Vector4 HorizontalAddF(float left, float right) 21 | { 22 | return HorizontalAdd(new Vector4(left), new Vector4(right)); 23 | } 24 | 25 | return new[] 26 | { 27 | new object[] 28 | { 29 | Vector128.Create(0f), Vector128.Create(0f), 30 | HorizontalAddF(0f, 0f) 31 | }, 32 | new object[] 33 | { 34 | Vector128.Create(1f), Vector128.Create(1f), 35 | HorizontalAddF(1f, 1f) 36 | }, 37 | new object[] 38 | { 39 | Vector128.Create(10f, -10f, float.MinValue, float.MaxValue), Vector128.Create(0.0005f, 0.1f, 2343242f, -123123123123f), 40 | HorizontalAdd(new Vector4(10f, -10f, float.MinValue, float.MaxValue), new Vector4(0.0005f, 0.1f, 2343242f, -123123123123f)) 41 | }, 42 | }; 43 | } 44 | } 45 | 46 | 47 | [Theory] 48 | [MemberData(nameof(Data))] 49 | public void HorizontalAdd_Theory(Vector128 left, Vector128 right, Vector4 expected) 50 | { 51 | Vector128 result = Vector.HorizontalAdd(left, right); 52 | 53 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/InBounds.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class InBounds 8 | { 9 | public static IEnumerable Data => DataSets.Single.CreateBinaryDataSet((val, bound) => val <= bound && val >= -bound ? Utils.Helpers.AllBitsSetSingle : 0f); 10 | 11 | [Theory] 12 | [MemberData(nameof(Data))] 13 | public static void InBounds_Theory(Vector128 value, Vector128 bounds, Vector128 expected) 14 | { 15 | value = Vector.InBounds(value, bounds); 16 | 17 | Assert.True(TestHelpers.AreEqual(value, expected)); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/IsFinite.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class IsFinite 8 | { 9 | public static IEnumerable Data => 10 | DataSets.Single.CreateUnaryDataSet(x => float.IsFinite(x) ? Utils.Helpers.AllBitsSetSingle : 0f); 11 | 12 | [Theory] 13 | [MemberData(nameof(Data))] 14 | public static void IsFinite_Theory(Vector128 value, Vector128 expected) 15 | { 16 | value = Vector.IsFinite(value); 17 | 18 | Assert.True(TestHelpers.AreEqual(value, expected)); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/IsInfinite.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class IsInfinite 8 | { 9 | public static IEnumerable Data => DataSets.Single.CreateUnaryDataSet(x => float.IsInfinity(x) ? Utils.Helpers.AllBitsSetSingle : 0f); 10 | 11 | [Theory] 12 | [MemberData(nameof(Data))] 13 | public static void IsInfinite_Theory(Vector128 value, Vector128 expected) 14 | { 15 | value = Vector.IsInfinite(value); 16 | 17 | Assert.True(TestHelpers.AreEqual(value, expected)); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/IsNaN.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class IsNaN 8 | { 9 | public static IEnumerable Data => 10 | new[] 11 | { 12 | new object[] 13 | { 14 | Vector128.Create(0.0f), new[] { false, false, false, false } 15 | }, 16 | new object[] 17 | { 18 | Vector128.Create(float.NaN), new[] { true, true, true, true } 19 | }, 20 | new object[] 21 | { 22 | Vector128.Create(float.PositiveInfinity), new[] { false, false, false, false } 23 | }, 24 | new object[] 25 | { 26 | Vector128.Create(float.NegativeInfinity), new[] { false, false, false, false } 27 | }, 28 | new object[] 29 | { 30 | Vector128.Create(0.0f, float.NaN, 0.0f, float.NaN), new[] { false, true, false, true } 31 | }, 32 | }; 33 | 34 | [Theory] 35 | [MemberData(nameof(Data))] 36 | public static void IsNaN_Theory(Vector128 values, bool[] expected) 37 | { 38 | var isNaN = Vector.IsNaN(values); 39 | 40 | Assert.True(TestHelpers.AreAllEqual(expected, isNaN.AsInt32())); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/IsNotNaN.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class IsNotNaN 8 | { 9 | public static IEnumerable Data => 10 | new[] 11 | { 12 | new object[] 13 | { 14 | Vector128.Create(0.0f), new[] { false, false, false, false } 15 | }, 16 | new object[] 17 | { 18 | Vector128.Create(float.NaN), new[] { true, true, true, true } 19 | }, 20 | new object[] 21 | { 22 | Vector128.Create(float.PositiveInfinity), new[] { false, false, false, false } 23 | }, 24 | new object[] 25 | { 26 | Vector128.Create(float.NegativeInfinity), new[] { false, false, false, false } 27 | }, 28 | new object[] 29 | { 30 | Vector128.Create(0.0f, float.NaN, 0.0f, float.NaN), new[] { false, true, false, true } 31 | }, 32 | }; 33 | 34 | [Theory] 35 | [MemberData(nameof(Data))] 36 | public static void IsNotNaN_Theory(Vector128 values, bool[] expected) 37 | { 38 | 39 | var isNaN = Vector.IsNotNaN(values); 40 | 41 | Assert.True(TestHelpers.AreAllNotEqual(expected, isNaN.AsInt32())); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/IsNotZero.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class IsNotZero 8 | { 9 | public static IEnumerable Data => DataSets.Single.CreateUnaryDataSet(x => x != 0 ? Utils.Helpers.AllBitsSetSingle : 0f); 10 | 11 | [Theory] 12 | [MemberData(nameof(Data))] 13 | public static void IsNotZero_Theory(Vector128 value, Vector128 expected) 14 | { 15 | value = Vector.IsNotZero(value); 16 | 17 | Assert.True(TestHelpers.AreEqual(value, expected)); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/IsZero.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class IsZero 8 | { 9 | public static IEnumerable Data => DataSets.Single.CreateUnaryDataSet(x => x == 0 ? Utils.Helpers.AllBitsSetSingle : 0f); 10 | 11 | [Theory] 12 | [MemberData(nameof(Data))] 13 | public static void IsZero_Theory(Vector128 value, Vector128 expected) 14 | { 15 | value = Vector.IsZero(value); 16 | 17 | Assert.True(TestHelpers.AreEqual(value, expected)); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Max.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using OpenTK; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | using static MathSharp.UnitTests.TestHelpers; 7 | 8 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 9 | { 10 | public class Max 11 | { 12 | public static IEnumerable Data => 13 | new[] 14 | { 15 | new object[] { Vector128.Create(0f), Vector128.Create(1f), new Vector4(MathF.Max(0f, 1f)) }, 16 | new object[] { Vector128.Create(1f), Vector128.Create(-1f), new Vector4(MathF.Max(1f, 0f)) }, 17 | new object[] { Vector128.Create(0.99999999999999999999999999999999999999999f), Vector128.Create(1f), new Vector4(MathF.Max(0.99999999999999999999999999999999999999999f, 1f)) }, 18 | new object[] { Vector128.Create(float.NegativeInfinity), Vector128.Create(float.PositiveInfinity), new Vector4(MathF.Max(float.NegativeInfinity, float.PositiveInfinity)) }, 19 | new object[] { Vector128.Create(float.NaN), Vector128.Create(0f), new Vector4(0f) }, 20 | }; 21 | 22 | [Theory] 23 | [MemberData(nameof(Data))] 24 | public void Max_Theory(Vector128 left, Vector128 right, Vector4 expected) 25 | { 26 | Vector128 result = Vector.Max(left, right); 27 | 28 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Min.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using OpenTK; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | using static MathSharp.UnitTests.TestHelpers; 7 | 8 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 9 | { 10 | public class Min 11 | { 12 | public static IEnumerable Data => 13 | new[] 14 | { 15 | new object[] { Vector128.Create(0f), Vector128.Create(1f), new Vector4(MathF.Min(0f, 1f)) }, 16 | new object[] { Vector128.Create(1f), Vector128.Create(-1f), new Vector4(MathF.Min(1f, -1f)) }, 17 | new object[] { Vector128.Create(0.99999999999999999999999999999999999999999f), Vector128.Create(1f), new Vector4(MathF.Min(0.99999999999999999999999999999999999999999f, 1f)) }, 18 | new object[] { Vector128.Create(float.NegativeInfinity), Vector128.Create(float.PositiveInfinity), new Vector4(MathF.Min(float.NegativeInfinity, float.PositiveInfinity)) }, 19 | new object[] { Vector128.Create(float.NaN), Vector128.Create(0f), new Vector4(0f) }, 20 | }; 21 | 22 | [Theory] 23 | [MemberData(nameof(Data))] 24 | public void Min_Theory(Vector128 left, Vector128 right, Vector4 expected) 25 | { 26 | Vector128 result = Vector.Min(left, right); 27 | 28 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Multiply.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 8 | { 9 | public class Multiply 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new Vector4(0f * 0f) }, 15 | new object[] { Vector128.Create(1f), Vector128.Create(1f), new Vector4(1f * 1f) }, 16 | new object[] { Vector128.Create(-1f), Vector128.Create(1f), new Vector4(-1f * 1f) }, 17 | new object[] { Vector128.Create(-1f, 0f, -1f, 0f), Vector128.Create(1f, 10f, 1f, 10f), new Vector4(-1f * 1f, 0f * 10f, -1f * 1f, 0f * 10f) }, 18 | new object[] { Vector128.Create(float.NegativeInfinity), Vector128.Create(float.PositiveInfinity), new Vector4(float.NegativeInfinity * float.PositiveInfinity) }, 19 | new object[] { Vector128.Create(float.MinValue), Vector128.Create(float.MaxValue), new Vector4(float.MinValue * float.MaxValue) }, 20 | }; 21 | 22 | [Theory] 23 | [MemberData(nameof(Data))] 24 | public void Multiply_Theory(Vector128 left, Vector128 right, Vector4 expected) 25 | { 26 | Vector128 result = Vector.Multiply(left, right); 27 | 28 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Negate.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 8 | { 9 | public class Negate 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector128.Create(1f), new Vector4(-1f) }, 15 | new object[] { Vector128.Create(-1f), new Vector4(1f) }, 16 | new object[] { Vector128.Create(-1f, float.NegativeInfinity, -1f, -0.00000000000000000001f), new Vector4(1f, float.PositiveInfinity, 1f, 0.00000000000000000001f) }, 17 | new object[] { Vector128.Create(float.NegativeInfinity), new Vector4(float.PositiveInfinity) }, 18 | }; 19 | 20 | [Theory] 21 | [MemberData(nameof(Data))] 22 | public void Negate_Theory(Vector128 vector, Vector4 expected) 23 | { 24 | Vector128 result = Vector.Negate(vector); 25 | 26 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Reciprocal.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class Reciprocal 8 | { 9 | public static IEnumerable Data => DataSets.Single.CreateUnaryDataSet(x => 1 / x); 10 | 11 | [Theory] 12 | [MemberData(nameof(Data))] 13 | public static void Reciprocal_Theory(Vector128 value, Vector128 expected) 14 | { 15 | value = Vector.Reciprocal(value); 16 | 17 | Assert.True(TestHelpers.AreEqual(value, expected)); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/ReciprocalApprox.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class ReciprocalApprox 8 | { 9 | public const float AllowedDifference = 1.5f * 2e-12f; 10 | 11 | public static IEnumerable Data => DataSets.Single.CreateUnaryDataSet(x => 1 / x); 12 | 13 | [Theory] 14 | [MemberData(nameof(Data))] 15 | public static void ReciprocalApprox_Theory(Vector128 value, Vector128 expected) 16 | { 17 | value = Vector.ReciprocalApprox(value); 18 | 19 | Assert.True(TestHelpers.AreApproxEqual(value, expected, AllowedDifference)); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/ReciprocalSqrt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 7 | { 8 | public class ReciprocalSqrt 9 | { 10 | public static IEnumerable Data => DataSets.Single.CreateUnaryDataSet(x => 1 / MathF.Sqrt(x)); 11 | 12 | [Theory] 13 | [MemberData(nameof(Data))] 14 | public static void ReciprocalSqrt_Theory(Vector128 value, Vector128 expected) 15 | { 16 | value = Vector.ReciprocalSqrt(value); 17 | 18 | Assert.True(TestHelpers.AreEqual(value, expected)); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/ReciprocalSqrtApprox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 7 | { 8 | public class ReciprocalSqrtApprox 9 | { 10 | public const float AllowedDifference = 1.5f * 2e-12f; 11 | 12 | public static IEnumerable Data => DataSets.Single.CreateUnaryDataSet(x => 1 / MathF.Sqrt(x)); 13 | 14 | [Theory] 15 | [MemberData(nameof(Data))] 16 | public static void ReciprocalSqrtApprox_Theory(Vector128 value, Vector128 expected) 17 | { 18 | value = Vector.ReciprocalSqrtApprox(value); 19 | 20 | Assert.True(TestHelpers.AreApproxEqual(value, expected, AllowedDifference)); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Remainder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using Xunit; 4 | 5 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 6 | { 7 | public class Remainder 8 | { 9 | public static IEnumerable Data => 10 | new[] 11 | { 12 | new object[] { new Vector4(10.4f), new Vector4(2f) }, 13 | new object[] { new Vector4(-10.4f), new Vector4(2f) }, 14 | new object[] { new Vector4(float.NaN, float.MinValue, float.MaxValue, float.PositiveInfinity), new Vector4(3f) }, 15 | new object[] { new Vector4(float.NaN, float.MinValue, float.MaxValue, float.PositiveInfinity), new Vector4(2.4f) }, 16 | new object[] { new Vector4(23.45f), new Vector4(219f) }, 17 | new object[] { new Vector4(12), new Vector4(float.NaN) }, 18 | new object[] { new Vector4(-10000000f), new Vector4(2.2f) }, 19 | new object[] { new Vector4(10000000f), new Vector4(2.2f) }, 20 | new object[] { new Vector4(float.NegativeInfinity), new Vector4(3f) } 21 | }; 22 | 23 | [Theory] 24 | [MemberData(nameof(Data))] 25 | public unsafe void Remainder_Theory(Vector4 left, Vector4 right) 26 | { 27 | var l = Vector.FromVector4D(&left.X); 28 | var r = Vector.FromVector4D(&right.X); 29 | 30 | var remainder = Vector.Remainder(l, r); 31 | var expected = new Vector4(left.X % right.X, left.Y % right.Y, left.Z % right.Z, left.W % right.W); 32 | 33 | TestHelpers.AreEqual(expected, remainder); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Round.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 7 | { 8 | public class Round 9 | { 10 | public static IEnumerable Data { get; } = DataSets.Single.CreateRoundingDataSet(MathF.Round); 11 | 12 | [Theory] 13 | [MemberData(nameof(Data))] 14 | public static void Round_Theory(Vector128 value, Vector128 expected) 15 | { 16 | value = Vector.Round(value); 17 | 18 | Assert.True(TestHelpers.AreEqual(value, expected)); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Sqrt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using OpenTK; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | using static MathSharp.UnitTests.TestHelpers; 7 | 8 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 9 | { 10 | public class Sqrt 11 | { 12 | public static IEnumerable Data => 13 | new[] 14 | { 15 | new object[] { Vector128.Create(0f), new Vector4(MathF.Sqrt(0f)) }, 16 | new object[] { Vector128.Create(1f), new Vector4(MathF.Sqrt(1f)) }, 17 | new object[] { Vector128.Create(-1f), new Vector4(MathF.Sqrt(-1)) }, 18 | new object[] { Vector128.Create(1f, 4f, 9f, 16f), new Vector4(MathF.Sqrt(1f), MathF.Sqrt(4f), MathF.Sqrt(9f), MathF.Sqrt(16f)) }, 19 | new object[] { Vector128.Create(0.5f, 111f, -0.0000001f, 0.0000001f), new Vector4(MathF.Sqrt(0.5f), MathF.Sqrt(111f), MathF.Sqrt(-0.0000001f), MathF.Sqrt(0.0000001f)) }, 20 | }; 21 | 22 | [Theory] 23 | [MemberData(nameof(Data))] 24 | public void Sqrt_Theory(Vector128 vector, Vector4 expected) 25 | { 26 | Vector128 result = Vector.Sqrt(vector); 27 | 28 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Subtract.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 8 | { 9 | public class Subtract 10 | { 11 | public static IEnumerable Data => 12 | new[] 13 | { 14 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new Vector4(0f - 0f) }, 15 | new object[] { Vector128.Create(1f), Vector128.Create(1f), new Vector4(1f - 1f) }, 16 | new object[] { Vector128.Create(-1f), Vector128.Create(1f), new Vector4(-1f - 1f) }, 17 | new object[] { Vector128.Create(-1f, 0f, -1f, 0f), Vector128.Create(1f, 10f, 1f, 10f), new Vector4(-1f - 1f, 0f - 10f, -1f - 1f, 0f - 10f) }, 18 | new object[] { Vector128.Create(float.NegativeInfinity), Vector128.Create(float.PositiveInfinity), new Vector4(float.NegativeInfinity - float.PositiveInfinity) }, 19 | new object[] { Vector128.Create(float.MinValue), Vector128.Create(float.MaxValue), new Vector4(float.MinValue - float.MaxValue) }, 20 | }; 21 | 22 | [Theory] 23 | [MemberData(nameof(Data))] 24 | public void Subtract_Theory(Vector128 left, Vector128 right, Vector4 expected) 25 | { 26 | Vector128 result = Vector.Subtract(left, right); 27 | 28 | Assert.True(AreEqual(expected, result), $"Expected {expected}, got {result}"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BasicMathsTests/Truncate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.Intrinsics; 4 | using Xunit; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BasicMathsTests 7 | { 8 | public class Truncate 9 | { 10 | public static IEnumerable Data { get; } = DataSets.Single.CreateRoundingDataSet(MathF.Truncate); 11 | 12 | [Theory] 13 | [MemberData(nameof(Data))] 14 | public static void Truncate_Theory(Vector128 value, Vector128 expected) 15 | { 16 | value = Vector.Truncate(value); 17 | 18 | Assert.True(TestHelpers.AreEqual(value, expected)); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BitOperationsTests/And.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BitOperationsTests 8 | { 9 | public class AndTests 10 | { 11 | public static readonly float AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1)); 12 | 13 | public static IEnumerable Data 14 | { 15 | get 16 | { 17 | static float AndF(float a, float b) 18 | { 19 | uint and = Unsafe.As(ref a) & Unsafe.As(ref b); 20 | return Unsafe.As(ref and); 21 | } 22 | 23 | return new[] 24 | { 25 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new Vector4(0f) }, 26 | new object[] { Vector128.Create(AllBitsSet), Vector128.Create(AllBitsSet), new Vector4(AllBitsSet) }, 27 | new object[] { Vector128.Create(235434f, -123f, 0, float.MaxValue), Vector128.Create(235434f, -123f, 0, float.MaxValue), new Vector4(235434f, -123f, 0, float.MaxValue) }, 28 | new object[] 29 | { 30 | Vector128.Create(0f, float.MinValue, float.PositiveInfinity, 1414123f), Vector128.Create(float.NaN, -0.00000000023434f, float.NegativeInfinity, 0), 31 | new Vector4(AndF(0f, float.NaN), AndF(float.MinValue, -0.00000000023434f), AndF(float.PositiveInfinity, float.NegativeInfinity), AndF(1414123f, 0f)) 32 | } 33 | }; 34 | } 35 | } 36 | 37 | [Theory] 38 | // TODO wtf 39 | #pragma warning disable xUnit1019 40 | [MemberData(nameof(Data))] 41 | #pragma warning restore xUnit1019 42 | public void And_Theory(Vector128 left, Vector128 right, Vector4 expected) 43 | { 44 | Vector128 vector = Vector.And(left, right); 45 | 46 | Assert.True(TestHelpers.AreEqual(expected, vector)); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BitOperationsTests/AndNot.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using static MathSharp.UnitTests.TestHelpers; 6 | using Xunit; 7 | 8 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BitOperationsTests 9 | { 10 | public class AndNotTests 11 | { 12 | public static readonly float AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1)); 13 | 14 | public static IEnumerable Data 15 | { 16 | get 17 | { 18 | static float AndNotF(float a, float b) 19 | { 20 | uint andNot = ~Unsafe.As(ref a) & Unsafe.As(ref b); 21 | return Unsafe.As(ref andNot); 22 | } 23 | 24 | return new[] 25 | { 26 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new Vector4(0f) }, 27 | new object[] { Vector128.Create(AllBitsSet), Vector128.Create(AllBitsSet), new Vector4(0f) }, 28 | new object[] { Vector128.Create(235434f, -123f, 0, float.MaxValue), Vector128.Create(235434f, -123f, 0, float.MaxValue), new Vector4(0f) }, 29 | new object[] 30 | { 31 | Vector128.Create(0f, float.MinValue, float.PositiveInfinity, 1414123f), Vector128.Create(float.NaN, -0.00000000023434f, float.NegativeInfinity, 0), 32 | new Vector4(AndNotF(0f, float.NaN), AndNotF(float.MinValue, -0.00000000023434f), AndNotF(float.PositiveInfinity, float.NegativeInfinity), AndNotF(1414123f, 0f)) 33 | } 34 | }; 35 | } 36 | } 37 | 38 | [Theory] 39 | // TODO wtf 40 | #pragma warning disable xUnit1019 41 | [MemberData(nameof(Data))] 42 | #pragma warning enable xUnit1019 43 | public void AndNot_Theory(Vector128 left, Vector128 right, Vector4 expected) 44 | { 45 | Vector128 vector = Vector.AndNot(left, right); 46 | 47 | Assert.True(AreEqual(expected, vector)); 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BitOperationsTests/Not.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BitOperationsTests 8 | { 9 | public class NotTests 10 | { 11 | public static readonly float AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1)); 12 | 13 | public static IEnumerable Data 14 | { 15 | get 16 | { 17 | static float NotF(float a) 18 | { 19 | uint not = ~Unsafe.As(ref a); 20 | return Unsafe.As(ref not); 21 | } 22 | 23 | return new[] 24 | { 25 | new object[] { Vector128.Create(0f), new Vector4(AllBitsSet) }, 26 | new object[] { Vector128.Create(AllBitsSet), new Vector4(0f) }, 27 | new object[] { Vector128.Create(235434f, -123f, 0, float.MaxValue), new Vector4(NotF(235434f), NotF(-123f), NotF(0f), NotF(float.MaxValue)) } 28 | }; 29 | } 30 | } 31 | 32 | [Theory] 33 | // TODO wtf 34 | #pragma warning disable xUnit1019 35 | [MemberData(nameof(Data))] 36 | #pragma warning enable xUnit1019 37 | public void Not_Theory(Vector128 value, Vector4 expected) 38 | { 39 | Vector128 vector = Vector.Not(value); 40 | 41 | Assert.True(TestHelpers.AreEqual(expected, vector)); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BitOperationsTests/Or.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BitOperationsTests 8 | { 9 | public class OrTests 10 | { 11 | public static readonly float AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1)); 12 | 13 | public static IEnumerable Data 14 | { 15 | get 16 | { 17 | static float OrF(float a, float b) 18 | { 19 | uint or = Unsafe.As(ref a) | Unsafe.As(ref b); 20 | return Unsafe.As(ref or); 21 | } 22 | 23 | return new[] 24 | { 25 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new Vector4(0f) }, 26 | new object[] { Vector128.Create(AllBitsSet), Vector128.Create(AllBitsSet), new Vector4(AllBitsSet) }, 27 | new object[] { Vector128.Create(235434f, -123f, 0, float.MaxValue), Vector128.Create(235434f, -123f, 0, float.MaxValue), new Vector4(235434f, -123f, 0, float.MaxValue) }, 28 | new object[] 29 | { 30 | Vector128.Create(0f, float.MinValue, float.PositiveInfinity, 1414123f), Vector128.Create(float.NaN, -0.00000000023434f, float.NegativeInfinity, 0), 31 | new Vector4(OrF(0f, float.NaN), OrF(float.MinValue, -0.00000000023434f), OrF(float.PositiveInfinity, float.NegativeInfinity), OrF(1414123f, 0f)) 32 | } 33 | }; 34 | } 35 | } 36 | 37 | [Theory] 38 | // TODO wtf 39 | #pragma warning disable xUnit1019 40 | [MemberData(nameof(Data))] 41 | #pragma warning enable xUnit1019 42 | public void Or_Theory(Vector128 left, Vector128 right, Vector4 expected) 43 | { 44 | Vector128 vector = Vector.Or(left, right); 45 | 46 | Assert.True(TestHelpers.AreEqual(expected, vector)); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/BitOperationsTests/Xor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using OpenTK; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.Intrinsics; 5 | using Xunit; 6 | 7 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.BitOperationsTests 8 | { 9 | public class XorTests 10 | { 11 | public static readonly float AllBitsSet = Unsafe.As(ref Unsafe.AsRef(-1)); 12 | 13 | public static IEnumerable Data 14 | { 15 | get 16 | { 17 | static float XorF(float a, float b) 18 | { 19 | uint xor = Unsafe.As(ref a) ^ Unsafe.As(ref b); 20 | return Unsafe.As(ref xor); 21 | } 22 | 23 | return new[] 24 | { 25 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new Vector4(0f) }, 26 | new object[] { Vector128.Create(AllBitsSet), Vector128.Create(AllBitsSet), new Vector4(0f) }, 27 | new object[] { Vector128.Create(235434f, -123f, 0, float.MaxValue), Vector128.Create(235434f, -123f, 0, float.MaxValue), new Vector4(0f) }, 28 | new object[] 29 | { 30 | Vector128.Create(0f, float.MinValue, float.PositiveInfinity, 1414123f), Vector128.Create(float.NaN, -0.00000000023434f, float.NegativeInfinity, 0), 31 | new Vector4(XorF(0f, float.NaN), XorF(float.MinValue, -0.00000000023434f), XorF(float.PositiveInfinity, float.NegativeInfinity), XorF(1414123f, 0f)) 32 | } 33 | }; 34 | } 35 | } 36 | 37 | [Theory] 38 | // TODO wtf 39 | #pragma warning disable xUnit1019 40 | [MemberData(nameof(Data))] 41 | #pragma warning enable xUnit1019 42 | public void Xor_Theory(Vector128 left, Vector128 right, Vector4 expected) 43 | { 44 | Vector128 vector = Vector.Xor(left, right); 45 | 46 | Assert.True(TestHelpers.AreEqual(expected, vector)); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/ComparisonTests/Equal.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.ComparisonTests 7 | { 8 | public class Equal 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new []{ true, true, true, true } }, 14 | new object[] { Vector128.Create(0f), Vector128.Create(1f), new []{ false, false, false, false } }, 15 | new object[] { Vector128.Create(0f, 1f, 0f, 1f), Vector128.Create(0f), new []{ true, false, true, false } }, 16 | new object[] { Vector128.Create(float.NaN), Vector128.Create(0f), new []{ false, false, false, false } }, 17 | new object[] 18 | { 19 | Vector128.Create(0f, float.MaxValue, float.MinValue, float.PositiveInfinity), 20 | Vector128.Create(float.Epsilon, float.MaxValue - 1E+32f, float.MinValue + 1E+32f, float.NegativeInfinity), 21 | new []{ false, false, false, false } 22 | } 23 | }; 24 | 25 | [Theory] 26 | [MemberData(nameof(Data))] 27 | public static void Equality_Theory(Vector128 left, Vector128 right, bool[] expected) 28 | { 29 | Vector128 result = Vector.CompareEqual(left, right).AsInt32(); 30 | 31 | Assert.True(AreAllEqual(expected, result)); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/ComparisonTests/GreaterThan.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.ComparisonTests 7 | { 8 | public class GreaterThan 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new []{ false, false, false, false } }, 14 | new object[] { Vector128.Create(0f), Vector128.Create(1f), new []{ false, false, false, false } }, 15 | new object[] { Vector128.Create(1f), Vector128.Create(0f), new []{ true, true, true, true } }, 16 | new object[] { Vector128.Create(1f, -1f, 1f, -1f), Vector128.Create(0f), new []{ true, false, true, false } }, 17 | new object[] { Vector128.Create(float.NaN), Vector128.Create(0f), new []{ true, true, true, true } }, 18 | new object[] 19 | { 20 | Vector128.Create(0f, float.MaxValue, float.MinValue, float.PositiveInfinity), 21 | Vector128.Create(float.Epsilon, float.MaxValue - 1E+32f, float.MinValue + 1E+32f, float.NegativeInfinity), 22 | new []{ false, true, false, true } 23 | }, 24 | new object[] 25 | { 26 | Vector128.Create(1f, 10000000f, 0.000001f, -1), 27 | Vector128.Create(1f, 10000000f, 0.000001f, -1), 28 | new []{ false, false, false, false } 29 | } 30 | }; 31 | 32 | [Theory] 33 | [MemberData(nameof(Data))] 34 | public static void GreaterThan_Theory(Vector128 left, Vector128 right, bool[] expected) 35 | { 36 | Vector128 result1 = Vector.CompareGreaterThan(left, right).AsInt32(); 37 | 38 | Assert.True(AreAllEqual(expected, result1)); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/ComparisonTests/GreaterThanOrEqualTo.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.ComparisonTests 7 | { 8 | public class GreaterThanOrEqualTo 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new []{ true, true, true, true } }, 14 | new object[] { Vector128.Create(0f), Vector128.Create(1f), new []{ false, false, false, false } }, 15 | new object[] { Vector128.Create(1f), Vector128.Create(0f), new []{ true, true, true, true } }, 16 | new object[] { Vector128.Create(1f, -1f, 1f, -1f), Vector128.Create(0f), new []{ true, false, true, false } }, 17 | new object[] { Vector128.Create(float.NaN), Vector128.Create(0f), new []{ true, true, true, true } }, 18 | new object[] 19 | { 20 | Vector128.Create(0f, float.MaxValue, float.MinValue, float.PositiveInfinity), 21 | Vector128.Create(float.Epsilon, float.MaxValue - 1E+32f, float.MinValue + 1E+32f, float.NegativeInfinity), 22 | new []{ false, true, false, true } 23 | }, 24 | new object[] 25 | { 26 | Vector128.Create(1f, 10000000f, 0.000001f, -1), 27 | Vector128.Create(1f, 10000000f, 0.000001f, -1), 28 | new []{ true, true, true, true } 29 | } 30 | }; 31 | 32 | [Theory] 33 | [MemberData(nameof(Data))] 34 | public static void GreaterThanOrEqualTo_Theory(Vector128 left, Vector128 right, bool[] expected) 35 | { 36 | Vector128 result1 = Vector.CompareGreaterThanOrEqual(left, right).AsInt32(); 37 | 38 | Assert.True(AreAllEqual(expected, result1)); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/ComparisonTests/LessThan.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.ComparisonTests 7 | { 8 | public class LessThan 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new []{ true, true, true, true } }, 14 | new object[] { Vector128.Create(0f), Vector128.Create(1f), new []{ false, false, false, false } }, 15 | new object[] { Vector128.Create(1f), Vector128.Create(0f), new []{ true, true, true, true } }, 16 | new object[] { Vector128.Create(1f, -1f, 1f, -1f), Vector128.Create(0f), new []{ true, false, true, false } }, 17 | new object[] { Vector128.Create(float.NaN), Vector128.Create(0f), new []{ true, true, true, true } }, 18 | new object[] 19 | { 20 | Vector128.Create(0f, float.MaxValue, float.MinValue, float.PositiveInfinity), 21 | Vector128.Create(float.Epsilon, float.MaxValue - 1E+32f, float.MinValue + 1E+32f, float.NegativeInfinity), 22 | new []{ false, true, false, true } 23 | }, 24 | new object[] 25 | { 26 | Vector128.Create(1f, 10000000f, 0.000001f, -1), 27 | Vector128.Create(1f, 10000000f, 0.000001f, -1), 28 | new []{ true, true, true, true } 29 | } 30 | }; 31 | 32 | [Theory] 33 | [MemberData(nameof(Data))] 34 | public static void LessThan_Theory(Vector128 left, Vector128 right, bool[] expected) 35 | { 36 | Vector128 result1 = Vector.CompareLessThan(left, right).AsInt32(); 37 | 38 | Assert.True(AreAllNotEqual(expected, result1)); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/ComparisonTests/LessThanOrEqualTo.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.ComparisonTests 7 | { 8 | public class LessThanOrEqualTo 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new []{ false, false, false, false } }, 14 | new object[] { Vector128.Create(0f), Vector128.Create(1f), new []{ false, false, false, false } }, 15 | new object[] { Vector128.Create(1f), Vector128.Create(0f), new []{ true, true, true, true } }, 16 | new object[] { Vector128.Create(1f, -1f, 1f, -1f), Vector128.Create(0f), new []{ true, false, true, false } }, 17 | new object[] { Vector128.Create(float.NaN), Vector128.Create(0f), new []{ true, true, true, true } }, 18 | new object[] 19 | { 20 | Vector128.Create(0f, float.MaxValue, float.MinValue, float.PositiveInfinity), 21 | Vector128.Create(float.Epsilon, float.MaxValue - 1E+32f, float.MinValue + 1E+32f, float.NegativeInfinity), 22 | new []{ false, true, false, true } 23 | }, 24 | new object[] 25 | { 26 | Vector128.Create(1f, 10000000f, 0.000001f, -1), 27 | Vector128.Create(1f, 10000000f, 0.000001f, -1), 28 | new []{ false, false, false, false } 29 | } 30 | }; 31 | 32 | [Theory] 33 | [MemberData(nameof(Data))] 34 | public static void LessThanOrEqualTo_Theory(Vector128 left, Vector128 right, bool[] expected) 35 | { 36 | Vector128 result1 = Vector.CompareLessThanOrEqual(left, right).AsInt32(); 37 | 38 | Assert.True(AreAllNotEqual(expected, result1)); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/ComparisonTests/NotEqual.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.Intrinsics; 3 | using Xunit; 4 | using static MathSharp.UnitTests.TestHelpers; 5 | 6 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.ComparisonTests 7 | { 8 | public class NotEqual 9 | { 10 | public static IEnumerable Data => 11 | new[] 12 | { 13 | new object[] { Vector128.Create(0f), Vector128.Create(0f), new []{ true, true, true, true } }, 14 | new object[] { Vector128.Create(0f), Vector128.Create(1f), new []{ false, false, false, false } }, 15 | new object[] { Vector128.Create(0f, 1f, 0f, 1f), Vector128.Create(0f), new []{ true, false, true, false } }, 16 | new object[] { Vector128.Create(float.NaN), Vector128.Create(0f), new []{ false, false, false, false } }, 17 | new object[] 18 | { 19 | Vector128.Create(0f, float.MaxValue, float.MinValue, float.PositiveInfinity), 20 | Vector128.Create(float.Epsilon, float.MaxValue - 1E+32f, float.MinValue + 1E+32f, float.NegativeInfinity), 21 | new []{ false, false, false, false } 22 | } 23 | }; 24 | 25 | [Theory] 26 | [MemberData(nameof(Data))] 27 | public static void Inequality_Theory(Vector128 left, Vector128 right, bool[] expected) 28 | { 29 | Vector128 result = Vector.CompareNotEqual(left, right).AsInt32(); 30 | 31 | Assert.True(AreAllNotEqual(expected, result)); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /tests/MathSharp.UnitTests/VectorTests/VectorSingle/VectorOperations/NormalizeApprox.cs: -------------------------------------------------------------------------------- 1 | namespace MathSharp.UnitTests.VectorTests.VectorSingle.VectorOperations 2 | { 3 | public class NormalizeApprox 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /vendor.yml: -------------------------------------------------------------------------------- 1 | doc/* linguist-vendored 2 | design-docs/* linguist-documentation 3 | --------------------------------------------------------------------------------