forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize BigInteger formatting (dotnet#100181)
* Cleanup DecStr formatting * Cleanup ToNumber portion * Clean and pool base1E9 buffer * Split leaf span and array path * Post cherry-pick update * Remove unworthy positive/negative span/string split * Use stackalloc for buffers * Update constant usage * Use UInt32ToDecChars * Fix type assert * Fix strLength --------- Co-authored-by: Tanner Gooding <[email protected]>
- Loading branch information
1 parent
14e3f1d
commit 2e49497
Showing
8 changed files
with
179 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...es/System.Private.CoreLib/src/System/Buffers/Text/FormattingHelpers.CountDigits.Int128.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Buffers.Text | ||
{ | ||
internal static partial class FormattingHelpers | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static int CountDigits(UInt128 value) | ||
{ | ||
ulong upper = value.Upper; | ||
|
||
// 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); | ||
} | ||
|
||
// We have more than 1e19, so we have at least 20 digits | ||
int digits = 20; | ||
|
||
if (upper > 5) | ||
{ | ||
// ((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 ((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; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static int CountHexDigits(UInt128 value) | ||
{ | ||
// The number of hex digits is log16(value) + 1, or log2(value) / 4 + 1 | ||
return ((int)UInt128.Log2(value) >> 2) + 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.