Why does the code generation of elementary math function "abs" not respect single-precision floating point data type?

3 Ansichten (letzte 30 Tage)
I am using Simulink with Embedded Coder R2019a. I am using the "Abs" block inside my Simulink model with input datatype as "single". The outport of the model is set to inherit input data type. When I generate code from the model, I see the following line of code:
out_sig_single = (F32)fabs(in_sig_single);
Even though the results of this code would be computationally correct, the generated code uses double precision floating point numbers for operation and then uses cast operator to cast it to a single precision floating point data type. My hardware only supports single precision math. Is there a way to make this operation only use single precision types?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 7 Sep. 2023
Bearbeitet: MathWorks Support Team am 13 Sep. 2023
In MATLAB R2019a, you can force the generated code to use the single precision function "fabsf" instead of double precision "fabs". To do this, please is to change the "Standard math library" configuration parameter to "C99 (ISO)". For more information about this setting, please refer to the documentation page "Standard Math Library":
https://www.mathworks.com/help/releases/R2019a/rtw/ref/standard-math-library.html
In newer releases including MATLAB R2023a, this configuration parameter is renamed to "Language Standard" and is available in "Configuration Parameters > Code Generation". Please see the MATLAB R2023a documentation page "Language Standard":
https://www.mathworks.com/help/releases/R2023a/rtw/ref/languagestandard.html
  3 Kommentare
Walter Roberson
Walter Roberson am 22 Aug. 2019
The C standard library does not define any single precision floating point absolute value until C99, which introduced fabsf() and cabsf()
C99 defines that on systems that support IEEE 754 that double (a mandatory C datatype) is to be double precision. However it does permit non-754 systems to define double the same as single, in which case it does not matter that the code reads double because datatype double would be single precision.
Therefore the distinction between single and double is only relevant for non-compliant systems that know the difference between single and double but do not implement double.
Martin Becker
Martin Becker am 6 Sep. 2023
In more recent versions of MATLAB/Simulink, the option is in the model settings (Ctrl+E) under Code Generation > Language standard > C99 (ISO). Then fabs() becomes fabsf()

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by