Skip to content

Commit 8a10bfd

Browse files
committed
Use GetArrayDataReference in Vector*
1 parent 9efc798 commit 8a10bfd

File tree

12 files changed

+89
-84
lines changed

12 files changed

+89
-84
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
@@ -469,7 +469,7 @@ public readonly void CopyTo(float[] array)
469469
ThrowHelper.ThrowArgumentException_DestinationTooShort();
470470
}
471471

472-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[0]), this);
472+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetArrayDataReference(array)), this);
473473
}
474474

475475
/// <summary>Copies the elements of the vector to a specified array starting at a specified index position.</summary>
@@ -497,7 +497,7 @@ public readonly void CopyTo(float[] array, int index)
497497
ThrowHelper.ThrowArgumentException_DestinationTooShort();
498498
}
499499

500-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[index]), this);
500+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (uint)index)), this);
501501
}
502502

503503
/// <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
@@ -499,7 +499,7 @@ public readonly void CopyTo(float[] array)
499499
ThrowHelper.ThrowArgumentException_DestinationTooShort();
500500
}
501501

502-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[0]), this);
502+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetArrayDataReference(array)), this);
503503
}
504504

505505
/// <summary>Copies the elements of the vector to a specified array starting at a specified index position.</summary>
@@ -527,7 +527,7 @@ public readonly void CopyTo(float[] array, int index)
527527
ThrowHelper.ThrowArgumentException_DestinationTooShort();
528528
}
529529

530-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref array[index]), this);
530+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (uint)index)), this);
531531
}
532532

533533
/// <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/VectorDebugView_1.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.CompilerServices;
5+
using System.Runtime.InteropServices;
56

67
namespace System.Numerics
78
{
@@ -20,7 +21,7 @@ public byte[] ByteView
2021
get
2122
{
2223
var items = new byte[Vector<byte>.Count];
23-
Unsafe.WriteUnaligned(ref items[0], _value);
24+
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(items), _value);
2425
return items;
2526
}
2627
}
@@ -30,7 +31,7 @@ public double[] DoubleView
3031
get
3132
{
3233
var items = new double[Vector<double>.Count];
33-
Unsafe.WriteUnaligned(ref Unsafe.As<double, byte>(ref items[0]), _value);
34+
Unsafe.WriteUnaligned(ref Unsafe.As<double, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
3435
return items;
3536
}
3637
}
@@ -40,7 +41,7 @@ public short[] Int16View
4041
get
4142
{
4243
var items = new short[Vector<short>.Count];
43-
Unsafe.WriteUnaligned(ref Unsafe.As<short, byte>(ref items[0]), _value);
44+
Unsafe.WriteUnaligned(ref Unsafe.As<short, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
4445
return items;
4546
}
4647
}
@@ -50,7 +51,7 @@ public int[] Int32View
5051
get
5152
{
5253
var items = new int[Vector<int>.Count];
53-
Unsafe.WriteUnaligned(ref Unsafe.As<int, byte>(ref items[0]), _value);
54+
Unsafe.WriteUnaligned(ref Unsafe.As<int, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
5455
return items;
5556
}
5657
}
@@ -60,7 +61,7 @@ public long[] Int64View
6061
get
6162
{
6263
var items = new long[Vector<long>.Count];
63-
Unsafe.WriteUnaligned(ref Unsafe.As<long, byte>(ref items[0]), _value);
64+
Unsafe.WriteUnaligned(ref Unsafe.As<long, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
6465
return items;
6566
}
6667
}
@@ -70,7 +71,7 @@ public nint[] NIntView
7071
get
7172
{
7273
var items = new nint[Vector<nint>.Count];
73-
Unsafe.WriteUnaligned(ref Unsafe.As<nint, byte>(ref items[0]), _value);
74+
Unsafe.WriteUnaligned(ref Unsafe.As<nint, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
7475
return items;
7576
}
7677
}
@@ -80,7 +81,7 @@ public nuint[] NUIntView
8081
get
8182
{
8283
var items = new nuint[Vector<nuint>.Count];
83-
Unsafe.WriteUnaligned(ref Unsafe.As<nuint, byte>(ref items[0]), _value);
84+
Unsafe.WriteUnaligned(ref Unsafe.As<nuint, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
8485
return items;
8586
}
8687
}
@@ -90,7 +91,7 @@ public sbyte[] SByteView
9091
get
9192
{
9293
var items = new sbyte[Vector<sbyte>.Count];
93-
Unsafe.WriteUnaligned(ref Unsafe.As<sbyte, byte>(ref items[0]), _value);
94+
Unsafe.WriteUnaligned(ref Unsafe.As<sbyte, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
9495
return items;
9596
}
9697
}
@@ -100,7 +101,7 @@ public float[] SingleView
100101
get
101102
{
102103
var items = new float[Vector<float>.Count];
103-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref items[0]), _value);
104+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
104105
return items;
105106
}
106107
}
@@ -110,7 +111,7 @@ public ushort[] UInt16View
110111
get
111112
{
112113
var items = new ushort[Vector<ushort>.Count];
113-
Unsafe.WriteUnaligned(ref Unsafe.As<ushort, byte>(ref items[0]), _value);
114+
Unsafe.WriteUnaligned(ref Unsafe.As<ushort, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
114115
return items;
115116
}
116117
}
@@ -120,7 +121,7 @@ public uint[] UInt32View
120121
get
121122
{
122123
var items = new uint[Vector<uint>.Count];
123-
Unsafe.WriteUnaligned(ref Unsafe.As<uint, byte>(ref items[0]), _value);
124+
Unsafe.WriteUnaligned(ref Unsafe.As<uint, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
124125
return items;
125126
}
126127
}
@@ -130,7 +131,7 @@ public ulong[] UInt64View
130131
get
131132
{
132133
var items = new ulong[Vector<ulong>.Count];
133-
Unsafe.WriteUnaligned(ref Unsafe.As<ulong, byte>(ref items[0]), _value);
134+
Unsafe.WriteUnaligned(ref Unsafe.As<ulong, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
134135
return items;
135136
}
136137
}

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
@@ -63,7 +63,7 @@ public Vector(T[] values)
6363
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
6464
}
6565

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

6969
/// <summary>Creates a new <see cref="Vector{T}" /> from a given array.</summary>
@@ -82,7 +82,7 @@ public Vector(T[] values, int index)
8282
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
8383
}
8484

85-
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref values[index]));
85+
this = Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(values), (uint)index)));
8686
}
8787

8888
/// <summary>Creates a new <see cref="Vector{T}" /> from a given readonly span.</summary>
@@ -624,7 +624,7 @@ public void CopyTo(T[] destination)
624624
ThrowHelper.ThrowArgumentException_DestinationTooShort();
625625
}
626626

627-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[0]), this);
627+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(destination)), this);
628628
}
629629

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

651-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[startIndex]), this);
651+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(destination), (uint)startIndex)), this);
652652
}
653653

654654
/// <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
@@ -735,7 +735,7 @@ public static void CopyTo<T>(this Vector128<T> vector, T[] destination)
735735
ThrowHelper.ThrowArgumentException_DestinationTooShort();
736736
}
737737

738-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[0]), vector);
738+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(destination)), vector);
739739
}
740740

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

765-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[startIndex]), vector);
765+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(destination), (uint)startIndex)), vector);
766766
}
767767

768768
/// <summary>Copies a <see cref="Vector128{T}" /> to a given span.</summary>
@@ -898,7 +898,7 @@ public static Vector128<T> Create<T>(T[] values)
898898
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
899899
}
900900

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

904904
/// <summary>Creates a new <see cref="Vector128{T}" /> from a given array.</summary>
@@ -919,7 +919,7 @@ public static Vector128<T> Create<T>(T[] values, int index)
919919
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
920920
}
921921

922-
return Unsafe.ReadUnaligned<Vector128<T>>(ref Unsafe.As<T, byte>(ref values[index]));
922+
return Unsafe.ReadUnaligned<Vector128<T>>(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(values), (uint)index)));
923923
}
924924

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

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.CompilerServices;
5+
using System.Runtime.InteropServices;
56

67
namespace System.Runtime.Intrinsics
78
{
@@ -19,7 +20,7 @@ public byte[] ByteView
1920
get
2021
{
2122
var items = new byte[Vector128<byte>.Count];
22-
Unsafe.WriteUnaligned(ref items[0], _value);
23+
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(items), _value);
2324
return items;
2425
}
2526
}
@@ -29,7 +30,7 @@ public double[] DoubleView
2930
get
3031
{
3132
var items = new double[Vector128<double>.Count];
32-
Unsafe.WriteUnaligned(ref Unsafe.As<double, byte>(ref items[0]), _value);
33+
Unsafe.WriteUnaligned(ref Unsafe.As<double, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
3334
return items;
3435
}
3536
}
@@ -39,7 +40,7 @@ public short[] Int16View
3940
get
4041
{
4142
var items = new short[Vector128<short>.Count];
42-
Unsafe.WriteUnaligned(ref Unsafe.As<short, byte>(ref items[0]), _value);
43+
Unsafe.WriteUnaligned(ref Unsafe.As<short, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
4344
return items;
4445
}
4546
}
@@ -49,7 +50,7 @@ public int[] Int32View
4950
get
5051
{
5152
var items = new int[Vector128<int>.Count];
52-
Unsafe.WriteUnaligned(ref Unsafe.As<int, byte>(ref items[0]), _value);
53+
Unsafe.WriteUnaligned(ref Unsafe.As<int, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
5354
return items;
5455
}
5556
}
@@ -59,7 +60,7 @@ public long[] Int64View
5960
get
6061
{
6162
var items = new long[Vector128<long>.Count];
62-
Unsafe.WriteUnaligned(ref Unsafe.As<long, byte>(ref items[0]), _value);
63+
Unsafe.WriteUnaligned(ref Unsafe.As<long, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
6364
return items;
6465
}
6566
}
@@ -69,7 +70,7 @@ public nint[] NIntView
6970
get
7071
{
7172
var items = new nint[Vector128<nint>.Count];
72-
Unsafe.WriteUnaligned(ref Unsafe.As<nint, byte>(ref items[0]), _value);
73+
Unsafe.WriteUnaligned(ref Unsafe.As<nint, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
7374
return items;
7475
}
7576
}
@@ -79,7 +80,7 @@ public nuint[] NUIntView
7980
get
8081
{
8182
var items = new nuint[Vector128<nuint>.Count];
82-
Unsafe.WriteUnaligned(ref Unsafe.As<nuint, byte>(ref items[0]), _value);
83+
Unsafe.WriteUnaligned(ref Unsafe.As<nuint, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
8384
return items;
8485
}
8586
}
@@ -89,7 +90,7 @@ public sbyte[] SByteView
8990
get
9091
{
9192
var items = new sbyte[Vector128<sbyte>.Count];
92-
Unsafe.WriteUnaligned(ref Unsafe.As<sbyte, byte>(ref items[0]), _value);
93+
Unsafe.WriteUnaligned(ref Unsafe.As<sbyte, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
9394
return items;
9495
}
9596
}
@@ -99,7 +100,7 @@ public float[] SingleView
99100
get
100101
{
101102
var items = new float[Vector128<float>.Count];
102-
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref items[0]), _value);
103+
Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
103104
return items;
104105
}
105106
}
@@ -109,7 +110,7 @@ public ushort[] UInt16View
109110
get
110111
{
111112
var items = new ushort[Vector128<ushort>.Count];
112-
Unsafe.WriteUnaligned(ref Unsafe.As<ushort, byte>(ref items[0]), _value);
113+
Unsafe.WriteUnaligned(ref Unsafe.As<ushort, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
113114
return items;
114115
}
115116
}
@@ -119,7 +120,7 @@ public uint[] UInt32View
119120
get
120121
{
121122
var items = new uint[Vector128<uint>.Count];
122-
Unsafe.WriteUnaligned(ref Unsafe.As<uint, byte>(ref items[0]), _value);
123+
Unsafe.WriteUnaligned(ref Unsafe.As<uint, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
123124
return items;
124125
}
125126
}
@@ -129,7 +130,7 @@ public ulong[] UInt64View
129130
get
130131
{
131132
var items = new ulong[Vector128<ulong>.Count];
132-
Unsafe.WriteUnaligned(ref Unsafe.As<ulong, byte>(ref items[0]), _value);
133+
Unsafe.WriteUnaligned(ref Unsafe.As<ulong, byte>(ref MemoryMarshal.GetArrayDataReference(items)), _value);
133134
return items;
134135
}
135136
}

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
@@ -574,7 +574,7 @@ public static void CopyTo<T>(this Vector256<T> vector, T[] destination)
574574
ThrowHelper.ThrowArgumentException_DestinationTooShort();
575575
}
576576

577-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[0]), vector);
577+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(destination)), vector);
578578
}
579579

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

604-
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[startIndex]), vector);
604+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(destination), (uint)startIndex)), vector);
605605
}
606606

607607
/// <summary>Copies a <see cref="Vector256{T}" /> to a given span.</summary>
@@ -737,7 +737,7 @@ public static Vector256<T> Create<T>(T[] values)
737737
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
738738
}
739739

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

743743
/// <summary>Creates a new <see cref="Vector256{T}" /> from a given array.</summary>
@@ -758,7 +758,7 @@ public static Vector256<T> Create<T>(T[] values, int index)
758758
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
759759
}
760760

761-
return Unsafe.ReadUnaligned<Vector256<T>>(ref Unsafe.As<T, byte>(ref values[index]));
761+
return Unsafe.ReadUnaligned<Vector256<T>>(ref Unsafe.As<T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(values), (uint)index)));
762762
}
763763

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

0 commit comments

Comments
 (0)