Main Content

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 but it may lead to a quotient that cannot be represented.

This optimization:

  • Increases execution speed.

  • Results in smaller code thereby reducing ROM consumption.

Risks

When you select the NoFixptDivByZeroProtection parameter, the code generator removes code that protects against the following errors:

  • When you divide by zero it is undefined and results in a runtime error in the generated code.

  • When you divide the minimum representable value of a signed integer by negative one, the ideal result is equal to the maximum representable value plus one (INT_MAX + 1), which is not representable. This exception may cause the application to unexpectedly halt or crash at run-time.

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.

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');
slbuild(model);
### Starting build procedure for: rtwdemo_nzcheck
### Successful completion of code generation for: rtwdemo_nzcheck

Build Summary

Top model targets built:

Model            Action          Rebuild Reason                                    
===================================================================================
rtwdemo_nzcheck  Code generated  Code generation information file does not exist.  

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

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 initialize function',1, 1);
/* Real-time model */
static 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;
}

/* Model step function */
void rtwdemo_nzcheck_step(void)
{
  /* Product: '<Root>/Divide' incorporates:
   *  Inport: '<Root>/In1'
   *  Inport: '<Root>/In2'
   */
  Y = div_s32(U1, U2);
}

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.

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

Build Summary

Top model targets built:

Model            Action          Rebuild Reason                   
==================================================================
rtwdemo_nzcheck  Code generated  Generated code was out of date.  

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

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 initialize function',1, 1);
/* Real-time model */
static RT_MODEL_rtwdemo_nzcheck rtwdemo_nzcheck_M_;
RT_MODEL_rtwdemo_nzcheck *const rtwdemo_nzcheck_M = &rtwdemo_nzcheck_M_;

/* Model step function */
void rtwdemo_nzcheck_step(void)
{
  /* Product: '<Root>/Divide' incorporates:
   *  Inport: '<Root>/In1'
   *  Inport: '<Root>/In2'
   */
  Y = U1 / U2;
}

Close the model and code generation report.

bdclose(model)
rtwdemoclean;
cd(currentDir)

Additional Information

There are several other factors that can affect the appearance of the generated code for division operations. The generated code for blocks containing MATLAB® code with integer or fixed-point division operations differs from the built-in Divide block in Simulink®. To balance the efficiency and semantics of fixed-point and integer divisions in these blocks, use fi objects and set the fimath properties to fit your needs and requires a Fixed-Point Designer™ license. Rounding and overflow modes also affect the size and efficiency of the generated code.

See Also

Related Topics