Skip to content

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.

3.5 KB
Raw Blame
; ============================================================================
; isa.inc — ISA selection helpers (AVX2+FMA+F16C default, AVX1 fallback)
;
;   AVX1=0 (default): unchanged code paths, AVX2 + FMA + F16C.
;   AVX1=1          : AVX (256-bit float) + VEX-128 SSSE3/SSE4.1 integer only.
;                     No FMA, no F16C, no AVX2. Targets Sandy Bridge / Ivy Bridge
;                     (Xeon E5-26xx v1/v2). Tested on Sandy Bridge only.
;
; FMA helpers: with AVX1=1 a fused a*b+c becomes a separate multiply and add
; (two roundings instead of one). 231-forms need an explicit scratch register.
; ============================================================================

%ifndef ISA_INC
%define ISA_INC

%ifndef AVX1
%define AVX1 0
%endif

; dst = dst + a * b            (scratch = tmp, may alias a)
%macro VFMADD231PS 4
%if AVX1
    vmulps  %4, %2, %3
    vaddps  %1, %1, %4
%else
    vfmadd231ps %1, %2, %3
%endif
%endmacro

; dst = dst - a * b            (scratch = tmp, may alias a)
%macro VFNMADD231PS 4
%if AVX1
    vmulps  %4, %2, %3
    vsubps  %1, %1, %4
%else
    vfnmadd231ps %1, %2, %3
%endif
%endmacro

; dst = dst * a + b            (no scratch needed)
%macro VFMADD213PS 3
%if AVX1
    vmulps  %1, %1, %2
    vaddps  %1, %1, %3
%else
    vfmadd213ps %1, %2, %3
%endif
%endmacro

; Scalar double variants
%macro VFMADD231SD 4
%if AVX1
    vmulsd  %4, %2, %3
    vaddsd  %1, %1, %4
%else
    vfmadd231sd %1, %2, %3
%endif
%endmacro

%macro VFNMADD231SD 4
%if AVX1
    vmulsd  %4, %2, %3
    vsubsd  %1, %1, %4
%else
    vfnmadd231sd %1, %2, %3
%endif
%endmacro

%macro VFMADD213SD 3
%if AVX1
    vmulsd  %1, %1, %2
    vaddsd  %1, %1, %3
%else
    vfmadd213sd %1, %2, %3
%endif
%endmacro

; Software fp16 -> fp32 for 4 halves in the low 64 bits of an xmm register
; (replacement for F16C vcvtph2ps xmm, xmm). Exact for zero, subnormal and
; normal halves (shift into fp32 position, then scale by 2^112, which also
; turns fp16 subnormals into the right normal fp32 value). Inf/NaN are not
; produced; Q8_0 block scales never hold them.
;   %1 = xmm in/out, %2 = xmm scratch
; Needs 16-byte constants f16_sign_mask (4x 0x00008000) and f16_magic
; (4x 0x77800000 = 2^112) in the including file.
%macro CVTPH2PS_SW 2
    vpmovzxwd %1, %1                    ; 4x u16 -> 4x u32 (SSE4.1)
    vpand   %2, %1, [rel f16_sign_mask] ; sign bit
    vpxor   %1, %1, %2                  ; |h|
    vpslld  %1, %1, 13                  ; exponent + mantissa -> fp32 slots
    vpslld  %2, %2, 16                  ; sign -> bit 31
    vmulps  %1, %1, [rel f16_magic]     ; rebias exponent by 2^112
    vorps   %1, %1, %2
%endmacro

; fp16 -> fp32 of 4 halves in the low 64 bits of %1 (in place).
; F16C when AVX1=0, exact software path when AVX1=1.  %2 = xmm scratch.
%macro CVTPH2PS4 2
%if AVX1
    CVTPH2PS_SW %1, %2
%else
    vcvtph2ps %1, %1
%endif
%endmacro

; Broadcast lane 0 of an xmm register to all 8 lanes of its own ymm register
; (AVX2 has vbroadcastss ymm, xmm; AVX1 only allows a memory source).
;   %1 = ymmN, %2 = xmmN (same register number)
%macro VBROADCASTSS_R 2
%if AVX1
    vshufps %2, %2, %2, 0
    vinsertf128 %1, %1, %2, 1
%else
    vbroadcastss %1, %2
%endif
%endmacro

; Sign-extend 8 int8 at [%3] to 8 int32 in ymm %1 (its low half is xmm %2).
; AVX2: one vpmovsxbd ymm, qword. AVX1: two 128-bit vpmovsxbd and a lane
; insert through the xmm scratch %4.   %3 = address expression, no brackets.
%macro PMOVSXBD8 4
%if AVX1
    vpmovsxbd %2, dword [%3]
    vpmovsxbd %4, dword [%3 + 4]
    vinsertf128 %1, %1, %4, 1
%else
    vpmovsxbd %1, qword [%3]
%endif
%endmacro

%endif