-
Notifications
You must be signed in to change notification settings - Fork 2.7k
CoreFX #24343 Vector Ctor using Span #16733
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,13 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#if netcoreapp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't we use the existing netstandard constant here (for instance just like https://github.com/dotnet/coreclr/blob/master/src/mscorlib/shared/System/MemoryExtensions.cs#L9). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discussed that here: #16733 (comment) |
||
using Internal.Runtime.CompilerServices; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be under ifdef. Otherwise, it won't build once mirrored to CoreFX used to build the portable Vector version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mirroring as in, is there going to be an attempt later sometime to single-instance the copies of shared code base between CoreClr and CoreFx repos? In that case do you want me to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This single instancing is in place already. Once this PR is merged, there will be PR with your change created in corefx automatically: https://github.com/dotnet/coreclr/blob/master/src/mscorlib/shared/README.md We use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In CoreFx, we include this file when building for Also - the whole new constructor should probably be under the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eerhardt I again went through the csproj for CoreFx project and in there Vector.cs is included if not targetting I am not able to understand how this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Both the You can think of
I think it should actually be under When building this code in dotnet/corefx for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thanks @eerhardt for the explanation. Actually what confused is me that if As for the PR, I am replacing it iwth There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That will not compile on CoreRT and ProjectN. It would need to be more combined with more conditions there. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jkotas Thank you. I have pushed a commit that defines There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: put this after all the System.* using directives There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we sort them alphabetically... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but for ones other than System (like internal), we group them separately (with system first). At least that is what I have seen (and followed) elsewhere. So, something like: using System.X;
using System.Y;
using System.Z;
using Internal.A;
using Internal.B;
using Internal.C; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Between, this PR is already merged and closed. So perhaps we can do all this in the optimization issue along with other clean up? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course. That would be great. |
||
#endif | ||
using System.Globalization; | ||
using System.Numerics.Hashing; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
namespace System.Numerics | ||
|
@@ -386,7 +390,7 @@ public unsafe Vector(T[] values, int index) | |
} | ||
if (index < 0 || (values.Length - index) < Count) | ||
{ | ||
throw new IndexOutOfRangeException(); | ||
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reintroduced this as per comment here from @4creators in one of the previous commits... |
||
} | ||
|
||
if (Vector.IsHardwareAccelerated) | ||
|
@@ -763,6 +767,37 @@ private Vector(ref Register existingRegister) | |
{ | ||
this.register = existingRegister; | ||
} | ||
|
||
#if netcoreapp | ||
/// <summary> | ||
/// Constructs a vector from the given span. The span must contain at least Vector'T.Count elements. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: use proper xml formatting (see
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. But I kept it same like other instances in the file for consistency. E.g. here i.e. below code, /// Constructs a vector from the given array. The size of the given array must be at least Vector'T.Count.
/// </summary>
[Intrinsic]
public unsafe Vector(T[] values) : this(values, 0) { } Do we want to fix this as part of the optimization issue #16745? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be addressed separately and we can make the changes to the whole file at the same time. Good point about consistency with other places in the file. |
||
/// </summary> | ||
public Vector(Span<T> values) | ||
: this() | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs bounds check to verify that there is enough elements in the Span, similar to constructors that take arrays. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Add test for it to your CoreFX PR too.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Working on this. I plan to model the new exception on lines of the ask in issue dotnet/corefx#25344. Whatever is done here will have to be replicated in fix for dotnet/corefx#25344. (Would like to work on that after this.) |
||
if ((typeof(T) == typeof(Byte)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about T == char? Also, nit, can you use, byte/sbyte,short/long/etc, instead (to be consistent with the default formatting VS suggests)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vector doesn't support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah I see. Tyvm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type names There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fair. It doesn't follow our coding guidelines, but like you mentioned, this should be fixed separately. Thanks! |
||
|| (typeof(T) == typeof(SByte)) | ||
|| (typeof(T) == typeof(UInt16)) | ||
|| (typeof(T) == typeof(Int16)) | ||
|| (typeof(T) == typeof(UInt32)) | ||
|| (typeof(T) == typeof(Int32)) | ||
|| (typeof(T) == typeof(UInt64)) | ||
|| (typeof(T) == typeof(Int64)) | ||
|| (typeof(T) == typeof(Single)) | ||
|| (typeof(T) == typeof(Double))) | ||
{ | ||
if (values.Length < Count) | ||
{ | ||
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may help users if this exception told them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had thought about this and was ok for this instance. But there is another instance for I was trying to have consistency in the exception being thrown across the three usages and with few other exceptions in the resources file e.g.
And anyways, the user would know the number of elements that (s)he is sending... |
||
} | ||
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values))); | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException(SR.Arg_TypeNotSupported); | ||
} | ||
} | ||
#endif | ||
#endregion Constructors | ||
|
||
#region Public Instance Methods | ||
|
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 can defined unconditionally here. The condition is not necessary.
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.
Done!