├── .gitattributes ├── .gitignore ├── Generator ├── ArrOverloader.cs ├── BaseGenerator.cs ├── DelOverloader.cs ├── Extensions.cs ├── Generator.csproj └── SyntaxReceiver.cs ├── LICENSE ├── README.md ├── SimpleSIMD.sln ├── SimpleSIMD ├── Comparison │ ├── Equal.cs │ ├── Greater.cs │ ├── GreaterOrEqual.cs │ ├── Less.cs │ └── LessOrEqual.cs ├── Delegates.cs ├── Elementwise │ ├── Abs.cs │ ├── Add.cs │ ├── And.cs │ ├── AndNot.cs │ ├── Concat.cs │ ├── Divide.cs │ ├── Multiply.cs │ ├── Negate.cs │ ├── Not.cs │ ├── Or.cs │ ├── Select.cs │ ├── Sqrt.cs │ ├── Subtract.cs │ ├── Ternary.cs │ └── Xor.cs ├── General │ ├── All.cs │ ├── Any.cs │ ├── Contains.cs │ ├── Fill.cs │ ├── Foreach.cs │ └── IndexOf.cs ├── Reduction │ ├── Aggregate.cs │ ├── Average.cs │ ├── Dot.cs │ ├── Identity.cs │ ├── Max.cs │ ├── Min.cs │ └── Sum.cs ├── SimdOps.cs └── SimpleSIMD.csproj └── Tests ├── Comparison.cs ├── Elementwise.cs ├── Tests.csproj └── Usings.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /Generator/ArrOverloader.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Immutable; 2 | using System.Text; 3 | 4 | namespace Generator; 5 | 6 | [Generator(LanguageNames.CSharp)] 7 | public class ArrOverloader : BaseGenerator 8 | { 9 | public ArrOverloader() : base("ArrOverloadAttribute", "SimpleSimd") 10 | { 11 | } 12 | 13 | protected override void ProcessMethod(StringBuilder source, IMethodSymbol methodSymbol) 14 | { 15 | string returnType = GetReturnType(methodSymbol); 16 | 17 | if (string.IsNullOrEmpty(returnType)) 18 | { 19 | ReportDiagnostic( 20 | "ASG001", 21 | $"Invalid '{AttributeName}' attribute target - method skipped for source generation.", 22 | "The method does not have a valid result span parameter. " + 23 | "The last parameter of the method must be of type Span.", 24 | methodSymbol); 25 | 26 | return; 27 | } 28 | 29 | string lengthArgument = GetLengthArgument(methodSymbol); 30 | 31 | if (string.IsNullOrEmpty(lengthArgument)) 32 | { 33 | ReportDiagnostic( 34 | "ASG002", 35 | $"Invalid '{AttributeName}' attribute target - method skipped for source generation.", 36 | "The method does not have a valid input span parameter. " + 37 | "The method must contain at least one parameter of type ReadOnlySpan.", 38 | methodSymbol); 39 | 40 | return; 41 | } 42 | 43 | string methodName = methodSymbol.Name; 44 | string accessibility = GetAccessibility(methodSymbol); 45 | string staticModifier = GetStaticModifier(methodSymbol); 46 | string arguments = GetArguments(methodSymbol); 47 | string parameters = GetParameters(methodSymbol); 48 | string generics = GetGenerics(methodSymbol); 49 | string constraints = GetConstraints(methodSymbol); 50 | 51 | _ = source.AppendLine( 52 | $$""" 53 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 54 | {{accessibility}} {{staticModifier}} {{returnType}}[] {{methodName}} {{generics}} ({{parameters}}) {{constraints}} 55 | { 56 | {{returnType}} [] result = new {{returnType}} [{{lengthArgument}}.Length]; 57 | {{methodName}} {{generics}} ({{arguments}}, result); 58 | return result; 59 | } 60 | 61 | """ 62 | ); 63 | } 64 | 65 | protected override string GetArguments(IMethodSymbol methodSymbol) 66 | { 67 | return methodSymbol.Parameters 68 | .Take(methodSymbol.Parameters.Length - 1) 69 | .Names() 70 | .CommaSeperated(); 71 | } 72 | 73 | protected override string GetParameters(IMethodSymbol methodSymbol) 74 | { 75 | return methodSymbol.Parameters 76 | .Take(methodSymbol.Parameters.Length - 1) 77 | .TypesNames() 78 | .Select(S => $"{S.Type} {S.Name}") 79 | .CommaSeperated(); 80 | } 81 | 82 | protected override string GetReturnType(IMethodSymbol methodSymbol) 83 | { 84 | if (methodSymbol.Parameters.Length == 0) 85 | { 86 | return string.Empty; 87 | } 88 | 89 | if (methodSymbol.Parameters[methodSymbol.Parameters.Length - 1].Type is not INamedTypeSymbol resultParameter) 90 | { 91 | return string.Empty; 92 | } 93 | 94 | if (resultParameter.Name != "Span") 95 | { 96 | return string.Empty; 97 | } 98 | 99 | if (resultParameter.IsGenericType == false) 100 | { 101 | return string.Empty; 102 | 103 | } 104 | 105 | return resultParameter.TypeArguments[0].Name; 106 | } 107 | 108 | private string GetLengthArgument(IMethodSymbol methodSymbol) 109 | { 110 | ImmutableArray parameterSymbols = methodSymbol.Parameters; 111 | 112 | for (int i = 0; i < parameterSymbols.Length - 1; i++) 113 | { 114 | if ((parameterSymbols[i].Type as INamedTypeSymbol)?.Name == "ReadOnlySpan") 115 | { 116 | return parameterSymbols[i].Name; 117 | } 118 | } 119 | 120 | return string.Empty; 121 | } 122 | } -------------------------------------------------------------------------------- /Generator/BaseGenerator.cs: -------------------------------------------------------------------------------- 1 | global using Microsoft.CodeAnalysis; 2 | global using System.Collections.Generic; 3 | global using System.Linq; 4 | using Microsoft.CodeAnalysis.CSharp; 5 | using Microsoft.CodeAnalysis.CSharp.Syntax; 6 | using Microsoft.CodeAnalysis.Text; 7 | using System.Text; 8 | 9 | namespace Generator; 10 | 11 | public abstract class BaseGenerator : ISourceGenerator 12 | { 13 | public string AttributeName { get; } 14 | public string AttributeNamespace { get; } 15 | 16 | private INamedTypeSymbol? attributeSymbol; 17 | private GeneratorExecutionContext? executionContext; 18 | 19 | protected BaseGenerator(string attributeName, string attributeNamespace) 20 | { 21 | AttributeName = attributeName; 22 | AttributeNamespace = attributeNamespace; 23 | } 24 | 25 | public void Initialize(GeneratorInitializationContext context) 26 | { 27 | context.RegisterForSyntaxNotifications(() => new SyntaxReceiver()); 28 | } 29 | 30 | public void Execute(GeneratorExecutionContext context) 31 | { 32 | executionContext = context; 33 | 34 | if (context.SyntaxReceiver is not SyntaxReceiver syntaxReciever) 35 | { 36 | return; 37 | } 38 | 39 | Compilation compilation = InjectAttribute(context); 40 | 41 | List methodSymbols = new(); 42 | 43 | foreach (MethodDeclarationSyntax methodDeclarations in syntaxReciever.MethodCandidates) 44 | { 45 | SemanticModel semanticModel = compilation.GetSemanticModel(methodDeclarations.SyntaxTree); 46 | IMethodSymbol? methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclarations); 47 | 48 | if (HasAttribute(methodSymbol, attributeSymbol)) 49 | { 50 | methodSymbols.Add(methodSymbol!); 51 | } 52 | } 53 | 54 | IEnumerable> methodGroups = methodSymbols.GroupBy(M => M.ContainingSymbol, SymbolEqualityComparer.Default); 55 | 56 | foreach (IGrouping methodGroup in methodGroups) 57 | { 58 | if (methodGroup.Key is not INamedTypeSymbol classSymbol) 59 | { 60 | continue; 61 | } 62 | 63 | string source = ProcessClass(classSymbol, methodGroup); 64 | 65 | context.AddSource(ToFileName(classSymbol.Name), SourceText.From(source, Encoding.UTF8)); 66 | } 67 | } 68 | 69 | private Compilation InjectAttribute(GeneratorExecutionContext context) 70 | { 71 | SourceText source = SourceText.From( 72 | $$""" 73 | using System; 74 | 75 | namespace {{AttributeNamespace}}; 76 | 77 | /// 78 | /// An attribute marking a method as a candidate for source generator. 79 | /// 80 | [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] 81 | public sealed class {{AttributeName}} : Attribute 82 | { 83 | } 84 | """ 85 | , Encoding.UTF8); 86 | 87 | context.AddSource(ToFileName(AttributeName), source); 88 | 89 | CSharpParseOptions? options = context.Compilation.SyntaxTrees.First().Options as CSharpParseOptions; 90 | 91 | SyntaxTree Syntaxtree = CSharpSyntaxTree.ParseText(source, options); 92 | 93 | Compilation compilation = context.Compilation.AddSyntaxTrees(Syntaxtree); 94 | 95 | attributeSymbol = compilation.GetTypeByMetadataName($"{AttributeNamespace}.{AttributeName}"); 96 | 97 | return compilation; 98 | } 99 | 100 | private bool HasAttribute(IMethodSymbol? methodSymbol, INamedTypeSymbol? attributeSymbol) 101 | { 102 | if (methodSymbol == null) 103 | { 104 | return false; 105 | } 106 | 107 | foreach (INamedTypeSymbol? candidateSymbol in methodSymbol.GetAttributes().Select(A => A.AttributeClass)) 108 | { 109 | if (candidateSymbol?.Equals(attributeSymbol, SymbolEqualityComparer.Default) ?? false) 110 | { 111 | return true; 112 | } 113 | } 114 | 115 | return false; 116 | } 117 | 118 | protected void ReportDiagnostic(string id, string title, string message, ISymbol? sourceSymbol) 119 | { 120 | DiagnosticDescriptor descriptor = new(id, title, message, $"SourceGenerator.{GetType().Name}", DiagnosticSeverity.Warning, true); 121 | 122 | Location? location = sourceSymbol?.DeclaringSyntaxReferences[0].GetSyntax().GetLocation(); 123 | 124 | executionContext?.ReportDiagnostic(Diagnostic.Create(descriptor, location)); 125 | } 126 | 127 | private string ProcessClass(INamedTypeSymbol classSymbol, IEnumerable classMethods) 128 | { 129 | INamespaceSymbol? namespaceSymbol = classSymbol.ContainingNamespace; 130 | 131 | if (namespaceSymbol is null) 132 | { 133 | ReportDiagnostic( 134 | "SG001", 135 | $"Invalid '{AttributeName}' attribute target - class or struct skipped for source generation.", 136 | "Invalid class or struct namespace for source generation. Class or struct can't be top level.", 137 | classSymbol); 138 | 139 | return string.Empty; 140 | } 141 | 142 | string accessibility = GetAccessibility(classSymbol); 143 | 144 | if (accessibility is not "public" or "internal") 145 | { 146 | ReportDiagnostic( 147 | "SG002", 148 | $"Invalid '{AttributeName}' attribute target - class or struct skipped for source generation.", 149 | "Invalid class or struct accessibility for source generation. Accessibility must be internal or public.", 150 | classSymbol); 151 | 152 | return string.Empty; 153 | } 154 | 155 | string staticModifier = GetStaticModifier(classSymbol); 156 | string generics = GetGenerics(classSymbol); 157 | 158 | StringBuilder source = new( 159 | $$""" 160 | #nullable enable 161 | 162 | using System; 163 | using System.Numerics; 164 | using System.Runtime.CompilerServices; 165 | 166 | namespace {{namespaceSymbol.ToDisplayString()}}; 167 | 168 | {{accessibility}} {{staticModifier}} partial class {{classSymbol.Name}} {{generics}} 169 | { 170 | 171 | """ 172 | ); 173 | 174 | foreach (IMethodSymbol method in classMethods) 175 | { 176 | ProcessMethod(source, method); 177 | } 178 | 179 | _ = source.AppendLine("}"); 180 | 181 | return source.ToString(); 182 | } 183 | 184 | protected abstract void ProcessMethod(StringBuilder source, IMethodSymbol methodSymbol); 185 | 186 | private string ToFileName(string name) 187 | { 188 | return $"{name}.g.cs"; 189 | } 190 | 191 | private string GetGenerics(INamedTypeSymbol classSymbol) 192 | { 193 | if (classSymbol.IsGenericType == false) 194 | { 195 | return string.Empty; 196 | } 197 | 198 | return $"<{classSymbol.TypeParameters.Names().CommaSeperated()}>"; 199 | } 200 | 201 | protected string GetAccessibility(ISymbol symbol) 202 | { 203 | return SyntaxFacts.GetText(symbol.DeclaredAccessibility); 204 | } 205 | 206 | protected string GetStaticModifier(ISymbol symbol) 207 | { 208 | return symbol.IsStatic ? "static" : string.Empty; 209 | } 210 | 211 | protected virtual string GetReturnType(IMethodSymbol methodSymbol) 212 | { 213 | return methodSymbol.ReturnType.ToDisplayString(); 214 | } 215 | 216 | protected virtual string GetArguments(IMethodSymbol methodSymbol) 217 | { 218 | return methodSymbol.Parameters.Names().CommaSeperated(); 219 | } 220 | 221 | protected virtual string GetParameters(IMethodSymbol methodSymbol) 222 | { 223 | return methodSymbol.Parameters 224 | .TypesNames() 225 | .Select(S => $"{S.Type} {S.Name}") 226 | .CommaSeperated(); 227 | } 228 | 229 | protected virtual string GetGenerics(IMethodSymbol methodSymbol) 230 | { 231 | if (methodSymbol.IsGenericMethod == false) 232 | { 233 | return string.Empty; 234 | } 235 | 236 | return $"<{methodSymbol.TypeParameters.Names().CommaSeperated()}>"; 237 | } 238 | 239 | protected virtual string GetConstraints(IMethodSymbol methodSymbol) 240 | { 241 | StringBuilder builder = new(); 242 | 243 | foreach (ITypeParameterSymbol typeSymbol in methodSymbol.TypeParameters) 244 | { 245 | _ = builder.Append(GetConstraints(typeSymbol)); 246 | } 247 | 248 | return builder.ToString(); 249 | } 250 | 251 | protected string GetConstraints(ITypeParameterSymbol typeSymbol) 252 | { 253 | IEnumerable constraints = EnumerateConstraints(typeSymbol); 254 | 255 | if (constraints.Any()) 256 | { 257 | return $"where {typeSymbol.Name} : {constraints.CommaSeperated()} "; 258 | } 259 | 260 | return string.Empty; 261 | } 262 | 263 | private IEnumerable EnumerateConstraints(ITypeParameterSymbol typeSymbol) 264 | { 265 | if (typeSymbol.HasNotNullConstraint) 266 | { 267 | yield return "notnull"; 268 | } 269 | else if (typeSymbol.HasUnmanagedTypeConstraint) 270 | { 271 | yield return "unmanaged"; 272 | } 273 | else if (typeSymbol.HasValueTypeConstraint) 274 | { 275 | yield return "struct"; 276 | } 277 | else if (typeSymbol.HasReferenceTypeConstraint) 278 | { 279 | yield return "class"; 280 | } 281 | 282 | if (typeSymbol.HasConstructorConstraint) 283 | { 284 | yield return "new()"; 285 | } 286 | 287 | foreach (string name in typeSymbol.ConstraintTypes.Types()) 288 | { 289 | yield return name; 290 | } 291 | } 292 | } -------------------------------------------------------------------------------- /Generator/DelOverloader.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis.CSharp.Syntax; 2 | using System.Text; 3 | 4 | namespace Generator; 5 | 6 | [Generator(LanguageNames.CSharp)] 7 | public class DelOverloader : BaseGenerator 8 | { 9 | public DelOverloader() : base("DelOverloadAttribute", "SimpleSimd") 10 | { 11 | } 12 | 13 | protected override void ProcessMethod(StringBuilder source, IMethodSymbol methodSymbol) 14 | { 15 | if (methodSymbol.TypeParameters.All(S => !IsValueDelegate(S))) 16 | { 17 | ReportDiagnostic( 18 | "DSG001", 19 | $"Invalid '{AttributeName}' attribute target - method skipped for source generation.", 20 | "The method does not have a parameter constrained to be a Value-Delegate. " + 21 | "At least one of the parameters must be a generic type constrained as IFunc or IAction.", 22 | methodSymbol); 23 | 24 | return; 25 | } 26 | 27 | string methodBody = GetMethodBody(methodSymbol); 28 | 29 | if (string.IsNullOrEmpty(methodBody)) 30 | { 31 | return; 32 | } 33 | 34 | string methodName = methodSymbol.Name; 35 | string accessibility = GetAccessibility(methodSymbol); 36 | string staticModifier = GetStaticModifier(methodSymbol); 37 | string returnType = GetReturnType(methodSymbol); 38 | string parameters = GetParameters(methodSymbol); 39 | string generics = GetGenerics(methodSymbol); 40 | string constraints = GetConstraints(methodSymbol); 41 | 42 | _ = source.AppendLine($"\t{accessibility} {staticModifier} {returnType} {methodName} {generics} ({parameters}) {constraints}\n{methodBody}"); 43 | } 44 | 45 | protected override string GetGenerics(IMethodSymbol methodSymbol) 46 | { 47 | IEnumerable generics = methodSymbol.TypeParameters 48 | .Where(P => !IsValueDelegate(P)) 49 | .Names(); 50 | 51 | return generics.Any() ? $"<{generics.CommaSeperated()}>" : string.Empty; 52 | } 53 | 54 | protected override string GetParameters(IMethodSymbol methodSymbol) 55 | { 56 | string parameters = base.GetParameters(methodSymbol); 57 | 58 | foreach (ITypeParameterSymbol typeSymbol in methodSymbol.TypeParameters) 59 | { 60 | foreach (ITypeSymbol constraintSymbol in typeSymbol.ConstraintTypes) 61 | { 62 | if (constraintSymbol.Name == "IFunc") 63 | { 64 | parameters = parameters.Replace(typeSymbol.Name, 65 | constraintSymbol 66 | .ToDisplayString() 67 | .Replace("SimpleSimd.IFunc", "System.Func")); 68 | } 69 | else if (constraintSymbol.Name == "IAction") 70 | { 71 | parameters = parameters.Replace(typeSymbol.Name, 72 | constraintSymbol 73 | .ToDisplayString() 74 | .Replace("SimpleSimd.IAction", "System.Action")); 75 | } 76 | } 77 | } 78 | 79 | return parameters; 80 | } 81 | 82 | protected override string GetConstraints(IMethodSymbol methodSymbol) 83 | { 84 | StringBuilder constraints = new(); 85 | 86 | foreach (ITypeParameterSymbol typeSymbol in methodSymbol.TypeParameters) 87 | { 88 | if (IsValueDelegate(typeSymbol) == false) 89 | { 90 | _ = constraints.Append(GetConstraints(typeSymbol)); 91 | } 92 | } 93 | 94 | return constraints.ToString(); 95 | } 96 | 97 | protected bool IsValueDelegate(ITypeParameterSymbol typeSymbol) 98 | { 99 | foreach (ITypeSymbol C in typeSymbol.ConstraintTypes) 100 | { 101 | if (C.Name is "IAction" or "IFunc") 102 | { 103 | return true; 104 | } 105 | } 106 | 107 | return false; 108 | } 109 | 110 | protected string GetMethodBody(IMethodSymbol methodSymbol) 111 | { 112 | MethodDeclarationSyntax? methodNode = methodSymbol.DeclaringSyntaxReferences[0].GetSyntax() as MethodDeclarationSyntax; 113 | 114 | return methodNode?.Body?.GetText().ToString() ?? string.Empty; 115 | } 116 | } -------------------------------------------------------------------------------- /Generator/Extensions.cs: -------------------------------------------------------------------------------- 1 | namespace Generator; 2 | 3 | internal static class Extensions 4 | { 5 | internal static IEnumerable<(string Type, string Name)> TypesNames(this IEnumerable? symbols) 6 | { 7 | return symbols?.Select(S => (S.ToDisplayString(), S.Name)) ?? Enumerable.Empty<(string, string)>(); 8 | } 9 | 10 | internal static IEnumerable Names(this IEnumerable? symbols) 11 | { 12 | return symbols?.Select(S => S.Name) ?? Enumerable.Empty(); 13 | } 14 | 15 | internal static IEnumerable Types(this IEnumerable? symbols) 16 | { 17 | return symbols?.Select(S => S.ToDisplayString()) ?? Enumerable.Empty(); 18 | } 19 | 20 | internal static string CommaSeperated(this IEnumerable? strings) 21 | { 22 | return strings != null ? string.Join(", ", strings) : string.Empty; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Generator/Generator.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0 5 | disable 6 | 11 7 | enable 8 | Library 9 | True 10 | latest-recommended 11 | True 12 | false 13 | 14 | 15 | 16 | 17 | 18 | all 19 | runtime; build; native; contentfiles; analyzers; buildtransitive 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Generator/SyntaxReceiver.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis.CSharp.Syntax; 2 | 3 | namespace Generator; 4 | 5 | internal class SyntaxReceiver : ISyntaxReceiver 6 | { 7 | public List MethodCandidates { get; } = new(); 8 | 9 | public void OnVisitSyntaxNode(SyntaxNode syntaxNode) 10 | { 11 | if (syntaxNode is not MethodDeclarationSyntax methodNode) 12 | { 13 | return; 14 | } 15 | 16 | if (methodNode.AttributeLists.Count == 0) 17 | { 18 | return; 19 | } 20 | 21 | MethodCandidates.Add(methodNode); 22 | } 23 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Gilad Freidkin 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimpleSIMD 2 | 3 | [![NuGet version (SimpleSIMD)](https://img.shields.io/nuget/v/SimpleSIMD.svg?style=flat-square)](https://www.nuget.org/packages/SimpleSIMD/) 4 | 5 | ### What is SIMD? 6 | Single Instruction, Multiple Data (SIMD) units refer to hardware components that perform the same operation on multiple data operands concurrently. 7 | The concurrency is performed on a single thread, while utilizing the full size of the processor register to perform several operations at one. 8 | This approach could be combined with standard multithreading for massive performence boosts in numeric computations. 9 | 10 | ## Goals And Purpose 11 | * Single API to unify SIMD for **All supported types** 12 | * Gain performence boost for mathematical computations using a simple API 13 | * Simplifies SIMD usage, and to make it easy to integrate it into an already existing solutions 14 | * Helps generalize several methemathical functions for supported types 15 | * Performs less allocations compared to standard LINQ implementations 16 | 17 | ## Available Functions 18 | 19 |
20 |

Comparison

21 | 22 | * Equal 23 | * Greater 24 | * GreaterOrEqual 25 | * Less 26 | * LessOrEqual 27 |
28 |
29 |

Elementwise

30 | 31 | * Negate 32 | * Abs 33 | * Add 34 | * Divide 35 | * Multiply 36 | * Subtract 37 | * And 38 | * AndNot 39 | * Or 40 | * Xor 41 | * Not 42 | * Select 43 | * Ternary (Conditional Select) 44 | * Concat 45 | * Sqrt 46 |
47 |
48 |

Reduction

49 | 50 | * Aggregate 51 | * Sum 52 | * Average 53 | * Max 54 | * Min 55 | * Dot 56 |
57 |
58 |

General

59 | 60 | * All 61 | * Any 62 | * Contains 63 | * IndexOf 64 | * Fill 65 | * Foreach 66 |
67 | 68 | ### Auto-Generated Functions 69 | For any of the ``Elementwise`` functions, an auto-generated overload is created, which doesn't accept ```Span result```, 70 | and instead returns ```T[]``` as the result. 71 | 72 | For any of the functions with the Value Delagate pattern, an auto-generated overload is created, which accepts regular delegates. 73 | Note that using this overload results in performence losses. Check [Value Delegates](#value-delegates) section for more info. 74 | 75 | ## Performance Benefits 76 | 77 | A simple benchmark to demonstrate performance gains of using SIMD. 78 | Benchmarked method was a ``Sum`` over an ``int[]``. 79 | 80 | | Method | Length | Mean | Error | StdDev | Median | Ratio | 81 | |------- |------- |--------------:|------------:|------------:|--------------:|------:| 82 | | SIMD | 10 | 3.556 ns | 0.0655 ns | 0.0581 ns | 3.537 ns | 0.66 | 83 | | Naive | 10 | 5.357 ns | 0.0568 ns | 0.0531 ns | 5.348 ns | 1.00 | 84 | | SIMD | 100 | 9.079 ns | 0.1948 ns | 0.1822 ns | 9.032 ns | 0.20 | 85 | | Naive | 100 | 46.178 ns | 0.5255 ns | 0.4658 ns | 46.203 ns | 1.00 | 86 | | SIMD | 1000 | 66.018 ns | 0.6931 ns | 0.6483 ns | 65.802 ns | 0.17 | 87 | | Naive | 1000 | 388.244 ns | 3.0852 ns | 2.8859 ns | 389.093 ns | 1.00 | 88 | | SIMD | 3000 | 185.507 ns | 1.3070 ns | 1.1587 ns | 185.375 ns | 0.16 | 89 | | Naive | 3000 | 1,139.552 ns | 11.9608 ns | 11.1881 ns | 1,139.374 ns | 1.00 | 90 | | SIMD | 6000 | 365.993 ns | 3.2114 ns | 3.0039 ns | 365.075 ns | 0.16 | 91 | | Naive | 6000 | 2,274.374 ns | 14.2898 ns | 12.6675 ns | 2,271.185 ns | 1.00 | 92 | | SIMD | 10000 | 585.275 ns | 5.2631 ns | 4.1091 ns | 586.638 ns | 0.15 | 93 | | Naive | 10000 | 3,938.198 ns | 46.8599 ns | 43.8328 ns | 3,926.622 ns | 1.00 | 94 | | SIMD | 30000 | 1,791.966 ns | 30.4379 ns | 48.2777 ns | 1,778.255 ns | 0.15 | 95 | | Naive | 30000 | 11,848.767 ns | 184.5488 ns | 163.5977 ns | 11,773.515 ns | 1.00 | 96 | | SIMD | 60000 | 3,612.872 ns | 71.7281 ns | 113.7683 ns | 3,580.606 ns | 0.15 | 97 | | Naive | 60000 | 23,606.125 ns | 249.0765 ns | 232.9863 ns | 23,542.178 ns | 1.00 | 98 | | SIMD | 100000 | 7,325.734 ns | 156.6350 ns | 451.9279 ns | 7,138.866 ns | 0.19 | 99 | | Naive | 100000 | 40,283.073 ns | 464.1261 ns | 434.1439 ns | 40,328.790 ns | 1.00 | 100 | 101 |
102 | Benchmark Details 103 | 104 | ``` 105 | BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.819) 106 | Intel Core i7-10510U CPU 1.80GHz, 1 CPU, 8 logical and 4 physical cores 107 | .NET SDK=7.0.100 108 | [Host] : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 109 | DefaultJob : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 110 | ``` 111 |
112 | 113 | ## Value Delegates 114 | This library uses the value delegate pattern. This pattern is used as a replacement for regular delegates. 115 | Calling functions using this patten may feel unusual since it requires creation of structs to pass as arguments instead of delegates, but it is very beneficial performance-wise. 116 | The performance difference makes using this pattern worthwhile in performance critical places. 117 | Since the focus of this library is **pure performance**, we use this pattern wherever possible. 118 | 119 | #### Usage: 120 | 121 | ``` csharp 122 | using System; 123 | using System.Numerics; 124 | using SimpleSimd; 125 | 126 | namespace MyProgram 127 | { 128 | class Program 129 | { 130 | static void Main() 131 | { 132 | // Creating the data 133 | // Can be int[], Span, ReadOnlySpan 134 | int[] Data = GetData(); 135 | 136 | // We need to create 2 structs which will serve as a replacement for delegates 137 | SimdOps.Sum(Data, new VecSelector(), new Selector()); 138 | } 139 | } 140 | 141 | // A struct which is used as Vector selector 142 | // Inheritence from IFunc, Vector> is according to Sum() signature 143 | struct VecSelector : IFunc, Vector> 144 | { 145 | public Vector Invoke(Vector param) => DoSomething(param); 146 | } 147 | 148 | // A struct which is used as int selector 149 | // Inheritence from IFunc is according to Sum() signature 150 | struct Selector : IFunc 151 | { 152 | public int Invoke(int param) => DoSomething(param); 153 | } 154 | } 155 | ``` 156 | 157 | #### benchmark: 158 | 159 | Both of the benchmarked methods have the exactly same code, both of them are accelerated using SIMD, 160 | the only difference is the argument types. 161 | 162 | ``` csharp 163 | // Delegate, baseline 164 | public static T Sum(ReadOnlySpan span, Func, Vector> vSelector, Func selector) 165 | where T : struct, INumber; 166 | 167 | // ValueDelegate 168 | public static T Sum(ReadOnlySpan span, F1 vSelector, F2 selector) 169 | where T : struct, INumber 170 | where F1 : struct, IFunc, Vector> 171 | where F2 : struct, IFunc; 172 | ``` 173 | 174 | | Method | Length | Mean | Error | StdDev | Median | Ratio | 175 | |-------------- |------- |--------------:|------------:|------------:|--------------:|------:| 176 | | Delegate | 10 | 9.477 ns | 0.0910 ns | 0.0851 ns | 9.467 ns | 1.00 | 177 | | ValueDelegate | 10 | 3.969 ns | 0.1078 ns | 0.1107 ns | 3.961 ns | 0.42 | 178 | | Delegate | 100 | 37.747 ns | 0.6666 ns | 0.6236 ns | 37.698 ns | 1.00 | 179 | | ValueDelegate | 100 | 9.295 ns | 0.1697 ns | 0.1587 ns | 9.276 ns | 0.25 | 180 | | Delegate | 1000 | 264.978 ns | 5.2711 ns | 4.9306 ns | 263.820 ns | 1.00 | 181 | | ValueDelegate | 1000 | 66.474 ns | 1.0799 ns | 1.0101 ns | 66.471 ns | 0.25 | 182 | | Delegate | 3000 | 773.737 ns | 11.6963 ns | 10.9407 ns | 773.347 ns | 1.00 | 183 | | ValueDelegate | 3000 | 186.632 ns | 3.7407 ns | 4.1578 ns | 185.751 ns | 0.24 | 184 | | Delegate | 6000 | 1,554.745 ns | 26.9752 ns | 25.2326 ns | 1,559.120 ns | 1.00 | 185 | | ValueDelegate | 6000 | 369.259 ns | 6.3982 ns | 5.6719 ns | 368.428 ns | 0.24 | 186 | | Delegate | 10000 | 2,612.493 ns | 51.2703 ns | 47.9583 ns | 2,615.721 ns | 1.00 | 187 | | ValueDelegate | 10000 | 624.057 ns | 12.4864 ns | 16.2358 ns | 622.558 ns | 0.24 | 188 | | Delegate | 30000 | 8,718.167 ns | 173.5442 ns | 170.4436 ns | 8,719.592 ns | 1.00 | 189 | | ValueDelegate | 30000 | 1,860.125 ns | 35.8075 ns | 47.8020 ns | 1,865.076 ns | 0.22 | 190 | | Delegate | 60000 | 17,259.904 ns | 330.4238 ns | 429.6443 ns | 17,109.451 ns | 1.00 | 191 | | ValueDelegate | 60000 | 3,715.645 ns | 72.8741 ns | 121.7563 ns | 3,689.114 ns | 0.22 | 192 | | Delegate | 100000 | 27,357.138 ns | 534.2404 ns | 548.6255 ns | 27,176.126 ns | 1.00 | 193 | | ValueDelegate | 100000 | 7,485.716 ns | 150.0830 ns | 440.1676 ns | 7,313.833 ns | 0.27 | 194 | 195 |
196 | Benchmark Details 197 | 198 | ``` 199 | BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.819) 200 | Intel Core i7-10510U CPU 1.80GHz, 1 CPU, 8 logical and 4 physical cores 201 | .NET SDK=7.0.100 202 | [Host] : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 203 | DefaultJob : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 204 | ``` 205 |
206 | 207 | ## Limitations 208 | * Methods are not lazily evaluated as IEnumerable 209 | * Old hardware might not support SIMD 210 | * Supported collection types: 211 | * ```T[]``` 212 | * ```Span``` 213 | * ```ReadOnlySpan``` 214 | * Supports only **Primitive Numeric Types** as array elements. Supported types are: 215 | * ```byte, sbyte``` 216 | * ```short, ushort``` 217 | * ```int, uint``` 218 | * ```long, ulong``` 219 | * ```nint, nuint``` 220 | * ```float``` 221 | * ```double``` 222 | 223 | ## Contributing 224 | **All ideas and suggestions are welcome.** 225 | Feel free to open an issue if you have an idea or a suggestion that might improve this project. 226 | If you encounter a bug or have a feature request, please open a relevent issue. 227 | 228 | ## License 229 | This project is licensed under MIT license. For more info see the [License File](LICENSE) 230 | -------------------------------------------------------------------------------- /SimpleSIMD.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31612.314 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleSIMD", "SimpleSIMD\SimpleSIMD.csproj", "{B8C57B9A-F7FC-4F91-B28B-974A1DA0BC78}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator", "Generator\Generator.csproj", "{6E73E13B-20BB-49DC-AEBB-BD7F32981153}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{90BA800F-925C-4220-9619-99B6C2425443}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {B8C57B9A-F7FC-4F91-B28B-974A1DA0BC78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {B8C57B9A-F7FC-4F91-B28B-974A1DA0BC78}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {B8C57B9A-F7FC-4F91-B28B-974A1DA0BC78}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {B8C57B9A-F7FC-4F91-B28B-974A1DA0BC78}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {6E73E13B-20BB-49DC-AEBB-BD7F32981153}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {6E73E13B-20BB-49DC-AEBB-BD7F32981153}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {6E73E13B-20BB-49DC-AEBB-BD7F32981153}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {6E73E13B-20BB-49DC-AEBB-BD7F32981153}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {90BA800F-925C-4220-9619-99B6C2425443}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {90BA800F-925C-4220-9619-99B6C2425443}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {90BA800F-925C-4220-9619-99B6C2425443}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {90BA800F-925C-4220-9619-99B6C2425443}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {CC39C22B-E7AA-4D76-967D-278543F581E1} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /SimpleSIMD/Comparison/Equal.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static bool Equal(ReadOnlySpan left, T right) where T : struct, INumber 6 | { 7 | return All(left, right, new Equal_VSelector(), new Equal_Selector()); 8 | } 9 | 10 | public static bool Equal(ReadOnlySpan left, ReadOnlySpan right) where T : struct, INumber 11 | { 12 | return All(left, right, new Equal_VSelector(), new Equal_Selector()); 13 | } 14 | } 15 | 16 | file struct Equal_VSelector : IFunc, Vector, bool> where T : struct, INumber 17 | { 18 | public bool Invoke(Vector left, Vector right) 19 | { 20 | return left == right; 21 | } 22 | } 23 | 24 | file struct Equal_Selector : IFunc where T : struct, INumber 25 | { 26 | public bool Invoke(T left, T right) 27 | { 28 | return left == right; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /SimpleSIMD/Comparison/Greater.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static bool Greater(ReadOnlySpan left, T right) where T : struct, INumber 6 | { 7 | return All(left, right, new Greater_VSelector(), new Greater_Selector()); 8 | } 9 | 10 | public static bool Greater(ReadOnlySpan left, ReadOnlySpan right) where T : struct, INumber 11 | { 12 | return All(left, right, new Greater_VSelector(), new Greater_Selector()); 13 | } 14 | } 15 | 16 | file struct Greater_VSelector : IFunc, Vector, bool> where T : struct, INumber 17 | { 18 | public bool Invoke(Vector left, Vector right) 19 | { 20 | return Vector.GreaterThanAll(left, right); 21 | } 22 | } 23 | 24 | file struct Greater_Selector : IFunc where T : struct, INumber 25 | { 26 | public bool Invoke(T left, T right) 27 | { 28 | return left > right; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /SimpleSIMD/Comparison/GreaterOrEqual.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static bool GreaterOrEqual(ReadOnlySpan left, T right) where T : struct, INumber 6 | { 7 | return !Less(left, right); 8 | } 9 | 10 | public static bool GreaterOrEqual(ReadOnlySpan left, ReadOnlySpan right) where T : struct, INumber 11 | { 12 | return !Less(left, right); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /SimpleSIMD/Comparison/Less.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static bool Less(ReadOnlySpan left, T right) where T : struct, INumber 6 | { 7 | return All(left, right, new Less_VSelector(), new Less_Selector()); 8 | } 9 | 10 | public static bool Less(ReadOnlySpan left, ReadOnlySpan right) where T : struct, INumber 11 | { 12 | return All(left, right, new Less_VSelector(), new Less_Selector()); 13 | } 14 | } 15 | 16 | file struct Less_VSelector : IFunc, Vector, bool> where T : struct, INumber 17 | { 18 | public bool Invoke(Vector left, Vector right) 19 | { 20 | return Vector.LessThanAll(left, right); 21 | } 22 | } 23 | 24 | file struct Less_Selector : IFunc where T : struct, INumber 25 | { 26 | public bool Invoke(T left, T right) 27 | { 28 | return left < right; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /SimpleSIMD/Comparison/LessOrEqual.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static bool LessOrEqual(ReadOnlySpan left, T right) where T : struct, INumber 6 | { 7 | return !Greater(left, right); 8 | } 9 | 10 | public static bool LessOrEqual(ReadOnlySpan left, ReadOnlySpan right) where T : struct, INumber 11 | { 12 | return !Greater(left, right); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /SimpleSIMD/Delegates.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public interface IAction where T : struct 4 | { 5 | void Invoke(T param); 6 | } 7 | 8 | public interface IFunc where TRes : struct 9 | { 10 | TRes Invoke(); 11 | } 12 | 13 | public interface IFunc where T : struct where TRes : struct 14 | { 15 | TRes Invoke(T param); 16 | } 17 | 18 | public interface IFunc where T1 : struct where T2 : struct where TRes : struct 19 | { 20 | TRes Invoke(T1 param1, T2 param2); 21 | } -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Abs.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Abs(ReadOnlySpan span, Span result) where T : struct, INumber 7 | { 8 | Select(span, new Abs_VSelector(), new Abs_Selector(), result); 9 | } 10 | } 11 | 12 | file struct Abs_VSelector : IFunc, Vector> where T : struct, INumber 13 | { 14 | public Vector Invoke(Vector vec) 15 | { 16 | return Vector.Abs(vec); 17 | } 18 | } 19 | 20 | file struct Abs_Selector : IFunc where T : struct, INumber 21 | { 22 | public T Invoke(T val) 23 | { 24 | return T.Abs(val); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Add.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Add(ReadOnlySpan left, T right, Span result) where T : struct, INumber 7 | { 8 | Concat(left, right, new Add_VSelector(), new Add_Selector(), result); 9 | } 10 | 11 | [ArrOverload] 12 | public static void Add(ReadOnlySpan left, ReadOnlySpan right, Span result) where T : struct, INumber 13 | { 14 | Concat(left, right, new Add_VSelector(), new Add_Selector(), result); 15 | } 16 | } 17 | 18 | file struct Add_VSelector : IFunc, Vector, Vector> where T : struct, INumber 19 | { 20 | public Vector Invoke(Vector left, Vector right) 21 | { 22 | return left + right; 23 | } 24 | } 25 | 26 | file struct Add_Selector : IFunc where T : struct, INumber 27 | { 28 | public T Invoke(T left, T right) 29 | { 30 | return left + right; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/And.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void And(ReadOnlySpan left, T right, Span result) where T : struct, IBinaryNumber 7 | { 8 | Concat(left, right, new And_VSelector(), new And_Selector(), result); 9 | } 10 | 11 | [ArrOverload] 12 | public static void And(ReadOnlySpan left, ReadOnlySpan right, Span result) where T : struct, IBinaryNumber 13 | { 14 | Concat(left, right, new And_VSelector(), new And_Selector(), result); 15 | } 16 | } 17 | 18 | file struct And_VSelector : IFunc, Vector, Vector> where T : struct, IBinaryNumber 19 | { 20 | public Vector Invoke(Vector left, Vector right) 21 | { 22 | return left & right; 23 | } 24 | } 25 | 26 | file struct And_Selector : IFunc where T : struct, IBinaryNumber 27 | { 28 | public T Invoke(T left, T right) 29 | { 30 | return left & right; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/AndNot.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void AndNot(ReadOnlySpan left, T right, Span result) where T : struct, IBinaryNumber 7 | { 8 | Concat(left, right, new AndNot_VSelector(), new AndNot_Selector(), result); 9 | } 10 | 11 | [ArrOverload] 12 | public static void AndNot(ReadOnlySpan left, ReadOnlySpan right, Span result) where T : struct, IBinaryNumber 13 | { 14 | Concat(left, right, new AndNot_VSelector(), new AndNot_Selector(), result); 15 | } 16 | } 17 | 18 | file struct AndNot_VSelector : IFunc, Vector, Vector> where T : struct, IBinaryNumber 19 | { 20 | public Vector Invoke(Vector left, Vector right) 21 | { 22 | return Vector.AndNot(left, right); 23 | } 24 | } 25 | 26 | file struct AndNot_Selector : IFunc where T : struct, IBinaryNumber 27 | { 28 | public T Invoke(T left, T right) 29 | { 30 | return left & ~right; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Concat.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | [DelOverload] 7 | public static void Concat(ReadOnlySpan left, T right, F1 vCombiner, F2 combiner, Span result) 8 | where T : struct, INumber 9 | where TRes : struct, INumber 10 | where F1 : struct, IFunc, Vector, Vector> 11 | where F2 : struct, IFunc 12 | { 13 | if (result.Length != left.Length) 14 | { 15 | ThrowArgOutOfRange(nameof(result)); 16 | } 17 | 18 | ref T rLeft = ref GetRef(left); 19 | ref TRes rResult = ref GetRef(result); 20 | 21 | int i = 0; 22 | 23 | if (Vector.IsHardwareAccelerated) 24 | { 25 | Vector vRight = new(right); 26 | 27 | ref Vector vrLeft = ref AsVector(rLeft); 28 | ref Vector vrResult = ref AsVector(rResult); 29 | 30 | int length = left.Length / Vector.Count; 31 | 32 | for (; i < length; i++) 33 | { 34 | vrResult.Offset(i) = vCombiner.Invoke(vrLeft.Offset(i), vRight); 35 | } 36 | 37 | i *= Vector.Count; 38 | } 39 | 40 | for (; i < left.Length; i++) 41 | { 42 | rResult.Offset(i) = combiner.Invoke(rLeft.Offset(i), right); 43 | } 44 | } 45 | 46 | [ArrOverload] 47 | [DelOverload] 48 | public static void Concat(T left, ReadOnlySpan right, F1 vCombiner, F2 combiner, Span result) 49 | where T : struct, INumber 50 | where TRes : struct, INumber 51 | where F1 : struct, IFunc, Vector, Vector> 52 | where F2 : struct, IFunc 53 | { 54 | if (result.Length != right.Length) 55 | { 56 | ThrowArgOutOfRange(nameof(result)); 57 | } 58 | 59 | ref T rRight = ref GetRef(right); 60 | ref TRes rResult = ref GetRef(result); 61 | 62 | int i = 0; 63 | 64 | if (Vector.IsHardwareAccelerated) 65 | { 66 | Vector vLeft = new(left); 67 | 68 | ref Vector vrRight = ref AsVector(rRight); 69 | ref Vector vrResult = ref AsVector(rResult); 70 | 71 | int length = right.Length / Vector.Count; 72 | 73 | for (; i < length; i++) 74 | { 75 | vrResult.Offset(i) = vCombiner.Invoke(vLeft, vrRight.Offset(i)); 76 | } 77 | 78 | i *= Vector.Count; 79 | } 80 | 81 | for (; i < right.Length; i++) 82 | { 83 | rResult.Offset(i) = combiner.Invoke(left, rRight.Offset(i)); 84 | } 85 | } 86 | 87 | [ArrOverload] 88 | [DelOverload] 89 | public static void Concat(ReadOnlySpan left, ReadOnlySpan right, F1 vCombiner, F2 combiner, Span result) 90 | where T : struct, INumber 91 | where TRes : struct, INumber 92 | where F1 : struct, IFunc, Vector, Vector> 93 | where F2 : struct, IFunc 94 | { 95 | if (right.Length != left.Length) 96 | { 97 | ThrowArgOutOfRange(nameof(right)); 98 | } 99 | 100 | if (result.Length != left.Length) 101 | { 102 | ThrowArgOutOfRange(nameof(result)); 103 | } 104 | 105 | ref T rLeft = ref GetRef(left); 106 | ref T rRight = ref GetRef(right); 107 | ref TRes rResult = ref GetRef(result); 108 | 109 | int i = 0; 110 | 111 | if (Vector.IsHardwareAccelerated) 112 | { 113 | ref Vector vrLeft = ref AsVector(rLeft); 114 | ref Vector vrRight = ref AsVector(rRight); 115 | ref Vector vrResult = ref AsVector(rResult); 116 | 117 | int length = left.Length / Vector.Count; 118 | 119 | for (; i < length; i++) 120 | { 121 | vrResult.Offset(i) = vCombiner.Invoke(vrLeft.Offset(i), vrRight.Offset(i)); 122 | } 123 | 124 | i *= Vector.Count; 125 | } 126 | 127 | for (; i < left.Length; i++) 128 | { 129 | rResult.Offset(i) = combiner.Invoke(rLeft.Offset(i), rRight.Offset(i)); 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Divide.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Divide(ReadOnlySpan left, T right, Span result) where T : struct, INumber 7 | { 8 | Concat(left, right, new Divide_VSelector(), new Divide_Selector(), result); 9 | } 10 | 11 | [ArrOverload] 12 | public static void Divide(T left, ReadOnlySpan right, Span result) where T : struct, INumber 13 | { 14 | Concat(left, right, new Divide_VSelector(), new Divide_Selector(), result); 15 | } 16 | 17 | [ArrOverload] 18 | public static void Divide(ReadOnlySpan left, ReadOnlySpan right, Span result) where T : struct, INumber 19 | { 20 | Concat(left, right, new Divide_VSelector(), new Divide_Selector(), result); 21 | } 22 | } 23 | 24 | file struct Divide_VSelector : IFunc, Vector, Vector> where T : struct, INumber 25 | { 26 | public Vector Invoke(Vector left, Vector right) 27 | { 28 | return left / right; 29 | } 30 | } 31 | 32 | file struct Divide_Selector : IFunc where T : struct, INumber 33 | { 34 | public T Invoke(T left, T right) 35 | { 36 | return left / right; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Multiply.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Multiply(ReadOnlySpan left, T right, Span result) where T : struct, INumber 7 | { 8 | Concat(left, right, new Multiply_VSelector(), new Multiply_Selector(), result); 9 | } 10 | 11 | [ArrOverload] 12 | public static void Multiply(ReadOnlySpan left, ReadOnlySpan right, Span result) where T : struct, INumber 13 | { 14 | Concat(left, right, new Multiply_VSelector(), new Multiply_Selector(), result); 15 | } 16 | } 17 | 18 | file struct Multiply_VSelector : IFunc, Vector, Vector> where T : struct, INumber 19 | { 20 | public Vector Invoke(Vector left, Vector right) 21 | { 22 | return left * right; 23 | } 24 | } 25 | 26 | file struct Multiply_Selector : IFunc where T : struct, INumber 27 | { 28 | public T Invoke(T left, T right) 29 | { 30 | return left * right; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Negate.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Negate(ReadOnlySpan span, Span result) where T : struct, INumber 7 | { 8 | Select(span, new Negate_VSelector(), new Negate_Selector(), result); 9 | } 10 | } 11 | 12 | file struct Negate_VSelector : IFunc, Vector> where T : struct, INumber 13 | { 14 | public Vector Invoke(Vector vec) 15 | { 16 | return -vec; 17 | } 18 | } 19 | 20 | file struct Negate_Selector : IFunc where T : struct, INumber 21 | { 22 | public T Invoke(T val) 23 | { 24 | return -val; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Not.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Not(ReadOnlySpan span, Span result) where T : struct, IBinaryNumber 7 | { 8 | Select(span, new Not_VSelector(), new Not_Selector(), result); 9 | } 10 | } 11 | 12 | file struct Not_VSelector : IFunc, Vector> where T : struct, IBinaryNumber 13 | { 14 | public Vector Invoke(Vector vec) 15 | { 16 | return ~vec; 17 | } 18 | } 19 | 20 | file struct Not_Selector : IFunc where T : struct, IBinaryNumber 21 | { 22 | public T Invoke(T val) 23 | { 24 | return ~val; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Or.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Or(ReadOnlySpan left, T right, Span result) where T : struct, IBinaryNumber 7 | { 8 | Concat(left, right, new Or_VSelector(), new Or_Selector(), result); 9 | } 10 | 11 | [ArrOverload] 12 | public static void Or(ReadOnlySpan left, ReadOnlySpan right, Span result) where T : struct, IBinaryNumber 13 | { 14 | Concat(left, right, new Or_VSelector(), new Or_Selector(), result); 15 | } 16 | } 17 | 18 | file struct Or_VSelector : IFunc, Vector, Vector> where T : struct, IBinaryNumber 19 | { 20 | public Vector Invoke(Vector left, Vector right) 21 | { 22 | return left | right; 23 | } 24 | } 25 | 26 | file struct Or_Selector : IFunc where T : struct, IBinaryNumber 27 | { 28 | public T Invoke(T left, T right) 29 | { 30 | return left | right; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Select.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | [DelOverload] 7 | public static void Select(ReadOnlySpan span, F1 vSelector, F2 selector, Span result) 8 | where T : struct, INumber 9 | where TRes : struct, INumber 10 | where F1 : struct, IFunc, Vector> 11 | where F2 : struct, IFunc 12 | { 13 | if (result.Length != span.Length) 14 | { 15 | ThrowArgOutOfRange(nameof(result)); 16 | } 17 | 18 | ref T rSpan = ref GetRef(span); 19 | ref TRes rResult = ref GetRef(result); 20 | 21 | int i = 0; 22 | 23 | if (Vector.IsHardwareAccelerated) 24 | { 25 | ref Vector vrSpan = ref AsVector(rSpan); 26 | ref Vector vrResult = ref AsVector(rResult); 27 | 28 | int length = span.Length / Vector.Count; 29 | 30 | for (; i < length; i++) 31 | { 32 | vrResult.Offset(i) = vSelector.Invoke(vrSpan.Offset(i)); 33 | } 34 | 35 | i *= Vector.Count; 36 | } 37 | 38 | for (; i < span.Length; i++) 39 | { 40 | rResult.Offset(i) = selector.Invoke(rSpan.Offset(i)); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Sqrt.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Sqrt(ReadOnlySpan span, Span result) where T : struct, IFloatingPointIeee754 7 | { 8 | Select(span, new Sqrt_VSelector(), new Sqrt_Selector(), result); 9 | } 10 | } 11 | 12 | file struct Sqrt_VSelector : IFunc, Vector> where T : struct, IFloatingPointIeee754 13 | { 14 | public Vector Invoke(Vector vec) 15 | { 16 | return Vector.SquareRoot(vec); 17 | } 18 | } 19 | 20 | file struct Sqrt_Selector : IFunc where T : struct, IFloatingPointIeee754 21 | { 22 | public T Invoke(T val) 23 | { 24 | return T.Sqrt(val); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Subtract.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Subtract(ReadOnlySpan left, T right, Span result) where T : struct, INumber 7 | { 8 | Concat(left, right, new Subtract_VSelector(), new Subtract_Selector(), result); 9 | } 10 | 11 | [ArrOverload] 12 | public static void Subtract(T left, ReadOnlySpan right, Span result) where T : struct, INumber 13 | { 14 | Concat(left, right, new Subtract_VSelector(), new Subtract_Selector(), result); 15 | } 16 | 17 | [ArrOverload] 18 | public static void Subtract(ReadOnlySpan left, ReadOnlySpan right, Span result) where T : struct, INumber 19 | { 20 | Concat(left, right, new Subtract_VSelector(), new Subtract_Selector(), result); 21 | } 22 | } 23 | 24 | file struct Subtract_VSelector : IFunc, Vector, Vector> where T : struct, INumber 25 | { 26 | public Vector Invoke(Vector left, Vector right) 27 | { 28 | return left - right; 29 | } 30 | } 31 | 32 | file struct Subtract_Selector : IFunc where T : struct, INumber 33 | { 34 | public T Invoke(T left, T right) 35 | { 36 | return left - right; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Ternary.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | [DelOverload] 7 | public static void Ternary(ReadOnlySpan span, F1 vCondition, F2 condition, T trueValue, T falseValue, Span result) 8 | where T : struct, INumber 9 | where F1 : struct, IFunc, Vector> 10 | where F2 : struct, IFunc 11 | { 12 | if (result.Length != span.Length) 13 | { 14 | ThrowArgOutOfRange(nameof(result)); 15 | } 16 | 17 | ref T rSpan = ref GetRef(span); 18 | ref T rResult = ref GetRef(result); 19 | 20 | int i = 0; 21 | 22 | if (Vector.IsHardwareAccelerated) 23 | { 24 | Vector vTrue = new(trueValue); 25 | Vector vFalse = new(falseValue); 26 | 27 | ref Vector vrSpan = ref AsVector(rSpan); 28 | ref Vector vrResult = ref AsVector(rResult); 29 | 30 | int length = span.Length / Vector.Count; 31 | 32 | for (; i < length; i++) 33 | { 34 | vrResult.Offset(i) = Vector.ConditionalSelect(vCondition.Invoke(vrSpan.Offset(i)), vTrue, vFalse); 35 | } 36 | 37 | i *= Vector.Count; 38 | } 39 | 40 | for (; i < span.Length; i++) 41 | { 42 | rResult.Offset(i) = condition.Invoke(rSpan.Offset(i)) ? trueValue : falseValue; 43 | } 44 | } 45 | 46 | [ArrOverload] 47 | [DelOverload] 48 | public static void Ternary(ReadOnlySpan span, F1 vCondition, F2 vTrueSelector, F3 vFalseSelector, F4 condition, F5 trueSelector, F6 falseSelector, Span result) 49 | where T : struct, INumber 50 | where F1 : struct, IFunc, Vector> 51 | where F2 : struct, IFunc, Vector> 52 | where F3 : struct, IFunc, Vector> 53 | where F4 : struct, IFunc 54 | where F5 : struct, IFunc 55 | where F6 : struct, IFunc 56 | { 57 | if (result.Length != span.Length) 58 | { 59 | ThrowArgOutOfRange(nameof(result)); 60 | } 61 | 62 | ref T rSpan = ref GetRef(span); 63 | ref T rResult = ref GetRef(result); 64 | 65 | int i = 0; 66 | 67 | if (Vector.IsHardwareAccelerated) 68 | { 69 | ref Vector vrSpan = ref AsVector(rSpan); 70 | ref Vector vrResult = ref AsVector(rResult); 71 | 72 | int length = span.Length / Vector.Count; 73 | 74 | for (; i < length; i++) 75 | { 76 | vrResult.Offset(i) = Vector.ConditionalSelect(vCondition.Invoke(vrSpan.Offset(i)), vTrueSelector.Invoke(vrSpan.Offset(i)), vFalseSelector.Invoke(vrSpan.Offset(i))); 77 | } 78 | 79 | i *= Vector.Count; 80 | } 81 | 82 | for (; i < span.Length; i++) 83 | { 84 | rResult.Offset(i) = condition.Invoke(rSpan.Offset(i)) ? trueSelector.Invoke(rSpan.Offset(i)) : falseSelector.Invoke(rSpan.Offset(i)); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /SimpleSIMD/Elementwise/Xor.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [ArrOverload] 6 | public static void Xor(ReadOnlySpan left, T right, Span result) where T : struct, IBinaryNumber 7 | { 8 | Concat(left, right, new Xor_VSelector(), new Xor_Selector(), result); 9 | } 10 | 11 | [ArrOverload] 12 | public static void Xor(ReadOnlySpan left, ReadOnlySpan right, Span result) where T : struct, IBinaryNumber 13 | { 14 | Concat(left, right, new Xor_VSelector(), new Xor_Selector(), result); 15 | } 16 | } 17 | 18 | file struct Xor_VSelector : IFunc, Vector, Vector> where T : struct, IBinaryNumber 19 | { 20 | public Vector Invoke(Vector left, Vector right) 21 | { 22 | return left ^ right; 23 | } 24 | } 25 | 26 | file struct Xor_Selector : IFunc where T : struct, IBinaryNumber 27 | { 28 | public T Invoke(T left, T right) 29 | { 30 | return left ^ right; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SimpleSIMD/General/All.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [DelOverload] 6 | public static bool All(ReadOnlySpan span, F1 vPredicate, F2 predicate) 7 | where T : struct, INumber 8 | where F1 : struct, IFunc, bool> 9 | where F2 : struct, IFunc 10 | { 11 | ref T rSpan = ref GetRef(span); 12 | 13 | int i = 0; 14 | 15 | if (Vector.IsHardwareAccelerated) 16 | { 17 | ref Vector vrSpan = ref AsVector(rSpan); 18 | 19 | int length = span.Length / Vector.Count; 20 | 21 | for (; i < length; i++) 22 | { 23 | if (vPredicate.Invoke(vrSpan.Offset(i)) == false) 24 | { 25 | return false; 26 | } 27 | } 28 | 29 | i *= Vector.Count; 30 | } 31 | 32 | for (; i < span.Length; i++) 33 | { 34 | if (predicate.Invoke(rSpan.Offset(i)) == false) 35 | { 36 | return false; 37 | } 38 | } 39 | 40 | return true; 41 | } 42 | 43 | [DelOverload] 44 | public static bool All(ReadOnlySpan left, T right, F1 vPredicate, F2 predicate) 45 | where T : struct, INumber 46 | where F1 : struct, IFunc, Vector, bool> 47 | where F2 : struct, IFunc 48 | { 49 | ref T rLeft = ref GetRef(left); 50 | 51 | int i = 0; 52 | 53 | if (Vector.IsHardwareAccelerated) 54 | { 55 | Vector vRight = new(right); 56 | 57 | ref Vector vrLeft = ref AsVector(rLeft); 58 | 59 | int length = left.Length / Vector.Count; 60 | 61 | for (; i < length; i++) 62 | { 63 | if (vPredicate.Invoke(vrLeft.Offset(i), vRight) == false) 64 | { 65 | return false; 66 | } 67 | } 68 | 69 | i *= Vector.Count; 70 | } 71 | 72 | for (; i < left.Length; i++) 73 | { 74 | if (predicate.Invoke(rLeft.Offset(i), right) == false) 75 | { 76 | return false; 77 | } 78 | } 79 | 80 | return true; 81 | } 82 | 83 | [DelOverload] 84 | public static bool All(ReadOnlySpan left, ReadOnlySpan right, F1 vPredicate, F2 predicate) 85 | where T : struct, INumber 86 | where F1 : struct, IFunc, Vector, bool> 87 | where F2 : struct, IFunc 88 | { 89 | if (right.Length != left.Length) 90 | { 91 | ThrowArgOutOfRange(nameof(right)); 92 | } 93 | 94 | ref T rLeft = ref GetRef(left); 95 | ref T rRight = ref GetRef(right); 96 | 97 | int i = 0; 98 | 99 | if (Vector.IsHardwareAccelerated) 100 | { 101 | ref Vector vrLeft = ref AsVector(rLeft); 102 | ref Vector vrRight = ref AsVector(rRight); 103 | 104 | int length = left.Length / Vector.Count; 105 | 106 | for (; i < length; i++) 107 | { 108 | if (vPredicate.Invoke(vrLeft.Offset(i), vrRight.Offset(i)) == false) 109 | { 110 | return false; 111 | } 112 | } 113 | 114 | i *= Vector.Count; 115 | } 116 | 117 | for (; i < left.Length; i++) 118 | { 119 | if (predicate.Invoke(rLeft.Offset(i), rRight.Offset(i)) == false) 120 | { 121 | return false; 122 | } 123 | } 124 | 125 | return true; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /SimpleSIMD/General/Any.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [DelOverload] 6 | public static bool Any(ReadOnlySpan span, F1 vPredicate, F2 predicate) 7 | where T : struct, INumber 8 | where F1 : struct, IFunc, bool> 9 | where F2 : struct, IFunc 10 | { 11 | ref T rSpan = ref GetRef(span); 12 | 13 | int i = 0; 14 | 15 | if (Vector.IsHardwareAccelerated) 16 | { 17 | ref Vector vrSpan = ref AsVector(rSpan); 18 | 19 | int length = span.Length / Vector.Count; 20 | 21 | for (; i < length; i++) 22 | { 23 | if (vPredicate.Invoke(vrSpan.Offset(i))) 24 | { 25 | return true; 26 | } 27 | } 28 | 29 | i *= Vector.Count; 30 | } 31 | 32 | for (; i < span.Length; i++) 33 | { 34 | if (predicate.Invoke(rSpan.Offset(i))) 35 | { 36 | return true; 37 | } 38 | } 39 | 40 | return false; 41 | } 42 | 43 | [DelOverload] 44 | public static bool Any(ReadOnlySpan left, T right, F1 vPredicate, F2 predicate) 45 | where T : struct, INumber 46 | where F1 : struct, IFunc, Vector, bool> 47 | where F2 : struct, IFunc 48 | { 49 | ref T rLeft = ref GetRef(left); 50 | 51 | int i = 0; 52 | 53 | if (Vector.IsHardwareAccelerated) 54 | { 55 | Vector vRight = new(right); 56 | 57 | ref Vector vrLeft = ref AsVector(rLeft); 58 | 59 | int length = left.Length / Vector.Count; 60 | 61 | for (; i < length; i++) 62 | { 63 | if (vPredicate.Invoke(vrLeft.Offset(i), vRight)) 64 | { 65 | return true; 66 | } 67 | } 68 | 69 | i *= Vector.Count; 70 | } 71 | 72 | for (; i < left.Length; i++) 73 | { 74 | if (predicate.Invoke(rLeft.Offset(i), right)) 75 | { 76 | return true; 77 | } 78 | } 79 | 80 | return false; 81 | } 82 | 83 | [DelOverload] 84 | public static bool Any(ReadOnlySpan left, ReadOnlySpan right, F1 vPredicate, F2 predicate) 85 | where T : struct, INumber 86 | where F1 : struct, IFunc, Vector, bool> 87 | where F2 : struct, IFunc 88 | { 89 | if (right.Length != left.Length) 90 | { 91 | ThrowArgOutOfRange(nameof(right)); 92 | } 93 | 94 | ref T rLeft = ref GetRef(left); 95 | ref T rRight = ref GetRef(right); 96 | 97 | int i = 0; 98 | 99 | if (Vector.IsHardwareAccelerated) 100 | { 101 | ref Vector vrLeft = ref AsVector(rLeft); 102 | ref Vector vrRight = ref AsVector(rRight); 103 | 104 | int length = left.Length / Vector.Count; 105 | 106 | for (; i < length; i++) 107 | { 108 | if (vPredicate.Invoke(vrLeft.Offset(i), vrRight.Offset(i))) 109 | { 110 | return true; 111 | } 112 | } 113 | 114 | i *= Vector.Count; 115 | } 116 | 117 | for (; i < left.Length; i++) 118 | { 119 | if (predicate.Invoke(rLeft.Offset(i), rRight.Offset(i))) 120 | { 121 | return true; 122 | } 123 | } 124 | 125 | return false; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /SimpleSIMD/General/Contains.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static bool Contains(ReadOnlySpan span, T value) where T : struct, INumber 6 | { 7 | ref T rSpan = ref GetRef(span); 8 | 9 | int i = 0; 10 | 11 | if (Vector.IsHardwareAccelerated) 12 | { 13 | Vector vValue = new(value); 14 | 15 | ref Vector vrSpan = ref AsVector(rSpan); 16 | 17 | int length = span.Length / Vector.Count; 18 | 19 | for (; i < length; i++) 20 | { 21 | if (Vector.EqualsAny(vrSpan.Offset(i), vValue)) 22 | { 23 | return true; 24 | } 25 | } 26 | 27 | i *= Vector.Count; 28 | } 29 | 30 | for (; i < span.Length; i++) 31 | { 32 | if (rSpan.Offset(i) == value) 33 | { 34 | return true; 35 | } 36 | } 37 | 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /SimpleSIMD/General/Fill.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static void Fill(Span span, T value) where T : struct, INumber 6 | { 7 | ref T rSpan = ref GetRef(span); 8 | 9 | int i = 0; 10 | 11 | if (Vector.IsHardwareAccelerated) 12 | { 13 | Vector vValue = new(value); 14 | 15 | ref Vector vrSpan = ref AsVector(rSpan); 16 | 17 | int length = span.Length / Vector.Count; 18 | 19 | for (; i < length; i++) 20 | { 21 | vrSpan.Offset(i) = vValue; 22 | } 23 | 24 | i *= Vector.Count; 25 | } 26 | 27 | for (; i < span.Length; i++) 28 | { 29 | rSpan.Offset(i) = value; 30 | } 31 | } 32 | 33 | [DelOverload] 34 | public static void Fill(Span span, F1 vFunc, F2 func) 35 | where T : struct, INumber 36 | where F1 : struct, IFunc> 37 | where F2 : struct, IFunc 38 | { 39 | ref T rSpan = ref GetRef(span); 40 | 41 | int i = 0; 42 | 43 | if (Vector.IsHardwareAccelerated) 44 | { 45 | ref Vector vrSpan = ref AsVector(rSpan); 46 | 47 | int length = span.Length / Vector.Count; 48 | 49 | for (; i < length; i++) 50 | { 51 | vrSpan.Offset(i) = vFunc.Invoke(); 52 | } 53 | 54 | i *= Vector.Count; 55 | } 56 | 57 | for (; i < span.Length; i++) 58 | { 59 | rSpan.Offset(i) = func.Invoke(); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /SimpleSIMD/General/Foreach.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [DelOverload] 6 | public static void Foreach(ReadOnlySpan span, F1 vAction, F2 action) 7 | where T : struct, INumber 8 | where F1 : struct, IAction> 9 | where F2 : struct, IAction 10 | { 11 | ref T rSpan = ref GetRef(span); 12 | 13 | int i = 0; 14 | 15 | if (Vector.IsHardwareAccelerated) 16 | { 17 | ref Vector vrSpan = ref AsVector(rSpan); 18 | 19 | int length = span.Length / Vector.Count; 20 | 21 | for (; i < length; i++) 22 | { 23 | vAction.Invoke(vrSpan.Offset(i)); 24 | } 25 | 26 | i *= Vector.Count; 27 | } 28 | 29 | for (; i < span.Length; i++) 30 | { 31 | action.Invoke(rSpan.Offset(i)); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /SimpleSIMD/General/IndexOf.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static int IndexOf(ReadOnlySpan span, T value) where T : struct, INumber 6 | { 7 | ref T rSpan = ref GetRef(span); 8 | 9 | int i = 0; 10 | 11 | if (Vector.IsHardwareAccelerated) 12 | { 13 | Vector vValue = new(value); 14 | 15 | ref Vector vrSpan = ref AsVector(rSpan); 16 | 17 | int length = span.Length / Vector.Count; 18 | 19 | for (; i < length; i++) 20 | { 21 | if (Vector.EqualsAny(vrSpan.Offset(i), vValue)) 22 | { 23 | int j = i * Vector.Count; 24 | int l = j + Vector.Count; 25 | 26 | for (; j < l; j++) 27 | { 28 | if (rSpan.Offset(j) == value) 29 | { 30 | return j; 31 | } 32 | } 33 | } 34 | } 35 | 36 | i *= Vector.Count; 37 | } 38 | 39 | for (; i < span.Length; i++) 40 | { 41 | if (rSpan.Offset(i) == value) 42 | { 43 | return i; 44 | } 45 | } 46 | 47 | return -1; 48 | } 49 | 50 | [DelOverload] 51 | public static int IndexOf(ReadOnlySpan span, F1 vPredicate, F2 predicate) 52 | where T : struct, INumber 53 | where F1 : struct, IFunc, bool> 54 | where F2 : struct, IFunc 55 | { 56 | ref T rSpan = ref GetRef(span); 57 | 58 | int i = 0; 59 | 60 | if (Vector.IsHardwareAccelerated) 61 | { 62 | ref Vector vrSpan = ref AsVector(rSpan); 63 | 64 | int length = span.Length / Vector.Count; 65 | 66 | for (; i < length; i++) 67 | { 68 | if (vPredicate.Invoke(vrSpan.Offset(i))) 69 | { 70 | int j = i * Vector.Count; 71 | int l = j + Vector.Count; 72 | 73 | for (; j < l; j++) 74 | { 75 | if (predicate.Invoke(rSpan.Offset(j))) 76 | { 77 | return j; 78 | } 79 | } 80 | } 81 | } 82 | 83 | i *= Vector.Count; 84 | } 85 | 86 | for (; i < span.Length; i++) 87 | { 88 | if (predicate.Invoke(rSpan.Offset(i))) 89 | { 90 | return i; 91 | } 92 | } 93 | 94 | return -1; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /SimpleSIMD/Reduction/Aggregate.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | [DelOverload] 6 | public static T Aggregate(ReadOnlySpan span, T seed, F1 vAccumulator, F2 accumulator) 7 | where T : struct, INumber 8 | where F1 : struct, IFunc, Vector, Vector> 9 | where F2 : struct, IFunc 10 | { 11 | T res = seed; 12 | 13 | ref T rSpan = ref GetRef(span); 14 | 15 | int i = 0; 16 | 17 | if (Vector.IsHardwareAccelerated) 18 | { 19 | Vector vRes = new(seed); 20 | 21 | ref Vector vrSpan = ref AsVector(rSpan); 22 | 23 | int length = span.Length / Vector.Count; 24 | 25 | for (; i < length; i++) 26 | { 27 | vRes = vAccumulator.Invoke(vRes, vrSpan.Offset(i)); 28 | } 29 | 30 | for (int j = 0; j < Vector.Count; j++) 31 | { 32 | res = accumulator.Invoke(res, vRes[j]); 33 | } 34 | 35 | i *= Vector.Count; 36 | } 37 | 38 | for (; i < span.Length; i++) 39 | { 40 | res = accumulator.Invoke(res, rSpan.Offset(i)); 41 | } 42 | 43 | return res; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /SimpleSIMD/Reduction/Average.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static T Average(ReadOnlySpan span) where T : struct, INumber 6 | { 7 | return Average(span, new ID_VSelector(), new ID_Selector()); 8 | } 9 | 10 | [DelOverload] 11 | public static T Average(ReadOnlySpan span, F1 vSelector, F2 selector) 12 | where T : struct, INumber 13 | where F1 : struct, IFunc, Vector> 14 | where F2 : struct, IFunc 15 | { 16 | return Sum(span, vSelector, selector) / Convert(span.Length); 17 | } 18 | 19 | private static TDst Convert(TSrc val) where TSrc : INumber where TDst : INumber 20 | { 21 | return TDst.CreateSaturating(val); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /SimpleSIMD/Reduction/Dot.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static T Dot(ReadOnlySpan left, T right) where T : struct, INumber 6 | { 7 | T dot = T.Zero; 8 | 9 | ref T rLeft = ref GetRef(left); 10 | 11 | int i = 0; 12 | 13 | if (Vector.IsHardwareAccelerated) 14 | { 15 | Vector vDot = Vector.Zero; 16 | Vector vRight = new(right); 17 | 18 | ref Vector vrLeft = ref AsVector(rLeft); 19 | 20 | int length = left.Length / Vector.Count; 21 | 22 | for (; i < length; i++) 23 | { 24 | vDot += vrLeft.Offset(i) * vRight; 25 | } 26 | 27 | dot = Vector.Dot(vDot, Vector.One); 28 | 29 | i *= Vector.Count; 30 | } 31 | 32 | for (; i < left.Length; i++) 33 | { 34 | dot += rLeft.Offset(i) * right; 35 | } 36 | 37 | return dot; 38 | } 39 | 40 | public static T Dot(ReadOnlySpan left, ReadOnlySpan right) where T : struct, INumber 41 | { 42 | if (right.Length != left.Length) 43 | { 44 | ThrowArgOutOfRange(nameof(right)); 45 | } 46 | 47 | T dot = T.Zero; 48 | 49 | ref T rLeft = ref GetRef(left); 50 | ref T rRight = ref GetRef(right); 51 | 52 | int i = 0; 53 | 54 | if (Vector.IsHardwareAccelerated) 55 | { 56 | Vector vDot = Vector.Zero; 57 | 58 | ref Vector vrLeft = ref AsVector(rLeft); 59 | ref Vector vrRight = ref AsVector(rRight); 60 | 61 | int length = left.Length / Vector.Count; 62 | 63 | for (; i < length; i++) 64 | { 65 | vDot += vrLeft.Offset(i) * vrRight.Offset(i); 66 | } 67 | 68 | dot = Vector.Dot(vDot, Vector.One); 69 | 70 | i *= Vector.Count; 71 | } 72 | 73 | for (; i < left.Length; i++) 74 | { 75 | dot += rLeft.Offset(i) * rRight.Offset(i); 76 | } 77 | 78 | return dot; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /SimpleSIMD/Reduction/Identity.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | internal struct ID_VSelector : IFunc, Vector> where T : struct, INumber 4 | { 5 | public Vector Invoke(Vector vec) 6 | { 7 | return vec; 8 | } 9 | } 10 | 11 | internal struct ID_Selector : IFunc where T : struct, INumber 12 | { 13 | public T Invoke(T val) 14 | { 15 | return val; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /SimpleSIMD/Reduction/Max.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static T Max(ReadOnlySpan span) where T : struct, INumber, IMinMaxValue 6 | { 7 | return Max(span, new ID_VSelector(), new ID_Selector()); 8 | } 9 | 10 | [DelOverload] 11 | public static T Max(ReadOnlySpan span, F1 vSelector, F2 selector) 12 | where T : struct, INumber, IMinMaxValue 13 | where F1 : struct, IFunc, Vector> 14 | where F2 : struct, IFunc 15 | { 16 | T max = T.MinValue; 17 | 18 | ref T rSpan = ref GetRef(span); 19 | 20 | int i = 0; 21 | 22 | if (Vector.IsHardwareAccelerated) 23 | { 24 | Vector vMax = new(max); 25 | 26 | ref Vector vrSpan = ref AsVector(rSpan); 27 | 28 | int length = span.Length / Vector.Count; 29 | 30 | for (; i < length; i++) 31 | { 32 | vMax = Vector.Max(vMax, vSelector.Invoke(vrSpan.Offset(i))); 33 | } 34 | 35 | for (int j = 0; j < Vector.Count; j++) 36 | { 37 | max = T.Max(max, vMax[j]); 38 | } 39 | 40 | i *= Vector.Count; 41 | } 42 | 43 | for (; i < span.Length; i++) 44 | { 45 | max = T.Max(max, selector.Invoke(rSpan.Offset(i))); 46 | } 47 | 48 | return max; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /SimpleSIMD/Reduction/Min.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static T Min(ReadOnlySpan span) where T : struct, INumber, IMinMaxValue 6 | { 7 | return Min(span, new ID_VSelector(), new ID_Selector()); 8 | } 9 | 10 | [DelOverload] 11 | public static T Min(ReadOnlySpan span, F1 vSelector, F2 selector) 12 | where T : struct, INumber, IMinMaxValue 13 | where F1 : struct, IFunc, Vector> 14 | where F2 : struct, IFunc 15 | { 16 | T min = T.MaxValue; 17 | 18 | ref T rSpan = ref GetRef(span); 19 | 20 | int i = 0; 21 | 22 | if (Vector.IsHardwareAccelerated) 23 | { 24 | Vector vMin = new(min); 25 | 26 | ref Vector vrSpan = ref AsVector(rSpan); 27 | 28 | int length = span.Length / Vector.Count; 29 | 30 | for (; i < length; i++) 31 | { 32 | vMin = Vector.Min(vMin, vSelector.Invoke(vrSpan.Offset(i))); 33 | } 34 | 35 | for (int j = 0; j < Vector.Count; j++) 36 | { 37 | min = T.Min(min, vMin[j]); 38 | } 39 | 40 | i *= Vector.Count; 41 | } 42 | 43 | for (; i < span.Length; i++) 44 | { 45 | min = T.Min(min, selector.Invoke(rSpan.Offset(i))); 46 | } 47 | 48 | return min; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /SimpleSIMD/Reduction/Sum.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleSimd; 2 | 3 | public static partial class SimdOps 4 | { 5 | public static T Sum(ReadOnlySpan span) where T : struct, INumber 6 | { 7 | return Sum(span, new ID_VSelector(), new ID_Selector()); 8 | } 9 | 10 | [DelOverload] 11 | public static T Sum(ReadOnlySpan span, F1 vSelector, F2 selector) 12 | where T : struct, INumber 13 | where F1 : struct, IFunc, Vector> 14 | where F2 : struct, IFunc 15 | { 16 | T sum = T.Zero; 17 | 18 | ref T rSpan = ref GetRef(span); 19 | 20 | int i = 0; 21 | 22 | if (Vector.IsHardwareAccelerated) 23 | { 24 | Vector vSum = Vector.Zero; 25 | 26 | ref Vector vrSpan = ref AsVector(rSpan); 27 | 28 | int length = span.Length / Vector.Count; 29 | 30 | for (; i < length; i++) 31 | { 32 | vSum += vSelector.Invoke(vrSpan.Offset(i)); 33 | } 34 | 35 | sum = Vector.Dot(vSum, Vector.One); 36 | 37 | i *= Vector.Count; 38 | } 39 | 40 | for (; i < span.Length; i++) 41 | { 42 | sum += selector.Invoke(rSpan.Offset(i)); 43 | } 44 | 45 | return sum; 46 | } 47 | } -------------------------------------------------------------------------------- /SimpleSIMD/SimdOps.cs: -------------------------------------------------------------------------------- 1 | global using System; 2 | global using System.Numerics; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | 6 | namespace SimpleSimd; 7 | 8 | public static partial class SimdOps 9 | { 10 | [MethodImpl(MethodImplOptions.NoInlining)] 11 | private static void ThrowArgOutOfRange(string name) 12 | { 13 | throw new ArgumentOutOfRangeException(name); 14 | } 15 | 16 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 17 | private static ref Vector AsVector(in T value) where T : struct, INumber 18 | { 19 | return ref Unsafe.As>(ref Unsafe.AsRef(value)); 20 | } 21 | 22 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 23 | private static ref T GetRef(ReadOnlySpan span) where T : struct, INumber 24 | { 25 | return ref MemoryMarshal.GetReference(span); 26 | } 27 | 28 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 29 | private static ref T GetRef(Span span) where T : struct, INumber 30 | { 31 | return ref MemoryMarshal.GetReference(span); 32 | } 33 | 34 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 35 | private static ref T Offset(this ref T source, int count) where T : struct 36 | { 37 | return ref Unsafe.Add(ref source, count); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /SimpleSIMD/SimpleSIMD.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net7.0 5 | SimpleSimd 6 | enable 7 | true 8 | Gilad Freidkin 9 | https://github.com/giladfrid009/SimpleSIMD 10 | SIMD, Library, Performance, Parallel, Math, Generic, Allocation, Array, Span 11 | MIT 12 | Easy to use SIMD accelerated span and array methods 13 | Now each method in SimdOps is generic, instead of SimdOps<T>, 14 | i.e SimdOps.Abs<T>() instead of SimdOps<T>.Abs(). 15 | Copyright 2020-2021 (c) Gilad Freidkin, All rights reserved 16 | 4.6.0 17 | 1. Now using the latest .NET7 18 | 2. Added AndNot vectorized method 19 | 3. Select and Concat doesn't throw anymore whenever Vector<Tres>.Count != Vector<T>.Count 20 | 4. Internal structural changes 21 | true 22 | README.md 23 | en 24 | True 25 | latest-recommended 26 | SimpleSIMD 27 | 28 | 29 | 30 | 31 | True 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Tests/Comparison.cs: -------------------------------------------------------------------------------- 1 | namespace Tests; 2 | 3 | public class Comparison_Tests 4 | { 5 | private static readonly Random rnd = new(0); 6 | private static readonly int len = 100; 7 | private static readonly int lowerBound = -100; 8 | private static readonly int middleBound = 100; 9 | private static readonly int upperBound = 300; 10 | private readonly int[] smallerArr; 11 | private readonly int[] largerArr; 12 | private readonly int[] constArr; 13 | 14 | public Comparison_Tests() 15 | { 16 | smallerArr = Enumerable.Range(0, len).Select(X => rnd.Next(lowerBound, middleBound)).ToArray(); 17 | largerArr = Enumerable.Range(0, len).Select(X => rnd.Next(middleBound, upperBound)).ToArray(); 18 | constArr = Enumerable.Repeat(middleBound, len).ToArray(); 19 | } 20 | 21 | [Fact] 22 | public void Equal_Test() 23 | { 24 | Assert.True(SimdOps.Equal(smallerArr, smallerArr)); 25 | Assert.True(SimdOps.Equal(constArr, middleBound)); 26 | 27 | Assert.False(SimdOps.Equal(smallerArr, largerArr)); 28 | Assert.False(SimdOps.Equal(constArr, upperBound)); 29 | } 30 | 31 | [Fact] 32 | public void Greater_Test() 33 | { 34 | Assert.True(SimdOps.Greater(largerArr, smallerArr)); 35 | Assert.True(SimdOps.Greater(largerArr, lowerBound)); 36 | 37 | Assert.False(SimdOps.Greater(smallerArr, smallerArr)); 38 | Assert.False(SimdOps.Greater(constArr, middleBound)); 39 | 40 | Assert.False(SimdOps.Greater(smallerArr, largerArr)); 41 | Assert.False(SimdOps.Greater(constArr, upperBound)); 42 | } 43 | 44 | [Fact] 45 | public void GreaterOrEqual_Test() 46 | { 47 | Assert.True(SimdOps.GreaterOrEqual(largerArr, smallerArr)); 48 | Assert.True(SimdOps.GreaterOrEqual(largerArr, lowerBound)); 49 | 50 | Assert.True(SimdOps.GreaterOrEqual(smallerArr, smallerArr)); 51 | Assert.True(SimdOps.GreaterOrEqual(constArr, middleBound)); 52 | 53 | Assert.False(SimdOps.GreaterOrEqual(smallerArr, largerArr)); 54 | Assert.False(SimdOps.GreaterOrEqual(constArr, upperBound)); 55 | } 56 | 57 | [Fact] 58 | public void Less_Test() 59 | { 60 | Assert.False(SimdOps.Less(largerArr, smallerArr)); 61 | Assert.False(SimdOps.Less(largerArr, lowerBound)); 62 | 63 | Assert.False(SimdOps.Less(smallerArr, smallerArr)); 64 | Assert.False(SimdOps.Less(constArr, middleBound)); 65 | 66 | Assert.True(SimdOps.Less(smallerArr, largerArr)); 67 | Assert.True(SimdOps.Less(constArr, upperBound)); 68 | } 69 | 70 | [Fact] 71 | public void LessOrEqual_Test() 72 | { 73 | Assert.False(SimdOps.LessOrEqual(largerArr, smallerArr)); 74 | Assert.False(SimdOps.LessOrEqual(largerArr, lowerBound)); 75 | 76 | Assert.True(SimdOps.LessOrEqual(smallerArr, smallerArr)); 77 | Assert.True(SimdOps.LessOrEqual(constArr, middleBound)); 78 | 79 | Assert.True(SimdOps.LessOrEqual(smallerArr, largerArr)); 80 | Assert.True(SimdOps.LessOrEqual(constArr, upperBound)); 81 | } 82 | } -------------------------------------------------------------------------------- /Tests/Elementwise.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | 3 | namespace Tests; 4 | public class Elementwise_Tests 5 | { 6 | private static readonly Random rnd = new(0); 7 | private static readonly int len = 100; 8 | private static readonly int lowerBound = -100; 9 | private static readonly int middleBound = 0; 10 | private static readonly int upperBound = 100; 11 | private readonly int[] negArr; 12 | private readonly int[] posArr; 13 | 14 | public Elementwise_Tests() 15 | { 16 | negArr = Enumerable.Range(0, len).Select(X => rnd.Next(lowerBound, middleBound - 1)).ToArray(); 17 | posArr = Enumerable.Range(0, len).Select(X => rnd.Next(middleBound + 1, upperBound)).ToArray(); 18 | } 19 | 20 | [Fact] 21 | public void Abs_Test() 22 | { 23 | Assert.True(Enumerable.SequenceEqual(negArr.Select(Math.Abs), SimdOps.Abs(negArr))); 24 | 25 | int[] res = new int[len]; 26 | 27 | SimdOps.Abs(negArr, res); 28 | Assert.True(Enumerable.SequenceEqual(negArr.Select(Math.Abs), res)); 29 | } 30 | 31 | [Fact] 32 | public void Negate_Test() 33 | { 34 | Assert.True(Enumerable.SequenceEqual(negArr.Select(X => -X), SimdOps.Negate(negArr))); 35 | 36 | int[] res = new int[len]; 37 | 38 | SimdOps.Negate(negArr, res); 39 | Assert.True(Enumerable.SequenceEqual(negArr.Select(X => -X), res)); 40 | } 41 | 42 | [Fact] 43 | public void Sqrt_Test() 44 | { 45 | double[] posArrDouble = posArr.Select(X => (double)X).ToArray(); 46 | 47 | Assert.True(Enumerable.SequenceEqual(posArrDouble.Select(Math.Sqrt), SimdOps.Sqrt(posArrDouble))); 48 | 49 | double[] res = new double[len]; 50 | 51 | SimdOps.Sqrt(posArrDouble, res); 52 | Assert.True(Enumerable.SequenceEqual(posArrDouble.Select(Math.Sqrt), res)); 53 | } 54 | 55 | [Fact] 56 | public void Add_Test() 57 | { 58 | Assert.True(Enumerable.SequenceEqual(SimdOps.Add(negArr, posArr), negArr.Zip(posArr, (N, P) => N + P))); 59 | 60 | Assert.True(Enumerable.SequenceEqual(SimdOps.Add(posArr, upperBound), posArr.Select(X => X + upperBound))); 61 | 62 | int[] res = new int[len]; 63 | 64 | SimdOps.Add(negArr, posArr, res); 65 | Assert.True(Enumerable.SequenceEqual(res, negArr.Zip(posArr, (N, P) => N + P))); 66 | 67 | SimdOps.Add(posArr, upperBound, res); 68 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X + upperBound))); 69 | } 70 | 71 | [Fact] 72 | public void Subtract_Test() 73 | { 74 | Assert.True(Enumerable.SequenceEqual(SimdOps.Subtract(negArr, posArr), negArr.Zip(posArr, (N, P) => N - P))); 75 | 76 | Assert.True(Enumerable.SequenceEqual(SimdOps.Subtract(posArr, upperBound), posArr.Select(X => X - upperBound))); 77 | 78 | int[] res = new int[len]; 79 | 80 | SimdOps.Subtract(negArr, posArr, res); 81 | Assert.True(Enumerable.SequenceEqual(res, negArr.Zip(posArr, (N, P) => N - P))); 82 | 83 | SimdOps.Subtract(posArr, upperBound, res); 84 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X - upperBound))); 85 | } 86 | 87 | [Fact] 88 | public void Multiply_Test() 89 | { 90 | Assert.True(Enumerable.SequenceEqual(SimdOps.Multiply(negArr, posArr), negArr.Zip(posArr, (N, P) => N * P))); 91 | 92 | Assert.True(Enumerable.SequenceEqual(SimdOps.Multiply(posArr, upperBound), posArr.Select(X => X * upperBound))); 93 | 94 | int[] res = new int[len]; 95 | 96 | SimdOps.Multiply(negArr, posArr, res); 97 | Assert.True(Enumerable.SequenceEqual(res, negArr.Zip(posArr, (N, P) => N * P))); 98 | 99 | SimdOps.Multiply(posArr, upperBound, res); 100 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X * upperBound))); 101 | } 102 | 103 | [Fact] 104 | public void Divide_Test() 105 | { 106 | Assert.True(Enumerable.SequenceEqual(SimdOps.Divide(negArr, posArr), negArr.Zip(posArr, (N, P) => N / P))); 107 | 108 | Assert.True(Enumerable.SequenceEqual(SimdOps.Divide(posArr, upperBound), posArr.Select(X => X / upperBound))); 109 | 110 | int[] res = new int[len]; 111 | 112 | SimdOps.Divide(negArr, posArr, res); 113 | Assert.True(Enumerable.SequenceEqual(res, negArr.Zip(posArr, (N, P) => N / P))); 114 | 115 | SimdOps.Divide(posArr, upperBound, res); 116 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X / upperBound))); 117 | } 118 | 119 | [Fact] 120 | public void Not_Test() 121 | { 122 | Assert.True(Enumerable.SequenceEqual(SimdOps.Not(negArr), negArr.Select(X => ~X))); 123 | 124 | int[] res = new int[len]; 125 | 126 | SimdOps.Not(negArr, res); 127 | Assert.True(Enumerable.SequenceEqual(res, negArr.Select(X => ~X))); 128 | } 129 | 130 | [Fact] 131 | public void And_Test() 132 | { 133 | Assert.True(Enumerable.SequenceEqual(SimdOps.And(negArr, posArr), negArr.Zip(posArr, (N, P) => N & P))); 134 | 135 | Assert.True(Enumerable.SequenceEqual(SimdOps.And(posArr, upperBound), posArr.Select(X => X & upperBound))); 136 | 137 | int[] res = new int[len]; 138 | 139 | SimdOps.And(negArr, posArr, res); 140 | Assert.True(Enumerable.SequenceEqual(res, negArr.Zip(posArr, (N, P) => N & P))); 141 | 142 | SimdOps.And(posArr, upperBound, res); 143 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X & upperBound))); 144 | } 145 | 146 | [Fact] 147 | public void Or_Test() 148 | { 149 | Assert.True(Enumerable.SequenceEqual(SimdOps.Or(negArr, posArr), negArr.Zip(posArr, (N, P) => N | P))); 150 | 151 | Assert.True(Enumerable.SequenceEqual(SimdOps.Or(posArr, upperBound), posArr.Select(X => X | upperBound))); 152 | 153 | int[] res = new int[len]; 154 | 155 | SimdOps.Or(negArr, posArr, res); 156 | Assert.True(Enumerable.SequenceEqual(res, negArr.Zip(posArr, (N, P) => N | P))); 157 | 158 | SimdOps.Or(posArr, upperBound, res); 159 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X | upperBound))); 160 | } 161 | 162 | [Fact] 163 | public void Xor_Test() 164 | { 165 | Assert.True(Enumerable.SequenceEqual(SimdOps.Xor(negArr, posArr), negArr.Zip(posArr, (N, P) => N ^ P))); 166 | 167 | Assert.True(Enumerable.SequenceEqual(SimdOps.Xor(posArr, upperBound), posArr.Select(X => X ^ upperBound))); 168 | 169 | int[] res = new int[len]; 170 | 171 | SimdOps.Xor(negArr, posArr, res); 172 | Assert.True(Enumerable.SequenceEqual(res, negArr.Zip(posArr, (N, P) => N ^ P))); 173 | 174 | SimdOps.Xor(posArr, upperBound, res); 175 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X ^ upperBound))); 176 | } 177 | 178 | [Fact] 179 | public void Concat_Test() 180 | { 181 | int[] res = new int[len]; 182 | 183 | SimdOps.Concat(posArr, negArr, (L, R) => L + 3 * R, (L, R) => L + 3 * R, res); 184 | Assert.True(Enumerable.SequenceEqual(res, posArr.Zip(negArr, (L, R) => L + 3 * R))); 185 | 186 | SimdOps.Concat(posArr, upperBound, (L, R) => L + 3 * R, (L, R) => L + 3 * R, res); 187 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X + 3 * upperBound))); 188 | 189 | SimdOps.Concat(upperBound, posArr, (L, R) => L + 3 * R, (L, R) => L + 3 * R, res); 190 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => upperBound + 3 * X))); 191 | } 192 | 193 | [Fact] 194 | public void Select_Test() 195 | { 196 | int[] res = new int[len]; 197 | 198 | Vector mask = new(1010); 199 | 200 | SimdOps.Select(posArr, X => -X * 2 + X | mask, X => -X * 2 + X | 1010, res); 201 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => -X * 2 + X | 1010))); 202 | } 203 | 204 | [Fact] 205 | public void Ternary_Test() 206 | { 207 | int[] res = new int[len]; 208 | 209 | Vector mask = new(1010); 210 | 211 | SimdOps.Ternary(posArr, X => Vector.GreaterThan(X, new Vector(50)), X => X > 50, 1, 0, res); 212 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X > 50 ? 1 : 0))); 213 | 214 | SimdOps.Ternary(posArr, X => Vector.GreaterThan(X, new Vector(50)), X => 2 * X, X => 3 * X, X => X > 50, X => 2 * X, X => 3 * X, res); 215 | Assert.True(Enumerable.SequenceEqual(res, posArr.Select(X => X > 50 ? 2 * X : 3 * X))); 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /Tests/Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | false 8 | 9 | 10 | 11 | 12 | 13 | 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | all 16 | 17 | 18 | runtime; build; native; contentfiles; analyzers; buildtransitive 19 | all 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Tests/Usings.cs: -------------------------------------------------------------------------------- 1 | global using SimpleSimd; 2 | global using Xunit; 3 | --------------------------------------------------------------------------------