Skip to content

Commit f453865

Browse files
authored
Use BigMul in BigMul (#100918)
* Use BigMul in BigMul * Reverse usage of BigMul(uint, uint) * Add aggressive inline to Math.BigMul(ulong, ulong)
1 parent b4f750f commit f453865

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/libraries/System.Private.CoreLib/src/System/Math.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,24 @@ public static long BigMul(long a, long b, out long low)
238238
return (long)high - ((a >> 63) & b) - ((b >> 63) & a);
239239
}
240240

241+
/// <summary>Produces the full product of two unsigned 64-bit numbers.</summary>
242+
/// <param name="a">The first number to multiply.</param>
243+
/// <param name="b">The second number to multiply.</param>
244+
/// <returns>The full product of the specified numbers.</returns>
245+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
246+
internal static UInt128 BigMul(ulong a, ulong b)
247+
{
248+
ulong high = BigMul(a, b, out ulong low);
249+
return new UInt128(high, low);
250+
}
251+
241252
/// <summary>Produces the full product of two 64-bit numbers.</summary>
242253
/// <param name="a">The first number to multiply.</param>
243254
/// <param name="b">The second number to multiply.</param>
244255
/// <returns>The full product of the specified numbers.</returns>
245256
internal static Int128 BigMul(long a, long b)
246257
{
247-
long high = Math.BigMul(a, b, out long low);
258+
long high = BigMul(a, b, out long low);
248259
return new Int128((ulong)high, (ulong)low);
249260
}
250261

src/libraries/System.Private.CoreLib/src/System/UInt128.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,18 +1396,18 @@ internal static UInt128 BigMul(UInt128 left, UInt128 right, out UInt128 lower)
13961396
// Basically, it's an optimized version of FOIL method applied to
13971397
// low and high qwords of each operand
13981398

1399-
UInt128 al = left._lower;
1400-
UInt128 ah = left._upper;
1399+
ulong al = left._lower;
1400+
ulong ah = left._upper;
14011401

1402-
UInt128 bl = right._lower;
1403-
UInt128 bh = right._upper;
1402+
ulong bl = right._lower;
1403+
ulong bh = right._upper;
14041404

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

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

14131413
//

0 commit comments

Comments
 (0)