When the automated conversion tool generates fixed-point code, it:
Minimizes fi-casts to improve code readability.
The tool tries to reduce the number of fi-casts by analyzing the floating-point code. If an arithmetic operation is comprised of only compile-time constants, the tool does not cast the operands to fixed point individually. Instead, it casts the entire expression to fixed point.
For example, here is the fixed-point code generated for the constant expression x = 1/sqrt(2) when the selected word length is 14.
|Original MATLAB® Code||Generated Fixed-Point Code|
x = 1/sqrt(2);
x = fi(1/sqrt( 2 ), 0, 14, 14, fm);
fm is the local fimath.
Avoids overflows in the generated fixed-point code.
The tool avoids overflows by:
Using full-precision arithmetic unless you specify otherwise.
Avoiding arithmetic operations that involve double and fi data types. Otherwise, if the word length of the fi data type is not able to represent the value in the double constant expression, overflows occur.
Controls bit growth.
The tool controls bit growth by using subscripted assignments, that is, assignments that use the colon (:) operator, in the generated code. When you use subscripted assignments, MATLAB overwrites the value of the left-hand side argument but retains the existing data type and array size. Using subscripted assignment keeps fixed-point variables fixed point rather than inadvertently turning them into doubles. Maintaining the fixed-point type reduces the number of type declarations in the generated code. Subscripted assignment also prevents bit growth which is useful when you want to maintain a particular data type for the output.