Skip to content

Commit 6ace41b

Browse files
committed
Minor code tweaks
1 parent 1ebc426 commit 6ace41b

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed

Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
112112
// so that only the right one will actually be translated into native code.
113113
if (sizeof(T) == 1)
114114
{
115-
byte valueByte = Unsafe.As<T, byte>(ref value);
116-
byte targetByte = Unsafe.As<T, byte>(ref target);
117-
118-
if (valueByte == targetByte)
115+
if (*(byte*)&value == *(byte*)&target)
119116
{
120117
return;
121118
}
@@ -124,10 +121,7 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
124121
}
125122
else if (sizeof(T) == 2)
126123
{
127-
ushort valueUShort = Unsafe.As<T, ushort>(ref value);
128-
ushort targetUShort = Unsafe.As<T, ushort>(ref target);
129-
130-
if (valueUShort == targetUShort)
124+
if (*(ushort*)&value == *(ushort*)&target)
131125
{
132126
return;
133127
}
@@ -136,10 +130,7 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
136130
}
137131
else if (sizeof(T) == 4)
138132
{
139-
uint valueUInt = Unsafe.As<T, uint>(ref value);
140-
uint targetUInt = Unsafe.As<T, uint>(ref target);
141-
142-
if (valueUInt == targetUInt)
133+
if (*(uint*)&value == *(uint*)&target)
143134
{
144135
return;
145136
}
@@ -148,10 +139,7 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
148139
}
149140
else if (sizeof(T) == 8)
150141
{
151-
ulong valueULong = Unsafe.As<T, ulong>(ref value);
152-
ulong targetULong = Unsafe.As<T, ulong>(ref target);
153-
154-
if (Bit64Compare(ref valueULong, ref targetULong))
142+
if (Bit64Compare(*(ulong*)&value, *(ulong*)&target))
155143
{
156144
return;
157145
}
@@ -160,26 +148,20 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
160148
}
161149
else if (sizeof(T) == 16)
162150
{
163-
ulong valueULong0 = Unsafe.As<T, ulong>(ref value);
164-
ulong targetULong0 = Unsafe.As<T, ulong>(ref target);
151+
ulong* p0 = (ulong*)&value;
152+
ulong* p1 = (ulong*)&target;
165153

166-
if (Bit64Compare(ref valueULong0, ref targetULong0))
154+
if (Bit64Compare(p0[0], p1[0]) && Bit64Compare(p0[1], p1[1]))
167155
{
168-
ulong valueULong1 = Unsafe.Add(ref Unsafe.As<T, ulong>(ref value), 1);
169-
ulong targetULong1 = Unsafe.Add(ref Unsafe.As<T, ulong>(ref target), 1);
170-
171-
if (Bit64Compare(ref valueULong1, ref targetULong1))
172-
{
173-
return;
174-
}
156+
return;
175157
}
176158

177159
ThrowHelper.ThrowArgumentExceptionForBitwiseEqualTo(value, target, name);
178160
}
179161
else
180162
{
181-
Span<byte> valueBytes = new Span<byte>(Unsafe.AsPointer(ref value), sizeof(T));
182-
Span<byte> targetBytes = new Span<byte>(Unsafe.AsPointer(ref target), sizeof(T));
163+
Span<byte> valueBytes = new Span<byte>(&value, sizeof(T));
164+
Span<byte> targetBytes = new Span<byte>(&target, sizeof(T));
183165

184166
if (valueBytes.SequenceEqual(targetBytes))
185167
{
@@ -193,16 +175,15 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
193175
// Compares 64 bits of data from two given memory locations for bitwise equality
194176
[Pure]
195177
[MethodImpl(MethodImplOptions.AggressiveInlining)]
196-
private static unsafe bool Bit64Compare(ref ulong left, ref ulong right)
178+
private static unsafe bool Bit64Compare(ulong left, ulong right)
197179
{
198180
// Handles 32 bit case, because using ulong is inefficient
199-
if (sizeof(IntPtr) == 4)
181+
if (sizeof(nint) == 4)
200182
{
201-
ref int r0 = ref Unsafe.As<ulong, int>(ref left);
202-
ref int r1 = ref Unsafe.As<ulong, int>(ref right);
183+
uint* p0 = (uint*)&left;
184+
uint* p1 = (uint*)&right;
203185

204-
return r0 == r1 &&
205-
Unsafe.Add(ref r0, 1) == Unsafe.Add(ref r1, 1);
186+
return p0[0] == p1[0] && p0[1] == p1[1];
206187
}
207188

208189
return left == right;

0 commit comments

Comments
 (0)