A program to recognize and reward our most engaged community members
I have this simple diagram, I code gen for stm32g431, but when I view the code I can see double cast.
Why is embed using double even though I converted each input and output to float?
You can force usage of single precision float using the compiler flags for instance if you are using STM32 compiler -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fshort-double
and in particular
-fshort-double
In these cases the compiler will take care and use single precision float for the doubles that are being used in the generated code.
Hi Xchum,
The behavior you are observing is due to the relational operators in Embed which uses double outputs as a standard for the relational operators outputs. Turns out that this is also compatible with all C standards historically (C89, C99, C11, C++) ..etc
By Design in Embed
Relational operators (>=, ==, etc.) produce boolean results (true/false as 1.0/0.0), not numeric values. Using double as the standard boolean representation ensures:
Consistent behavior across all diagrams Maximum simulation precision Type stability — outputs don't change based on input types
STM32G4 Code Generation The double type is only used during simulation. During code generation for STM32G4:
The generated C code is: result = (a >= b) — already optimal The C compiler produces efficient comparison code (no double arithmetic) The comparison itself uses the input types (float, int, etc.)
The diagram type doesn't affect the generated comparison operation.
If you need the output as float or int, insert a Convert block:
[float >= float] → [double] → [Convert to float] → [Next Block]
which is something you have forced rightly in your diagram by the way .
The Convert block is cheap from a cost of memory that the device would take — it compiles to a simple cast and and is negligible overhead.
Current behavior = accurate simulation + optimal embedded code. Use Convert blocks for explicit type control when interfacing with specific APIs or data structures.
Hope this behavior is clear.
But I am already using the convert block but the generated code still has double cast, this behavior is strange. Am I missing something?
I see the following on my side. As you see the float seems to be honored if you have the convert block at the end setup to convert to float as well .
I am sensing that in your case the final convert may be setup to be integer in which it could explain why you see double as I explained before the relational operator's behavior here.
The image you shared also shows multiple casts which includes double, why is it using double when we have converted all the inputs and outputs? please forgive if this seems like a silly question, I'm new to it. I just want to make sure the generated code for my MCU do not use double.
All intermediate conversions go through a double today in Embed today for blocks like sum, relational operators and this the reason you see these doubles.
However if you really would not want to see doubles. I would recommend on using fixed point blocks which would make the overall codegen use no floating point operations all along. Attached is a diagram for example
If this is the case then , is there no way to utilize the FPU in my MCU? No way to use single precision float?
Thank you so much buddy, compiler flags is the answer I was looking for. It converted all the basic blocks to float 😁 half of my problem is solved. Is it possible to get your email or social media handle, If you can help me out on this embed project, that would be the happiest highlight of my life so far 😭