Skip to content

Commit 14203ab

Browse files
committed
Wokaround an aliasing bug in GetArrayDataReference
1 parent 2299bd7 commit 14203ab

File tree

7 files changed

+22
-22
lines changed

7 files changed

+22
-22
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ public readonly void CopyTo(float[] array)
575575
ThrowHelper.ThrowArgumentException_DestinationTooShort();
576576
}
577577

578-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetArrayDataReference(array)), this);
578+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[0]), this);
579579
}
580580

581581
/// <summary>Copies the elements of the vector to a specified array starting at a specified index position.</summary>
@@ -603,7 +603,7 @@ public readonly void CopyTo(float[] array, int index)
603603
ThrowHelper.ThrowArgumentException_DestinationTooShort();
604604
}
605605

606-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), index)), this);
606+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[index]), this);
607607
}
608608

609609
/// <summary>Copies the vector to the given <see cref="Span{T}" />.The length of the destination span must be at least 2.</summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ public readonly void CopyTo(float[] array)
597597
ThrowHelper.ThrowArgumentException_DestinationTooShort();
598598
}
599599

600-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetArrayDataReference(array)), this);
600+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[0]), this);
601601
}
602602

603603
/// <summary>Copies the elements of the vector to a specified array starting at a specified index position.</summary>
@@ -625,7 +625,7 @@ public readonly void CopyTo(float[] array, int index)
625625
ThrowHelper.ThrowArgumentException_DestinationTooShort();
626626
}
627627

628-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), index)), this);
628+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[index]), this);
629629
}
630630

631631
/// <summary>Copies the vector to the given <see cref="Span{T}" />. The length of the destination span must be at least 3.</summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ public readonly void CopyTo(float[] array)
682682
ThrowHelper.ThrowArgumentException_DestinationTooShort();
683683
}
684684

685-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetArrayDataReference(array)), this);
685+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[0]), this);
686686
}
687687

688688
/// <summary>Copies the elements of the vector to a specified array starting at a specified index position.</summary>
@@ -710,7 +710,7 @@ public readonly void CopyTo(float[] array, int index)
710710
ThrowHelper.ThrowArgumentException_DestinationTooShort();
711711
}
712712

713-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), index)), this);
713+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[index]), this);
714714
}
715715

716716
/// <summary>Copies the vector to the given <see cref="Span{T}" />. The length of the destination span must be at least 4.</summary>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public Vector(T[] values)
6767
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
6868
}
6969

70-
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(values)));
70+
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref values[0]));
7171
}
7272

7373
/// <summary>Creates a new <see cref="Vector{T}" /> from a given array.</summary>
@@ -86,7 +86,7 @@ public Vector(T[] values, int index)
8686
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
8787
}
8888

89-
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(values), index)));
89+
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref values[index]));
9090
}
9191

9292
/// <summary>Creates a new <see cref="Vector{T}" /> from a given readonly span.</summary>
@@ -666,7 +666,7 @@ public void CopyTo(T[] destination)
666666
ThrowHelper.ThrowArgumentException_DestinationTooShort();
667667
}
668668

669-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(destination)), this);
669+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[0]), this);
670670
}
671671

672672
/// <summary>Copies a <see cref="Vector{T}" /> to a given array starting at the specified index.</summary>
@@ -690,7 +690,7 @@ public void CopyTo(T[] destination, int startIndex)
690690
ThrowHelper.ThrowArgumentException_DestinationTooShort();
691691
}
692692

693-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(destination), startIndex)), this);
693+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[startIndex]), this);
694694
}
695695

696696
/// <summary>Copies a <see cref="Vector{T}" /> to a given span.</summary>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ public static void CopyTo<T>(this Vector128<T> vector, T[] destination)
610610
ThrowHelper.ThrowArgumentException_DestinationTooShort();
611611
}
612612

613-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(destination)), vector);
613+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[0]), vector);
614614
}
615615

616616
/// <summary>Copies a <see cref="Vector128{T}" /> to a given array starting at the specified index.</summary>
@@ -638,7 +638,7 @@ public static unsafe void CopyTo<T>(this Vector128<T> vector, T[] destination, i
638638
ThrowHelper.ThrowArgumentException_DestinationTooShort();
639639
}
640640

641-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(destination), startIndex)), vector);
641+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[startIndex]), vector);
642642
}
643643

644644
/// <summary>Copies a <see cref="Vector128{T}" /> to a given span.</summary>
@@ -790,7 +790,7 @@ public static Vector128<T> Create<T>(T[] values)
790790
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
791791
}
792792

793-
return Unsafe.ReadUnaligned<Vector128<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(values)));
793+
return Unsafe.ReadUnaligned<Vector128<T>>(ref Unsafe.As<T, byte>(ref values[0]));
794794
}
795795

796796
/// <summary>Creates a new <see cref="Vector128{T}" /> from a given array.</summary>
@@ -812,7 +812,7 @@ public static Vector128<T> Create<T>(T[] values, int index)
812812
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
813813
}
814814

815-
return Unsafe.ReadUnaligned<Vector128<T>>(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(values), index)));
815+
return Unsafe.ReadUnaligned<Vector128<T>>(ref Unsafe.As<T, byte>(ref values[index]));
816816
}
817817

818818
/// <summary>Creates a new <see cref="Vector128{T}" /> from a given readonly span.</summary>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public static void CopyTo<T>(this Vector256<T> vector, T[] destination)
536536
ThrowHelper.ThrowArgumentException_DestinationTooShort();
537537
}
538538

539-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(destination)), vector);
539+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[0]), vector);
540540
}
541541

542542
/// <summary>Copies a <see cref="Vector256{T}" /> to a given array starting at the specified index.</summary>
@@ -564,7 +564,7 @@ public static void CopyTo<T>(this Vector256<T> vector, T[] destination, int star
564564
ThrowHelper.ThrowArgumentException_DestinationTooShort();
565565
}
566566

567-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(destination), startIndex)), vector);
567+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[startIndex]), vector);
568568
}
569569

570570
/// <summary>Copies a <see cref="Vector256{T}" /> to a given span.</summary>
@@ -716,7 +716,7 @@ public static Vector256<T> Create<T>(T[] values)
716716
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
717717
}
718718

719-
return Unsafe.ReadUnaligned<Vector256<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(values)));
719+
return Unsafe.ReadUnaligned<Vector256<T>>(ref Unsafe.As<T, byte>(ref values[0]));
720720
}
721721

722722
/// <summary>Creates a new <see cref="Vector256{T}" /> from a given array.</summary>
@@ -738,7 +738,7 @@ public static Vector256<T> Create<T>(T[] values, int index)
738738
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
739739
}
740740

741-
return Unsafe.ReadUnaligned<Vector256<T>>(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(values), index)));
741+
return Unsafe.ReadUnaligned<Vector256<T>>(ref Unsafe.As<T, byte>(ref values[index]));
742742
}
743743

744744
/// <summary>Creates a new <see cref="Vector256{T}" /> from a given readonly span.</summary>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public static void CopyTo<T>(this Vector64<T> vector, T[] destination)
476476
ThrowHelper.ThrowArgumentException_DestinationTooShort();
477477
}
478478

479-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(destination)), vector);
479+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[0]), vector);
480480
}
481481

482482
/// <summary>Copies a <see cref="Vector64{T}" /> to a given array starting at the specified index.</summary>
@@ -504,7 +504,7 @@ public static unsafe void CopyTo<T>(this Vector64<T> vector, T[] destination, in
504504
ThrowHelper.ThrowArgumentException_DestinationTooShort();
505505
}
506506

507-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(destination), startIndex)), vector);
507+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[startIndex]), vector);
508508
}
509509

510510
/// <summary>Copies a <see cref="Vector64{T}" /> to a given span.</summary>
@@ -657,7 +657,7 @@ public static Vector64<T> Create<T>(T[] values)
657657
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
658658
}
659659

660-
return Unsafe.ReadUnaligned<Vector64<T>>(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(values)));
660+
return Unsafe.ReadUnaligned<Vector64<T>>(ref Unsafe.As<T, byte>(ref values[0]));
661661
}
662662

663663
/// <summary>Creates a new <see cref="Vector64{T}" /> from a given array.</summary>
@@ -679,7 +679,7 @@ public static Vector64<T> Create<T>(T[] values, int index)
679679
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
680680
}
681681

682-
return Unsafe.ReadUnaligned<Vector64<T>>(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(values), index)));
682+
return Unsafe.ReadUnaligned<Vector64<T>>(ref Unsafe.As<T, byte>(ref values[index]));
683683
}
684684

685685
/// <summary>Creates a new <see cref="Vector64{T}" /> from a given readonly span.</summary>

0 commit comments

Comments
 (0)