From 4cea09c6b8f354463d9f08df3dbbd25b89649e3d Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 2 Oct 2026 13:06:53 +0900 Subject: [PATCH] =?UTF-8?q?=E6=8A=9C=E3=81=8D=E5=8F=96=E3=82=8A=E3=81=AE?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E3=82=92=E6=B1=BA=E3=82=81=E3=82=8B=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ComputeGuard/ComputeSampling.cs | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 ComputeGuard/ComputeSampling.cs diff --git a/ComputeGuard/ComputeSampling.cs b/ComputeGuard/ComputeSampling.cs new file mode 100644 index 0000000..b6dace2 --- /dev/null +++ b/ComputeGuard/ComputeSampling.cs @@ -0,0 +1,54 @@ +namespace ComputeGuard; + +internal readonly record struct ComputeSample(int X, int Y); + +internal static class ComputeSampling +{ + public const int FixedCount = 5; + + private const ulong Increment = 0x9E3779B97F4A7C15UL; + private const ulong FirstMultiplier = 0xBF58476D1CE4E5B9UL; + private const ulong SecondMultiplier = 0x94D049BB133111EBUL; + private const int FirstShift = 30; + private const int SecondShift = 27; + private const int ThirdShift = 31; + private const int HalfBits = 32; + private const ulong LowerHalf = 0xFFFFFFFFUL; + + public static void Fill(ulong key, int width, int height, Span destination) + { + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height); + + for (var index = 0; index < destination.Length; index++) + { + destination[index] = index switch + { + 0 => new ComputeSample(0, 0), + 1 => new ComputeSample(width - 1, 0), + 2 => new ComputeSample(0, height - 1), + 3 => new ComputeSample(width - 1, height - 1), + 4 => new ComputeSample(width / 2, height / 2), + _ => Hashed(key, index, width, height), + }; + } + } + + private static ComputeSample Hashed(ulong key, int index, int width, int height) + { + var state = Mix(key + (ulong)index * Increment); + var x = (int)(((state >> HalfBits) * (ulong)width) >> HalfBits); + var y = (int)(((state & LowerHalf) * (ulong)height) >> HalfBits); + return new ComputeSample(x, y); + } + + private static ulong Mix(ulong value) + { + value ^= value >> FirstShift; + value *= FirstMultiplier; + value ^= value >> SecondShift; + value *= SecondMultiplier; + value ^= value >> ThirdShift; + return value; + } +}