Skip to content

Commit 1cce4a6

Browse files
author
Ubuntu
committed
trying to reduce the cost of the 4-byte char
1 parent bd4a55a commit 1cce4a6

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/UTF8.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,8 @@ private unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust
13521352
Vector128<byte> fourthByte = Vector128.Create((byte)(0b11110000u - 0x80));
13531353
Vector128<byte> v0f = Vector128.Create((byte)0x0F);
13541354
Vector128<byte> v80 = Vector128.Create((byte)0x80);
1355+
Vector128<byte> fourthByteMinusOne = Vector128.Create((byte)(0b11110000u - 1));
1356+
Vector128<sbyte> largestcont = Vector128.Create((sbyte)-65); // -65 => 0b10111111
13551357
// Performance note: we could process 64 bytes at a time for better speed in some cases.
13561358
int start_point = processedLength;
13571359

@@ -1457,15 +1459,15 @@ private unsafe static (int utfadjust, int scalaradjust) calculateErrorPathadjust
14571459
return invalidBytePointer;
14581460
}
14591461
prevIncomplete = AdvSimd.SubtractSaturate(currentBlock, maxValue);
1460-
Vector128<sbyte> largestcont = Vector128.Create((sbyte)-65); // -65 => 0b10111111
14611462
contbytes += -AdvSimd.Arm64.AddAcross(AdvSimd.CompareLessThanOrEqual(Vector128.AsSByte(currentBlock), largestcont)).ToScalar();
1462-
1463-
// computing n4 is more expensive than we would like:
1464-
Vector128<byte> fourthByteMinusOne = Vector128.Create((byte)(0b11110000u - 1));
14651463
Vector128<byte> largerthan0f = AdvSimd.CompareGreaterThan(currentBlock, fourthByteMinusOne);
1466-
byte n4add = (byte)AdvSimd.Arm64.AddAcross(largerthan0f).ToScalar();
1467-
int negn4add = (int)(byte)-n4add;
1468-
n4 += negn4add;
1464+
ulong n4marker = AdvSimd.Arm64.MaxAcross(Vector128.AsUInt32(largerthan0f)).ToScalar();
1465+
if (n4marker != 0)
1466+
{
1467+
byte n4add = (byte)AdvSimd.Arm64.AddAcross(largerthan0f).ToScalar();
1468+
int negn4add = (int)(byte)-n4add;
1469+
n4 += negn4add;
1470+
}
14691471
}
14701472
}
14711473
bool hasIncompete = AdvSimd.Arm64.MaxAcross(Vector128.AsUInt32(prevIncomplete)).ToScalar() != 0;

0 commit comments

Comments
 (0)