Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 357e213

Browse files
committed
Fix review comments
1 parent 3f420d3 commit 357e213

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

src/mscorlib/Resources/Strings.resx

+3
Original file line numberDiff line numberDiff line change
@@ -3718,4 +3718,7 @@
37183718
<data name="Argument_OverlapAlignmentMismatch" xml:space="preserve">
37193719
<value>Overlapping spans have mismatching alignment.</value>
37203720
</data>
3721+
<data name="Arg_InsufficientNumberOfElements" xml:space="preserve">
3722+
<value>At least {0} element(s) are expected in the parameter "{1}".</value>
3723+
</data>
37213724
</root>

src/mscorlib/shared/System/Numerics/GenerationConfig.ttinclude

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<#@ import namespace="System.Linq" #>
22
<#@ import namespace="System.Collections.Generic" #>
3+
<#@ import namespace="System.Text" #>
34
<#+
45
/* This file includes static data used as compilation configuration for the rest of the code generation.
56
It is shared here to ensure that all generated code compiles with the same constants and configurations. */
@@ -144,4 +145,28 @@
144145
string keyword = (type == allTypes.ToArray()[0]) ? "if" : "else if";
145146
return string.Format("{0} (typeof(T) == typeof({1}))", keyword, type.Name);
146147
}
147-
#>
148+
149+
public string MakeTypeComparisonCondition(Type type)
150+
{
151+
return string.Format("(typeof(T) == typeof({0}))", type.Name);
152+
}
153+
154+
public string GenerateIfConditionAllTypes(IEnumerable<Type> allTypes)
155+
{
156+
StringBuilder sbuilder = new StringBuilder();
157+
var arrAllTypes = allTypes.ToArray();
158+
for (int index = 0; index < arrAllTypes.Length; ++index)
159+
{
160+
if (index == 0)
161+
{
162+
sbuilder.Append("if (").Append(MakeTypeComparisonCondition(arrAllTypes[index]));
163+
}
164+
else
165+
{
166+
sbuilder.AppendLine().Append(" || ").Append(MakeTypeComparisoncondition(arrAllTypes[index]));
167+
}
168+
}
169+
sbuilder.Append(")");
170+
return sbuilder.ToString();
171+
}
172+
#>

src/mscorlib/shared/System/Numerics/Vector.cs

+25-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#if !netstandard
56
using Internal.Runtime.CompilerServices;
7+
#endif
68
using System.Globalization;
79
using System.Numerics.Hashing;
810
using System.Runtime.CompilerServices;
@@ -388,7 +390,7 @@ public unsafe Vector(T[] values, int index)
388390
}
389391
if (index < 0 || (values.Length - index) < Count)
390392
{
391-
throw new IndexOutOfRangeException();
393+
throw new IndexOutOfRangeException(String.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
392394
}
393395

394396
if (Vector.IsHardwareAccelerated)
@@ -767,14 +769,33 @@ private Vector(ref Register existingRegister)
767769
}
768770

769771
/// <summary>
770-
/// Constructs a vector from the given span.
771-
/// The span must contain at least Vector'T.Count elements.
772+
/// Constructs a vector from the given span. The span must contain at least Vector'T.Count elements.
772773
/// </summary>
773774
[Intrinsic]
774775
public unsafe Vector(Span<T> values)
775776
: this()
776777
{
777-
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)));
778+
if ((typeof(T) == typeof(Byte))
779+
|| (typeof(T) == typeof(SByte))
780+
|| (typeof(T) == typeof(UInt16))
781+
|| (typeof(T) == typeof(Int16))
782+
|| (typeof(T) == typeof(UInt32))
783+
|| (typeof(T) == typeof(Int32))
784+
|| (typeof(T) == typeof(UInt64))
785+
|| (typeof(T) == typeof(Int64))
786+
|| (typeof(T) == typeof(Single))
787+
|| (typeof(T) == typeof(Double)))
788+
{
789+
if (values.Length < Count)
790+
{
791+
throw new IndexOutOfRangeException(String.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
792+
}
793+
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)));
794+
}
795+
else
796+
{
797+
throw new NotSupportedException(SR.Arg_TypeNotSupported);
798+
}
778799
}
779800
#endregion Constructors
780801

src/mscorlib/shared/System/Numerics/Vector.tt

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
<#@ import namespace="System.Runtime.InteropServices" #>
88
<#@ include file="GenerationConfig.ttinclude" #><# GenerateCopyrightHeader(); #>
99

10+
#if !netstandard
1011
using Internal.Runtime.CompilerServices;
12+
#endif
1113
using System.Globalization;
1214
using System.Numerics.Hashing;
1315
using System.Runtime.CompilerServices;
@@ -200,7 +202,7 @@ namespace System.Numerics
200202
}
201203
if (index < 0 || (values.Length - index) < Count)
202204
{
203-
throw new IndexOutOfRangeException();
205+
throw new IndexOutOfRangeException(String.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
204206
}
205207

206208
if (Vector.IsHardwareAccelerated)
@@ -287,14 +289,24 @@ namespace System.Numerics
287289
}
288290

289291
/// <summary>
290-
/// Constructs a vector from the given span.
291-
/// The span must contain at least Vector'T.Count elements.
292+
/// Constructs a vector from the given span. The span must contain at least Vector'T.Count elements.
292293
/// </summary>
293294
[Intrinsic]
294295
public unsafe Vector(Span<T> values)
295296
: this()
296297
{
297-
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)));
298+
<#=GenerateIfConditionAllTypes(supportedTypes)#>
299+
{
300+
if (values.Length < Count)
301+
{
302+
throw new IndexOutOfRangeException(String.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
303+
}
304+
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)));
305+
}
306+
else
307+
{
308+
throw new NotSupportedException(SR.Arg_TypeNotSupported);
309+
}
298310
}
299311
#endregion Constructors
300312

0 commit comments

Comments
 (0)