11// Copyright (c) Six Labors.
22// 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
44
55#if ! NET6_0_OR_GREATER
6+ using System . Runtime . CompilerServices ;
67#if NETCOREAPP3_0_OR_GREATER
78using System . Runtime . Intrinsics ;
89using System . Runtime . Intrinsics . X86 ;
@@ -44,36 +45,37 @@ internal static class Adler32
4445 /// Calculates the Adler32 checksum with the bytes taken from the span.
4546 /// </summary>
4647 /// <param name="buffer">The readonly span of bytes.</param>
47- /// <param name="offset">The offset.</param>
48- /// <param name="length">The length.</param>
4948 /// <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 )
5151 {
52- if ( buffer . Length == 0 )
52+ if ( buffer . IsEmpty )
5353 {
5454 return SeedValue ;
5555 }
5656
5757#if NETCOREAPP3_0_OR_GREATER
5858 if ( Ssse3 . IsSupported && buffer . Length >= MinBufferSize )
5959 {
60- return CalculateSse ( buffer , offset , length ) ;
60+ return CalculateSse ( buffer ) ;
6161 }
6262#endif
6363
64- return CalculateScalar ( buffer , offset , length ) ;
64+ return CalculateScalar ( buffer ) ;
6565 }
6666
6767#if NETCOREAPP3_0_OR_GREATER
6868 // 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 )
7071 {
7172 uint s1 = SeedValue & 0xFFFF ;
7273 uint s2 = ( SeedValue >> 16 ) & 0xFFFF ;
7374
7475 // Process the data in blocks.
7576 const int BLOCK_SIZE = 1 << 5 ;
7677
78+ uint length = ( uint ) buffer . Length ;
7779 uint blocks = length / BLOCK_SIZE ;
7880 length -= blocks * BLOCK_SIZE ;
7981
@@ -82,7 +84,7 @@ private static unsafe uint CalculateSse(ReadOnlySpan<byte> buffer, uint offset,
8284 fixed ( byte * tapPtr = Tap1Tap2 )
8385 {
8486 index += ( int ) blocks * BLOCK_SIZE ;
85- var localBufferPtr = bufferPtr + offset ;
87+ var localBufferPtr = bufferPtr ;
8688
8789 // _mm_setr_epi8 on x86
8890 Vector128 < sbyte > tap1 = Sse2 . LoadVector128 ( ( sbyte * ) tapPtr ) ;
@@ -192,15 +194,17 @@ private static unsafe uint CalculateSse(ReadOnlySpan<byte> buffer, uint offset,
192194 }
193195#endif
194196
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 )
196199 {
197200 uint s1 = SeedValue & 0xFFFF ;
198201 uint s2 = ( SeedValue >> 16 ) & 0xFFFF ;
199202 uint k ;
200203
201204 fixed ( byte * bufferPtr = buffer )
202205 {
203- var localBufferPtr = bufferPtr + offset ;
206+ var localBufferPtr = bufferPtr ;
207+ uint length = ( uint ) buffer . Length ;
204208
205209 while ( length > 0 )
206210 {
0 commit comments