Skip to content

Commit 84a9fdd

Browse files
CopilotHMBSbige
andcommitted
Replace MemoryMarshal.GetReference with GetReference extension and reuse base addresses
Co-authored-by: HMBSbige <19153265+HMBSbige@users.noreply.github.com>
1 parent 90a0905 commit 84a9fdd

File tree

13 files changed

+93
-67
lines changed

13 files changed

+93
-67
lines changed

src/CryptoBase/FastUtils.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,25 @@ public static void Xor16(ReadOnlySpan<byte> stream, ReadOnlySpan<byte> source, S
205205
{
206206
if (Vector128.IsHardwareAccelerated)
207207
{
208-
ref Vector128<byte> v0 = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(stream));
209-
ref Vector128<byte> v1 = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(source));
210-
ref Vector128<byte> dst = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(destination));
208+
ref byte streamRef = ref stream.GetReference();
209+
ref byte sourceRef = ref source.GetReference();
210+
ref byte destRef = ref destination.GetReference();
211+
212+
ref Vector128<byte> v0 = ref Unsafe.As<byte, Vector128<byte>>(ref streamRef);
213+
ref Vector128<byte> v1 = ref Unsafe.As<byte, Vector128<byte>>(ref sourceRef);
214+
ref Vector128<byte> dst = ref Unsafe.As<byte, Vector128<byte>>(ref destRef);
211215

212216
dst = v0 ^ v1;
213217
}
214218
else
215219
{
216-
ref UInt128 v0 = ref Unsafe.As<byte, UInt128>(ref MemoryMarshal.GetReference(stream));
217-
ref UInt128 v1 = ref Unsafe.As<byte, UInt128>(ref MemoryMarshal.GetReference(source));
218-
ref UInt128 dst = ref Unsafe.As<byte, UInt128>(ref MemoryMarshal.GetReference(destination));
220+
ref byte streamRef = ref stream.GetReference();
221+
ref byte sourceRef = ref source.GetReference();
222+
ref byte destRef = ref destination.GetReference();
223+
224+
ref UInt128 v0 = ref Unsafe.As<byte, UInt128>(ref streamRef);
225+
ref UInt128 v1 = ref Unsafe.As<byte, UInt128>(ref sourceRef);
226+
ref UInt128 dst = ref Unsafe.As<byte, UInt128>(ref destRef);
219227

220228
dst = v0 ^ v1;
221229
}
@@ -229,15 +237,21 @@ public static void Xor16(Span<byte> source, ReadOnlySpan<byte> stream)
229237
{
230238
if (Vector128.IsHardwareAccelerated)
231239
{
232-
ref Vector128<byte> v0 = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(source));
233-
ref Vector128<byte> v1 = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(stream));
240+
ref byte sourceRef = ref source.GetReference();
241+
ref byte streamRef = ref stream.GetReference();
242+
243+
ref Vector128<byte> v0 = ref Unsafe.As<byte, Vector128<byte>>(ref sourceRef);
244+
ref Vector128<byte> v1 = ref Unsafe.As<byte, Vector128<byte>>(ref streamRef);
234245

235246
v0 ^= v1;
236247
}
237248
else
238249
{
239-
ref UInt128 v0 = ref Unsafe.As<byte, UInt128>(ref MemoryMarshal.GetReference(source));
240-
ref UInt128 v1 = ref Unsafe.As<byte, UInt128>(ref MemoryMarshal.GetReference(stream));
250+
ref byte sourceRef = ref source.GetReference();
251+
ref byte streamRef = ref stream.GetReference();
252+
253+
ref UInt128 v0 = ref Unsafe.As<byte, UInt128>(ref sourceRef);
254+
ref UInt128 v1 = ref Unsafe.As<byte, UInt128>(ref streamRef);
241255

242256
v0 ^= v1;
243257
}

src/CryptoBase/Macs/GHash/GHashX86.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public GHashX86(scoped ReadOnlySpan<byte> key)
1818
{
1919
ArgumentOutOfRangeException.ThrowIfLessThan(key.Length, KeySize, nameof(key));
2020

21-
ref Vector128<byte> v = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(key));
21+
ref byte keyRef = ref key.GetReference();
22+
ref Vector128<byte> v = ref Unsafe.As<byte, Vector128<byte>>(ref keyRef);
2223
_key = v.ReverseEndianness128();
2324

2425
Reset();
@@ -28,7 +29,8 @@ public GHashX86(scoped ReadOnlySpan<byte> key)
2829
private void GFMul(scoped ReadOnlySpan<byte> x)
2930
{
3031
Vector128<ulong> a = _key.AsUInt64();
31-
Vector128<ulong> b = (Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(x)).ReverseEndianness128() ^ _buffer).AsUInt64();
32+
ref byte xRef = ref x.GetReference();
33+
Vector128<ulong> b = (Unsafe.As<byte, Vector128<byte>>(ref xRef).ReverseEndianness128() ^ _buffer).AsUInt64();
3234

3335
Vector128<uint> tmp3 = Pclmulqdq.CarrylessMultiply(a, b, 0x00).AsUInt32();
3436
Vector128<uint> tmp4 = Pclmulqdq.CarrylessMultiply(a, b, 0x10).AsUInt32();

src/CryptoBase/SymmetricCryptos/BlockCryptoModes/CTR/CTR128StreamMode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ private void UpdateCore(ReadOnlySpan<byte> source, Span<byte> destination)
5454
{
5555
crypto.Encrypt(counter, stream);
5656

57-
ref UInt128 c = ref Unsafe.As<byte, UInt128>(ref MemoryMarshal.GetReference(counter));
57+
ref byte counterRef = ref counter.GetReference();
58+
ref UInt128 c = ref Unsafe.As<byte, UInt128>(ref counterRef);
5859
c = BinaryPrimitives.ReverseEndianness(BinaryPrimitives.ReverseEndianness(c) + 1);
5960
}
6061

src/CryptoBase/SymmetricCryptos/BlockCryptoModes/CTR/CTR128StreamModeBlock16X86.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public CTR128StreamModeBlock16X86(IBlockCrypto crypto, ReadOnlySpan<byte> iv)
3535
_counter = ArrayPool<byte>.Shared.Rent(BlockSize16);
3636
_keyStream = ArrayPool<byte>.Shared.Rent(BlockSize16);
3737

38-
_iCounter = FastUtils.BroadcastVector128ToVector256(ref MemoryMarshal.GetReference(iv)).ReverseEndianness128().IncUpper128Le();
38+
ref byte ivRef = ref iv.GetReference();
39+
_iCounter = FastUtils.BroadcastVector128ToVector256(ref ivRef).ReverseEndianness128().IncUpper128Le();
3940

4041
Reset();
4142
}
@@ -84,7 +85,7 @@ private void UpdateKeyStream()
8485
Vector256<byte> v6 = _counterV6.ReverseEndianness128();
8586
Vector256<byte> v7 = _counterV7.ReverseEndianness128();
8687

87-
ref byte cRef = ref MemoryMarshal.GetReference(c);
88+
ref byte cRef = ref c.GetReference();
8889
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 0 * 2 * BlockSize), v0);
8990
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 1 * 2 * BlockSize), v1);
9091
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 2 * 2 * BlockSize), v2);

src/CryptoBase/SymmetricCryptos/BlockCryptoModes/CTR/CTR128StreamModeBlock4X86.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public CTR128StreamModeBlock4X86(IBlockCrypto crypto, ReadOnlySpan<byte> iv)
3232
_counter = ArrayPool<byte>.Shared.Rent(BlockSize4);
3333
_keyStream = ArrayPool<byte>.Shared.Rent(BlockSize4);
3434

35-
ref Vector128<byte> v = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(iv));
35+
ref byte ivRef = ref iv.GetReference();
36+
ref Vector128<byte> v = ref Unsafe.As<byte, Vector128<byte>>(ref ivRef);
3637
_iCounter = v.ReverseEndianness128();
3738

3839
Reset();
@@ -79,7 +80,7 @@ private void UpdateKeyStream()
7980
Vector128<byte> v2 = _counterV2.ReverseEndianness128();
8081
Vector128<byte> v3 = _counterV3.ReverseEndianness128();
8182

82-
ref byte cRef = ref MemoryMarshal.GetReference(c);
83+
ref byte cRef = ref c.GetReference();
8384
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 0 * BlockSize), v0);
8485
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 1 * BlockSize), v1);
8586
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 2 * BlockSize), v2);

src/CryptoBase/SymmetricCryptos/BlockCryptoModes/CTR/CTR128StreamModeBlock8AvxX86.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public CTR128StreamModeBlock8AvxX86(IBlockCrypto crypto, ReadOnlySpan<byte> iv)
3232
_counter = ArrayPool<byte>.Shared.Rent(BlockSize8);
3333
_keyStream = ArrayPool<byte>.Shared.Rent(BlockSize8);
3434

35-
_iCounter = FastUtils.BroadcastVector128ToVector256(ref MemoryMarshal.GetReference(iv)).ReverseEndianness128().IncUpper128Le();
35+
ref byte ivRef = ref iv.GetReference();
36+
_iCounter = FastUtils.BroadcastVector128ToVector256(ref ivRef).ReverseEndianness128().IncUpper128Le();
3637

3738
Reset();
3839
}
@@ -77,7 +78,7 @@ private void UpdateKeyStream()
7778
Vector256<byte> v2 = _counterV2.ReverseEndianness128();
7879
Vector256<byte> v3 = _counterV3.ReverseEndianness128();
7980

80-
ref byte cRef = ref MemoryMarshal.GetReference(c);
81+
ref byte cRef = ref c.GetReference();
8182
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 0 * 2 * BlockSize), v0);
8283
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 1 * 2 * BlockSize), v1);
8384
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 2 * 2 * BlockSize), v2);

src/CryptoBase/SymmetricCryptos/BlockCryptoModes/CTR/CTR128StreamModeBlock8X86.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public CTR128StreamModeBlock8X86(IBlockCrypto crypto, ReadOnlySpan<byte> iv)
3535
_counter = ArrayPool<byte>.Shared.Rent(BlockSize8);
3636
_keyStream = ArrayPool<byte>.Shared.Rent(BlockSize8);
3737

38-
ref Vector128<byte> v = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(iv));
38+
ref byte ivRef = ref iv.GetReference();
39+
ref Vector128<byte> v = ref Unsafe.As<byte, Vector128<byte>>(ref ivRef);
3940
_iCounter = v.ReverseEndianness128();
4041

4142
Reset();
@@ -85,7 +86,7 @@ private void UpdateKeyStream()
8586
Vector128<byte> v6 = _counterV6.ReverseEndianness128();
8687
Vector128<byte> v7 = _counterV7.ReverseEndianness128();
8788

88-
ref byte cRef = ref MemoryMarshal.GetReference(c);
89+
ref byte cRef = ref c.GetReference();
8990
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 0 * BlockSize), v0);
9091
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 1 * BlockSize), v1);
9192
Unsafe.WriteUnaligned(ref Unsafe.Add(ref cRef, 2 * BlockSize), v2);

src/CryptoBase/SymmetricCryptos/BlockCryptoModes/CTR/CTR128StreamModeX86.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public CTR128StreamModeX86(IBlockCrypto crypto, ReadOnlySpan<byte> iv)
2222

2323
_internalBlockCrypto = crypto;
2424

25-
ref Vector128<byte> v = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(iv));
25+
ref byte ivRef = ref iv.GetReference();
26+
ref Vector128<byte> v = ref Unsafe.As<byte, Vector128<byte>>(ref ivRef);
2627
_iCounter = v.ReverseEndianness128();
2728

2829
Reset();

src/CryptoBase/SymmetricCryptos/BlockCryptoModes/Xts/XtsMode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public XtsMode(IBlockCrypto dataCrypto, IBlockCrypto tweakCrypto, ReadOnlySpan<b
2121

2222
_dataCrypto = dataCrypto;
2323
_tweakCrypto = tweakCrypto;
24-
_iv = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(iv));
24+
ref byte ivRef = ref iv.GetReference();
25+
_iv = Unsafe.ReadUnaligned<Vector128<byte>>(ref ivRef);
2526
}
2627

2728
[SkipLocalsInit]

src/CryptoBase/SymmetricCryptos/BlockCryptos/SM4/SM4Utils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ public static void Encrypt8(uint[] rk, ReadOnlySpan<byte> source, Span<byte> des
201201

202202
public static void Encrypt16(uint[] rk, ReadOnlySpan<byte> source, Span<byte> destination)
203203
{
204-
ref byte sourceRef = ref MemoryMarshal.GetReference(source);
205-
ref byte dstRef = ref MemoryMarshal.GetReference(destination);
204+
ref byte sourceRef = ref source.GetReference();
205+
ref byte dstRef = ref destination.GetReference();
206206

207207
ref Vector256<byte> s0 = ref Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref sourceRef, 0 * 32));
208208
ref Vector256<byte> s1 = ref Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref sourceRef, 1 * 32));

0 commit comments

Comments
 (0)