MATLAB Examples

Remove Code That Guards Against Division Exceptions for Integers and Fixed-Point Data

Optimize generated code by removing code that protects against division by zero and overflows in division INT_MIN/-1 operations for integers and fixed-point data. If you are sure that these arithmetic exceptions do not occur during program execution, enable this optimization.

This optimization:

  • Increases execution speed.
  • Reduces ROM consumption.

NOTE: If you enable this optimization, it is possible that simulation results and results from generated code are not in bit-for-bit agreement. This example requires an Embedded Coder® license.

Contents

Example Model

In the model rtwdemo_nzcheck, two signals of type int8 feed into a divide block.

model = 'rtwdemo_nzcheck';
open_system(model);

Generate Code

In your system's temporary folder, create a temporary folder for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

Build the model.

set_param(model, 'NoFixptDivByZeroProtection', 'off');
rtwbuild(model);
### Starting build procedure for model: rtwdemo_nzcheck
### Successful completion of code generation for model: rtwdemo_nzcheck

View the generated code without the optimization. Here is a portion of rtwdemo_nzcheck.c.

cfile = fullfile(cgDir,'rtwdemo_nzcheck_ert_rtw','rtwdemo_nzcheck.c');
rtwdemodbtype(cfile,'/* Real-time model','/* Model step function',1, 1);
/* Real-time model */
RT_MODEL_rtwdemo_nzcheck rtwdemo_nzcheck_M_;
RT_MODEL_rtwdemo_nzcheck *const rtwdemo_nzcheck_M = &rtwdemo_nzcheck_M_;
int32_T div_s32(int32_T numerator, int32_T denominator)
{
  int32_T quotient;
  uint32_T tempAbsQuotient;
  if (denominator == 0) {
    quotient = numerator >= 0 ? MAX_int32_T : MIN_int32_T;

    /* Divide by zero handler */
  } else {
    tempAbsQuotient = (numerator < 0 ? ~(uint32_T)numerator + 1U : (uint32_T)
                       numerator) / (denominator < 0 ? ~(uint32_T)denominator +
      1U : (uint32_T)denominator);
    quotient = (numerator < 0) != (denominator < 0) ? -(int32_T)tempAbsQuotient :
      (int32_T)tempAbsQuotient;
  }

  return quotient;
}

Enable Optimization

  1. Open the Configuration Parameters dialog box.
  2. On the Optimization pane, select Remove code that protects against division arithmetic exceptions.

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

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

Generate Code with Optimization

The optimized code does not contain code that checks for whether or not the divisor has a value of zero.

Build the model.

rtwbuild(model);
### Starting build procedure for model: rtwdemo_nzcheck
### Successful completion of code generation for model: rtwdemo_nzcheck

The following is a portion of rtwdemo_nzcheck.c. The code that protects against division arithmetic exceptions is not in the generated code.

rtwdemodbtype(cfile,'/* Real-time model','/* Model step function',1, 1);
/* Real-time model */
RT_MODEL_rtwdemo_nzcheck rtwdemo_nzcheck_M_;
RT_MODEL_rtwdemo_nzcheck *const rtwdemo_nzcheck_M = &rtwdemo_nzcheck_M_;

Close the model and code generation report.

bdclose(model)
rtwdemoclean;
cd(currentDir)