Files
daedalus-fourier/tests/bench_neon_h264idct8.c
marfrit db2205d0e3 Cycle 7 closed: H.264 IDCT 8x8 = 151 Mblock/s NEON, Phase 4 deferred
M1: 10000/10000 bit-exact first try (column-major-block lesson
from cycle 6 carried over cleanly).

M3: 151.2 Mblock/s per core. Per-block 6.6 ns. 155x the
1080p30 floor (0.972 Mblock/s req'd).

Phase-1 prediction of R7 = 0.5-0.9 YELLOW/GREEN was WRONG. H.264
IDCT 8x8 is dramatically lighter than VP9 IDCT 8x8 (18.5x faster
NEON):

  VP9 IDCT 8x8: 122 ns/block (Q14 trig + COSPI multiplies)
  H.264 IDCT 8x8: 6.6 ns/block (pure integer butterfly + shifts)

Phase 4 deferred via the cycle 6 lightweight-kernel rationale:
NEON per-block << QPU dispatch floor; offload doesn't help.

Phase 9 lesson updated: H.264 transforms (both 4x4 and 8x8) are
NEON-trivial. Skip ALL H.264 transform cycles for QPU. Target
compute-heavy H.264 kernels only (deblock = cycle 8 next; MC
likely RED).

Cycle 7 = 2nd consecutive "predicted GREEN, measured CPU-only"
result. Forces a sharper view of which kernels QPU can actually
help with: deblock and possibly some VP9 cases.

- tests/h264_idct8_ref.c (column-major C ref)
- tests/bench_neon_h264idct8.c (M1 + M3 bench)
- CMakeLists.txt: cycle 7 bench wiring
- docs/k7_h264idct8_phase3_and_4.md (closure)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 14:16:42 +00:00

196 lines
7.0 KiB
C

/*
* Cycle 7 Phase 3 — NEON M3 baseline for H.264 IDCT 8x8 + add.
*
* Tests ff_h264_idct8_add_neon against the standalone C reference
* (M1) and measures throughput (M3).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
extern void daedalus_h264_idct8_add_ref(uint8_t *dst, int16_t *block, ptrdiff_t stride);
extern void ff_h264_idct8_add_neon(uint8_t *dst, int16_t *block, ptrdiff_t stride);
#define DST_STRIDE 16
#define DST_ROWS 8
#define DST_BYTES (DST_ROWS * DST_STRIDE)
#define BLOCK_INT16 64
static uint64_t xs_state;
static inline uint64_t xs(void) {
uint64_t x = xs_state;
x ^= x << 13; x ^= x >> 7; x ^= x << 17;
return xs_state = x;
}
static void gen_block(int16_t b[BLOCK_INT16])
{
memset(b, 0, BLOCK_INT16 * sizeof(int16_t));
int n_nonzero = 1 + (int)(xs() % 24);
for (int i = 0; i < n_nonzero; i++) {
int pos = (int)(xs() % BLOCK_INT16);
int16_t v = (int16_t)((int)(xs() % 2048) - 1024);
b[pos] = v;
}
}
static double now_seconds(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
static int correctness_check(uint64_t seed, int n)
{
xs_state = seed ? seed : 0xc0de8000ULL;
int mismatches = 0, prints = 0;
int16_t block_a[BLOCK_INT16], block_b[BLOCK_INT16], block_saved[BLOCK_INT16];
uint8_t dst_a[DST_BYTES], dst_b[DST_BYTES], dst_initial[DST_BYTES];
for (int i = 0; i < n; i++) {
gen_block(block_a);
memcpy(block_b, block_a, sizeof(block_a));
memcpy(block_saved, block_a, sizeof(block_a));
for (int r = 0; r < 8; r++)
for (int c = 0; c < 8; c++)
dst_a[r * DST_STRIDE + c] = dst_b[r * DST_STRIDE + c] = (uint8_t)(xs() & 0xff);
memcpy(dst_initial, dst_a, DST_BYTES);
daedalus_h264_idct8_add_ref(dst_a, block_a, DST_STRIDE);
ff_h264_idct8_add_neon(dst_b, block_b, DST_STRIDE);
int diff = 0;
for (int r = 0; r < 8; r++)
for (int c = 0; c < 8; c++)
if (dst_a[r*DST_STRIDE + c] != dst_b[r*DST_STRIDE + c]) diff++;
if (diff) {
if (prints < 3) {
fprintf(stderr, "MISMATCH block %d (%d/64 pix diff):\n", i, diff);
fprintf(stderr, " block (column-major view as cols):");
for (int c = 0; c < 8; c++) {
fprintf(stderr, "\n c%d ", c);
for (int r = 0; r < 8; r++) fprintf(stderr, "%6d ", block_saved[c*8 + r]);
}
fprintf(stderr, "\n ref dst:");
for (int r = 0; r < 8; r++) {
fprintf(stderr, "\n r%d ", r);
for (int c = 0; c < 8; c++) fprintf(stderr, "%3u ", dst_a[r*DST_STRIDE+c]);
}
fprintf(stderr, "\n neon dst:");
for (int r = 0; r < 8; r++) {
fprintf(stderr, "\n r%d ", r);
for (int c = 0; c < 8; c++) fprintf(stderr, "%3u ", dst_b[r*DST_STRIDE+c]);
}
fprintf(stderr, "\n");
prints++;
}
mismatches++;
}
}
printf("M1₇ correctness: %d / %d blocks bit-exact (%.4f%%)\n",
n - mismatches, n, 100.0 * (n - mismatches) / n);
return mismatches;
}
static void throughput_neon(uint64_t seed, int n_blocks, double duration_s)
{
xs_state = seed ? seed : 0xc0de8000ULL;
int16_t *master_blocks = malloc((size_t) n_blocks * BLOCK_INT16 * sizeof(int16_t));
int16_t *work_blocks = malloc((size_t) n_blocks * BLOCK_INT16 * sizeof(int16_t));
uint8_t *master_dst = malloc((size_t) n_blocks * 64);
uint8_t *work_dst = malloc((size_t) n_blocks * 64);
if (!master_blocks || !work_blocks || !master_dst || !work_dst) {
fprintf(stderr, "alloc fail\n"); exit(1);
}
for (int i = 0; i < n_blocks; i++) {
gen_block(master_blocks + i * BLOCK_INT16);
for (int j = 0; j < 64; j++) master_dst[i * 64 + j] = (uint8_t)(xs() & 0xff);
}
memcpy(work_blocks, master_blocks, (size_t) n_blocks * BLOCK_INT16 * sizeof(int16_t));
memcpy(work_dst, master_dst, (size_t) n_blocks * 64);
for (int i = 0; i < n_blocks; i++)
ff_h264_idct8_add_neon(work_dst + i * 64, work_blocks + i * BLOCK_INT16, 8);
double t0 = now_seconds();
double t_end = t0 + duration_s;
uint64_t done = 0;
while (now_seconds() < t_end) {
memcpy(work_blocks, master_blocks, (size_t) n_blocks * BLOCK_INT16 * sizeof(int16_t));
memcpy(work_dst, master_dst, (size_t) n_blocks * 64);
for (int i = 0; i < n_blocks; i++)
ff_h264_idct8_add_neon(work_dst + i * 64, work_blocks + i * BLOCK_INT16, 8);
done += n_blocks;
}
double elapsed = now_seconds() - t0;
int iters = (int)(done / n_blocks);
double s0 = now_seconds();
for (int i = 0; i < iters; i++) {
memcpy(work_blocks, master_blocks, (size_t) n_blocks * BLOCK_INT16 * sizeof(int16_t));
memcpy(work_dst, master_dst, (size_t) n_blocks * 64);
}
double s1 = now_seconds();
double kernel_seconds = elapsed - (s1 - s0);
double mbps = done / kernel_seconds / 1e6;
printf("M3₇ NEON throughput:\n");
printf(" blocks/batch: %d\n", n_blocks);
printf(" batches done: %d\n", iters);
printf(" total blocks: %llu\n", (unsigned long long) done);
printf(" elapsed (kernel)=%.6f s\n", kernel_seconds);
printf(" throughput = %.3f Mblock/s\n", mbps);
printf(" per-block = %.1f ns\n", kernel_seconds / done * 1e9);
printf(" H.264 1080p30 IDCT8 floor: %.2fx margin (0.972 Mblock/s req'd)\n", mbps / 0.972);
free(master_blocks); free(work_blocks); free(master_dst); free(work_dst);
}
int main(int argc, char **argv)
{
int n_blocks = 65536;
double duration = 5.0;
uint64_t seed = 0;
int do_correctness = 1;
static struct option opts[] = {
{"blocks", required_argument, 0, 'b'},
{"duration", required_argument, 0, 'd'},
{"seed", required_argument, 0, 's'},
{"no-correctness", no_argument, 0, 'C'},
{0,0,0,0}
};
for (int c; (c = getopt_long(argc, argv, "b:d:s:C", opts, 0)) != -1;) {
switch (c) {
case 'b': n_blocks = atoi(optarg); break;
case 'd': duration = atof(optarg); break;
case 's': seed = strtoull(optarg, 0, 0); break;
case 'C': do_correctness = 0; break;
default: return 2;
}
}
if (do_correctness) {
printf("=== M1₇ bit-exact (10000 random 8x8 blocks) ===\n");
int mis = correctness_check(seed, 10000);
if (mis != 0) {
fprintf(stderr, "M1 gate FAILED — refusing to measure throughput.\n");
return 1;
}
printf("\n");
}
printf("=== M3₇ NEON throughput ===\n");
throughput_neon(seed, n_blocks, duration);
return 0;
}