Skip to content

Commit

Permalink
Replace some more Unsafe.SizeOf with sizeof
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jul 16, 2024
1 parent 8d403f8 commit 5f53468
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void VerifyPackageInfo()
SSPIWrapper.GetVerifyPackageInfo(GlobalSSPI.SSPISecureChannel, SecurityPackage, true);
}

private static void SetAlpn(ref InputSecurityBuffers inputBuffers, List<SslApplicationProtocol> alpn, Span<byte> localBuffer)
private static unsafe void SetAlpn(ref InputSecurityBuffers inputBuffers, List<SslApplicationProtocol> alpn, Span<byte> localBuffer)
{
if (alpn.Count == 1 && alpn[0] == SslApplicationProtocol.Http11)
{
Expand All @@ -84,7 +84,7 @@ private static void SetAlpn(ref InputSecurityBuffers inputBuffers, List<SslAppli
else
{
int protocolLength = Interop.Sec_Application_Protocols.GetProtocolLength(alpn);
int bufferLength = Unsafe.SizeOf<Interop.Sec_Application_Protocols>() + protocolLength;
int bufferLength = sizeof(Interop.Sec_Application_Protocols) + protocolLength;

Span<byte> alpnBuffer = bufferLength <= localBuffer.Length ? localBuffer : new byte[bufferLength];
Interop.Sec_Application_Protocols.SetProtocols(alpnBuffer, alpn, protocolLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2276,28 +2276,28 @@ static T VectorizedSmall8(ref T xRef, ref T yRef, nuint remainder)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<T> CreateAlignmentMaskVector128<T>(int count)
{
if (Unsafe.SizeOf<T>() == 1)
if (sizeof(T) == 1)
{
return Vector128.LoadUnsafe(
ref Unsafe.As<byte, T>(ref MemoryMarshal.GetReference(AlignmentByteMask_64x65)),
(uint)(count * 64));
}

if (Unsafe.SizeOf<T>() == 2)
if (sizeof(T) == 2)
{
return Vector128.LoadUnsafe(
ref Unsafe.As<ushort, T>(ref MemoryMarshal.GetReference(AlignmentUInt16Mask_32x33)),
(uint)(count * 32));
}

if (Unsafe.SizeOf<T>() == 4)
if (sizeof(T) == 4)
{
return Vector128.LoadUnsafe(
ref Unsafe.As<uint, T>(ref MemoryMarshal.GetReference(AlignmentUInt32Mask_16x17)),
(uint)(count * 16));
}

Debug.Assert(Unsafe.SizeOf<T>() == 8);
Debug.Assert(sizeof(T) == 8);
{
return Vector128.LoadUnsafe(
ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(AlignmentUInt64Mask_8x9)),
Expand All @@ -2312,28 +2312,28 @@ ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(AlignmentUInt64Mask_8x9))
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<T> CreateAlignmentMaskVector256<T>(int count)
{
if (Unsafe.SizeOf<T>() == 1)
if (sizeof(T) == 1)
{
return Vector256.LoadUnsafe(
ref Unsafe.As<byte, T>(ref MemoryMarshal.GetReference(AlignmentByteMask_64x65)),
(uint)(count * 64));
}

if (Unsafe.SizeOf<T>() == 2)
if (sizeof(T) == 2)
{
return Vector256.LoadUnsafe(
ref Unsafe.As<ushort, T>(ref MemoryMarshal.GetReference(AlignmentUInt16Mask_32x33)),
(uint)(count * 32));
}

if (Unsafe.SizeOf<T>() == 4)
if (sizeof(T) == 4)
{
return Vector256.LoadUnsafe(
ref Unsafe.As<uint, T>(ref MemoryMarshal.GetReference(AlignmentUInt32Mask_16x17)),
(uint)(count * 16));
}

Debug.Assert(Unsafe.SizeOf<T>() == 8);
Debug.Assert(sizeof(T) == 8);
{
return Vector256.LoadUnsafe(
ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(AlignmentUInt64Mask_8x9)),
Expand All @@ -2348,28 +2348,28 @@ ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(AlignmentUInt64Mask_8x9))
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<T> CreateAlignmentMaskVector512<T>(int count)
{
if (Unsafe.SizeOf<T>() == 1)
if (sizeof(T) == 1)
{
return Vector512.LoadUnsafe(
ref Unsafe.As<byte, T>(ref MemoryMarshal.GetReference(AlignmentByteMask_64x65)),
(uint)(count * 64));
}

if (Unsafe.SizeOf<T>() == 2)
if (sizeof(T) == 2)
{
return Vector512.LoadUnsafe(
ref Unsafe.As<ushort, T>(ref MemoryMarshal.GetReference(AlignmentUInt16Mask_32x33)),
(uint)(count * 32));
}

if (Unsafe.SizeOf<T>() == 4)
if (sizeof(T) == 4)
{
return Vector512.LoadUnsafe(
ref Unsafe.As<uint, T>(ref MemoryMarshal.GetReference(AlignmentUInt32Mask_16x17)),
(uint)(count * 16));
}

Debug.Assert(Unsafe.SizeOf<T>() == 8);
Debug.Assert(sizeof(T) == 8);
{
return Vector512.LoadUnsafe(
ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(AlignmentUInt64Mask_8x9)),
Expand All @@ -2384,28 +2384,28 @@ ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(AlignmentUInt64Mask_8x9))
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<T> CreateRemainderMaskVector128<T>(int count)
{
if (Unsafe.SizeOf<T>() == 1)
if (sizeof(T) == 1)
{
return Vector128.LoadUnsafe(
ref Unsafe.As<byte, T>(ref MemoryMarshal.GetReference(RemainderByteMask_64x65)),
(uint)(count * 64) + 48); // last 16 bytes in the row
}

if (Unsafe.SizeOf<T>() == 2)
if (sizeof(T) == 2)
{
return Vector128.LoadUnsafe(
ref Unsafe.As<ushort, T>(ref MemoryMarshal.GetReference(RemainderUInt16Mask_32x33)),
(uint)(count * 32) + 24); // last 8 shorts in the row
}

if (Unsafe.SizeOf<T>() == 4)
if (sizeof(T) == 4)
{
return Vector128.LoadUnsafe(
ref Unsafe.As<uint, T>(ref MemoryMarshal.GetReference(RemainderUInt32Mask_16x17)),
(uint)(count * 16) + 12); // last 4 ints in the row
}

Debug.Assert(Unsafe.SizeOf<T>() == 8);
Debug.Assert(sizeof(T) == 8);
{
return Vector128.LoadUnsafe(
ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(RemainderUInt64Mask_8x9)),
Expand All @@ -2420,28 +2420,28 @@ ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(RemainderUInt64Mask_8x9))
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<T> CreateRemainderMaskVector256<T>(int count)
{
if (Unsafe.SizeOf<T>() == 1)
if (sizeof(T) == 1)
{
return Vector256.LoadUnsafe(
ref Unsafe.As<byte, T>(ref MemoryMarshal.GetReference(RemainderByteMask_64x65)),
(uint)(count * 64) + 32); // last 32 bytes in the row
}

if (Unsafe.SizeOf<T>() == 2)
if (sizeof(T) == 2)
{
return Vector256.LoadUnsafe(
ref Unsafe.As<ushort, T>(ref MemoryMarshal.GetReference(RemainderUInt16Mask_32x33)),
(uint)(count * 32) + 16); // last 16 shorts in the row
}

if (Unsafe.SizeOf<T>() == 4)
if (sizeof(T) == 4)
{
return Vector256.LoadUnsafe(
ref Unsafe.As<uint, T>(ref MemoryMarshal.GetReference(RemainderUInt32Mask_16x17)),
(uint)(count * 16) + 8); // last 8 ints in the row
}

Debug.Assert(Unsafe.SizeOf<T>() == 8);
Debug.Assert(sizeof(T) == 8);
{
return Vector256.LoadUnsafe(
ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(RemainderUInt64Mask_8x9)),
Expand All @@ -2456,28 +2456,28 @@ ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(RemainderUInt64Mask_8x9))
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<T> CreateRemainderMaskVector512<T>(int count)
{
if (Unsafe.SizeOf<T>() == 1)
if (sizeof(T) == 1)
{
return Vector512.LoadUnsafe(
ref Unsafe.As<byte, T>(ref MemoryMarshal.GetReference(RemainderByteMask_64x65)),
(uint)(count * 64));
}

if (Unsafe.SizeOf<T>() == 2)
if (sizeof(T) == 2)
{
return Vector512.LoadUnsafe(
ref Unsafe.As<ushort, T>(ref MemoryMarshal.GetReference(RemainderUInt16Mask_32x33)),
(uint)(count * 32));
}

if (Unsafe.SizeOf<T>() == 4)
if (sizeof(T) == 4)
{
return Vector512.LoadUnsafe(
ref Unsafe.As<uint, T>(ref MemoryMarshal.GetReference(RemainderUInt32Mask_16x17)),
(uint)(count * 16));
}

Debug.Assert(Unsafe.SizeOf<T>() == 8);
Debug.Assert(sizeof(T) == 8);
{
return Vector512.LoadUnsafe(
ref Unsafe.As<ulong, T>(ref MemoryMarshal.GetReference(RemainderUInt64Mask_8x9)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2738,27 +2738,27 @@ private static T HorizontalAggregate<T, TAggregate>(Vector128<T> x) where TAggre
{
// We need to do log2(count) operations to compute the total sum

if (Unsafe.SizeOf<T>() == 1)
if (sizeof(T) == 1)
{
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsByte(), Vector128.Create((byte)8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7)).As<byte, T>());
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsByte(), Vector128.Create((byte)4, 5, 6, 7, 0, 1, 2, 3, 8, 9, 10, 11, 12, 13, 14, 15)).As<byte, T>());
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsByte(), Vector128.Create((byte)2, 3, 0, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)).As<byte, T>());
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsByte(), Vector128.Create((byte)1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)).As<byte, T>());
}
else if (Unsafe.SizeOf<T>() == 2)
else if (sizeof(T) == 2)
{
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsInt16(), Vector128.Create(4, 5, 6, 7, 0, 1, 2, 3)).As<short, T>());
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsInt16(), Vector128.Create(2, 3, 0, 1, 4, 5, 6, 7)).As<short, T>());
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsInt16(), Vector128.Create(1, 0, 2, 3, 4, 5, 6, 7)).As<short, T>());
}
else if (Unsafe.SizeOf<T>() == 4)
else if (sizeof(T) == 4)
{
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsInt32(), Vector128.Create(2, 3, 0, 1)).As<int, T>());
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsInt32(), Vector128.Create(1, 0, 3, 2)).As<int, T>());
}
else
{
Debug.Assert(Unsafe.SizeOf<T>() == 8);
Debug.Assert(sizeof(T) == 8);
x = TAggregate.Invoke(x, Vector128.Shuffle(x.AsInt64(), Vector128.Create(1, 0)).As<long, T>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private static void InvokeSpanIntoSpan<TInput, TOutput, TUnaryOperator>(

nuint remainder = (uint)x.Length;

if (Vector512.IsHardwareAccelerated && Vector512<TInput>.IsSupported && Vector512<TOutput>.IsSupported && TUnaryOperator.Vectorizable && Unsafe.SizeOf<TInput>() == Unsafe.SizeOf<TOutput>())
if (Vector512.IsHardwareAccelerated && Vector512<TInput>.IsSupported && Vector512<TOutput>.IsSupported && TUnaryOperator.Vectorizable && sizeof(TInput) == sizeof(TOutput))
{
if (remainder >= (uint)Vector512<TInput>.Count)
{
Expand All @@ -106,7 +106,7 @@ private static void InvokeSpanIntoSpan<TInput, TOutput, TUnaryOperator>(
return;
}

if (Vector256.IsHardwareAccelerated && Vector256<TInput>.IsSupported && Vector256<TOutput>.IsSupported && TUnaryOperator.Vectorizable && Unsafe.SizeOf<TInput>() == Unsafe.SizeOf<TOutput>())
if (Vector256.IsHardwareAccelerated && Vector256<TInput>.IsSupported && Vector256<TOutput>.IsSupported && TUnaryOperator.Vectorizable && sizeof(TInput) == sizeof(TOutput))
{
if (remainder >= (uint)Vector256<TInput>.Count)
{
Expand All @@ -124,7 +124,7 @@ private static void InvokeSpanIntoSpan<TInput, TOutput, TUnaryOperator>(
return;
}

if (Vector128.IsHardwareAccelerated && Vector128<TInput>.IsSupported && Vector128<TOutput>.IsSupported && TUnaryOperator.Vectorizable && Unsafe.SizeOf<TInput>() == Unsafe.SizeOf<TOutput>())
if (Vector128.IsHardwareAccelerated && Vector128<TInput>.IsSupported && Vector128<TOutput>.IsSupported && TUnaryOperator.Vectorizable && sizeof(TInput) == sizeof(TOutput))
{
if (remainder >= (uint)Vector128<TInput>.Count)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,31 @@ private void WriteTextNodeWithLength(XmlBinaryNodeType nodeType, int length)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void WriteTextNodeRaw<T>(XmlBinaryNodeType nodeType, T value)
private unsafe void WriteTextNodeRaw<T>(XmlBinaryNodeType nodeType, T value)
where T : unmanaged
{
// GetTextNodeBuffer performs bounds checks and ensures returned buffer has size of at least (1 + Unsafe.SizeOf<T>())
byte[] buffer = GetTextNodeBuffer(1 + Unsafe.SizeOf<T>(), out int offset);
// GetTextNodeBuffer performs bounds checks and ensures returned buffer has size of at least (1 + sizeof(T))
byte[] buffer = GetTextNodeBuffer(1 + sizeof(T), out int offset);

Debug.Assert(offset >= 0 && offset + 1 + Unsafe.SizeOf<T>() <= buffer.Length, "WriteTextNodeRaw");
Debug.Assert(offset >= 0 && offset + 1 + sizeof(T) <= buffer.Length, "WriteTextNodeRaw");
ref byte bytePtr = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(buffer), offset);

bytePtr = (byte)nodeType;
Unsafe.WriteUnaligned<T>(ref Unsafe.Add(ref bytePtr, 1), value);
Advance(1 + Unsafe.SizeOf<T>());
Advance(1 + sizeof(T));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void WriteRaw<T>(T value)
private unsafe void WriteRaw<T>(T value)
where T : unmanaged
{
// GetBuffer performs bounds checks and ensures returned buffer has size of at least (Unsafe.SizeOf<T>())
byte[] buffer = GetBuffer(Unsafe.SizeOf<T>(), out int offset);
// GetBuffer performs bounds checks and ensures returned buffer has size of at least (sizeof(T))
byte[] buffer = GetBuffer(sizeof(T), out int offset);

Debug.Assert(offset >= 0 && offset + Unsafe.SizeOf<T>() <= buffer.Length, "WriteRaw");
Debug.Assert(offset >= 0 && offset + sizeof(T) <= buffer.Length, "WriteRaw");
ref byte bytePtr = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(buffer), offset);
Unsafe.WriteUnaligned<T>(ref bytePtr, value);
Advance(Unsafe.SizeOf<T>());
Advance(sizeof(T));
}

private void WriteTextNodeWithInt8(XmlBinaryNodeType nodeType, byte value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ private unsafe static void AllocateArrayPinned_ManagedValueType_CanRoundtripThro

EmbeddedValueType<string>[] array = uninitialized ? GC.AllocateUninitializedArray<EmbeddedValueType<string>>(length, pinned: true) : GC.AllocateArray<EmbeddedValueType<string>>(length, pinned: true);
byte* pointer = (byte*)Unsafe.AsPointer(ref array[0]); // Unsafe.AsPointer is safe since array is pinned
var size = Unsafe.SizeOf<EmbeddedValueType<string>>();
int size = sizeof(EmbeddedValueType<string>);

GC.Collect();

Expand Down
4 changes: 2 additions & 2 deletions src/mono/browser/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public override string ReadString()

private unsafe T ReadBigEndian<T>() where T : struct
{
Span<byte> data = stackalloc byte[Unsafe.SizeOf<T>()];
Span<byte> data = stackalloc byte[sizeof(T)];
T ret = default;
Read(data);
if (BitConverter.IsLittleEndian)
Expand All @@ -545,7 +545,7 @@ public override void Write(string val)

private unsafe void WriteBigEndian<T>(T val) where T : struct
{
Span<byte> data = stackalloc byte[Unsafe.SizeOf<T>()];
Span<byte> data = stackalloc byte[sizeof(T)];
new Span<byte>(&val, data.Length).CopyTo(data);
if (BitConverter.IsLittleEndian)
{
Expand Down

0 comments on commit 5f53468

Please sign in to comment.