Fixed-Point Operator Code Replacement - MATLAB & Simulink (original) (raw)

If you have a Fixed-Point Designer™ license, you can define fixed-point operator code replacement entries to match:

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 createAndAddConceptualArg function: CheckSlope: Specify the valuetrue.CheckBias: Specify the valuetrue.DataTypeMode (orDataType/Scaling equivalent): Specify fixed-point binary-point-only scaling.FractionLength: Specify a fraction length (for example, 3).
A specific slope bias scaling combination on the operator inputs and output. RTW.TflCOperationEntry createAndAddConceptualArg function: CheckSlope: Specify the valuetrue.CheckBias: Specify the valuetrue.DataTypeMode (orDataType/Scaling equivalent): Specify fixed-point [slope bias] scaling.Slope (orSlopeAdjustmentFactor/FixedExponent equivalent): Specify a slope value (for example,15).Bias: Specify a bias value (for example,2).
Net slope between operator inputs and output (multiplication and division). RTW.TflCOperationEntryGenerator_NetSlope setTflCOperationEntryParameters function: NetSlopeAdjustmentFactor: Specify the slope adjustment factor (F) part of the net slope,F2E (for example, 1.0).NetFixedExponent: Specify the fixed exponent (E) part of the net slope,F2E (for example, -3.0). createAndAddConceptualArg function: CheckSlope: Specify the valuefalse.CheckBias: Specify the valuefalse.DataType: Specify the value'Fixed'.
Relative scaling between operator inputs and output (multiplication and division). RTW.TflCOperationEntryGenerator setTflCOperationEntryParameters function: RelativeScalingFactorF: Specify the slope adjustment factor (F) part of the relative scaling factor,F2E (for example, 1.0).RelativeScalingFactorE: Specify the fixed exponent (E) part of the relative scaling factor, F2E (for example, -3.0). createAndAddConceptualArg function: CheckSlope: Specify the valuefalse.CheckBias: Specify the valuefalse.DataType: Specify the value'Fixed'.
Equal slope and zero net bias across operator inputs and output (addition and subtraction). RTW.TflCOperationEntryGenerator setTflCOperationEntryParameters function: SlopesMustBeTheSame: Specify the valuetrue.MustHaveZeroNetBias: Specify the valuetrue. createAndAddConceptualArg function: CheckSlope: Specify the valuefalse.CheckBias: Specify the valuefalse.

Fixed-Point Numbers and Arithmetic

Fixed-point numbers use integers and integer arithmetic to represent real numbers and arithmetic with the following encoding scheme:

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 andMustHaveZeroNetBias 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 functionsetTflCOperationEntryParameters.)

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 andMustHaveZeroNetBias 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 functionsetTflCOperationEntryParameters.)

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 parametersNetSlopeAdjustmentFactor and NetFixedExponent. For the s8_mul_s8_u8 function, set NetSlopeAdjustmentFactor to 1 and NetFixedExponent to 0.0. Also, set the operator entry parameterSlopesMustBeTheSame to false and the parameterMustHaveZeroNetBias 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 functionsetTflCOperationEntryParameters.)

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 andNetFixedExponent. For the s16_netslope0p5_div_s16_s16 function, you would set NetSlopeAdjustmentFactor to 1 andNetFixedExponent to 0.0. Also, set the operator entry parameterSlopesMustBeTheSame to false and the parameterMustHaveZeroNetBias 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 functionsetTflCOperationEntryParameters.)

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 parameterMustHaveZeroNetBias 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 parameterMustHaveZeroNetBias 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.)

See Also

Topics