1
1
// Copyright (c) Six Labors.
2
2
// Licensed under the Apache License, Version 2.0.
3
- // https://github.com/SixLabors/ImageSharp/blob/master/src/ImageSharp/Formats/Png /Zlib/Adler32.cs
3
+ // https://github.com/SixLabors/ImageSharp/blob/master/src/ImageSharp/Compression /Zlib/Adler32.cs
4
4
5
5
#if ! NET6_0_OR_GREATER
6
+ using System . Runtime . CompilerServices ;
6
7
#if NETCOREAPP3_0_OR_GREATER
7
8
using System . Runtime . Intrinsics ;
8
9
using System . Runtime . Intrinsics . X86 ;
@@ -44,36 +45,37 @@ internal static class Adler32
44
45
/// Calculates the Adler32 checksum with the bytes taken from the span.
45
46
/// </summary>
46
47
/// <param name="buffer">The readonly span of bytes.</param>
47
- /// <param name="offset">The offset.</param>
48
- /// <param name="length">The length.</param>
49
48
/// <returns>The <see cref="uint"/>.</returns>
50
- public static uint Calculate ( ReadOnlySpan < byte > buffer , uint offset , uint length )
49
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
50
+ public static uint Calculate ( ReadOnlySpan < byte > buffer )
51
51
{
52
- if ( buffer . Length == 0 )
52
+ if ( buffer . IsEmpty )
53
53
{
54
54
return SeedValue ;
55
55
}
56
56
57
57
#if NETCOREAPP3_0_OR_GREATER
58
58
if ( Ssse3 . IsSupported && buffer . Length >= MinBufferSize )
59
59
{
60
- return CalculateSse ( buffer , offset , length ) ;
60
+ return CalculateSse ( buffer ) ;
61
61
}
62
62
#endif
63
63
64
- return CalculateScalar ( buffer , offset , length ) ;
64
+ return CalculateScalar ( buffer ) ;
65
65
}
66
66
67
67
#if NETCOREAPP3_0_OR_GREATER
68
68
// Based on https://github.com/chromium/chromium/blob/master/third_party/zlib/adler32_simd.c
69
- private static unsafe uint CalculateSse ( ReadOnlySpan < byte > buffer , uint offset , uint length )
69
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
70
+ private static unsafe uint CalculateSse ( ReadOnlySpan < byte > buffer )
70
71
{
71
72
uint s1 = SeedValue & 0xFFFF ;
72
73
uint s2 = ( SeedValue >> 16 ) & 0xFFFF ;
73
74
74
75
// Process the data in blocks.
75
76
const int BLOCK_SIZE = 1 << 5 ;
76
77
78
+ uint length = ( uint ) buffer . Length ;
77
79
uint blocks = length / BLOCK_SIZE ;
78
80
length -= blocks * BLOCK_SIZE ;
79
81
@@ -82,7 +84,7 @@ private static unsafe uint CalculateSse(ReadOnlySpan<byte> buffer, uint offset,
82
84
fixed ( byte * tapPtr = Tap1Tap2 )
83
85
{
84
86
index += ( int ) blocks * BLOCK_SIZE ;
85
- var localBufferPtr = bufferPtr + offset ;
87
+ var localBufferPtr = bufferPtr ;
86
88
87
89
// _mm_setr_epi8 on x86
88
90
Vector128 < sbyte > tap1 = Sse2 . LoadVector128 ( ( sbyte * ) tapPtr ) ;
@@ -192,15 +194,17 @@ private static unsafe uint CalculateSse(ReadOnlySpan<byte> buffer, uint offset,
192
194
}
193
195
#endif
194
196
195
- private static unsafe uint CalculateScalar ( ReadOnlySpan < byte > buffer , uint offset , uint length )
197
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
198
+ private static unsafe uint CalculateScalar ( ReadOnlySpan < byte > buffer )
196
199
{
197
200
uint s1 = SeedValue & 0xFFFF ;
198
201
uint s2 = ( SeedValue >> 16 ) & 0xFFFF ;
199
202
uint k ;
200
203
201
204
fixed ( byte * bufferPtr = buffer )
202
205
{
203
- var localBufferPtr = bufferPtr + offset ;
206
+ var localBufferPtr = bufferPtr ;
207
+ uint length = ( uint ) buffer . Length ;
204
208
205
209
while ( length > 0 )
206
210
{
0 commit comments