mpaland / printf

Tiny, fast, non-dependent and fully loaded printf implementation for embedded systems. Extensive test suite passing.
MIT License
2.54k stars 464 forks source link

More complete fixes, including integer stuff #96

Open essele opened 3 years ago

essele commented 3 years ago

Hi,

Further to my previous pull request, this one also sorts out the duplicate out_rev from the previous pull, but also fixes an issue with integer processing as well.

%#3x would truncate a two digit hex number (because of the 0x) previously which it shouldn't do. This is now fixed.

All tests still pass with the same exceptions as before (one incorrect and one "improved precision" related.)

Lee.

eyalroz commented 3 years ago

Also, is this PR of yours independent from the previous PR?

essele commented 3 years ago

Interesting. On my current project, where I’ve worked hard to remove the use of as many standard library math functions as possible, this resulted in a much smaller implementation. The pow() library call depends on a whole set of other complex and large functions which can otherwise be avoided (obviously different if you are using pow() elsewhere.)

My use case has changed slightly and stack usage is my primary concern so I’ve written a different implementation (based on many of the concepts in your great implementation!) — so I’m not using the version below any more. So the PR’s are there for your use if you see anything useful, if not, then so be it.

Cheers,

Lee.

On 30 Jun 2021, at 08:13, Eyal Rozenberg @.***> wrote:

@eyalroz commented on this pull request.

In printf.c https://github.com/mpaland/printf/pull/96#discussion_r661191449:

// import float.h for DBL_MAX

if defined(PRINTF_SUPPORT_FLOAT)

include

endif

+// +// Helper macro to record and remove negativity (also works for -0.0) +// +#define _ISIGNCHECK(i,n) if (i < 0) { i = -i; n = 1; } else { n = 0; } +#define _FSIGNCHECK(f,n) if (1.0/f < 0.0) { f = -f; n = 1; } else { n = 0; } + +#if defined(PRINTF_SUPPORT_FLOAT) +// +// This is actually a fairly efficient implementation compared to pow() as it's It may be more time efficient, but it's not more space efficient.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mpaland/printf/pull/96#pullrequestreview-695770755, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK4K3FYN2ETWJUUJ3NZ7XLTVK7XZANCNFSM4YU5GXIQ.

eyalroz commented 3 years ago

On my current project, where I’ve worked hard to remove the use of as many standard library math functions as possible, this resulted in a much smaller implementation. The pow() library call depends on a whole set of other complex and large functions which can otherwise be avoided

Yes, but even then - you could use a loop to multiply (you only need ~log(p) steps, not p, if you square each time or square-and-multiply). That's slower than the lookup (typically), but might be smaller. Although TBH I haven't checked the size anywhere. In asymptotic terms you've certainly traded space to gain time, but that depends on how big the compiled multiplication loop ends up being.

eyalroz commented 3 years ago

so I’ve written a different implementation (based on many of the concepts in your great implementation!)

I think you're mistaking me for @mpaland ... I'm just trying to pick up PRs to add to my fork.