[Issue] Using printf with int64_t types.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Issue] Using printf with int64_t types.

Postby kolban » Thu Oct 13, 2016 4:00 am

I am working with 3rd party code that has logic similar to the following:

Code: Select all

#include <inttypes.h>
int 64_t bigi = 1234;
printf("Here is a big int %" PRId64 "\n", bigi);
The result should have been:

Code: Select all

Here is a big int 1234
however what is produced is

Code: Select all

Here is a big int ld
Through expansion, the core statement is equivalent to:

Code: Select all

printf("Here is a big int %lld\n", bigi);
I'll be the first to say that the source looks "odd" to me ... however googling around, it is apparently quite legitimate. I'm still searching for a workaround or fix. I don't have the opportunity to easily make a permanent change to the source code so the sample above is what I would like to get working.
Last edited by kolban on Wed Dec 28, 2016 1:41 am, edited 1 time in total.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Using printf with int64_t types.

Postby ESP_Angus » Thu Oct 13, 2016 6:42 am

ESP32 libc (newlib) is compiled with "nano" I/O options, which disables 64-bit formatting. Solution is something like:
printf("Here is a big int %" PRId32 "%" PRId32 "\n", (int)(bigi >> 32), (int)bigi);
Unfortunately I don't know of a way to do this to third-party code without editing it.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Using printf with int64_t types.

Postby kolban » Thu Oct 13, 2016 4:00 pm

Oooh ... I had assumed "newlib" was "newlib" but I am starting to get a sense that there is "newlib" and "newlib-nano" which are in some ways distinct from each other. May I ask ... what is the background on "newlib" as distributed by ESP-IDF? I seem to see that it is distributed in binary format. Can you provide us knowledge on the heritage of the source used to compile it? What flags and settings might have been used when the libraries are generated?

For example ... there appears to be (and I could be very wrong here) a flag used when compiling "newlib" that enables printf long-long support ... for example. It seems to be:

enable-newlib-io-long-long

See - http://stackoverflow.com/questions/3294 ... ng-support

By knowing how our version of newlib was compiled, I could see whether that flag was enabled and, if it was ... think about the puzzle one way ... or if I found that it was not enabled, think about the puzzle in a different way.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: Using printf with int64_t types.

Postby ESP_igrr » Thu Oct 13, 2016 7:55 pm

Here you go:

Source code: https://github.com/espressif/newlib-esp32

Temporary gist with build options used for newlib in ROM: https://gist.github.com/igrr/92f6115dd0 ... 2ba729b519
I will document this in esp-idf, but it will take a few days.

I do find it a bit unfortunate that newlib doesn't use the same weak linking mechanism for 64bit formatting as it does for floating point formatting...

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Using printf with int64_t types.

Postby kolban » Thu Oct 13, 2016 8:11 pm

THANK YOU that is exceptionally responsive of you. Again, thanks for the fantastic turnaround and I'm going to start studying as soon as possible. My evening will now be full!

2016-11-06: A Github issue now exists ... see https://github.com/espressif/esp-idf/issues/52
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 146 guests