Code You Can Replace From Simulink Models
Code that the code generator replaces depends on the code replacement library (CRL) that you use. By default, the code generator does not apply a code replacement library. Your choice of libraries is dependent on product licensing and whether you have access to custom libraries.
For information on how to explore functions and operators that a code replacement library supports, see Choose a Code Replacement Library.
Math Functions – Simulink Support
When generating C/C++ code from a Simulink® model, depending on code replacement libraries available in your development environment, you can configure the code generator to replace instances of the following math functions with application-specific implementations. To replace math functions that you use inside MATLAB function blocks, the functions must also meet the requirements in Code You Can Replace from MATLAB Code.
Function | Data Type Support | Scalar, Vector, Matrix Support | Real, Complex Support |
---|---|---|---|
abs 1 | Integer Floating point Fixed point | Scalar Vector Matrix | Real |
acos | Floating point | Scalar | Real Complex input/complex output Real input/complex output |
acosd 2 | Floating point | Scalar Vector Matrix | Real Complex |
acosh | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
acot 2 | Floating point | Scalar Vector Matrix | Real Complex |
acotd 2 | Floating point | Scalar Vector Matrix | Real Complex |
acoth 2 | Floating point | Scalar Vector Matrix | Real Complex |
acsc 2 | Floating point | Scalar Vector Matrix | Real Complex |
acscd 2 | Floating point | Scalar Vector Matrix | Real Complex |
acsch 2 | Floating point | Scalar Vector Matrix | Real Complex |
asec 2 | Floating point | Scalar Vector Matrix | Real Complex |
asecd 2 | Floating point | Scalar Vector Matrix | Real Complex |
asech 2 | Floating point | Scalar Vector Matrix | Real Complex |
asin | Floating point | Scalar | Real Complex input/complex output Real input/complex output |
asind 2 | Floating point | Scalar Vector Matrix | Real Complex |
asinh | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
atan | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
atan2 3 | Floating point | Scalar Vector Matrix | Real |
atan2d 2 | Floating point | Scalar Vector Matrix | Real |
atand 2 | Floating point | Scalar Vector Matrix | Real Complex |
atanh | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
ceil | Floating-point Scalar | Floating-point Scalar | Floating-point Scalar |
| Integer | Scalar | Real |
cos 3 | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
cosd 2 | Floating point | Scalar Vector Matrix | Real Complex |
cosh | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
cot 2 | Floating point | Scalar Vector Matrix | Real Complex |
cotd 2 | Floating point | Scalar Vector Matrix | Real Complex |
coth 2 | Floating point | Scalar Vector Matrix | Real Complex |
csc 2 | Floating point | Scalar Vector Matrix | Real Complex |
cscd 2 | Floating point | Scalar Vector Matrix | Real Complex |
csch 2 | Floating point | Scalar Vector Matrix | Real Complex |
| Integer Floating point | Scalar | Real |
exp | Floating point | Scalar Vector Matrix | Real |
fix | Floating point | Scalar | Real |
floor | Floating-point Scalar | Floating-point Scalar | Floating-point Scalar |
fmod 4 | Floating point | Scalar | Real |
frexp | Floating point | Scalar | Real |
hypot | Floating point | Scalar Vector Matrix | Real |
ldexp | Floating point | Scalar | Real |
ln | Floating point | Scalar | Real |
log | Floating point | Scalar Vector Matrix | Real |
log10 | Floating point | Scalar Vector Matrix | Real |
log2 2 | Floating point | Scalar Vector Matrix | Real Complex |
max | Integer Floating point Fixed point | Scalar | Real |
min | Integer Floating point Fixed point | Scalar | Real |
mod | Integer Floating point | Scalar Vector Matrix | Real |
pow | Floating point | Scalar Vector Matrix | Real |
rem | Floating point | Scalar Vector Matrix | Real |
round | Floating point | Scalar | Real |
rSqrt | Integer Floating point | Scalar Vector Matrix | Real |
saturate | Integer Floating point Fixed point | Scalar Vector Matrix | Real |
sec 2 | Floating point | Scalar Vector Matrix | Real Complex |
secd 2 | Floating point | Scalar Vector Matrix | Real Complex |
sech 2 | Floating point | Scalar Vector Matrix | Real Complex |
sign | Integer Floating point Fixed point | Scalar Vector Matrix | Real |
| Floating point | Scalar Vector Matrix | Real |
sin 3 | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
sincos 3 | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
sind 2 | Floating point | Scalar Vector Matrix | Real Complex |
sinh | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
sqrt | Integer Floating point Fixed point | Scalar Vector Matrix | Real |
tan | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
tand 2 | Floating point | Scalar Vector Matrix | Real Complex |
tanh | Floating point | Scalar Vector Matrix | Real Complex input/complex output Real input/complex output |
1 Wrap on integer overflow only. Clear block parameter Saturate on integer overflow. 2 Only when used with the MATLAB Function block. 3 Supports the CORDIC approximation method and the Lookup approximation method. 4 Stateflow® support only. |
Math Functions – Stateflow Support
When generating C/C++ code from Stateflow charts, depending on code replacement libraries available in your development environment, you can configure the code generator to replace instances of the following math functions with application-specific implementations.
Function | Data Type Support | Scalar, Vector, Matrix Support | Real, Complex Support |
---|---|---|---|
abs 1 | Integer Floating point | Scalar | Real |
acos 2 | Floating point | Scalar Vector Matrix | Real Complex Complex input/complex output Real input/complex output |
acosd 3 | Floating point | Scalar Vector Matrix | Real Complex |
acot 3 | Floating point | Scalar Vector Matrix | Real Complex |
acotd 3 | Floating point | Scalar Vector Matrix | Real Complex |
acoth 3,5 | Floating point | Scalar Vector Matrix | Real Complex |
acsc 3 | Floating point | Scalar Vector Matrix | Real Complex |
acscd 3 | Floating point | Scalar Vector Matrix | Real Complex |
acsch 3 | Floating point | Scalar Vector Matrix | Real Complex |
asec 3 | Floating point | Scalar Vector Matrix | Real Complex |
asecd 3 | Floating point | Scalar Vector Matrix | Real Complex |
asech 3 | Floating point | Scalar Vector Matrix | Real Complex |
asin 2 | Floating point | Scalar Vector Matrix | Real Complex Complex input/complex output Real input/complex output |
asind 3 | Floating point | Scalar Vector Matrix | Real Complex |
atan 2 | Floating point | Scalar Vector Matrix | Real Complex Complex input/complex output Real input/complex output |
atan2 2 | Floating point | Scalar Vector Matrix | Real |
atan2d 3 | Floating point | Scalar Vector Matrix | Real |
atand 3 | Floating point | Scalar Vector Matrix | Real Complex |
ceil | Floating-point Scalar | Floating-point Scalar | Floating-point Scalar |
cos 3 | Floating point | Scalar Vector Matrix | Real Complex Complex input/complex output Real input/complex output |
cosd 3 | Floating point | Scalar Vector Matrix | Real Complex |
cosh 2 | Floating point | Scalar Vector Matrix | Real Complex Complex input/complex output Real input/complex output |
cot 3 | Floating point | Scalar Vector Matrix | Real Complex |
cotd 3 | Floating point | Scalar Vector Matrix | Real Complex |
coth 3 | Floating point | Scalar Vector Matrix | Real Complex |
csc 3 | Floating point | Scalar Vector Matrix | Real Complex |
cscd 3 | Floating point | Scalar Vector Matrix | Real Complex |
csch 3 | Floating point | Scalar Vector Matrix | Real Complex |
exp | Floating point | Scalar | Real |
floor | Floating-point Scalar | Floating-point Scalar | Floating-point Scalar |
fmod | Floating point | Scalar | Real |
hypot 3 | Floating point | Scalar Vector Matrix | Real |
ldexp | Floating point | Scalar | Real |
log 2 | Floating point | Scalar Vector Matrix | Real Complex |
log10 2 | Floating point | Scalar Vector Matrix | Real Complex |
log2 3 | Floating point | Scalar Vector Matrix | Real Complex |
max | Integer Floating point | Scalar | Real |
min | Integer Floating point | Scalar | Real |
pow | Floating point | Scalar | Real |
sec 3 | Floating point | Scalar Vector Matrix | Real Complex |
secd 3 | Floating point | Scalar Vector Matrix | Real Complex |
sech 3 | Floating point | Scalar Vector Matrix | Real Complex |
sin 2 | Floating point | Scalar Vector Matrix | Real Complex Complex input/complex output Real input/complex output |
sind 3 | Floating point | Scalar Vector Matrix | Real Complex |
sinh 2 | Floating point | Scalar Vector Matrix | Real Complex Complex input/complex output Real input/complex output |
sqrt | Floating point | Scalar | Real |
tan 2 | Floating point | Scalar Vector Matrix | Real Complex Complex input/complex output Real input/complex output |
tand 3 | Floating point | Scalar Vector Matrix | Real Complex |
tanh 2 | Floating point | Scalar Vector Matrix | Real Complex Complex input/complex output Real input/complex output |
1 Wrap on integer overflow only. 2 For models involving vectors or matrices, the code generator replaces only functions coded in the MATLAB® action language. 3 The code generator replaces only functions coded in the MATLAB action language. |
Memory Functions
Depending on code replacement libraries available in your development environment, you can configure the code generator to replace instances of the following memory functions with application-specific implementations.
Function | Data Type Support | Scalar, Vector, Matrix Support | Real, Complex Support |
---|---|---|---|
memcmp | Void pointer (void* ) | Scalar Vector Matrix | Real Complex |
memcpy | Void pointer (void* ) | Scalar Vector Matrix | Real Complex |
memset | Void pointer (void* ) | Scalar Vector Matrix | Real Complex |
memset2zero | Void pointer (void* ) | Scalar Vector Matrix | Real Complex |
Some target processors provide optimized functions to set memory
to zero. Use the code replacement library programming interface to
replace the memset2zero
function with more efficient
target-specific functions.
Nonfinite Functions
Depending on code replacement libraries available in your development environment, you can configure the code generator to replace instances of the following nonfinite functions with application-specific implementations.
Function | Data Type Support | Scalar, Vector, Matrix Support | Real, Complex Support |
---|---|---|---|
getInf | Floating point | Scalar | Real |
getMinusInf | Floating point | Scalar | Real |
getNaN | Floating point | Scalar | Real |
rtIsInf | Floating point | Scalar | Real Complex |
rtIsNaN | Floating point | Scalar | Real Complex |
Mutex and Semaphore Functions
Mutex and semaphore functions control access to resources shared by multiple processes in multicore target environments. MathWorks® provides code replacement libraries that support mutex and semaphore replacement for Rate Transition and Task Transition blocks on Windows®, Linux®, Mac, and VxWorks® platforms.
Generated mutex and semaphore code typically consists of:
In model initialization code, an initialization function call to create a mutex or semaphore to control entry to a critical section of code.
In model step code:
Before code for a data transfer between tasks enters the critical section, mutex lock or semaphore wait function calls to reserve a critical section of code.
After code for a data transfer between tasks finishes executing the critical section, mutex unlock or semaphore post function calls to release the critical section of code.
In model termination code, an optional destroy function call to explicitly delete the mutex or semaphore.
Depending on code replacement libraries available in your development environment, you can configure the code generator to replace instances of the following mutex and semaphore functions with application-specific implementations.
Function | Key |
---|---|
Mutex Destroy | RTW_MUTEX_DESTROY |
Mutex Init | RTW_MUTEX_INIT |
Mutex Lock | RTW_MUTEX_LOCK |
Mutex Unlock | RTW_MUTEX_UNLOCK |
Semaphore Destroy | RTW_SEM_DESTROY |
Semaphore Init | RTW_SEM_INIT |
Semaphore Post | RTW_SEM_POST |
Semaphore Wait | RTW_SEM_WAIT |
Operators
When generating C/C++ code from a Simulink model, depending on code replacement libraries available in your development environment, you can configure the code generator to replace instances of the following operators with application-specific implementations.
Mixed data type support indicates that you can specify different data types for different inputs.
Operator | Key | Data Type Support | Scalar, Vector, Matrix Support | Real, Complex Support |
---|---|---|---|---|
Addition (+ )1 | RTW_OP_ADD | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Subtraction (- )1 | RTW_OP_MINUS | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Multiplication (* )2 | RTW_OP_MUL | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Division (/ ) | RTW_OP_DIV | Integer Floating point Fixed-point Mixed | Scalar | Real Complex |
Data type conversion (cast) | RTW_OP_CAST | Integer Floating point3 Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Shift left (<< ) | RTW_OP_SL | Integer Fixed-point Mixed | Scalar Vector Matrix4 | Real |
Shift right arithmetic (>> )5 | RTW_OP_SRA | Integer Fixed-point Mixed | Scalar Vector Matrix4 | Real |
Shift right logical (>> ) | RTW_OP_SRL | Integer Fixed-point Mixed | Scalar Vector Matrix4 | Real |
Element-wise matrix multiplication (.* )6 | RTW_OP_ELEM_MUL | Integer Floating point Fixed-point Mixed | Vector Matrix | Real Complex |
Matrix right division (/ ) | RTW_OP_RDIV | Integer Floating point Fixed-point Mixed | Vector Matrix | Real Complex |
Matrix left division (\ ) | RTW_OP_LDIV | Integer Floating point Fixed-point Mixed | Vector Matrix | Real Complex |
Matrix inversion (inv ) | RTW_OP_INV | Integer Floating point Fixed-point Mixed | Vector Matrix | Real Complex |
Complex conjugation | RTW_OP_CONJUGATE | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Transposition (.' ) | RTW_OP_TRANS | Integer Floating point Fixed-point Mixed | Vector Matrix | Real Complex |
Hermitian (complex conjugate) transposition (' ) | RTW_OP_HERMITIAN | Integer Floating point Fixed-point Mixed | Vector Matrix | Real Complex |
Multiplication with transposition2 | RTW_OP_TRMUL | Integer Floating point Fixed-point Mixed | Vector Matrix | Real Complex |
Multiplication with Hermitian transposition2 | RTW_OP_HMMUL | Integer Floating point Fixed-point Mixed | Vector Matrix | Real Complex |
Multiplication followed by shift right arithmetic (u1*u2>>u3 )7 | RTW_OP_MUL_SRA | Integer Fixed-point | Scalar | Real |
Multiplication followed by division (u1*u2/u3 )8 | RTW_OP_MULDIV | Integer Fixed-point | Scalar | Real |
Greater than (> ) | RTW_OP_GREATER_ | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Greater than or equal (>= ) | RTW_OP_GREATER_ | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Less than (< ) | RTW_OP_LESS_THAN | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Less than or equal (<= ) | RTW_OP_LESS_THAN_ | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Equal (== ) | RTW_OP_EQUAL | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
Not equal (!= ) | RTW_OP_NOT_EQUAL | Integer Floating point Fixed-point Mixed | Scalar Vector Matrix | Real Complex |
1 See Replace Addition and Subtraction Operator Code for details to consider when defining mappings for addition and subtraction code replacements. 2 Can map to Basic Linear Algebra Subroutine (BLAS) multiplication functions. 3 Scaled floating point is not supported. 4 Shift operator replacement with matrix data is supported for shift values that you specify with an input port. Replacement is not supported for shift values that you specify in a block parameter dialog. 5 The code generator converts some arithmetic shift rights to logical shift rights. To avoid unexpected results, when creating a code replacement library that includes a table entry for an arithmetic shift right implementation, also include an entry for a logical shift right implementation. 6 Use
the multiplication ( 7 Requires
scalar, real, or fixed-point data types with zero bias; output type
of the multiplication operation to accommodate all
possible output values; shift operand is an unsigned integer; and
net slope is equal to 1 ( 8 Requires
scalar, real, or fixed-point data types with zero bias; output type
of the multiplication operation to accommodate all possible
output values; and net slope is equal to 1 ( |
Blocks You Can Replace
This table shows the blocks you can replace, the key for each block, block parameter settings that you can specify, and valid block parameter arguments for each block. The table also indicates which block parameter settings and block parameter arguments you are required to set in a block replacement entry for each block. For fixed-point implementations for supported blocks, you must also specify the required data type parameters.
Block | Key | Block Parameters | Block Parameter Arguments | Data Type Block Parameters for Fixed-Point Implementations |
---|---|---|---|---|
Discrete FIR Filter | DiscreteFir | Required parameters:
|
| Required parameters:
|
Optional parameters:
| Not supported — do not specify these parameters:
| |||
Biquad Filter (DSP System Toolbox) | Biquad Filter | Required parameters:
|
| Required parameters:
|
Optional parameters:
| ||||
FFT (DSP System Toolbox) | FFT | Required parameters:
| None | Fixed-point implementations are not supported. |
IFFT (DSP System Toolbox) | IFFT | Required parameters:
| None | Fixed-point implementations are not supported. |
FIR Decimation (DSP System Toolbox) | FIR Decimation | Required parameters:
|
| Fixed-point implementations are not supported. |
FIR Interpolation (DSP System Toolbox) | FIR Interpolation | Required parameters:
|
| Fixed-point implementations are not supported. |