%% 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&reg; license.


%   Copyright 2014 The MathWorks, Inc.

%% Example Model
% In the model <matlab:rtwdemo_nzcheck 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);

%%
% 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);

%% Enable Optimization
% # Open the Configuration Parameters dialog box.  
% # 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);

%%
% 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);

%%
% 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&reg; code with integer or fixed-point division operations differs from the built-in Divide block in Simulink&reg;.  
% 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&trade; license.
% Rounding and overflow modes also affect the size and efficiency of the generated code. 

