Skip to content

Commit

Permalink
Integrated fixed precision types with RandomRanges.tt.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4rs-mt committed Feb 1, 2024
1 parent 334d9b6 commit 8cd83f0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
28 changes: 26 additions & 2 deletions Src/ILGPU.Algorithms/Random/RandomExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2021-2023 ILGPU Project
// Copyright (c) 2021-2024 ILGPU Project
// www.ilgpu.net
//
// File: RandomExtensions.cs
Expand All @@ -21,7 +21,7 @@ namespace ILGPU.Algorithms.Random
/// <summary>
/// Represents useful helpers for random generators.
/// </summary>
public static class RandomExtensions
public static partial class RandomExtensions
{
/// <summary>
/// 1.0 / int.MaxValue
Expand Down Expand Up @@ -176,6 +176,30 @@ public static long Next<TRandomProvider>(
return Math.Min(intermediate + minValue, maxValue - 1);
}

/// <summary>
/// Generates a random long in [minValue..maxValue).
/// </summary>
/// <param name="randomProvider">The random provider.</param>
/// <param name="minValue">The minimum value (inclusive).</param>
/// <param name="maxValue">The maximum values (exclusive).</param>
/// <returns>A random long in [minValue..maxValue).</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Next<TRandomProvider>(
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
/// <summary>
/// Generates a new random vector containing provided RNG-based values.
Expand Down
8 changes: 6 additions & 2 deletions Src/ILGPU.Algorithms/Random/RandomRanges.tt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<string, string>()
{
{ "Int8", "(byte)randomProvider.Next(0, byte.MaxValue)" },
Expand All @@ -29,6 +32,7 @@ var functionMapping = new Dictionary<string, string>()
{ "Double", "randomProvider.NextDouble()" },
};
#>
using ILGPU.Algorithms.FixedPrecision;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
Expand Down

0 comments on commit 8cd83f0

Please sign in to comment.