For-Loop Best Practices for HDL Code Generation
When you generate HDL code from your MATLAB® design, you are converting an algorithm into an architecture that must meet hardware area and speed requirements. Some best practices for using loops in MATLAB code for HDL code generation are:
Use monotonically increasing loop counters, with increments of 1, to minimize the amount of hardware generated in the HDL code.
When implementing a loop to find an index value, use a conditional
if-elsestatement inside of the loop.
If you want to use the loop streaming optimization:
When assigning new values to persistent variables inside a loop, do not use other persistent variables on the right side of the assignment. Instead, use an intermediate variable.
If a loop modifies any elements in a persistent array, the loop should modify all of the elements in the persistent array.
Monotonically Increasing Loop Counters
By using monotonically increasing loop counters with increments of 1, you can reduce the amount of hardware in the generated HDL code. The following loop is an example of a monotonically increasing loop counter with increments of 1.
a=1; for i=1:10 a=a+1; end
If a loop counter increases by an increment other than 1, the generated HDL code can require additional adders. Due to this additional hardware, do not use the following type of loop.
a=1; for i=1:2:10 a=a+1; end
If a loop counter decreases, the generated HDL code can require additional adders. Due to this additional hardware, do not use the following type of loop.
a=1; for i=10:-1:1 a=a+1; end
Find Indices Using Loops
When generating HDL code, control flow-like breaks are not possible, see Supported MATLAB Data Types, Operators, and Control Flow Statements. Because a
for loop must run for a constant number of iterations, to
find an index using loops, use a conditional inside the loop, such as an
if-else statement. For example, to find the first index of a
one in the array
u, you can use this code:
function y = fcn(u) y = fi(0, 0, ceil(log2(numel(u))), 0); for ii = cast(1:numel(u), 'like', y) if y == 0 && u(ii) == true y = ii; end end end
The final value for
y is the smallest possible fixed-point
value of the index that contains a one in the array
Persistent Variables in Loops
If a loop contains multiple persistent variables, when you assign values to persistent variables, use intermediate variables that are not persistent on the right side of the assignment. This practice makes dependencies clear to the compiler and assists internal optimizations during the HDL code generation process. If you want to use the loop streaming optimization to reduce the amount of generated hardware, this practice is recommended.
In the following example,
var1 is used on the right side of the
assignment. Because a persistent variable is on the right side of an assignment, do
not use this type of loop:
for i=1:10 var1 = 1 + i; var2 = var1 * 2; end
Instead of using
var1 on the right side of the assignment, use
an intermediate variable that is not persistent. This example demonstrates this with
the intermediate variable
for i=1:10 var_intermediate = 1 + i; var1 = var_intermediate; var2 = var_intermediate * 2; end
Persistent Arrays in Loops
If a loop modifies elements in a persistent array, make sure that the loop modifies all of the elements in the persistent array. If all elements of the persistent array are not modified within the loop, HDL Coder™ cannot perform the loop streaming optimization.
In the following example,
a is a persistent array. The first
element is modified outside of the loop. Do not use this type of loop.
for i=2:10 a(i)=1+i; end a(1)=24;
Rather than modifying the first element outside the loop, modify all of the elements inside the loop.
for i=1:10 if i==1 a(i)=24; else a(i)=1+i; end end