From 4890190de68fd7020c870d971f4a3e3bc40addef Mon Sep 17 00:00:00 2001 From: rameel Date: Mon, 14 Jul 2025 18:33:50 +0500 Subject: [PATCH] Backport ExtractMostSignificantBits and drop .NET7.0 target --- .../Ramstack.Globbing.Tests.csproj | 2 +- .../SimdConfigurationTests.cs | 1 + Ramstack.Globbing/Internal/PathHelper.cs | 18 +++++++++++++----- Ramstack.Globbing/Ramstack.Globbing.csproj | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Ramstack.Globbing.Tests/Ramstack.Globbing.Tests.csproj b/Ramstack.Globbing.Tests/Ramstack.Globbing.Tests.csproj index 2f124f9..2a75b0b 100644 --- a/Ramstack.Globbing.Tests/Ramstack.Globbing.Tests.csproj +++ b/Ramstack.Globbing.Tests/Ramstack.Globbing.Tests.csproj @@ -1,6 +1,6 @@ - net6.0;net7.0 + net6.0 enable enable preview diff --git a/Ramstack.Globbing.Tests/SimdConfigurationTests.cs b/Ramstack.Globbing.Tests/SimdConfigurationTests.cs index 7371115..e76671b 100644 --- a/Ramstack.Globbing.Tests/SimdConfigurationTests.cs +++ b/Ramstack.Globbing.Tests/SimdConfigurationTests.cs @@ -15,6 +15,7 @@ public void VerifySimdConfiguration() Assert.That(Sse2.IsSupported, Is.False); Assert.That(Sse41.IsSupported, Is.False); Assert.That(Avx2.IsSupported, Is.False); + Assert.That(AdvSimd.Arm64.IsSupported, Is.False); Assert.That(AdvSimd.IsSupported, Is.False); } diff --git a/Ramstack.Globbing/Internal/PathHelper.cs b/Ramstack.Globbing/Internal/PathHelper.cs index 2cff6c5..a03a128 100644 --- a/Ramstack.Globbing/Internal/PathHelper.cs +++ b/Ramstack.Globbing/Internal/PathHelper.cs @@ -334,7 +334,7 @@ public PathSegmentIterator() => while ((int)_position < length) { - if ((Avx2.IsSupported || Sse2.IsSupported || AdvSimd.IsSupported) && _mask != 0) + if ((Avx2.IsSupported || Sse2.IsSupported || AdvSimd.Arm64.IsSupported) && _mask != 0) { var offset = BitOperations.TrailingZeroCount(_mask); if (AdvSimd.IsSupported) @@ -446,8 +446,7 @@ public PathSegmentIterator() => if (_mask == 0) _position += Vector128.Count; } - #if NET7_0_OR_GREATER - else if (AdvSimd.IsSupported && (int)_position + Vector128.Count <= length) + else if (AdvSimd.Arm64.IsSupported && (int)_position + Vector128.Count <= length) { var chunk = LoadVector128(ref source, _position); var backslashMask = CreateBackslash128Bitmask(flags); @@ -466,15 +465,24 @@ public PathSegmentIterator() => // This avoids reloading SIMD registers and repeating comparisons // on the same chunk of data. // - _mask = comparison.ExtractMostSignificantBits(); + _mask = ExtractMostSignificantBits(comparison); // // Advance position to the next chunk when no separators found // if (_mask == 0) _position += Vector128.Count; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static uint ExtractMostSignificantBits(Vector128 v) + { + var sum = AdvSimd.Arm64.AddAcross( + AdvSimd.ShiftLogical( + AdvSimd.And(v, Vector128.Create((ushort)0x8000)), + Vector128.Create((short)-15, -14, -13, -12, -11, -10, -9, -8))); + return sum.ToScalar(); + } } - #endif else { for (; (int)_position < length; _position++) diff --git a/Ramstack.Globbing/Ramstack.Globbing.csproj b/Ramstack.Globbing/Ramstack.Globbing.csproj index 78e0e99..9be81d9 100644 --- a/Ramstack.Globbing/Ramstack.Globbing.csproj +++ b/Ramstack.Globbing/Ramstack.Globbing.csproj @@ -1,6 +1,6 @@  - net6.0;net7.0 + net6.0 Fast and zero-allocation .NET globbing library for matching file paths using glob patterns. enable enable