Skip to content

Offer a new define to avoid int64 conversions#221

Open
satur9nine wants to merge 1 commit into
eyalroz:masterfrom
satur9nine:allow_avoid_int64
Open

Offer a new define to avoid int64 conversions#221
satur9nine wants to merge 1 commit into
eyalroz:masterfrom
satur9nine:allow_avoid_int64

Conversation

@satur9nine

Copy link
Copy Markdown

The new define PRINTF_AVOID_INT64_TO_FLOAT_CONVERSION when combined with PRINTF_USE_DOUBLE_INTERNALLY ensures that int64 to float conversions are not performed.

@satur9nine

satur9nine commented Apr 29, 2026

Copy link
Copy Markdown
Author

Firstly thank you for this library! I am using it on an ARM Cortex M7 MCU with hardfloat support, compiling with GCC 13 and using the following defines:

// Completly replace newlib
#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD 1

// Exponent support is unneeded for our use case
#define PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS 0

// Writeback is considered a security risk
#define PRINTF_SUPPORT_WRITEBACK_SPECIFIER 0

// Better performance with float, double is slower
#define PRINTF_USE_DOUBLE_INTERNALLY 0

I noticed in the elf dump the following functions __aeabi_l2f and __addsf3 adding 540 bytes of executable code. The root cause was this code:

scaled_remainder = (abs_number - (floating_point_t) number_.integral) * powers_of_10[precision];
remainder = scaled_remainder - (floating_point_t) number_.fractional;

Which uses int_fast64_t intermediates and converts them back to floating_point_t. This existing code allows for more precision when printing %f but for our needs that amount of precision is not required and not worth the performance and space cost. We are just printing basic logs and prefer high performance and save flash for more important things.

I suspect many people choose this library for the exact same reason so this flag would be appreciated. It is completely opt-in so no existing behavior changes.

The new define PRINTF_AVOID_INT64_TO_FLOAT_CONVERSION when combined with
PRINTF_USE_DOUBLE_INTERNALLY ensures that int64 to float conversions are
not performed.
@satur9nine satur9nine force-pushed the allow_avoid_int64 branch from 1dd6a87 to 0117c30 Compare May 22, 2026 06:38
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.

1 participant