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:

Model             Build Reason                                         Status                        Build Duration
===================================================================================================================
LogicalAsBoolean  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 21.321s 

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

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:

Model             Build Reason                     Status                        Build Duration
===============================================================================================
LogicalAsBoolean  Generated code was out of date.  Code generated and compiled.  0h 0m 9.5809s 

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

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