Skip to content

Commit

Permalink
Wokaround an aliasing bug in GetArrayDataReference
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Dec 16, 2022
1 parent 2299bd7 commit 14203ab
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public readonly void CopyTo(float[] array)
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}

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

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

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

/// <summary>Copies the vector to the given <see cref="Span{T}" />.The length of the destination span must be at least 2.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public readonly void CopyTo(float[] array)
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}

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

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

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

/// <summary>Copies the vector to the given <see cref="Span{T}" />. The length of the destination span must be at least 3.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ public readonly void CopyTo(float[] array)
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}

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

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

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

/// <summary>Copies the vector to the given <see cref="Span{T}" />. The length of the destination span must be at least 4.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Vector(T[] values)
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
}

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

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

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

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

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

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

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

/// <summary>Copies a <see cref="Vector{T}" /> to a given span.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public static void CopyTo<T>(this Vector128<T> vector, T[] destination)
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}

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

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

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

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

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

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

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

/// <summary>Creates a new <see cref="Vector128{T}" /> from a given readonly span.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public static void CopyTo<T>(this Vector256<T> vector, T[] destination)
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}

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

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

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

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

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

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

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

/// <summary>Creates a new <see cref="Vector256{T}" /> from a given readonly span.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public static void CopyTo<T>(this Vector64<T> vector, T[] destination)
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}

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

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

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

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

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

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

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

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

0 comments on commit 14203ab

Please sign in to comment.