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
159 changes: 105 additions & 54 deletions src/libraries/Common/src/System/Number.Formatting.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,93 @@ internal static partial class Number
private const int DefaultPrecisionExponentialFormat = 6;

private const int MaxUInt32DecDigits = 10;
private const string PosNumberFormat = "#";

private static readonly string[] s_posCurrencyFormats =
[
"$#", "#$", "$ #", "# $"
];

private static readonly string[] s_negCurrencyFormats =
[
"($#)", "-$#", "$-#", "$#-",
"(#$)", "-#$", "#-$", "#$-",
"-# $", "-$ #", "# $-", "$ #-",
"$ -#", "#- $", "($ #)", "(# $)",
"$- #"
];

private static readonly string[] s_posPercentFormats =
[
"# %", "#%", "%#", "% #"
];

private static readonly string[] s_negPercentFormats =
[
"-# %", "-#%", "-%#",
"%-#", "%#-",
"#-%", "#%-",
"-% #", "# %-", "% #-",
"% -#", "#- %"
];

private static readonly string[] s_negNumberFormats =
[
"(#)", "-#", "- #", "#-", "# -",
];

private static ReadOnlySpan<byte> GetCurrencyFormat(bool isNegative, int index)
Comment thread
tannergooding marked this conversation as resolved.
{
if (isNegative)
{
return index switch
{
0 => "($#)"u8,
1 => "-$#"u8,
2 => "$-#"u8,
3 => "$#-"u8,
4 => "(#$)"u8,
5 => "-#$"u8,
6 => "#-$"u8,
7 => "#$-"u8,
8 => "-# $"u8,
9 => "-$ #"u8,
10 => "# $-"u8,
11 => "$ #-"u8,
12 => "$ -#"u8,
13 => "#- $"u8,
14 => "($ #)"u8,
15 => "(# $)"u8,
16 => "$- #"u8,
_ => throw new UnreachableException(),
};
}

return index switch
{
0 => "$#"u8,
1 => "#$"u8,
2 => "$ #"u8,
3 => "# $"u8,
_ => throw new UnreachableException(),
};
}

private static ReadOnlySpan<byte> GetPercentFormat(bool isNegative, int index)
{
if (isNegative)
{
return index switch
{
0 => "-# %"u8,
1 => "-#%"u8,
2 => "-%#"u8,
3 => "%-#"u8,
4 => "%#-"u8,
5 => "#-%"u8,
6 => "#%-"u8,
7 => "-% #"u8,
8 => "# %-"u8,
9 => "% #-"u8,
10 => "% -#"u8,
11 => "#- %"u8,
_ => throw new UnreachableException(),
};
}

return index switch
{
0 => "# %"u8,
1 => "#%"u8,
2 => "%#"u8,
3 => "% #"u8,
_ => throw new UnreachableException(),
};
}

private static ReadOnlySpan<byte> GetNumberFormat(bool isNegative, int index)
{
if (!isNegative)
{
return "#"u8;
}

return index switch
{
0 => "(#)"u8,
1 => "-#"u8,
2 => "- #"u8,
3 => "#-"u8,
4 => "# -"u8,
_ => throw new UnreachableException(),
};
}

internal static char ParseFormatSpecifier(ReadOnlySpan<char> format, out int digits)
{
Expand Down Expand Up @@ -723,23 +776,23 @@ private static unsafe void FormatCurrency<TChar>(ref ValueListBuilder<TChar> vlb
{
Debug.Assert(sizeof(TChar) is sizeof(char) or sizeof(byte));

string fmt = number.IsNegative ?
s_negCurrencyFormats[info.CurrencyNegativePattern] :
s_posCurrencyFormats[info.CurrencyPositivePattern];
ReadOnlySpan<byte> fmt = GetCurrencyFormat(
number.IsNegative,
number.IsNegative ? info.CurrencyNegativePattern : info.CurrencyPositivePattern);

foreach (char ch in fmt)
foreach (byte ch in fmt)
{
switch (ch)
{
case '#':
case (byte)'#':
FormatFixed(ref vlb, ref number, nMaxDigits, info.CurrencyGroupSizes(), info.CurrencyDecimalSeparatorTChar<TChar>(), info.CurrencyGroupSeparatorTChar<TChar>());
break;

case '-':
case (byte)'-':
vlb.Append(info.NegativeSignTChar<TChar>());
break;

case '$':
case (byte)'$':
vlb.Append(info.CurrencySymbolTChar<TChar>());
break;

Expand Down Expand Up @@ -893,19 +946,17 @@ private static unsafe void FormatNumber<TChar>(ref ValueListBuilder<TChar> vlb,
{
Debug.Assert(sizeof(TChar) is sizeof(char) or sizeof(byte));

string fmt = number.IsNegative ?
s_negNumberFormats[info.NumberNegativePattern] :
PosNumberFormat;
ReadOnlySpan<byte> fmt = GetNumberFormat(number.IsNegative, info.NumberNegativePattern);

foreach (char ch in fmt)
foreach (byte ch in fmt)
{
switch (ch)
{
case '#':
case (byte)'#':
FormatFixed(ref vlb, ref number, nMaxDigits, info.NumberGroupSizes(), info.NumberDecimalSeparatorTChar<TChar>(), info.NumberGroupSeparatorTChar<TChar>());
break;

case '-':
case (byte)'-':
vlb.Append(info.NegativeSignTChar<TChar>());
break;

Expand Down Expand Up @@ -1020,23 +1071,23 @@ private static unsafe void FormatPercent<TChar>(ref ValueListBuilder<TChar> vlb,
{
Debug.Assert(sizeof(TChar) is sizeof(char) or sizeof(byte));

string fmt = number.IsNegative ?
s_negPercentFormats[info.PercentNegativePattern] :
s_posPercentFormats[info.PercentPositivePattern];
ReadOnlySpan<byte> fmt = GetPercentFormat(
number.IsNegative,
number.IsNegative ? info.PercentNegativePattern : info.PercentPositivePattern);

foreach (char ch in fmt)
foreach (byte ch in fmt)
{
switch (ch)
{
case '#':
case (byte)'#':
FormatFixed(ref vlb, ref number, nMaxDigits, info.PercentGroupSizes(), info.PercentDecimalSeparatorTChar<TChar>(), info.PercentGroupSeparatorTChar<TChar>());
break;

case '-':
case (byte)'-':
vlb.Append(info.NegativeSignTChar<TChar>());
break;

case '%':
case (byte)'%':
vlb.Append(info.PercentSymbolTChar<TChar>());
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;

namespace System;

Expand Down Expand Up @@ -30,6 +31,13 @@ private readonly struct DiyFp128FixedCoefficient(ulong lo, ulong hi)
internal readonly ulong High = hi;
}

private static ReadOnlySpan<DiyFp128FixedCoefficient> DiyFp128FixedCoefficients(ReadOnlySpan<ulong> coefficients)
{
Debug.Assert((coefficients.Length & 1) == 0);

return MemoryMarshal.Cast<ulong, DiyFp128FixedCoefficient>(coefficients);
}
Comment thread
tannergooding marked this conversation as resolved.

// High 64 bits of a 64x64 product (Intel's UMULH).
private static ulong DiyFp128MultiplyHigh(ulong a, ulong b) => Math.BigMul(a, b, out _);

Expand All @@ -44,32 +52,32 @@ private readonly struct DiyFp128FixedCoefficient(ulong lo, ulong hi)
// ln2_lo = ln2 - ln2_hi, as an unpacked value.
private static DiyFp128 ExpLn2Low => new DiyFp128(UxSignBit, -66, 0xD871319FF0342542, 0xFC32F366359D2749);

private static readonly DiyFp128FixedCoefficient[] ExpCoefficients =
private static ReadOnlySpan<DiyFp128FixedCoefficient> ExpCoefficients => DiyFp128FixedCoefficients(
[
new(0x0219C7290393A749, 0x0000000000000000),
new(0x2E468FC7B47B630C, 0x0000000000000000),
new(0xCA85AD657F5C80BD, 0x0000000000000003),
new(0xD268B2CB49B64EAE, 0x000000000000004B),
new(0x9E18D9E0EB90C661, 0x00000000000005A0),
new(0x1DC178468FE824F4, 0x000000000000654B),
new(0xF9CCF1842631A1A2, 0x000000000006B9FC),
new(0x9CCECE542F079EEB, 0x00000000006B9FCF),
new(0x301F26EFF3934011, 0x00000000064E5D2A),
new(0xA1B4271D14562C06, 0x000000005849184E),
new(0x3625ED5697A1173A, 0x000000047BB63BFE),
new(0x89C71FC24062E495, 0x00000035CC8ACFEA),
new(0xEB8E5DDFF9B4C26E, 0x0000024FC9F6EF13),
new(0x338FAAC2198CD02D, 0x0000171DE3A556C7),
new(0xD00D00D00E2C1D71, 0x0000D00D00D00D00),
new(0x8068068066CFB7B5, 0x0006806806806806),
new(0x82D82D82D829D3B1, 0x002D82D82D82D82D),
new(0x111111111113746F, 0x0111111111111111),
new(0x5555555555555AA3, 0x0555555555555555),
new(0x5555555555555380, 0x1555555555555555),
new(0xFFFFFFFFFFFFFFFE, 0x3FFFFFFFFFFFFFFF),
new(0x0000000000000000, 0x8000000000000000),
new(0x0000000000000000, 0x8000000000000000),
];
0x0219C7290393A749, 0x0000000000000000,
0x2E468FC7B47B630C, 0x0000000000000000,
0xCA85AD657F5C80BD, 0x0000000000000003,
0xD268B2CB49B64EAE, 0x000000000000004B,
0x9E18D9E0EB90C661, 0x00000000000005A0,
0x1DC178468FE824F4, 0x000000000000654B,
0xF9CCF1842631A1A2, 0x000000000006B9FC,
0x9CCECE542F079EEB, 0x00000000006B9FCF,
0x301F26EFF3934011, 0x00000000064E5D2A,
0xA1B4271D14562C06, 0x000000005849184E,
0x3625ED5697A1173A, 0x000000047BB63BFE,
0x89C71FC24062E495, 0x00000035CC8ACFEA,
0xEB8E5DDFF9B4C26E, 0x0000024FC9F6EF13,
0x338FAAC2198CD02D, 0x0000171DE3A556C7,
0xD00D00D00E2C1D71, 0x0000D00D00D00D00,
0x8068068066CFB7B5, 0x0006806806806806,
0x82D82D82D829D3B1, 0x002D82D82D82D82D,
0x111111111113746F, 0x0111111111111111,
0x5555555555555AA3, 0x0555555555555555,
0x5555555555555380, 0x1555555555555555,
0xFFFFFFFFFFFFFFFE, 0x3FFFFFFFFFFFFFFF,
0x0000000000000000, 0x8000000000000000,
0x0000000000000000, 0x8000000000000000,
]);

// ---- exp10 (base 10) constant table (dpml_exp_x.h) ----
// The exp10 polynomial approximates 10^t directly, so the reduction subtracts scale*log10(2) and the
Expand All @@ -84,32 +92,32 @@ private readonly struct DiyFp128FixedCoefficient(ulong lo, ulong hi)
// log10(2)_lo, as an unpacked value.
private static DiyFp128 Exp10Ln2Low => new DiyFp128(UxSignBit, -66, 0xE0ED4CA7E906DD0F, 0xB2A59E75785C196C);

private static readonly DiyFp128FixedCoefficient[] Exp10Coefficients =
private static ReadOnlySpan<DiyFp128FixedCoefficient> Exp10Coefficients => DiyFp128FixedCoefficients(
[
new(0xAA326D76E12A5F3D, 0x000000000005D18C),
new(0xBB46D2D76A135C14, 0x000000000037BD19),
new(0x2188762E74D6A84B, 0x0000000001FBA820),
new(0x10A5EEBAE5E25723, 0x0000000011396F18),
new(0xB3FCD05A246EA126, 0x000000008E20E630),
new(0x11F8F23A20DD37FD, 0x00000004570FB29C),
new(0x167B5D1D64BF3431, 0x000000200AF8FBFF),
new(0xB407C79F854435F8, 0x000000DEA8177BC6),
new(0xAEF77A1B0616E83B, 0x000005AA7A612E29),
new(0x119B2348D3C5FBA9, 0x0000227315A5882E),
new(0x20D8613A1E07D507, 0x0000C27F096FC05F),
new(0x7F472BC73DD8F81C, 0x0003F59FABB213AC),
new(0x674C9F4591A76481, 0x0012EA52B2D182AF),
new(0xC9822F93893BB4F4, 0x005225F11764F507),
new(0xF088AE28F92F4908, 0x014116B05FDAA5CD),
new(0xC160BBA8AA4224B1, 0x045B937F0CCEA1AC),
new(0xD9F3DCD36EBEE310, 0x0D3F6B8423E45AEB),
new(0x5C6542259124B3BC, 0x22853FFA3A9AEC44),
new(0xEA51F65ED9F90D3B, 0x4AF5D827F6631131),
new(0x6A4F9D820D46BA57, 0x82382C8EF1652304),
new(0x80A99CE52D65A6EC, 0xA9A92639E753443A),
new(0xEA56D62B82D30A2C, 0x935D8DDDAAA8AC16),
new(0x0000000000000000, 0x4000000000000000),
];
0xAA326D76E12A5F3D, 0x000000000005D18C,
0xBB46D2D76A135C14, 0x000000000037BD19,
0x2188762E74D6A84B, 0x0000000001FBA820,
0x10A5EEBAE5E25723, 0x0000000011396F18,
0xB3FCD05A246EA126, 0x000000008E20E630,
0x11F8F23A20DD37FD, 0x00000004570FB29C,
0x167B5D1D64BF3431, 0x000000200AF8FBFF,
0xB407C79F854435F8, 0x000000DEA8177BC6,
0xAEF77A1B0616E83B, 0x000005AA7A612E29,
0x119B2348D3C5FBA9, 0x0000227315A5882E,
0x20D8613A1E07D507, 0x0000C27F096FC05F,
0x7F472BC73DD8F81C, 0x0003F59FABB213AC,
0x674C9F4591A76481, 0x0012EA52B2D182AF,
0xC9822F93893BB4F4, 0x005225F11764F507,
0xF088AE28F92F4908, 0x014116B05FDAA5CD,
0xC160BBA8AA4224B1, 0x045B937F0CCEA1AC,
0xD9F3DCD36EBEE310, 0x0D3F6B8423E45AEB,
0x5C6542259124B3BC, 0x22853FFA3A9AEC44,
0xEA51F65ED9F90D3B, 0x4AF5D827F6631131,
0x6A4F9D820D46BA57, 0x82382C8EF1652304,
0x80A99CE52D65A6EC, 0xA9A92639E753443A,
0xEA56D62B82D30A2C, 0x935D8DDDAAA8AC16,
0x0000000000000000, 0x4000000000000000,
]);

// 1.0 as an unpacked value (Intel's UX_ONE).
private static DiyFp128 DiyFp128One => new DiyFp128(0, 1, 0x8000000000000000, 0);
Expand Down
Loading