Skip to content

Instantly share code, notes, and snippets.

@sbomer
Last active April 24, 2023 22:21
Show Gist options
  • Save sbomer/4bfb41750259d60810aeb0b92bcc0279 to your computer and use it in GitHub Desktop.
Save sbomer/4bfb41750259d60810aeb0b92bcc0279 to your computer and use it in GitHub Desktop.
Using platform analyzer for arbitrary features
using System.Diagnostics;
class Program {
public static void Main() {
if (Avx2.IsSupported) {
Avx2.Use(); // No warning. :)
} else {
// fallback logic...
}
}
public static void Bad() {
Avx2.Use(); // Warns!
}
public static void BadAssert() {
Debug.Assert(Avx2.IsSupported);
Avx2.Use(); // No warning... we need to be careful with debug asserts. :(
}
}
[RequiresFeature("Avx2.0")]
public class Avx2 {
[FeatureCheck("Avx2.0")]
public static bool IsSupported => true;
public static void Use() {}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Runtime.Versioning.SupportedOSPlatformAttribute" Alias="RequiresFeatureAttribute" />
<Using Include="System.Runtime.Versioning.SupportedOSPlatformGuardAttribute" Alias="FeatureCheckAttribute" />
<SupportedPlatform Include="Avx" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment