Skip to content

Commit 92b0d26

Browse files
committed
Respond to PR feedback
1 parent 6a930d9 commit 92b0d26

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/libraries/System.Private.CoreLib/src/System/Number.BigInteger.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ public static void Pow10(uint exponent, out BigInteger result)
928928
ref BigInteger product = ref temp2;
929929

930930
exponent >>= 3;
931-
uint index = 0;
931+
int index = 0;
932932

933933
while (exponent != 0)
934934
{
@@ -938,7 +938,14 @@ public static void Pow10(uint exponent, out BigInteger result)
938938
// Multiply into the next temporary
939939
unsafe
940940
{
941-
ref BigInteger rhs = ref Unsafe.As<uint, BigInteger>(ref Unsafe.AsRef(in Pow10BigNumTable[Pow10BigNumTableIndices[(int)index]]));
941+
// It is safe to reinterpret the memory at the indexed position of Pow10BigNumTable as a BigInteger,
942+
// because Pow10BigNumTable is laid out such that each indexed position contains a valid BigInteger
943+
// representation with the correct structure and alignment.
944+
945+
int pow10BigNumTableIndex = Pow10BigNumTableIndices[index];
946+
Debug.Assert((pow10BigNumTableIndex + sizeof(BigInteger)) < Pow10BigNumTable.Length);
947+
948+
ref BigInteger rhs = ref Unsafe.As<uint, BigInteger>(ref Unsafe.AsRef(in Pow10BigNumTable[pow10BigNumTableIndex]));
942949
Multiply(ref lhs, ref rhs, out product);
943950
}
944951

src/libraries/System.Private.CoreLib/src/System/Number.Dragon4.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ private static uint Dragon4(ulong mantissa, int exponent, uint mantissaHighBitId
7777

7878
// For normalized IEEE floating-point values, each time the exponent is incremented the margin also doubles.
7979
// That creates a subset of transition numbers where the high margin is twice the size of the low margin.
80-
scoped ref BigInteger scaledMarginHigh = ref Unsafe.NullRef<BigInteger>();
8180
BigInteger optionalMarginHigh;
8281

8382
if (hasUnequalMargins)
8483
{
84+
// The high and low margins are different
85+
8586
if (exponent > 0) // We have no fractional component
8687
{
8788
// 1) Expand the input value by multiplying out the mantissa and exponent.
@@ -118,12 +119,11 @@ private static uint Dragon4(ulong mantissa, int exponent, uint mantissaHighBitId
118119
// scaledMarginHigh = 2 * 2 * 2^(-1)
119120
BigInteger.SetUInt32(out optionalMarginHigh, 2);
120121
}
121-
122-
// The high and low margins are different
123-
scaledMarginHigh = ref optionalMarginHigh;
124122
}
125123
else
126124
{
125+
// The high and low margins are equal
126+
127127
if (exponent > 0) // We have no fractional component
128128
{
129129
// 1) Expand the input value by multiplying out the mantissa and exponent.
@@ -155,10 +155,13 @@ private static uint Dragon4(ulong mantissa, int exponent, uint mantissaHighBitId
155155
BigInteger.SetUInt32(out scaledMarginLow, 1);
156156
}
157157

158-
// The high and low margins are equal
159-
scaledMarginHigh = ref scaledMarginLow;
158+
// This is unused for this path, but we need it viewed as "initialized" so the
159+
// scaledMarginHigh tracking works as expected.
160+
Unsafe.SkipInit(out optionalMarginHigh);
160161
}
161162

163+
scoped ref BigInteger scaledMarginHigh = ref (hasUnequalMargins ? ref optionalMarginHigh : ref scaledMarginLow);
164+
162165
// Compute an estimate for digitExponent that will be correct or undershoot by one.
163166
//
164167
// This optimization is based on the paper "Printing Floating-Point Numbers Quickly and Accurately" by Burger and Dybvig http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.72.4656&rep=rep1&type=pdf

src/libraries/System.Private.CoreLib/src/System/Number.NumberToFloatingPointBits.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ private static ulong ConvertBigIntegerToFloatingPointBits<TFloat>(ref BigInteger
881881
hasZeroTail &= (bottomBlock & unusedBottomBlockBitsMask) == 0;
882882
}
883883

884-
for (int i = 0; i != bottomBlockIndex; i++)
884+
for (int i = 0; i < bottomBlockIndex; i++)
885885
{
886886
hasZeroTail &= (value.GetBlock(i) == 0);
887887
}

0 commit comments

Comments
 (0)