Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BigMul in BigMul #100918

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ static ulong SoftwareFallback(ulong a, ulong b, out ulong low)
uint bl = (uint)b;
uint bh = (uint)(b >> 32);

ulong mull = ((ulong)al) * bl;
ulong t = ((ulong)ah) * bl + (mull >> 32);
ulong tl = ((ulong)al) * bh + (uint)t;
ulong mull = BigMul(al, bl);
tannergooding marked this conversation as resolved.
Show resolved Hide resolved
ulong t = BigMul(ah, bl) + (mull >> 32);
ulong tl = BigMul(al, bh) + (uint)t;

low = tl << 32 | (uint)mull;

return ((ulong)ah) * bh + (t >> 32) + (tl >> 32);
return BigMul(ah, bh) + (t >> 32) + (tl >> 32);
}
}

Expand All @@ -236,13 +236,23 @@ public static long BigMul(long a, long b, out long low)
return (long)high - ((a >> 63) & b) - ((b >> 63) & a);
}

/// <summary>Produces the full product of two unsigned 64-bit numbers.</summary>
/// <param name="a">The first number to multiply.</param>
/// <param name="b">The second number to multiply.</param>
/// <returns>The full product of the specified numbers.</returns>
internal static UInt128 BigMul(ulong a, ulong b)
{
ulong high = BigMul(a, b, out ulong low);
return new UInt128(high, low);
}

/// <summary>Produces the full product of two 64-bit numbers.</summary>
/// <param name="a">The first number to multiply.</param>
/// <param name="b">The second number to multiply.</param>
/// <returns>The full product of the specified numbers.</returns>
internal static Int128 BigMul(long a, long b)
{
long high = Math.BigMul(a, b, out long low);
long high = BigMul(a, b, out long low);
return new Int128((ulong)high, (ulong)low);
}

Expand Down
16 changes: 8 additions & 8 deletions src/libraries/System.Private.CoreLib/src/System/UInt128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,18 +1396,18 @@ internal static UInt128 BigMul(UInt128 left, UInt128 right, out UInt128 lower)
// Basically, it's an optimized version of FOIL method applied to
// low and high qwords of each operand

UInt128 al = left._lower;
UInt128 ah = left._upper;
ulong al = left._lower;
ulong ah = left._upper;

UInt128 bl = right._lower;
UInt128 bh = right._upper;
ulong bl = right._lower;
ulong bh = right._upper;

UInt128 mull = al * bl;
UInt128 t = ah * bl + mull._upper;
UInt128 tl = al * bh + t._lower;
UInt128 mull = Math.BigMul(al, bl);
UInt128 t = Math.BigMul(ah, bl) + mull._upper;
UInt128 tl = Math.BigMul(al, bh) + t._lower;

lower = new UInt128(tl._lower, mull._lower);
return ah * bh + t._upper + tl._upper;
return Math.BigMul(ah, bh) + t._upper + tl._upper;
}

//
Expand Down