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.
; ============================================================================
; sse2_compat.inc — VEX (AVX) scalar/xmm mnemonics -> plain SSE2 (x86-64 baseline)
;
; Included only when SSE2=1 (math.asm, kernel.asm, runtime.asm, transformer.asm).
; NASM lets a multi-line macro replace an instruction name,
; so existing scalar and 128-bit code assembles unchanged but emits legacy-SSE
; encodings. The packed 256-bit (ymm) kernels are NOT handled here: they have
; hand-written SSE2 versions under `%if SSE2` in matmul_sse2.asm / math.asm /
; transformer.asm.
;
; Rules enforced at assembly time:
; * 3-operand ops lower to `movaps d,a ; op d,b` (or `op d,a` when the op is
; commutative and d==b). A non-commutative op with d==b!=a is an error
; (it would need a scratch register) — fix the call site.
; * Packed logic ops (xorps/andps/...) with a MEMORY operand need 16-byte
; aligned memory in legacy SSE; keep such constants `align 16`.
; Everything here is SSE2 or older (no SSE3 haddps, no SSE4.1 roundsd/blendv).
; ============================================================================
; ---- lowering helpers -------------------------------------------------------
%macro __V3C 4 ; commutative: %1=sse op, %2=d, %3=a, %4=b
%ifidn %2,%3
%1 %2, %4
%elifidn %2,%4
%1 %2, %3
%else
movaps %2, %3
%1 %2, %4
%endif
%endmacro
%macro __V3N 4 ; non-commutative (and max/min: operand order matters)
%ifidn %2,%3
%1 %2, %4
%elifidn %2,%4
%error "sse2_compat: d==b!=a for a non-commutative op (needs a scratch register)"
%else
movaps %2, %3
%1 %2, %4
%endif
%endmacro
; ---- scalar float / double arithmetic ---------------------------------------
%macro vaddss 3
__V3C addss, %1, %2, %3
%endmacro
%macro vaddsd 3
__V3C addsd, %1, %2, %3
%endmacro
%macro vmulss 3
__V3C mulss, %1, %2, %3
%endmacro
%macro vmulsd 3
__V3C mulsd, %1, %2, %3
%endmacro
%macro vsubss 3
__V3N subss, %1, %2, %3
%endmacro
%macro vsubsd 3
__V3N subsd, %1, %2, %3
%endmacro
%macro vdivss 3
__V3N divss, %1, %2, %3
%endmacro
%macro vdivsd 3
__V3N divsd, %1, %2, %3
%endmacro
%macro vmaxss 3
__V3N maxss, %1, %2, %3
%endmacro
%macro vsqrtss 3 ; d = sqrt(b); upper lanes are don't-care in this code base
sqrtss %1, %3
%endmacro
; ---- logic on registers/constants ------------------------------------------
%macro vxorps 3
__V3C xorps, %1, %2, %3
%endmacro
%macro vxorpd 3
__V3C xorpd, %1, %2, %3
%endmacro
; ---- moves -------------------------------------------------------------------
%macro vmovss 2
movss %1, %2
%endmacro
%macro vmovss 3 ; d = (a upper, b low)
%ifidn %1,%2
movss %1, %3
%else
movaps %1, %2
movss %1, %3
%endif
%endmacro
%macro vmovsd 2
movsd %1, %2
%endmacro
%macro vmovsd 3
%ifidn %1,%2
movsd %1, %3
%else
movaps %1, %2
movsd %1, %3
%endif
%endmacro
%macro vmovapd 2
movapd %1, %2
%endmacro
%macro vmovq 2
movq %1, %2
%endmacro
; ---- scalar FMA3 (math.asm's FP64 sin/cos/log/exp) -> mul+add ------------------
; Two roundings instead of one: results differ from FMA hardware in the last bit,
; well inside the documented accuracy of those routines.
%macro vfmadd213sd 3 ; d = d*a + b
mulsd %1, %2
addsd %1, %3
%endmacro
%macro vfmadd231sd 3 ; d += a*b (a is clobbered)
mulsd %2, %3
addsd %1, %2
%endmacro
%macro vfnmadd231sd 3 ; d -= a*b (b is clobbered, must be a register)
mulsd %3, %2
subsd %1, %3
%endmacro
; ---- compare / convert --------------------------------------------------------
%macro vucomiss 2
ucomiss %1, %2
%endmacro
%macro vucomisd 2
ucomisd %1, %2
%endmacro
%macro vcvtsi2sd 3 ; d = (double)int ; a is ignored (upper lanes unused)
cvtsi2sd %1, %3
%endmacro
%macro vcvtsi2ss 3
cvtsi2ss %1, %3
%endmacro
%macro vcvtss2sd 3
cvtss2sd %1, %3
%endmacro
%macro vcvtsd2ss 3
cvtsd2ss %1, %3
%endmacro
%macro vcvtsd2si 2 ; rounds per MXCSR (nearest-even), like the VEX form
cvtsd2si %1, %2
%endmacro
%macro vcvttsd2si 2
cvttsd2si %1, %2
%endmacro