Skip to content

Commit 6d7bf0d

Browse files
committed
Add vectorized Count<T> support for nint/nuint
1 parent 4788f70 commit 6d7bf0d

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

CommunityToolkit.HighPerformance/Helpers/Internals/SpanHelper.Count.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ public static nint Count<T>(ref T r0, nint length, T value)
6969
return CountSimd(ref r1, length, target);
7070
}
7171

72+
#if NET6_0_OR_GREATER
73+
if (typeof(T) == typeof(nint) ||
74+
typeof(T) == typeof(nuint))
75+
{
76+
ref nint r1 = ref Unsafe.As<T, nint>(ref r0);
77+
nint target = Unsafe.As<T, nint>(ref value);
78+
79+
return CountSimd(ref r1, length, target);
80+
}
81+
#endif
82+
7283
return CountSequential(ref r0, length, value);
7384
}
7485

@@ -320,6 +331,13 @@ private static unsafe nint GetUpperBound<T>()
320331
return (nint)(void*)long.MaxValue;
321332
}
322333

334+
#if NET6_0_OR_GREATER
335+
if (typeof(T) == typeof(nint))
336+
{
337+
return nint.MaxValue;
338+
}
339+
#endif
340+
323341
throw null!;
324342
}
325343

@@ -353,6 +371,13 @@ private static nint CastToNativeInt<T>(T value)
353371
return (nint)(ulong)(long)(object)value;
354372
}
355373

374+
#if NET6_0_OR_GREATER
375+
if (typeof(T) == typeof(nint))
376+
{
377+
return (nint)(object)value;
378+
}
379+
#endif
380+
356381
throw null!;
357382
}
358383
}

tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ReadOnlySpanExtensions.Count.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Runtime.InteropServices;
7-
using CommunityToolkit.HighPerformance;
87
using CommunityToolkit.HighPerformance.UnitTests.Buffers.Internals;
98
using Microsoft.VisualStudio.TestTools.UnitTesting;
109

@@ -49,6 +48,15 @@ public void Test_ReadOnlySpanExtensions_RandomCount64()
4948
TestForType(Math.PI, CreateRandomData);
5049
}
5150

51+
#if NET6_0_OR_GREATER
52+
[TestMethod]
53+
public void Test_ReadOnlySpanExtensions_RandomCountPtr()
54+
{
55+
TestForType(nint.MaxValue / 2, CreateRandomData);
56+
TestForType(nuint.MaxValue / 2, CreateRandomData);
57+
}
58+
#endif
59+
5260
[TestMethod]
5361
public void Test_ReadOnlySpanExtensions_RandomCountManaged()
5462
{
@@ -148,6 +156,15 @@ public void Test_ReadOnlySpanExtensions_FilledCount64()
148156
TestForType((ulong)47128480128401, (count, _) => CreateFilledData(count, (ulong)47128480128401));
149157
}
150158

159+
#if NET6_0_OR_GREATER
160+
[TestMethod]
161+
public void Test_ReadOnlySpanExtensions_FilledCountPtr()
162+
{
163+
TestForType((nint)37438941, (count, _) => CreateFilledData(count, (nint)37438941));
164+
TestForType((nuint)37438941, (count, _) => CreateFilledData(count, (nuint)37438941));
165+
}
166+
#endif
167+
151168
/// <summary>
152169
/// Performs a test for a specified type.
153170
/// </summary>

0 commit comments

Comments
 (0)