Fixed-Point Operator Code Replacement
If you have a Fixed-Point Designer™ license, you can define fixed-point operator code replacement entries to match:
A binary-point-only scaling combination on the operator inputs and output.
A slope bias scaling combination on the operator inputs and output.
Relative scaling or net slope between multiplication or division operator inputs and output. Use one of these methods to map a range of slope and bias values to a replacement function for multiplication or division.
Equal slope and zero net bias across addition or subtraction operator inputs and output. Use this method to disregard specific slope and bias values and map relative slope and bias values to a replacement function for addition or subtraction.
Common Ways to Match Fixed-Point Operator Entries
The following table maps common ways to match fixed-point operator code replacement entries with the associated fixed-point parameters that you specify in a code replacement table definition file.
Match | Create entry | Minimally specify parameters |
---|---|---|
A specific binary-point-only scaling combination on the operator inputs and output. | RTW.TflCOperationEntry |
|
A specific slope bias scaling combination on the operator inputs and output. | RTW.TflCOperationEntry |
|
Net slope between operator inputs and output (multiplication and division). | RTW.TflCOperationEntryGenerator_NetSlope |
|
Relative scaling between operator inputs and output (multiplication and division). | RTW.TflCOperationEntryGenerator |
|
Equal slope and zero net bias across operator inputs and output (addition and subtraction). | RTW.TflCOperationEntryGenerator |
|
Fixed-Point Numbers and Arithmetic
Fixed-point numbers use integers and integer arithmetic to represent real numbers and arithmetic with the following encoding scheme:
is an arbitrarily precise real-world value.
is the approximate real-world value that results from fixed-point representation.
is an integer that encodes , referred to as the quantized integer.
is a coefficient of , referred to as the slope.
is an additive correction, referred to as the bias.
The general equation for an operation between fixed-point operands is:
The objective of fixed-point operator replacement is to replace an operator that accepts and returns fixed-point or integer inputs and output with a function that accepts and returns built-in C numeric data types. The following sections provide additional programming information for each supported operator.
Addition
The operation V0 = V1 + V2 implies that
If an addition replacement function is defined such that the scaling on the operands and sum are equal and the net bias
is zero (for example, a function s8_add_s8_s8
that adds
two signed 8-bit values and produces a signed 8-bit result), then the operator entry must set
the operator entry parameters SlopesMustBeTheSame
and
MustHaveZeroNetBias
to true
. To match for
replacement, the slopes must be the same for all addition
conceptual arguments. (For parameter descriptions, see the reference page for the function
setTflCOperationEntryParameters
.)
Subtraction
The operation V0 = V1 − V2 implies that
If a subtraction replacement function is defined such that the scaling on the operands and difference are equal and the net bias
is zero (for example, a function s8_sub_s8_s8
that
subtracts two signed 8-bit values and produces a signed 8-bit result), then the operator entry
must set the operator entry parameters SlopesMustBeTheSame
and
MustHaveZeroNetBias
to true
. To match for
replacement, the slopes must be the same for all subtraction
conceptual arguments. (For parameter descriptions, see the reference page for the function
setTflCOperationEntryParameters
.)
Multiplication
There are different ways to specify multiplication replacements. The most direct way is to
specify an exact match of the input and output types. This is feasible if a model contains
only a few known slope and bias combinations. Use the TflCOperationEntry
class and specify the exact values of slope and bias on each argument. For scenarios where
there are numerous slope/bias combinations, it is not feasible to specify each value with a
different entry. Use a net slope entry or create a custom entry.
The operation V0 = V1 * V2 implies, for binary-point-only scaling, that
where Sn is the net slope.
It is common to replace all multiplication operations
that have a net slope of 1.0 with a function that performs C-style multiplication. For
example, to replace all signed 8-bit multiplications that
have a net scaling of 1.0 with the s8_mul_s8_u8_
replacement function, the
operator entry must define a net slope factor,
F2E. You specify the values for
F and E using operator entry parameters
NetSlopeAdjustmentFactor
and NetFixedExponent
. For
the s8_mul_s8_u8
function, set NetSlopeAdjustmentFactor
to 1 and NetFixedExponent
to 0.0. Also, set the operator entry parameter
SlopesMustBeTheSame
to false
and the parameter
MustHaveZeroNetBias
to true
. To match for
replacement, the biases must be zero for all multiplication
conceptual arguments. (For parameter descriptions, see the reference page for the function
setTflCOperationEntryParameters
.)
Note
When an operator entry specifies NetSlopeAdjustmentFactor
and NetFixedExponent
,
matching entries must have arguments with zero bias.
Division
There are different ways to specify division replacements. The most direct way is to specify
an exact match of the input and output types. This is feasible if a model contains only a few
known slope and bias combinations. Use the TflCOperationEntry
class and
specify the exact values of slope and bias on each argument. For scenarios where there are
numerous slope/bias combinations, it is not feasible to specify each value with a different
entry. Use a net slope entry or create a custom entry (see Customize Match and Replacement Process).
The operation V0 = (V1 / V2) implies, for binary-point-only scaling, that
where Sn is the net slope.
It is common to replace all division operations that have
a net slope of 1.0 with a function that performs C-style division. For example, to replace
all signed 8-bit divisions that have a net scaling of 1.0
with the s16_netslope0p5_div_s16_s16
replacement function, the operator
entry must define a net slope factor, F2E. You
specify the values for F and E using operator entry
parameters NetSlopeAdjustmentFactor
and
NetFixedExponent
. For the s16_netslope0p5_div_s16_s16
function, you would set NetSlopeAdjustmentFactor
to 1 and
NetFixedExponent
to 0.0. Also, set the operator entry parameter
SlopesMustBeTheSame
to false
and the parameter
MustHaveZeroNetBias
to true
. To match for
replacement, the biases must be zero for all division
conceptual arguments. (For parameter descriptions, see the reference page for the function
setTflCOperationEntryParameters
.)
Note
When an operator entry specifies NetSlopeAdjustmentFactor
and NetFixedExponent
,
matching entries must have arguments with zero bias.
Data Type Conversion (Cast)
The data type conversion operation V0 = V1 implies, for binary-point-only scaling, that
where Sn is the net slope. Set the operator entry
parameter SlopesMustBeTheSame
to false
and the parameter
MustHaveZeroNetBias
to true
. To match for
replacement, the biases must be zero for all cast conceptual
arguments. (For parameter descriptions, see the reference page for the function setTflCOperationEntryParameters
.)
Shift
The shift left or shift right operation V0 = (V1 / 2n) implies, for binary-point-only scaling, that
where Sn is the net slope. Set the operator entry
parameter SlopesMustBeTheSame
to false
and the parameter
MustHaveZeroNetBias
to true
. To match for
replacement, the biases must be zero for all shift conceptual
arguments. (For parameter descriptions, see the reference page for the function setTflCOperationEntryParameters
.)
Related Topics
- Code You Can Replace From Simulink Models
- Define Code Replacement Library Optimizations
- Binary-Point-Only Scaling Code Replacement
- Slope Bias Scaling Code Replacement
- Net Slope Scaling Code Replacement
- Equal Slope and Zero Net Bias Code Replacement
- Data Type Conversions (Casts) and Operator Code Replacement
- Shift Left Operations and Code Replacement
- Data Alignment for Code Replacement
- Remap Operator Output to Function Input
- Customize Match and Replacement Process
- Develop a Code Replacement Library