Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/polyglot/asm/omx_sdot.S # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / polyglot / asm / omx_sdot.S` 214 lines ยท 5733 bytes Raw Blame History 1 /* POLYGLOT-0 lane B1: Omega-X int8 SDOT 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 algorithm (y = W.x, W row-major int8 5 * ternary, x int8, y int32, exact). gcc -S output of the MA-3 C kernels was 6 * NOT generated or consulted. Not compiler-derived. 7 * 8 * int omx_asm_sdot_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). Weights: m rows of n bytes, no padding (the 11 * same packed form as MA-3 R1_sdot). 12 * Returns 0 (OMA_RZ_OK) or -1 (OMA_RZ_E_ARG) for NULL p/x/y/mem or an 13 * empty shape. 14 * 15 * Method: 16 * n >= 16: rows in blocks of 4. Per 64 columns: x loaded once (4 q regs), 17 * each row's 64 bytes loaded and SDOT-accumulated into two accumulators 18 * (8 total, v24-v31). Then 16-column steps. The final n%16 columns are 19 * done by one overlapping 16-byte load ending exactly at column n-1, 20 * with x lanes that were already counted masked to zero (mask table), 21 * so no byte outside W, x or y is ever read or written. 22 * Rows left over (m%4) use the same scheme with one row and four 23 * accumulators. 24 * n < 16: plain scalar multiply-add per element. 25 * Accumulation is int32; |partial| <= 128*n <= INT32_MAX for n <= max_n 26 * (OMA_RZ_MAX_N), so every lane sum is exact. 27 * Registers: only v0-v7 and v16-v31 (d8-d15 are callee-saved and untouched), 28 * x0-x17. Leaf function, no stack use. Unaligned x and y are fine 29 * (ld1/ldr/str on normal memory). 30 */ 31 .arch armv8.6-a+dotprod+i8mm 32 .text 33 34 .p2align 6 35 .globl omx_asm_sdot_run 36 .type omx_asm_sdot_run, %function 37 omx_asm_sdot_run: 38 cbz x0, .Lsd_err 39 cbz x1, .Lsd_err 40 cbz x2, .Lsd_err 41 ldp x3, x4, [x0] // x3 = m, x4 = n 42 ldr x5, [x0, #16] // x5 = weight base of current row block 43 cbz x5, .Lsd_err 44 cbz x3, .Lsd_err 45 cbz x4, .Lsd_err 46 cmp x4, #16 47 b.lo .Lsd_scalar 48 49 lsr x6, x4, #6 // x6 = number of 64-column steps 50 ubfx x7, x4, #4, #2 // x7 = remaining 16-column steps (0..3) 51 and x8, x4, #15 // x8 = tail t (0..15) 52 adrp x16, .Lsd_mask 53 add x16, x16, :lo12:.Lsd_mask 54 add x16, x16, x8 // mask: lanes >= 16-t are 0xff 55 mov x15, #16 56 sub x15, x15, x8 // back-off for the overlapping tail load 57 58 .Lsd_blk4: 59 cmp x3, #4 60 b.lo .Lsd_rows1 61 movi v24.16b, #0 62 movi v25.16b, #0 63 movi v26.16b, #0 64 movi v27.16b, #0 65 movi v28.16b, #0 66 movi v29.16b, #0 67 movi v30.16b, #0 68 movi v31.16b, #0 69 mov x9, x1 70 mov x10, x5 71 add x11, x10, x4 72 add x12, x11, x4 73 add x13, x12, x4 74 cbz x6, 1f 75 mov x14, x6 76 0: ld1 {v0.16b, v1.16b, v2.16b, v3.16b}, [x9], #64 77 ld1 {v4.16b, v5.16b, v6.16b, v7.16b}, [x10], #64 78 ld1 {v16.16b, v17.16b, v18.16b, v19.16b}, [x11], #64 79 sdot v24.4s, v4.16b, v0.16b 80 sdot v28.4s, v5.16b, v1.16b 81 sdot v25.4s, v16.16b, v0.16b 82 sdot v29.4s, v17.16b, v1.16b 83 ld1 {v20.16b, v21.16b, v22.16b, v23.16b}, [x12], #64 84 sdot v24.4s, v6.16b, v2.16b 85 sdot v28.4s, v7.16b, v3.16b 86 sdot v25.4s, v18.16b, v2.16b 87 sdot v29.4s, v19.16b, v3.16b 88 ld1 {v4.16b, v5.16b, v6.16b, v7.16b}, [x13], #64 89 sdot v26.4s, v20.16b, v0.16b 90 sdot v30.4s, v21.16b, v1.16b 91 sdot v27.4s, v4.16b, v0.16b 92 sdot v31.4s, v5.16b, v1.16b 93 sdot v26.4s, v22.16b, v2.16b 94 sdot v30.4s, v23.16b, v3.16b 95 sdot v27.4s, v6.16b, v2.16b 96 sdot v31.4s, v7.16b, v3.16b 97 subs x14, x14, #1 98 b.ne 0b 99 1: cbz x7, 3f 100 mov x14, x7 101 2: ldr q0, [x9], #16 102 ldr q4, [x10], #16 103 ldr q5, [x11], #16 104 ldr q6, [x12], #16 105 ldr q7, [x13], #16 106 sdot v24.4s, v4.16b, v0.16b 107 sdot v25.4s, v5.16b, v0.16b 108 sdot v26.4s, v6.16b, v0.16b 109 sdot v27.4s, v7.16b, v0.16b 110 subs x14, x14, #1 111 b.ne 2b 112 3: cbz x8, 4f 113 sub x9, x9, x15 // now x + n - 16 114 sub x10, x10, x15 // now row + n - 16 115 sub x11, x11, x15 116 sub x12, x12, x15 117 sub x13, x13, x15 118 ldr q0, [x9] 119 ldr q1, [x16] 120 and v0.16b, v0.16b, v1.16b // zero the lanes already counted 121 ldr q4, [x10] 122 ldr q5, [x11] 123 ldr q6, [x12] 124 ldr q7, [x13] 125 sdot v28.4s, v4.16b, v0.16b 126 sdot v29.4s, v5.16b, v0.16b 127 sdot v30.4s, v6.16b, v0.16b 128 sdot v31.4s, v7.16b, v0.16b 129 4: add v24.4s, v24.4s, v28.4s 130 add v25.4s, v25.4s, v29.4s 131 add v26.4s, v26.4s, v30.4s 132 add v27.4s, v27.4s, v31.4s 133 addp v24.4s, v24.4s, v25.4s // [r0 r0 r1 r1] 134 addp v26.4s, v26.4s, v27.4s // [r2 r2 r3 r3] 135 addp v24.4s, v24.4s, v26.4s // [r0 r1 r2 r3] 136 str q24, [x2], #16 137 add x5, x5, x4, lsl #2 138 sub x3, x3, #4 139 b .Lsd_blk4 140 141 .Lsd_rows1: 142 cbz x3, .Lsd_ok 143 .Lsd_row1: 144 movi v24.16b, #0 145 movi v25.16b, #0 146 movi v26.16b, #0 147 movi v27.16b, #0 148 mov x9, x1 149 mov x10, x5 150 cbz x6, 1f 151 mov x14, x6 152 0: ld1 {v0.16b, v1.16b, v2.16b, v3.16b}, [x9], #64 153 ld1 {v4.16b, v5.16b, v6.16b, v7.16b}, [x10], #64 154 sdot v24.4s, v4.16b, v0.16b 155 sdot v25.4s, v5.16b, v1.16b 156 sdot v26.4s, v6.16b, v2.16b 157 sdot v27.4s, v7.16b, v3.16b 158 subs x14, x14, #1 159 b.ne 0b 160 1: cbz x7, 3f 161 mov x14, x7 162 2: ldr q0, [x9], #16 163 ldr q4, [x10], #16 164 sdot v24.4s, v4.16b, v0.16b 165 subs x14, x14, #1 166 b.ne 2b 167 3: cbz x8, 4f 168 sub x9, x9, x15 169 sub x10, x10, x15 170 ldr q0, [x9] 171 ldr q1, [x16] 172 and v0.16b, v0.16b, v1.16b 173 ldr q4, [x10] 174 sdot v25.4s, v4.16b, v0.16b 175 4: add v24.4s, v24.4s, v25.4s 176 add v26.4s, v26.4s, v27.4s 177 add v24.4s, v24.4s, v26.4s 178 addv s24, v24.4s 179 str s24, [x2], #4 180 add x5, x5, x4 181 subs x3, x3, #1 182 b.ne .Lsd_row1 183 .Lsd_ok: 184 mov w0, #0 185 ret 186 187 /* n < 16: scalar; rows are contiguous so x5 walks all of W. */ 188 .Lsd_scalar: 189 0: mov w6, #0 190 mov x9, x1 191 mov x14, x4 192 1: ldrsb w10, [x5], #1 193 ldrsb w11, [x9], #1 194 madd w6, w10, w11, w6 195 subs x14, x14, #1 196 b.ne 1b 197 str w6, [x2], #4 198 subs x3, x3, #1 199 b.ne 0b 200 mov w0, #0 201 ret 202 203 .Lsd_err: 204 mov w0, #-1 205 ret 206 .size omx_asm_sdot_run, . - omx_asm_sdot_run 207 208 .section .rodata 209 .p2align 4 210 .Lsd_mask: 211 .fill 16, 1, 0x00 212 .fill 16, 1, 0xff 213 214 .section .note.GNU-stack, "", %progbits