-fsingle-precision-constant breaks C++ standard library Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
Post Reply
User avatar
Spym
Posts: 45
Joined: Tue Oct 15, 2013 10:20 am
Has thanked: 2 times
Been thanked: 3 times

-fsingle-precision-constant breaks C++ standard library

Post by Spym »

This post is about GCC and C++.

The flag -fsingle-precision-constant is harmful and should be removed from the default build configuration.

1. Its use is incompatible with the C++ standard library shipped with GCC (fails to compile).
2. The application may still need to perform double-precision arithmetic even though it will be emulated in software (my application needs that).

If not removed completely, this flag should be made optional or disableable at the very least.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: -fsingle-precision-constant breaks C++ standard library

Post by Giovanni »

The problem is that the option is needed when the FPU is only able to handle single precision floats as far I know.

I will search more info.

Edit: It is explained here: https://www.embeddedrelated.com/showthr ... 6863-1.php

I will look into making it optional.

Giovanni
User avatar
Spym
Posts: 45
Joined: Tue Oct 15, 2013 10:20 am
Has thanked: 2 times
Been thanked: 3 times

Re: -fsingle-precision-constant breaks C++ standard library

Post by Spym »

Giovanni wrote:The problem is that the option is needed when the FPU is only able to handle single precision floats as far I know.


No, it's not true, it's easy to check by disabling the option (it is disabled in my application) and checking the compiled code (it still uses the FPU for floating-point arithmetics). The only practical effect of this option (according to the documentation https://gcc.gnu.org/wiki/FloatingPointMath) is that the compiler will treat double-precision literals as single-precision literals. This is exactly the behavior that breaks the standard library.

From the documentation:

-fsingle-precision-constant causes floating-point constants to be loaded in single precision even when this is not exact. This avoids promoting operations on single precision variables to double precision like in x + 1.0/3.0. Note that this also uses single precision constants in operations on double precision variables. This can improve performance due to less memory traffic.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: -fsingle-precision-constant breaks C++ standard library

Post by Giovanni »

Improving performance is exactly the point, this is why it is there, in order to avoid unnecessary conversions.

Giovanni
User avatar
Spym
Posts: 45
Joined: Tue Oct 15, 2013 10:20 am
Has thanked: 2 times
Been thanked: 3 times

Re: -fsingle-precision-constant breaks C++ standard library

Post by Spym »

Improving performance is great only as long as you don't break valid code :)

The developer can always achieve identical results by using the single-precision suffix "F", e.g. "3.14F". All high-integrity coding standards I know (MISRA, HIC++) require this anyway, in order to avoid unintended implicit type conversions.
faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

Re: -fsingle-precision-constant breaks C++ standard library

Post by faisal »

I also think it should be defined by the user.

Fyi, some stm32f7s and the H7 I believe have hardware double float support... so there's that.
User avatar
Spym
Posts: 45
Joined: Tue Oct 15, 2013 10:20 am
Has thanked: 2 times
Been thanked: 3 times

Re: -fsingle-precision-constant breaks C++ standard library

Post by Spym »

faisal wrote:Fyi, some stm32f7s and the H7 I believe have hardware double float support... so there's that.


This is correct. Cortex M7 implemented in upper STM32's supports hardware double precision floating point arighmetic.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: -fsingle-precision-constant breaks C++ standard library

Post by Giovanni »

It has been removed from trunk and 18.2.x, anyway, you could have overridden it by using USE_FPU_OPT in you makefile. Demos for micros with double precision FPU did that already.

Giovanni
Post Reply