Fixed-Point Operations in Stateflow Charts
Fixed-point numbers use integers and integer arithmetic to approximate real numbers. They enable you to perform computations involving real numbers without requiring floating-point support in underlying system hardware. For more information, see Fixed-Point Data in Stateflow Charts.
Arithmetic Operations for Fixed-Point Data
The general equation for a binary arithmetic operation between fixed-point operands is
c = a <op> b
a
and b
are fixed-point numbers and
<op>
refers to addition, subtraction, multiplication, or
division. The result of the operation is a fixed-point number c
of the
form
V_{c}
≈
S_{c}
Q_{c}
+ B_{c}
.
The fixed-point type for c
determines the slope
S_{c}
, the bias
B_{c}
, and the number of bits
used to store the quantized integer
Q_{c}
. For each arithmetic
operation, this table lists the value of the quantized integer
Q_{c}
in terms of the values
of the operands (a
and b
) and the fixed-point type for
c
.
Operation | Slope and Bias Scaling | Binary-Point Scaling | |
---|---|---|---|
Addition | c = a+b | Q_{c} =
round((S_{a} /S_{c} )Q_{a}
+
(S_{b} /S_{c} )Q_{b}
+ (B_{a} +
B_{b} –
B_{c} )/S_{c} ) | Q_{c} =
round((S_{a} /S_{c} )Q_{a}
+
(S_{b} /S_{c} )Q_{b} ) |
Subtraction | c = a-b | Q_{c} =
round((S_{a} /S_{c} )Q_{a}
–
(S_{b} /S_{c} )Q_{b}
– (B_{a} –
B_{b} –
B_{c} )/S_{c} ) | Q_{c} =
round((S_{a} /S_{c} )Q_{a}
–
(S_{b} /S_{c} )Q_{b} ) |
Multiplication | c = a*b | Q_{c} =
round((S_{a} S_{b} /S_{c} )Q_{a} Q_{b}
+
(B_{a} S_{b} /S_{c} )Q_{a}
+
(B_{b} S_{a} /S_{c} )Q_{b}
+
(B_{a} B_{b}
–
B_{c} )/S_{c} ) | Q_{c} =
round((S_{a} S_{b} /S_{c} )Q_{a} Q_{b} ) |
Division | c = a/b | Q_{c} =
round((S_{a} Q_{a}
+
B_{a} )/(S_{c} (S_{b} Q_{b}
+ B_{b} )) –
(B_{c} /S_{c} )) | Q_{c} =
round((S_{a} /(S_{b} S_{c} ))Q_{a} /Q_{b} ) |
To simplify these expressions and avoid computationally expensive operations, use binary-point scaling to encode all your fixed-point data. With this setting, the slope is an integer power of two and the bias is zero. Then, all fixed-point operations consist of integer arithmetic and bit shifts on the quantized integers.
Note
The result of an arithmetic operation depends on the rounding method that computes the
value of the quantized integer
Q_{c}
. For more information,
see Conversion Operations.
Relational Operations for Fixed-Point Data
You can use relational operations to compare fixed-point data. Comparing fixed-point values of different types can yield unexpected results because each operand is converted to a common type for comparison. Because of rounding or overflow errors during the conversion, some values that appear to be equal are not equal as fixed-point numbers.
For example, suppose that a
and b
represent the
real-world value V = 2.2 in two different fixed-point encoding schemes:
a
is a fixed-point number with slope S_{a}
= 0.3 and bias B_{a}
= 0.1. The quantized integer fora
is:Q
_{a}
= (V – B_{a}
)/S_{a}
= (2.2 – 0.1)/0.3 = 7.b
is a fixed-point number with slope S_{b}
= 0.7 and bias B_{b}
= 0.1. The quantized integer forb
is:Q
_{b}
= (V – B_{b}
)/S_{b}
= (2.2 – 0.1)/0.7 = 3.
To compare these values, the chart first converts them to a common
fixed-point type with slope S_{comp} = 1.06811 ·
10^{–5} ≈
S_{a}
/28087 ≈
S_{b}
·
2^{–16} and bias B_{comp} = 0.1. (In this case, the slope S_{comp}
arises from the approximate value of S_{a}
/S_{b}
= 0.3/0.7 ≈ 28087 · 2^{–16}.) In this common encoding scheme, a
and
b
correspond to these quantized integers:
Q_{a'}
=
S_{a}
Q_{a}
/S_{comp}
=
Q_{a}
(S_{a}
/S_{comp})
≈ 7 ⨉ 28087 = 196609
Q_{b'}
=
S_{b}
Q_{b}
/S_{comp}
=
Q_{b}
(S_{b}
/S_{comp})
≈ 3 ⨉ 2^{16} = 196608.
After the conversion, the quantized integers are different. Although
a
and b
represent the same real-world value, they
are not equal as fixed-point numbers.
Note
In charts that use C as the action language, comparisons of fixed-point operands with mismatched biases are not supported.
Logical Operations for Fixed-Point Data
In a logical operation, a fixed-point operand a
is interpreted as
false
if it corresponds to the real-world value for zero in the
fixed-point type of a
. Otherwise, a
is interpreted as
true
.
In charts that use MATLAB^{®} as the action language, using
a
in a logical operation is equivalent to the expressiona ~= cast(0,"like",a)
.In charts that use C as the action language, using
a
in a logical operation is equivalent to the expressiona != 0c
, where0c
is a fixed-point context-sensitive constant. See Fixed-Point Context-Sensitive Constants.
For example, suppose that a
is a fixed-point number with a slope of S_{a}
=
0.25 and a bias of B_{a}
=
5.1. Using a
in a logical operation is equivalent to
testing whether the quantized integer
Q_{a}
satisfies the
condition
Q_{a}
= round((0 –
B_{a}
)/S_{a}
)
= round(–5.1 / 0.25) = round(–20.4) = –20.
Therefore, a
is equivalent to false
when its real-world approximation is
V_{a}
≈
S_{a}
Q_{a}
+ B_{a}
= 0.25 ⨉ ( –20) + 5.1
= 0.1.
Promotion Rules for Fixed-Point Operations
The rules for selecting the numeric type used for the result of an operation on fixed-point numbers are called fixed-point promotion rules. These rules help to maintain computational efficiency and usability.
The fixed-point promotion rules determine a result type for an operation c = a
by selecting the slope
S<op>
b _{c}
, the bias
B_{c}
, and the number of bits
w_{c}
used to store the quantized integer
Q_{c}
. These parameters depend
on the fixed-point types of the operands a
and b
, the
operation <op>
to be performed, and the action language
property for the chart.
In a chart that uses MATLAB as the action language, you control the fixed-point promotion rules through the fixed-point properties for the chart. See Fixed-Point Properties.
If you set the MATLAB Chart
fimath
property toSame as MATLAB
, then arithmetic operations follow the default fixed-point promotion rules for MATLAB. See Performing Fixed-Point Arithmetic (Fixed-Point Designer).If you specify a chart
fimath
object withSumMode
andProductMode
set toSpecifyPrecision
, then you can define the word length, slope, and bias for all sums and products explicitly. See fimath Object Properties (Fixed-Point Designer).
In a chart that uses C as the action language, the fixed-point promotion rules determine the type for an intermediate value of the result. This intermediate value is then cast to the type that you specify for
c
.For all arithmetic operations, the default number of bits
w_{c}
used to store the quantized integer is the larger value between:The maximum number of bits in the operand types (
w_{a}
andw_{b}
).The number of bits in the integer word size for the target machine (
w_{int}
).
To specify the value of
w_{int}
, open the Configuration Parameters dialog box and, in the Hardware Implementation pane, set the Device vendor parameter toCustom Processor
and the int parameter to the target integer word size. For more information, see Hardware Implementation Pane (Simulink).You can avoid overflow and improve the precision in your floating-point operations by using the special assignment operation of the form
c := a
. The special assignment operation does not follow the fixed-point promotion rules. Instead, the chart determines the result of the operation by using the type that you specify for<op>
bc
. See Override Fixed-Point Promotion in C Charts.
Addition and Subtraction
By default, charts that use MATLAB as the action language support addition and subtraction only on fixed-point
data defined through binary-point scaling. If either operand is a signed fixed-point
number, then the result is also signed. The choice of word length accommodates the integer
and fraction parts of each operand in addition to a possible carry bit. The fraction
length of the result is equal to the fraction length of the most precise operand. To
perform addition and subtraction on fixed-point data defined by using either a slope that
is not an integer power of two or a nonzero bias, specify a chart
fimath
object with SumMode
set to
SpecifyPrecision
.
Charts that use C as the action language support addition and subtraction for operands of all fixed-point data types. The result is a signed fixed-point number only if both operands are signed. Mixing signed and unsigned operands can yield unexpected results and is not recommended. The slope of the result is equal to the slope of the least precise operand. To simplify calculations and yield efficient code, the biases of the two inputs are added for an addition operation and subtracted for a subtraction operation.
a | b | MATLAB as the Action Language | C as the Action Language | |
---|---|---|---|---|
Sign | s_{a} | s_{b} | s_{c} =
s_{a} || s_{b} | s_{c} =
s_{a}
&&
s_{b} |
Word length | w_{a} | w_{b} | w_{c} =
max(w_{a} –
f_{a} ,
w_{b} –
f_{b} ) +
max(f_{a} ,
f_{b} ) +
1 | w_{c} =
max(w_{a} ,
w_{b} ,
w_{int} ) |
Fraction length | f_{a} | f_{b} | f_{c} =
max(f_{a} ,
f_{b} ) | f_{c} =
min(f_{a} ,
f_{b} ) |
Slope | S_{a}
(2^{-fa}
if using binary-point scaling) | S_{b}
(2^{-fb}
if using binary-point scaling) | S_{c} =
min(S_{a} ,
S_{b} ) | S_{c} =
max(S_{a} ,
S_{b} ) |
Bias | B_{a} (0 if using
binary-point scaling) | B_{b} (0 if using
binary-point scaling) | B_{c} =
0 | B_{c} =
B_{a} +
B_{b} for addition or B_{c} =
B_{a} –
B_{b} for subtraction |
Multiplication
By default, charts that use MATLAB as the action language support multiplication only on fixed-point data
defined through binary-point scaling. If either operand is a signed fixed-point number,
then the result is also signed. A full precision product requires a word length equal to
the sum of the word lengths of the operands. The fraction length of a product is the sum
of the fraction lengths of the operands. To perform multiplication on fixed-point data
defined by using either a slope that is not an integer power of two or a nonzero bias,
specify a chart fimath
object with ProductMode
set
to SpecifyPrecision
.
Charts that use C as the action language support multiplication only on fixed-point data operands defined by nonzero biases. The result is a signed fixed-point number only if both operands are signed. Mixing signed and unsigned operands can yield unexpected results and is not recommended. The slope of a product is the product of the slopes of the operands.
a | b | MATLAB as the Action Language | C as the Action Language | |
---|---|---|---|---|
Sign | s_{a} | s_{b} | s_{c} =
s_{a} || s_{b} | s_{c} =
s_{a}
&&
s_{b} |
Word length | w_{a} | w_{b} | w_{c} =
w_{a} +
w_{b} | w_{c} =
max(w_{a} ,
w_{b} ,
w_{int} ) |
Fraction length | f_{a} | f_{b} | f_{c} =
f_{a} +
f_{b} | f_{c} =
f_{a} +
f_{b} |
Slope | S_{a}
(2^{-fa}
if using binary-point scaling) | S_{b}
(2^{-fb}
if using binary-point scaling) | S_{c} =
S_{a} S_{b} | S_{c} =
S_{a} S_{b} |
Bias | B_{a} =
0 | B_{b} =
0 | B_{c} =
0 | B_{c} =
0 |
Division
Charts that use MATLAB as the action language support division only on fixed-point data defined through binary-point scaling. If either operand is a signed fixed-point number, then the result is also signed. A full precision quotient requires a word length equal to the maximum number of bits in the operands. The fraction length of a quotient is the difference of the fraction lengths of the operands.
Charts that use C as the action language support division for fixed-point data operands defined by nonzero biases. The result is a signed fixed-point number only if both operands are signed. Mixing signed and unsigned operands can yield unexpected results and is not recommended. The slope of a quotient is the quotient of the slopes of the operands.
a | b | MATLAB as the Action Language | C as the Action Language | |
---|---|---|---|---|
Sign | s_{a} | s_{b} | s_{c} =
s_{a} || s_{b} | s_{c} =
s_{a}
&&
s_{b} |
Word length | w_{a} | w_{b} | w_{c} =
max(w_{a} ,
w_{b} ) | w_{c} =
max(w_{a} ,
w_{b} ,
w_{int} ) |
Fraction length | f_{a} | f_{b} | f_{c} =
f_{a} –
f_{b} | f_{c} =
f_{a} –
f_{b} |
Slope | S_{a}
(2^{-fa}
if using binary-point scaling) | S_{b}
(2^{-fb}
if using binary-point scaling) | S_{c} =
S_{a} /S_{b} | S_{c} =
S_{a} /S_{b} |
Bias | B_{a} =
0 | B_{b} =
0 | B_{c} =
0 | B_{c} =
0 |
Unary Minus
The only unary operation that requires a promotion of its result type is the unary
minus operation c = -a
. Taking the negative of an unsigned fixed-point
number can yield unexpected results and is not recommended. The word size of the result
depends on the action language property of the chart. The slope of the result is equal to
the slope of the operand. The bias of the result type is the negative of the bias of the
operand.
a | MATLAB as the Action Language | C as the Action Language | |
---|---|---|---|
Sign | s_{a} | s_{c} =
s_{a} | s_{c} =
s_{a} |
Word length | w_{a} | w_{c} =
w_{a} | w_{c} =
max(w_{a} ,
w_{int} ) |
Fraction length | f_{a} | f_{c} =
f_{a} | f_{c} =
f_{a} |
Slope | S_{a}
(2^{-fa}
if using binary-point scaling) | S_{c} =
S_{a} | S_{c} =
S_{a} |
Bias | B_{a} (0 if using
binary-point scaling) | B_{c} =
–B_{a} | B_{c} =
–B_{a} |
Arithmetic with Mixed Numeric Types
This table summarizes the fixed-point promotion rules for a binary operation between a fixed-point number and an operand of a different numeric type.
Numeric Type of Second Operand | MATLAB as the Action Language | C as the Action Language |
---|---|---|
Floating-point numbers:
| Before performing the operation, the chart casts the floating-point operand to a fixed-point number. The type used for the cast depends on the operation:
The result of the operation is a fixed-point number. | Before performing the operation, the chart casts the fixed-point
operand to a floating-point number . The casting operation uses the same type
( |
Integers:
| The integer operand is treated as a fixed-point number of the same word length and signedness with slope S = 1 and bias B = 0. The result of the operation is a fixed-point number. | The integer operand is treated as a fixed-point number of the same word length and signedness with slope S = 1 and bias B = 0. The result of the operation is a fixed-point number. |