Control Signed Left Shifts in Generated Code - MATLAB & Simulink (original) (raw)
Main Content
If you have an Embedded Coder® license, you can control whether MATLAB® Coder™ replaces multiplications by powers of two with signed left bitwise shifts. Some coding standards, such as MISRA™, do not allow bitwise operations on signed integers.
By default, MATLAB Coder replaces multiplication by powers of two with signed left shifts. Here is an example of generated C code that uses a signed left shift for multiplication by eight.
To increase the likelihood of generating MISRA C:2012 compliant code, disable the replacement of multiplication by powers of two with signed left shifts. Here is an example of generated C code that does not use a signed left shift for multiplication by eight:
Control Signed Left Shifts Using the MATLAB Coder App
- Set Build type to one of the following:
Static Library (.lib)
Dynamic Library (.dll)
Executable (.exe)
- Click Settings.
- On the Code Appearance tab, select or clear the Use signed shift left for fixed-point operations and multiplication by powers of 2 check box.
Control Signed Left Shifts Using the Command-Line Interface
- Create a code configuration object for
'lib'
,'dll'
, or'exe'
. For example:
cfg = coder.config('lib','ecoder',true); % or dll or exe - Set the
EnableSignedLeftShifts
property totrue
orfalse
. For example:
cfg.EnableSignedLeftShifts = false;