@@ -112,10 +112,7 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
112
112
// so that only the right one will actually be translated into native code.
113
113
if ( sizeof ( T ) == 1 )
114
114
{
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 )
119
116
{
120
117
return ;
121
118
}
@@ -124,10 +121,7 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
124
121
}
125
122
else if ( sizeof ( T ) == 2 )
126
123
{
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 )
131
125
{
132
126
return ;
133
127
}
@@ -136,10 +130,7 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
136
130
}
137
131
else if ( sizeof ( T ) == 4 )
138
132
{
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 )
143
134
{
144
135
return ;
145
136
}
@@ -148,10 +139,7 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
148
139
}
149
140
else if ( sizeof ( T ) == 8 )
150
141
{
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 ) )
155
143
{
156
144
return ;
157
145
}
@@ -160,26 +148,20 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
160
148
}
161
149
else if ( sizeof ( T ) == 16 )
162
150
{
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 ;
165
153
166
- if ( Bit64Compare ( ref valueULong0 , ref targetULong0 ) )
154
+ if ( Bit64Compare ( p0 [ 0 ] , p1 [ 0 ] ) && Bit64Compare ( p0 [ 1 ] , p1 [ 1 ] ) )
167
155
{
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 ;
175
157
}
176
158
177
159
ThrowHelper . ThrowArgumentExceptionForBitwiseEqualTo ( value , target , name ) ;
178
160
}
179
161
else
180
162
{
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 ) ) ;
183
165
184
166
if ( valueBytes . SequenceEqual ( targetBytes ) )
185
167
{
@@ -193,16 +175,15 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
193
175
// Compares 64 bits of data from two given memory locations for bitwise equality
194
176
[ Pure ]
195
177
[ 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 )
197
179
{
198
180
// Handles 32 bit case, because using ulong is inefficient
199
- if ( sizeof ( IntPtr ) == 4 )
181
+ if ( sizeof ( nint ) == 4 )
200
182
{
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 ;
203
185
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 ] ;
206
187
}
207
188
208
189
return left == right ;
0 commit comments