coder
coder
const
Fold expressions into constants in generated code
collapse all in page
Syntax
out = coder.const(expression)
[out1,...,outN] = coder.const(handle,arg1,...,argN)
Description
out = coder.const(expression) evaluates expression and replaces out with the result of the
evaluation in generated code.
example
[out1,...,outN] = coder.const(handle,arg1,...,argN) evaluates the multi-output function having
handle handle. It then replaces out1,...,outN with the results of the evaluation in the generated
code. To learn about the behavior of coder.const when handle accepts zero inputs and returns
zero or one outputs, see Tips.
example
Examples
collapse all
Fold Function Calls into Constants in Generated Code
This example shows how to specify constants in generated code using coder.const. The code
generator folds an expression or a function call in a coder.const statement into a constant in
generated code. Because the generated code does not have to evaluate the expression or call the
function every time, this optimization reduces the execution time of the generated code.
Write a function AddShift that takes an input Shift and adds it to the elements of a vector. The
vector consists of the square of the first 10 natural numbers. AddShift generates this vector.
function y = AddShift(Shift) %#codegen
y = (1:10).^2+Shift;
Generate code for AddShift using the codegen command. Open the Code Generation Report.
codegen -config:lib -launchreport AddShift -args 0
The code generator produces code for creating the vector. It adds Shift to each element of the
vector during vector creation. The definition of AddShift in generated code looks as follows:
void AddShift(double Shift, double y[10])
{
int k;
for (k = 0; k < 10; k++) {
y[k] = (double)((1 + k) * (1 + k)) + Shift;
}
}
Replace the expression (1:10).^2 with coder.const((1:10).^2), and then generate code
for AddShift again using the codegen command. Open the Code Generation Report.
codegen -config:lib -launchreport AddShift -args 0
The code generator creates the vector containing the squares of the first 10 natural numbers. In
the generated code, it adds Shift to each element of this vector. The definition of AddShift in
generated code looks as follows:
void AddShift(double Shift, double y[10])
{
int i;
static const signed char iv[10] = { 1, 4, 9, 16, 25, 36,
49, 64, 81, 100 };
function y = gettable(n)
y = zeros(1,n);
for i = 1:n
y(i) = sin((i-1)/(2*pi*n));
end
Generate code for getsine using an argument of type int32. Open the Code Generation Report.
codegen -config:lib -launchreport getsine -args int32(0)
The generated code contains instructions for creating the lookup table.
Replace the statement:
tbl = gettable(1024);
with:
tbl = coder.const(gettable(1024));
Generate code for getsine using an argument of type int32. Open the Code Generation Report.
The generated code contains the lookup table itself. coder.const forces the
expression gettable(1024) to be evaluated during code generation. The generated code does not
contain instructions for the evaluation. The generated code contains the result of the evaluation
itself.