-
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 all commits
ccf86db
0ab3dfe
163d95a
30afcdc
6166f7f
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,162 @@ private static int GetFloatingPointMaxDigitsAndPrecision(char fmt, ref int preci | |||||||||||||||||||||||||
return maxDigits; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private static void FormatFloatAsHex<TNumber, TChar>(ref ValueListBuilder<TChar> vlb, TNumber value, char fmt, int precision) | ||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||
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 mantissa for hex output (leading digit should be 1.xxx in range [1, 2)) | ||||||||||||||||||||||||||
int actualExponent; | ||||||||||||||||||||||||||
ulong significand; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (biasedExponent == 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Denormal number - normalize by shifting until we get leading 1 | ||||||||||||||||||||||||||
significand = mantissa; | ||||||||||||||||||||||||||
int lz = BitOperations.LeadingZeroCount(significand) - (64 - mantissaBits); | ||||||||||||||||||||||||||
significand <<= lz; | ||||||||||||||||||||||||||
actualExponent = 1 - exponentBias - lz; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Normal number - add implicit leading bit | ||||||||||||||||||||||||||
significand = (1UL << mantissaBits) | mantissa; | ||||||||||||||||||||||||||
actualExponent = biasedExponent - exponentBias; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Shift significand so the leading 1 is at bit 60 (first nibble position) | ||||||||||||||||||||||||||
// This ensures the integer part is 1.xxx | ||||||||||||||||||||||||||
int shift = 63 - mantissaBits; | ||||||||||||||||||||||||||
significand <<= shift; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Adjust exponent for the shift (we divided by 2^shift, so add shift to exponent) | ||||||||||||||||||||||||||
// But we also want the leading nibble at bit 60, so shift right by 3 more | ||||||||||||||||||||||||||
significand >>= 3; | ||||||||||||||||||||||||||
actualExponent += 3; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Output integer part (should always be 1 for normalized form) | ||||||||||||||||||||||||||
char hexBase = fmt == 'X' ? 'A' : 'a'; | ||||||||||||||||||||||||||
int firstNibble = (int)(significand >> 60); | ||||||||||||||||||||||||||
char firstHexChar = firstNibble < 10 ? (char)('0' + firstNibble) : (char)(hexBase + firstNibble - 10); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom(firstHexChar)); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Remove the first nibble | ||||||||||||||||||||||||||
significand <<= 4; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// 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)(significand >> 60); | ||||||||||||||||||||||||||
char hexChar = nibble < 10 ? (char)('0' + nibble) : (char)(hexBase + nibble - 10); | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom(hexChar)); | ||||||||||||||||||||||||||
significand <<= 4; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Output exponent | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('p')); | ||||||||||||||||||||||||||
if (actualExponent >= 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('+')); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Format exponent as decimal without allocating string | ||||||||||||||||||||||||||
FormatInt32ToValueListBuilder(ref vlb, actualExponent); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private static void FormatInt32ToValueListBuilder<TChar>(ref ValueListBuilder<TChar> vlb, int value) | ||||||||||||||||||||||||||
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; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Handle zero specially | ||||||||||||||||||||||||||
if (value == 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom('0')); | ||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Format digits in reverse, then reverse the span | ||||||||||||||||||||||||||
int startIndex = vlb.Length; | ||||||||||||||||||||||||||
uint uvalue = (uint)value; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
while (uvalue > 0) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
vlb.Append(TChar.CastFrom((char)('0' + (uvalue % 10)))); | ||||||||||||||||||||||||||
uvalue /= 10; | ||||||||||||||||||||||||||
Comment on lines
+669
to
+670
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. Could use |
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Reverse the digits | ||||||||||||||||||||||||||
int endIndex = vlb.Length - 1; | ||||||||||||||||||||||||||
while (startIndex < endIndex) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
TChar temp = vlb[startIndex]; | ||||||||||||||||||||||||||
vlb[startIndex] = vlb[endIndex]; | ||||||||||||||||||||||||||
vlb[endIndex] = temp; | ||||||||||||||||||||||||||
startIndex++; | ||||||||||||||||||||||||||
endIndex--; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
public static string FormatFloat<TNumber>(TNumber value, string? format, NumberFormatInfo info) | ||||||||||||||||||||||||||
where TNumber : unmanaged, IBinaryFloatParseAndFormatInfo<TNumber> | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
|
@@ -598,6 +754,13 @@ public static bool TryFormatFloat<TNumber, TChar>(TNumber value, ReadOnlySpan<ch | |||||||||||||||||||||||||
precision = TNumber.MaxPrecisionCustomFormat; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Handle hex float formatting (X or x) | ||||||||||||||||||||||||||
if (fmt == 'X' || fmt == 'x') | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
FormatFloatAsHex(ref vlb, value, fmt, precision); | ||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
NumberBuffer number = new NumberBuffer(NumberBufferKind.FloatingPoint, pDigits, TNumber.NumberBufferLength); | ||||||||||||||||||||||||||
number.IsNegative = TNumber.IsNegative(value); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot, I had gpt-5 review your implementation of FormatFloatAsHex, and it had the following feedback:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed all issues in FormatFloatAsHex:
The implementation now correctly outputs standard hex float format with leading digit in [1, 2) range.