-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add hexadecimal float/double parsing and formatting support (IEEE 754:2008) #120637
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
ccf86db
0ab3dfe
163d95a
30afcdc
6166f7f
24c6010
8ca1ffd
f5d2798
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -526,6 +526,132 @@ | |||||||||||||||||||||||||
return maxDigits; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private static void FormatFloatAsHex<TNumber, TChar>(ref ValueListBuilder<TChar> vlb, TNumber value, char fmt, int precision, NumberFormatInfo info) | ||||||||||||||||||||||||||
where TNumber : unmanaged, IBinaryFloatParseAndFormatInfo<TNumber> | ||||||||||||||||||||||||||
where TChar : unmanaged, IUtfChar<TChar> | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Get the raw bits | ||||||||||||||||||||||||||
ulong bits = TNumber.FloatToBits(value); | ||||||||||||||||||||||||||
int mantissaBits = TNumber.NormalMantissaBits; | ||||||||||||||||||||||||||
int exponentBias = TNumber.ExponentBias; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Extract sign, exponent, and mantissa | ||||||||||||||||||||||||||
bool isNegative = (bits >> (mantissaBits + TNumber.ExponentBits)) != 0; | ||||||||||||||||||||||||||
int biasedExponent = (int)((bits >> mantissaBits) & ((1UL << TNumber.ExponentBits) - 1)); | ||||||||||||||||||||||||||
ulong mantissa = bits & TNumber.NormalMantissaMask; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Add sign | ||||||||||||||||||||||||||
if (isNegative) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('-')); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Add "0x" prefix | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('0')); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom(fmt)); // 'x' or 'X' | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Handle special cases | ||||||||||||||||||||||||||
if (biasedExponent == TNumber.InfinityExponent) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Infinity or NaN - just output as 0 | ||||||||||||||||||||||||||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('0')); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('p')); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('+')); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('0')); | ||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (biasedExponent == 0 && mantissa == 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Zero | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('0')); | ||||||||||||||||||||||||||
if (precision > 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('.')); | ||||||||||||||||||||||||||
for (int i = 0; i < precision; i++) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('0')); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('p')); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('+')); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('0')); | ||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Normalize: add implicit leading 1 for normal numbers | ||||||||||||||||||||||||||
int actualExponent; | ||||||||||||||||||||||||||
if (biasedExponent == 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Denormal number | ||||||||||||||||||||||||||
actualExponent = 1 - exponentBias; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Normal number - add implicit leading bit | ||||||||||||||||||||||||||
mantissa |= (1UL << mantissaBits); | ||||||||||||||||||||||||||
actualExponent = biasedExponent - exponentBias; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Normalize mantissa so the leading bit is in the MSB position | ||||||||||||||||||||||||||
int shift = 64 - mantissaBits - 1; | ||||||||||||||||||||||||||
mantissa <<= shift; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Output integer part (always "1" for normalized) | ||||||||||||||||||||||||||
char hexBase = fmt == 'X' ? 'A' : 'a'; | ||||||||||||||||||||||||||
int firstNibble = (int)(mantissa >> 60); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom((char)('0' + (firstNibble > 9 ? 0 : firstNibble)))); | ||||||||||||||||||||||||||
if (firstNibble > 9) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom((char)(hexBase + firstNibble - 10))); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Remove the first nibble | ||||||||||||||||||||||||||
mantissa = (mantissa << 4) & 0xFFFFFFFFFFFFFFFF; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Determine how many hex digits to output | ||||||||||||||||||||||||||
int hexDigits = precision >= 0 ? precision : (mantissaBits + 3) / 4; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (hexDigits > 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('.')); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
for (int i = 0; i < hexDigits; i++) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
int nibble = (int)(mantissa >> 60); | ||||||||||||||||||||||||||
char hexChar = nibble < 10 ? (char)('0' + nibble) : (char)(hexBase + nibble - 10); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom(hexChar)); | ||||||||||||||||||||||||||
mantissa = (mantissa << 4) & 0xFFFFFFFFFFFFFFFF; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Output exponent | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('p')); | ||||||||||||||||||||||||||
if (actualExponent >= 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('+')); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Format exponent as decimal | ||||||||||||||||||||||||||
FormatInt32(ref vlb, actualExponent, 0, null, info); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private static void FormatInt32<TChar>(ref ValueListBuilder<TChar> vlb, int value, int precision, string? format, NumberFormatInfo info) | ||||||||||||||||||||||||||
Check failure on line 639 in src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs
|
||||||||||||||||||||||||||
where TChar : unmanaged, IUtfChar<TChar> | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
if (value < 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('-')); | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential integer overflow when negating
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||
value = -value; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
string numStr = ((uint)value).ToString(); | ||||||||||||||||||||||||||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
foreach (char c in numStr) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom(c)); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
public static string FormatFloat<TNumber>(TNumber value, string? format, NumberFormatInfo info) | ||||||||||||||||||||||||||
where TNumber : unmanaged, IBinaryFloatParseAndFormatInfo<TNumber> | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
|
@@ -598,6 +724,13 @@ | |||||||||||||||||||||||||
precision = TNumber.MaxPrecisionCustomFormat; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Handle hex float formatting (X or x) | ||||||||||||||||||||||||||
if (fmt == 'X' || fmt == 'x') | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
FormatFloatAsHex(ref vlb, value, fmt, precision, info); | ||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
NumberBuffer number = new NumberBuffer(NumberBufferKind.FloatingPoint, pDigits, TNumber.NumberBufferLength); | ||||||||||||||||||||||||||
number.IsNegative = TNumber.IsNegative(value); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.