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

Commit d3bb5cd

Browse files
WinCPPdotnet-bot
authored andcommitted
CoreFX #24343 Vector Ctor using Span (dotnet/coreclr#16733)
* CoreFX #24343 Vector using Span dotnet/corefx#24343 * CoreFX #24343 Vector using Span dotnet/corefx#24343 * CoreFX #24343 Vector using Span dotnet/corefx#24343 Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent 850e2f3 commit d3bb5cd

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

src/Common/src/CoreLib/System/Numerics/GenerationConfig.ttinclude

+27-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,29 @@
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+
bool firstTime = true;
158+
foreach (var type in allTypes)
159+
{
160+
if (firstTime)
161+
{
162+
sbuilder.Append("if (").Append(MakeTypeComparisonCondition(type));
163+
firstTime = false;
164+
}
165+
else
166+
{
167+
sbuilder.AppendLine().Append(" || ").Append(MakeTypeComparisonCondition(type));
168+
}
169+
}
170+
sbuilder.Append(")");
171+
return sbuilder.ToString();
172+
}
173+
#>

src/Common/src/CoreLib/System/Numerics/Vector.cs

+36-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
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 netcoreapp
6+
using Internal.Runtime.CompilerServices;
7+
#endif
58
using System.Globalization;
69
using System.Numerics.Hashing;
710
using System.Runtime.CompilerServices;
11+
using System.Runtime.InteropServices;
812
using System.Text;
913

1014
namespace System.Numerics
@@ -386,7 +390,7 @@ public unsafe Vector(T[] values, int index)
386390
}
387391
if (index < 0 || (values.Length - index) < Count)
388392
{
389-
throw new IndexOutOfRangeException();
393+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
390394
}
391395

392396
if (Vector.IsHardwareAccelerated)
@@ -763,6 +767,37 @@ private Vector(ref Register existingRegister)
763767
{
764768
this.register = existingRegister;
765769
}
770+
771+
#if netcoreapp
772+
/// <summary>
773+
/// Constructs a vector from the given span. The span must contain at least Vector'T.Count elements.
774+
/// </summary>
775+
public Vector(Span<T> values)
776+
: this()
777+
{
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(SR.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+
}
799+
}
800+
#endif
766801
#endregion Constructors
767802

768803
#region Public Instance Methods

src/Common/src/CoreLib/System/Numerics/Vector.tt

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

10+
#if netcoreapp
11+
using Internal.Runtime.CompilerServices;
12+
#endif
1013
using System.Globalization;
1114
using System.Numerics.Hashing;
1215
using System.Runtime.CompilerServices;
16+
using System.Runtime.InteropServices;
1317
using System.Text;
1418

1519
namespace System.Numerics
@@ -198,7 +202,7 @@ namespace System.Numerics
198202
}
199203
if (index < 0 || (values.Length - index) < Count)
200204
{
201-
throw new IndexOutOfRangeException();
205+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
202206
}
203207

204208
if (Vector.IsHardwareAccelerated)
@@ -283,6 +287,28 @@ namespace System.Numerics
283287
{
284288
this.register = existingRegister;
285289
}
290+
291+
#if netcoreapp
292+
/// <summary>
293+
/// Constructs a vector from the given span. The span must contain at least Vector'T.Count elements.
294+
/// </summary>
295+
public Vector(Span<T> values)
296+
: this()
297+
{
298+
<#=GenerateIfConditionAllTypes(supportedTypes)#>
299+
{
300+
if (values.Length < Count)
301+
{
302+
throw new IndexOutOfRangeException(SR.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+
}
310+
}
311+
#endif
286312
#endregion Constructors
287313

288314
#region Public Instance Methods

0 commit comments

Comments
 (0)