Skip to content

Commit 2f4e87a

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

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
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+
uint carry = BigIntegerCalculator.LeftShiftSelf(zd, smallShift);
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+
uint carry = BigIntegerCalculator.RightShiftSelf(zd, smallShift);
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);
50935090

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

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

Lines changed: 10 additions & 8 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+
uint carry = LeftShiftSelf(bits, smallShift);
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+
uint carry = RightShiftSelf(bits, smallShift);
5856
bits[^1] |= carry;
5957

6058
if (digitShift == 0)
@@ -98,33 +96,37 @@ 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 uint LeftShiftSelf(Span<uint> bits, int shift)
102100
{
103101
Debug.Assert((uint)shift < 32);
104102
if (shift == 0)
105-
return;
103+
return 0;
106104

105+
uint carry = 0;
107106
int back = 32 - shift;
108107
for (int i = 0; i < bits.Length; i++)
109108
{
110109
uint value = carry | bits[i] << shift;
111110
carry = bits[i] >> back;
112111
bits[i] = value;
113112
}
113+
return carry;
114114
}
115-
public static void RightShiftSelf(Span<uint> bits, int shift, ref uint carry)
115+
public static uint RightShiftSelf(Span<uint> bits, int shift)
116116
{
117117
Debug.Assert((uint)shift < 32);
118118
if (shift == 0)
119-
return;
119+
return 0;
120120

121+
uint carry = 0;
121122
int back = 32 - shift;
122123
for (int i = bits.Length - 1; i >= 0; i--)
123124
{
124125
uint value = carry | bits[i] >> shift;
125126
carry = bits[i] << back;
126127
bits[i] = value;
127128
}
129+
return carry;
128130
}
129131
}
130132
}

0 commit comments

Comments
 (0)