|
| 1 | +--- |
| 2 | +title: "CA1516: Use cross-platform intrinsics" |
| 3 | +description: "Learn about code analyzer rule CA1516 - Use cross-platform intrinsics" |
| 4 | +ms.date: 07/09/2025 |
| 5 | +ms.topic: reference |
| 6 | +f1_keywords: |
| 7 | + - CA1516 |
| 8 | + - UseCrossPlatformIntrinsicsAnalyzer |
| 9 | +helpviewer_keywords: |
| 10 | + - CA1516 |
| 11 | +author: TannerGooding |
| 12 | +dev_langs: |
| 13 | + - CSharp |
| 14 | +--- |
| 15 | + |
| 16 | +# CA1516: Use cross-platform intrinsics |
| 17 | + |
| 18 | +| Property | Value | |
| 19 | +|-------------------------------------|------------------------------------------------| |
| 20 | +| **Rule ID** | CA1516 | |
| 21 | +| **Title** | Use cross-platform intrinsics | |
| 22 | +| **Category** | [Maintainability](maintainability-warnings.md) | |
| 23 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 24 | +| **Enabled by default in .NET 9** | No | |
| 25 | + |
| 26 | +## Cause |
| 27 | + |
| 28 | +A platform or architecture specific intrinsic is used when a cross-platform equivalent exists. |
| 29 | + |
| 30 | +## Rule description |
| 31 | + |
| 32 | +This rule detects usage of platform-specific intrinsics that can be replaced with an equivalent cross-platform intrinsic instead. |
| 33 | + |
| 34 | +## How to fix violations |
| 35 | + |
| 36 | +Apply the fixer that switches the code to use the equivalent cross-platform intrinsic. |
| 37 | + |
| 38 | +## Example |
| 39 | + |
| 40 | +The following code snippet shows three similar violations of CA1516: |
| 41 | + |
| 42 | +```csharp |
| 43 | +using System; |
| 44 | +using System.Runtime.Intrinsics; |
| 45 | +using System.Runtime.Intrinsics.Arm; |
| 46 | +using System.Runtime.Intrinsics.Wasm; |
| 47 | +using System.Runtime.Intrinsics.X86; |
| 48 | + |
| 49 | +class C |
| 50 | +{ |
| 51 | + Vector128<byte> M1(Vector128<byte> x, Vector128<byte> y) => AdvSimd.Add(x, y); |
| 52 | + Vector128<byte> M2(Vector128<byte> x, Vector128<byte> y) => Sse2.Add(x, y); |
| 53 | + Vector128<byte> M3(Vector128<byte> x, Vector128<byte> y) => PackedSimd.Add(x, y); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +The following code snippet fixes the violation and would be applied by the fixer: |
| 58 | + |
| 59 | +```csharp |
| 60 | +using System; |
| 61 | +using System.Runtime.Intrinsics; |
| 62 | + |
| 63 | +class C |
| 64 | +{ |
| 65 | + Vector128<byte> M1(Vector128<byte> x, Vector128<byte> y) => x + y; |
| 66 | + Vector128<byte> M2(Vector128<byte> x, Vector128<byte> y) => x + y; |
| 67 | + Vector128<byte> M3(Vector128<byte> x, Vector128<byte> y) => x + y; |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +Once the fix has been applied, it becomes more obvious that the three methods could be simplified to be a single method that works on all platforms. |
| 72 | + |
| 73 | +## When to suppress warnings |
| 74 | + |
| 75 | +It's safe to suppress a violation of this rule if you're not concerned about the maintainability of your code. |
| 76 | + |
| 77 | +## Suppress a warning |
| 78 | + |
| 79 | +If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 80 | + |
| 81 | +```csharp |
| 82 | +#pragma warning disable CA1516 |
| 83 | +// The code that's violating the rule is on this line. |
| 84 | +#pragma warning restore CA1516 |
| 85 | +``` |
| 86 | + |
| 87 | +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). |
| 88 | + |
| 89 | +```ini |
| 90 | +[*.{cs,vb}] |
| 91 | +dotnet_diagnostic.CA1516.severity = none |
| 92 | +``` |
| 93 | + |
| 94 | +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
| 95 | + |
| 96 | +## Configure code to analyze |
| 97 | + |
| 98 | +You can configure which _output assembly kinds_ to apply this rule to. For example, to only apply this rule to code that produces a console application or a dynamically linked library (that is, not a UI app), add the following key-value pair to an *.editorconfig* file in your project: |
| 99 | + |
| 100 | +```ini |
| 101 | +dotnet_code_quality.CA1516.output_kind = ConsoleApplication, DynamicallyLinkedLibrary |
| 102 | +``` |
| 103 | + |
| 104 | +For more information, see [output_kind](../code-quality-rule-options.md#output_kind). |
0 commit comments