Code You Can Replace From Simulink Models - MATLAB & Simulink (original) (raw)
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 insideMATLAB 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 |
---|---|---|---|
abs1 | IntegerFloating point Fixed point | ScalarVectorMatrix | Real |
acos | Floating point | Scalar | RealComplex input/complex outputReal input/complex output |
acosd2 | Floating point | ScalarVectorMatrix | RealComplex |
acosh | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
acot2 | Floating point | ScalarVectorMatrix | RealComplex |
acotd2 | Floating point | ScalarVectorMatrix | RealComplex |
acoth2 | Floating point | ScalarVectorMatrix | RealComplex |
acsc2 | Floating point | ScalarVectorMatrix | RealComplex |
acscd2 | Floating point | ScalarVectorMatrix | RealComplex |
acsch2 | Floating point | ScalarVectorMatrix | RealComplex |
asec2 | Floating point | ScalarVectorMatrix | RealComplex |
asecd2 | Floating point | ScalarVectorMatrix | RealComplex |
asech2 | Floating point | ScalarVectorMatrix | RealComplex |
asin | Floating point | Scalar | RealComplex input/complex outputReal input/complex output |
asind2 | Floating point | ScalarVectorMatrix | RealComplex |
asinh | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
atan | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
atan23 | Floating point | ScalarVectorMatrix | Real |
atan2d2 | Floating point | ScalarVectorMatrix | Real |
atand2 | Floating point | ScalarVectorMatrix | RealComplex |
atanh | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
ceil | Floating-pointScalar | Floating-pointScalar | Floating-pointScalar |
circularIndex — Delay block with Use circular buffer for state selected. | Integer | Scalar | Real |
cos3 | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
cosd2 | Floating point | ScalarVectorMatrix | RealComplex |
cosh | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
cot2 | Floating point | ScalarVectorMatrix | RealComplex |
cotd2 | Floating point | ScalarVectorMatrix | RealComplex |
coth2 | Floating point | ScalarVectorMatrix | RealComplex |
csc2 | Floating point | ScalarVectorMatrix | RealComplex |
cscd2 | Floating point | ScalarVectorMatrix | RealComplex |
csch2 | Floating point | ScalarVectorMatrix | RealComplex |
exactrSqrt — Sqrt block with: Function set to rSqrtMethod set to Exact | IntegerFloating point | Scalar | Real |
exp | Floating point | ScalarVectorMatrix | Real |
fix | Floating point | Scalar | Real |
floor | Floating-pointScalar | Floating-pointScalar | Floating-pointScalar |
fmod4 | Floating point | Scalar | Real |
frexp | Floating point | Scalar | Real |
hypot | Floating point | ScalarVectorMatrix | Real |
ldexp | Floating point | Scalar | Real |
ln | Floating point | Scalar | Real |
log | Floating point | ScalarVectorMatrix | Real |
log10 | Floating point | ScalarVectorMatrix | Real |
log22 | Floating point | ScalarVectorMatrix | RealComplex |
max | IntegerFloating point Fixed point | Scalar | Real |
min | IntegerFloating point Fixed point | Scalar | Real |
mod | IntegerFloating point | ScalarVectorMatrix | Real |
pow | Floating point | ScalarVectorMatrix | Real |
relay | IntegerFloating pointFixed-pointBoolean | ScalarVectorMatrix | Real |
rem | Floating point | ScalarVectorMatrix | Real |
round | Floating point | Scalar | Real |
rSqrt | IntegerFloating point | ScalarVectorMatrix | Real |
saturate | IntegerFloating point Fixed point | ScalarVectorMatrix | Real |
sec2 | Floating point | ScalarVectorMatrix | RealComplex |
secd2 | Floating point | ScalarVectorMatrix | RealComplex |
sech2 | Floating point | ScalarVectorMatrix | RealComplex |
sign | IntegerFloating point Fixed point | ScalarVectorMatrix | Real |
signPow — Math Function block with: Function set to powSigned power enabled | Floating point | ScalarVectorMatrix | Real |
sin3 | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
sincos3 | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
sind2 | Floating point | ScalarVectorMatrix | RealComplex |
sinh | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
sqrt | IntegerFloating point Fixed point | ScalarVectorMatrix | Real |
tan | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal input/complex output |
tand2 | Floating point | ScalarVectorMatrix | RealComplex |
tanh | Floating point | ScalarVectorMatrix | RealComplex input/complex outputReal 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 |
---|---|---|---|
abs1 | IntegerFloating point | Scalar | Real |
acos2 | Floating point | ScalarVectorMatrix | RealComplexComplex input/complex outputReal input/complex output |
acosd3 | Floating point | ScalarVectorMatrix | RealComplex |
acot3 | Floating point | ScalarVectorMatrix | RealComplex |
acotd3 | Floating point | ScalarVectorMatrix | RealComplex |
acoth3,5 | Floating point | ScalarVectorMatrix | RealComplex |
acsc3 | Floating point | ScalarVectorMatrix | RealComplex |
acscd3 | Floating point | ScalarVectorMatrix | RealComplex |
acsch3 | Floating point | ScalarVectorMatrix | RealComplex |
asec3 | Floating point | ScalarVectorMatrix | RealComplex |
asecd3 | Floating point | ScalarVectorMatrix | RealComplex |
asech3 | Floating point | ScalarVectorMatrix | RealComplex |
asin2 | Floating point | ScalarVectorMatrix | RealComplexComplex input/complex outputReal input/complex output |
asind3 | Floating point | ScalarVectorMatrix | RealComplex |
atan2 | Floating point | ScalarVectorMatrix | RealComplexComplex input/complex outputReal input/complex output |
atan22 | Floating point | ScalarVectorMatrix | Real |
atan2d3 | Floating point | ScalarVectorMatrix | Real |
atand3 | Floating point | ScalarVectorMatrix | RealComplex |
ceil | Floating-pointScalar | Floating-pointScalar | Floating-pointScalar |
cos3 | Floating point | ScalarVectorMatrix | RealComplexComplex input/complex outputReal input/complex output |
cosd3 | Floating point | ScalarVectorMatrix | RealComplex |
cosh2 | Floating point | ScalarVectorMatrix | RealComplexComplex input/complex outputReal input/complex output |
cot3 | Floating point | ScalarVectorMatrix | RealComplex |
cotd3 | Floating point | ScalarVectorMatrix | RealComplex |
coth3 | Floating point | ScalarVectorMatrix | RealComplex |
csc3 | Floating point | ScalarVectorMatrix | RealComplex |
cscd3 | Floating point | ScalarVectorMatrix | RealComplex |
csch3 | Floating point | ScalarVectorMatrix | RealComplex |
exp | Floating point | Scalar | Real |
floor | Floating-pointScalar | Floating-pointScalar | Floating-pointScalar |
fmod | Floating point | Scalar | Real |
hypot3 | Floating point | ScalarVectorMatrix | Real |
ldexp | Floating point | Scalar | Real |
log2 | Floating point | ScalarVectorMatrix | RealComplex |
log102 | Floating point | ScalarVectorMatrix | RealComplex |
log23 | Floating point | ScalarVectorMatrix | RealComplex |
max | IntegerFloating point | Scalar | Real |
min | IntegerFloating point | Scalar | Real |
pow | Floating point | Scalar | Real |
sec3 | Floating point | ScalarVectorMatrix | RealComplex |
secd3 | Floating point | ScalarVectorMatrix | RealComplex |
sech3 | Floating point | ScalarVectorMatrix | RealComplex |
sin2 | Floating point | ScalarVectorMatrix | RealComplexComplex input/complex outputReal input/complex output |
sind3 | Floating point | ScalarVectorMatrix | RealComplex |
sinh2 | Floating point | ScalarVectorMatrix | RealComplexComplex input/complex outputReal input/complex output |
sqrt | Floating point | Scalar | Real |
tan2 | Floating point | ScalarVectorMatrix | RealComplexComplex input/complex outputReal input/complex output |
tand3 | Floating point | ScalarVectorMatrix | RealComplex |
tanh2 | Floating point | ScalarVectorMatrix | RealComplexComplex input/complex outputReal 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*) | ScalarVectorMatrix | RealComplex |
memcpy | Void pointer (void*) | ScalarVectorMatrix | RealComplex |
memset | Void pointer (void*) | ScalarVectorMatrix | RealComplex |
memset2zero | Void pointer (void*) | ScalarVectorMatrix | RealComplex |
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 | RealComplex |
rtIsNaN | Floating point | Scalar | RealComplex |
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 | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
Subtraction (-)1 | RTW_OP_MINUS | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
Multiplication (*)2 | RTW_OP_MUL | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
Division (/) | RTW_OP_DIV | IntegerFloating pointFixed-pointMixed | Scalar | RealComplex |
Data type conversion (cast) | RTW_OP_CAST | IntegerFloating point3Fixed-pointMixed | ScalarVectorMatrix | RealComplex |
Shift left (<<) | RTW_OP_SL | IntegerFixed-pointMixed | ScalarVectorMatrix4 | Real |
Shift right arithmetic (>>)5 | RTW_OP_SRA | IntegerFixed-pointMixed | ScalarVectorMatrix4 | Real |
Shift right logical (>>) | RTW_OP_SRL | IntegerFixed-pointMixed | ScalarVectorMatrix4 | Real |
Element-wise matrix multiplication (.*)6 | RTW_OP_ELEM_MUL | IntegerFloating pointFixed-pointMixed | VectorMatrix | RealComplex |
Matrix right division (/) | RTW_OP_RDIV | IntegerFloating pointFixed-pointMixed | VectorMatrix | RealComplex |
Matrix left division (\) | RTW_OP_LDIV | IntegerFloating pointFixed-pointMixed | VectorMatrix | RealComplex |
Matrix inversion (inv) | RTW_OP_INV | IntegerFloating pointFixed-pointMixed | VectorMatrix | RealComplex |
Complex conjugation | RTW_OP_CONJUGATE | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
Transposition (.') | RTW_OP_TRANS | IntegerFloating pointFixed-pointMixed | VectorMatrix | RealComplex |
Hermitian (complex conjugate) transposition (') | RTW_OP_HERMITIAN | IntegerFloating pointFixed-pointMixed | VectorMatrix | RealComplex |
Multiplication with transposition2 | RTW_OP_TRMUL | IntegerFloating pointFixed-pointMixed | VectorMatrix | RealComplex |
Multiplication with Hermitian transposition2 | RTW_OP_HMMUL | IntegerFloating pointFixed-pointMixed | VectorMatrix | RealComplex |
Multiplication followed by shift right arithmetic (u1*u2>>u3)7 | RTW_OP_MUL_SRA | IntegerFixed-point | Scalar | Real |
Multiplication followed by division (u1*u2/u3)8 | RTW_OP_MULDIV | IntegerFixed-point | Scalar | Real |
Greater than (>) | RTW_OP_GREATER_THAN | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
Greater than or equal (>=) | RTW_OP_GREATER_THAN_OR_EQUAL | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
Less than (<) | RTW_OP_LESS_THAN | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
Less than or equal (<=) | RTW_OP_LESS_THAN_OR_EQUAL | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
Equal (==) | RTW_OP_EQUAL | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
Not equal (!=) | RTW_OP_NOT_EQUAL | IntegerFloating pointFixed-pointMixed | ScalarVectorMatrix | RealComplex |
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 (*) operator (RTW_OP_MUL) for scalar 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 (U1_slope * U2_slope == Mul_output_slope and Mul_output_slope == output_slope_of_shift_operation).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 (U1_slope * U2_slope == Mul_output_slope == U3_slope * Div_output_slope). |
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: Coefficient source (CoefSource)Filter structure (FilterStructure)Input processing (InputProcessing) | Coefficients (Coefficients)Initial states (InitialStates) | Required parameters: Tap sum (TapSumDataTypeStr)Coefficients (CoefDataTypeStr)Product output (ProductDataTypeStr)Accumulator (AccumDataTypeStr)State (StateDataTypeStr)Output (OutDataTypeStr)Lock data type settings against changes by the fixed-point tools (LockScale)Integer rounding mode (RndMeth)Saturate on integer overflow (SaturateOnIntegerOverflow) |
Optional parameters: Coefficients (Coefficients)Initial states (InitialStates)Show enable port (ShowEnablePort)External reset (ExternalReset)Sample time (SampleTime) | Not supported — do not specify these parameters: Coefficients minimum (CoeffMin)Coefficients maximum (CoeffMax)Output minimum (OutMin)Output maximum (OutMax) | |||
Biquad Filter (DSP System Toolbox) | Biquad Filter | Required parameters: Coefficient source (FilterSource)Filter structure (IIRFiltStruct)Input processing (InputProcessing) | SOS Matrix (Mx6) (BiQuadCoeffs)Initial conditions (IC)Initial conditions on zeros side (ICnum)Initial conditions on poles side (ICden)Scale values (ScaleValue) | Required parameters: Rounding mode (RoundingMode)Saturate on integer overflow (OverflowMode)Coefficients (firstCoeffMode)Product output (prodOutputMode)Accumulator (accumMode)States (memoryMode)Output (outputMode)Section input (stageInputMode)Section output (stageOutputMode)Multiplicand (multiplicandMode)Lock data type settings against changes by the fixed-point tools (LockScale) |
Optional parameters: SOS Matrix (Mx6) (BiQuadCoeffs)Scale values (ScaleValues)Initial conditions (IC)Initial conditions on zeros side (ICnum)Initial conditions on poles side (ICden)Scale values mode (ScaleValueMode)Action when the a0 values of the SOS matrix are not one (a0Flag)Optimize unity scale values (OptimizeUnityScaleValues) | ||||
FFT (DSP System Toolbox) | FFT | Required parameters: FFT implementation (FFTImplementation)Output in bit-reversed order (BitRevOrder)Divide output by FFT length (Normalize)Inherit FFT length from input dimensions (InheritFFTLength)FFT length (FFTLength) — Required ifInheritFFTLength is offWrap input data when FFT length is shorter than input length (WrapInput) — Required ifInheritFFTLength is off | None | Fixed-point implementations are not supported. |
IFFT (DSP System Toolbox) | IFFT | Required parameters: FFT implementation (FFTImplementation)Input is in bit-reversed order (BitRevOrder)Input is conjugate symmetric (cs_in)Divide output by FFT length (Normalize)Inherit FFT length from input dimensions (InheritFFTLength)FFT length (FFTLength) — Required ifInheritFFTLength is offWrap input data when FFT length is shorter than input length (WrapInput) — Required ifInheritFFTLength is off | None | Fixed-point implementations are not supported. |
FIR Decimation (DSP System Toolbox) | FIR Decimation | Required parameters: Coefficient source (FilterSource) — Must be set to dialog parameters, input port, or autoDecimation factor (D)FIR filter coefficients (h) — Required ifFilterSource is dialog parametersFilter structure (FiltStruct)Input processing (InputProcessing)Rate options (framing)Output buffer initial conditions (OutputBufInitCond) — Required ifframing is allow multirate processingAllow arbitrary frame length for fixed-size input signals (AllowArbitraryInputLength) — Required if framing is enforce single-rate processing | Decimation factor (D)FIR filter coefficients (h) — Required ifFilterSource is dialog parameters | Fixed-point implementations are not supported. |
FIR Interpolation (DSP System Toolbox) | FIR Interpolation | Required parameters: Coefficient source (FilterSource) — Must be set to dialog parameters, input port, or autoInterpolation factor (L)FIR filter coefficients (h) — Required ifFilterSource is dialog parametersInput processing (InputProcessing)Rate options (framing)Output buffer initial conditions (OutputBufInitCond) — Required ifframing is allow multirate processing | Interpolation factor (L)FIR filter coefficients (h) — Required ifFilterSource is dialog parameters | Fixed-point implementations are not supported. |