MATLAB: Do I receive different results in the generated code between Real-Time Workshop 6.0 (R14) and Real-Time Workshop 6.1 (R14SP1) and 6.0 (R14) when performing integer multiplication in the model

castintegermultiplicationoverflowrtwsimulink codertypecastvariable

I have a model that performs integer multiplication. The generated code for these operations by Real-Time Workshop 6.0 (R14) and Real-Time Workshop 6.1 (R14SP1) differs. The code generated by Real-Time Workshop 6.0 (R14) typecasts my int16 variables as int32, prior to the multiplication, as follows:
int32_T rtb_Product;
rtb_Product = (int32_T)EXT_s16Input * (int32_T)demo_cast_P.Constant1_Value;
The code generated by Real-Time Workshop 6.1 (R14SP1) does not typecast the variables, as shown below:
int32_T rtb_Product;
rtb_Product = EXT_s16Input * demo_cast_P.Constant1_Value;
This difference produces incorrect results when an integer overflow occurs due to the multiplication.

Best Answer

This bug has been fixed in Release 14 Service Pack 2 (R14SP2). For previous releases, please read below for any possible workarounds:
We have verified that there is a bug in Real-Time Workshop 6.1 (R14SP1) in the way that the generated code is optimized.
Currently, to workaround this issue, disable the "Ignore integer downcasts in folded expressions" option in the Optimization pane of the Configurations Parameters dialog box.