#! @file vector.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the encodings of vector instructions: SSE's, on 16 bytes, written #! with a mandatory prefix and an opcode map before the opcode, and AVX's, of #! the same opcodes, written with a VEX prefix, which names a third register and #! may work on 32 bytes. Asking the processor which of them it has, `cpuid` and #! `xgetbv`, and `vzeroupper`, which ends working on 32 bytes, are here too. #! #! An instruction is a word: its opcode in the low byte, its map above it (1 for #! `0F`, 2 for `0F 38`, and 3 for `0F 3A`), its mandatory prefix above that (0 #! for none, 1 for `66`, 2 for `F3`, and 3 for `F2`), as VEX numbers both, and #! VEX's `W` at bit 24. import std; module binary::amd64; #! Moving 16 or 32 bytes. pub const vec_movdqu_load: u32 = 0x02016F; #! `movdqu x, m`. pub const vec_movdqu_store: u32 = 0x02017F; #! `movdqu m, x`. pub const vec_movdqa: u32 = 0x01016F; #! `movdqa x, x`. pub const vec_movd_load: u32 = 0x01016E; #! `movd x, m32`. #! Integer arithmetic, by the width of each lane. pub const vec_paddb: u32 = 0x0101FC; pub const vec_paddw: u32 = 0x0101FD; pub const vec_paddd: u32 = 0x0101FE; pub const vec_paddq: u32 = 0x0101D4; pub const vec_psubb: u32 = 0x0101F8; pub const vec_psubw: u32 = 0x0101F9; pub const vec_psubd: u32 = 0x0101FA; pub const vec_psubq: u32 = 0x0101FB; pub const vec_pmullw: u32 = 0x0101D5; pub const vec_pmulld: u32 = 0x010240; #! SSE4.1. pub const vec_pand: u32 = 0x0101DB; pub const vec_pandn: u32 = 0x0101DF; pub const vec_por: u32 = 0x0101EB; pub const vec_pxor: u32 = 0x0101EF; pub const vec_pcmpeqb: u32 = 0x010174; pub const vec_pcmpeqw: u32 = 0x010175; pub const vec_pcmpeqd: u32 = 0x010176; pub const vec_pcmpeqq: u32 = 0x010229; #! SSE4.1. pub const vec_pcmpgtb: u32 = 0x010164; pub const vec_pcmpgtw: u32 = 0x010165; pub const vec_pcmpgtd: u32 = 0x010166; pub const vec_pcmpgtq: u32 = 0x010237; #! SSE4.2. pub const vec_pminub: u32 = 0x0101DA; pub const vec_pmaxub: u32 = 0x0101DE; pub const vec_pminsw: u32 = 0x0101EA; pub const vec_pmaxsw: u32 = 0x0101EE; pub const vec_pminsb: u32 = 0x010238; #! SSE4.1, as the rest of the least and greatest. pub const vec_pmaxsb: u32 = 0x01023C; pub const vec_pminuw: u32 = 0x01023A; pub const vec_pmaxuw: u32 = 0x01023E; pub const vec_pminsd: u32 = 0x010239; pub const vec_pmaxsd: u32 = 0x01023D; pub const vec_pminud: u32 = 0x01023B; pub const vec_pmaxud: u32 = 0x01023F; pub const vec_pabsb: u32 = 0x01021C; #! SSSE3, as the rest of the absolute values. pub const vec_pabsw: u32 = 0x01021D; pub const vec_pabsd: u32 = 0x01021E; pub const vec_psllw: u32 = 0x0101F1; #! Shifts by the count in the low word of a register. pub const vec_pslld: u32 = 0x0101F2; pub const vec_psllq: u32 = 0x0101F3; pub const vec_psrlw: u32 = 0x0101D1; pub const vec_psrld: u32 = 0x0101D2; pub const vec_psrlq: u32 = 0x0101D3; pub const vec_psraw: u32 = 0x0101E1; pub const vec_psrad: u32 = 0x0101E2; pub const vec_packsswb: u32 = 0x010163; pub const vec_pmovmskb: u32 = 0x0101D7; #! A general register's bits of each byte's top bit. pub const vec_punpcklqdq: u32 = 0x01016C; #! Real arithmetic: of `f32`s, and of `f64`s with the prefix `66`, which these #! add. pub const vec_addps: u32 = 0x000158; pub const vec_subps: u32 = 0x00015C; pub const vec_mulps: u32 = 0x000159; pub const vec_divps: u32 = 0x00015E; pub const vec_minps: u32 = 0x00015D; pub const vec_maxps: u32 = 0x00015F; pub const vec_sqrtps: u32 = 0x000151; pub const vec_andps: u32 = 0x000154; pub const vec_andnps: u32 = 0x000155; pub const vec_orps: u32 = 0x000156; pub const vec_xorps: u32 = 0x000157; pub const vec_cmpps: u32 = 0x0001C2; #! With a predicate. pub const vec_movmskps: u32 = 0x000150; #! A general register's bits of each lane's sign. pub const vec_roundps: u32 = 0x010308; #! SSE4.1, with how it rounds; `roundpd` is `09`. pub const vec_roundpd: u32 = 0x010309; pub const vec_doubles: u32 = 0x010000; #! The prefix making an `f32` operation of `f64`s. #! AVX's alone. pub const vec_vfmadd231ps: u32 = 0x0102B8; #! FMA3: `x = a * b + x`; `pd` is the same with `W`. pub const vec_vinsertf128: u32 = 0x010318; #! The upper half of a register from another's lower, or memory. pub const vec_wide: u32 = 0x1000000; #! VEX's `W`. # The byte of each mandatory prefix. const pp_bytes: u8[4] = [0, 0x66, 0xF3, 0xF2]; # Writes a mandatory prefix, REX where the registers need one, the escape and # map of an instruction, and its opcode. def assembly::packed_opcode(&mut this, op: u32, r: u32, index: reg, base: reg): void { val pp := (op >> 16) & 3; if pp != 0 { this.byte(pp_bytes[pp as u64]); } this.rex((op & vec_wide) != 0, r, index, base, false); this.byte(0x0F); val map := (op >> 8) & 3; if map == 2 { this.byte(0x38); } else if map == 3 { this.byte(0x3A); } this.byte((op & 0xFF) as u8); } #! An SSE instruction of two registers, `op dst, src`, on 16 bytes. A general #! register is named by its number, as `pmovmskb` writes one. Where the code is #! written with VEX, it is VEX's form, which names `dst` again as the operand #! read first, but for an instruction of one operand. #! @param this The code. #! @param op The instruction. #! @param dst The register in ModRM's reg. #! @param src The register in its rm. pub def assembly::packed(&mut this, op: u32, dst: u32, src: u32): void { if this.avx { this.vex(op, false, dst, vec_unary(op) ? 0 : dst, src); return; } this.packed_opcode(op, dst, reg::none, (src & 15) as reg); this.byte(modrm_reg(dst, src)); } #! An SSE instruction of a register and memory, `op r, m`, as a load or store, #! with VEX where the code is written so. #! @param this The code. #! @param op The instruction. #! @param r The register. #! @param m The memory. pub def assembly::packed_mem(&mut this, op: u32, r: u32, m: mem): void { if this.avx { this.vex_mem(op, false, r, m); return; } this.packed_opcode(op, r, m.index, m.base); this.modrm_mem(r, m); } #! An SSE instruction of two registers and a byte, `op dst, src, imm`. #! @param this The code. #! @param op The instruction. #! @param dst The register in ModRM's reg. #! @param src The register in its rm. #! @param imm The byte. pub def assembly::packed_imm(&mut this, op: u32, dst: u32, src: u32, imm: u8): void { this.packed(op, dst, src); this.byte(imm); } # Whether an instruction reads only the operand in ModRM's rm, as a move, a # square root, rounding, an absolute value, or gathering a mask's bits, so VEX # names no other register. def vec_unary(op: u32): bool { val code := op & 0xFF; val map := (op >> 8) & 3; if map == 1 { return code == 0x6F || code == 0x7F || code == 0x6E || code == 0x51 || code == 0xD7 || code == 0x50 || code == 0x28; } if map == 2 { return code == 0x1C || code == 0x1D || code == 0x1E; } return code == 0x08 || code == 0x09; } # Writes a VEX prefix and an instruction's opcode: the two-byte form where the # map is `0F`, `W` is clear, and neither the index nor the base needs REX's bits, # and the three-byte form otherwise. `v` is the register VEX names, or 0 where # the instruction names none, which VEX writes as all ones. def assembly::vex_opcode(&mut this, op: u32, long: bool, r: u32, v: u32, x: u32, b: u32): void { val pp := (op >> 16) & 3; val map := (op >> 8) & 3; val w: u32 = (op & vec_wide) != 0 ? 1 : 0; val length: u32 = long ? 4 : 0; val last := ((~v & 15) << 3) | length | pp; val not_r := ((r >> 3) & 1) ^ 1; if map == 1 && w == 0 && (x & 8) == 0 && (b & 8) == 0 { this.byte(0xC5); this.byte(((not_r << 7) | last) as u8); } else { this.byte(0xC4); this.byte(((not_r << 7) | ((((x >> 3) & 1) ^ 1) << 6) | ((((b >> 3) & 1) ^ 1) << 5) | map) as u8); this.byte(((w << 7) | last) as u8); } this.byte((op & 0xFF) as u8); } #! An AVX instruction of three registers, `op dst, v, src`, on 32 bytes where #! `long` and 16 otherwise. One of two names no `v`, which is given as 0. #! @param this The code. #! @param op The instruction. #! @param long Whether it works on 32 bytes. #! @param dst The register in ModRM's reg. #! @param v The register VEX names. #! @param src The register in ModRM's rm. pub def assembly::vex(&mut this, op: u32, long: bool, dst: u32, v: u32, src: u32): void { this.vex_opcode(op, long, dst, v, 0, src); this.byte(modrm_reg(dst, src)); } #! An AVX instruction of three registers and a byte, `op dst, v, src, imm`. #! @param this The code. #! @param op The instruction. #! @param long Whether it works on 32 bytes. #! @param dst The register in ModRM's reg. #! @param v The register VEX names, or 0. #! @param src The register in ModRM's rm. #! @param imm The byte. pub def assembly::vex_imm(&mut this, op: u32, long: bool, dst: u32, v: u32, src: u32, imm: u8): void { this.vex(op, long, dst, v, src); this.byte(imm); } #! An AVX instruction of a register and memory, `op r, m`, as a load or store. #! @param this The code. #! @param op The instruction. #! @param long Whether it works on 32 bytes. #! @param r The register. #! @param m The memory. pub def assembly::vex_mem(&mut this, op: u32, long: bool, r: u32, m: mem): void { val x: u32 = m.index != reg::none ? m.index as u32 : 0; val b: u32 = m.base != reg::none ? m.base as u32 : 0; this.vex_opcode(op, long, r, 0, x, b); this.modrm_mem(r, m); } #! An AVX instruction of a register, memory, and a byte, naming a third #! register too, `op r, v, m, imm`, as `vinsertf128`. #! @param this The code. #! @param op The instruction. #! @param long Whether it works on 32 bytes. #! @param r The register in ModRM's reg. #! @param v The register VEX names, or 0. #! @param m The memory. #! @param imm The byte. pub def assembly::vex_mem_imm(&mut this, op: u32, long: bool, r: u32, v: u32, m: mem, imm: u8): void { val x: u32 = m.index != reg::none ? m.index as u32 : 0; val b: u32 = m.base != reg::none ? m.base as u32 : 0; this.vex_opcode(op, long, r, v, x, b); this.modrm_mem(r, m); this.byte(imm); } #! `cpuid`: what the processor is, as the leaf in `eax` asks, into `eax`, #! `ebx`, `ecx`, and `edx`. #! @param this The code. pub def assembly::cpuid(&mut this): void { this.byte(0x0F); this.byte(0xA2); } #! `xgetbv`: the extended state the system keeps, as `ecx` names it, into #! `edx:eax`. #! @param this The code. pub def assembly::xgetbv(&mut this): void { this.byte(0x0F); this.byte(0x01); this.byte(0xD0); } #! `vzeroupper`: clears the upper halves of the vector registers, which the code #! after working on 32 bytes asks, so SSE's instructions after it run at speed. #! @param this The code. pub def assembly::vzeroupper(&mut this): void { this.byte(0xC5); this.byte(0xF8); this.byte(0x77); }