From 7736bd544c3dffe7f078b1118a2c07c070d8f0da Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 2 Oct 2026 13:05:39 +0900 Subject: [PATCH] =?UTF-8?q?GPU=20=E3=81=AE=E8=AA=A4=E5=B7=AE=E3=81=AE?= =?UTF-8?q?=E3=83=A2=E3=83=87=E3=83=AB=E3=81=A8=20ULP=20=E3=81=AE=E8=B7=9D?= =?UTF-8?q?=E9=9B=A2=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/ComputeErrorModel.cs | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ComputeGuard/ComputeErrorModel.cs diff --git a/ComputeGuard/ComputeErrorModel.cs b/ComputeGuard/ComputeErrorModel.cs new file mode 100644 index 0000000..ecd1d47 --- /dev/null +++ b/ComputeGuard/ComputeErrorModel.cs @@ -0,0 +1,46 @@ +namespace ComputeGuard; + +internal static class ComputeErrorModel +{ + public static readonly double GpuOperationError = Math.ScaleB(1d, -23); + + public static readonly double CpuOperationError = Math.ScaleB(1d, -24); + + public static readonly double UnitInLastPlace = Math.ScaleB(1d, -23); + + public static readonly double FlushLimit = Math.ScaleB(1d, -126); + + public static double Gamma(int operations, double operationError) + { + ArgumentOutOfRangeException.ThrowIfNegative(operations); + + if (!double.IsFinite(operationError) || operationError < 0d) + { + throw new ArgumentOutOfRangeException(nameof(operationError)); + } + + var product = operations * operationError; + if (product >= 1d) + { + throw new ArgumentOutOfRangeException(nameof(operations)); + } + + return product / (1d - product); + } + + public static long UlpDistance(float first, float second) + { + if (float.IsNaN(first) || float.IsNaN(second)) + { + return long.MaxValue; + } + + return Math.Abs((long)Ordered(first) - Ordered(second)); + } + + private static int Ordered(float value) + { + var bits = BitConverter.SingleToInt32Bits(value); + return bits < 0 ? int.MinValue - bits : bits; + } +}