Main Content

Optimize Generated Code Using Boolean Data for Logical Signals

Optimize generated code by storing logical signals as Boolean data. When you select the model configuration parameter Implement logic signals as Boolean data (vs. double), blocks that generate logic signals output Boolean signals.

The optimization:

  • Reduces the ROM and RAM consumption.

  • Improves execution speed.

Example Model

Consider the model LogicalAsBoolean. The outputs of the Relational Operator, Logical Operator and HitCrossing blocks are double, even though they represent logical data.

model = 'LogicalAsBoolean';
open_system(model);

Generate Code

Build the model.

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

Build Summary

Top model targets built:

Model             Action                        Rebuild Reason                                    
==================================================================================================
LogicalAsBoolean  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 42.707s

View the generated code without the optimization. These lines of code are in LogicalAsBoolean.h.

hfile = fullfile('LogicalAsBoolean_grt_rtw',...
    'LogicalAsBoolean.h');
coder.example.extractLines(hfile,'/* External outputs','/* Parameters (default storage) */',1,0);
/* External outputs (root outports fed by signals with default storage) */
typedef struct {
  real_T Out1;                         /* '<Root>/Out1' */
  real_T Out2;                         /* '<Root>/Out2' */
  real_T Out3;                         /* '<Root>/Out3' */
} ExtY_LogicalAsBoolean_T;

Enable Optimization

  1. Open the Configuration Parameters dialog box.

  2. Select the Implement logic signals as Boolean data (vs. double) parameter.

Alternatively, you can use the command-line API to enable the optimization:

set_param(model,'BooleanDataType','on');

Generate Code with Optimization

The generated code stores the logical signal output as Boolean data.

Build the model.

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

Build Summary

Top model targets built:

Model             Action                        Rebuild Reason                   
=================================================================================
LogicalAsBoolean  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 17.493s

View the generated code with the optimization. These lines of code are in LogicalAsBoolean.h.

coder.example.extractLines(hfile,'/* External outputs','/* Parameters (default storage) */',1,0);
/* External outputs (root outports fed by signals with default storage) */
typedef struct {
  boolean_T Out1;                      /* '<Root>/Out1' */
  boolean_T Out2;                      /* '<Root>/Out2' */
  boolean_T Out3;                      /* '<Root>/Out3' */
} ExtY_LogicalAsBoolean_T;

Close the model and code generation report.

bdclose(model)

See Also

Related Topics