Multiply-Accumulate
R2026bPerform a multiply-accumulate operation on the inputs
Multiply-Accumulate block
To add a block to a model, double-click the canvas and start typing the block name. Then, select the block from the list.
Libraries:
HDL Coder /
HDL Operations
Description
The Multiply-Accumulate block computes the product of inputs a, b, and adds a bias value c to produce dataOut.
dataOut = sum(a.* b) + c
The block uses the Operation Mode parameter to control how it
performs accumulation. With Operation Mode set to
Vector, the block computes the dot product of values at
the input ports a, b, then adds the bias
value c. By default, the bias value c is
0. To use a nonzero bias value, set Bias to
Dialog or Input port. The block performs
multiplication in full precision regardless of the Output data type
setting. The Output data type and Integer rounding
mode settings apply to the addition operation. To generate HDL code for
the block, use vector inputs. For scalar inputs, use the Multiply-Add block.
When Operation Mode is set to Streaming - using
Start and End ports or Streaming - using Number of
Samples, the block uses control signals to indicate when accumulation
starts, when accumulation ends, and when the output is valid. Use Streaming -
using Start and End ports when you want control signals to define the
accumulation interval. Use Streaming - using Number of Samples when
you want the block to accumulate a fixed number of valid samples.
Ports
Input
First multiplicand input, specified as a scalar, vector, matrix, or
array. This port provides one operand for the multiply-accumulate
operations. The block multiplies the value at this port with the value
at port b and accumulates the result according to
Operation Mode. When Operation
Mode is set to Vector, the
block computes a dot product across the input elements. When
Operation Mode is set to Streaming
- using Start and End ports or Streaming
- using Number of Samples, the block multiplies scalar
input samples and accumulates the result over time.
Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point
Second multiplicand input, specified as a scalar, vector, matrix, or
array. This port provides the second operand for the multiply-accumulate
operation. The block multiplies the value at this port with the value at
port a and accumulates the result according to
Operation Mode. When Operation
Mode is set to Vector, the
block computes a dot product across the input elements. When
Operation Mode is set to Streaming
- using Start and End ports or Streaming
- using Number of Samples, the block multiplies scalar
input samples and accumulates the result over time.
Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point
Bias or initial accumulator input, specified as a scalar. This port
provides the bias value or initial accumulation value when
Bias is set to Input
port. When Operation Mode is set
to Vector, the block adds this value to the
dot product. When Operation Mode is set to
Streaming - using Start and End ports or
Streaming - using Number of Samples, the
block uses this value as the initial accumulation value.
Dependencies
To enable this port, set Bias to
Input port.
The data type is the same base type as the dataOut accumulator type.
Start of accumulation control signal, specified as a scalar
Boolean value. This port provides a control
signal that indicates when accumulation starts. When Operation
Mode is set to Streaming - using Start and
End ports, the block uses startIn
with validIn to begin accumulation. To start
obtaining the accumulated output value from the
dataOut signal, both
validIn and startIn must
be high. The dataOut signal produces the
accumulated result from the next clock cycle.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Start and End
Ports.
Data Types: Boolean
Valid input control signal, specified as a scalar
Boolean value. This port provides the control
signal that indicates the input signal is valid for accumulation. When
Operation Mode is set to Streaming
- using Start and End Ports, the block begins
producing the accumulated output value at dataOut
only when both validIn and
startIn are high. The
dataOut signal produces the accumulated result
from the next clock cycle. When Operation Mode is
set to Streaming - using Number of Samples,
the block accumulates only the input samples for which
validIn is high. The
validIn signal has higher priority than the
startIn and endIn
signals.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Start and End ports
or Streaming - using Number of Samples.
Data Types: Boolean
End of accumulation control signal, specified as a scalar
Boolean value. This port provides the control
signal that indicates the end of accumulation. You can use the
startIn and endIn signals
with the validIn signal to indicate a frame that
contains the accumulated output.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Start and End ports
and then select Enable end input and
output.
Data Types: Boolean
Output
Accumulated output result, returned as a scalar. When
Operation Mode is set to
Vector, this port outputs the dot product
of value at port a and b plus
the bias value. When Operation Mode is set to
Streaming - using Start and End Ports,
this port outputs the accumulated result for the frame defined by the
start and end control signals. When Operation Mode
is set to Streaming - using Number of
Samples, this port outputs the accumulated result for the
specified number of valid samples.
Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | fixed point
Start of accumulation output control signal, returned as a scalar
Boolean value. This port generates the output
signal that indicates the start of accumulation. When both
validIn and startIn
become high, the startOut signal becomes high in
the next clock cycle. The clock cycle at which
startOut becomes high indicates the start of a
frame and that the dataOut signal has started
producing valid accumulated output.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Start and End ports
and then select Enable start output port.
Data Types: Boolean
Valid output signal, returned as a scalar Boolean
value. This port generates the output control signal that indicates the
dataOut signal is valid. When the
validIn signal becomes high, the
validOut signal becomes high in the next clock
cycle and indicates that the dataOut is
valid.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Start and End ports
or Streaming - using Number of Samples,
and then select Enable valid output
port.
Data Types: Boolean
End of accumulation output signal, returned as a scalar
Boolean value. This port generates the output
control signal that indicates the end of accumulation. You can use the
clock cycles between when the startOut signal
becomes high and when the endOut signal becomes
high to indicate a valid frame that contains the accumulated
output.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Start and End ports
or Streaming - using Number of Samples
and then select Enable end input and output
ports.
Data Types: Boolean
Count output control signal, returned as a scalar value.
This port generates output control signal to indicate the number of
samples accumulated. The value of this signal increases from
1 to the value that you specify for
Number of Samples. As long as the
validIn signal is high, the
countOut increments by 1
every clock cycle. The bit width of this port is
ceil(log2(Number of Samples + 1)).
Dependencies
To enable this port, set Operation Mode to
Streaming - using Number of Samples
and then select Enable count output port.
Data Types: uint8 | uint16 | uint32 | fixed point
Parameters
Specify how the block performs accumulation.
When Operation Mode is set to
Vector, the block performs the dot product of the values at the input ports a and b and adds the value at port c to produce the result.When Operation Mode is set to
Streaming - using Start and End ports, the block accumulates scalar input samples over a frame defined by control signals.When Operation Mode is set to
Streaming - using Number of Samples, the block accumulates a specified number of valid scalar input samples.
Programmatic Use
Block parameter:
opMode |
| Type: character vector |
Value:
'Vector' | 'Streaming - using Start and
End ports' | 'Streaming - using Number of
Samples' |
Default:
'Vector' |
Specify how the block gets the bias or initial accumulator value.
When Bias is set to
Dialog, the block uses the value specified by Initial valueWhen Bias is set to
Input port, the block uses input port c.
Programmatic Use
Each operating mode uses a separate set of parameters for bias source and value:
Vector mode:
Block parameter (source):
initValueSetting |
| Type: character vector |
Value:
'Dialog' | 'Input
port' |
Default:
'Dialog' |
Block parameter (value):
initValue |
| Type: character vector |
| Value: A scalar number |
Default:
'0' |
Streaming - using Start and End ports mode:
Block parameter (source):
initValueSetting2 |
Block parameter (value):
initValue2 |
Streaming - using Number of Samples mode:
Block parameter (source):
initValueSetting3 |
Block parameter (value):
initValue3 |
Specify the Number of Samples to define a frame containing the number of samples of valid accumulated output dataOut.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Number of
Samples.
Programmatic Use
Block parameter:
num_samples |
| Type: character vector |
| Value: A positive integer greater than zero |
Default:
'2' |
Set the output data type to:
A rule that inherits a data type, such as
Inherit: Same as first input.A built-in data type, such as
singleorint16.The name of a data type object. For instance, a
Simulink.NumericTypeobject.An expression that evaluates to a valid data type, for example,
fixdt(1,16,0)
The streaming modes do not support Inherit: Inherit via internal
rule. When you set the Output data type,
you can use the Data Type Assistant. To display the
assistant, click the Show data type assistant
.
Programmatic Use
Block parameter:
OutDataTypeStr |
| Type: character vector |
Default:
'Inherit: Inherit via internal rule' |
To see possible values that you can specify for this parameter, see Programmatically Specify Block Parameters and Properties.
Specify the rounding mode that the block uses for fixed-point addition.
CeilingRounds positive and negative numbers toward positive infinity. Equivalent to the MATLAB®
ceilfunction.ConvergentRounds number to the nearest representable value. If a tie occurs, rounds to the nearest even integer. Equivalent to the Fixed-Point Designer™
convergentfunction.FloorRounds positive and negative numbers toward negative infinity. Equivalent to the MATLAB
floorfunction.NearestRounds the number to the nearest representable value. If a tie occurs, rounds toward positive infinity. Equivalent to the Fixed-Point Designer
nearestfunction.RoundRounds number to the nearest representable value. If a tie occurs, rounds positive numbers toward positive infinity and rounds negative numbers toward negative infinity. Equivalent to the Fixed-Point Designer
roundfunction.SimplestChooses between rounding toward floor and rounding toward zero to generate rounding code that is as efficient as possible.
ZeroRounds number toward zero. Equivalent to the MATLAB
fixfunction.
Programmatic Use
Each operating mode uses a separate rounding mode parameter:
Block parameter (Vector mode):
RndMeth |
Block parameter (Streaming - using Start
and End ports):
RndMeth2 |
Block parameter (Streaming - using Number
of Samples):
RndMeth3 |
| Type: character vector |
Values:
'Floor' | 'Ceiling' |
'Convergent' | 'Nearest'
| 'Round' | 'Simplest' |
'Zero' |
Default:
'Floor' |
To see possible values that you can specify for this parameter, see Programmatically Specify Block Parameters and Properties.
Specify how the block handles fixed-point overflow during accumulation.
off (Wrap)The block wraps the result on overflow. This mode is the most efficient for HDL code generation.
on (Saturate)The block saturates the result on overflow. When the result overflows, the block sets the output to the closest representable value.
Dependencies
To enable this parameter, set Operation Mode to
Vector.
Programmatic Use
Block parameter:
DoSatur |
| Type: character vector |
Values:
'off' | 'on' |
Default:
'off' |
Select this parameter to enable the validOut output port.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Number of Samples or
Streaming - using Start and End
ports.
Programmatic Use
Block parameter:
validOut |
| Type: character vector |
Values:
'off' | 'on' |
Default:
'on' |
Select this parameter to enable the endIn input port and the endOut output port.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Start and End
Ports.
Programmatic Use
Block parameter:
endInandOut |
| Type: character vector |
Values:
'off' | 'on' |
Default:
'on' |
Select this parameter to enable the startOut output port.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Start and End
Ports.
Programmatic Use
Block parameter:
startOut |
| Type: character vector |
Values:
'off' | 'on' |
Default:
'on' |
Select this parameter to enable the countOut port.
Dependencies
To enable this port, set Operation Mode to
Streaming - using Number of
Samples.
Programmatic Use
Block parameter:
countOut |
| Type: character vector |
Values:
'off' | 'on' |
Default:
'on' |
Tips
With the Multiply-Accumulate block, you can:
Perform matrix multiplication operations. For example, if you have two matrix inputs with dimensions
N-by-MandM-by-P, you can compute the result by usingN-by-Pmultiply-accumulate operations in parallel.Replace a sequence of multiplication and addition operations, such as in filter blocks, and improve the performance on hardware by mapping to DSP slices on the FPGA. This figure shows how you can use the Multiply-Accumulate block with the
sfir_fixedmodel.
Algorithms
You can use the Operation Mode setting for the block to
specify a streaming mode of operation. When you select Streaming - using
Start and End Ports, you see three additional settings enabled by
default. The settings include:
Enable valid output port
Enable end input and output ports
Enable start output port
Leave these settings enabled. When you apply the settings, three additional input ports and three additional output ports appear:
| Input Ports | Output Ports |
|---|---|
| startIn | startOut |
| validIn | validOut |
| endIn | endOut |
This figure illustrates the streaming mode of operation using the start and end
ports. In this example, the bias value is 8.

Initially, when validIn is low, dataOut
is zero. At time 1s, both startIn and
validIn become high. Therefore,
validOut becomes high in the next clock cycle and
dataOut starts producing valid accumulation output. During
accumulation, dataOut takes the values of
a and b from the previous clock cycle.
For example, at time t = 2s, dataOut = 1*1 +
8 = 9.
To continue accumulation, make startIn low at the next clock cycle and keep validIn high. dataOut continues accumulating the inputs until validIn becomes low. At each time step, dataOut computes the product of the inputs from the previous clock cycle and sums the result with the dataOut value from the previous clock cycle. For example, at time t = 3s, dataOut = 2*2 + 9 = 13.
When validIn becomes low, dataOut holds the output value as seen at time t = 5s. At t = 5s, endIn and validIn are high. Therefore, endOut becomes high in the next clock cycle, which indicates end of frame. Therefore the frame between t = 2s (when startOut is high) and t = 6s (when endOut is high) indicates a frame containing valid output.
If startIn, validIn, and endIn are all high at the same time, only the dataOut corresponding to those inputs are accumulated as seen at t = 8s. If startIn is high for multiple clock cycles, and if validIn is high, the accumulator is reset at each clock cycle as seen at t = 10s and t = 11s. The accumulation continues at t = 12s.
You can use the Operation Mode setting for the block to
specify a streaming mode of operation. When you select Streaming - using
Number of Samples, you see two additional settings enabled by default.
The settings include:
Enable valid output port
Enable count output port
Leave these settings enabled. When you apply the settings, you have an additional input port validIn and three additional output ports appear:
endOut
validOut
countOut
This figure illustrates the streaming mode of operation using the number of
samples. In this example, the bias value is 8 and the
Number of Samples is 5.

Initially, when validIn is low, dataOut
is 0 and countOut is 1.
At time 1s, validIn becomes high. Therefore,
validOut becomes high in the next clock cycle, and
dataOut starts producing valid accumulation output. During
accumulation, dataOut takes the values of
a and b from the previous clock cycle.
For example, at time t = 2s, dataOut = 1*1 =
1. countOut increments by
1 at the next clock cycle, that is, at t =
3s, countOut becomes
2.
To continue accumulation, keep validIn high.
dataOut continues accumulating the inputs until
validIn becomes low. When five valid outputs are obtained
from dataOut, countOut becomes
5 and endOut becomes high, which
indicates the end of the frame. Therefore, the time between when
countOut is 1 and when
countOut is 5 indicates a frame containing valid
output.
The accumulator counter is now reset and countOut starts from
1. When validIn becomes high again,
dataOut starts accumulating a new set of values and
countOut starts incrementing for each valid
dataOut.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
HDL Coder™ provides additional configuration options that affect HDL implementation and synthesized logic.
| HDL Architecture Setting | Description |
|---|---|
Parallel | For input vectors of size |
Serial(Default) | For input vectors of size
When you synthesize your design, depending on the input bit widths, this architecture maps to one DSP slice on the FPGA. |
| General | |
|---|---|
| ConstrainedOutputPipeline | Number of registers to place at
the outputs by moving existing delays in the design. Distributed pipelining
does not redistribute these registers. The default value is
|
| InputPipeline | Number of input pipeline stages
to insert in the generated code. Distributed pipelining and constrained
output pipelining can move these registers. The default value is
|
| OutputPipeline | Number of output pipeline stages
to insert in the generated code. Distributed pipelining and constrained
output pipelining can move these registers. The default value is
|
| SynthesisAttributes |
Specifies the synthesis attributes for the blocks and block output signals in the model. The generated HDL code contains these attributes. For more information, see SynthesisAttributes. |
When you use complex signals, this block can generate HDL code, but does not map to DSP slices.
For the mapping of the Multiply-Accumulate block to DSP slices, use these reset type settings:
For AMD® FPGA boards, set Reset type to
Synchronous.For Altera® FPGA boards, set Reset type to
Asynchronous.
To set the reset type, select HDL Code Generation > Global settings > Clock settings > Reset type.
Matrix data types are not supported at the block port interfaces for HDL code generation. If you have matrix type signals, use the Product, Matrix Multiply block in matrix multiplication mode.
Streaming modes of operation for the block are not supported inside a Resettable Subsystem block for HDL code generation.
The block does not support floating-point data types for HDL code generation.
Inputs
aandbmust have the same dimensions. InVectormode, inputs must not both be scalar.For fixed-point inputs, ports
aandbmust have matching signedness, word length, and fraction length.In streaming modes, all input ports must operate at the same sample rate.
The Number of Samples parameter must be a positive integer greater than zero.
Version History
Introduced in R2017bUse the SynthesisAttributes HDL block property to specify the synthesis attributes for the block and its output signals. HDL Coder includes these attributes in the generated HDL code.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)