Skip to content

Commit 1d6cafa

Browse files
committed
Don't use Unsafe.BitCast on Mono
1 parent 6c6d71e commit 1d6cafa

File tree

6 files changed

+86
-10
lines changed

6 files changed

+86
-10
lines changed

src/libraries/System.Private.CoreLib/src/System/Numerics/Vector.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ public static Vector<TTo> As<TFrom, TTo>(this Vector<TFrom> vector)
8383
ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<TFrom>();
8484
ThrowHelper.ThrowForUnsupportedNumericsVectorBaseType<TTo>();
8585

86+
#if MONO
87+
return Unsafe.As<Vector<TFrom>, Vector<TTo>>(ref value);
88+
#else
8689
return Unsafe.BitCast<Vector<TFrom>, Vector<TTo>>(vector);
90+
#endif
8791
}
8892

8993
/// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see cref="Vector{Byte}" />.</summary>

src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.Extensions.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@ public static unsafe partial class Vector
1212
/// <summary>Reinterprets a <see cref="Vector4" /> as a new <see cref="Plane" />.</summary>
1313
/// <param name="value">The vector to reinterpret.</param>
1414
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Plane" />.</returns>
15-
[Intrinsic]
16-
internal static Plane AsPlane(this Vector4 value) => Unsafe.BitCast<Vector4, Plane>(value);
15+
internal static Plane AsPlane(this Vector4 value)
16+
{
17+
#if MONO
18+
return Unsafe.As<Vector4, Plane>(ref value);
19+
#else
20+
return Unsafe.BitCast<Vector4, Plane>(value);
21+
#endif
22+
}
1723

1824
/// <summary>Reinterprets a <see cref="Vector4" /> as a new <see cref="Quaternion" />.</summary>
1925
/// <param name="value">The vector to reinterpret.</param>
2026
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Quaternion" />.</returns>
21-
[Intrinsic]
22-
internal static Quaternion AsQuaternion(this Vector4 value) => Unsafe.BitCast<Vector4, Quaternion>(value);
27+
internal static Quaternion AsQuaternion(this Vector4 value)
28+
{
29+
#if MONO
30+
return Unsafe.As<Vector4, Quaternion>(ref value);
31+
#else
32+
return Unsafe.BitCast<Vector4, Quaternion>(value);
33+
#endif
34+
}
2335

2436
/// <summary>Gets the element at the specified index.</summary>
2537
/// <param name="vector">The vector to get the element from.</param>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector128.cs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ public static Vector128<TTo> As<TFrom, TTo>(this Vector128<TFrom> vector)
101101
ThrowHelper.ThrowForUnsupportedIntrinsicsVector128BaseType<TFrom>();
102102
ThrowHelper.ThrowForUnsupportedIntrinsicsVector128BaseType<TTo>();
103103

104+
#if MONO
105+
return Unsafe.As<Vector128<TFrom>, Vector128<TTo>>(ref vector);
106+
#else
104107
return Unsafe.BitCast<Vector128<TFrom>, Vector128<TTo>>(vector);
108+
#endif
105109
}
106110

107111
/// <summary>Reinterprets a <see cref="Vector128{T}" /> as a new <see cref="Vector128{Byte}" />.</summary>
@@ -164,12 +168,26 @@ public static Vector128<TTo> As<TFrom, TTo>(this Vector128<TFrom> vector)
164168
/// <summary>Reinterprets a <see cref="Vector128{Single}" /> as a new <see cref="Plane" />.</summary>
165169
/// <param name="value">The vector to reinterpret.</param>
166170
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Plane" />.</returns>
167-
internal static Plane AsPlane(this Vector128<float> value) => Unsafe.BitCast<Vector128<float>, Plane>(value);
171+
internal static Plane AsPlane(this Vector128<float> value)
172+
{
173+
#if MONO
174+
return Unsafe.As<Vector128<float>, Plane>(ref value);
175+
#else
176+
return Unsafe.BitCast<Vector128<float>, Plane>(value);
177+
#endif
178+
}
168179

169180
/// <summary>Reinterprets a <see cref="Vector128{Single}" /> as a new <see cref="Quaternion" />.</summary>
170181
/// <param name="value">The vector to reinterpret.</param>
171182
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Quaternion" />.</returns>
172-
internal static Quaternion AsQuaternion(this Vector128<float> value) => Unsafe.BitCast<Vector128<float>, Quaternion>(value);
183+
internal static Quaternion AsQuaternion(this Vector128<float> value)
184+
{
185+
#if MONO
186+
return Unsafe.As<Vector128<float>, Quaternion>(ref value);
187+
#else
188+
return Unsafe.BitCast<Vector128<float>, Quaternion>(value);
189+
#endif
190+
}
173191

174192
/// <summary>Reinterprets a <see cref="Vector128{T}" /> as a new <see cref="Vector128{SByte}" />.</summary>
175193
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
@@ -218,12 +236,28 @@ public static Vector128<TTo> As<TFrom, TTo>(this Vector128<TFrom> vector)
218236
/// <summary>Reinterprets a <see cref="Plane" /> as a new <see cref="Vector128{Single}" />.</summary>
219237
/// <param name="value">The plane to reinterpret.</param>
220238
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Vector128{Single}" />.</returns>
221-
internal static Vector128<float> AsVector128(this Plane value) => Unsafe.BitCast<Plane, Vector128<float>>(value);
239+
[Intrinsic]
240+
internal static Vector128<float> AsVector128(this Plane value)
241+
{
242+
#if MONO
243+
return Unsafe.As<Plane, Vector128<float>>(ref value);
244+
#else
245+
return Unsafe.BitCast<Plane, Vector128<float>>(value);
246+
#endif
247+
}
222248

223249
/// <summary>Reinterprets a <see cref="Quaternion" /> as a new <see cref="Vector128{Single}" />.</summary>
224250
/// <param name="value">The quaternion to reinterpret.</param>
225251
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Vector128{Single}" />.</returns>
226-
internal static Vector128<float> AsVector128(this Quaternion value) => Unsafe.BitCast<Quaternion, Vector128<float>>(value);
252+
[Intrinsic]
253+
internal static Vector128<float> AsVector128(this Quaternion value)
254+
{
255+
#if MONO
256+
return Unsafe.As<Quaternion, Vector128<float>>(ref value);
257+
#else
258+
return Unsafe.BitCast<Quaternion, Vector128<float>>(value);
259+
#endif
260+
}
227261

228262
/// <summary>Reinterprets a <see cref="Vector2" /> as a new <see cref="Vector128{Single}" />.</summary>
229263
/// <param name="value">The vector to reinterpret.</param>
@@ -241,7 +275,14 @@ public static Vector128<TTo> As<TFrom, TTo>(this Vector128<TFrom> vector)
241275
/// <param name="value">The vector to reinterpret.</param>
242276
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Vector128{Single}" />.</returns>
243277
[Intrinsic]
244-
public static Vector128<float> AsVector128(this Vector4 value) => Unsafe.BitCast<Vector4, Vector128<float>>(value);
278+
public static Vector128<float> AsVector128(this Vector4 value)
279+
{
280+
#if MONO
281+
return Unsafe.As<Vector4, Vector128<float>>(ref value);
282+
#else
283+
return Unsafe.BitCast<Vector4, Vector128<float>>(value);
284+
#endif
285+
}
245286

246287
/// <summary>Reinterprets a <see cref="Vector{T}" /> as a new <see cref="Vector128{T}" />.</summary>
247288
/// <typeparam name="T">The type of the elements in the vector.</typeparam>
@@ -285,7 +326,14 @@ public static Vector3 AsVector3(this Vector128<float> value)
285326
/// <param name="value">The vector to reinterpret.</param>
286327
/// <returns><paramref name="value" /> reinterpreted as a new <see cref="Vector4" />.</returns>
287328
[Intrinsic]
288-
public static Vector4 AsVector4(this Vector128<float> value) => Unsafe.BitCast<Vector128<float>, Vector4>(value);
329+
public static Vector4 AsVector4(this Vector128<float> value)
330+
{
331+
#if MONO
332+
return Unsafe.As<Vector128<float>, Vector4>(ref value);
333+
#else
334+
return Unsafe.BitCast<Vector128<float>, Vector4>(value);
335+
#endif
336+
}
289337

290338
/// <summary>Reinterprets a <see cref="Vector128{T}" /> as a new <see cref="Vector{T}" />.</summary>
291339
/// <typeparam name="T">The type of the elements in the vector.</typeparam>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector256.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ public static Vector256<TTo> As<TFrom, TTo>(this Vector256<TFrom> vector)
106106
ThrowHelper.ThrowForUnsupportedIntrinsicsVector256BaseType<TFrom>();
107107
ThrowHelper.ThrowForUnsupportedIntrinsicsVector256BaseType<TTo>();
108108

109+
#if MONO
110+
return Unsafe.As<Vector256<TFrom>, Vector256<TTo>>(ref value);
111+
#else
109112
return Unsafe.BitCast<Vector256<TFrom>, Vector256<TTo>>(vector);
113+
#endif
110114
}
111115

112116
/// <summary>Reinterprets a <see cref="Vector256{T}" /> as a new <see cref="Vector256{Byte}" />.</summary>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector512.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ public static Vector512<TTo> As<TFrom, TTo>(this Vector512<TFrom> vector)
106106
ThrowHelper.ThrowForUnsupportedIntrinsicsVector512BaseType<TFrom>();
107107
ThrowHelper.ThrowForUnsupportedIntrinsicsVector512BaseType<TTo>();
108108

109+
#if MONO
110+
return Unsafe.As<Vector512<TFrom>, Vector512<TTo>>(ref value);
111+
#else
109112
return Unsafe.BitCast<Vector512<TFrom>, Vector512<TTo>>(vector);
113+
#endif
110114
}
111115

112116
/// <summary>Reinterprets a <see cref="Vector512{T}" /> as a new <see cref="Vector512{Byte}" />.</summary>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector64.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ public static Vector64<TTo> As<TFrom, TTo>(this Vector64<TFrom> vector)
8989
ThrowHelper.ThrowForUnsupportedIntrinsicsVector64BaseType<TFrom>();
9090
ThrowHelper.ThrowForUnsupportedIntrinsicsVector64BaseType<TTo>();
9191

92+
#if MONO
93+
return Unsafe.As<Vector64<TFrom>, Vector64<TTo>>(ref value);
94+
#else
9295
return Unsafe.BitCast<Vector64<TFrom>, Vector64<TTo>>(vector);
96+
#endif
9397
}
9498

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

0 commit comments

Comments
 (0)