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 DivisionExceptions, two signals of type int8 feed into a divide block.

model = 'DivisionExceptions';
open_system(model);

Generate Code

Build the model.

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

Build Summary

Top model targets built:

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

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

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

cfile = fullfile('DivisionExceptions_ert_rtw','DivisionExceptions.c');
coder.example.extractLines(cfile,'/* Real-time model','/* Model initialize function',1, 1);
/* Real-time model */
static RT_MODEL_DivisionExceptions DivisionExceptions_M_;
RT_MODEL_DivisionExceptions *const DivisionExceptions_M = &DivisionExceptions_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 DivisionExceptions_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: DivisionExceptions
### Successful completion of code generation for: DivisionExceptions

Build Summary

Top model targets built:

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

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

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

coder.example.extractLines(cfile,'/* Real-time model','/* Model initialize function',1, 1);
/* Real-time model */
static RT_MODEL_DivisionExceptions DivisionExceptions_M_;
RT_MODEL_DivisionExceptions *const DivisionExceptions_M = &DivisionExceptions_M_;

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

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