This example shows how to remove code that maps NaN
to
integer zero. For floating-point to integer conversions involving
saturation, Simulink converts NaN
to integer zero during
simulation. If your model contains an input value of NaN
,
you can specify that the code generator produce code that maps
NaN
to zero. Without this code, there is a
mismatch between simulation and code generation results because in Standard
C, every condition involving NaN
evaluates to false.
If input values of NaN
doe not exist in your application,
you can remove code that maps NaN
to integer zero.
Removing this code reduces the size and increases the speed of the generated
code.
In this model, a Data Type Conversion block converts an
input signal from a double
to a
uint8
. In this example, the model is
named conversion_ex
.
Use Inport, Outport, and Data Type Conversion blocks to create the example model.
Open the Inport Block Parameters dialog box and click
the Signal Attributes tab. For
the Data Type parameter, select
double
.
Open the Data Type Conversion dialog box. For the
Output data type parameter,
select uint8
.
Select Saturate on integer overflow. Selecting this parameter specifies that an out-of-range signal value equals either the minimum or maximum value that the data type can represent.
For the signal feeding into the Data Type Conversion
block, open the Signal Properties dialog box. Enter
a name of U
. In the Code Mappings
editor, under Signals, set the storage Class, to
ImportedExtern
.
For the signal leaving the Data Type
Conversion block, open the Signal
Properties dialog box. Enter a name of
Y
. In the Code Mappings editor,
under Signals, set the storage class, to
ImportedExtern
.
Set the Configuration
Parameters > Solver > Solver options > Type parameter to
Fixed-step
.
Disable the Configuration Parameters > Optimization > Advanced parameters > Remove code from floating-point to integer conversions with saturation that maps NaN to zero parameter.
Enable the Configuration Parameters > Code Generation > Report > Create code generation report parameter and the Open report automatically parameter.
Enable the Configuration Parameters > Code Generation > Build process > Generate code only parameter. Then, in the model window, press Ctrl+B. When code generation is complete, an HTML code generation report opens.
In the Code Generation report, select the
nan_int_ex.c
file and view the
model step function. For an input value of
NaN
, there is agreement between
the generated code and simulation because
NaN
maps to integer
zero.
/* Model step function */ void nan_int_ex_step(void) { /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates: * Inport: '<Root>/In1' */ if (U < 256.0) { if (U >= 0.0) { Y = (uint8_T)U; } else { Y = 0U; } } else if (U >= 256.0) { Y = MAX_uint8_T; } else { Y = 0U; }
Enable the Configuration Parameters > Optimization > Code generation > Integer and fixed-point > Remove code from floating-point to integer conversions that wraps out-of-range values parameter. Generate code.
In the Code Generation report, select the
nan_int_ex.c
section and view
the model step function. The generated code maps
NaN
to 255 and not integer
zero. The generated code is more efficient without
the extra code that maps NaN
to
integer zero, but the execution of the generated
code does not produce the same results as simulation
for NaN
values.
/* Model step function */ void nan_int_ex_step(void) { /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates: * Inport: '<Root>/In1' */ if (U < 256.0) { if (U >= 0.0) { Y = (uint8_T)U; } else { Y = 0U; } } else { Y = MAX_uint8_T; } /* End of DataTypeConversion: '<Root>/Data Type Conversion' */
Remove code from floating-point to integer conversions with saturation that maps NaN to zero