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

Fix mono regressions in Number.Formatting.cs #112050

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -272,7 +272,7 @@ internal static partial class Number

// Optimizations using "TwoDigits" inspired by:
// https://engineering.fb.com/2013/03/15/developer-tools/three-optimization-tips-for-c/
private static ReadOnlySpan<byte> TwoDigitsCharsAsBytes =>
private static readonly byte[] TwoDigitsCharsAsBytes =
MemoryMarshal.AsBytes<char>("00010203040506070809" +
"10111213141516171819" +
"20212223242526272829" +
Expand All @@ -282,9 +282,9 @@ internal static partial class Number
"60616263646566676869" +
"70717273747576777879" +
"80818283848586878889" +
"90919293949596979899");
private static ReadOnlySpan<byte> TwoDigitsBytes =>
"00010203040506070809"u8 +
"90919293949596979899").ToArray();
private static readonly byte[] TwoDigitsBytes =
("00010203040506070809"u8 +
"10111213141516171819"u8 +
"20212223242526272829"u8 +
"30313233343536373839"u8 +
Expand All @@ -293,7 +293,7 @@ internal static partial class Number
"60616263646566676869"u8 +
"70717273747576777879"u8 +
"80818283848586878889"u8 +
"90919293949596979899"u8;
"90919293949596979899"u8).ToArray();

public static unsafe string FormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info)
{
Expand Down Expand Up @@ -1572,7 +1572,7 @@ internal static unsafe void WriteTwoDigits<TChar>(uint value, TChar* ptr) where

Unsafe.CopyBlockUnaligned(
ref *(byte*)ptr,
ref Unsafe.Add(ref MemoryMarshal.GetReference(typeof(TChar) == typeof(char) ? TwoDigitsCharsAsBytes : TwoDigitsBytes), (uint)sizeof(TChar) * 2 * value),
ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(typeof(TChar) == typeof(char) ? TwoDigitsCharsAsBytes : TwoDigitsBytes), (uint)sizeof(TChar) * 2 * value),
(uint)sizeof(TChar) * 2);
}

Expand All @@ -1588,13 +1588,12 @@ internal static unsafe void WriteFourDigits<TChar>(uint value, TChar* ptr) where

(value, uint remainder) = Math.DivRem(value, 100);

ref byte charsArray = ref MemoryMarshal.GetReference(typeof(TChar) == typeof(char) ? TwoDigitsCharsAsBytes : TwoDigitsBytes);
ref byte charsArray = ref MemoryMarshal.GetArrayDataReference(typeof(TChar) == typeof(char) ? TwoDigitsCharsAsBytes : TwoDigitsBytes);

Unsafe.CopyBlockUnaligned(
ref *(byte*)ptr,
ref Unsafe.Add(ref charsArray, (uint)sizeof(TChar) * 2 * value),
(uint)sizeof(TChar) * 2);

Unsafe.CopyBlockUnaligned(
ref *(byte*)(ptr + 2),
ref Unsafe.Add(ref charsArray, (uint)sizeof(TChar) * 2 * remainder),
Expand Down
Loading