Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Unsafe.BitCast in SpanHelprs.Fill #111091

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Jan 4, 2025

No description provided.

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

@xtqqczze xtqqczze force-pushed the use-unsafe-bitcast branch from 7ec2876 to 93e46f0 Compare January 4, 2025 23:12
? (Vector<byte>)(new Vector<double>((double)(object)tmp!))
: (Vector<byte>)(new Vector<ulong>(Unsafe.As<T, ulong>(ref tmp)));
? (Vector<byte>)(new Vector<double>(Unsafe.BitCast<T, double>(value)))
: (Vector<byte>)(new Vector<ulong>(Unsafe.BitCast<T, ulong>(value)));
Copy link
Member

@MihaZupan MihaZupan Jan 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method reachable for e.g. int??
The conditions above look like they'd let it use the vectorized path, but BitCast doesn't work with nullable.

Hopefully that should be caught by tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole thing could really be simplified to just return Vector.Create<T>(value).As<T, byte>();

Copy link
Contributor Author

@xtqqczze xtqqczze Jan 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these throw a NotSupportedException at runtime for Nullable<int>, we have to keep using Unsafe.As, unfortunately.

sharplab

Vector<byte> vector;

if (sizeof(T) == 1)
{
vector = new Vector<byte>(Unsafe.As<T, byte>(ref tmp));
vector = new Vector<byte>(Unsafe.BitCast<T, byte>(value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and a similar suggestion for most other paths:

Suggested change
vector = new Vector<byte>(Unsafe.BitCast<T, byte>(value));
vector = Vector.Create<T>(value).As<T, byte>();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The paths could likely just all be collapsed to a single if (...) block as well if using the above pattern, notably

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 5, 2025

@MihuBot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Memory community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants