Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.
; ============================================================================
; matmul_sse2.asm — Q8_0 matmul kernels for the x86-64 BASELINE (SSE2 only)
;
; Drop-in replacement for matmul.asm when built with SSE2=1 (FAST_Q8=1 only).
; No AVX, no AVX2, no FMA, no F16C, no SSSE3/SSE4: runs on any x86-64 CPU
; (Core 2, Athlon 64 / Phenom, Atom, ...).
;
; Same weight layout, same exported symbols and the SAME arithmetic structure as
; the AVX2 kernel in matmul.asm; the only numeric difference is that FMA becomes
; mul+add (two roundings):
; * the activation vector is quantized to Q8_0 once (quantize_x_q8_0),
; * every 32-weight block gives 8 int32 lane sums (4 products per lane),
; converted to float, scaled by (w_scale * x_scale) and accumulated into
; 8 lanes (kept as xmm lo/hi pairs), then reduced in the same order.
;
; SSE2 has no pmaddubsw/pabsb/psignb, so the int8 dot uses pmaddwd on int16:
; * quantize_x_q8_0 stores x as int16, split per 16 elements into an EVEN-
; element vector and an ODD-element vector (q8_xw, 64 bytes per block);
; * the weight bytes are split the same way with shifts:
; even = (w << 8) >> 8 (arith) odd = w >> 8 (arith)
; pmaddwd(even_w, even_x) + pmaddwd(odd_w, odd_x) = four products per lane,
; exactly the grouping (elements 4j..4j+3) of the AVX kernels.
; * fp16 block scales are converted with integer SSE2 ops (no F16C); the
; conversion is exact for every 16-bit input (checked against vcvtph2ps).
;
; Functions (identical to matmul.asm):
; matmul_set_fp16, quantize_x_q8_0, dequant_row_q8_0, matmul_q8_0_range,
; dot_f32_avx2, saxpy_f32_avx2
; ============================================================================
bits 64
default rel
%ifndef SSE2
%define SSE2 0
%endif
%ifndef FAST_Q8
%define FAST_Q8 1
%endif
%if !SSE2
%error "matmul_sse2.asm is only for SSE2=1 builds"
%endif
%if !FAST_Q8
%error "SSE2=1 supports only FAST_Q8=1 (INT8 x INT8 matmul path)"
%endif
%ifndef Q8_PREFETCH
%define Q8_PREFETCH prefetcht0
%endif
%define Q8_MAX_K 16384
section .text
global matmul_set_fp16
global quantize_x_q8_0
global dequant_row_q8_0
global matmul_q8_0_range
global dot_f32_avx2
global saxpy_f32_avx2
; ----------------------------------------------------------------------------
; H2F4 v, t1, t2
; v = 4 x fp16 values, each zero-extended in a 32-bit lane -> 4 x float32.
; Exact IEEE half->single for every input (normal, subnormal, zero, +-inf, NaN):
; shifting the 15 magnitude bits into float position gives exponent e (half bias)
; in a float with bias 127; multiplying by 2^112 re-biases it, and for half
; subnormals (float denormals after the shift) the same multiply normalizes
; them exactly (needs MXCSR.DAZ = 0, the kernel's default). Half inf/NaN
; (exponent 31) get their float exponent forced to 255 afterwards.
; Clobbers t1, t2.
; ----------------------------------------------------------------------------
%macro H2F4 3
movdqa %2, %1
pand %2, [rel h2f_8000]
pxor %1, %2 ; v = magnitude
pslld %2, 16 ; t1 = sign bit in place
movdqa %3, %1
pand %3, [rel h2f_7c00]
pcmpeqd %3, [rel h2f_7c00] ; t2 = (exp == 31) [inf / nan]
pand %3, [rel h2f_7f800000] ; t2 = exponent 255 for those lanes
pslld %1, 13
mulps %1, [rel h2f_2p112] ; rebias (and normalize subnormals)
por %1, %3
por %1, %2
%endmacro
; HSUM4 v, t : v.lane0 = (v0+v1)+(v2+v3) (same adds, same order as 2x haddps)
%macro HSUM4 2
movaps %2, %1
shufps %2, %2, 0xDD ; (v1, v3, v1, v3)
shufps %1, %1, 0x88 ; (v0, v2, v0, v2)
addps %1, %2 ; (v0+v1, v2+v3, ...)
movaps %2, %1
shufps %2, %2, 0x55 ; lane0 = v2+v3
addss %1, %2
%endmacro
; ----------------------------------------------------------------------------
; void matmul_set_fp16(uint64 is_fp16)
; ----------------------------------------------------------------------------
matmul_set_fp16:
xor eax, eax
test rdi, rdi
setnz al
mov [rel g_fp16_scales], eax
ret
; ----------------------------------------------------------------------------
; void dequant_row_q8_0(const void *row_ptr, float *out, uint64 K)
; rdi = row_ptr, rsi = out, rdx = K (multiple of 32)
; ----------------------------------------------------------------------------
%macro DEQ16 1 ; %1 = byte offset of the 16-int8 chunk (0 / 16)
movdqu xmm0, [rdi + r9 + %1]
movdqa xmm1, xmm0
punpcklbw xmm0, xmm0
psraw xmm0, 8 ; elements 0..7 as int16
punpckhbw xmm1, xmm1
psraw xmm1, 8 ; elements 8..15 as int16
movdqa xmm2, xmm0
punpcklwd xmm0, xmm0
psrad xmm0, 16 ; elements 0..3 as int32
punpckhwd xmm2, xmm2
psrad xmm2, 16 ; elements 4..7
movdqa xmm3, xmm1
punpcklwd xmm1, xmm1
psrad xmm1, 16 ; elements 8..11
punpckhwd xmm3, xmm3
psrad xmm3, 16 ; elements 12..15
cvtdq2ps xmm0, xmm0
cvtdq2ps xmm2, xmm2
cvtdq2ps xmm1, xmm1
cvtdq2ps xmm3, xmm3
mulps xmm0, xmm4
mulps xmm2, xmm4
mulps xmm1, xmm4
mulps xmm3, xmm4
movups [rsi + r10 + (%1)*4], xmm0
movups [rsi + r10 + (%1)*4 + 16], xmm2
movups [rsi + r10 + (%1)*4 + 32], xmm1
movups [rsi + r10 + (%1)*4 + 48], xmm3
%endmacro
dequant_row_q8_0:
lea r8, [rdi + rdx] ; scales_ptr = row_ptr + K
mov rcx, rdx
shr rcx, 5 ; nb = K / 32
mov r11d, [rel g_fp16_scales]
xor eax, eax ; b = 0
.lp:
cmp rax, rcx
jae .done
test r11d, r11d
jnz .sc_f16
movss xmm4, [r8 + rax*4] ; v1 fp32 scale[b]
jmp .sc_ok
.sc_f16:
movzx r10d, word [r8 + rax*2] ; v2 fp16 scale[b]
movd xmm4, r10d
H2F4 xmm4, xmm5, xmm6
.sc_ok:
shufps xmm4, xmm4, 0
mov r9, rax
shl r9, 5 ; b * 32 (q offset)
mov r10, rax
shl r10, 7 ; b * 128 (out offset)
DEQ16 0
DEQ16 16
inc rax
jmp .lp
.done:
ret
; ----------------------------------------------------------------------------
; void quantize_x_q8_0(const float *x, uint64 K)
; rdi = x (f32), rsi = K (multiple of 32, <= Q8_MAX_K)
; q8_xw[b*64 ..] : int16, per block of 32 elements
; [even elems 0..15 (8 x i16)][odd elems 0..15][even 16..31][odd 16..31]
; q8_xs[b] : float32 scale = max_abs / 127.0f
; ----------------------------------------------------------------------------
quantize_x_q8_0:
mov rcx, rsi
shr rcx, 5 ; nb = K / 32
lea r9, [rel q8_xw]
lea r10, [rel q8_xs]
movaps xmm12, [rel q8_abs_mask4]
movss xmm13, [rel q8_inv_127]
movss xmm14, [rel q8_f127]
xorps xmm15, xmm15
xor eax, eax ; b = 0
xor r8d, r8d ; x byte offset (b * 128)
xor edx, edx ; xw byte offset (b * 64)
.qlp:
cmp rax, rcx
jae .qdone
movups xmm0, [rdi + r8]
movups xmm1, [rdi + r8 + 16]
movups xmm2, [rdi + r8 + 32]
movups xmm3, [rdi + r8 + 48]
movups xmm4, [rdi + r8 + 64]
movups xmm5, [rdi + r8 + 80]
movups xmm6, [rdi + r8 + 96]
movups xmm7, [rdi + r8 + 112]
; max |x| over the 32 floats
movaps xmm8, xmm0
andps xmm8, xmm12
movaps xmm9, xmm1
andps xmm9, xmm12
maxps xmm8, xmm9
movaps xmm9, xmm2
andps xmm9, xmm12
movaps xmm10, xmm3
andps xmm10, xmm12
maxps xmm9, xmm10
maxps xmm8, xmm9
movaps xmm9, xmm4
andps xmm9, xmm12
movaps xmm10, xmm5
andps xmm10, xmm12
maxps xmm9, xmm10
movaps xmm10, xmm6
andps xmm10, xmm12
movaps xmm11, xmm7
andps xmm11, xmm12
maxps xmm10, xmm11
maxps xmm9, xmm10
maxps xmm8, xmm9
movaps xmm9, xmm8
shufps xmm9, xmm9, 0x0E
maxps xmm8, xmm9
movaps xmm9, xmm8
shufps xmm9, xmm9, 0x01
maxss xmm8, xmm9 ; xmm8 = max_abs
movaps xmm9, xmm8
mulss xmm9, xmm13 ; scale_x = max_abs / 127.0f
movss [r10 + rax*4], xmm9
xorps xmm10, xmm10 ; inv_scale = 0.0f if max_abs == 0
ucomiss xmm8, xmm15
jbe .qinv_ok
movaps xmm10, xmm14
divss xmm10, xmm8 ; inv_scale = 127.0f / max_abs
.qinv_ok:
shufps xmm10, xmm10, 0
mulps xmm0, xmm10
mulps xmm1, xmm10
mulps xmm2, xmm10
mulps xmm3, xmm10
mulps xmm4, xmm10
mulps xmm5, xmm10
mulps xmm6, xmm10
mulps xmm7, xmm10
cvtps2dq xmm0, xmm0
cvtps2dq xmm1, xmm1
cvtps2dq xmm2, xmm2
cvtps2dq xmm3, xmm3
cvtps2dq xmm4, xmm4
cvtps2dq xmm5, xmm5
cvtps2dq xmm6, xmm6
cvtps2dq xmm7, xmm7
; split into even / odd elements, pack to int16
movaps xmm8, xmm0
shufps xmm8, xmm1, 0x88 ; e0 e2 e4 e6
movaps xmm9, xmm2
shufps xmm9, xmm3, 0x88 ; e8 e10 e12 e14
shufps xmm0, xmm1, 0xDD ; e1 e3 e5 e7
shufps xmm2, xmm3, 0xDD ; e9 e11 e13 e15
packssdw xmm8, xmm9 ; even 0..15
packssdw xmm0, xmm2 ; odd 0..15
movaps xmm10, xmm4
shufps xmm10, xmm5, 0x88
movaps xmm11, xmm6
shufps xmm11, xmm7, 0x88
shufps xmm4, xmm5, 0xDD
shufps xmm6, xmm7, 0xDD
packssdw xmm10, xmm11 ; even 16..31
packssdw xmm4, xmm6 ; odd 16..31
; clamp to [-127, 127] (same final values as packsswb + pmaxsb(-127))
movdqa xmm1, [rel q8_m127w]
pmaxsw xmm8, xmm1
pmaxsw xmm0, xmm1
pmaxsw xmm10, xmm1
pmaxsw xmm4, xmm1
movdqa xmm1, [rel q8_p127w]
pminsw xmm8, xmm1
pminsw xmm0, xmm1
pminsw xmm10, xmm1
pminsw xmm4, xmm1
movdqa [r9 + rdx], xmm8
movdqa [r9 + rdx + 16], xmm0
movdqa [r9 + rdx + 32], xmm10
movdqa [r9 + rdx + 48], xmm4
add r8, 128
add rdx, 64
inc rax
jmp .qlp
.qdone:
ret
; ----------------------------------------------------------------------------
; DOT32 lo, hi, off : lo/hi = 4 x float (int8x32 dot, 4 products per lane)
; lo = elements 0..15 of the block, hi = elements 16..31
; w = [rax + rbx + off], x = [r11 + rbx*2 + off*2] scratch: xmm8, xmm9
; ----------------------------------------------------------------------------
%macro DOT32 3
movdqu xmm8, [rax + rbx + %3]
movdqa xmm9, xmm8
psllw xmm8, 8
psraw xmm8, 8 ; even weights as int16
psraw xmm9, 8 ; odd weights as int16
pmaddwd xmm8, [r11 + rbx*2 + (%3)*2]
pmaddwd xmm9, [r11 + rbx*2 + (%3)*2 + 16]
paddd xmm8, xmm9
cvtdq2ps %1, xmm8
movdqu xmm8, [rax + rbx + %3 + 16]
movdqa xmm9, xmm8
psllw xmm8, 8
psraw xmm8, 8
psraw xmm9, 8
pmaddwd xmm8, [r11 + rbx*2 + (%3)*2 + 32]
pmaddwd xmm9, [r11 + rbx*2 + (%3)*2 + 48]
paddd xmm8, xmm9
cvtdq2ps %2, xmm8
%endmacro
; BLK accLo, accHi, off, pshufd-imm : acc += dot(block) * scale[lane]
; xmm12 = 4 block scales (w_scale * x_scale), xmm10/xmm11 = scratch, xmm13 = broadcast
%macro BLK 4
DOT32 xmm10, xmm11, %3
pshufd xmm13, xmm12, %4
mulps xmm10, xmm13
mulps xmm11, xmm13
addps %1, xmm10
addps %2, xmm11
%endmacro
; 4 blocks (128 weights = 2 cache lines); ymm0..3 of the AVX kernel == xmm0..7 here
%macro INT8_1ROW_4BLK 0
Q8_PREFETCH [r8 + rbx]
Q8_PREFETCH [r8 + rbx + 64]
BLK xmm0, xmm1, 0, 0x00
BLK xmm2, xmm3, 32, 0x55
BLK xmm4, xmm5, 64, 0xAA
BLK xmm6, xmm7, 96, 0xFF
%endmacro
; row reduction: same add order as the AVX kernel, result -> [rdx]
%macro ROW_REDUCE 0
addps xmm0, xmm2
addps xmm1, xmm3
addps xmm4, xmm6
addps xmm5, xmm7
addps xmm0, xmm4
addps xmm1, xmm5
HSUM4 xmm0, xmm8
HSUM4 xmm1, xmm9
addss xmm0, xmm1
movss [rdx], xmm0
%endmacro
%macro ZERO_ACC 0
xorps xmm0, xmm0
xorps xmm1, xmm1
xorps xmm2, xmm2
xorps xmm3, xmm3
xorps xmm4, xmm4
xorps xmm5, xmm5
xorps xmm6, xmm6
xorps xmm7, xmm7
%endmacro
; ----------------------------------------------------------------------------
; void matmul_q8_0_range(const void *W, const float *x, float *out,
; uint64 K, uint64 r0, uint64 r1)
; rdi = W, rsi = x (unused: x was quantized by quantize_x_q8_0), rdx = out,
; rcx = K, r8 = r0, r9 = r1
; ----------------------------------------------------------------------------
matmul_q8_0_range:
push rbx
push rbp
push r12
push r13
push r14
push r15
mov rsi, r9
sub rsi, r8 ; rsi = rows_left = r1 - r0
jle .exit
lea rdx, [rdx + r8 * 4] ; rdx = out_ptr = out + r0 * 4
lea r11, [rel q8_xw]
lea rbp, [rel q8_xs]
mov r15, rcx ; r15 = K
mov r13, rcx
and r13, -128 ; r13 = K_128 = K & ~127
cmp dword [rel g_fp16_scales], 0
jne .fast_fp16
; ========================================================================
; v1 FP32 scales: row_stride = K + (K >> 3)
; ========================================================================
mov r14, rcx
shr r14, 3
add r14, rcx ; r14 = row_stride
mov rax, r8
imul rax, r14
add rax, rdi ; rax = w_cur = W + r0 * row_stride
align 16
.row_f32:
lea r12, [rax + r14] ; r12 = w_next
lea r8, [rax + r15] ; r8 = s_cur (fp32)
ZERO_ACC
xor ebx, ebx ; b_q: 0, 128, ...
xor r9d, r9d ; b_s: 0, 16, ...
test r13, r13
jz .tail_f32
align 16
.blk4_f32:
movups xmm12, [r8 + r9] ; 4x fp32 scale_w
mulps xmm12, [rbp + r9] ; * 4x fp32 scale_x
INT8_1ROW_4BLK
add rbx, 128
add r9, 16
cmp rbx, r13
jb .blk4_f32
.tail_f32:
cmp rbx, r15
jae .red_f32
DOT32 xmm10, xmm11, 0
movss xmm13, [r8 + r9]
mulss xmm13, [rbp + r9]
shufps xmm13, xmm13, 0
mulps xmm10, xmm13
mulps xmm11, xmm13
addps xmm0, xmm10
addps xmm1, xmm11
add rbx, 32
add r9, 4
jmp .tail_f32
.red_f32:
ROW_REDUCE
add rdx, 4
mov rax, r12
dec rsi
jnz .row_f32
jmp .exit
; ========================================================================
; v2 FP16 scales: row_stride = K + (K >> 4)
; ========================================================================
.fast_fp16:
mov r14, rcx
shr r14, 4
add r14, rcx ; r14 = row_stride
mov rax, r8
imul rax, r14
add rax, rdi ; rax = w_cur = W + r0 * row_stride
align 16
.row_f16:
lea r12, [rax + r14] ; r12 = w_next
lea r8, [rax + r15] ; r8 = s_cur (fp16)
ZERO_ACC
xor ebx, ebx
xor r9d, r9d ; b_s: 0, 8, ... (fp16 offset; xs = r9*2)
test r13, r13
jz .tail_f16
align 16
.blk4_f16:
movq xmm12, [r8 + r9] ; 4x fp16 scale_w
pxor xmm13, xmm13
punpcklwd xmm12, xmm13 ; -> 4 x 32-bit lanes
H2F4 xmm12, xmm8, xmm9
mulps xmm12, [rbp + r9*2] ; * 4x fp32 scale_x
INT8_1ROW_4BLK
add rbx, 128
add r9, 8
cmp rbx, r13
jb .blk4_f16
.tail_f16:
cmp rbx, r15
jae .red_f16
DOT32 xmm10, xmm11, 0
movzx ecx, word [r8 + r9]
movd xmm13, ecx
H2F4 xmm13, xmm8, xmm9
mulss xmm13, [rbp + r9*2]
shufps xmm13, xmm13, 0
mulps xmm10, xmm13
mulps xmm11, xmm13
addps xmm0, xmm10
addps xmm1, xmm11
add rbx, 32
add r9, 2
jmp .tail_f16
.red_f16:
ROW_REDUCE
add rdx, 4
mov rax, r12
dec rsi
jnz .row_f16
.exit:
pop r15
pop r14
pop r13
pop r12
pop rbp
pop rbx
ret
; ----------------------------------------------------------------------------
; float dot_f32_avx2(const float *a, const float *b, uint64 n) -> xmm0
; rdi = a, rsi = b, rdx = n (multiple of 32). Same 8-lane x 4-accumulator
; structure and reduction order as the AVX kernel (xmm lo/hi pairs).
; ----------------------------------------------------------------------------
%macro DOTSTEP 2 ; %1 = byte offset, %2 = accumulator
movups xmm8, [rdi + rax + %1]
movups xmm9, [rsi + rax + %1]
mulps xmm8, xmm9
addps %2, xmm8
%endmacro
dot_f32_avx2:
ZERO_ACC
shl rdx, 2
xor eax, eax
.dlp:
cmp rax, rdx
jae .dred
DOTSTEP 0, xmm0
DOTSTEP 16, xmm1
DOTSTEP 32, xmm2
DOTSTEP 48, xmm3
DOTSTEP 64, xmm4
DOTSTEP 80, xmm5
DOTSTEP 96, xmm6
DOTSTEP 112, xmm7
add rax, 128
jmp .dlp
.dred:
addps xmm0, xmm2 ; ymm0 += ymm1
addps xmm1, xmm3
addps xmm4, xmm6 ; ymm2 += ymm3
addps xmm5, xmm7
addps xmm0, xmm4 ; ymm0 += ymm2
addps xmm1, xmm5
addps xmm0, xmm1 ; lo + hi
HSUM4 xmm0, xmm2
ret
; ----------------------------------------------------------------------------
; void saxpy_f32_avx2(float *dst, const float *src, uint64 n, float alpha)
; rdi = dst, rsi = src, rdx = n (multiple of 32), xmm0 = alpha
; dst[i] += alpha * src[i]
; ----------------------------------------------------------------------------
%macro SAXSTEP 1
movups xmm1, [rdi + rax + %1]
movups xmm2, [rsi + rax + %1]
mulps xmm2, xmm0
addps xmm1, xmm2
movups [rdi + rax + %1], xmm1
%endmacro
saxpy_f32_avx2:
shufps xmm0, xmm0, 0
shl rdx, 2
xor eax, eax
.slp:
cmp rax, rdx
jae .sdone
SAXSTEP 0
SAXSTEP 16
SAXSTEP 32
SAXSTEP 48
SAXSTEP 64
SAXSTEP 80
SAXSTEP 96
SAXSTEP 112
add rax, 128
jmp .slp
.sdone:
ret
section .rodata
align 16
q8_abs_mask4: times 4 dd 0x7FFFFFFF
q8_m127w: times 8 dw -127
q8_p127w: times 8 dw 127
h2f_8000: times 4 dd 0x00008000
h2f_7c00: times 4 dd 0x00007C00
h2f_7f800000: times 4 dd 0x7F800000
h2f_2p112: times 4 dd 0x77800000 ; 2^112
q8_f127: dd 127.0
q8_inv_127: dd 0.007874015748031496 ; 1.0f / 127.0f
section .bss
alignb 64
g_fp16_scales: resd 1
alignb 64
q8_xw: resb Q8_MAX_K * 2
alignb 64
q8_xs: resb (Q8_MAX_K / 32) * 4