restore dtostrf when floats are disabled in printf/scanf + round fix by d-a-v · Pull Request #7087 · esp8266/Arduino · GitHub
Skip to content

restore dtostrf when floats are disabled in printf/scanf + round fix#7087

Closed
d-a-v wants to merge 9 commits into
esp8266:masterfrom
d-a-v:dtostrfix
Closed

restore dtostrf when floats are disabled in printf/scanf + round fix#7087
d-a-v wants to merge 9 commits into
esp8266:masterfrom
d-a-v:dtostrfix

Conversation

@d-a-v

@d-a-v d-a-v commented Feb 16, 2020

Copy link
Copy Markdown
Collaborator

Replaced by #7093 (which updates only 1 file, not 23 :)

This PR restores dtosrf() only when floats are disabled in printf.
boards.txt builder adds a new define -DNOPRINTFLOAT in this case which enables dtostrf compilation.
A small fix has been added for the rounding issue, which I believe is generic enough, but will always be a subject for discussion.
Some multiplications have been removed from the algorithm too.

fixes #7073 with solution 6
updates #7068
fixes #7043

dtostrf(1045.5, 0) now returns 1046.

@mcspr @arendst Can you test this ?

@d-a-v d-a-v changed the title dtostrf: restore when floats are disabld in printf/scanf dtostrf: restore when floats are disabled in printf/scanf Feb 16, 2020
@d-a-v d-a-v changed the title dtostrf: restore when floats are disabled in printf/scanf restore dtostrf when floats are disabled in printf/scanf + round fix Feb 16, 2020
@arendst

arendst commented Feb 16, 2020

Copy link
Copy Markdown

@d-a-v

d-a-v commented Feb 16, 2020

Copy link
Copy Markdown
Collaborator Author

d-a-v approved this pull request.

@mcspr mcspr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread cores/esp8266/core_esp8266_noniso.cpp Outdated
return utoa((unsigned int)value, result, base);
}

#ifdef NOPRINTFLOAT

@mcspr mcspr Feb 16, 2020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NO_PRINTF_FLOAT?
NO_FLOAT_PRINTF?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dogpiling on this (sorry). Adding new defines normally ends up breaking PIO or other folks.

Would it be possible, through whatever magic the -u _printf_float uses, to somehow make the proper version be linked-in?

I could also drag this into the newlib if that would be needed to make it work "seamlessly" with the existing infra/flags.

@mcspr mcspr Feb 18, 2020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of it as something like the existing NO_GLOBAL_... defines
PIO user btw, only change that would be required is adding env.Append(CXXFLAGS=["NO_PRINTF_FLOAT"]) to the user build script (or, modifying platformio.ini build_flags=... variable, which is also file that is controlled by user), Core files don't need to change

Re 'magic', it's just a simple != NULL:
https://github.com/earlephilhower/newlib-xtensa/blob/b326aa447a1e5b33d6ed7d164aa2c59eb3754b9f/newlib/libc/stdio/nano-vfprintf.c#L651-L653

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, then could it be as simple as:

char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
    if (!_printf_float) {
        return __dtostrf(number, width, char prec, s);
    } else {
      char fmt[32];
      sprintf(fmt, "%%%d.%df", width, prec);
      sprintf(s, fmt, number);
      return s;
  }
}

static char * __dtostrf(double number, signed char width, unsigned char prec, char *s) {
...all the @d-a-v fixed code here...
}

This way the existing flag which will be eval'd a compile time will either cause the dtostrf guts to be omitted and use printf, or vice-versa.

No flag changes, no tool flow changes. Assuming it works...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great ! 👍 thanks @mcspr @earlephilhower

Comment thread cores/esp8266/core_esp8266_noniso.cpp Outdated
#else // !NOPRINTFLOAT

char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
char fmt[32];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in the issue, does this need to have a safeguard such as asm(".global _printf_float");?
ref https://sourceware.org/binutils/docs/as/Global.html#Global

.global makes the symbol visible to ld.

earlephilhower and others added 2 commits February 16, 2020 09:17
Corrected stack start and end in stack_thunk_dump_stack().

@earlephilhower earlephilhower left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for un-borking my borkage! You went the extra mile and optimized for all cases, which is awesome.

Can we make it transparent, though? You're the weak-linkage expert on the team, I think. :)

Comment thread cores/esp8266/core_esp8266_noniso.cpp Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dogpiling on this (sorry). Adding new defines normally ends up breaking PIO or other folks.

Would it be possible, through whatever magic the -u _printf_float uses, to somehow make the proper version be linked-in?

I could also drag this into the newlib if that would be needed to make it work "seamlessly" with the existing infra/flags.

…log limitation and other fixes (esp8266#6887)

* upstream lwIP is now downloaded by a makefile, not subsubmoduled

* lwip2: upstream lwIP not sub-sub-modules anymore
lwip2: Allow IPv4 and IPv6 DNS and SNTP server configured via DHCP to co-exist (patch against upstream)

* lwip2: enable tcp-listen-with-backlog feature

* lwip2 submodule update:
- enable more efficient chksum algorithm thanks to Richard Allen
- enable tcp listener with backlog

* more comments, fix backlog management, fix API
* move default value definition in .cpp
because one must not believe it can be redefined before including WiFiServer.h

* improved backlog handling, it is no more a breaking change
* configTime(tzsec,dstsec,): fix UTC/local management
This PR also remove dead code since probably newlib updates
The NTP-TZ-DST example is also updated
* restore sntp_set_timezone_in_seconds()
fixes esp8266#6678
* +configTzTime()
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.

dtostrf/Print(float) inconsistent value/methods, downstream use [BUG]: Rounding Values when Using String Type Cast

6 participants