Last active
April 24, 2023 22:21
-
-
Save sbomer/4bfb41750259d60810aeb0b92bcc0279 to your computer and use it in GitHub Desktop.
Using platform analyzer for arbitrary features
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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