Skip to content

Commit f0733dd

Browse files
committed
Refactor BigIntegerCalculator.*ShiftSelf
1 parent a2f57a6 commit f0733dd

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,8 +2533,7 @@ public static implicit operator BigInteger(nuint value)
25332533

25342534
bits.CopyTo(zd);
25352535

2536-
uint carry = 0;
2537-
BigIntegerCalculator.LeftShiftSelf(zd, smallShift, ref carry);
2536+
BigIntegerCalculator.LeftShiftSelf(zd, smallShift, out uint carry);
25382537

25392538
Debug.Assert(carry == over);
25402539
Debug.Assert(z[^1] != 0);
@@ -2612,8 +2611,7 @@ private static BigInteger LeftShift(int value, int digitShift, int smallShift)
26122611
zd[^1] = 0;
26132612
bits.Slice(digitShift).CopyTo(zd);
26142613

2615-
uint carry = 0;
2616-
BigIntegerCalculator.RightShiftSelf(zd, smallShift, ref carry);
2614+
BigIntegerCalculator.RightShiftSelf(zd, smallShift, out uint carry);
26172615

26182616
bool neg = value._sign < 0;
26192617
if (neg && (carry != 0 || bits.Slice(0, digitShift).ContainsAnyExcept(0u)))
@@ -5088,8 +5086,7 @@ static bool INumberBase<BigInteger>.TryConvertToTruncating<TOther>(BigInteger va
50885086
}
50895087
}
50905088

5091-
uint carry = 0;
5092-
BigIntegerCalculator.RightShiftSelf(zd, smallShift, ref carry);
5089+
BigIntegerCalculator.RightShiftSelf(zd, smallShift, out _);
50935090

50945091
BigInteger result = new BigInteger(zd, false);
50955092

src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.ShiftRot.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public static void RotateLeft(Span<uint> bits, int digitShift, int smallShift)
3939
{
4040
Debug.Assert(bits.Length > 0);
4141

42-
uint carry = 0;
43-
LeftShiftSelf(bits, smallShift, ref carry);
42+
LeftShiftSelf(bits, smallShift, out uint carry);
4443
bits[0] |= carry;
4544

4645
if (digitShift == 0)
@@ -53,8 +52,7 @@ public static void RotateRight(Span<uint> bits, int digitShift, int smallShift)
5352
{
5453
Debug.Assert(bits.Length > 0);
5554

56-
uint carry = 0;
57-
RightShiftSelf(bits, smallShift, ref carry);
55+
RightShiftSelf(bits, smallShift, out uint carry);
5856
bits[^1] |= carry;
5957

6058
if (digitShift == 0)
@@ -98,9 +96,11 @@ stackalloc uint[StackAllocThreshold]
9896
ArrayPool<uint>.Shared.Return(tmpFromPool);
9997
}
10098

101-
public static void LeftShiftSelf(Span<uint> bits, int shift, ref uint carry)
99+
public static void LeftShiftSelf(Span<uint> bits, int shift, out uint carry)
102100
{
103101
Debug.Assert((uint)shift < 32);
102+
103+
carry = 0;
104104
if (shift == 0)
105105
return;
106106

@@ -112,9 +112,11 @@ public static void LeftShiftSelf(Span<uint> bits, int shift, ref uint carry)
112112
bits[i] = value;
113113
}
114114
}
115-
public static void RightShiftSelf(Span<uint> bits, int shift, ref uint carry)
115+
public static void RightShiftSelf(Span<uint> bits, int shift, out uint carry)
116116
{
117117
Debug.Assert((uint)shift < 32);
118+
119+
carry = 0;
118120
if (shift == 0)
119121
return;
120122

0 commit comments

Comments
 (0)