Skip to content

Commit

Permalink
simplifying somewhat
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lemire committed May 27, 2024
1 parent bf4f6b3 commit 71b6fe2
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions src/UTF8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,16 +671,7 @@ public unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust(
contbytes += tempcont;
}

// We update the continuation bytes count using just one SIMD instruction (Avx2.CompareGreaterThan).
// Then we need popcount to count the number of continuation bytes and some arithmetic operations.
// We use the fact that as two's complement, -65 is 0b10111111, so we can use CompareGreaterThan
// to find continuation bytes: any byte greater than -65 is a not continuation byte. E.g., the next one
// is 0b11111110 (-64) and so forth. The smallest possible value is -128, which is 0b10000000.

Vector256<sbyte> largestcont = Vector256.Create((sbyte)-65); // -65 => 0b10111111
uint noncont = (uint)Avx2.MoveMask(Avx2.CompareGreaterThan(Vector256.AsSByte(currentBlock), largestcont));
contbytes += (int)(32-Popcnt.PopCount(noncont));

contbytes += (int)Popcnt.PopCount((uint)Avx2.MoveMask(byte_2_high));
// We use two instructions (SubtractSaturate and MoveMask) to update n4, with one arithmetic operation.
n4 += (int)Popcnt.PopCount((uint)Avx2.MoveMask(Avx2.SubtractSaturate(currentBlock, fourthByte)));
}
Expand Down

0 comments on commit 71b6fe2

Please sign in to comment.