Repository navigation
Multiply‐accumulate (MAC) Unit
- 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+ 2add+dt+ takenbf) 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.
- A 32-bit multiply (
dmuls.l/mul.l) costs 3 cycles no matter what. Usemuls.w(1 cycle) whenever both operands fit in 16 bits. - Don't read the result too soon. Put 3 other instructions between a 32-bit multiply and its
sts(1 aftermuls.w). Reading it immediately wastes 2–4 cycles. - Read the
MACas rarely as possible. Eachstscosts at least 1 cycle and can stall. Accumulating withmac.wand reading once saves ≈1 per readout avoided;mac.wfrom memory costs about the same as just loading the operands. Pairing (dmuls.l;mul.l;sts mach;sts macl) still needs twosts. It saves 1–2 cycles because the second multiply fills the first one's stall, not because it reads less. - In hand asm, put
stsandmuls.won 4-byte-aligned addresses. They cost +1 cycle in the second half of a longword (addr%4==2).dmuls.l,mul.land a singlemac.w/mac.lcost the same at any alignment. Two chainedmac.ware 1 cycle cheaper when the first one is at addr%4==2, unless anstsreads the result right after them. - 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). - Separating
muls.wandstsby 1 instruction also gives +1 cycle, but only withmuls.wat 4n. At 4n+2 it costs +2.
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 |
| 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).
-
Naive:
stsright afterdmuls.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,r0A standalone (called)
FixedMulcan'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 >> 16inlines to the same instructions under GCC 12.1 -Os, withsts maclfirst, thensts mach;xtrct, and no call/rts. -
Scheduled: 3 independent instructions after
dmuls.l(no stall). One of the two adjacentstsstill 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,r0GCC 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.lbefore thedmuls.l), it emitted sts immediately afterdmuls.l. Check the disassembly. -
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,r2Two
FixedMuls can't shareMACH:MACL, because each needs both halves. -
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) >> 16It only applies to small fixed-point values.
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.wsaves 1 cycle (10.08 vs 11.10).dmuls.l → mul.lsaves 2 (12.10 vs 14.10). - Chained
mac.w @Rm+,@Rn+: here the claim holds. The firstmac.wcosts 3, and after that every pair costs 5, memory reads included. Eachmac.wdirectly 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 anopbetween 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
stsreads the result straight after the chain:mac.w; mac.w; sts maclcosts 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.
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 thedmuls.lproduct; MACL holds the full 32-bitmuls.wproduct. The low 32 bits of thedmuls.lproduct are lost. This is the pairing pattern:dmuls.l a,b; <2 insns>; muls.w c,d; <1 insn>; sts mach,r1; sts macl,r2gives(a*b)>>32andc*d. -
mac.w→mac.w: the products are added intoMACH: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.
The savings were measured with the multiplies and sts adjacent; these exact sequences were not timed.
-
dmuls.l→muls.wpairing (saves 1 cycle over readinga*bbefore themuls.w). Keeps the high half ofa*band all ofc*d. Withmul.linstead ofmuls.wit saves 2 and gives the low 32 bits ofc*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*dPut the
muls.wat a 4-byte-aligned address, or it costs 1 extra cycle. -
dmuls.l→mul.l(saves 2 cycles over readinga*bbefore themul.l). Keeps the high half ofa*band the low half ofc*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 -
mac.wchain (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, uselds rX,macl(andlds rY,mach) instead ofclrmac.
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.wcost 20 cycles (chain starting at addr%4==2), plusdt1 and the takenbf3: 24 cycles per 8 products, 16 memory reads included. That is 3 cycles per product. -
dtgoes at the top of the loop. Between themac.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
stsruns once and costs at most 3 cycles. - For
mac.l(32-bit data), use the same layout with 8mac.l: 30 cycles per 8 products, ≈4 per product.