## Addition and Subtraction Algorithm Types for Code Replacement

When you replace an addition or subtraction operator, your library function algorithm must match the algorithm type of the operator that you want to replace. Addition and subtraction operations are either cast-before-operation or cast-after-operation algorithms. First, determine which type of algorithm your library function implements. Then, to replace an operator in the generated code, determine how the code generator classifies the operator algorithm. Match your library function algorithm to the classification. For an example, see Replace Addition and Subtraction Operator Code.

### Algorithm Options

When creating a code replacement table entry for an addition or subtraction operator, first determine the type of algorithm that your library function implements.

• Cast-before-operation (CBO), default — Prior to performing the addition or subtraction operation, the algorithm type casts input values to the output type. If the output data type cannot exactly represent the input values, losses can occur as a result of the cast to the output type. Additional loss can occur when the result of the operation is cast to the final output type.

• Cast-after-operation (CAO) — The algorithm computes the ideal result of the addition or subtraction operation of the two inputs. The algorithm then type casts the result to the output data type. Loss occurs during the type cast. This algorithm behaves similarly to the C language except when the signedness of the operands does not match. For example, when you add a signed long operand to an unsigned long operand, standard C language rules convert the signed long operand to an unsigned long operand. The result is a value that is not ideal.

### Algorithm Classification

During code generation, the code generator examines addition and subtraction operations, including adjacent type cast operations, to determine the type of algorithm to compute the expression result. Based on the data types in the expression and the type of the accumulator (type used to hold the result of the addition or subtraction operation), the code generator uses these rules.

• Floating-point types only

Input 1 Data TypeInput 2 Data TypeAccumulator Data Type Output Data TypeClassification
`double``double``double``double`CBO, CAO
`double``double``double``single`
`double``double``single``double`
`double``double``single``single`CBO
`double``single``double``double`CBO, CAO
`double``single``double``single`
`double``single``single``double`
`double``single``single``single`CBO
`single``single``single``single`CBO, CAO
`single``single``single``double`
`single``single``double``single`
`single``single``double``double`CBO, CAO

• Floating-point and fixed-point types on the immediate addition or subtraction operation

AlgorithmConditions
CBO

One of the following is true:

• Operation type is `double`.

• Operation type is `single` and input types are `single` or fixed-point.

CAOOperation type is a superset of input types—that is, output type can represent values of input types without loss of data.

• Fixed-point types only

AlgorithmConditions
CBO

At least one of the following is true:

• Accumulator type equals output type (```Tacc == Tout```).

• Output type is a superset of input types (`Tacc >= {Tin1, Tin2}`) and accumulator type is a superset of output type (`Tacc >= Tout`).

• Operation does not incur range or precision loss.

CAONet bias is zero and the data types in the expression have equal slope adjustment factors. For more information on net bias, see “Addition” or “Subtraction” in Fixed-Point Operator Code Replacement (for MATLAB® code) or Fixed-Point Operator Code Replacement (for Simulink® models).

In many cases, the numerical result of a CBO operation is equal to that of a CAO operation. For example, if the input and output types are such that the operation produces the ideal result, as in the case of `int8 + int8 —> int16`. To maximize the probability of code replacement occurring in such cases, set the algorithm to cast-after-operation.

### Limitations

• When classifying an operation as a CAO operation, the code generator includes the adjacent casts in the expression when the expression involves only fixed-point types. Otherwise, the code generator classifies and replaces only the immediate addition or subtraction operation. Casts that the code generator excludes from the classification appear in the generated code.

• To enable the code generator to include multiple cast operations, which follow an addition or subtraction of fixed-point data, in the classification of an expression, the rounding mode must be `simplest` or `floor`. Consider the expression `y=(cast A)(cast B)(u1+u2)`. If the rounding mode of `(cast A)`, `(cast B)`, and the addition operator (+) are set to `simplest` or `floor`, the code generator takes into account `(cast A)` and ```(cast B)``` when classifying the expression and performing the replacement only.