주요 콘텐츠

coder.loop.reverse

R2026b

Reverse loop iteration order in generated code

Since R2023a

    Description

    coder.loop.reverse(loopID) directs the code generator to reverse the loop iteration order for the for-loop with the index name loopID in the generated code.

    Use this directive when the loop iterator has a known upper bound. The loop direction must not affect correctness, and there must be no forward or backward dependencies. For more information about loop optimizations, see Optimize Loops in Generated Code.

    example

    Examples

    collapse all

    Examine the function applyReverse, which performs an addition operation on an array. This function uses the coder.loop.reverse directive to reverse the iteration order of the for-loop.

    function out = applyReverse %#codegen
    out = zeros(1,100);
    
    coder.loop.reverse("i");
    for i = 1:100
        out(i) = out(i)+i;
    end
    

    Generate a C static library for this function.

    codegen -config:lib -report applyReverse

    Open the code generation report and inspect the generated code. The for-loop iterates in reverse from 99 to 0.

    for (i = 99; i >= 0; i--) {
        out[i] += (double)i + 1.0;
      }

    Input Arguments

    collapse all

    Index name of the for-loop to reverse, specified as a string scalar or character vector.

    Tips

    • To view potential issues that the code generator encounters when applying the coder.loop.reverse directive, review the Code Insights section of the code generation report. See Code Generation Reports.

    Extended Capabilities

    expand all

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2023a