From a2a032a904a1346aacf55993fdabc5c42578cd79 Mon Sep 17 00:00:00 2001 From: Mandar Sahasrabuddhe Date: Wed, 7 Mar 2018 06:12:46 +0530 Subject: [PATCH] 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 --- .../Numerics/GenerationConfig.ttinclude | 28 +++++++++++++- .../shared/System/Numerics/Vector.cs | 37 ++++++++++++++++++- .../shared/System/Numerics/Vector.tt | 28 +++++++++++++- 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Numerics/GenerationConfig.ttinclude b/src/System.Private.CoreLib/shared/System/Numerics/GenerationConfig.ttinclude index cdd9c95213e..a21188e51ba 100644 --- a/src/System.Private.CoreLib/shared/System/Numerics/GenerationConfig.ttinclude +++ b/src/System.Private.CoreLib/shared/System/Numerics/GenerationConfig.ttinclude @@ -1,5 +1,6 @@ <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections.Generic" #> +<#@ import namespace="System.Text" #> <#+ /* This file includes static data used as compilation configuration for the rest of the code generation. It is shared here to ensure that all generated code compiles with the same constants and configurations. */ @@ -144,4 +145,29 @@ string keyword = (type == allTypes.ToArray()[0]) ? "if" : "else if"; return string.Format("{0} (typeof(T) == typeof({1}))", keyword, type.Name); } -#> \ No newline at end of file + + public string MakeTypeComparisonCondition(Type type) + { + return string.Format("(typeof(T) == typeof({0}))", type.Name); + } + + public string GenerateIfConditionAllTypes(IEnumerable allTypes) + { + StringBuilder sbuilder = new StringBuilder(); + bool firstTime = true; + foreach (var type in allTypes) + { + if (firstTime) + { + sbuilder.Append("if (").Append(MakeTypeComparisonCondition(type)); + firstTime = false; + } + else + { + sbuilder.AppendLine().Append(" || ").Append(MakeTypeComparisonCondition(type)); + } + } + sbuilder.Append(")"); + return sbuilder.ToString(); + } +#> diff --git a/src/System.Private.CoreLib/shared/System/Numerics/Vector.cs b/src/System.Private.CoreLib/shared/System/Numerics/Vector.cs index 5fd286732ee..42f86d92977 100644 --- a/src/System.Private.CoreLib/shared/System/Numerics/Vector.cs +++ b/src/System.Private.CoreLib/shared/System/Numerics/Vector.cs @@ -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 +using Internal.Runtime.CompilerServices; +#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.Count, nameof(values))); } if (Vector.IsHardwareAccelerated) @@ -763,6 +767,37 @@ private Vector(ref Register existingRegister) { this.register = existingRegister; } + +#if netcoreapp + /// + /// Constructs a vector from the given span. The span must contain at least Vector'T.Count elements. + /// + public Vector(Span values) + : this() + { + if ((typeof(T) == typeof(Byte)) + || (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.Count, nameof(values))); + } + this = Unsafe.ReadUnaligned>(ref Unsafe.As(ref MemoryMarshal.GetReference(values))); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } +#endif #endregion Constructors #region Public Instance Methods diff --git a/src/System.Private.CoreLib/shared/System/Numerics/Vector.tt b/src/System.Private.CoreLib/shared/System/Numerics/Vector.tt index 275f47350d2..d7622466b7e 100644 --- a/src/System.Private.CoreLib/shared/System/Numerics/Vector.tt +++ b/src/System.Private.CoreLib/shared/System/Numerics/Vector.tt @@ -7,9 +7,13 @@ <#@ import namespace="System.Runtime.InteropServices" #> <#@ include file="GenerationConfig.ttinclude" #><# GenerateCopyrightHeader(); #> +#if netcoreapp +using Internal.Runtime.CompilerServices; +#endif using System.Globalization; using System.Numerics.Hashing; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Text; namespace System.Numerics @@ -198,7 +202,7 @@ namespace System.Numerics } if (index < 0 || (values.Length - index) < Count) { - throw new IndexOutOfRangeException(); + throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector.Count, nameof(values))); } if (Vector.IsHardwareAccelerated) @@ -283,6 +287,28 @@ namespace System.Numerics { this.register = existingRegister; } + +#if netcoreapp + /// + /// Constructs a vector from the given span. The span must contain at least Vector'T.Count elements. + /// + public Vector(Span values) + : this() + { + <#=GenerateIfConditionAllTypes(supportedTypes)#> + { + if (values.Length < Count) + { + throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector.Count, nameof(values))); + } + this = Unsafe.ReadUnaligned>(ref Unsafe.As(ref MemoryMarshal.GetReference(values))); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } +#endif #endregion Constructors #region Public Instance Methods