diff --git a/Src/ILGPU.Algorithms/Random/RandomExtensions.cs b/Src/ILGPU.Algorithms/Random/RandomExtensions.cs index 9c60c05f47..f5a3e22e7f 100644 --- a/Src/ILGPU.Algorithms/Random/RandomExtensions.cs +++ b/Src/ILGPU.Algorithms/Random/RandomExtensions.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU Algorithms -// Copyright (c) 2021-2023 ILGPU Project +// Copyright (c) 2021-2024 ILGPU Project // www.ilgpu.net // // File: RandomExtensions.cs @@ -21,7 +21,7 @@ namespace ILGPU.Algorithms.Random /// /// Represents useful helpers for random generators. /// - public static class RandomExtensions + public static partial class RandomExtensions { /// /// 1.0 / int.MaxValue @@ -176,6 +176,30 @@ public static long Next( return Math.Min(intermediate + minValue, maxValue - 1); } + /// + /// Generates a random long in [minValue..maxValue). + /// + /// The random provider. + /// The minimum value (inclusive). + /// The maximum values (exclusive). + /// A random long in [minValue..maxValue). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong Next( + ref TRandomProvider randomProvider, + ulong minValue, + ulong maxValue) + where TRandomProvider : struct, IRandomProvider + { + Debug.Assert(minValue < maxValue, "Values out of range"); + ulong dist = maxValue - minValue; + + // Check whether the bit range matches in theory + ulong intermediate = dist > 1UL << 23 + ? (ulong)(randomProvider.NextFloat() * dist) + : (ulong)(randomProvider.NextDouble() * dist); + return Math.Min(intermediate + minValue, maxValue - 1UL); + } + #if NET7_0_OR_GREATER /// /// Generates a new random vector containing provided RNG-based values. diff --git a/Src/ILGPU.Algorithms/Random/RandomRanges.tt b/Src/ILGPU.Algorithms/Random/RandomRanges.tt index 1282190e4c..66f54e0fd8 100644 --- a/Src/ILGPU.Algorithms/Random/RandomRanges.tt +++ b/Src/ILGPU.Algorithms/Random/RandomRanges.tt @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU Algorithms -// Copyright (c) 2023 ILGPU Project +// Copyright (c) 2023-2024 ILGPU Project // www.ilgpu.net // // File: RandomRanges.tt/RandomRanges.cs @@ -11,12 +11,15 @@ <#@ template debug="false" hostspecific="false" language="C#" #> <#@ include file="../TypeInformation.ttinclude"#> +<#@ include file="../FixedPrecision/FixedIntConfig.ttinclude"#> <#@ assembly name="System.Core" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ output extension=".cs" #> <# -var rngTypes = SignedIntTypes.Concat(FloatTypes); +var rngTypes = SignedIntTypes + .Concat(FloatTypes) + .Concat(FixedPrecisionIntTypes.SelectMany(t => t.ToBasicTypeInformation())); var functionMapping = new Dictionary() { { "Int8", "(byte)randomProvider.Next(0, byte.MaxValue)" }, @@ -29,6 +32,7 @@ var functionMapping = new Dictionary() { "Double", "randomProvider.NextDouble()" }, }; #> +using ILGPU.Algorithms.FixedPrecision; using System; using System.Diagnostics.CodeAnalysis; using System.Numerics;