! C1V2 hot kernels in SH-2 assembly: the IDCT and the YCbCr -> RGB555 conversion. ! Both compute exactly what c1dec.c's C versions compute (r32x/c1.py is the spec). ! ! c1_fast_t layout (c1dec.h; c1dec.c checks the offsets with _Static_assert): ! +0 int16 tT[64] IDCT pass 1, transposed: tT[c*8 + u] (the C build uses int32 t) ! +256 int32 res[64] IDCT output, res[y*8 + c] ! +512 int16 coef[64] coefficients X[u*8 + v] ! +768 int16 kt[64] the basis transposed: kt[c*8 + v] = K[v][c] ! +1024 conversion tables (CONV_*), see c1_conv_mb .include "c1_geom.inc" ! C1_W, the stored width (build_c1.sh) .section .sdram_text,"ax",@progbits ! --------------------------------------------------------------------------------- ! void c1_idct_mac(c1_fast_t *f, int rows, int cols) r4 = f, r5 = rows, r6 = cols ! ! Pass 1, u < rows, every c: tT[c*8+u] = (2048 + sum_{v> 12 ! Pass 2, every y and c: res[y*8+c] = (2048 + sum_{u> 12 ! (kt[y*8+u] = K[u][y].) Each sum is a run of MAC.W @Rm+,@Rn+ entered with BRAF so that ! exactly n MACs run, n = cols or rows. |X| < 2400 so |tT| < 13.6k fits 16 bits, and the ! sums fit MACL (32 bits). >> 12 is arithmetic: ((x ^ 0x80000000) >>> 12) - 0x80000. ! --------------------------------------------------------------------------------- .align 2 .global _c1_idct_mac _c1_idct_mac: mov.l r8,@-r15 mov.l r9,@-r15 mov.l r10,@-r15 mov.l r11,@-r15 mov.l r12,@-r15 mov.l r13,@-r15 mov.l r14,@-r15 mov.l .Li2048,r9 mov.l .Lisign,r10 mov.l .Libias,r11 mov #8,r0 sub r5,r0 add r0,r0 mov.l r0,@-r15 ! pass 2's BRAF offset: (8 - rows) * 2 mov #8,r0 sub r6,r0 add r0,r0 mov r0,r6 ! pass 1's BRAF offset: (8 - cols) * 2 mov.w .Li512,r12 add r4,r12 ! r12 = &X[u*8] mov.w .Li768,r13 add r4,r13 ! r13 = kt mov r4,r14 ! r14 = &tT[u] .Lp1u: mov r13,r2 ! &kt[c*8] mov r14,r8 ! &tT[c*8 + u] mov #8,r7 .Lp1c: mov r12,r1 mov r2,r3 clrmac mov r6,r0 braf r0 nop mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ sts macl,r0 add r9,r0 xor r10,r0 shlr8 r0 shlr2 r0 shlr2 r0 add r11,r0 mov.w r0,@r8 add #16,r8 dt r7 bf/s .Lp1c add #16,r2 add #16,r12 dt r5 bf/s .Lp1u add #2,r14 mov.l @r15+,r6 ! pass 2's offset mov.w .Li256,r8 add r4,r8 ! r8 = &res[0] mov #8,r5 ! y .Lp2y: mov r4,r2 ! &tT[c*8] mov #8,r7 .Lp2c: mov r13,r1 ! &kt[y*8] mov r2,r3 clrmac mov r6,r0 braf r0 nop mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ sts macl,r0 add r9,r0 xor r10,r0 shlr8 r0 shlr2 r0 shlr2 r0 add r11,r0 mov.l r0,@r8 add #4,r8 dt r7 bf/s .Lp2c add #16,r2 dt r5 bf/s .Lp2y add #16,r13 mov.l @r15+,r14 mov.l @r15+,r13 mov.l @r15+,r12 mov.l @r15+,r11 mov.l @r15+,r10 mov.l @r15+,r9 rts mov.l @r15+,r8 .align 1 .Li512: .short 512 .Li768: .short 768 .Li256: .short 256 .align 2 .Li2048: .long 2048 .Lisign: .long 0x80000000 .Libias: .long 0xFFF80000 ! --------------------------------------------------------------------------------- ! void c1_idct_add(c1_fast_t *f, int rows, int cols, uint8_t *dst, int stride, int intra) ! r4 = f, r5 = rows, r6 = cols, r7 = dst (8x8 in a plane), @r15 = stride, @(4,r15) = intra ! ! c1_idct_mac's two passes, but pass 2 adds each output into dst and clips to 0-255: ! dst[y][c] = clip(dst[y][c] + ((R + sum) >> 12)), R = 2048 ! An intra block is clip(128 + idct): the caller zeroes dst and R = 2048 + (128 << 12), ! exact because 128 << 12 is a whole multiple of the shift. ! --------------------------------------------------------------------------------- .align 2 .global _c1_idct_add _c1_idct_add: mov.l @r15,r1 ! stride mov.l @(4,r15),r2 ! intra mov.l r8,@-r15 mov.l r9,@-r15 mov.l r10,@-r15 mov.l r11,@-r15 mov.l r12,@-r15 mov.l r13,@-r15 mov.l r14,@-r15 mov.l .La2048,r9 tst r2,r2 bt .Lainter mov.l .La128s,r0 add r0,r9 ! intra: + 128 << 12 .Lainter: mov.l .Lasign,r10 mov.l .Labias,r11 mov.l r7,@-r15 ! dst add #-8,r1 mov.l r1,@-r15 ! stride - 8 mov #8,r0 sub r5,r0 add r0,r0 mov.l r0,@-r15 ! pass 2's BRAF offset mov #8,r0 sub r6,r0 add r0,r0 mov r0,r6 ! pass 1's BRAF offset mov.w .La512,r12 add r4,r12 mov.w .La768,r13 add r4,r13 mov r4,r14 .Lap1u: mov r13,r2 mov r14,r8 mov #8,r7 .Lap1c: mov r12,r1 mov r2,r3 clrmac mov r6,r0 braf r0 nop mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ sts macl,r0 mov.l .La2048,r1 ! pass 1 always rounds with 2048 add r1,r0 xor r10,r0 shlr8 r0 shlr2 r0 shlr2 r0 add r11,r0 mov.w r0,@r8 add #16,r8 dt r7 bf/s .Lap1c add #16,r2 add #16,r12 dt r5 bf/s .Lap1u add #2,r14 mov r4,r2 ! &tT[c*8], c = 0 mov.l @r15+,r6 ! pass 2's offset mov.l @r15+,r4 ! stride - 8 mov.l @r15+,r8 ! dst mov.w .La255,r12 mov #8,r5 .Lap2y: mov #8,r7 .Lap2c: mov r13,r1 mov r2,r3 clrmac mov r6,r0 braf r0 nop mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ mac.w @r1+,@r3+ sts macl,r0 add r9,r0 xor r10,r0 shlr8 r0 shlr2 r0 shlr2 r0 add r11,r0 mov.b @r8+,r1 ! the prediction (a cache read) extu.b r1,r1 add r1,r0 cmp/pz r0 bf .Laneg cmp/gt r12,r0 bt .Labig .Lastore: ! Pack four pixels into r14 (big-endian: the first in the top byte) and write ! them as one longword: the cache is write-through, so every store is a bus ! write, and byte stores made four times as many. dst is 8-byte aligned. shll8 r14 or r0,r14 dt r7 mov r7,r0 tst #3,r0 bf .Lanostore add #-4,r8 mov.l r14,@r8 add #4,r8 .Lanostore: tst r7,r7 bf/s .Lap2c add #16,r2 add #-128,r2 ! back to tT[0] add r4,r8 ! next row of dst dt r5 bf/s .Lap2y add #16,r13 mov.l @r15+,r14 mov.l @r15+,r13 mov.l @r15+,r12 mov.l @r15+,r11 mov.l @r15+,r10 mov.l @r15+,r9 rts mov.l @r15+,r8 .Laneg: bra .Lastore mov #0,r0 .Labig: bra .Lastore mov r12,r0 .align 1 .La512: .short 512 .La768: .short 768 .La255: .short 255 .align 2 .La2048: .long 2048 .La128s: .long 0x80000 ! 128 << 12 .Lasign: .long 0x80000000 .Labias: .long 0xFFF80000 ! --------------------------------------------------------------------------------- ! void c1_conv_mb(const uint8_t *y, const uint8_t *cb, const uint8_t *cr, ! volatile uint32_t *dst, const uint8_t *tabs) ! r4 = y (the MB's top-left, stride 320, 16-byte aligned), r5 = cb, r6 = cr (stride ! 160), r7 = dst (frame buffer words, stride 640 bytes), @r15 = tabs: ! tabs + 0 uint8 cl5[768] clip to 0-255 then 5 bits, index v + 256 ! tabs + 768 int32 cbT[256] (BCB[u] << 16) | (GCB[u] & 0xFFFF) ! tabs + 1792 int32 crT[256] (RCR[v] << 16) | (GCR[v] & 0xFFFF) ! Per 2x2: tr = cl5 + 256 + RCR, tg = ... + GCB + GCR, tb = ... + BCB. Each pixel pair ! (x, x+1) is read as one word of Y; its R, G and B bytes are paired in registers ! (left pixel in the top half) and each pair is shifted into place once: ! pixel = tr[y] | tg[y] << 5 | tb[y] << 10 (big-endian: the left pixel at the lower ! address). Rewritten from the 2026-11-04 review (two pixels per shift, Y as words). ! --------------------------------------------------------------------------------- .align 2 .global _c1_conv_mb _c1_conv_mb: mov.l @r15,r0 ! tabs mov.l r8,@-r15 mov.l r9,@-r15 mov.l r10,@-r15 mov.l r11,@-r15 mov.l r12,@-r15 mov.l r13,@-r15 mov.l r14,@-r15 mov.w .Lc256,r8 add r0,r8 ! r8 = cl5 + 256 mov.w .Lc768,r9 add r0,r9 ! r9 = cbT mov.w .Lc1792,r10 add r0,r10 ! r10 = crT mov #8,r0 mov.l r0,@-r15 ! row pairs left .Lcrow: .Lcblk: mov.b @r5+,r0 ! u mov.w @r4,r11 ! y0:y1 (top row) extu.b r0,r0 shll2 r0 mov.l @(r0,r9),r12 ! BCB<<16 | GCB mov.b @r6+,r0 ! v exts.w r12,r2 ! GCB extu.b r0,r0 shll2 r0 mov.l @(r0,r10),r13 ! RCR<<16 | GCR swap.w r12,r3 exts.w r3,r3 ! BCB exts.w r13,r0 ! GCR add r0,r2 swap.w r13,r1 exts.w r1,r1 ! RCR add r8,r1 ! tr add r8,r2 ! tg add r8,r3 ! tb ! top pair swap.b r11,r0 extu.b r0,r0 ! y0 mov.b @(r0,r1),r12 ! r_a mov.b @(r0,r2),r13 ! g_a mov.b @(r0,r3),r14 ! b_a extu.b r11,r0 ! y1 mov.b @(r0,r1),r11 ! r_b shll16 r12 or r11,r12 ! R pair mov.b @(r0,r2),r11 ! g_b shll16 r13 or r11,r13 ! G pair mov.b @(r0,r3),r11 ! b_b shll16 r14 or r11,r14 ! B pair shll2 r13 shll2 r13 shll r13 ! G pair << 5 shll8 r14 shll2 r14 ! B pair << 10 or r13,r12 or r14,r12 mov.l r12,@r7 ! bottom pair mov.w .Lc320,r0 mov.w @(r0,r4),r11 ! y0:y1 (bottom row) swap.b r11,r0 extu.b r0,r0 mov.b @(r0,r1),r12 mov.b @(r0,r2),r13 mov.b @(r0,r3),r14 extu.b r11,r0 mov.b @(r0,r1),r11 shll16 r12 or r11,r12 mov.b @(r0,r2),r11 shll16 r13 or r11,r13 mov.b @(r0,r3),r11 shll16 r14 or r11,r14 shll2 r13 shll2 r13 shll r13 shll8 r14 shll2 r14 or r13,r12 or r14,r12 mov.w .Lc640,r0 mov.l r12,@(r0,r7) add #2,r4 mov r4,r0 tst #15,r0 ! eight 2x2 blocks: y back on a 16-byte boundary bf/s .Lcblk add #4,r7 mov.w .Lc624,r0 ! next row pair: y += 640 - 16 add r0,r4 mov.w .Lc1248,r0 ! dst += 1280 - 32 add r0,r7 mov.w .Lc152,r0 ! chroma += 160 - 8 add r0,r5 add r0,r6 mov.l @r15,r0 dt r0 bf/s .Lcrow mov.l r0,@r15 add #4,r15 mov.l @r15+,r14 mov.l @r15+,r13 mov.l @r15+,r12 mov.l @r15+,r11 mov.l @r15+,r10 mov.l @r15+,r9 rts mov.l @r15+,r8 .align 1 .Lc256: .short 256 .Lc768: .short 768 .Lc1792: .short 1792 .Lc320: .short C1_W ! the bottom row of a pair .Lc640: .short 640 .Lc624: .short (2 * C1_W - 16) ! next row pair .Lc1248: .short 1248 .Lc152: .short (C1_W / 2 - 8) ! next chroma row .align 2