Skip to content

Commit bec9604

Browse files
committed
Replace uses of TestZ intrinsic
1 parent bd84734 commit bec9604

File tree

4 files changed

+24
-44
lines changed

4 files changed

+24
-44
lines changed

src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ private static unsafe void Avx2Decode(ref byte* srcBytes, ref byte* destBytes, b
791791
Vector256<sbyte> hi = Avx2.Shuffle(lutHi, hiNibbles);
792792
Vector256<sbyte> lo = Avx2.Shuffle(lutLo, loNibbles);
793793

794-
if (!Avx.TestZ(lo, hi))
794+
if ((lo & hi) != Vector256<sbyte>.Zero)
795795
{
796796
break;
797797
}

src/libraries/System.Private.CoreLib/src/System/Text/Ascii.Utility.cs

+14-34
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ private static bool VectorContainsNonAsciiChar(Vector128<byte> asciiVector)
15031503
// prefer architecture specific intrinsic as they offer better perf
15041504
if (Sse41.IsSupported)
15051505
{
1506-
return !Sse41.TestZ(asciiVector, Vector128.Create((byte)0x80));
1506+
return (asciiVector & Vector128.Create((byte)0x80)) != Vector128<byte>.Zero;
15071507
}
15081508
else if (AdvSimd.Arm64.IsSupported)
15091509
{
@@ -1520,22 +1520,13 @@ private static bool VectorContainsNonAsciiChar(Vector128<byte> asciiVector)
15201520
private static bool VectorContainsNonAsciiChar(Vector128<ushort> utf16Vector)
15211521
{
15221522
// prefer architecture specific intrinsic as they offer better perf
1523-
if (Sse2.IsSupported)
1523+
if (Sse2.IsSupported && !Sse41.IsSupported)
15241524
{
1525-
if (Sse41.IsSupported)
1526-
{
1527-
Vector128<ushort> asciiMaskForTestZ = Vector128.Create((ushort)0xFF80);
1528-
// If a non-ASCII bit is set in any WORD of the vector, we have seen non-ASCII data.
1529-
return !Sse41.TestZ(utf16Vector.AsInt16(), asciiMaskForTestZ.AsInt16());
1530-
}
1531-
else
1532-
{
1533-
Vector128<ushort> asciiMaskForAddSaturate = Vector128.Create((ushort)0x7F80);
1534-
// The operation below forces the 0x8000 bit of each WORD to be set iff the WORD element
1535-
// has value >= 0x0800 (non-ASCII). Then we'll treat the vector as a BYTE vector in order
1536-
// to extract the mask. Reminder: the 0x0080 bit of each WORD should be ignored.
1537-
return (Sse2.MoveMask(Sse2.AddSaturate(utf16Vector, asciiMaskForAddSaturate).AsByte()) & 0b_1010_1010_1010_1010) != 0;
1538-
}
1525+
Vector128<ushort> asciiMaskForAddSaturate = Vector128.Create((ushort)0x7F80);
1526+
// The operation below forces the 0x8000 bit of each WORD to be set iff the WORD element
1527+
// has value >= 0x0800 (non-ASCII). Then we'll treat the vector as a BYTE vector in order
1528+
// to extract the mask. Reminder: the 0x0080 bit of each WORD should be ignored.
1529+
return (Sse2.MoveMask(Sse2.AddSaturate(utf16Vector, asciiMaskForAddSaturate).AsByte()) & 0b_1010_1010_1010_1010) != 0;
15391530
}
15401531
else if (AdvSimd.Arm64.IsSupported)
15411532
{
@@ -1556,18 +1547,10 @@ private static bool VectorContainsNonAsciiChar(Vector128<ushort> utf16Vector)
15561547
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15571548
private static bool VectorContainsNonAsciiChar(Vector256<ushort> utf16Vector)
15581549
{
1559-
if (Avx.IsSupported)
1560-
{
1561-
Vector256<ushort> asciiMaskForTestZ = Vector256.Create((ushort)0xFF80);
1562-
return !Avx.TestZ(utf16Vector.AsInt16(), asciiMaskForTestZ.AsInt16());
1563-
}
1564-
else
1565-
{
1566-
const ushort asciiMask = ushort.MaxValue - 127; // 0xFF80
1567-
Vector256<ushort> zeroIsAscii = utf16Vector & Vector256.Create(asciiMask);
1568-
// If a non-ASCII bit is set in any WORD of the vector, we have seen non-ASCII data.
1569-
return zeroIsAscii != Vector256<ushort>.Zero;
1570-
}
1550+
const ushort asciiMask = ushort.MaxValue - 127; // 0xFF80
1551+
Vector256<ushort> zeroIsAscii = utf16Vector & Vector256.Create(asciiMask);
1552+
// If a non-ASCII bit is set in any WORD of the vector, we have seen non-ASCII data.
1553+
return zeroIsAscii != Vector256<ushort>.Zero;
15711554
}
15721555

15731556
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -1600,14 +1583,13 @@ private static bool AllCharsInVectorAreAscii<T>(Vector128<T> vector)
16001583
if (typeof(T) == typeof(byte))
16011584
{
16021585
return
1603-
Sse41.IsSupported ? Sse41.TestZ(vector.AsByte(), Vector128.Create((byte)0x80)) :
1586+
Sse41.IsSupported ? (vector.AsByte(), Vector128.Create((byte)0x80)) != Vector128<byte>.Zero :
16041587
AdvSimd.Arm64.IsSupported ? AllBytesInUInt64AreAscii(AdvSimd.Arm64.MaxPairwise(vector.AsByte(), vector.AsByte()).AsUInt64().ToScalar()) :
16051588
vector.AsByte().ExtractMostSignificantBits() == 0;
16061589
}
16071590
else
16081591
{
16091592
return
1610-
Sse41.IsSupported ? Sse41.TestZ(vector.AsUInt16(), Vector128.Create((ushort)0xFF80)) :
16111593
AdvSimd.Arm64.IsSupported ? AllCharsInUInt64AreAscii(AdvSimd.Arm64.MaxPairwise(vector.AsUInt16(), vector.AsUInt16()).AsUInt64().ToScalar()) :
16121594
(vector.AsUInt16() & Vector128.Create((ushort)0xFF80)) == Vector128<ushort>.Zero;
16131595
}
@@ -1623,14 +1605,12 @@ private static bool AllCharsInVectorAreAscii<T>(Vector256<T> vector)
16231605
if (typeof(T) == typeof(byte))
16241606
{
16251607
return
1626-
Avx.IsSupported ? Avx.TestZ(vector.AsByte(), Vector256.Create((byte)0x80)) :
1608+
Avx.IsSupported ? (vector.AsByte() & Vector256.Create((byte)0x80)) == Vector256<byte>.Zero:
16271609
vector.AsByte().ExtractMostSignificantBits() == 0;
16281610
}
16291611
else
16301612
{
1631-
return
1632-
Avx.IsSupported ? Avx.TestZ(vector.AsUInt16(), Vector256.Create((ushort)0xFF80)) :
1633-
(vector.AsUInt16() & Vector256.Create((ushort)0xFF80)) == Vector256<ushort>.Zero;
1613+
return (vector.AsUInt16() & Vector256.Create((ushort)0xFF80)) == Vector256<ushort>.Zero:
16341614
}
16351615
}
16361616

src/libraries/System.Private.CoreLib/src/System/Text/Latin1Utility.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private static unsafe nuint GetIndexOfFirstNonLatin1Char_Sse2(char* pBuffer, nui
267267
{
268268
// If a non-Latin-1 bit is set in any WORD of the combined vector, we have seen non-Latin-1 data.
269269
// Jump to the non-Latin-1 handler to figure out which particular vector contained non-Latin-1 data.
270-
if (!Sse41.TestZ(combinedVector, latin1MaskForTestZ))
270+
if ((combinedVector & latin1MaskForTestZ) != Vector128<ushort>.Zero)
271271
{
272272
goto FoundNonLatin1DataInFirstOrSecondVector;
273273
}
@@ -312,7 +312,7 @@ private static unsafe nuint GetIndexOfFirstNonLatin1Char_Sse2(char* pBuffer, nui
312312
{
313313
// If a non-Latin-1 bit is set in any WORD of the combined vector, we have seen non-Latin-1 data.
314314
// Jump to the non-Latin-1 handler to figure out which particular vector contained non-Latin-1 data.
315-
if (!Sse41.TestZ(firstVector, latin1MaskForTestZ))
315+
if ((firstVector & latin1MaskForTestZ) != Vector128<ushort>.Zero)
316316
{
317317
goto FoundNonLatin1DataInFirstVector;
318318
}
@@ -347,7 +347,7 @@ private static unsafe nuint GetIndexOfFirstNonLatin1Char_Sse2(char* pBuffer, nui
347347
{
348348
// If a non-Latin-1 bit is set in any WORD of the combined vector, we have seen non-Latin-1 data.
349349
// Jump to the non-Latin-1 handler to figure out which particular vector contained non-Latin-1 data.
350-
if (!Sse41.TestZ(firstVector, latin1MaskForTestZ))
350+
if ((firstVector & latin1MaskForTestZ) != Vector128<ushort>.Zero)
351351
{
352352
goto FoundNonLatin1DataInFirstVector;
353353
}
@@ -381,7 +381,7 @@ private static unsafe nuint GetIndexOfFirstNonLatin1Char_Sse2(char* pBuffer, nui
381381
if (Sse41.IsSupported)
382382
#pragma warning restore IntrinsicsInSystemPrivateCoreLibAttributeNotSpecificEnough
383383
{
384-
if (!Sse41.TestZ(firstVector, latin1MaskForTestZ))
384+
if ((firstVector & latin1MaskForTestZ) != Vector128<ushort>.Zero)
385385
{
386386
goto FoundNonLatin1DataInFirstVector;
387387
}
@@ -795,7 +795,7 @@ private static unsafe nuint NarrowUtf16ToLatin1_Sse2(char* pUtf16Buffer, byte* p
795795
if (Sse41.IsSupported)
796796
#pragma warning restore IntrinsicsInSystemPrivateCoreLibAttributeNotSpecificEnough
797797
{
798-
if (!Sse41.TestZ(utf16VectorFirst, latin1MaskForTestZ))
798+
if ((utf16VectorFirst & latin1MaskForTestZ) != Vector128<short>.Zero)
799799
{
800800
return 0;
801801
}
@@ -837,7 +837,7 @@ private static unsafe nuint NarrowUtf16ToLatin1_Sse2(char* pUtf16Buffer, byte* p
837837
if (Sse41.IsSupported)
838838
#pragma warning restore IntrinsicsInSystemPrivateCoreLibAttributeNotSpecificEnough
839839
{
840-
if (!Sse41.TestZ(utf16VectorFirst, latin1MaskForTestZ))
840+
if ((utf16VectorFirst & latin1MaskForTestZ) != Vector128<short>.Zero)
841841
{
842842
goto Finish;
843843
}
@@ -878,7 +878,7 @@ private static unsafe nuint NarrowUtf16ToLatin1_Sse2(char* pUtf16Buffer, byte* p
878878
if (Sse41.IsSupported)
879879
#pragma warning restore IntrinsicsInSystemPrivateCoreLibAttributeNotSpecificEnough
880880
{
881-
if (!Sse41.TestZ(combinedVector, latin1MaskForTestZ))
881+
if ((combinedVector, & latin1MaskForTestZ) != Vector128<short>.Zero)
882882
{
883883
goto FoundNonLatin1DataInLoop;
884884
}
@@ -914,7 +914,7 @@ private static unsafe nuint NarrowUtf16ToLatin1_Sse2(char* pUtf16Buffer, byte* p
914914
if (Sse41.IsSupported)
915915
#pragma warning restore IntrinsicsInSystemPrivateCoreLibAttributeNotSpecificEnough
916916
{
917-
if (!Sse41.TestZ(utf16VectorFirst, latin1MaskForTestZ))
917+
if ((utf16VectorFirst & latin1MaskForTestZ) != Vector128<short>.Zero)
918918
{
919919
goto Finish; // found non-Latin-1 data
920920
}

src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8Utility.Transcoding.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ public static OperationStatus TranscodeToUtf8(char* pInputBuffer, int inputLengt
968968
}
969969
else
970970
{
971-
if (!Sse41.TestZ(utf16Data, nonAsciiUtf16DataMask))
971+
if ((utf16Data & nonAsciiUtf16DataMask) != Vector128<short>.Zero)
972972
{
973973
goto LoopTerminatedDueToNonAsciiDataInVectorLocal;
974974
}

0 commit comments

Comments
 (0)