MATLAB: Is the Data Type Conversion code not included in the code generated by Real-Time Workshop Embedded Coder 5.1 (R2008a)

castconversiondataembeddedEmbedded Coderreal timetypeworkshop

I implement the following code in Simulink:
y = x * 128
M16C is a 16-bit microprocessor provided by RENESAS and it has some instructions (mnemonics) for shift operation depending on the data size of operand. For example if x and y are UINT16 the operation is coded as:
y = x << 7U;
If x is a UINT8, then it is possible to insert a Data Type Conversion block and the code generated using RTW-EC is :
y = (uint16_T)x << 7U;
RTW-EC deletes the cast operation represented by Data Type Conversion block if the following conditions are satisfied.
1. The cast data type is signed.
2. The cast data size is equal to "int" size in "hardware implementation" configuration.
In that case the generated code is:
y = x << 7U;
The result of right hand side makes precision loss (overflow). To avoid this overflow, x must be cast to 16-bit.

Best Answer

This is an expected behavior in Real-Time Workshop Embedded Coder.
To work around this issue, disable the option Eliminate superfluous temporary variables (Expression folding) which generates this code:
rtb_DataTypeConversion4 = (int16_T)sample_m_U.In1;
rtb_DataTypeConversion4 <<= 7U;
sample_m_Y.Out1 = rtb_DataTypeConversion4;
In order to disable the option "Eliminate superfluous temporary variables (Expression folding)", uncheck the checkbox for this option in the model's
Simulation -> Configuration Parameters -> Optimization pane.