Multiply‐accumulate (MAC) Unit - viciious/32XDK GitHub Wiki

Real-hardware SH-2 multiplier timings

How it was measured

  • Every test was a loop of exactly 12 instructions at the same 16-byte alignment, run 1000 × 475 times, with code and data in cache.
  • Timing came from the SH-2 watchdog timer (Fs/4096).
  • The calibration loop (8 nop + 2 add + dt + taken bf) measured 14.06 cycles against 14 expected. Two runs agreed within ±0.02.
  • "Cost" means the extra cycles over an identical loop with nops in place of the section under test.

Summary/rules of thumb

  1. A 32-bit multiply (dmuls.l/mul.l) costs 3 cycles no matter what. Use muls.w (1 cycle) whenever both operands fit in 16 bits.
  2. Don't read the result too soon. Put 3 other instructions between a 32-bit multiply and its sts (1 after muls.w). Reading it immediately wastes 2–4 cycles.
  3. Read the MAC as rarely as possible. Each sts costs at least 1 cycle and can stall. Accumulating with mac.w and reading once saves ≈1 per readout avoided; mac.w from memory costs about the same as just loading the operands. Pairing (dmuls.l; mul.l; sts mach; sts macl) still needs two sts. It saves 1–2 cycles because the second multiply fills the first one's stall, not because it reads less.
  4. In hand asm, put sts and muls.w on 4-byte-aligned addresses. They cost +1 cycle in the second half of a longword (addr%4==2). dmuls.l, mul.l and a single mac.w/mac.l cost the same at any alignment. Two chained mac.w are 1 cycle cheaper when the first one is at addr%4==2, unless an sts reads the result right after them.
  5. For an even-length chain, start it at addr%4==2 so that the cheap slots line up; it saves 1 cycle (mac.w) or 2 (mac.l).
  6. Separating muls.w and sts by 1 instruction also gives +1 cycle, but only with muls.w at 4n. At 4n+2 it costs +2.

Measured facts

Cost = cycles the instruction takes (a nop takes 1). "Waits" = stall cycles on top of that, when the instruction needs a result that isn't ready yet.

Instruction or sequence Cost
dmuls.l or mul.l (identical timing) 3 cycles, always
muls.w 1 cycle (2 if at addr%4==2)
sts mach / sts macl 1 cycle (2 if at addr%4==2), plus any wait for the result
sts right after a 32-bit multiply waits 2–4 extra cycles
sts 2 instructions after a 32-bit multiply (sts at addr%4==0) or 3 (otherwise) no wait
sts right after muls.w waits 1 extra cycle; 1 instruction in between removes it
MACH vs MACL ready at the same time
two 32-bit multiplies back to back 6 cycles, the same as when separated
muls.w right after dmuls.l waits 2 extra cycles
dmuls.l right after muls.w no wait (as in SH7604 manual Fig 7.58)
mul.l / muls.w write MACL only, MACH is unchanged
mov.w @Rn+ (cache hit) ≈1.5 cycles
mac.w @Rm+,@Rn+ (cache hit) 3 cycles at any alignment, the same as its 2 mov.w loads: the multiply-add is free
mac.l @Rm+,@Rn+ (cache hit) 3 cycles at any alignment, including its two 32-bit reads
two chained mac.w 6 cycles if the first is at addr%4==0, 5 if it is at addr%4==2
a mac.w directly after another mac.w 2 cycles at addr%4==0, 3 at addr%4==2 (reads included)
a mac.l directly after another mac.l 3 cycles at addr%4==0, 5 at addr%4==2 (reads included)
chain of 8 mac.w 20 cycles starting at addr%4==2, 21 at addr%4==0: 2.5 per product
chain of 8 mac.l 30 cycles starting at addr%4==2, 32 at addr%4==0: ≈4 per product
two mac.w with a nop between them 6 cycles for the two mac.w, at any alignment
sts macl right after two chained mac.w 1 cycle if the chain starts at addr%4==0, 3 if it starts at addr%4==2
lds + final sts around a mac.w chain ≈1 extra cycle in total

Other patterns

Pattern Effect Measured
mac.w @Rm+,@Rn+ instead of mov.w loads + muls.w for 16-bit dot products from memory 2× mac.w = 6.2, about the same as the 4 loads alone 4 loads = 6.12 and paired muls.w + 2 sts = 10.08, measured separately
Keep accumulating in MACH:MACL and read once ≈1 cycle per readout avoided yes
dmuls.l a; mul.l c; sts mach; sts macl → hi(a·b) and lo(c·d) saves 2.0 cycles vs reading between the two 12.1 vs 14.1
The same with muls.w as the second multiply saves 1.0 10.1 vs 11.1
muls.w instead of mul.l for 16-bit operands saves ≈3 per multiply+read 11.1 vs 14.1 serial
muls.w straight after dmuls.l wastes ≈2 yes
sts straight after muls.w wastes ≈1–2 yes

Two register multiplies with their reads take about 12 cycles (paired dmuls.l; mul.l; sts; sts = 12.1).

16.16 fixed point multiply (FixedMul)

  1. Naive: sts right after dmuls.l (stalls).

    FixedMul:               ! r4 = a, r5 = b, returns (a*b) >> 16
        dmuls.l r4,r5
        sts     mach,r1     ! stalls waiting for the result
        sts     macl,r0
        rts
        xtrct   r1,r0
    

    A standalone (called) FixedMul can't avoid the stall: its only work is the multiply, so nothing can fill the gap before the sts. The C macro (int64_t)a*b >> 16 inlines to the same instructions under GCC 12.1 -Os, with sts macl first, then sts mach; xtrct, and no call/rts.

  2. Scheduled: 3 independent instructions after dmuls.l (no stall). One of the two adjacent sts still sits at addr%4==2.

        dmuls.l r4,r5
        <independent insn>  ! must not touch MACH/MACL
        <independent insn>  ! and must not need the product
        <independent insn>
        sts     mach,r1     ! no stall
        sts     macl,r0
        xtrct   r1,r0
    

    GCC 12.1 sometimes does this itself. Given FixedMul(a,b) + x*3 + (y<<2) - d, it put 7 instructions between them. In another test (loads + add + mul.l before the dmuls.l), it emitted sts immediately after dmuls.l. Check the disassembly.

  3. Two FixedMuls: read A, start B, then finish A while B runs.

        dmuls.l r4,r5       ! A
        <3 independent insns>
        sts     mach,r1
        sts     macl,r0     ! A fully read; MAC free
        dmuls.l r6,r7       ! B
        xtrct   r1,r0       ! finish A while B runs
        <2 independent insns>
        sts     mach,r3
        sts     macl,r2
        xtrct   r3,r2
    

    Two FixedMuls can't share MACH:MACL, because each needs both halves.

  4. Both raw operands fit in signed 16 bits, i.e. |x| < 0.5 in 16.16. The product and the result then always fit, so no other conditions apply.

        muls.w  r4,r5
        <1 independent insn>
        sts     macl,r0
        shlr16  r0
        exts.w  r0,r0       ! shlr16 + exts.w = arithmetic (a*b) >> 16
    

    It only applies to small fixed-point values.

The "2 multiplies in 5 cycles" claim

The claim: one multiply costs 3 clocks, a second only 2 more (Tom Forsyth). On real hardware the multiplies themselves never overlap: a 32-bit multiply always costs 3, a muls.w 1. The measured savings come from elsewhere:

  • Pairing: issue the second multiply before reading the first, then read both results after a single wait. dmuls.l → muls.w saves 1 cycle (10.08 vs 11.10). dmuls.l → mul.l saves 2 (12.10 vs 14.10).
  • Chained mac.w @Rm+,@Rn+: here the claim holds. The first mac.w costs 3, and after that every pair costs 5, memory reads included. Each mac.w directly after another costs 2 at addr%4==0 and 3 at addr%4==2. Starting the chain at addr%4==2 gets 5 for the first pair as well (6 when it starts at addr%4==0, and 6 with a nop between the two). Measured chain costs, starting at addr%4==2 / addr%4==0: 2 → 5 / 6, 4 → 10 / 11, 6 → 15 / 16, 8 → 20 / 21.
  • The saving disappears if sts reads the result straight after the chain: mac.w; mac.w; sts macl costs 8 starting at addr%4==2 and 7 starting at addr%4==0. So when the chain is read right away, start it at addr%4==0.

Keeping both results

dmuls.l writes MACH and MACL. muls.w and mul.l' write only MACL`.

  • dmuls.l → muls.w: both are usable. MACH keeps the high 32 bits of the dmuls.l product; MACL holds the full 32-bit muls.w product. The low 32 bits of the dmuls.l product are lost. This is the pairing pattern: dmuls.l a,b; <2 insns>; muls.w c,d; <1 insn>; sts mach,r1; sts macl,r2 gives (a*b)>>32 and c*d.
  • mac.w → mac.w: the products are added into MACH:MACL, so only their sum is kept, not each product. Use it for dot products and sums of products.

muls.w or mul.l as the second multiply?

Use muls.w whenever c and d both fit in signed 16 bits. The pair is then 2 cycles cheaper in total (10.08 vs 12.10), because muls.w costs 1 cycle instead of 3. mul.l saves more against its own serial version (2 vs 1), but the muls.w pair is still the faster of the two. Its product is also the full, exact c*d, since a 16×16 product always fits in 32 bits. Use mul.l only when c or d needs more than 16 bits; it then gives the low 32 bits of c*d.

Code examples

The savings were measured with the multiplies and sts adjacent; these exact sequences were not timed.

  1. dmuls.l → muls.w pairing (saves 1 cycle over reading a*b before the muls.w). Keeps the high half of a*b and all of c*d. With mul.l instead of muls.w it saves 2 and gives the low 32 bits of c*d:

        dmuls.l r4,r5       ! MACH:MACL = a*b
        <2 independent insns>
        muls.w  r6,r7       ! MACL = c*d, MACH still = high 32 bits of a*b
        <1 independent insn>
        sts     mach,r1     ! r1 = (a*b) >> 32
        sts     macl,r2     ! r2 = c*d
    

    Put the muls.w at a 4-byte-aligned address, or it costs 1 extra cycle.

  2. dmuls.l → mul.l (saves 2 cycles over reading a*b before the mul.l). Keeps the high half of a*b and the low half of c*d:

        ! r4 = a, r5 = b, r6 = c, r7 = d
        dmuls.l r4,r5       ! MACH:MACL = a*b
        <2-3 independent insns>
        mul.l   r6,r7       ! MACL = low 32 bits of c*d, MACH still = high 32 bits of a*b
        <2-3 independent insns>
        sts     mach,r1     ! r1 = high 32 bits of a*b = (a*b) >> 32
        sts     macl,r2     ! r2 = low 32 bits of c*d
    
  3. mac.w chain (the multiplies cost ≈0 over the loads). Sum of products of two int16 arrays:

        clrmac              ! MACH:MACL = 0
        mac.w   @r4+,@r5+   ! += a[0]*b[0]
        mac.w   @r4+,@r5+   ! += a[1]*b[1]
        sts     macl,r0     ! r0 = a[0]*b[0] + a[1]*b[1]
    

    The S flag must be clear (clrs) for plain, non-saturating accumulation. To start from a value instead of 0, use lds rX,macl (and lds rY,mach) instead of clrmac.

Fastest mac.w loop.

Every cost below was measured, except where noted.

! r4 = a (int16_t *), r5 = b (int16_t *), r6 = n / 8 (n a multiple of 8)
! returns sum(a[i] * b[i]) in r0 (MACH holds the high half)
dot16:
    clrmac
    stc     sr,r0       ! clear S: plain 64-bit accumulate, no saturation
    mov     #-3,r1      ! (clrs is SH-3 only: illegal on the SH-2)
    and     r1,r0
    ldc     r0,sr
    .balignw 4,0x0009   ! loop label at addr%4==0
1:  dt      r6          ! addr%4==0; mac.w doesn't touch T
    mac.w   @r4+,@r5+   ! addr%4==2: the chain starts here
    mac.w   @r4+,@r5+
    mac.w   @r4+,@r5+
    mac.w   @r4+,@r5+
    mac.w   @r4+,@r5+
    mac.w   @r4+,@r5+
    mac.w   @r4+,@r5+
    mac.w   @r4+,@r5+
    bf      1b          ! taken: 3 cycles
    rts
    sts     macl,r0     ! read once, after the loop
  • The 8 mac.w cost 20 cycles (chain starting at addr%4==2), plus dt 1 and the taken bf 3: 24 cycles per 8 products, 16 memory reads included. That is 3 cycles per product.
  • dt goes at the top of the loop. Between the mac.ws it would break the chain, and at the end it would make the chain start at addr%4==0 (21 instead of 20).
  • The loop as a whole was not timed; its cost is the sum of the measured parts. The final sts runs once and costs at most 3 cycles.
  • For mac.l (32-bit data), use the same layout with 8 mac.l: 30 cycles per 8 products, ≈4 per product.
⚠️ **GitHub.com Fallback** ⚠️