Skip to content

Commit

Permalink
Merge pull request dotnet/coreclr#21795 from fiigii/fixGeneric
Browse files Browse the repository at this point in the history
Remove unnecessary ThrowIfUnsupportedType calls and definitions

Commit migrated from dotnet/coreclr@834f8d9
  • Loading branch information
CarolEidt authored Jan 12, 2019
2 parents ae63b99 + 68a0331 commit cc4f5c0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static int Count
{
get
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();
return Vector128.Size / Unsafe.SizeOf<T>();
}
}
Expand All @@ -53,7 +53,7 @@ public static Vector128<T> Zero
[Intrinsic]
get
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();
return default;
}
}
Expand Down Expand Up @@ -91,15 +91,6 @@ internal static bool IsSupported
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void ThrowIfUnsupportedType()
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}
}

/// <summary>Reinterprets the current instance as a new <see cref="Vector128{U}" />.</summary>
/// <typeparam name="U">The type of the vector the current instance should be reinterpreted as.</typeparam>
/// <returns>The current instance reinterpreted as a new <see cref="Vector128{U}" />.</returns>
Expand All @@ -108,8 +99,8 @@ internal static void ThrowIfUnsupportedType()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector128<U> As<U>() where U : struct
{
ThrowIfUnsupportedType();
Vector128<U>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();
ThrowHelper.ThrowForUnsupportedVectorBaseType<U>();
return Unsafe.As<Vector128<T>, Vector128<U>>(ref Unsafe.AsRef(in this));
}

Expand Down Expand Up @@ -184,7 +175,7 @@ public Vector128<U> As<U>() where U : struct
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Vector128<T> other)
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

if (Sse.IsSupported && (typeof(T) == typeof(float)))
{
Expand Down Expand Up @@ -243,11 +234,11 @@ public override bool Equals(object obj)
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index" /> was less than zero or greater than the number of elements.</exception>
public T GetElement(int index)
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

if ((uint)(index) >= (uint)(Count))
{
throw new ArgumentOutOfRangeException(nameof(index));
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
}

ref T e0 = ref Unsafe.As<Vector128<T>, T>(ref Unsafe.AsRef(in this));
Expand All @@ -262,11 +253,11 @@ public T GetElement(int index)
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index" /> was less than zero or greater than the number of elements.</exception>
public Vector128<T> WithElement(int index, T value)
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

if ((uint)(index) >= (uint)(Count))
{
throw new ArgumentOutOfRangeException(nameof(index));
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
}

Vector128<T> result = this;
Expand All @@ -280,7 +271,7 @@ public Vector128<T> WithElement(int index, T value)
/// <exception cref="NotSupportedException">The type of the current instance (<typeparamref name="T" />) is not supported.</exception>
public override int GetHashCode()
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

int hashCode = 0;

Expand All @@ -297,8 +288,8 @@ public override int GetHashCode()
/// <exception cref="NotSupportedException">The type of the current instance (<typeparamref name="T" />) is not supported.</exception>
public Vector64<T> GetLower()
{
ThrowIfUnsupportedType();
Vector64<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

return Unsafe.As<Vector128<T>, Vector64<T>>(ref Unsafe.AsRef(in this));
}

Expand All @@ -308,8 +299,7 @@ public Vector64<T> GetLower()
/// <exception cref="NotSupportedException">The type of the current instance (<typeparamref name="T" />) is not supported.</exception>
public Vector128<T> WithLower(Vector64<T> value)
{
ThrowIfUnsupportedType();
Vector64<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

Vector128<T> result = this;
Unsafe.As<Vector128<T>, Vector64<T>>(ref result) = value;
Expand All @@ -321,8 +311,7 @@ public Vector128<T> WithLower(Vector64<T> value)
/// <exception cref="NotSupportedException">The type of the current instance (<typeparamref name="T" />) is not supported.</exception>
public Vector64<T> GetUpper()
{
ThrowIfUnsupportedType();
Vector64<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

ref Vector64<T> lower = ref Unsafe.As<Vector128<T>, Vector64<T>>(ref Unsafe.AsRef(in this));
return Unsafe.Add(ref lower, 1);
Expand All @@ -334,8 +323,7 @@ public Vector64<T> GetUpper()
/// <exception cref="NotSupportedException">The type of the current instance (<typeparamref name="T" />) is not supported.</exception>
public Vector128<T> WithUpper(Vector64<T> value)
{
ThrowIfUnsupportedType();
Vector64<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

Vector128<T> result = this;
ref Vector64<T> lower = ref Unsafe.As<Vector128<T>, Vector64<T>>(ref result);
Expand All @@ -349,7 +337,7 @@ public Vector128<T> WithUpper(Vector64<T> value)
[Intrinsic]
public T ToScalar()
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();
return Unsafe.As<Vector128<T>, T>(ref Unsafe.AsRef(in this));
}

Expand Down Expand Up @@ -377,7 +365,7 @@ public string ToString(string format)
/// <exception cref="NotSupportedException">The type of the current instance (<typeparamref name="T" />) is not supported.</exception>
public string ToString(string format, IFormatProvider formatProvider)
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
int lastElement = Count - 1;
Expand All @@ -403,8 +391,7 @@ public string ToString(string format, IFormatProvider formatProvider)
[Intrinsic]
public Vector256<T> ToVector256()
{
ThrowIfUnsupportedType();
Vector256<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

Vector256<T> result = Vector256<T>.Zero;
Unsafe.As<Vector256<T>, Vector128<T>>(ref result) = this;
Expand All @@ -417,8 +404,7 @@ public Vector256<T> ToVector256()
[Intrinsic]
public unsafe Vector256<T> ToVector256Unsafe()
{
ThrowIfUnsupportedType();
Vector256<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

// This relies on us stripping the "init" flag from the ".locals"
// declaration to let the upper bits be uninitialized.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static int Count
{
get
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();
return Vector256.Size / Unsafe.SizeOf<T>();
}
}
Expand All @@ -55,7 +55,7 @@ public static Vector256<T> Zero
[Intrinsic]
get
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();
return default;
}
}
Expand Down Expand Up @@ -93,15 +93,6 @@ internal static bool IsSupported
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void ThrowIfUnsupportedType()
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}
}

/// <summary>Reinterprets the current instance as a new <see cref="Vector256{U}" />.</summary>
/// <typeparam name="U">The type of the vector the current instance should be reinterpreted as.</typeparam>
/// <returns>The current instance reinterpreted as a new <see cref="Vector256{U}" />.</returns>
Expand All @@ -110,8 +101,8 @@ internal static void ThrowIfUnsupportedType()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector256<U> As<U>() where U : struct
{
ThrowIfUnsupportedType();
Vector256<U>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();
ThrowHelper.ThrowForUnsupportedVectorBaseType<U>();
return Unsafe.As<Vector256<T>, Vector256<U>>(ref Unsafe.AsRef(in this));
}

Expand Down Expand Up @@ -183,6 +174,7 @@ public Vector256<U> As<U>() where U : struct
/// <param name="other">The <see cref="Vector256{T}" /> to compare with the current instance.</param>
/// <returns><c>true</c> if <paramref name="other" /> is equal to the current instance; otherwise, <c>false</c>.</returns>
/// <exception cref="NotSupportedException">The type of the current instance (<typeparamref name="T" />) is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Vector256<T> other)
{
if (Avx.IsSupported)
Expand Down Expand Up @@ -243,11 +235,11 @@ public override bool Equals(object obj)
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index" /> was less than zero or greater than the number of elements.</exception>
public T GetElement(int index)
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

if ((uint)(index) >= (uint)(Count))
{
throw new ArgumentOutOfRangeException(nameof(index));
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
}

ref T e0 = ref Unsafe.As<Vector256<T>, T>(ref Unsafe.AsRef(in this));
Expand All @@ -262,11 +254,11 @@ public T GetElement(int index)
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index" /> was less than zero or greater than the number of elements.</exception>
public Vector256<T> WithElement(int index, T value)
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

if ((uint)(index) >= (uint)(Count))
{
throw new ArgumentOutOfRangeException(nameof(index));
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
}

Vector256<T> result = this;
Expand All @@ -280,7 +272,7 @@ public Vector256<T> WithElement(int index, T value)
/// <exception cref="NotSupportedException">The type of the current instance (<typeparamref name="T" />) is not supported.</exception>
public override int GetHashCode()
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

int hashCode = 0;

Expand All @@ -298,8 +290,7 @@ public override int GetHashCode()
[Intrinsic]
public Vector128<T> GetLower()
{
ThrowIfUnsupportedType();
Vector128<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();
return Unsafe.As<Vector256<T>, Vector128<T>>(ref Unsafe.AsRef(in this));
}

Expand All @@ -310,8 +301,7 @@ public Vector128<T> GetLower()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector256<T> WithLower(Vector128<T> value)
{
ThrowIfUnsupportedType();
Vector128<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

if (Avx2.IsSupported && ((typeof(T) != typeof(float)) && (typeof(T) != typeof(double))))
{
Expand Down Expand Up @@ -342,8 +332,7 @@ Vector256<T> SoftwareFallback(in Vector256<T> t, Vector128<T> x)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector128<T> GetUpper()
{
ThrowIfUnsupportedType();
Vector128<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

if (Avx2.IsSupported && ((typeof(T) != typeof(float)) && (typeof(T) != typeof(double))))
{
Expand Down Expand Up @@ -374,8 +363,7 @@ Vector128<T> SoftwareFallback(in Vector256<T> t)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector256<T> WithUpper(Vector128<T> value)
{
ThrowIfUnsupportedType();
Vector128<T>.ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

if (Avx2.IsSupported && ((typeof(T) != typeof(float)) && (typeof(T) != typeof(double))))
{
Expand Down Expand Up @@ -407,7 +395,7 @@ Vector256<T> SoftwareFallback(in Vector256<T> t, Vector128<T> x)
[Intrinsic]
public T ToScalar()
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();
return Unsafe.As<Vector256<T>, T>(ref Unsafe.AsRef(in this));
}

Expand Down Expand Up @@ -435,7 +423,7 @@ public string ToString(string format)
/// <exception cref="NotSupportedException">The type of the current instance (<typeparamref name="T" />) is not supported.</exception>
public string ToString(string format, IFormatProvider formatProvider)
{
ThrowIfUnsupportedType();
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
int lastElement = Count - 1;
Expand Down
Loading

0 comments on commit cc4f5c0

Please sign in to comment.