-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backported some performance improvements from .NET 9. Fix for multi u…
…tf16 unicode code points.
- Loading branch information
Showing
3 changed files
with
84 additions
and
53 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
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,64 @@ | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace ModTek.Features.Logging; | ||
|
||
// copied from .NET 9 | ||
internal static class FormattingHelpers | ||
{ | ||
internal static unsafe void WriteDigits(byte* positionPtr, long value, int digits) | ||
{ | ||
const byte AsciiZero = (byte)'0'; | ||
|
||
byte* current; | ||
for (current = positionPtr + digits - 1; current >= positionPtr; current--) | ||
{ | ||
var temp = value + AsciiZero; | ||
value /= 10; | ||
*current = (byte)(temp - (value * 10)); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static int CountDigits(uint value) | ||
{ | ||
var tableValue = s_countDigitsTable[(uint)BitOperations.Log2(value)]; | ||
return (int)((value + tableValue) >> 32); | ||
} | ||
// Algorithm based on https://lemire.me/blog/2021/06/03/computing-the-number-of-digits-of-an-integer-even-faster. | ||
private static readonly long[] s_countDigitsTable = | ||
[ | ||
4294967296, | ||
8589934582, | ||
8589934582, | ||
8589934582, | ||
12884901788, | ||
12884901788, | ||
12884901788, | ||
17179868184, | ||
17179868184, | ||
17179868184, | ||
21474826480, | ||
21474826480, | ||
21474826480, | ||
21474826480, | ||
25769703776, | ||
25769703776, | ||
25769703776, | ||
30063771072, | ||
30063771072, | ||
30063771072, | ||
34349738368, | ||
34349738368, | ||
34349738368, | ||
34349738368, | ||
38554705664, | ||
38554705664, | ||
38554705664, | ||
41949672960, | ||
41949672960, | ||
41949672960, | ||
42949672960, | ||
42949672960, | ||
]; | ||
} |