Main Content

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 license and want to develop a custom code replacement library, see Code Replacement Customization.

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.

FunctionData Type SupportScalar, Vector, Matrix SupportReal, Complex Support
abs1Integer
Floating point
Fixed point
Scalar
Vector
Matrix
Real
acosFloating pointScalarReal
Complex input/complex output
Real input/complex output
acosd2Floating pointScalar
Vector
Matrix
Real
Complex
acoshFloating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
acot2Floating pointScalar
Vector
Matrix
Real
Complex
acotd2Floating pointScalar
Vector
Matrix
Real
Complex
acoth2Floating pointScalar
Vector
Matrix
Real
Complex
acsc2Floating pointScalar
Vector
Matrix
Real
Complex
acscd2Floating pointScalar
Vector
Matrix
Real
Complex
acsch2Floating pointScalar
Vector
Matrix
Real
Complex
asec2Floating pointScalar
Vector
Matrix
Real
Complex
asecd2Floating pointScalar
Vector
Matrix
Real
Complex
asech2Floating pointScalar
Vector
Matrix
Real
Complex
asinFloating pointScalarReal
Complex input/complex output
Real input/complex output
asind2Floating pointScalar
Vector
Matrix
Real
Complex
asinhFloating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
atanFloating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
atan2Floating pointScalar
Vector
Matrix
Real
atan2d2Floating pointScalar
Vector
Matrix
Real
atand2Floating pointScalar
Vector
Matrix
Real
Complex
atanhFloating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
ceilFloating-point
Scalar
Floating-point
Scalar
Floating-point
Scalar
cos3Floating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
cosd2Floating pointScalar
Vector
Matrix
Real
Complex
coshFloating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
cot2Floating pointScalar
Vector
Matrix
Real
Complex
cotd2Floating pointScalar
Vector
Matrix
Real
Complex
coth2Floating pointScalar
Vector
Matrix
Real
Complex
csc2Floating pointScalar
Vector
Matrix
Real
Complex
cscd2Floating pointScalar
Vector
Matrix
Real
Complex
csch2Floating pointScalar
Vector
Matrix
Real
Complex
exactrSqrtInteger
Floating point
ScalarReal
expFloating pointScalar
Vector
Matrix
Real
fixFloating pointScalarReal
floorFloating-point
Scalar
Floating-point
Scalar
Floating-point
Scalar
fmod4Floating pointScalarReal
frexpFloating pointScalarReal
hypotFloating pointScalar
Vector
Matrix
Real
ldexpFloating pointScalarReal
lnFloating pointScalarReal
logFloating pointScalar
Vector
Matrix
Real
log10Floating pointScalar
Vector
Matrix
Real
log22Floating pointScalar
Vector
Matrix
Real
Complex
maxInteger
Floating point
Fixed point
ScalarReal
minInteger
Floating point
Fixed point
ScalarReal
modInteger
Floating point
Scalar
Vector
Matrix
Real
powFloating pointScalar
Vector
Matrix
Real
remFloating pointScalar
Vector
Matrix
Real
roundFloating pointScalarReal
rSqrtInteger
Floating point
Scalar
Vector
Matrix
Real
saturateInteger
Floating point
Fixed point
Scalar
Vector
Matrix
Real
sec2Floating pointScalar
Vector
Matrix
Real
Complex
secd2Floating pointScalar
Vector
Matrix
Real
Complex
sech2Floating pointScalar
Vector
Matrix
Real
Complex
signInteger
Floating point
Fixed point
Scalar
Vector
Matrix
Real
signPowFloating pointScalar
Vector
Matrix
Real
sin3Floating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
sincos3Floating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
sind2Floating pointScalar
Vector
Matrix
Real
Complex
sinhFloating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
sqrtInteger
Floating point
Fixed point
Scalar
Vector
Matrix
Real
tanFloating pointScalar
Vector
Matrix
Real
Complex input/complex output
Real input/complex output
tand2Floating pointScalar
Vector
Matrix
Real
Complex
tanhFloating pointScalar
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.

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.

FunctionData Type SupportScalar, Vector, Matrix SupportReal, Complex Support
abs1Integer
Floating point
ScalarReal
acos2Floating pointScalar
Vector
Matrix
Real
Complex
Complex input/complex output
Real input/complex output
acosd3Floating pointScalar
Vector
Matrix
Real
Complex
acot3Floating pointScalar
Vector
Matrix
Real
Complex
acotd3Floating pointScalar
Vector
Matrix
Real
Complex
acoth3,5Floating pointScalar
Vector
Matrix
Real
Complex
acsc3Floating pointScalar
Vector
Matrix
Real
Complex
acscd3Floating pointScalar
Vector
Matrix
Real
Complex
acsch3Floating pointScalar
Vector
Matrix
Real
Complex
asec3Floating pointScalar
Vector
Matrix
Real
Complex
asecd3Floating pointScalar
Vector
Matrix
Real
Complex
asech3Floating pointScalar
Vector
Matrix
Real
Complex
asin2Floating pointScalar
Vector
Matrix
Real
Complex
Complex input/complex output
Real input/complex output
asind3Floating pointScalar
Vector
Matrix
Real
Complex
atan2Floating pointScalar
Vector
Matrix
Real
Complex
Complex input/complex output
Real input/complex output
atan22Floating pointScalar
Vector
Matrix
Real
atan2d3Floating pointScalar
Vector
Matrix
Real
atand3Floating pointScalar
Vector
Matrix
Real
Complex
ceilFloating-point
Scalar
Floating-point
Scalar
Floating-point
Scalar
cos3Floating pointScalar
Vector
Matrix
Real
Complex
Complex input/complex output
Real input/complex output
cosd3Floating pointScalar
Vector
Matrix
Real
Complex
cosh2Floating pointScalar
Vector
Matrix
Real
Complex
Complex input/complex output
Real input/complex output
cot3Floating pointScalar
Vector
Matrix
Real
Complex
cotd3Floating pointScalar
Vector
Matrix
Real
Complex
coth3Floating pointScalar
Vector
Matrix
Real
Complex
csc3Floating pointScalar
Vector
Matrix
Real
Complex
cscd3Floating pointScalar
Vector
Matrix
Real
Complex
csch3Floating pointScalar
Vector
Matrix
Real
Complex
expFloating pointScalarReal
floorFloating-point
Scalar
Floating-point
Scalar
Floating-point
Scalar
fmodFloating pointScalarReal
hypot3Floating pointScalar
Vector
Matrix
Real
ldexpFloating pointScalarReal
log2Floating pointScalar
Vector
Matrix
Real
Complex
log102Floating pointScalar
Vector
Matrix
Real
Complex
log23Floating pointScalar
Vector
Matrix
Real
Complex
maxInteger
Floating point
ScalarReal
minInteger
Floating point
ScalarReal
powFloating pointScalarReal
sec3Floating pointScalar
Vector
Matrix
Real
Complex
secd3Floating pointScalar
Vector
Matrix
Real
Complex
sech3Floating pointScalar
Vector
Matrix
Real
Complex
sin2Floating pointScalar
Vector
Matrix
Real
Complex
Complex input/complex output
Real input/complex output
sind3Floating pointScalar
Vector
Matrix
Real
Complex
sinh2Floating pointScalar
Vector
Matrix
Real
Complex
Complex input/complex output
Real input/complex output
sqrtFloating pointScalarReal
tan2Floating pointScalar
Vector
Matrix
Real
Complex
Complex input/complex output
Real input/complex output
tand3Floating pointScalar
Vector
Matrix
Real
Complex
tanh2Floating pointScalar
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.

4Embedded Coder® does not support the use of code replacement libraries for machine-parented data.

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.

FunctionData Type SupportScalar, Vector, Matrix SupportReal, Complex Support
memcmpVoid pointer (void*)Scalar
Vector
Matrix
Real
Complex
memcpyVoid pointer (void*)Scalar
Vector
Matrix
Real
Complex
memsetVoid pointer (void*)Scalar
Vector
Matrix
Real
Complex
memset2zeroVoid 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.

FunctionData Type SupportScalar, Vector, Matrix SupportReal, Complex Support
getInfFloating pointScalarReal
getMinusInfFloating pointScalarReal
getNaNFloating pointScalarReal
rtIsInfFloating pointScalarReal
Complex
rtIsNaNFloating pointScalarReal
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.

FunctionKey
Mutex DestroyRTW_MUTEX_DESTROY
Mutex InitRTW_MUTEX_INIT
Mutex LockRTW_MUTEX_LOCK
Mutex UnlockRTW_MUTEX_UNLOCK
Semaphore DestroyRTW_SEM_DESTROY
Semaphore InitRTW_SEM_INIT
Semaphore PostRTW_SEM_POST
Semaphore WaitRTW_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.

OperatorKeyData Type SupportScalar, Vector, Matrix SupportReal, Complex Support
Addition (+)1RTW_OP_ADDInteger
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Subtraction (-)1RTW_OP_MINUSInteger
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Multiplication (*)2RTW_OP_MULInteger
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Division (/)RTW_OP_DIVInteger
Floating point
Fixed-point
Mixed
ScalarReal
Complex
Data type conversion (cast)RTW_OP_CASTInteger
Floating point3
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Shift left (<<)RTW_OP_SLInteger
Fixed-point
Mixed
Scalar
Vector
Matrix4
Real
Shift right arithmetic (>>)5RTW_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 (.*)6RTW_OP_ELEM_MULInteger
Floating point
Fixed-point
Mixed
Vector
Matrix
Real
Complex
Matrix right division (/)RTW_OP_RDIVInteger
Floating point
Fixed-point
Mixed
Vector
Matrix
Real
Complex
Matrix left division (\)RTW_OP_LDIVInteger
Floating point
Fixed-point
Mixed
Vector
Matrix
Real
Complex
Matrix inversion (inv)RTW_OP_INVInteger
Floating point
Fixed-point
Mixed
Vector
Matrix
Real
Complex
Complex conjugationRTW_OP_CONJUGATEInteger
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Transposition (.')RTW_OP_TRANSInteger
Floating point
Fixed-point
Mixed
Vector
Matrix
Real
Complex
Hermitian (complex conjugate) transposition (')RTW_OP_HERMITIANInteger
Floating point
Fixed-point
Mixed
Vector
Matrix
Real
Complex
Multiplication with transposition2RTW_OP_TRMULInteger
Floating point
Fixed-point
Mixed
Vector
Matrix
Real
Complex
Multiplication with Hermitian transposition2RTW_OP_HMMULInteger
Floating point
Fixed-point
Mixed
Vector
Matrix
Real
Complex
Multiplication followed by shift right arithmetic (u1*u2>>u3)7RTW_OP_MUL_SRAInteger
Fixed-point
ScalarReal
Multiplication followed by division (u1*u2/u3)8RTW_OP_MULDIVInteger
Fixed-point
ScalarReal
Greater than (>)RTW_OP_GREATER_
THAN
Integer
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Greater than or equal (>=)RTW_OP_GREATER_
THAN_OR_EQUAL
Integer
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Less than (<)RTW_OP_LESS_THANInteger
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Less than or equal (<=)RTW_OP_LESS_THAN_
OR_EQUAL
Integer
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Equal (==)RTW_OP_EQUALInteger
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex
Not equal (!=)RTW_OP_NOT_EQUALInteger
Floating point
Fixed-point
Mixed
Scalar
Vector
Matrix
Real
Complex

1 See Addition and Subtraction Operator Code Replacement 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).

Related Topics