Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -511,16 +511,23 @@ internal BigInteger(ReadOnlySpan<uint> value, bool negative)
{
this = default;
}
else if (value.Length == 1 && value[0] < kuMaskHighBit)
else if (value.Length == 1)
{
// Values like (Int32.MaxValue+1) are stored as "0x80000000" and as such cannot be packed into _sign
_sign = negative ? -(int)value[0] : (int)value[0];
_bits = null;
if (_sign == int.MinValue)
if (value[0] < kuMaskHighBit)
{
_sign = negative ? -(int)value[0] : (int)value[0];
_bits = null;
}
else if (negative && value[0] == kuMaskHighBit)
{
// Although Int32.MinValue fits in _sign, we represent this case differently for negate
this = s_bnMinInt;
}
else
{
_sign = negative ? -1 : +1;
_bits = [value[0]];
}
}
else
{
Expand Down
21 changes: 17 additions & 4 deletions src/libraries/System.Runtime.Numerics/tests/BigInteger/parse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,23 @@ private static void VerifyDefaultParse(Random random)
// BasicTests
VerifyFailParseToString(null, typeof(ArgumentNullException));
VerifyFailParseToString(string.Empty, typeof(FormatException));
VerifyParseToString("0");
VerifyParseToString("000");
VerifyParseToString("1");
VerifyParseToString("001");

foreach (var value in new string[]
{
"0",
"000",
"1",
"001",
int.MaxValue.ToString(),
int.MinValue.ToString(),
long.MaxValue.ToString(),
long.MinValue.ToString(),
Int128.MaxValue.ToString(),
Int128.MinValue.ToString(),
})
{
VerifyParseToString(value);
}

// SimpleNumbers - Small
for (int i = 0; i < s_samples; i++)
Expand Down