Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/7.0] Fix FormattingHelpers.CountDigits(UInt128) and add more tests for Int128/UInt128.ToString #74536

Merged
merged 1 commit into from
Aug 25, 2022
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 @@ -14,22 +14,37 @@ public static int CountDigits(UInt128 value)
{
ulong upper = value.Upper;

if (upper < 5)
// 1e19 is 8AC7_2304_89E8_0000
// 1e20 is 5_6BC7_5E2D_6310_0000
// 1e21 is 36_35C9_ADC5_DEA0_0000

if (upper == 0)
{
// We have less than 64-bits, so just return the lower count
return CountDigits(value.Lower);
}

int digits = 19;
// We have more than 1e19, so we have at least 20 digits
int digits = 20;

if (upper > 5)
{
digits++;
// ((2^128) - 1) / 1e20 < 34_02_823_669_209_384_635 which
// is 18.5318 digits, meaning the result definitely fits
// into 64-bits and we only need to add the lower digit count

value /= new UInt128(0x5, 0x6BC7_5E2D_6310_0000); // value /= 1e20
Debug.Assert(value.Upper == 0);

digits += CountDigits(value.Lower);
}
else if (value.Lower >= 0x6BC75E2D63100000)
else if ((upper == 5) && (value.Lower >= 0x6BC75E2D63100000))
{
// We're greater than 1e20, but definitely less than 1e21
// so we have exactly 21 digits

digits++;
Debug.Assert(digits == 21);
}

return digits;
Expand Down
6 changes: 6 additions & 0 deletions src/libraries/System.Runtime/tests/System/Int128Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public static IEnumerable<object[]> ToString_TestData()
yield return new object[] { (Int128)(-4567), defaultSpecifier, defaultFormat, "-4567" };
yield return new object[] { (Int128)0, defaultSpecifier, defaultFormat, "0" };
yield return new object[] { (Int128)4567, defaultSpecifier, defaultFormat, "4567" };
yield return new object[] { new Int128(0x0000_0000_0000_0001, 0x0000_0000_0000_0003), defaultSpecifier, defaultFormat, "18446744073709551619" };
yield return new object[] { new Int128(0x0000_0000_0000_0001, 0x0000_0000_0000_000A), defaultSpecifier, defaultFormat, "18446744073709551626" };
yield return new object[] { new Int128(0x0000_0000_0000_0005, 0x0000_0000_0000_0001), defaultSpecifier, defaultFormat, "92233720368547758081" };
yield return new object[] { new Int128(0x0000_0000_0000_0005, 0x6BC7_5E2D_6310_0000), defaultSpecifier, defaultFormat, "100000000000000000000" };
yield return new object[] { new Int128(0x0000_0000_0000_0036, 0x35C9_ADC5_DEA0_0000), defaultSpecifier, defaultFormat, "1000000000000000000000" };
yield return new object[] { new Int128(0x0013_4261_72C7_4D82, 0x2B87_8FE8_0000_0000), defaultSpecifier, defaultFormat, "100000000000000000000000000000000000" };
yield return new object[] { Int128.MaxValue, defaultSpecifier, defaultFormat, "170141183460469231731687303715884105727" };
}

Expand Down
8 changes: 8 additions & 0 deletions src/libraries/System.Runtime/tests/System/UInt128Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public static IEnumerable<object[]> ToString_TestData()
{
yield return new object[] { (UInt128)0, defaultSpecifier, defaultFormat, "0" };
yield return new object[] { (UInt128)4567, defaultSpecifier, defaultFormat, "4567" };
yield return new object[] { new UInt128(0x0000_0000_0000_0001, 0x0000_0000_0000_0003), defaultSpecifier, defaultFormat, "18446744073709551619" };
yield return new object[] { new UInt128(0x0000_0000_0000_0001, 0x0000_0000_0000_000A), defaultSpecifier, defaultFormat, "18446744073709551626" };
yield return new object[] { new UInt128(0x0000_0000_0000_0005, 0x0000_0000_0000_0001), defaultSpecifier, defaultFormat, "92233720368547758081" };
yield return new object[] { new UInt128(0x0000_0000_0000_0005, 0x6BC7_5E2D_6310_0000), defaultSpecifier, defaultFormat, "100000000000000000000" };
yield return new object[] { new UInt128(0x0000_0000_0000_0036, 0x35C9_ADC5_DEA0_0000), defaultSpecifier, defaultFormat, "1000000000000000000000" };
yield return new object[] { new UInt128(0x0013_4261_72C7_4D82, 0x2B87_8FE8_0000_0000), defaultSpecifier, defaultFormat, "100000000000000000000000000000000000" };
yield return new object[] { new UInt128(0x7FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF), defaultSpecifier, defaultFormat, "170141183460469231731687303715884105727" };
yield return new object[] { new UInt128(0x8000_0000_0000_0000, 0x0000_0000_0000_0000), defaultSpecifier, defaultFormat, "170141183460469231731687303715884105728" };
yield return new object[] { UInt128.MaxValue, defaultSpecifier, defaultFormat, "340282366920938463463374607431768211455" };
}

Expand Down