llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.91k stars 11.51k forks source link

[libc] `printf` doesn't print floating point numbers on cortex-m33 #100816

Open PiJoules opened 1 month ago

PiJoules commented 1 month ago

Doubles and floats don't appear to be printed for cortex-m33. If I have

printf("d2i32 %f->%d\n", x, (int32_t)x);

where x is a float, what will be printed is

d2i32 %f->-2147483648

The same goes for if x were a double

printf("u %u->%f\n", x, (double)x);
// u 32->%f

Likewise %g doesn't seem to work either

printf("%g %10.18g %10.18g, %10.18g, %10.18g %10.18g\n", x, f, x + 0.37777777777777777777777777777, x - 0.377777777777777777777777777777, g, 123456789.0 / x);
// %g %10.18g %10.18g, %10.18g, %10.18g %10.18g

The target flags used are

--target=armv8m.main-unknown-eabi -mfloat-abi=softfp -mcpu=cortex-m33 -march=armv8m.main+fp+dsp
michaelrj-google commented 1 month ago

This is because LIBC_CONF_PRINTF_DISABLE_FLOAT is set to true for baremetal: https://github.com/llvm/llvm-project/blob/main/libc/config/baremetal/config.json#L8

Setting that to false will enable floating point conversions in printf, but that will also increase binary size significantly.