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
Vc
≈
Sc
Qc
+ Bc
.
The fixed-point type for c
determines the slope
Sc
, the bias
Bc
, and the number of bits
used to store the quantized integer
Qc
. For each arithmetic
operation, this table lists the value of the quantized integer
Qc
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 | Qc =
round((Sa /Sc )Qa
+
(Sb /Sc )Qb
+ (Ba +
Bb –
Bc )/Sc ) | Qc =
round((Sa /Sc )Qa
+
(Sb /Sc )Qb ) |
Subtraction | c = a-b | Qc =
round((Sa /Sc )Qa
–
(Sb /Sc )Qb
– (Ba –
Bb –
Bc )/Sc ) | Qc =
round((Sa /Sc )Qa
–
(Sb /Sc )Qb ) |
Multiplication | c = a*b | Qc =
round((Sa Sb /Sc )Qa Qb
+
(Ba Sb /Sc )Qa
+
(Bb Sa /Sc )Qb
+
(Ba Bb
–
Bc )/Sc ) | Qc =
round((Sa Sb /Sc )Qa Qb ) |
Division | c = a/b | Qc =
round((Sa Qa
+
Ba )/(Sc (Sb Qb
+ Bb )) –
(Bc /Sc )) | Qc =
round((Sa /(Sb Sc ))Qa /Qb ) |
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
Qc
. 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 Sa
= 0.3 and bias Ba
= 0.1. The quantized integer fora
is:Q
a
= (V – Ba
)/Sa
= (2.2 – 0.1)/0.3 = 7.b
is a fixed-point number with slope Sb
= 0.7 and bias Bb
= 0.1. The quantized integer forb
is:Q
b
= (V – Bb
)/Sb
= (2.2 – 0.1)/0.7 = 3.
To compare these values, the chart first converts them to a common
fixed-point type with slope Scomp = 1.06811 ·
10–5 ≈
Sa
/28087 ≈
Sb
·
2–16 and bias Bcomp = 0.1. (In this case, the slope Scomp
arises from the approximate value of Sa
/Sb
= 0.3/0.7 ≈ 28087 · 2–16.) In this common encoding scheme, a
and
b
correspond to these quantized integers:
Qa'
=
Sa
Qa
/Scomp
=
Qa
(Sa
/Scomp)
≈ 7 ⨉ 28087 = 196609
Qb'
=
Sb
Qb
/Scomp
=
Qb
(Sb
/Scomp)
≈ 3 ⨉ 216 = 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 Sa
=
0.25 and a bias of Ba
=
5.1. Using a
in a logical operation is equivalent to
testing whether the quantized integer
Qa
satisfies the
condition
Qa
= round((0 –
Ba
)/Sa
)
= round(–5.1 / 0.25) = round(–20.4) = –20.
Therefore, a
is equivalent to false
when its real-world approximation is
Va
≈
Sa
Qa
+ Ba
= 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
Bc
, and the number of bits
wc
used to store the quantized integer
Qc
. 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.
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
wc
used to store the quantized integer is the larger value between:The maximum number of bits in the operand types (
wa
andwb
).The number of bits in the integer word size for the target machine (
wint
).
To specify the value of
wint
, 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 | sa | sb | sc =
sa || sb | sc =
sa
&&
sb |
Word length | wa | wb | wc =
max(wa –
fa ,
wb –
fb ) +
max(fa ,
fb ) +
1 | wc =
max(wa ,
wb ,
wint ) |
Fraction length | fa | fb | fc =
max(fa ,
fb ) | fc =
min(fa ,
fb ) |
Slope | Sa
(2-fa
if using binary-point scaling) | Sb
(2-fb
if using binary-point scaling) | Sc =
min(Sa ,
Sb ) | Sc =
max(Sa ,
Sb ) |
Bias | Ba (0 if using
binary-point scaling) | Bb (0 if using
binary-point scaling) | Bc =
0 | Bc =
Ba +
Bb for addition or Bc =
Ba –
Bb 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 | sa | sb | sc =
sa || sb | sc =
sa
&&
sb |
Word length | wa | wb | wc =
wa +
wb | wc =
max(wa ,
wb ,
wint ) |
Fraction length | fa | fb | fc =
fa +
fb | fc =
fa +
fb |
Slope | Sa
(2-fa
if using binary-point scaling) | Sb
(2-fb
if using binary-point scaling) | Sc =
Sa Sb | Sc =
Sa Sb |
Bias | Ba =
0 | Bb =
0 | Bc =
0 | Bc =
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 | sa | sb | sc =
sa || sb | sc =
sa
&&
sb |
Word length | wa | wb | wc =
max(wa ,
wb ) | wc =
max(wa ,
wb ,
wint ) |
Fraction length | fa | fb | fc =
fa –
fb | fc =
fa –
fb |
Slope | Sa
(2-fa
if using binary-point scaling) | Sb
(2-fb
if using binary-point scaling) | Sc =
Sa /Sb | Sc =
Sa /Sb |
Bias | Ba =
0 | Bb =
0 | Bc =
0 | Bc =
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 | sa | sc =
sa | sc =
sa |
Word length | wa | wc =
wa | wc =
max(wa ,
wint ) |
Fraction length | fa | fc =
fa | fc =
fa |
Slope | Sa
(2-fa
if using binary-point scaling) | Sc =
Sa | Sc =
Sa |
Bias | Ba (0 if using
binary-point scaling) | Bc =
–Ba | Bc =
–Ba |
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. |