Skip to content

Commit

Permalink
Remove internal dependency on ShuffleUnsafe's behaviour wrt high bit
Browse files Browse the repository at this point in the history
  • Loading branch information
hamarb123 committed Aug 6, 2024
1 parent 95b8eb8 commit 57e4884
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -832,11 +832,7 @@ internal static Vector128<byte> SimdShuffle(Vector128<byte> left, Vector128<byte
right &= mask8F;
}

#if NET9_0_OR_GREATER
return Vector128.ShuffleUnsafe(left, right);
#else
return Base64Helper.ShuffleUnsafe(left, right);
#endif
return Base64Helper.ShuffleUnsafeModified(left, right);
}

#if NET9_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,17 @@ internal static bool VectorContainsNonAsciiChar(Vector512<ushort> utf16Vector)
// If a non-ASCII bit is set in any WORD of the vector, we have seen non-ASCII data.
return zeroIsAscii != Vector512<ushort>.Zero;
}
#endif

#if NET
// same as ShuffleUnsafe, except that we guarantee that if the high bit is set, it gives 0
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector128<byte> ShuffleUnsafe(Vector128<byte> vector, Vector128<byte> indices)
#if NET9_0_OR_GREATER
[CompExactlyDependsOn(typeof(Ssse3))]
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
[CompExactlyDependsOn(typeof(PackedSimd))]
#endif
internal static Vector128<byte> ShuffleUnsafeModified(Vector128<byte> vector, Vector128<byte> indices)
{
if (Ssse3.IsSupported)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Text;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -1018,7 +1019,9 @@ public static int LastIndexOfAny<TNegator>(ref byte searchSpace, int searchSpace

[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CompExactlyDependsOn(typeof(Sse2))]
[CompExactlyDependsOn(typeof(Ssse3))]
[CompExactlyDependsOn(typeof(AdvSimd))]
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
[CompExactlyDependsOn(typeof(PackedSimd))]
private static Vector128<byte> IndexOfAnyLookup<TNegator, TOptimizations>(Vector128<short> source0, Vector128<short> source1, Vector128<byte> bitmapLookup)
where TNegator : struct, INegator
Expand Down Expand Up @@ -1052,7 +1055,7 @@ private static Vector128<byte> IndexOfAnyLookupCore(Vector128<byte> source, Vect

// The bitmapLookup represents a 8x16 table of bits, indicating whether a character is present in the needle.
// Lookup the rows via the lower nibble and the column via the higher nibble.
Vector128<byte> bitMask = Vector128.ShuffleUnsafe(bitmapLookup, lowNibbles);
Vector128<byte> bitMask = Base64Helper.ShuffleUnsafeModified(bitmapLookup, lowNibbles);

// For values above 127, the high nibble will be above 7. We construct the positions vector for the shuffle such that those values map to 0.
Vector128<byte> bitPositions = Vector128.ShuffleUnsafe(Vector128.Create(0x8040201008040201, 0).AsByte(), highNibbles);
Expand Down Expand Up @@ -1089,6 +1092,7 @@ private static Vector256<byte> IndexOfAnyLookupCore(Vector256<byte> source, Vect
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CompExactlyDependsOn(typeof(Ssse3))]
[CompExactlyDependsOn(typeof(AdvSimd))]
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
[CompExactlyDependsOn(typeof(PackedSimd))]
private static Vector128<byte> IndexOfAnyLookup<TNegator>(Vector128<byte> source, Vector128<byte> bitmapLookup0, Vector128<byte> bitmapLookup1)
where TNegator : struct, INegator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Text;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.Wasm;
using System.Runtime.Intrinsics.X86;

namespace System.Buffers
Expand All @@ -17,6 +19,7 @@ internal static class TeddyHelper
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CompExactlyDependsOn(typeof(Ssse3))]
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
[CompExactlyDependsOn(typeof(PackedSimd))]
public static (Vector128<byte> Result, Vector128<byte> Prev0) ProcessInputN2(
Vector128<byte> input,
Vector128<byte> prev0,
Expand Down Expand Up @@ -89,7 +92,9 @@ public static (Vector512<byte> Result, Vector512<byte> Prev0) ProcessInputN2(

[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CompExactlyDependsOn(typeof(Ssse3))]
[CompExactlyDependsOn(typeof(AdvSimd))]
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
[CompExactlyDependsOn(typeof(PackedSimd))]
public static (Vector128<byte> Result, Vector128<byte> Prev0, Vector128<byte> Prev1) ProcessInputN3(
Vector128<byte> input,
Vector128<byte> prev0, Vector128<byte> prev1,
Expand Down Expand Up @@ -293,9 +298,10 @@ private static (Vector512<byte> Low, Vector512<byte> High) GetNibbles(Vector512<
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CompExactlyDependsOn(typeof(Ssse3))]
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
[CompExactlyDependsOn(typeof(PackedSimd))]
private static Vector128<byte> Shuffle(Vector128<byte> maskLow, Vector128<byte> maskHigh, Vector128<byte> low, Vector128<byte> high)
{
return Vector128.ShuffleUnsafe(maskLow, low) & Vector128.ShuffleUnsafe(maskHigh, high);
return Base64Helper.ShuffleUnsafeModified(maskLow, low) & Vector128.ShuffleUnsafe(maskHigh, high);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down

0 comments on commit 57e4884

Please sign in to comment.