Skip to content

Commit f7652ef

Browse files
[release/8.0-staging] Fix Vector512.CopyTo (#95026)
* Fix Vector512.CopyTo * Also unify Vector * Handle float vectors too * Fix legacy vectors * Simplify the change * Update Vector_1.cs * Add tests * Fix build --------- Co-authored-by: Michał Petryka <[email protected]>
1 parent b126b9a commit f7652ef

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,7 @@ public static void CopyTo<T>(this Vector512<T> vector, T[] destination)
441441
ThrowHelper.ThrowArgumentException_DestinationTooShort();
442442
}
443443

444-
ref byte address = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(destination));
445-
Unsafe.WriteUnaligned(ref address, vector);
444+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[0]), vector);
446445
}
447446

448447
/// <summary>Copies a <see cref="Vector512{T}" /> to a given array starting at the specified index.</summary>
@@ -468,8 +467,7 @@ public static void CopyTo<T>(this Vector512<T> vector, T[] destination, int star
468467
ThrowHelper.ThrowArgumentException_DestinationTooShort();
469468
}
470469

471-
ref byte address = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(destination));
472-
Unsafe.WriteUnaligned(ref Unsafe.Add(ref address, startIndex), vector);
470+
Unsafe.WriteUnaligned(ref Unsafe.As<T, byte>(ref destination[startIndex]), vector);
473471
}
474472

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

src/libraries/System.Runtime.Intrinsics/tests/Vectors/Vector128Tests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,6 +4517,22 @@ public void Vector128SingleEqualsNonCanonicalNaNTest()
45174517
}
45184518
}
45194519

4520+
[Fact]
4521+
public void Vector128SingleCopyToTest()
4522+
{
4523+
float[] array = new float[4];
4524+
Vector128.Create(2.0f).CopyTo(array);
4525+
Assert.True(array.AsSpan().SequenceEqual([2.0f, 2.0f, 2.0f, 2.0f]));
4526+
}
4527+
4528+
[Fact]
4529+
public void Vector128SingleCopyToOffsetTest()
4530+
{
4531+
float[] array = new float[5];
4532+
Vector128.Create(2.0f).CopyTo(array, 1);
4533+
Assert.True(array.AsSpan().SequenceEqual([0.0f, 2.0f, 2.0f, 2.0f, 2.0f]));
4534+
}
4535+
45204536
[Fact]
45214537
public void IsSupportedByte() => TestIsSupported<byte>();
45224538

src/libraries/System.Runtime.Intrinsics/tests/Vectors/Vector256Tests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5539,6 +5539,22 @@ public void Vector256SingleEqualsNonCanonicalNaNTest()
55395539
}
55405540
}
55415541

5542+
[Fact]
5543+
public void Vector256SingleCopyToTest()
5544+
{
5545+
float[] array = new float[8];
5546+
Vector256.Create(2.0f).CopyTo(array);
5547+
Assert.True(array.AsSpan().SequenceEqual([2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f]));
5548+
}
5549+
5550+
[Fact]
5551+
public void Vector256SingleCopyToOffsetTest()
5552+
{
5553+
float[] array = new float[9];
5554+
Vector256.Create(2.0f).CopyTo(array, 1);
5555+
Assert.True(array.AsSpan().SequenceEqual([0.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f]));
5556+
}
5557+
55425558
[Fact]
55435559
public void IsSupportedByte() => TestIsSupported<byte>();
55445560

src/libraries/System.Runtime.Intrinsics/tests/Vectors/Vector512Tests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5016,6 +5016,22 @@ public void Vector512SingleEqualsNonCanonicalNaNTest()
50165016
}
50175017
}
50185018

5019+
[Fact]
5020+
public void Vector512SingleCopyToTest()
5021+
{
5022+
float[] array = new float[16];
5023+
Vector512.Create(2.0f).CopyTo(array);
5024+
Assert.True(array.AsSpan().SequenceEqual([2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f]));
5025+
}
5026+
5027+
[Fact]
5028+
public void Vector512SingleCopyToOffsetTest()
5029+
{
5030+
float[] array = new float[17];
5031+
Vector512.Create(2.0f).CopyTo(array, 1);
5032+
Assert.True(array.AsSpan().SequenceEqual([0.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f]));
5033+
}
5034+
50195035
[Fact]
50205036
public void IsSupportedByte() => TestIsSupported<byte>();
50215037

src/libraries/System.Runtime.Intrinsics/tests/Vectors/Vector64Tests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,6 +3937,22 @@ public void Vector64SingleEqualsNonCanonicalNaNTest()
39373937
}
39383938
}
39393939

3940+
[Fact]
3941+
public void Vector64SingleCopyToTest()
3942+
{
3943+
float[] array = new float[2];
3944+
Vector64.Create(2.0f).CopyTo(array);
3945+
Assert.True(array.AsSpan().SequenceEqual([2.0f, 2.0f]));
3946+
}
3947+
3948+
[Fact]
3949+
public void Vector64SingleCopyToOffsetTest()
3950+
{
3951+
float[] array = new float[3];
3952+
Vector64.Create(2.0f).CopyTo(array, 1);
3953+
Assert.True(array.AsSpan().SequenceEqual([0.0f, 2.0f, 2.0f]));
3954+
}
3955+
39403956
[Fact]
39413957
public void IsSupportedByte() => TestIsSupported<byte>();
39423958

0 commit comments

Comments
 (0)