## Data Type Inheritance Rules in Simulink

To control the data type of a signal in a Simulink^{®} model, you specify a data type for the corresponding block output.
Manually selecting appropriate word length and scaling for the fixed-point parameters in
your model can be challenging and complicates model maintenance. To aid you, Simulink provides several data type inheritance rules to automate the process of
selecting the output data type for blocks in your model.

Blocks can inherit data types from a variety of sources, including signals to which
they are connected and particular block parameters. You can specify the value of a data
type parameter as a rule that determines how the output signal inherits its data type.
To view the inheritance rules that a block supports, use the **Output data
type** drop-down menu on the block dialog box.

Several inheritance rules are provided depending on the block selected, including:

`Inherit: Inherit via internal rule`

`Inherit: Keep MSB`

`Inherit: Keep LSB`

`Inherit: Match scaling`

When Simulink can determine an appropriate output data type that is efficient and can exactly represent the ideal result on the specified hardware, all these inheritance rules produce the same output data type. When Simulink cannot represent the ideal result accurately and efficiently, these rules differ in which bits are dropped to balance precision, range, and efficiency of the generated code.

The data type rules `Inherit: Keep MSB`

,
`Inherit: Keep LSB`

, and ```
Inherit: Match
scaling
```

are available for these blocks:

The examples in the following sections demonstrate in detail how these data type inheritance rules are applied in different situations for Product and Add operations. In general, the Gain block follows similar behavior as the Product block with respect to data type inheritance rules.

For illustration purposes, these examples showcase how the data type rules perform on
a system whose largest integer is 32 bits. On the **Hardware
Implementation** pane of the **Configuration Parameters**
dialog, **long** is set to 32 bits and the **Support long
long** parameter is disabled.

### Data Type Rules for Product with Binary-Point Scaling

For a product operation, you can specify a data type inheritance rule for the output data type. For this example, assume that both inputs to the Product block are single word and use binary-point scaling. The first input has a data type of `ufix32_En6`

, meaning a 32-bit unsigned fixed-point type with a fraction length of 6. The second input has a data type of `ufix32_En4`

, meaning a 32-bit unsigned fixed-point type with a fraction length of 4. See Fixed-Point Numbers in Simulink (Fixed-Point Designer) for more information on this notation.

`open_system("data_type_inherit_rules_product_binary_point_scaling")`

This product operation is illustrated schematically below for each of the data type rules.

`Inherit: Keep MSB`

The data type rule `Inherit: Keep MSB`

keeps the most significant bits in the product output and guarantees single-word results. This rule is useful when you need to preserve the range of the result and are okay with losing precision, for example when multiplying large numbers.

In this example, 32 bits of precision have been dropped from the 64-bit ideal result.

`Inherit: Match Scaling`

The data type rule `Inherit: Match Scaling`

does no scaling adjustment, and single-word output is guaranteed. This rule is useful when you need to preserve precision in the output and are okay with losing range bits, for example when multiplying small numbers in a large data type. This rule produces a plain multiplication in C, giving the most efficient generated code.

In this example, multiplying the scaling of the inputs gives ${2}^{-6}*\text{\hspace{0.17em}}{2}^{-4}\text{\hspace{0.17em}}=\text{\hspace{0.17em}}{2}^{-10}$. An output with no scaling adjustments yields ${2}^{-10}$, equivalent to keeping the least significant bits of the result. In this example, 32 bits of range have been dropped from the 64-bit ideal result.

`Inherit: Inherit via internal rule`

The data type rule `Inherit: Inherit via internal rule`

is useful when you need to maintain backwards compatibility in an existing model. For new models, the `Inherit: Keep MSB`

and `Inherit: Match Scaling`

rules are recommended for their clearer numerical intent and better efficiency on hardware.

In this example, both inputs are 32 bit, making the ideal product word length 64 bits (32 bits X 32 bits = 64 bits). The internal rule uses the ideal 64 bit word length for the output. Additionally, the internal rule uses the ideal fraction length for the output, which is the product of the input fraction lengths (En6 X En4 = En10). In summary, the rule is

$${\mathrm{WL}}_{\mathrm{ideal}\text{\hspace{0.17em}}\mathrm{product}}={\mathrm{WL}}_{\mathrm{input}\text{\hspace{0.17em}}1}+{\mathrm{WL}}_{\mathrm{input}\text{\hspace{0.17em}}2}$$

$${\mathrm{FL}}_{\mathrm{ideal}\text{\hspace{0.17em}}\mathrm{product}}={\mathrm{FL}}_{\mathrm{input}\text{\hspace{0.17em}}1}+{\mathrm{FL}}_{\mathrm{input}\text{\hspace{0.17em}}2}$$

Note that `Inherit: Inherit via internal rule`

gives the ideal 64-bit data type for the product output, even though the specified hardware in this example supports a maximum word length of 32 bits. The generated code will be less efficient on this hardware.

### Data Type Inheritance Rules for Product with Slope Scaling

For a product operation, you can specify a data type inheritance rule for the output data type. For this example, assume that both inputs to the product block are single word and use slope scaling. The first input has a data type of `ufix32_S1p2`

, meaning a 32-bit unsigned fixed-point type with a slope of 1.2. The second input has a data type of `ufix32_En2`

, meaning a 32-bit unsigned fixed-point type with a fraction length of 2. See Fixed-Point Numbers in Simulink (Fixed-Point Designer) for more information on this notation.

`open_system("data_type_inherit_rules_product_slope_scaling.slx")`

`Inherit: Keep MSB`

The data type rule `Inherit: Keep MSB`

keeps the most significant bits in the product output and guarantees single-word results. This rule is useful when you need to preserve the range of the result and are okay with losing precision, for example when multiplying large numbers.

In this example, the ideal result of the least-significant bit is $1.2\text{\hspace{0.17em}}\mathit{X}\text{\hspace{0.17em}}{2}^{-2}=0.3$. The output gives the power of the least-significant bit as 1288490188.8. The number of precision bits dropped from the ideal result to preserve the range is

${\mathrm{log}}_{2}\left(\frac{1288490188.8}{0.3}\right)=32$.

`Inherit: Match Scaling`

The data type rule `Inherit: Match Scaling`

does no scaling adjustment and single-word output is guaranteed. This rule is useful when you need to preserve precision in the output and are okay with losing range bits, for example when multiplying small numbers in a large data type. This rule results in a plain multiplication in C, giving the most efficient generated code.

In this example, the output scaling is the $1.2\text{\hspace{0.17em}}\mathit{X}\text{\hspace{0.17em}}{2}^{-2}=0.3$, the same as the ideal result. 32 bits of range have been dropped from the 64-bit ideal result, keeping the least-significant bits of the result.

`Inherit: Inherit via internal rule`

The data type rule `Inherit: Inherit via internal rule`

is useful when you need to maintain backwards compatibility in an existing model. For new models, the `Inherit: Keep MSB`

and `Inherit: Match Scaling`

rules are recommended for their clearer numerical intent and better efficiency on hardware.

In this example, the combined slope of the inputs is $1.2\text{\hspace{0.17em}}\mathit{X}\text{\hspace{0.17em}}{2}^{-2}=0.3$. Therefore, an output scaling of 0.3 results in a net scaling of 0 and requires no scaling adjustments. The word length is the same as in the binary-point case (32 bits X 32 bits = 64 bits). Because multiplications can have large results, this rule allows the word length to double to represent the full result and avoid bit loss, even if it does not produce efficient generated code.

Because multiplications have large results, the "internal rule" allows the word length to double to represent the full result and avoid bit loss, even if it does not produce efficient generated code.

### Data Type Inheritance Parameter Settings for Add

For an addition operation, you can specify a data type inheritance rule separately for the accumulator type and the output data type. This table summarizes which settings to choose for the output and accumulator data types to achieve the desired result.

Desired Behavior | Accumulator data type | Output data type |
---|---|---|

Keep MSB | ```
Inherit: Inherit via internal
rule
``` | `Inherit: Keep MSB` |

Keep LSB | ```
Inherit: Inherit via internal
rule
``` | `Inherit: Keep LSB` |

Internal rule | ```
Inherit: Inherit via internal
rule
``` | `Inherit: Same as accumulator` |

### Data Type Inheritance Rules for Add with Binary-Point Scaling When Ideal Type Is Single Word

For this example, assume that both inputs to the sum block are single word and use binary-point scaling. The first input has a data type of `ufix16_En6`

, meaning a 16-bit unsigned fixed-point type with a fraction length of 6. The second input has a data type of `ufix16_En4`

, meaning a 16-bit unsigned fixed-point type with a fraction length of 4. See Fixed-Point Numbers in Simulink (Fixed-Point Designer) for more information on this notation.

`open_system("data_type_inherit_rules_add_binary_point_scaling_single_word.slx")`

This addition operation is illustrated schematically below. Note that in addition, the binary points must be aligned before performing the addition operation and an extra bit must be added to the result to account for overflows.

In this example, the ideal result type is smaller than the 32-bit maximum allowed on the specified hardware. Therefore, the three data type inheritance rules (`Inherit: Keep MSB`

, `Inherit: Keep LSB`

, and `Inherit: Same as accumulator`

) give the same result for the output data type.

The ideal result type has a word length of 12 - (-6) + 1 = 19 bits, including the overflow bit. Because the 32-bit hardware selected can fully represent the ideal type, the resulting output data type has a 32-bit word length and 6-bit fraction length.

### Data Type Inheritance Rules for Add with Binary-Point Scaling When Ideal Type is Multi-Word

For an addition operation, you can specify a data type inheritance rule separately for the accumulator type and the output data type. For this example, assume that both inputs to the sum block are single word and use binary-point scaling. The first input has a data type of `ufix32_En6`

, meaning a 32-bit unsigned fixed-point type with a fraction length of 6. The second input has a data type of `ufix32_En4`

, meaning a 32-bit unsigned fixed-point type with a fraction length of 4. See Fixed-Point Numbers in Simulink (Fixed-Point Designer) for more information on this notation.

In this example, the ideal result type is larger than the 32-bit maximum allowed on the specified hardware. Therefore, the data type inheritance rules must specify which bits to drop from the output result.

`open_system("data_type_inherit_rules_add_binary_point_scaling_multi_word.slx")`

This situation is illustrated schematically in the diagram below.

`Inherit: Keep MSB`

The output data type rule `Inherit: Keep MSB`

keeps the most significant bits in the sum output and guarantees single-word results. This rule is useful when you need to preserve the range of the result and are okay with losing precision, for example when one of the inputs has a significantly higher range than the other input, and the result must cover this range.

The accumulator data type rule is set to `Inherit: Inherit via internal rule`

. In this configuration, the accumulator data type rule automatically matches the output data type rule to avoid intermediate losses.

For this example, the ideal result type has a word length of 28 - (-6) + 1 = 35 bits, including the overflow bit. Because this is larger than the 32 bits supported by the specified hardware, the rule preserves the range. The power of the most-significant bit is 28 and the power of the least-significant bit is -3, resulting in an output data type with a word length of 32 bits and a fraction length of 3 bits.

`Inherit: Keep LSB`

The output data type rule `Inherit: Keep LSB`

keeps the least significant bits in the sum output and guarantees single-word results. This rule is useful when you need to preserve the precision of the result and are okay with losing range information, for example when adding values with meaningful precision, and whose ranges are significantly smaller than their data types can represent.

The accumulator data type rule is set to `Inherit: Inherit via internal rule`

. In this configuration, the accumulator data type rule automatically matches the output data type rule to avoid intermediate losses.

For this example, the ideal result type has a word length of 28 - (-6) + 1 = 35 bits, including the overflow bit. Because this is larger than the 32 bits supported by the specified hardware, the rule preserves precision in the result, keeping the least-significant bits. This results in an output data type with a word length of 32 bits and a fraction length of 6 bits.

`Inherit: Same as accumulator/Inherit: Inherit via internal rule`

The output data type rule `Inherit: Same as accumulator`

is useful when you need to maintain backwards compatibility in an existing model. For new models, the `Inherit: Keep MSB`

and `Inherit: Keep LSB`

rules are recommended for their clearer numerical intent and better efficiency on hardware.

The accumulator data type rule is set to `Inherit: Inherit via internal rule`

. In this configuration, the sum block uses an internal rule to determine both the accumulator and the output data type.

For this example, the ideal result type has a word length of 28 - (-6) + 1 = 35 bits, including the overflow bit. Because this is larger than the 32 bits supported by the specified hardware, the rule must drop bits. In this case, the rule picks the input type with the greater range, giving an output type with a word length of 32 bits and a fraction length of 4 bits. Because overflow has occurred, this output type causes loss of both range and precision as compared to the ideal result.

### Data Type Inheritance Rules for Add with Slope Scaling When Ideal Result Is Single Word

For an addition operation, you can specify a data type inheritance rule separately for the accumulator type and the output data type. For this example, assume that both inputs to the sum block are single word and use slope scaling. The first input has a data type of `ufix16_Sp4`

, meaning a 16-bit unsigned fixed-point type with a slope of 0.4. The second input has a data type of `ufix16_En6`

, meaning a 16-bit unsigned fixed-point type with 6 bits of fraction. See Fixed-Point Numbers in Simulink (Fixed-Point Designer) for more information on this notation.

`open_system("data_type_inherit_rules_add_slope_scaling_single_word.slx")`

`Inherit: Keep MSB and Inherit: Keep LSB`

The data type rules `Inherit: Keep MSB`

and `Inherit: Keep LSB`

take advantage of factors common to the slopes of the inputs to produce an output data type that can represent all possible resulting values with sufficient precision and range. If such an output data type fits in a single word, then `Inherit: Keep MSB`

and `Inherit: Keep LSB`

produce the same output data type.

For this example, the ideal result type is smaller than the 32-bit maximum allowed on the specified hardware. Therefore, the resulting output type has a word length of 32 bits. The slope is expressed as the common factor 0.2.

`Inherit: Same as accumulator/Inherit: Inherit via internal rule`

The output data type rule `Inherit: Same as accumulator`

is useful when you need to maintain backwards compatibility in an existing model. For new models, the `Inherit: Keep MSB`

and `Inherit: Keep LSB`

rules are recommended for their clearer numerical intent and better efficiency on hardware.

The accumulator data type rule is set to `Inherit: Inherit via internal rule`

. In this configuration, the sum block uses an internal rule to determine both the accumulator and the output data type.

For this example, the ideal result type is smaller than the 32-bit maximum allowed on the specified hardware. Therefore, the resulting output type has a word length of 32 bits. The slope is not expressed as a common factor of the inputs.

### Data Type Inheritance Rules for Add with Slope Scaling when Ideal Result Type is Multi-Word

For an addition operation, you can specify a data type inheritance rule separately for the accumulator type and the output data type. For this example, assume that both inputs to the sum block are single word and use slope scaling. The first input has a data type of ufix32_Sp4, meaning a 32-bit unsigned fixed-point type with a slope of 0.4. The second input has a data type of ufix32_En6, meaning a 32-bit unsigned fixed-point type with 6 bits of fraction. See Fixed-Point Numbers in Simulink (Fixed-Point Designer) for more information on this notation.

`open_system("data_type_inherit_rules_add_slope_scaling_multi_word.slx")`

`Inherit: Keep MSB`

The output data type rule `Inherit: Keep MSB`

keeps the most significant bits in the sum output and guarantees single-word results. This rule is useful when you need to preserve the range of the result and are okay with losing precision, for example when adding large numbers. When possible, the `Inherit: Keep MSB`

rule increases precision of the result by expressing the slopes as multiples of a common factor, if one is available.

The accumulator data type rule is set to `Inherit: Inherit via internal rule`

. In this configuration, the accumulator data type rule automatically matches the output data type rule to avoid intermediate losses.

For this example, the 35-bit ideal result type is larger than the 32-bit maximum allowed on the specified hardware. Therefore, the resulting output type has a word length of 32 bits, preserving the range bits and dropping 3 bits of precision. The slopes of the inputs can be expressed as the common factor 0.2. After dropping 3 bits of precision, the output slope is $0.2\text{\hspace{0.17em}}\mathit{X}\text{\hspace{0.17em}}{2}^{3}=1.6$.

`Inherit: Keep LSB`

The output data type rule `Inherit: Keep LSB`

keeps the least significant bits in the sum output and guarantees single-word results. This rule is useful when you need to preserve the precision of the result and are okay with losing range information, for example when adding small numbers.

`Inherit: Inherit via internal rule`

. In this configuration, the accumulator data type rule automatically matches the output data type rule to avoid intermediate losses.

For this example, the 35-bit ideal result type is larger than the 32-bit maximum allowed on the specified hardware. Therefore, the resulting output type has a word length of 32 bits, preserving the precision bits and dropping 3 bits of range. The slope of 0.2 is the common factor of the two input slopes.

`Inherit: Same as accumulator/Inherit: Inherit via internal rule`

The output data type rule `Inherit: Same as accumulator`

is useful when you need to maintain backwards compatibility in an existing model. For new models, the `Inherit: Keep MSB`

and `Inherit: Keep LSB`

rules are recommended for their clearer numerical intent and better efficiency on hardware.

The accumulator data type rule is set to `Inherit: Inherit via internal rule`

. In this configuration, the sum block uses an internal rule to determine both the accumulator and the output data type.

For this example, the 35-bit ideal result type is larger than the 32-bit maximum allowed on the specified hardware and the slope is not expressed as a common factor of the inputs. In this case, the internal rule picked the input type with the greater positive range.

### Data Type Inheritance Rules for Multiword Inputs

The data type rules `Inherit: Keep MSB`

,
`Inherit: Match Scaling`

, and ```
Inherit:
Keep LSB
```

are optimized for single word inputs. Multiword inputs
exhibit different behavior in response to data type rules that is not covered in
this topic.

## See Also

Gain | Product, Matrix Multiply | Add | Control Block Parameter Data Types | Control Data Types of Signals | Specify Fixed-Point Attributes for Blocks (DSP System Toolbox)