omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / polyglot / asm / omx_crumb.S
| 1 | /* POLYGLOT-0 lane B1: Omega-X 2-bit crumb realization, run kernel in hand |
| 2 | * AArch64 assembly (GNU as 2.42). spec/polyglot-0.md sections 1 and 5. |
| 3 | * |
| 4 | * Derivation: written by hand from the crumb code and the algorithm. gcc -S |
| 5 | * output of the MA-3 C kernels was NOT generated or consulted. Not |
| 6 | * compiler-derived. |
| 7 | * |
| 8 | * int omx_asm_crumb_run(const oma_rz_plan *p, const int8_t *x, int32_t *y) |
| 9 | * p->m at [p,#0], p->n at [p,#8], p->mem at [p,#16] (static-asserted in |
| 10 | * src/polyglot/omx_asm.c). |
| 11 | * Packed layout (identical to MA-3 R2c_crumb): per row, ceil(n/64) chunks |
| 12 | * of 16 bytes (row_bytes = 16*ceil(n/64)); bits 2k..2k+1 of byte j of |
| 13 | * chunk c hold weight c*64 + 16k + j as a 2-bit two's-complement code |
| 14 | * (+1 = 01, 0 = 00, -1 = 11); columns >= n are code 00. |
| 15 | * Returns 0 (OMA_RZ_OK) or -1 (OMA_RZ_E_ARG) for NULL p/x/y/mem or an |
| 16 | * empty shape. |
| 17 | * |
| 18 | * Decode: w_k = (int8)(b << (6 - 2k)) >> 6 (arithmetic): shl + sshr for |
| 19 | * k = 0..2, a single sshr for k = 3. Lane j of decoded vector k pairs with |
| 20 | * x[c*64 + 16k + j], so each decoded vector goes straight into SDOT with |
| 21 | * one 16-byte x vector: 7 shifts + 4 SDOT per 64 weights per row. |
| 22 | * Rows in blocks of 4 (x loaded once per chunk per block, 8 accumulators |
| 23 | * v24-v31), leftover rows one at a time. |
| 24 | * x tail: if n % 64 != 0 the last chunk's x is copied once per call into a |
| 25 | * zeroed 64-byte stack buffer, so x is never read past x[n-1]. Weight |
| 26 | * reads stay inside m*row_bytes; y writes are exactly m int32. |
| 27 | * Registers: v0-v7, v16-v31 only (d8-d15 untouched), x0-x16. 64 bytes of |
| 28 | * stack. int32 accumulation is exact for n <= OMA_RZ_MAX_N. |
| 29 | */ |
| 30 | .arch armv8.6-a+dotprod+i8mm |
| 31 | .text |
| 32 | |
| 33 | /* one row, one 64-weight chunk: wv = 16 packed bytes, x in v0-v3 */ |
| 34 | .macro CROW wv, A, B, t0, t1, t2, t3 |
| 35 | shl \t0\().16b, \wv\().16b, #6 |
| 36 | shl \t1\().16b, \wv\().16b, #4 |
| 37 | shl \t2\().16b, \wv\().16b, #2 |
| 38 | sshr \t3\().16b, \wv\().16b, #6 |
| 39 | sshr \t0\().16b, \t0\().16b, #6 |
| 40 | sshr \t1\().16b, \t1\().16b, #6 |
| 41 | sshr \t2\().16b, \t2\().16b, #6 |
| 42 | sdot \B\().4s, \t3\().16b, v3.16b |
| 43 | sdot \A\().4s, \t0\().16b, v0.16b |
| 44 | sdot \B\().4s, \t1\().16b, v1.16b |
| 45 | sdot \A\().4s, \t2\().16b, v2.16b |
| 46 | .endm |
| 47 | |
| 48 | /* four rows (x10..x13), one chunk */ |
| 49 | .macro CHUNK4 |
| 50 | ldr q4, [x10], #16 |
| 51 | ldr q5, [x11], #16 |
| 52 | ldr q6, [x12], #16 |
| 53 | ldr q7, [x13], #16 |
| 54 | CROW v4, v24, v28, v16, v17, v18, v19 |
| 55 | CROW v5, v25, v29, v20, v21, v22, v23 |
| 56 | CROW v6, v26, v30, v16, v17, v18, v19 |
| 57 | CROW v7, v27, v31, v20, v21, v22, v23 |
| 58 | .endm |
| 59 | |
| 60 | .p2align 6 |
| 61 | .globl omx_asm_crumb_run |
| 62 | .type omx_asm_crumb_run, %function |
| 63 | omx_asm_crumb_run: |
| 64 | cbz x0, .Lcr_err |
| 65 | cbz x1, .Lcr_err |
| 66 | cbz x2, .Lcr_err |
| 67 | ldp x3, x4, [x0] // x3 = m, x4 = n |
| 68 | ldr x5, [x0, #16] // x5 = packed base of current row block |
| 69 | cbz x5, .Lcr_err |
| 70 | cbz x3, .Lcr_err |
| 71 | cbz x4, .Lcr_err |
| 72 | |
| 73 | sub sp, sp, #64 |
| 74 | movi v0.16b, #0 |
| 75 | stp q0, q0, [sp] |
| 76 | stp q0, q0, [sp, #32] |
| 77 | lsr x6, x4, #6 // x6 = full chunks |
| 78 | ands x8, x4, #63 // x8 = tail columns (0..63) |
| 79 | cinc x7, x6, ne // chunks per row |
| 80 | lsl x7, x7, #4 // x7 = row_bytes |
| 81 | cbz x8, 1f |
| 82 | add x9, x1, x6, lsl #6 // x + 64*full |
| 83 | mov x10, sp |
| 84 | mov x14, x8 |
| 85 | 0: ldrb w11, [x9], #1 |
| 86 | strb w11, [x10], #1 |
| 87 | subs x14, x14, #1 |
| 88 | b.ne 0b |
| 89 | 1: |
| 90 | |
| 91 | .Lcr_blk4: |
| 92 | cmp x3, #4 |
| 93 | b.lo .Lcr_rows1 |
| 94 | movi v24.16b, #0 |
| 95 | movi v25.16b, #0 |
| 96 | movi v26.16b, #0 |
| 97 | movi v27.16b, #0 |
| 98 | movi v28.16b, #0 |
| 99 | movi v29.16b, #0 |
| 100 | movi v30.16b, #0 |
| 101 | movi v31.16b, #0 |
| 102 | mov x9, x1 |
| 103 | mov x10, x5 |
| 104 | add x11, x10, x7 |
| 105 | add x12, x11, x7 |
| 106 | add x13, x12, x7 |
| 107 | cbz x6, 1f |
| 108 | mov x14, x6 |
| 109 | 0: ld1 {v0.16b, v1.16b, v2.16b, v3.16b}, [x9], #64 |
| 110 | CHUNK4 |
| 111 | subs x14, x14, #1 |
| 112 | b.ne 0b |
| 113 | 1: cbz x8, 2f |
| 114 | ld1 {v0.16b, v1.16b, v2.16b, v3.16b}, [sp] |
| 115 | CHUNK4 |
| 116 | 2: add v24.4s, v24.4s, v28.4s |
| 117 | add v25.4s, v25.4s, v29.4s |
| 118 | add v26.4s, v26.4s, v30.4s |
| 119 | add v27.4s, v27.4s, v31.4s |
| 120 | addp v24.4s, v24.4s, v25.4s |
| 121 | addp v26.4s, v26.4s, v27.4s |
| 122 | addp v24.4s, v24.4s, v26.4s // [r0 r1 r2 r3] |
| 123 | str q24, [x2], #16 |
| 124 | add x5, x5, x7, lsl #2 |
| 125 | sub x3, x3, #4 |
| 126 | b .Lcr_blk4 |
| 127 | |
| 128 | .Lcr_rows1: |
| 129 | cbz x3, .Lcr_ok |
| 130 | .Lcr_row1: |
| 131 | movi v24.16b, #0 |
| 132 | movi v28.16b, #0 |
| 133 | mov x9, x1 |
| 134 | mov x10, x5 |
| 135 | cbz x6, 1f |
| 136 | mov x14, x6 |
| 137 | 0: ld1 {v0.16b, v1.16b, v2.16b, v3.16b}, [x9], #64 |
| 138 | ldr q4, [x10], #16 |
| 139 | CROW v4, v24, v28, v16, v17, v18, v19 |
| 140 | subs x14, x14, #1 |
| 141 | b.ne 0b |
| 142 | 1: cbz x8, 2f |
| 143 | ld1 {v0.16b, v1.16b, v2.16b, v3.16b}, [sp] |
| 144 | ldr q4, [x10], #16 |
| 145 | CROW v4, v24, v28, v16, v17, v18, v19 |
| 146 | 2: add v24.4s, v24.4s, v28.4s |
| 147 | addv s24, v24.4s |
| 148 | str s24, [x2], #4 |
| 149 | add x5, x5, x7 |
| 150 | subs x3, x3, #1 |
| 151 | b.ne .Lcr_row1 |
| 152 | .Lcr_ok: |
| 153 | add sp, sp, #64 |
| 154 | mov w0, #0 |
| 155 | ret |
| 156 | |
| 157 | .Lcr_err: |
| 158 | mov w0, #-1 |
| 159 | ret |
| 160 | .size omx_asm_crumb_run, . - omx_asm_crumb_run |
| 161 | |
| 162 | .section .note.GNU-stack, "", %progbits |