Is it possible to force single precision instead of double precision when generating code from simulink?

34 ビュー (過去 30 日間)
Hello!
I am facing an issue where my generated code from Simulink contains some typecasts to double. Especially when i use the ABS block. In the code there's always fabs and a typecast to double even when it's a variable of type single... I would expect something like a call of fabsf without a typecast.
My actual problem is, that my code is going to run on a 32-bit embedded target with a FPU. So i'm fine with single as a data type and the software is designed to use single precision. But i want to avoid double as a data type at any cost due to efficiency. Of course i could run some S&R scripts, but isn't there a way to avoid double in the first place??
(actually it's real32_t and real_t just for clarification...)

採用された回答

Christian Boettcher
Christian Boettcher 2017 年 3 月 29 日
The Solution for me was to use my own Target Function Library. I just added some entries for fabsf and it's fine now.
I used this example.

その他の回答 (3 件)

Mohsen Nosratinia
Mohsen Nosratinia 2017 年 12 月 1 日
Check the "Code Replacement Library" option in Simulation -> Model Configuration parameters -> Code Generation -> Interface. If it is set to C89/C90 you will never get fabsf() because it was not part of C89. C89 only had fabs() for double-precision so embedded coder correctly casts single to double, calls fabs() and casts it back to single. If you change that option to C99 you will see fabsf() in generated code without any type casts. The same goes for some other functions like fminf(), cosf(), sqrtf(), and many more.

Andy Bartlett
Andy Bartlett 2018 年 2 月 9 日
Hi, This video shows the Single Precision Converter tool. This tool will change block data types to single precision and handle settings like making sure the target library is C99. The Single Precision Converter is available with Fixed-Point Designer. Regards Andy Bartlett

Chinmayi Lanka
Chinmayi Lanka 2017 年 3 月 28 日
The following example walks you through how you can generate code with 'single' as default data type: http://www.mathworks.com/examples/simulink-coder/mw/simulinkcoder-ex01097737-use-single-data-type-as-default-for-underspecified-types?s_cid=rlcnt_ME#6
  2 件のコメント
Christian Boettcher
Christian Boettcher 2017 年 3 月 29 日
I already tried that, but unfortunatly this doesn't apply for the absolute value block... There's still a typecast to double.

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by