This example shows how to remove code for out-of-range floating-point to integer conversions. Without this code, there might be a mismatch between simulation and code generation results. Standard C does not define the behavior of out-of-range floating-point to integer conversions, while these conversions are well-defined during simulation. In Standard C and during simulation, floating-point to integer conversions are well-defined for input values in the range of the output type.
If the input values in your application are in the range of the output type, remove code for out-of-range floating-point to integer conversions. 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
.
A uint8
can support values from 0 to 255. If the
input signal has a value outside of this range, an out-of-range conversion
occurs. 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 select
the Signal tab. For the
Data Type parameter, select
double
.
Open the Data Type Conversion dialog box. For the
Output data type parameter,
select uint8
.
For the signal feeding into the Data Type
Conversion block, open the signal
Properties dialog box. Enter the name
U
. On the Code
Generation tab, for the
Storage Class parameter,
select
ImportedExtern
.
For the signal leaving the Data Type Conversion block,
open the Signal Properties dialog box. Enter the
name Y
. On the Code
Generation tab, for the
Storage Class parameter,
select
ImportedExtern
.
Open the Configuration Parameters dialog box. On the
Solver pane, for the
Type parameter, select
Fixed-step
.
On the Code Generation > Report pane, select Create code generation report and Open report automatically.
On the Code Generation pane, select Generate code only, and 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 conversion_ex.c
file
and view the model step function. The code generator applies the fmod
function
to handle out-of range-results.
/* Model step function */ void conversion_ex_step(void) { real_T tmp; /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates: Inport: '<>/In1' */ tmp = floor(U); if (rtIsNaN(tmp) || rtIsInf(tmp)) { tmp = 0.0; } else { tmp = fmod(tmp, 256.0); } Y = (uint8_T) (tmp < 0.0 ? (int32_T) (uint8_T)-(int8_T) (uint8_T)-tmp : (int32_T) (uint8_T) tmp);
Open the Configuration Parameters dialog box. On the Optimization pane, select Remove code from floating-point to integer conversions that wraps out-of-range values. Generate code.
In the code generation report, select the conversion_ex.c
file
and view the model step function. The generated code does not contain
code that protects against out-of-range values.
/* Model step function */ void conversion_ex_step(void) { /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates: * Inport: '<Root>/In1' */ Y = (uint8_T)U;
The generated code is more efficient without this protective code, but it is possible that the execution of generated code does not produce the same results as simulation for values not in the range of 0 to 255.
Remove code from floating-point to integer conversions that wraps out-of-range values