@ matvec_neon.S - hand-written ARMv7-A NEON matrix-vector kernels (variants 4 and 7). @ @ void matvec_f32_asm(const float *W, const float *x, float *y, int rows, int cols); @ void matvec_i8_asm (const int8_t *W, const int8_t *x, int32_t *y, int rows, int cols); @ @ AAPCS (hard-float): r0=W, r1=x, r2=y, r3=rows, [sp]=cols (5th arg on the stack). @ Callee-saved: r4-r11, d8-d15 (= q4-q7). We use r4-r6 and q4-q7 as accumulators, @ so we push/pop them. q0-q3 and q8-q15 are scratch. @ Requirements: cols % 16 == 0 (f32) or % 32 == 0 (i8); W and x 16-byte aligned @ (the :128 alignment hints below fault on unaligned data); rows * rowbytes keeps @ every row 16-byte aligned because cols is a multiple of 16/32. .syntax unified .arch armv7-a .fpu neon-vfpv4 .text .align 4 @ --------------------------------------------------------------------------- @ float32: y[r] = dot(W[r,:], x) @ 16 floats per iteration, 4 independent q accumulators (q4-q7) to hide the @ vmla latency; pld fetches 256 bytes (4 cache lines) ahead on the W stream. @ --------------------------------------------------------------------------- .global matvec_f32_asm .type matvec_f32_asm, %function matvec_f32_asm: push {r4-r6, lr} @ save callee-saved core regs (4 regs: sp stays 8-aligned) vpush {d8-d15} @ save callee-saved NEON regs ldr r4, [sp, #(4*4 + 8*8)] @ r4 = cols (stack arg, above our 16+64 byte frame) lsr r4, r4, #4 @ r4 = cols / 16 = inner iterations per row mov r5, r1 @ r5 = x base (r1 advances during a row) cmp r3, #0 beq 3f @ nothing to do 1: @ ---- row loop ---- mov r1, r5 @ rewind x mov r6, r4 @ inner counter veor q4, q4, q4 @ clear 4 accumulators veor q5, q5, q5 veor q6, q6, q6 veor q7, q7, q7 2: @ ---- inner loop: 16 MACs per pass ---- pld [r0, #256] @ prefetch W 256 B ahead vld1.32 {d16-d19}, [r0:128]! @ W[0..7] vld1.32 {d24-d27}, [r1:128]! @ x[0..7] vld1.32 {d20-d23}, [r0:128]! @ W[8..15] vld1.32 {d28-d31}, [r1:128]! @ x[8..15] vmla.f32 q4, q8, q12 @ acc += W*x (four independent chains) vmla.f32 q5, q9, q13 vmla.f32 q6, q10, q14 vmla.f32 q7, q11, q15 subs r6, r6, #1 bne 2b vadd.f32 q4, q4, q5 @ reduce 4 accumulators -> 1 q vadd.f32 q6, q6, q7 vadd.f32 q4, q4, q6 vadd.f32 d8, d8, d9 @ 4 lanes -> 2 vpadd.f32 d8, d8, d8 @ 2 lanes -> 1 vst1.32 {d8[0]}, [r2]! @ y[r] = sum subs r3, r3, #1 bne 1b 3: vpop {d8-d15} pop {r4-r6, pc} .size matvec_f32_asm, .-matvec_f32_asm @ --------------------------------------------------------------------------- @ int8 x int8 -> int32: y[r] = sum W[r,c]*x[c] (exact) @ 32 bytes per iteration: vmull.s8 widens to int16 products, vpadal.s16 @ pairwise-adds them into int32 accumulators (no overflow: |product| <= 16384). @ --------------------------------------------------------------------------- .global matvec_i8_asm .type matvec_i8_asm, %function matvec_i8_asm: push {r4-r6, lr} vpush {d8-d15} ldr r4, [sp, #(4*4 + 8*8)] @ r4 = cols lsr r4, r4, #5 @ cols / 32 iterations per row mov r5, r1 cmp r3, #0 beq 3f 1: mov r1, r5 mov r6, r4 veor q4, q4, q4 veor q5, q5, q5 veor q6, q6, q6 veor q7, q7, q7 2: pld [r0, #256] vld1.8 {d16-d19}, [r0:128]! @ 32 W bytes vld1.8 {d20-d23}, [r1:128]! @ 32 x bytes vmull.s8 q12, d16, d20 @ 8 x int8*int8 -> int16 vmull.s8 q13, d17, d21 vmull.s8 q14, d18, d22 vmull.s8 q15, d19, d23 vpadal.s16 q4, q12 @ acc32 += pairwise sums of int16 vpadal.s16 q5, q13 vpadal.s16 q6, q14 vpadal.s16 q7, q15 subs r6, r6, #1 bne 2b vadd.i32 q4, q4, q5 vadd.i32 q6, q6, q7 vadd.i32 q4, q4, q6 vadd.i32 d8, d8, d9 vpadd.i32 d8, d8, d8 vst1.32 {d8[0]}, [r2]! subs r3, r3, #1 bne 1b 3: vpop {d8-d15} pop {r4-r6, pc} .size matvec_i8_asm, .-matvec_i8_asm .section .note.GNU-stack,"",%progbits