Skip to content

Commit e9be820

Browse files
authored
Merge pull request #66 from CommunityToolkit/dev/vectorized-nint-count
Add vectorized Count<T> support for nint/nuint
2 parents 4788f70 + 4109697 commit e9be820

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

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

Lines changed: 29 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

@@ -229,7 +240,11 @@ private static nint CountSimd<T>(ref T r0, nint length, T value)
229240
offset += Vector<T>.Count;
230241
}
231242

243+
#if NET6_0_OR_GREATER
244+
result += CastToNativeInt(Vector.Sum(partials));
245+
#else
232246
result += CastToNativeInt(Vector.Dot(partials, Vector<T>.One));
247+
#endif
233248
length -= offset - initialOffset;
234249
}
235250
while (length >= Vector<T>.Count);
@@ -320,6 +335,13 @@ private static unsafe nint GetUpperBound<T>()
320335
return (nint)(void*)long.MaxValue;
321336
}
322337

338+
#if NET6_0_OR_GREATER
339+
if (typeof(T) == typeof(nint))
340+
{
341+
return nint.MaxValue;
342+
}
343+
#endif
344+
323345
throw null!;
324346
}
325347

@@ -353,6 +375,13 @@ private static nint CastToNativeInt<T>(T value)
353375
return (nint)(ulong)(long)(object)value;
354376
}
355377

378+
#if NET6_0_OR_GREATER
379+
if (typeof(T) == typeof(nint))
380+
{
381+
return (nint)(object)value;
382+
}
383+
#endif
384+
356385
throw null!;
357386
}
358387
}

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)