dotnet / dotnet/runtime

[API Proposal]: VectorXXX types, ISpanFormattable, and other interfaces (debugging QoL mostly)

Open
#121,100 4 comments 1 reaction 0 assignees View on GitHub
api-suggestion area-System.Runtime.Intrinsics
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Background and motivation

There was an issue back in the 3.0 days about IFormattable and VectorXXX types, and ultimately we did get an IFormattable implementation for `Vector`, which is helpful for sure and is a lot nicer than having to step through the debugger on large datasets.

However, it looks like maybe the work to implement that got abandoned at some point and not carried through to the other VectorXXX types - only Vector.

It sure is handy, especially, for debugging purposes to be able to dump a bunch of hex representations of your vectors using the same syntax as any other numeric scalar type. But `Vector(64|128|256|512)` don't implement IFormattable or any others.

Interestingly, the `ISimdVector` type (which is internal) that they all implement [has this to say about it](https://github.com/dotnet/runtime/blob/3fe8518d51bbcaa179bbe275b2597fbe1b88bc5a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/ISimdVector_2.cs#L28-L31).

So it looks like the ISpanFormattable and IUtf8SpanFormattable interfaces (and a few others) have been commented out since that type was added 2 years ago, [here](https://github.com/dotnet/runtime/commit/15ffcff82fe8a3eea529ae5d67a9fc52be990d9f#diff-6b5f5c0f35cf00da934a70db6f4e65907761bc975d342637cd332ae5ce955120R28).

Any chance an implementation of the commented-out interfaces, at least on the VectorXXX types themselves (especially for formatting), might sneak their way into .net10? 🥺

Also, that ISimdVector seems like an interface that might be reasonable to promote to public, perhaps? Could be handy for type parameter constraints in user code. 🤷‍♂️

### API Proposal

Detailed up top, but:
```csharp

public readonly unsafe struct Vector128 : ISimdVector, T>, IUtf8SpanFormattable // etc
```

### API Usage

```csharp
Vector256 longVector = Vector256.Create ( 0x5555555555555555L );
Console.WriteLine ( $"{longVector:X16}" ); // <---- Currently just prints the decimal value. Want format string to be respected.
```

### Alternative Designs

None, really. IFormattable is an old interface.

But I suppose an option is to uncomment the other interfaces on ISimdVector to declare it for all implementing types. That might be riskier though, for other runtimes, perhaps?

### Risks

None that I can immediately think of, unless the ISimdVector interface were made public before doing this. But I'm not sure, personally, what sorts of breakage of user code might be possible due to alterations to an interface that is currently non-public. 🤔

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.