Files
UVtools/UVtools.Core/Extensions/SpanExtensions.cs
T
Tiago Conceição 5fdac82dd6 Bene4 Mono and Layer Issue Z Tracker
* (Add) Settings & Issues: Enable or disable Empty Layers
* (Add) PrusaSlicer Printer: Nova Bene4 Mono
* (Add) CWS: Support the GRAY2RGB and RBG2GRAY encoding for Bene Mono
* (Add) Layer issue Z map paired with layer navigation tracker bar
* (Change) Shortcuts: + and - to go up and down on layers were change to W and S keys. Reason: + and - are bound to zoom and can lead to problems
* (Upgrade) OpenCV from 4.2 to 4.3
* (Fix) CWS: Add missing Platform X,Y,Z size when converting from SL1
* (Fix) CWS: Invert XY resolution when converting from SL1
2020-09-05 06:56:20 +01:00

42 lines
1.4 KiB
C#

/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
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());
}
}
}
}