Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'UVtools.Core/Extensions/SpanExtensions.cs')
-rw-r--r--UVtools.Core/Extensions/SpanExtensions.cs34
1 files changed, 34 insertions, 0 deletions
diff --git a/UVtools.Core/Extensions/SpanExtensions.cs b/UVtools.Core/Extensions/SpanExtensions.cs
new file mode 100644
index 0000000..0a9ffeb
--- /dev/null
+++ b/UVtools.Core/Extensions/SpanExtensions.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Runtime.CompilerServices;
+using System.Threading.Tasks;
+
+namespace UVtools.Core.Extensions
+{
+ public static class SpanExtensions
+ {
+ public static unsafe void Fill<T>(this Span<T> span, Func<T> provider) where T : struct
+ {
+ int
+ cores = Environment.ProcessorCount,
+ batch = span.Length / cores,
+ mod = span.Length % cores,
+ size = Unsafe.SizeOf<T>();
+ ref T r0 = ref span.GetPinnableReference();
+ fixed (byte* p0 = &Unsafe.As<T, byte>(ref r0))
+ {
+ byte* p = p0;
+ Parallel.For(0, cores, i =>
+ {
+ byte* pi = p + i * batch * size;
+ for (int j = 0; j < batch; j++, pi += size)
+ Unsafe.Write(pi, provider());
+ });
+
+ // Remaining values
+ if (mod < 1) return;
+ for (int i = span.Length - mod; i < span.Length; i++)
+ Unsafe.Write(p + i * size, provider());
+ }
+ }
+ }
+}