The reductions that make a converted model smaller and easier to read, each with the model before and after it.

5 Reductions

The initial conversion performed by Modelify is a very verbose conversion. A Simulink block is created for each operation in C code. This can cause there to be a large model for a relatively small amount of code. To improve upon this verbose conversion, you can apply Reductions that can reduce the number of blocks necessary to represent the logic in the C code or to change the model to make it more readable.

Reductions can simplify or improve the model in several different ways. A reduction will achieve at least one of the following objectives:

  • reduce a number of blocks by replacing them with simpler or higher level Simulink block(s) that achieve the same result
  • replace a set of blocks to conform to specific modeling guidelines
  • remove sections of the model that do not affect the simulation result. Those model sections can occur when original C code has dead code.

You can enable or disable each reduction individually. This allows you to activate only the reductions you need to match the modeling guidelines used at your company.

The Reductions page, with a checked list of reductions such as Absolute Value, Advanced Arithmetic, and Bus Assignment, each with its category and description.

A description of each reduction is included below. Each reduction explanation includes two model screenshots that represent the model before and after the reduction.

Absolute Value

This reduction simplifies the model by replacing a set of blocks, whose logic represents the absolute of a value, by an Absolute value block.

There are two cases considered: one that returns an absolute value (case 1) and another that returns the negative of an absolute value (case 2).

Case 1:
A Switch block passes the input i when a Relational Operator finds it greater than 0, and otherwise passes i times −1.
Before Reduction
One Abs block takes the input i to return_value.
After Reduction
Case 2:
A Switch block passes i times −1 when a Relational Operator finds i greater than 0, and otherwise passes i.
Before Reduction
An Abs block followed by a Unary Minus block takes the input i to return_value.
After Reduction

Advanced Arithmetic

This reduction simplifies the model by:

  • removing Constants with value 0 from Sum blocks
  • removing Constants with value 1 from Product blocks
  • removing identical inputs to an arithmetic block

Remove Constants with value 0 from Sum blocks

There are two cases considered: one that just removes Constant and Sum blocks (case 1) and another which additionally requires the addition of a Unary Minus block (case 2).

Case 1:
A Sum block adds a Constant 0 to the input p, and a Product block multiplies the result by a Constant 3.
Before Reduction
The Product block multiplies p by the Constant 3 directly.
After Reduction
Case 2:
A Sum block subtracts the input p from a Constant 0, and a Product block multiplies the result by a Constant 3.
Before Reduction
A Unary Minus block negates p, and the Product block multiplies it by the Constant 3.
After Reduction

Remove Constants with value 1 from Product blocks

There are two cases considered: one that just removes Constant and Product blocks (case 1) and another which additionally requires the addition of a reciprocal Math Function block (case 2).

Case 1:
A Product block multiplies a Constant 1 by the input p, and a second Product block multiplies the result by a Constant 3.
Before Reduction
One Product block multiplies p by the Constant 3.
After Reduction
Case 2:
A Product block divides a Constant 1 by the input p, and a second Product block multiplies the result by a Constant 3.
Before Reduction
A Math Function block computes 1/u of p, and the Product block multiplies it by the Constant 3.
After Reduction

Remove identical inputs to an arithmetic block

Reduce identical inputs to a product block and replace them with a Math Function block.

Square (u^2)

When there are exactly two inputs to a product block with the same value, they are reduced to a square Math Function block.

One Product block multiplies the input p by itself, another multiplies a Constant 3 by a Constant 3, and a Sum block adds the two products.
Before Reduction
Two Math Function blocks compute u² of p and of the Constant 3, and the Sum block adds them.
After Reduction
Power (u^v)

Similar to the square reduction, this one is applied when there are more than two inputs to a product block with the same value.

This reduction only applies if the Arithmetic reduction is enabled, which collapses a chain of Product blocks with two inputs into a single Product block with several inputs.

A Product block multiplies the input p by itself three times.
Before Reduction
A Math Function block raises p to the power of a Constant 3.
After Reduction
Power of base 10 (10^u)

When there are two or more inputs to a product block from Constant blocks with value 10, they are reduced to a power of base 10 Math Function block.

This reduction only applies if the Arithmetic reduction is enabled.

A Product block multiplies three Constant 10 blocks.
Before Reduction
A Math Function block computes 10 to the power of a Constant 3.
After Reduction
Reciprocal (1/u)

This reduction applies when the first input to a product block is a Constant with value 1 and the remaining inputs are reduced to a single block by some other reduction (in the example, the square reduction).

This reduction only applies if the Arithmetic reduction is enabled

A Product block divides a Constant 1 by the input p twice.
Before Reduction
A Math Function block squares p, and a second Math Function block takes the reciprocal.
After Reduction

Arithmetic

This reduces multiple Sum blocks to a single Sum block with multiple inputs.

A Sum block adds a Constant 3 and the input p1, and a second Sum block subtracts the input p2 from the result.
Before Reduction
One Sum block adds the Constant 3 and p1 and subtracts p2.
After Reduction

Similarly, multiple Product blocks are reduced to a single Product block with multiple inputs.

A Product block multiplies a Constant 3 by the input p1, and a second Product block divides the result by the input p2.
Before Reduction
One Product block multiplies the Constant 3 by p1 and divides by p2.
After Reduction

Bus Assignment

This reduces multiple Bus Assignment blocks that are used with the same Bus input into a single Bus Assignment block.

Two Bus Assignment blocks in a row set the field G of the bus s to a Constant 100 and the field B to a Constant 255.
Before Reduction
One Bus Assignment block sets both G and B of the bus s.
After Reduction

If all elements of the bus are assigned by the several Bus Assignment blocks, then they are all replaced by a Bus Creator block.

Three Bus Assignment blocks in a row set the fields R, G, and B of a bus made from a Constant 0 to the Constants 10, 100, and 255.
Before Reduction
One Bus Creator block builds the bus from the Constants 10, 100, and 255 as R, G, and B.
After Reduction

Bus Selector

This joins multiple Bus Selector blocks if they are used with the same Bus input.

Two Bus Selector blocks take the fields R and B from the bus s, and a Sum block adds them.
Before Reduction
One Bus Selector block takes both R and B from the bus s for the Sum block.
After Reduction

Constant Vector

This replaces a group of Constant blocks and a Mux block, which create a single vector output, with a Constant 1-D vector.

A Mux block joins the Constants 1, 3, and 5 into a vector, and a Selector block picks one element.
Before Reduction
One Constant block holds the vector [1; 3; 5] for the Selector block.
After Reduction

Similarly, when there is a group of Constants, Mux and Vector Concatenate blocks so that they create a 2-dimensional array, then they are all replaced by a single Constant block that produces a matrix signal.

Three Mux blocks join the Constants 10 to 60 in pairs, and a Vector Concatenate block joins the pairs into a 2×3 matrix.
Before Reduction
One Constant block holds the 2×3 matrix [10 30 50; 20 40 60].
After Reduction

Data Type Conversions

There are cases where a converted model contains unnecessary Data Type Conversion blocks. This is usually caused by unnecessary casts operations in the original C code.

Data Type Conversion blocks convert p to int32 and d to single before a Product block divides them, and a third converts the result to double.
Before Reduction
The Product block divides p by d with no Data Type Conversion blocks, and outputs a double.
After Reduction

Dead Paths

This removes parts of the diagram that do not have any effect on the simulation output. These diagram sections correspond to either dead code or unused parameters in C functions.

Beside the Sum of the input p and a Constant 2, a Sum of p and a Constant 1 and the input temp each end in a Terminator.
Before Reduction
Only the Sum of p and the Constant 2 remains.
After Reduction

Demux

This reduces multiple Selector blocks that are used with the same vector input into a Demux block.

If some elements from the vector are not selected, then corresponding outputs from the Demux block will be connected to Terminator blocks. The reduction will be applied only if the number of Selector blocks to be removed is larger than the number of possibly added Terminator blocks.

Five Selector blocks each pick one element of the input array for a Sum block, a Relational Operator, and a Switch.
Before Reduction
One Demux block splits the input array into its three elements for the Sum block, the Relational Operator, and the Switch.
After Reduction

From and Goto

Inport blocks with two or more connections are simplified using From and Goto blocks. Note that for large diagrams, many line branches could cause considerable line crossing and cluttering. In those cases, usage of Goto and From blocks could improve readability.

A line carries the input p to a Relational Operator and across the model to a Switch.
Before Reduction
A Goto block tags p as [p], and From blocks bring it to the Relational Operator and the Switch.
After Reduction

Gain

This searches for blocks in the diagram that represent a product by a constant and it replaces them with Gain blocks. It can detect and group constants in a single expression for the Gain block.

Product blocks multiply the input p by a Constant K5, and p by a Constant 2 and then a Constant 5, and a Sum block adds the results.
Before Reduction
A Gain block of K5 and a Gain block of 2 * 5 multiply p, and the Sum block adds them.
After Reduction

Group Action Transitions

This joins multiple sequential action transitions into a single transition.

A Stateflow flow chart computes p, i, d, and calculated in four transitions, one action each, before it checks calculated against MAX.
Before Reduction
The same flow chart computes p, i, d, and calculated in one transition with all four actions.
After Reduction

Increment

This replaces blocks that represent an addition or subtraction by 1 by an Increment Stored Integer or Decrement Stored Integer block respectively.

One Sum block adds a Constant 1 to the input p1, another subtracts a Constant 1 from the input p2, and a Product block multiplies the results.
Before Reduction
An Increment Stored Integer block adds 1 to p1, a Decrement Stored Integer block subtracts 1 from p2, and the Product block multiplies them.
After Reduction

Logical Blocks

This reduces multiple Logical Operator blocks with the same operator into a single block. It also performs some optimizations if the TRUE/FALSE Constants reduction is enabled. If the operator is AND and one of the values is false, the result will always be false. In such cases, it removes the Logical Operator block and re-routes signals accordingly. It performs a similar optimization if the operator is OR and one of the values is true.

Three AND blocks combine the inputs c1, c2, and c3 and a Constant false, and an OR block joins two of their results.
Before Reduction
One three-input AND block combines c1, c2, and c3, and the OR block joins it with the Constant false.
After Reduction

Relational Operator

This reduces relational operators on constants to Compare to Constant blocks.

Relational Operator blocks compare the input p with a Constant 10 and a Constant 0 for an AND block and a Switch.
Before Reduction
Compare To Constant blocks test p ~= 10 and p >= 0, with no Constant blocks.
After Reduction

Saturation & MinMax

This reduction replaces a set of blocks by a MinMax, a Saturation or Saturation Dynamic block. All possible cases are considered below.

Min

It detects a combination of blocks in the diagram that represent the logic for getting the minimum of a number of values and it replaces the involved blocks with a MinMax block using the min function.

A Switch block passes a Constant 5 or the input dt, by a Relational Operator that compares them.
Before Reduction
One MinMax block outputs the min of the Constant 5 and dt.
After Reduction
Max

Similarly, it applies the same logic to detect and reduce to the max function. This example considers the case of getting the maximum of more than two values.

Two Switch blocks, each with a Relational Operator, pick between the input x and the Constants 20 and 10.
Before Reduction
One MinMax block outputs the max of x and the Constants 10 and 20.
After Reduction
Saturation

It searches for logic that bounds the range of a signal between two pre-defined values and it replaces involved blocks with a Saturation Block.

Two Switch blocks, each with a Relational Operator, keep the input in between the Constants 0 and 100.
Before Reduction
One Saturation block limits the input in.
After Reduction
Saturation Dynamic

It similarly searches for logic that bounds the range of a signal between two dynamically given values and it replaces involved blocks with a Saturation Dynamic Block.

Two Switch blocks, each with a Relational Operator, keep the input in between the inputs lo and hi.
Before Reduction
One Saturation Dynamic block keeps in between lo and hi.
After Reduction

Sum/Product of Element

This reduces the sum or product of all elements of a vector, which includes a Selector block per element, to one Sum or Product block.

This reduction only applies if the Arithmetic reduction is enabled, since it requires that all Selector blocks are connected to a single Sum or Product block.

Sum
Three Selector blocks each pick one element of the input array, and a Sum block adds them.
Before Reduction
One Sum block adds the elements of array.
After Reduction
Product
Three Selector blocks each pick one element of the input array, and a Product block multiplies them.
Before Reduction
One Product block multiplies the elements of array.
After Reduction

TRUE/FALSE Constants

This changes Constants with C constants TRUE/FALSE to actual Simulink boolean types, enabling some other optimizations. The example below shows that types for signals are boolean instead of int32 and it applies an optimization by using a NOT Logical Operator block.

Two Switch blocks pick between Constants true and false, by Relational Operators that compare the inputs a and b and a Constant 2.
Before Reduction
A NOT block on the second comparison replaces the inner Switch block and its Constants true and false.
After Reduction

The image below shows another case where an optimization is applied to reduce the model size.

A Switch block picks a Constant true or false by a Relational Operator that tests p <= 10, and a Data Type Conversion block makes the result boolean.
Before Reduction
The Relational Operator's result of p <= 10 goes straight to return_value.
After Reduction

Unary Minus

It reduces the model by removing Unary Minus blocks where possible.

Unary Minus blocks negate the Constant 2 twice, the Constant 1, and the Constant MAX_VAL before a Product block and a Sum block.
Before Reduction
The negations are folded into the Constants 2, −1, and −MAX_VAL.
After Reduction

Unit Delay Constant

Unit Delay blocks are included for static local variables. They allow modeling these variables' behavior; that is, they have an initial value that can change and be maintained on successive calls to the function.

Modelify can detect whether a static local variable is never changed in the function body. In those cases, the Unit Delay block can be replaced by a Constant block using the initialization value of the Unit Delay block.

Source code for sample conversion:

The C function redUnitDelayConstant, with a static int static_modified that it increments and a static float static_unmodified that it only reads.

The image below shows the two cases - one variable that is never modified (static_unmodified) and the other (static_modified) that is modified in each function invocation.

Two Unit Delay blocks hold the two static variables: one in a loop with an Increment Stored Integer block, and one in a loop with nothing that changes it.
Before Reduction
The Unit Delay of the unchanged variable becomes a Constant 10, and the incremented variable keeps its Unit Delay.
After Reduction

Vector Assignments

This reduces multiple Assignment blocks that are used with the same vector input into a single Mux block.

Three Assignment blocks in a row write a Constant 1 and the inputs p1 and p2 into a vector of three zeros.
Before Reduction
One Mux block joins the Constant 1, p1, and p2 into the vector.
After Reduction

Vector Selector

This reduces multiple Selector blocks that are used with the same vector input and index the same element of the input vector into a single Selector block.

Five Selector blocks pick elements of the input array, and two of them repeat the picks of others.
Before Reduction
Three Selector blocks, one for each element of array, feed the Sum block, the Relational Operator, and the Switch.
After Reduction
Contents

Have a question or want us to fix something?

What is this about?