Skip to content

Commit

Permalink
Don't use Unsafe.BitCast on Mono
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Jun 3, 2024
1 parent 6c6d71e commit 4dae1fc
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ public static Vector<TTo> As<TFrom, TTo>(this Vector<TFrom> vector)
ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<TFrom>();
ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<TTo>();

#if MONO
return Unsafe.As<Vector<TFrom>, Vector<TTo>>(ref vector);
#else
return Unsafe.BitCast<Vector<TFrom>, Vector<TTo>>(vector);
#endif
}

/// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see cref="Vector{Byte}" />.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@ public static unsafe partial class Vector
/// <summary>Reinterprets a <see cref="Vector4" /> as a new <see cref="Plane" />.</summary>
/// <param name="value">The vector to reinterpret.</param>
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Plane" />.</returns>
[Intrinsic]
internal static Plane AsPlane(this Vector4 value) => Unsafe.BitCast<Vector4, Plane>(value);
internal static Plane AsPlane(this Vector4 value)
{
#if MONO
return Unsafe.As<Vector4, Plane>(ref value);
#else
return Unsafe.BitCast<Vector4, Plane>(value);
#endif
}

/// <summary>Reinterprets a <see cref="Vector4" /> as a new <see cref="Quaternion" />.</summary>
/// <param name="value">The vector to reinterpret.</param>
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Quaternion" />.</returns>
[Intrinsic]
internal static Quaternion AsQuaternion(this Vector4 value) => Unsafe.BitCast<Vector4, Quaternion>(value);
internal static Quaternion AsQuaternion(this Vector4 value)
{
#if MONO
return Unsafe.As<Vector4, Quaternion>(ref value);
#else
return Unsafe.BitCast<Vector4, Quaternion>(value);
#endif
}

/// <summary>Gets the element at the specified index.</summary>
/// <param name="vector">The vector to get the element from.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ public static Vector128<TTo> As<TFrom, TTo>(this Vector128<TFrom> vector)
ThrowHelper.ThrowForUnsupportedIntrinsicsVector128BaseType<TFrom>();
ThrowHelper.ThrowForUnsupportedIntrinsicsVector128BaseType<TTo>();

#if MONO
return Unsafe.As<Vector128<TFrom>, Vector128<TTo>>(ref vector);
#else
return Unsafe.BitCast<Vector128<TFrom>, Vector128<TTo>>(vector);
#endif
}

/// <summary>Reinterprets a <see cref="Vector128{T}" /> as a new <see cref="Vector128{Byte}" />.</summary>
Expand Down Expand Up @@ -164,12 +168,26 @@ public static Vector128<TTo> As<TFrom, TTo>(this Vector128<TFrom> vector)
/// <summary>Reinterprets a <see cref="Vector128{Single}" /> as a new <see cref="Plane" />.</summary>
/// <param name="value">The vector to reinterpret.</param>
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Plane" />.</returns>
internal static Plane AsPlane(this Vector128<float> value) => Unsafe.BitCast<Vector128<float>, Plane>(value);
internal static Plane AsPlane(this Vector128<float> value)
{
#if MONO
return Unsafe.As<Vector128<float>, Plane>(ref value);
#else
return Unsafe.BitCast<Vector128<float>, Plane>(value);
#endif
}

/// <summary>Reinterprets a <see cref="Vector128{Single}" /> as a new <see cref="Quaternion" />.</summary>
/// <param name="value">The vector to reinterpret.</param>
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Quaternion" />.</returns>
internal static Quaternion AsQuaternion(this Vector128<float> value) => Unsafe.BitCast<Vector128<float>, Quaternion>(value);
internal static Quaternion AsQuaternion(this Vector128<float> value)
{
#if MONO
return Unsafe.As<Vector128<float>, Quaternion>(ref value);
#else
return Unsafe.BitCast<Vector128<float>, Quaternion>(value);
#endif
}

/// <summary>Reinterprets a <see cref="Vector128{T}" /> as a new <see cref="Vector128{SByte}" />.</summary>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
Expand Down Expand Up @@ -218,12 +236,28 @@ public static Vector128<TTo> As<TFrom, TTo>(this Vector128<TFrom> vector)
/// <summary>Reinterprets a <see cref="Plane" /> as a new <see cref="Vector128{Single}" />.</summary>
/// <param name="value">The plane to reinterpret.</param>
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Vector128{Single}" />.</returns>
internal static Vector128<float> AsVector128(this Plane value) => Unsafe.BitCast<Plane, Vector128<float>>(value);
[Intrinsic]
internal static Vector128<float> AsVector128(this Plane value)
{
#if MONO
return Unsafe.As<Plane, Vector128<float>>(ref value);
#else
return Unsafe.BitCast<Plane, Vector128<float>>(value);
#endif
}

/// <summary>Reinterprets a <see cref="Quaternion" /> as a new <see cref="Vector128{Single}" />.</summary>
/// <param name="value">The quaternion to reinterpret.</param>
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Vector128{Single}" />.</returns>
internal static Vector128<float> AsVector128(this Quaternion value) => Unsafe.BitCast<Quaternion, Vector128<float>>(value);
[Intrinsic]
internal static Vector128<float> AsVector128(this Quaternion value)
{
#if MONO
return Unsafe.As<Quaternion, Vector128<float>>(ref value);
#else
return Unsafe.BitCast<Quaternion, Vector128<float>>(value);
#endif
}

/// <summary>Reinterprets a <see cref="Vector2" /> as a new <see cref="Vector128{Single}" />.</summary>
/// <param name="value">The vector to reinterpret.</param>
Expand All @@ -241,7 +275,14 @@ public static Vector128<TTo> As<TFrom, TTo>(this Vector128<TFrom> vector)
/// <param name="value">The vector to reinterpret.</param>
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Vector128{Single}" />.</returns>
[Intrinsic]
public static Vector128<float> AsVector128(this Vector4 value) => Unsafe.BitCast<Vector4, Vector128<float>>(value);
public static Vector128<float> AsVector128(this Vector4 value)
{
#if MONO
return Unsafe.As<Vector4, Vector128<float>>(ref value);
#else
return Unsafe.BitCast<Vector4, Vector128<float>>(value);
#endif
}

/// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see cref="Vector128{T}" />.</summary>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
Expand Down Expand Up @@ -285,7 +326,14 @@ public static Vector3 AsVector3(this Vector128<float> value)
/// <param name="value">The vector to reinterpret.</param>
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Vector4" />.</returns>
[Intrinsic]
public static Vector4 AsVector4(this Vector128<float> value) => Unsafe.BitCast<Vector128<float>, Vector4>(value);
public static Vector4 AsVector4(this Vector128<float> value)
{
#if MONO
return Unsafe.As<Vector128<float>, Vector4>(ref value);
#else
return Unsafe.BitCast<Vector128<float>, Vector4>(value);
#endif
}

/// <summary>Reinterprets a <see cref="Vector128{T}" /> as a new <see cref="Vector{T}" />.</summary>
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ public static Vector256<TTo> As<TFrom, TTo>(this Vector256<TFrom> vector)
ThrowHelper.ThrowForUnsupportedIntrinsicsVector256BaseType<TFrom>();
ThrowHelper.ThrowForUnsupportedIntrinsicsVector256BaseType<TTo>();

#if MONO
return Unsafe.As<Vector256<TFrom>, Vector256<TTo>>(ref vector);
#else
return Unsafe.BitCast<Vector256<TFrom>, Vector256<TTo>>(vector);
#endif
}

/// <summary>Reinterprets a <see cref="Vector256{T}" /> as a new <see cref="Vector256{Byte}" />.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ public static Vector512<TTo> As<TFrom, TTo>(this Vector512<TFrom> vector)
ThrowHelper.ThrowForUnsupportedIntrinsicsVector512BaseType<TFrom>();
ThrowHelper.ThrowForUnsupportedIntrinsicsVector512BaseType<TTo>();

#if MONO
return Unsafe.As<Vector512<TFrom>, Vector512<TTo>>(ref vector);
#else
return Unsafe.BitCast<Vector512<TFrom>, Vector512<TTo>>(vector);
#endif
}

/// <summary>Reinterprets a <see cref="Vector512{T}" /> as a new <see cref="Vector512{Byte}" />.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public static Vector64<TTo> As<TFrom, TTo>(this Vector64<TFrom> vector)
ThrowHelper.ThrowForUnsupportedIntrinsicsVector64BaseType<TFrom>();
ThrowHelper.ThrowForUnsupportedIntrinsicsVector64BaseType<TTo>();

#if MONO
return Unsafe.As<Vector64<TFrom>, Vector64<TTo>>(ref vector);
#else
return Unsafe.BitCast<Vector64<TFrom>, Vector64<TTo>>(vector);
#endif
}

/// <summary>Reinterprets a <see cref="Vector64{T}" /> as a new <see cref="Vector64{Byte}" />.</summary>
Expand Down

0 comments on commit 4dae1fc

Please sign in to comment.