stdio/format: fix printing sign for long double#487
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates stdio/format.c to preserve the sign bit when downconverting a long double to a double during floating-point formatting. It introduces an msb parameter to format_sprintfDouble to pass the sign bit. A review comment correctly points out that checking the sign bit with __builtin_signbit(doubleNumber) > 0 is unsafe, as the function is only guaranteed to return a non-zero value which could be negative on some platforms. It is recommended to use != 0 instead.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
b3bfc24 to
af61ddc
Compare
af61ddc to
65b9b02
Compare
JIRA: RTOS-1368
65b9b02 to
3bcf8eb
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request modifies the floating-point formatting logic in stdio/format.c to preserve the sign bit during down-conversion by passing an explicit msb flag to format_sprintfDouble. The reviewer pointed out that promoting standard double values to long double and back introduces unnecessary performance overhead and potential emulation bugs on soft-float platforms (such as RISC-V). It is recommended to keep doubleNumber as a standard double and handle long double locally only when the FLAG_LONG_DOUBLE flag is set.
| long double doubleNumber; | ||
| uint8_t msb = 0; | ||
|
|
||
| if (fmt == 'g' || fmt == 'G') { | ||
| flags |= FLAG_NO_TRAILING_ZEROS; | ||
| } | ||
| if ((flags & FLAG_LONG_DOUBLE) != 0) { | ||
| /* NOTE: support for long double is incomplete */ | ||
| doubleNumber = (double)va_arg((args), long double); | ||
| doubleNumber = va_arg((args), long double); | ||
| } | ||
| else { | ||
| doubleNumber = (double)va_arg((args), double); | ||
| doubleNumber = (long double)va_arg((args), double); | ||
| } | ||
|
|
||
| /* preserve sign bit during down-conversion to double */ | ||
| if (__builtin_signbit(doubleNumber) != 0) { | ||
| msb = 1; | ||
| } | ||
|
|
||
| CHECK_FAIL(ret, format_sprintfDouble(ctx, feed, doubleNumber, flags, minFieldWidth, precision, tolower(fmt))); | ||
| CHECK_FAIL(ret, format_sprintfDouble(ctx, feed, (double)doubleNumber, flags, minFieldWidth, precision, tolower(fmt), msb)); |
There was a problem hiding this comment.
Casting standard double values to long double and then back to double introduces significant performance overhead on platforms without hardware 128-bit float support (such as RISC-V), as it forces the use of soft-float emulation routines (__extenddftf2 and __trunctfdf2) for every standard double formatting.
Additionally, this unnecessarily subjects standard double formatting to the buggy __trunctfdf2 emulation routine, which could lead to incorrect sign behavior even for standard double values.
We can avoid this overhead and risk by keeping doubleNumber as a standard double and only using long double locally when FLAG_LONG_DOUBLE is set.
double doubleNumber;
uint8_t msb = 0;
if (fmt == 'g' || fmt == 'G') {
flags |= FLAG_NO_TRAILING_ZEROS;
}
if ((flags & FLAG_LONG_DOUBLE) != 0) {
/* NOTE: support for long double is incomplete */
long double ld = va_arg((args), long double);
if (__builtin_signbit(ld) != 0) {
msb = 1;
}
doubleNumber = (double)ld;
}
else {
doubleNumber = va_arg((args), double);
if (__builtin_signbit(doubleNumber) != 0) {
msb = 1;
}
}
CHECK_FAIL(ret, format_sprintfDouble(ctx, feed, doubleNumber, flags, minFieldWidth, precision, tolower(fmt), msb));References
- Avoid unnecessary type promotion/demotion overhead (such as double to long double) on soft-float platforms to prevent performance degradation and potential emulation bugs.
JIRA: RTOS-1368
Description
When down-converting from long double to double on RISCV, GCC is emitting
__trunctfdf2()which truncates sign bit for special values (NaN, inf). This fix preservers sign bit before down-convertion, and adds it later when needed.Motivation and Context
Fixes phoenix-rtos/phoenix-rtos-project#1007
Types of changes
How Has This Been Tested?
Checklist:
Special treatment