Contenu principal

Multiply-Accumulate

R2026b

Perform a multiply-accumulate operation on the inputs

  • Multiply-Accumulate block

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

expand all

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

expand all

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

expand all

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 value

  • When 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 single or int16.

  • The name of a data type object. For instance, a Simulink.NumericType object.

  • 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.

Ceiling

Rounds positive and negative numbers toward positive infinity. Equivalent to the MATLAB® ceil function.

Convergent

Rounds number to the nearest representable value. If a tie occurs, rounds to the nearest even integer. Equivalent to the Fixed-Point Designer™ convergent function.

Floor

Rounds positive and negative numbers toward negative infinity. Equivalent to the MATLAB floor function.

Nearest

Rounds the number to the nearest representable value. If a tie occurs, rounds toward positive infinity. Equivalent to the Fixed-Point Designer nearest function.

Round

Rounds 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 round function.

Simplest

Chooses between rounding toward floor and rounding toward zero to generate rounding code that is as efficient as possible.

Zero

Rounds number toward zero. Equivalent to the MATLAB fix function.

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-M and M-by-P, you can compute the result by using N-by-P multiply-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_fixed model.

    Illustrates the mapping of multiplication and addition operations to DSP slices.

Algorithms

expand all

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Version History

Introduced in R2017b

expand all