@@ -112,7 +112,10 @@ 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
- if ( * ( byte * ) & value == * ( byte * ) & target )
115
+ byte valueByte = Unsafe . As < T , byte > ( ref value ) ;
116
+ byte targetByte = Unsafe . As < T , byte > ( ref target ) ;
117
+
118
+ if ( valueByte == targetByte )
116
119
{
117
120
return ;
118
121
}
@@ -121,7 +124,10 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
121
124
}
122
125
else if ( sizeof ( T ) == 2 )
123
126
{
124
- if ( * ( ushort * ) & value == * ( ushort * ) & target )
127
+ ushort valueUShort = Unsafe . As < T , ushort > ( ref value ) ;
128
+ ushort targetUShort = Unsafe . As < T , ushort > ( ref target ) ;
129
+
130
+ if ( valueUShort == targetUShort )
125
131
{
126
132
return ;
127
133
}
@@ -130,7 +136,10 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
130
136
}
131
137
else if ( sizeof ( T ) == 4 )
132
138
{
133
- if ( * ( uint * ) & value == * ( uint * ) & target )
139
+ uint valueUInt = Unsafe . As < T , uint > ( ref value ) ;
140
+ uint targetUInt = Unsafe . As < T , uint > ( ref target ) ;
141
+
142
+ if ( valueUInt == targetUInt )
134
143
{
135
144
return ;
136
145
}
@@ -139,7 +148,10 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
139
148
}
140
149
else if ( sizeof ( T ) == 8 )
141
150
{
142
- if ( Bit64Compare ( * ( ulong * ) & value , * ( ulong * ) & target ) )
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 ) )
143
155
{
144
156
return ;
145
157
}
@@ -148,20 +160,26 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
148
160
}
149
161
else if ( sizeof ( T ) == 16 )
150
162
{
151
- ulong * p0 = ( ulong * ) & value ;
152
- ulong * p1 = ( ulong * ) & target ;
163
+ ulong valueULong0 = Unsafe . As < T , ulong > ( ref value ) ;
164
+ ulong targetULong0 = Unsafe . As < T , ulong > ( ref target ) ;
153
165
154
- if ( Bit64Compare ( p0 [ 0 ] , p1 [ 0 ] ) && Bit64Compare ( p0 [ 1 ] , p1 [ 1 ] ) )
166
+ if ( Bit64Compare ( ref valueULong0 , ref targetULong0 ) )
155
167
{
156
- return ;
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
+ }
157
175
}
158
176
159
177
ThrowHelper . ThrowArgumentExceptionForBitwiseEqualTo ( value , target , name ) ;
160
178
}
161
179
else
162
180
{
163
- Span< byte > valueBytes = new Span < byte > ( & value , sizeof ( T ) ) ;
164
- Span < byte > targetBytes = new Span < byte > ( & target , sizeof ( T ) ) ;
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 ) ) ;
165
183
166
184
if ( valueBytes . SequenceEqual ( targetBytes ) )
167
185
{
@@ -175,15 +193,16 @@ public static unsafe void IsBitwiseEqualTo<T>(T value, T target, string name)
175
193
// Compares 64 bits of data from two given memory locations for bitwise equality
176
194
[ Pure ]
177
195
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
178
- private static unsafe bool Bit64Compare( ulong left , ulong right )
196
+ private static unsafe bool Bit64Compare ( ref ulong left , ref ulong right )
179
197
{
180
198
// Handles 32 bit case, because using ulong is inefficient
181
- if ( sizeof ( nint ) == 4 )
199
+ if ( sizeof ( IntPtr ) == 4 )
182
200
{
183
- uint * p0 = ( uint * ) & left ;
184
- uint * p1 = ( uint * ) & right ;
201
+ ref int r0 = ref Unsafe . As < ulong , int > ( ref left ) ;
202
+ ref int r1 = ref Unsafe . As < ulong , int > ( ref right ) ;
185
203
186
- return p0[ 0 ] == p1 [ 0 ] && p0 [ 1 ] == p1 [ 1 ] ;
204
+ return r0 == r1 &&
205
+ Unsafe . Add ( ref r0 , 1 ) == Unsafe . Add ( ref r1 , 1 ) ;
187
206
}
188
207
189
208
return left == right ;
0 commit comments