Main Content

Specify Single-Precision Data Type for Embedded Application

When you want code that uses only single precision, such as when you are targeting a single-precision processor, you can use model configuration parameters and block parameters to prevent the introduction of double in the model.

To design and validate a single-precision model, see Validate a Floating-Point Embedded Model. If you have Fixed-Point Designer™, you can use the Single Precision Converter app (see Single Precision Converter (Fixed-Point Designer)).

Use single Data Type as Default for Underspecified Types

This example shows how to avoid introducing a double-precision data type in code generated for a single-precision hardware target.

If you specify an inherited data type for signals, but data type propagation rules cannot determine data types for the signals, the signal data types default to double. You can use a model configuration parameter to specify the default data type as single.

Explore Example Model

Open the example model UnderspecifiedDataType and configure it to show the generated names of blocks.

model = 'UnderspecifiedDataType';
load_system(model)
set_param(model,'HideAutomaticNames','off')
open_system(model);

The root inports In2, In3, and In4 specify Inherit: Auto for the Data type block parameter. The downstream blocks also use inherited data types.

Generate Code with double as Default Data Type

The model starts with the configuration parameter System target file set to ert.tlc, which requires Embedded Coder®. Set System target file to grt.tlc instead.

set_param(model,'SystemTargetFile','grt.tlc')

Generate code from the model.

slbuild(model)
### Starting build procedure for: UnderspecifiedDataType
### Successful completion of build procedure for: UnderspecifiedDataType

Build Summary

Top model targets built:

Model                   Action                        Rebuild Reason                                    
========================================================================================================
UnderspecifiedDataType  Code generated and compiled.  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 9.0857s

In the code generation report, view the file UnderspecifiedDataType.h. The code uses the double data type to define the variables In2, In3, and In4 because the Inport data types are underspecified in the model.

cfile = fullfile('UnderspecifiedDataType_grt_rtw',...
    'UnderspecifiedDataType.h');
coder.example.extractLines(cfile,...
    '/* External inputs (root inport signals with default storage) */',...
    '/* External outputs (root outports fed by signals with default storage) */',...
    1, 0);
/* External inputs (root inport signals with default storage) */
typedef struct {
  int8_T In1;                          /* '<Root>/In1' */
  real_T In2;                          /* '<Root>/In2' */
  real_T In3;                          /* '<Root>/In3' */
  real_T In4;                          /* '<Root>/In4' */
} ExtU_UnderspecifiedDataType_T;

Generate Code with single as Default Data Type

Open the Configuration Parameters dialog box. On the Math and Data Types pane, select single in the Default for underspecified data type drop-down list.

Alternatively, enable the optimization at the command prompt.

set_param(model, 'DefaultUnderspecifiedDataType', 'single');

Generate code from the model.

slbuild(model)
### Starting build procedure for: UnderspecifiedDataType
### Successful completion of build procedure for: UnderspecifiedDataType

Build Summary

Top model targets built:

Model                   Action                        Rebuild Reason                   
=======================================================================================
UnderspecifiedDataType  Code generated and compiled.  Generated code was out of date.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 8.3078s

In the code generation report, view the file UnderspecifiedDataType.h. The code uses the single data type to define the variables In2, In3, and In4.

coder.example.extractLines(cfile,...
    '/* External inputs (root inport signals with default storage) */',...
    '/* External outputs (root outports fed by signals with default storage) */',...
    1, 0);
/* External inputs (root inport signals with default storage) */
typedef struct {
  int8_T In1;                          /* '<Root>/In1' */
  real32_T In2;                        /* '<Root>/In2' */
  real32_T In3;                        /* '<Root>/In3' */
  real32_T In4;                        /* '<Root>/In4' */
} ExtU_UnderspecifiedDataType_T;

Related Topics