From 1912fe1b53a887c080fcb08960d2b4be4aa334e4 Mon Sep 17 00:00:00 2001 From: WinCPP Date: Thu, 1 Mar 2018 21:44:41 +0530 Subject: [PATCH 1/3] CoreFX #24343 Vector using Span dotnet/corefx#24343 --- src/mscorlib/Resources/Strings.resx | 5 ++- .../Numerics/GenerationConfig.ttinclude | 28 ++++++++++++++- src/mscorlib/shared/System/Numerics/Vector.cs | 35 ++++++++++++++++++- src/mscorlib/shared/System/Numerics/Vector.tt | 26 +++++++++++++- 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/mscorlib/Resources/Strings.resx b/src/mscorlib/Resources/Strings.resx index 2fa5d880b03d..c8572175e81e 100644 --- a/src/mscorlib/Resources/Strings.resx +++ b/src/mscorlib/Resources/Strings.resx @@ -3715,4 +3715,7 @@ Overlapping spans have mismatching alignment. - + + At least {0} element(s) are expected in the parameter "{1}". + + \ No newline at end of file diff --git a/src/mscorlib/shared/System/Numerics/GenerationConfig.ttinclude b/src/mscorlib/shared/System/Numerics/GenerationConfig.ttinclude index cdd9c95213ee..a21188e51bab 100644 --- a/src/mscorlib/shared/System/Numerics/GenerationConfig.ttinclude +++ b/src/mscorlib/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/mscorlib/shared/System/Numerics/Vector.cs b/src/mscorlib/shared/System/Numerics/Vector.cs index 5fd286732ee6..e7f1d0892610 100644 --- a/src/mscorlib/shared/System/Numerics/Vector.cs +++ b/src/mscorlib/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 (!netfx && !netstandard) +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,35 @@ private Vector(ref Register existingRegister) { this.register = existingRegister; } + + /// + /// 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); + } + } #endregion Constructors #region Public Instance Methods diff --git a/src/mscorlib/shared/System/Numerics/Vector.tt b/src/mscorlib/shared/System/Numerics/Vector.tt index 275f47350d2d..996037fad9ef 100644 --- a/src/mscorlib/shared/System/Numerics/Vector.tt +++ b/src/mscorlib/shared/System/Numerics/Vector.tt @@ -7,9 +7,13 @@ <#@ import namespace="System.Runtime.InteropServices" #> <#@ include file="GenerationConfig.ttinclude" #><# GenerateCopyrightHeader(); #> +#if (!netfx && !netstandard) +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,26 @@ namespace System.Numerics { this.register = existingRegister; } + + /// + /// 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); + } + } #endregion Constructors #region Public Instance Methods From 52e95add0cc1eccc2120034d940f6cab77be4d56 Mon Sep 17 00:00:00 2001 From: WinCPP Date: Thu, 1 Mar 2018 21:44:41 +0530 Subject: [PATCH 2/3] CoreFX #24343 Vector using Span dotnet/corefx#24343 --- src/mscorlib/System.Private.CoreLib.csproj | 1 + src/mscorlib/shared/System/Numerics/Vector.cs | 4 +++- src/mscorlib/shared/System/Numerics/Vector.tt | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj index 1da5ccba252c..8c2d418788a5 100644 --- a/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/mscorlib/System.Private.CoreLib.csproj @@ -34,6 +34,7 @@ true true $(DefineConstants);CORECLR;_USE_NLS_PLUS_TABLE;RESOURCE_SATELLITE_CONFIG;CODE_ANALYSIS_BASELINE + $(DefineConstants);netcoreapp <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory)/Documentation diff --git a/src/mscorlib/shared/System/Numerics/Vector.cs b/src/mscorlib/shared/System/Numerics/Vector.cs index e7f1d0892610..42f86d929770 100644 --- a/src/mscorlib/shared/System/Numerics/Vector.cs +++ b/src/mscorlib/shared/System/Numerics/Vector.cs @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#if (!netfx && !netstandard) +#if netcoreapp using Internal.Runtime.CompilerServices; #endif using System.Globalization; @@ -768,6 +768,7 @@ 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. /// @@ -796,6 +797,7 @@ public Vector(Span values) throw new NotSupportedException(SR.Arg_TypeNotSupported); } } +#endif #endregion Constructors #region Public Instance Methods diff --git a/src/mscorlib/shared/System/Numerics/Vector.tt b/src/mscorlib/shared/System/Numerics/Vector.tt index 996037fad9ef..d7622466b7ec 100644 --- a/src/mscorlib/shared/System/Numerics/Vector.tt +++ b/src/mscorlib/shared/System/Numerics/Vector.tt @@ -7,7 +7,7 @@ <#@ import namespace="System.Runtime.InteropServices" #> <#@ include file="GenerationConfig.ttinclude" #><# GenerateCopyrightHeader(); #> -#if (!netfx && !netstandard) +#if netcoreapp using Internal.Runtime.CompilerServices; #endif using System.Globalization; @@ -288,6 +288,7 @@ 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. /// @@ -307,6 +308,7 @@ namespace System.Numerics throw new NotSupportedException(SR.Arg_TypeNotSupported); } } +#endif #endregion Constructors #region Public Instance Methods From 71d5215a2c007f7fe9c944bec47f0ed0fc70fefd Mon Sep 17 00:00:00 2001 From: WinCPP Date: Thu, 1 Mar 2018 21:44:41 +0530 Subject: [PATCH 3/3] CoreFX #24343 Vector using Span dotnet/corefx#24343 --- src/mscorlib/System.Private.CoreLib.csproj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj index 8c2d418788a5..56bd520751f7 100644 --- a/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/mscorlib/System.Private.CoreLib.csproj @@ -33,8 +33,7 @@ false true true - $(DefineConstants);CORECLR;_USE_NLS_PLUS_TABLE;RESOURCE_SATELLITE_CONFIG;CODE_ANALYSIS_BASELINE - $(DefineConstants);netcoreapp + $(DefineConstants);CORECLR;_USE_NLS_PLUS_TABLE;RESOURCE_SATELLITE_CONFIG;CODE_ANALYSIS_BASELINE;netcoreapp <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory)/Documentation