@@ -2,32 +2,25 @@ namespace CryptoBase;
22
33public 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