Skip to content

Commit 4b0dfd1

Browse files
committed
Minor micro-optimization in DeflaterHuffman type
1 parent 70ed21e commit 4b0dfd1

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,15 @@ public static short BitReverse(int toReverse)
381381
* fail as expected. We can't simply check whether the value is lower than
382382
* 15 << 12, because higher values are acceptable in the first 3 accesses.
383383
* Doing this reduces the total number of index checks from 4 down to just 1. */
384-
Guard.MustBeLessThanOrEqualTo<uint>((uint)(toReverse >> 12), 15, nameof(toReverse));
384+
int toReverseRightShiftBy12 = toReverse >> 12;
385+
Guard.MustBeLessThanOrEqualTo<uint>((uint)toReverseRightShiftBy12, 15, nameof(toReverse));
385386

386387
ref byte bit4ReverseRef = ref MemoryMarshal.GetReference(Bit4Reverse);
387388

388389
return (short)(Unsafe.Add(ref bit4ReverseRef, toReverse & 0xF) << 12
389390
| Unsafe.Add(ref bit4ReverseRef, (toReverse >> 4) & 0xF) << 8
390391
| Unsafe.Add(ref bit4ReverseRef, (toReverse >> 8) & 0xF) << 4
391-
| Unsafe.Add(ref bit4ReverseRef, toReverse >> 12));
392+
| Unsafe.Add(ref bit4ReverseRef, toReverseRightShiftBy12));
392393
}
393394

394395
/// <inheritdoc/>

0 commit comments

Comments
 (0)