-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
base: main
Are you sure you want to change the base?
Conversation
Tagging subscribers to this area: @dotnet/area-system-memory |
fe6f0ed
to
1eb387c
Compare
1eb387c
to
7ec2876
Compare
7ec2876
to
93e46f0
Compare
? (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))); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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>();
There was a problem hiding this comment.
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.
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)); |
There was a problem hiding this comment.
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:
vector = new Vector<byte>(Unsafe.BitCast<T, byte>(value)); | |
vector = Vector.Create<T>(value).As<T, byte>(); |
There was a problem hiding this comment.
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
This reverts commit 93e46f0.
Unsafe.BitCast
to avoid taking addressUnsafe.BitCast
in SpanHelprs.Fill
No description provided.