-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Numerics
Milestone
Description
Background and motivation
.NET 9 lacks BigMul
API for nuint
for some reason.
BigMul
is useful for implementing something like multiplicative hashing, decoding factoradic with fixed max input value, Lemire's algorithm
for generating bounded random integers efficiently, and fast fraction multiplications (necessary for something like MemoryMarshal.Cast<Vector4, Vector3>()
).
API Proposal
namespace System
{
public readonly struct UIntPtr
{
public static nuint BigMul(nuint a, nuint b, out nuint low);
}
public readonly struct IntPtr
{
public static nint BigMul(nint a, nint b, out nint low);
}
}
Approved API
- Changed
(a, b, out low)
to(left, right, out lower)
to match the newer versions of these methods. - Exposed the UInt128 and Int128 versions, too (because we thought they already were public)
namespace System
{
public readonly partial struct UIntPtr
{
public static nuint BigMul(nuint left, nuint right, out nuint lower);
}
public readonly partial struct IntPtr
{
public static nint BigMul(nint left, nint right, out nint lower);
}
public readonly partial struct UInt128
{
public static UInt128 BigMul(UInt128 left, UInt128 right, out UInt128 lower);
}
public readonly partial struct Int128
{
public static Int128 BigMul(Int128 left, Int128 right, out Int128 lower);
}
}
API Usage
var sb = new StringBuilder("0.");
var low = ~(nuint)0;
while (low > 0)
{
sb.Append($"{nuint.BigMul(10, low, out low)}");
}
Console.Out.WriteLine(sb);
Alternative Designs
Risks
None
Daniel-Svensson
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Numerics