This is an archive of the discontinued LLVM Phabricator instance.

[libc] add printf auto float conversion
ClosedPublic

Authored by michaelrj on Jan 31 2023, 11:38 AM.

Details

Summary

This patch adds the final conversion to printf, %g. This is a floating
point conversion that selects automatically between the %e and %f
formats based on the precision requested and resulting exponent.
Additionally it trims trailing zeroes. With this done all that's left
for finishing printf is adding long double support to the decimal float
conversions.

Diff Detail

Event Timeline

michaelrj created this revision.Jan 31 2023, 11:38 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 31 2023, 11:38 AM
michaelrj requested review of this revision.Jan 31 2023, 11:38 AM
michaelrj added inline comments.Jan 31 2023, 11:44 AM
libc/src/stdio/printf_core/float_dec_converter.h
807

above this line are minor bugfixes I made to the previous conversion (%e) that I discovered while testing the new conversion. All the new code is below this line.

michaelrj updated this revision to Diff 493744.Jan 31 2023, 3:04 PM

rebase to include inline changes

sivachandra added inline comments.Feb 3 2023, 11:45 AM
libc/test/src/stdio/sprintf_test.cpp
2121

Does this TODO belong here or somewhere in the implementation?

2398

Please add a comment explaining why we have these commented out blocks.

2699

Why is this commented out?

michaelrj updated this revision to Diff 494716.Feb 3 2023, 1:20 PM
michaelrj marked 3 inline comments as done.

adjust comments

libc/test/src/stdio/sprintf_test.cpp
2121

Probably yes, but it's also here to explain why the long double tests are commented out.

2699

I commented it out because I was testing the code with the system's sprintf, and this test was causing a segfault. I'll likely remove this test and the special case I put in in a separate patch because it's not necessary or in the standard.

sivachandra added inline comments.Feb 3 2023, 3:00 PM
libc/test/src/stdio/sprintf_test.cpp
2121

In the test, you should add a comment like, TODO: Uncomment the below tests after long double support is added. And in the implementaiton, add a comment like, `TODO: Extend the algorithm for long doubles with a bigger table, or use an alternate algorithm." Otherwise, it feels like the TODO is for the tests and not for the implementation.

sivachandra accepted this revision.Feb 3 2023, 3:00 PM
This revision is now accepted and ready to land.Feb 3 2023, 3:00 PM
michaelrj updated this revision to Diff 494753.Feb 3 2023, 3:16 PM
michaelrj marked an inline comment as done.

adjust TODO comments

This revision was landed with ongoing or failed builds.Feb 3 2023, 3:17 PM
This revision was automatically updated to reflect the committed changes.