Real-Time Workshop | ![]() ![]() |
Use of Data Types
In most processors, the use of integer data types can result in a significant reduction in data storage requirements, as well as a large increase in the speed of operation. You can achieve large performance gains on most processors by identifying those portions of your block diagram that are really integer calculations (such as accumulators), and implementing them with integer data types.
Floating-point DSP targets are an obvious exception to this rule.
The accumulator from the previous example used 64-bit floating-point calculations by default. The block diagram in Figure 9-14 implements the accumulator with 16-bit integer operations.
Figure 9-15: Accumulator Implemented with 16-bit Integers
If the Saturate on integer overflow option of the Sum block is turned off, the code generated from the integer implementation looks the same as code generated from the floating-point block diagram. However, since Sum_synth_accum is performing integer arithmetic internally, the accumulator executes more efficiently.
Note that, by default, the Saturate on integer overflow option is on. This option generates extra error-checking code from the integer implementation, as in the following example.
void MdlOutputs(int_T tid) { /* UnadornAccum Block: <Root>/Sum_synth_accum */ { int16_T tmpVar = rtB.Sum_synth_accum; rtB.Sum_synth_accum = tmpVar + (1); if ((tmpVar >= 0) && ((1) >= 0) && (rtB.Sum_synth_accum < 0)) { rtB.Sum_synth_accum = MAX_int16_T; } else if ((tmpVar < 0) && ((1) < 0) && (rtB.Sum_synth_accum >= 0)) { rtB.Sum_synth_accum = MIN_int16_T; } } /* Outport Block: <Root>/Out1 */ rtY.Out1 = rtB.Sum_synth_accum; }
The floating-point implementation would not have generated the saturation error checks, which apply only to integers. When using integer data types, consider whether or not you need to generate saturation checking code.
Figure 9-16 shows an efficient way to add reset capability to the accumulator. When resetSig
is greater than or equal to the threshold of the Switch block, the Switch block passes the reset value (0) back into the accumulator.
Figure 9-16: Integer Accumulator with Reset via External Input
The size of the resultant code is minimal. The code uses no floating-point operations.
void MdlOutputs(int_T tid) { /* local block i/o variables */ int16_T rtb_temp3; /* UnitDelay Block: <Root>/accumState */ rtb_temp3 = rtDWork.accumState_DSTATE; /* Expression for <Root>/Sum incorporates: */ /* Constant Block: <Root>/Increment */ /* Sum Block: <Root>/Sum */ { int16_T tmpVar1 = 0; int16_T tmpVar2; /* port 0 */ tmpVar1 = (1); /* port 1 */ tmpVar2 = tmpVar1 + rtb_temp3; if ((tmpVar1 >= 0) && (rtb_temp3 >= 0) && (tmpVar2 < 0)) { tmpVar2 = MAX_int16_T; } else if ((tmpVar1 < 0) && (rtb_temp3 < 0) && (tmpVar2 >= 0)) { tmpVar2 = MIN_int16_T; } rtb_temp3 = tmpVar2; } /* Outport Block: <Root>/accumVal */ rtY.accumVal = rtb_temp3; /* Expression for <Root>/Switch incorporates: */ /* Inport Block: <Root>/resetSig */ /* Constant Block: <Root>/ResetValue */ /* Switch Block: <Root>/Switch */ if (rtU.resetSig) { rtB.Switch = (0); } else { rtB.Switch = rtb_temp3; } }
In this example, it would be easy to use an input to the system as the reset value, rather than a constant.
Generating Pure Integer Code
The Real-Time Workshop Embedded Coder target provides the Integer code only option to ensure that generated code contains no floating-point data or operations. When this option is selected, an error is raised if any noninteger data or expressions are encountered during compilation of the model. The error message reports the offending blocks and parameters.
If pure integer code generation is important to your design, you should consider using the Real-Time Workshop Embedded Coder target (or a target of your own, based on the Real-Time Workshop Embedded Coder target).
To generate pure integer code, select ERT code generation options (1) from the Category menu in the Real-Time Workshop pane. Then select the Integer code only option, as shown below.
The Real-Time Workshop Embedded Coder target offers many other optimizations. See the Real-Time Workshop Embedded Coder documentation for further information.
Data Type Optimizations with Fixed-Point Blockset
and Stateflow
The Fixed-Point Blockset (a separate product) is designed to deliver the highest levels of performance for noninteger algorithms on processors lacking floating-point hardware. The Fixed-Point Blockset's code generation in Real-Time Workshop implements calculations using a processor's integer operations. The code generation strategy maps the integer value set to a range of expected real world values to achieve the high efficiency.
Finite-state machine or flowchart constructs can often represent decision logic (or mode logic) efficiently. Stateflow (a separate product) provides these capabilities. Stateflow, which is fully integrated into Simulink, supports integer data-typed code generation.
![]() | Accumulators | Stateflow Optimizations | ![]() |