Skip to content

Fix reversed output and optimize base-10 conversion #7

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

Merged
merged 2 commits into from
Jul 1, 2025

Conversation

visitorckw
Copy link
Collaborator

Fix incorrect decimal output and improves the efficiency of base-10
conversion routines used by itoa() and vsprintf(). The bug was caused
by an unnecessary reversal of digits in __str_base10(), resulting in
reversed output. This is corrected by writing digits in the correct
order and applying a single final reversal in itoa() for signed
numbers. Additionally, the implementation is simplified by removing the
temporary buffer in __str_base10() and writing digits directly into the
output buffer, reducing memory operations and improving performance.

The __str_base10() function mistakenly reversed the digits again,
causing the final string to be printed backward. This patch corrects
the digit copy order in __str_base10() and adds a final string reversal
in itoa() to restore the correct output order for base 10 conversions.

Fixes: b3e756c ("Optimize base-10 conversion for RV32I libc")
Eliminate the temporary tmp[] buffer in __str_base10() by writing
digits directly into the output buffer. This avoids an extra memory
copy and improves efficiency while keeping the logic simple. The
maximum digit count comment is also removed as it is no longer
relevant.
@jserv jserv merged commit 0516bf7 into sysprog21:main Jul 1, 2025
@jserv
Copy link
Contributor

jserv commented Jul 1, 2025

Thank @visitorckw for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants