During the simulation of your test file, the Fixed-Point Converter app detects dead code or code that is constant folded. The app uses the code coverage information when translating your code from floating-point MATLAB® code to fixed-point MATLAB code. Reviewing code coverage results helps you to verify that your test file is exercising the algorithm adequately.
The app inserts inline comments in the fixed-point code to mark the dead and untranslated regions. It includes the code coverage information in the generated fixed-point conversion HTML report. The app editor displays a color-coded bar to the left of the code. This table describes the color coding.
|Coverage Bar Color||Indicates|
One of the following situations:
Different shades of green indicate different ranges of line execution counts. The darkest shade of green indicates the highest range.
|Orange||The entry-point function executes multiple times, but the code executes one time.|
|Red||Code does not execute.|
Dead code is code that does not execute during simulation. Dead code can result from these scenarios:
Defensive code containing intended corner cases that are not reached
Human error in the code, resulting in code that cannot be reached by any execution path
Inadequate test bench range
This example shows how to detect dead code in your algorithm by using the Fixed-Point Converter .
In a local writable folder, create the function
function y = myFunction(u,v) %#codegen for i = 1:length(u) if u(i) > v(i) y=bar(u,v); else tmp = u; v = tmp; y = baz(u,v); end end end function y = bar(u,v) y = u+v; end function y = baz(u,v) y = u-v; end
In the same folder, create a test file,
u = 1:100; v = 101:200; myFunction(u,v);
From the apps gallery, open the Fixed-Point Converter .
On the Select Source Files page,
browse to the
myFunction file, and click Open.
Click Next. On the Define
Input Types page, browse to select the test file that you
myFunction_tb. Click Autodefine
Click Next. On the Convert to Fixed-Point page, click Analyze to simulate the entry-point functions, gather range information, and get proposed data types.
The color-coded bar on the left side of the edit window indicates
whether the code executes. The code in the first condition of the
if-statement does not execute during simulation because u is
never greater than v. The
never executes because the if-statement never executes. These parts
of the algorithm are marked with a red bar, indicating that they are
To apply the proposed data types to the function, click Convert .
The Fixed-Point Converter generates a fixed-point
myFunction_fixpt. The generated fixed-point
code contains comments around the pieces of code identified as dead
code. The Validation Results pane proposes that
you use a more thorough test bench.
When the Fixed-Point Converter detects dead code, consider editing your test file so that your algorithm is exercised over its full range. If your test file already reflects the full range of the input variables, consider editing your algorithm to eliminate the dead code.
Close the Fixed-Point Converter .
Edit the test file
include a wider range of inputs.
u = 1:100; v = -50:2:149; myFunction(u,v);
Reopen the Fixed-Point Converter .
Using the same function and the edited test file, go through the conversion process again.
After you click Analyze, this time the code coverage bar shows that all parts of the algorithm execute with the new test file input ranges.
To finish the conversion process and convert the function to fixed point, click Convert.