Stateflow    

Promotion Rules for Fixed-Point Operations

Operations with at least one fixed-point operand require rules for selecting the type of the intermediate result for that operation. For example, in the action statement c = a + b, where a or b is a fixed-point number, an intermediate result type for a + b must first be chosen before the result is calculated and assigned to c.

The rules for selecting the numeric types used to hold the results of operations with a fixed-point number are referred to as the fixed-point promotion rules. The primary goal of these rules is to maintain good computational efficiency with reasonable usability.

The following topics describe the process of selecting an intermediate result type for all binary operations with at least one fixed-point operand:

Default Selection of the Number of Bits of the Result Type

A fixed-point number with and is treated as an integer. In operations with integers, the C language promotes any integer input with fewer bits than the type int to the type int and then performs the operation.

The type int is the integer word size for C on a given platform. Result word size is increased to the integer word size because processors can perform operations at this size efficiently.

Stateflow maintains consistency with the C language by using the following default rule to assign the number of bits for the result type of an operation with fixed-point numbers:

Setting the Integer Word Size for a Target.   The preceding default rule for selecting the bit size of the result for operations with fixed-point numbers relies on the definition of the integer word size for your target. You can set the integer word size for the targets that you build in Simulink with the following procedure:

  1. Select Simulink parameters from the Simulink menu in your Simulink model window.
  2. In the resulting Simulation Parameters dialog, select the Advanced tab.
  3. At the bottom of the Advanced panel, select Microprocessor for the Production hardware characteristics field.
  4. In the window pane at the bottom of the panel, select Number of bits for C 'int' to highlight it.
  5. In the Value field to the right, select the target integer size in bits.
  6. Select OK to accept the changes.

When you build any target after making this change, Stateflow uses this integer size in generated code to select result types for your fixed-point operations.

Unary Promotions

Only the unary minus (-) operation requires a promotion of its result type. The word size of the result is given by the default procedure for selecting the bit size of the result type for an operation involving fixed-point data. See Default Selection of the Number of Bits of the Result Type. The bias, , of the result type is the negative of the bias of the operand.

Binary Operation Promotion for Integer Operand with Fixed-Point Operand

Integers as operands in binary operations with fixed-point numbers are treated as fixed-point numbers of the same word size with slope, , equal to 1, and a bias, , equal to 0. The operation now becomes a binary operation between two fixed-point operands. See Binary Operation Promotion for Two Fixed-Point Operands.

Binary Operation Promotion for Double Operand with Fixed-Point Operand

When one operand is of type double in a binary operation with a fixed-point type, the result type is double. In this case, the fixed-point operand is cast to type double, and the operation is performed.

Binary Operation Promotion for Single Operand with Fixed-Point Operand

When one operand is of type single in a binary operation with a fixed-point type, the result type is single. In this case, the fixed-point operand is cast to type single, and the operation is performed.

Binary Operation Promotion for Two Fixed-Point Operands

Operations with both operands of fixed-point type produce an intermediate result of fixed-point type. The resulting fixed-point type is chosen through the application of a set of operator-specific rules. The procedure for producing an intermediate result type from an operation with operands of different fixed-point types is summarized in the following topics:

Addition (+) and Subtraction (-).   The output type for addition and subtraction is chosen so that the maximum positive range of either input can be represented in the output while preserving maximum precision. The base word type of the output follows the rule in Default Selection of the Number of Bits of the Result Type. 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.

Multiplication (*) and Division (/).   The output type for multiplication and division is chosen to yield the most efficient code implementation. Nonzero biases are not supported for multiplication and division by Stateflow (see note).

The slope for the result type of the product of the multiplication of two fixed-point numbers is the product of the slopes of the operands. Similarly, the slope of the result type of the quotient of the division of two fixed-point numbers is the quotient of the slopes. The base word type is chosen to conform to the rule in Default Selection of the Number of Bits of the Result Type.

Relational Operations (>, <, >=, <=, ==, -=, !=, <>).   Stateflow supports the following relational (comparison) operations on all fixed-point types: >, <, >=, <=, ==, -=, !=, <>. See Supported Operations with Fixed-Point Operands for an example and description of these operations. Stateflow requires that both operands in a comparison have equal biases (see note).

Comparing fixed-point values of different types can yield unexpected results because Stateflow must convert each operand to a common type for comparison. Because of rounding or overflow errors during the conversion, values that do not appear equal might be equal and values that appear to be equal might not be equal.

For example, compare the following two unsigned 8 bit fixed-point numbers, a and b, in an 8 bit target environment:

Fixed-Point Number a
Fixed-Point Number b








By rule, the result type for comparison is 8 bit. Converting b, the least precise operand, to the type of a, the most precise operand, could result in overflow. Consequently, a is converted to the type of b. Because the bias values for both operands are 0, the conversion is made as follows:

Although they represent different values, a and b are considered equal as fixed-point numbers.

Logical Operations (&, |, &&, ||).   If a is a fixed-point number used in a logical operation, it is interpreted with the equivalent substitution a != 0.0C where 0.0C is an expression for zero in the fixed-point type of a (see Fixed-Point Context-Sensitive Constants). For example, if a is a fixed-point number in the logical operation a && b, this operation is equivalent to the following:

The preceding operation is not a check to see whether the quantized integer for a, , is not 0. If the real-world value for a fixed-point number a is 0, this implies that . Therefore, the expression a != 0, for fixed-point number a, is actually equivalent to the following expression:

For example, if a fixed-point number, a, has a slope of 2-2, and a bias of 5, the test a != 0 is equivalent to the test if.


  Supported Operations with Fixed-Point Operands Assignment (=, :=) and Cast Operations