Documentation

Supported Operations on Chart Data

Binary and Bitwise Operations

The table below summarizes the interpretation of all binary operators in C charts. These operators work with the following order of precedence (0 = highest, 10 = lowest). Logical binary operators with the same precedence evaluate from left to right. The order of evaluation for other operators is unspecified. For example, the order in which `foo()` and `bar()` are evaluated is unspecified in the assignment below.

`A = foo() > bar();`

For more predictable results, it is good coding practice to split expressions that depend on the order of evaluation into multiple statements.

You can specify that the binary operators `&`, `^`, `|`, `&&`, and `||` are interpreted as bitwise operators in Stateflow® generated C code for a chart or for all the charts in a model. See these individual operators in the table below for specific binary or bitwise operator interpretations.

Example

Precedence

Description

`a ^ b`

0

Operand `a` raised to power `b`

Enabled when you clear Enable C-bit operations in the Chart properties dialog box. See Specify Chart Properties.

`a * b`

1

Multiplication

`a / b`

1

Division

`a %% b`

1

Remainder

`a + b`

2

`a - b`

2

Subtraction

`a >> b`

3

Shift operand `a` right by `b` bits. Noninteger operands for this operator are first cast to integers before the bits are shifted.

`a << b`

3

Shift operand `a` left by `b` bits. Noninteger operands for this operator are first cast to integers before the bits are shifted.

```a > b```

4

Comparison of the first operand greater than the second operand

`a < b`

4

Comparison of the first operand less than the second operand

`a >= b`

4

Comparison of the first operand greater than or equal to the second operand

`a <= b`

4

Comparison of the first operand less than or equal to the second operand

`a == b`

5

Comparison of equality of two operands

`a ~= b`

5

Comparison of inequality of two operands

`a != b`

5

Comparison of inequality of two operands

`a <> b`

5

Comparison of inequality of two operands

```a & b```

6

One of the following:

• Bitwise AND of two operands

Enabled when you select Enable C-bit operations in the Chart properties dialog box (default). See Specify Chart Properties.

• Logical AND of two operands

Enabled when you clear Enable C-bit operations in the Chart properties dialog box.

`a ^ b`

7

Bitwise XOR of two operands

Enabled when you select Enable C-bit operations in the Chart properties dialog box (default). See Specify Chart Properties.

`a | b`

8

One of the following:

• Bitwise OR of two operands

Enabled when you select Enable C-bit operations in the Chart properties dialog box (default). See Specify Chart Properties.

• Logical OR of two operands

Enabled when you clear Enable C-bit operations in the Chart properties dialog box.

`a && b`

9

Logical AND of two operands

`a || b`

10

Logical OR of two operands

Unary Operations

The following unary operators are supported in C charts. Unary operators have higher precedence than the binary operators, except for the power operator `a ^ b`. The power operator has the highest level of precedence. The operators are evaluated right to left (right associative).

Example

Description

`~a`

Logical NOT of `a`

Complement of `a` (if `bitops` is enabled)

`!a`

Logical NOT of `a`

`-a`

Negative of `a`

Unary Actions

The following unary actions are supported in C charts.

Example

Description

`a++`

Increment `a`

`a--`

Decrement `a`

Assignment Operations

The following assignment operations are supported in C charts.

Example

Description

`a = expression`

Simple assignment

`a := expression`

Used primarily with fixed-point numbers. See Assignment (=, :=) Operations for a detailed description.

`a += expression`

Equivalent to `a = a + expression`

`a -= expression`

Equivalent to `a = a - expression`

`a *= expression`

Equivalent to `a = a * expression`

`a /= expression`

Equivalent to `a = a / expression`

The following assignment operations are supported in C charts when Enable C-bit operations is selected in the properties dialog box for the chart. See Specify Chart Properties.

Example

Description

`a |= expression`

Equivalent to `a = a | expression` (bit operation). See operation `a | b` in Binary and Bitwise Operations.

`a &= expression`

Equivalent to `a = a & expression` (bit operation). See operation `a & b` in Binary and Bitwise Operations.

`a ^= expression`

Equivalent to `a = a ^ expression` (bit operation). See operation `a ^ b` in Binary and Bitwise Operations.

The address operator (&) is available in C charts for use with both Stateflow and custom code variables. The pointer operator (*) is available for use only with custom code variables.

Note

The parser uses a relaxed set of restrictions and does not catch syntax errors until compile time.

The following examples show syntax that is valid for both Stateflow and custom code variables. The prefix `cc_` shows the places where you can use only custom code variables, and the prefix `sfcc_` shows the places where you can use either Stateflow or custom code variables.

```cc_varPtr = &sfcc_var; cc_ptr = &sfcc_varArray[<expression>]; cc_function(&sfcc_varA, &sfcc_varB, &sfcc_varC); cc_function(&sfcc_sf.varArray[<expression>]); ```

The following examples show syntax that is valid only for custom code variables.

```varStruct.field = <expression>; (*varPtr) = <expression>; varPtr->field = <expression>; myVar = varPtr->field; varPtrArray[index]->field = <expression>; varPtrArray[expression]->field = <expression>; myVar = varPtrArray[expression]->field; ```

Type Cast Operations

You can use type cast operators to convert a value of one type to a value that can be represented in another type. Normally, you do not need to use type cast operators in actions because Stateflow software checks whether the types involved in a variable assignment differ and compensates by inserting the required type cast operator of the target language (typically C) in the generated code. However, external (custom) code might require data in a different type from those currently available. In this case, Stateflow software cannot determine the required type casts, and you must explicitly use a type cast operator to specify the target language type cast operator to generate.

For example, you might have a custom code function that requires integer RGB values for a graphic plot. You might have these values in Stateflow data, but only in data of type `double`. To call this function, you must type cast the original data and assign the result to integers, which you use as arguments to the function.

Stateflow type cast operations have two forms: the MATLAB® type cast form and the explicit form using the `cast` operator. These operators and the special `type` operator, which works with the explicit `cast` operator, are described in the topics that follow.

MATLAB Form Type Cast Operators

The MATLAB type casting form has the general form

```<type_op>(<expression>) ```

`<type_op>` is a conversion type operator that can be `double`, `single`, `int32`, `int16`, `int8`, `uint32`, `uint16`, `uint8`, or `boolean`. `<expression>` is the expression to be converted. For example, you can cast the expression `x+3` to a 16-bit unsigned integer and assign its value to the data `y` as follows:

```y = uint16(x+3) ```

Explicit Type Cast Operator

You can also type cast with the explicit `cast` operator, which has the following general form:

```cast(<expression>,<type>) ```

As in the preceding example, the statement

```y = cast(x+3,'uint16') ```

will cast the expression `x+3` to a 16-bit unsigned integer and assign it to `y`, which can be of any type.

type Operator

To make type casting more convenient, you can use a `type` operator that works with the explicit type cast operator `cast` to let you assign types to data based on the types of other data.

The `type` operator returns the type of an existing Stateflow data according to the general form

```type(<data>) ```

where `<data>` is the data whose type you want to return.

The return value from a `type` operation can be used only in an explicit `cast` operation. For example, if you want to convert the data `y` to the same type as that of data `z`, use the following statement:

```cast(y,type(z)) ```

In this case, the data `z` can have any acceptable Stateflow type.

Replace Operators with Application Implementations

You can configure the code generator to change the code that it generates for operators such that the code meets application requirements. To do this you configure the code generator to apply a code replacement library (CRL) during code generation. If you have an Embedded Coder® license, you can develop and apply custom code replacement libraries.

Operator entries of the code replacement library can specify integral or fixed-point operand and result patterns. Operator entries can be used for the following built-in operators:

 `+` `-` `*` `/`

For example, you can replace an expression such as ```y = u1 + u2``` with a target-specific implementation, as long as `u1`, `u2`, and `y` have types that permit a match with an addition entry in the code replacement library.

C chart semantics might limit operator entry matching because the chart uses the target integer size as its intermediate type in arithmetic expressions. For example, suppose a Stateflow action contains this arithmetic expression:

`y = (u1 + u2) % 3`

This expression computes the intermediate addition into a target integer. If the target integer size is 32 bits, you cannot replace this expression with an addition operator from the code replacement library to produce a signed 16-bit result, without a loss of precision.

For more information about replacing code, using code replacement libraries that MathWorks® provides, see What Is Code Replacement? (Simulink Coder) and Code Replacement Libraries (Simulink Coder). For information about developing custom code replacement libraries, see What Is Code Replacement Customization? (Embedded Coder) and Code You Can Replace From Simulink Models (Embedded Coder).