Skip to content

Commit 6b5bdc6

Browse files
committed
refactor: GetRef
1 parent e34bca8 commit 6b5bdc6

File tree

11 files changed

+225
-179
lines changed

11 files changed

+225
-179
lines changed

src/CryptoBase/Digests/CRC32/Crc32X86.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ public void Reset()
6262
private static uint Update(ReadOnlySpan<byte> buffer, uint crc)
6363
{
6464
int length = buffer.Length;
65+
ref byte ptr = ref buffer.GetReference();
6566

66-
ref Vector128<ulong> x1 = ref Unsafe.As<byte, Vector128<ulong>>(ref MemoryMarshal.GetReference(buffer));
67-
ref Vector128<ulong> x2 = ref Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(0x10));
68-
ref Vector128<ulong> x3 = ref Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(0x20));
69-
ref Vector128<ulong> x4 = ref Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(0x30));
67+
ref Vector128<ulong> x1 = ref Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, 0 * 0x10));
68+
ref Vector128<ulong> x2 = ref Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, 1 * 0x10));
69+
ref Vector128<ulong> x3 = ref Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, 2 * 0x10));
70+
ref Vector128<ulong> x4 = ref Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, 3 * 0x10));
7071
Vector128<ulong> vCrc = Vector128.CreateScalar(crc).AsUInt64();
7172
x1 = Sse2.Xor(x1, vCrc);
7273

@@ -90,13 +91,13 @@ private static uint Update(ReadOnlySpan<byte> buffer, uint crc)
9091
x3 = Sse2.Xor(x3, t3);
9192
x4 = Sse2.Xor(x4, t4);
9293

93-
x1 = Sse2.Xor(x1, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
94+
x1 = Sse2.Xor(x1, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
9495
offset += 0x10;
95-
x2 = Sse2.Xor(x2, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
96+
x2 = Sse2.Xor(x2, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
9697
offset += 0x10;
97-
x3 = Sse2.Xor(x3, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
98+
x3 = Sse2.Xor(x3, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
9899
offset += 0x10;
99-
x4 = Sse2.Xor(x4, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
100+
x4 = Sse2.Xor(x4, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
100101
offset += 0x10;
101102

102103
length -= 0x40;
@@ -122,7 +123,7 @@ private static uint Update(ReadOnlySpan<byte> buffer, uint crc)
122123
t = Pclmulqdq.CarrylessMultiply(x1, K3K4, 0x11);
123124
x1 = Pclmulqdq.CarrylessMultiply(x1, K3K4, 0x00);
124125
x1 = Sse2.Xor(x1, t);
125-
x1 = Sse2.Xor(x1, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
126+
x1 = Sse2.Xor(x1, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
126127

127128
length -= 0x10;
128129
offset += 0x10;

src/CryptoBase/Digests/CRC32C/Crc32CX86.cs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ public void Reset()
6969
private static uint Update(ReadOnlySpan<byte> buffer, uint crc)
7070
{
7171
int length = buffer.Length;
72+
ref byte ptr = ref buffer.GetReference();
7273

73-
ref Vector128<ulong> x1 = ref Unsafe.As<byte, Vector128<ulong>>(ref MemoryMarshal.GetReference(buffer));
74-
ref Vector128<ulong> x2 = ref Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(0x10));
75-
ref Vector128<ulong> x3 = ref Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(0x20));
76-
ref Vector128<ulong> x4 = ref Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(0x30));
74+
ref Vector128<ulong> x1 = ref Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, 0 * 0x10));
75+
ref Vector128<ulong> x2 = ref Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, 1 * 0x10));
76+
ref Vector128<ulong> x3 = ref Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, 2 * 0x10));
77+
ref Vector128<ulong> x4 = ref Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, 3 * 0x10));
7778
Vector128<ulong> vCrc = Vector128.CreateScalar(crc).AsUInt64();
7879
x1 = Sse2.Xor(x1, vCrc);
7980

@@ -97,13 +98,13 @@ private static uint Update(ReadOnlySpan<byte> buffer, uint crc)
9798
x3 = Sse2.Xor(x3, t3);
9899
x4 = Sse2.Xor(x4, t4);
99100

100-
x1 = Sse2.Xor(x1, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
101+
x1 = Sse2.Xor(x1, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
101102
offset += 0x10;
102-
x2 = Sse2.Xor(x2, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
103+
x2 = Sse2.Xor(x2, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
103104
offset += 0x10;
104-
x3 = Sse2.Xor(x3, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
105+
x3 = Sse2.Xor(x3, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
105106
offset += 0x10;
106-
x4 = Sse2.Xor(x4, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
107+
x4 = Sse2.Xor(x4, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
107108
offset += 0x10;
108109

109110
length -= 0x40;
@@ -129,7 +130,7 @@ private static uint Update(ReadOnlySpan<byte> buffer, uint crc)
129130
t = Pclmulqdq.CarrylessMultiply(x1, K3K4, 0x11);
130131
x1 = Pclmulqdq.CarrylessMultiply(x1, K3K4, 0x00);
131132
x1 = Sse2.Xor(x1, t);
132-
x1 = Sse2.Xor(x1, Unsafe.As<byte, Vector128<ulong>>(ref buffer.GetRef(offset)));
133+
x1 = Sse2.Xor(x1, Unsafe.As<byte, Vector128<ulong>>(ref Unsafe.Add(ref ptr, offset)));
133134

134135
length -= 0x10;
135136
offset += 0x10;
@@ -156,42 +157,51 @@ private static uint Update(ReadOnlySpan<byte> buffer, uint crc)
156157
[MethodImpl(MethodImplOptions.AggressiveInlining)]
157158
private void UpdateSse42(ReadOnlySpan<byte> source)
158159
{
160+
int length = source.Length;
161+
int offset = 0;
162+
ref byte sourceRef = ref source.GetReference();
163+
ref uint state = ref _state;
164+
159165
if (Sse42.X64.IsSupported)
160166
{
161-
while (source.Length >= 8)
167+
while (length >= 8)
162168
{
163-
ref ulong data = ref Unsafe.As<byte, ulong>(ref MemoryMarshal.GetReference(source));
164-
_state = (uint)Sse42.X64.Crc32(_state, data);
165-
source = source.Slice(8);
169+
ref ulong data = ref Unsafe.As<byte, ulong>(ref Unsafe.Add(ref sourceRef, offset));
170+
state = (uint)Sse42.X64.Crc32(state, data);
171+
offset += 8;
172+
length -= 8;
166173
}
167174

168-
if (source.Length >= 4)
175+
if (length >= 4)
169176
{
170-
ref uint data = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
171-
_state = Sse42.Crc32(_state, data);
172-
source = source.Slice(4);
177+
ref uint data = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref sourceRef, offset));
178+
state = Sse42.Crc32(state, data);
179+
offset += 4;
180+
length -= 4;
173181
}
174182
}
175183
else
176184
{
177-
while (source.Length >= 4)
185+
while (length >= 4)
178186
{
179-
ref uint data = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
180-
_state = Sse42.Crc32(_state, data);
181-
source = source.Slice(4);
187+
ref uint data = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref sourceRef, offset));
188+
state = Sse42.Crc32(state, data);
189+
offset += 4;
190+
length -= 4;
182191
}
183192
}
184193

185-
if (source.Length >= 2)
194+
if (length >= 2)
186195
{
187-
ref ushort data = ref Unsafe.As<byte, ushort>(ref MemoryMarshal.GetReference(source));
188-
_state = Sse42.Crc32(_state, data);
189-
source = source.Slice(2);
196+
ref ushort data = ref Unsafe.As<byte, ushort>(ref Unsafe.Add(ref sourceRef, offset));
197+
state = Sse42.Crc32(state, data);
198+
offset += 2;
199+
length -= 2;
190200
}
191201

192-
foreach (ref readonly byte b in source)
202+
if (length > 0)
193203
{
194-
_state = Sse42.Crc32(_state, b);
204+
state = Sse42.Crc32(state, Unsafe.Add(ref sourceRef, offset));
195205
}
196206
}
197207

src/CryptoBase/FastUtils.cs

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,25 @@ namespace CryptoBase;
22

33
public static class FastUtils
44
{
5-
/// <summary>
6-
/// Get span ref without bounds checking
7-
/// </summary>
5+
/// <inheritdoc cref="MemoryMarshal.GetReference{T}(Span{T})" />
86
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9-
public static ref T GetRef<T>(this Span<T> span, int index)
7+
public static ref T GetReference<T>(this Span<T> span)
108
{
11-
return ref Unsafe.Add(ref MemoryMarshal.GetReference(span), index);
9+
return ref MemoryMarshal.GetReference(span);
1210
}
1311

14-
/// <summary>
15-
/// Get span ref without bounds checking
16-
/// </summary>
12+
/// <inheritdoc cref="MemoryMarshal.GetReference{T}(ReadOnlySpan{T})" />
1713
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18-
public static ref T GetRef<T>(this ReadOnlySpan<T> span, int index)
14+
public static ref T GetReference<T>(this ReadOnlySpan<T> span)
1915
{
20-
return ref Unsafe.Add(ref MemoryMarshal.GetReference(span), index);
16+
return ref MemoryMarshal.GetReference(span);
2117
}
2218

23-
/// <summary>
24-
/// Get array ref without bounds checking
25-
/// </summary>
19+
/// <inheritdoc cref="MemoryMarshal.GetArrayDataReference{T}" />
2620
[MethodImpl(MethodImplOptions.AggressiveInlining)]
27-
public static ref T GetRef<T>(this T[] array, int index)
21+
public static ref T GetReference<T>(this T[] array)
2822
{
29-
ref T data = ref MemoryMarshal.GetArrayDataReference(array);
30-
return ref Unsafe.Add(ref data, index);
23+
return ref MemoryMarshal.GetArrayDataReference(array);
3124
}
3225

3326
/// <inheritdoc cref="Vector256.Create{T}(Vector128{T})" />
@@ -55,13 +48,17 @@ public static void Xor(ReadOnlySpan<byte> stream, ReadOnlySpan<byte> source, Spa
5548
int i = 0;
5649
int left = length;
5750

51+
ref byte streamRef = ref stream.GetReference();
52+
ref byte sourceRef = ref source.GetReference();
53+
ref byte destinationRef = ref destination.GetReference();
54+
5855
if (Vector512.IsHardwareAccelerated)
5956
{
6057
while (left >= Vector512<byte>.Count)
6158
{
62-
ref Vector512<byte> v0 = ref Unsafe.As<byte, Vector512<byte>>(ref stream.GetRef(i));
63-
ref Vector512<byte> v1 = ref Unsafe.As<byte, Vector512<byte>>(ref source.GetRef(i));
64-
ref Vector512<byte> dst = ref Unsafe.As<byte, Vector512<byte>>(ref destination.GetRef(i));
59+
ref Vector512<byte> v0 = ref Unsafe.As<byte, Vector512<byte>>(ref Unsafe.Add(ref streamRef, i));
60+
ref Vector512<byte> v1 = ref Unsafe.As<byte, Vector512<byte>>(ref Unsafe.Add(ref sourceRef, i));
61+
ref Vector512<byte> dst = ref Unsafe.As<byte, Vector512<byte>>(ref Unsafe.Add(ref destinationRef, i));
6562

6663
dst = v0 ^ v1;
6764
i += Vector512<byte>.Count;
@@ -73,9 +70,9 @@ public static void Xor(ReadOnlySpan<byte> stream, ReadOnlySpan<byte> source, Spa
7370
{
7471
while (left >= Vector256<byte>.Count)
7572
{
76-
ref Vector256<byte> v0 = ref Unsafe.As<byte, Vector256<byte>>(ref stream.GetRef(i));
77-
ref Vector256<byte> v1 = ref Unsafe.As<byte, Vector256<byte>>(ref source.GetRef(i));
78-
ref Vector256<byte> dst = ref Unsafe.As<byte, Vector256<byte>>(ref destination.GetRef(i));
73+
ref Vector256<byte> v0 = ref Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref streamRef, i));
74+
ref Vector256<byte> v1 = ref Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref sourceRef, i));
75+
ref Vector256<byte> dst = ref Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref destinationRef, i));
7976

8077
dst = v0 ^ v1;
8178
i += Vector256<byte>.Count;
@@ -87,9 +84,9 @@ public static void Xor(ReadOnlySpan<byte> stream, ReadOnlySpan<byte> source, Spa
8784
{
8885
while (left >= Vector128<byte>.Count)
8986
{
90-
ref Vector128<byte> v0 = ref Unsafe.As<byte, Vector128<byte>>(ref stream.GetRef(i));
91-
ref Vector128<byte> v1 = ref Unsafe.As<byte, Vector128<byte>>(ref source.GetRef(i));
92-
ref Vector128<byte> dst = ref Unsafe.As<byte, Vector128<byte>>(ref destination.GetRef(i));
87+
ref Vector128<byte> v0 = ref Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref streamRef, i));
88+
ref Vector128<byte> v1 = ref Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref sourceRef, i));
89+
ref Vector128<byte> dst = ref Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref destinationRef, i));
9390

9491
dst = v0 ^ v1;
9592
i += Vector128<byte>.Count;
@@ -99,9 +96,9 @@ public static void Xor(ReadOnlySpan<byte> stream, ReadOnlySpan<byte> source, Spa
9996

10097
while (left >= sizeof(ulong))
10198
{
102-
ref ulong v0 = ref Unsafe.As<byte, ulong>(ref stream.GetRef(i));
103-
ref ulong v1 = ref Unsafe.As<byte, ulong>(ref source.GetRef(i));
104-
ref ulong dst = ref Unsafe.As<byte, ulong>(ref destination.GetRef(i));
99+
ref ulong v0 = ref Unsafe.As<byte, ulong>(ref Unsafe.Add(ref streamRef, i));
100+
ref ulong v1 = ref Unsafe.As<byte, ulong>(ref Unsafe.Add(ref sourceRef, i));
101+
ref ulong dst = ref Unsafe.As<byte, ulong>(ref Unsafe.Add(ref destinationRef, i));
105102

106103
dst = v0 ^ v1;
107104
i += sizeof(ulong);
@@ -110,17 +107,17 @@ public static void Xor(ReadOnlySpan<byte> stream, ReadOnlySpan<byte> source, Spa
110107

111108
if (left >= sizeof(uint))
112109
{
113-
ref uint v0 = ref Unsafe.As<byte, uint>(ref stream.GetRef(i));
114-
ref uint v1 = ref Unsafe.As<byte, uint>(ref source.GetRef(i));
115-
ref uint dst = ref Unsafe.As<byte, uint>(ref destination.GetRef(i));
110+
ref uint v0 = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref streamRef, i));
111+
ref uint v1 = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref sourceRef, i));
112+
ref uint dst = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref destinationRef, i));
116113

117114
dst = v0 ^ v1;
118115
i += sizeof(uint);
119116
}
120117

121118
for (; i < length; ++i)
122119
{
123-
destination.GetRef(i) = (byte)(source.GetRef(i) ^ stream.GetRef(i));
120+
Unsafe.Add(ref destinationRef, i) = (byte)(Unsafe.Add(ref sourceRef, i) ^ Unsafe.Add(ref streamRef, i));
124121
}
125122
}
126123

@@ -133,12 +130,15 @@ public static void Xor(Span<byte> source, ReadOnlySpan<byte> stream, int length)
133130
int i = 0;
134131
int left = length;
135132

133+
ref byte streamRef = ref stream.GetReference();
134+
ref byte sourceRef = ref source.GetReference();
135+
136136
if (Vector512.IsHardwareAccelerated)
137137
{
138138
while (left >= Vector512<byte>.Count)
139139
{
140-
ref Vector512<byte> v0 = ref Unsafe.As<byte, Vector512<byte>>(ref source.GetRef(i));
141-
ref Vector512<byte> v1 = ref Unsafe.As<byte, Vector512<byte>>(ref stream.GetRef(i));
140+
ref Vector512<byte> v0 = ref Unsafe.As<byte, Vector512<byte>>(ref Unsafe.Add(ref sourceRef, i));
141+
ref Vector512<byte> v1 = ref Unsafe.As<byte, Vector512<byte>>(ref Unsafe.Add(ref streamRef, i));
142142

143143
v0 ^= v1;
144144
i += Vector512<byte>.Count;
@@ -150,8 +150,8 @@ public static void Xor(Span<byte> source, ReadOnlySpan<byte> stream, int length)
150150
{
151151
while (left >= Vector256<byte>.Count)
152152
{
153-
ref Vector256<byte> v0 = ref Unsafe.As<byte, Vector256<byte>>(ref source.GetRef(i));
154-
ref Vector256<byte> v1 = ref Unsafe.As<byte, Vector256<byte>>(ref stream.GetRef(i));
153+
ref Vector256<byte> v0 = ref Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref sourceRef, i));
154+
ref Vector256<byte> v1 = ref Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref streamRef, i));
155155

156156
v0 ^= v1;
157157
i += Vector256<byte>.Count;
@@ -163,8 +163,8 @@ public static void Xor(Span<byte> source, ReadOnlySpan<byte> stream, int length)
163163
{
164164
while (left >= Vector128<byte>.Count)
165165
{
166-
ref Vector128<byte> v0 = ref Unsafe.As<byte, Vector128<byte>>(ref source.GetRef(i));
167-
ref Vector128<byte> v1 = ref Unsafe.As<byte, Vector128<byte>>(ref stream.GetRef(i));
166+
ref Vector128<byte> v0 = ref Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref sourceRef, i));
167+
ref Vector128<byte> v1 = ref Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref streamRef, i));
168168

169169
v0 ^= v1;
170170
i += Vector128<byte>.Count;
@@ -174,8 +174,8 @@ public static void Xor(Span<byte> source, ReadOnlySpan<byte> stream, int length)
174174

175175
while (left >= sizeof(ulong))
176176
{
177-
ref ulong v0 = ref Unsafe.As<byte, ulong>(ref source.GetRef(i));
178-
ref ulong v1 = ref Unsafe.As<byte, ulong>(ref stream.GetRef(i));
177+
ref ulong v0 = ref Unsafe.As<byte, ulong>(ref Unsafe.Add(ref sourceRef, i));
178+
ref ulong v1 = ref Unsafe.As<byte, ulong>(ref Unsafe.Add(ref streamRef, i));
179179

180180
v0 ^= v1;
181181
i += sizeof(ulong);
@@ -184,16 +184,16 @@ public static void Xor(Span<byte> source, ReadOnlySpan<byte> stream, int length)
184184

185185
if (left >= sizeof(uint))
186186
{
187-
ref uint v0 = ref Unsafe.As<byte, uint>(ref source.GetRef(i));
188-
ref uint v1 = ref Unsafe.As<byte, uint>(ref stream.GetRef(i));
187+
ref uint v0 = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref sourceRef, i));
188+
ref uint v1 = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref streamRef, i));
189189

190190
v0 ^= v1;
191191
i += sizeof(uint);
192192
}
193193

194194
for (; i < length; ++i)
195195
{
196-
source.GetRef(i) ^= stream.GetRef(i);
196+
Unsafe.Add(ref sourceRef, i) ^= Unsafe.Add(ref streamRef, i);
197197
}
198198
}
199199

@@ -252,11 +252,15 @@ public static void XorLess16(ReadOnlySpan<byte> stream, ReadOnlySpan<byte> sourc
252252
int i = 0;
253253
int left = length;
254254

255+
ref byte streamRef = ref stream.GetReference();
256+
ref byte sourceRef = ref source.GetReference();
257+
ref byte destinationRef = ref destination.GetReference();
258+
255259
if (left >= sizeof(ulong))
256260
{
257-
ref ulong v0 = ref Unsafe.As<byte, ulong>(ref stream.GetRef(i));
258-
ref ulong v1 = ref Unsafe.As<byte, ulong>(ref source.GetRef(i));
259-
ref ulong dst = ref Unsafe.As<byte, ulong>(ref destination.GetRef(i));
261+
ref ulong v0 = ref Unsafe.As<byte, ulong>(ref Unsafe.Add(ref streamRef, i));
262+
ref ulong v1 = ref Unsafe.As<byte, ulong>(ref Unsafe.Add(ref sourceRef, i));
263+
ref ulong dst = ref Unsafe.As<byte, ulong>(ref Unsafe.Add(ref destinationRef, i));
260264

261265
dst = v0 ^ v1;
262266
i += sizeof(ulong);
@@ -265,17 +269,17 @@ public static void XorLess16(ReadOnlySpan<byte> stream, ReadOnlySpan<byte> sourc
265269

266270
if (left >= sizeof(uint))
267271
{
268-
ref uint v0 = ref Unsafe.As<byte, uint>(ref stream.GetRef(i));
269-
ref uint v1 = ref Unsafe.As<byte, uint>(ref source.GetRef(i));
270-
ref uint dst = ref Unsafe.As<byte, uint>(ref destination.GetRef(i));
272+
ref uint v0 = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref streamRef, i));
273+
ref uint v1 = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref sourceRef, i));
274+
ref uint dst = ref Unsafe.As<byte, uint>(ref Unsafe.Add(ref destinationRef, i));
271275

272276
dst = v0 ^ v1;
273277
i += sizeof(uint);
274278
}
275279

276280
for (; i < length; ++i)
277281
{
278-
destination.GetRef(i) = (byte)(source.GetRef(i) ^ stream.GetRef(i));
282+
Unsafe.Add(ref destinationRef, i) = (byte)(Unsafe.Add(ref sourceRef, i) ^ Unsafe.Add(ref streamRef, i));
279283
}
280284
}
281285
}

0 commit comments

Comments
 (0)