Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,370 @@
|
||||
#! @file evex.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how an executable works 64 bytes of lanes with AVX-512, where
|
||||
#! `lanes_needs` said the processor running it has it: AVX-512F, BW, DQ, and VL,
|
||||
#! with the system keeping the registers' upper 384 bits and the mask
|
||||
#! registers. Such a vector is held in its slot, never a register, as only code
|
||||
#! that asked the processor may touch `zmm` registers. Each operation loads its
|
||||
#! operands from their slots into `zmm16` and those after it, works them with
|
||||
#! EVEX's encodings, and stores its result in its slot; comparing lanes gives a
|
||||
#! mask in `k1`, which becomes the mask's lanes, and choosing lanes or taking a
|
||||
#! mask's bits takes them back into `k1`. VEX names no register past `zmm15`,
|
||||
#! so neither `vzeroupper` nor SSE's instructions meet those, and code working
|
||||
#! 64 bytes clears nothing after.
|
||||
#!
|
||||
#! An instruction is a word as `binary::amd64` writes vector instructions: its
|
||||
#! opcode, its map above it, its mandatory prefix above that, and `W` at bit 24.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
# Moving 64 bytes, and 16 into every quarter of a register, or into one.
|
||||
const z_movdqu64_load: u32 = 0x102016F;
|
||||
const z_movdqu64_store: u32 = 0x102017F;
|
||||
const z_broadcasti32x4: u32 = 0x01025A;
|
||||
const z_inserti32x4: u32 = 0x010338;
|
||||
# Integer arithmetic, by width: 16, 32, and 64 bits.
|
||||
const z_paddw: u32 = 0x0101FD;
|
||||
const z_paddd: u32 = 0x0101FE;
|
||||
const z_paddq: u32 = 0x10101D4;
|
||||
const z_psubw: u32 = 0x0101F9;
|
||||
const z_psubd: u32 = 0x0101FA;
|
||||
const z_psubq: u32 = 0x10101FB;
|
||||
const z_pmullw: u32 = 0x0101D5;
|
||||
const z_pmulld: u32 = 0x010240;
|
||||
const z_pmullq: u32 = 0x1010240;
|
||||
# Bitwise operations of 32-bit lanes, and of 64 with `W`; and AVX-512's of
|
||||
# three operands by a table of eight bits.
|
||||
const z_pandd: u32 = 0x0101DB;
|
||||
const z_pord: u32 = 0x0101EB;
|
||||
const z_pxord: u32 = 0x0101EF;
|
||||
const z_pternlogd: u32 = 0x010325;
|
||||
# The least, greatest, and absolute values of integers, by sign and width; `W`
|
||||
# makes the 32-bit ones' 64-bit.
|
||||
const z_pminsw: u32 = 0x0101EA;
|
||||
const z_pmaxsw: u32 = 0x0101EE;
|
||||
const z_pminuw: u32 = 0x01023A;
|
||||
const z_pmaxuw: u32 = 0x01023E;
|
||||
const z_pminsd: u32 = 0x010239;
|
||||
const z_pmaxsd: u32 = 0x01023D;
|
||||
const z_pminud: u32 = 0x01023B;
|
||||
const z_pmaxud: u32 = 0x01023F;
|
||||
const z_pabsw: u32 = 0x01021D;
|
||||
const z_pabsd: u32 = 0x01021E;
|
||||
const z_pabsq: u32 = 0x101021F;
|
||||
# Rounding reals with how they round, and comparing lanes into a mask, by a
|
||||
# predicate.
|
||||
const z_rndscaleps: u32 = 0x010308;
|
||||
const z_rndscalepd: u32 = 0x1010309;
|
||||
const z_pcmpd: u32 = 0x01031F;
|
||||
const z_pcmpud: u32 = 0x01031E;
|
||||
const z_pcmpw: u32 = 0x101033F;
|
||||
const z_pcmpuw: u32 = 0x101033E;
|
||||
const z_cmpps: u32 = 0x0001C2;
|
||||
# A mask's lanes from `k1`, and `k1` from a mask's lanes, of 16, 32, and 64-bit
|
||||
# lanes; and lanes chosen by `k1`.
|
||||
const z_pmovm2w: u32 = 0x1020228;
|
||||
const z_pmovm2d: u32 = 0x020238;
|
||||
const z_pmovm2q: u32 = 0x1020238;
|
||||
const z_pmovw2m: u32 = 0x1020229;
|
||||
const z_pmovd2m: u32 = 0x020239;
|
||||
const z_pmovq2m: u32 = 0x1020239;
|
||||
const z_pblendmw: u32 = 0x1010266;
|
||||
const z_pblendmd: u32 = 0x010264;
|
||||
const z_pblendmq: u32 = 0x1010264;
|
||||
const z_blendmps: u32 = 0x010265;
|
||||
const z_blendmpd: u32 = 0x1010265;
|
||||
# Splatting a general register's lane, or a real's low lane.
|
||||
const z_pbroadcastw_r: u32 = 0x01027B;
|
||||
const z_pbroadcastd_r: u32 = 0x01027C;
|
||||
const z_pbroadcastq_r: u32 = 0x101027C;
|
||||
const z_broadcastss: u32 = 0x010218;
|
||||
const z_broadcastsd: u32 = 0x1010219;
|
||||
# A mask register's bits into a general register, written with VEX: 16 bits,
|
||||
# and 32 with `F2`.
|
||||
const k_movw: u32 = 0x000193;
|
||||
const k_movd: u32 = 0x030193;
|
||||
|
||||
# The registers 64 bytes are worked in.
|
||||
const z16: u32 = 16;
|
||||
const z17: u32 = 17;
|
||||
const z18: u32 = 18;
|
||||
|
||||
#! Writes EVEX's four bytes and an instruction's opcode, on 64 bytes: `r` the
|
||||
#! register in ModRM's reg, `v` the one EVEX names, or 0 where the instruction
|
||||
#! names none, `b` the base or register in ModRM's rm, and `k` the mask
|
||||
#! register writing is masked by, or 0. A vector register is any of the 32, and
|
||||
#! a general one, or the base of `memory`, any of the 16.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param r ModRM's reg.
|
||||
#! @param v The register EVEX names.
|
||||
#! @param b ModRM's rm.
|
||||
#! @param k The mask register.
|
||||
#! @param memory Whether `b` is the base of memory, which names no index.
|
||||
def selector::evex_opcode(&mut this, op: u32, r: u32, v: u32, b: u32, k: u32, memory: bool): void {
|
||||
val pp := (op >> 16) & 3;
|
||||
val map := (op >> 8) & 3;
|
||||
val w: u32 = (op & vec_wide) != 0 ? 1 : 0;
|
||||
# R, X, B, and R' inverted: the fourth and fifth bits of `r` and `b`, where X
|
||||
# is the fifth of a register `b`.
|
||||
val x: u32 = memory ? 1 : ((b >> 4) & 1) ^ 1;
|
||||
this.b.text.byte(0x62);
|
||||
this.b.text.byte(((((r >> 3) & 1) ^ 1) << 7 | x << 6 | (((b >> 3) & 1) ^ 1) << 5 | (((r >> 4) & 1) ^ 1) << 4 | map) as u8);
|
||||
this.b.text.byte((w << 7 | ((~v & 15) << 3) | 1 << 2 | pp) as u8);
|
||||
# Of 512 bits, V' inverted, and the mask.
|
||||
this.b.text.byte((2 << 5 | (((v >> 4) & 1) ^ 1) << 3 | (k & 7)) as u8);
|
||||
this.b.text.byte((op & 0xFF) as u8);
|
||||
}
|
||||
|
||||
#! An instruction of registers on 64 bytes: `op dst, v, src`.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param dst ModRM's reg.
|
||||
#! @param v The register EVEX names, or 0.
|
||||
#! @param src ModRM's rm.
|
||||
#! @param k The mask register, or 0.
|
||||
def selector::evex(&mut this, op: u32, dst: u32, v: u32, src: u32, k: u32): void {
|
||||
this.evex_opcode(op, dst, v, src, k, false);
|
||||
this.b.text.byte((0xC0 | ((dst & 7) << 3) | (src & 7)) as u8);
|
||||
}
|
||||
|
||||
#! An instruction of registers on 64 bytes, with a byte after it.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param dst ModRM's reg.
|
||||
#! @param v The register EVEX names, or 0.
|
||||
#! @param src ModRM's rm.
|
||||
#! @param imm The byte.
|
||||
def selector::evex_imm(&mut this, op: u32, dst: u32, v: u32, src: u32, imm: u8): void {
|
||||
this.evex(op, dst, v, src, 0);
|
||||
this.b.text.byte(imm);
|
||||
}
|
||||
|
||||
#! An instruction of a register and memory on 64 bytes, as a load or store: the
|
||||
#! memory a base register and a displacement, written whole, which EVEX does
|
||||
#! not scale.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param r The register.
|
||||
#! @param m The memory.
|
||||
def selector::evex_mem(&mut this, op: u32, r: u32, m: mem): void {
|
||||
val base := m.base as u32;
|
||||
this.evex_opcode(op, r, 0, base, 0, true);
|
||||
this.b.text.byte((0x80 | ((r & 7) << 3) | (base & 7)) as u8);
|
||||
if (base & 7) == 4 {
|
||||
this.b.text.byte(0x24);
|
||||
}
|
||||
this.b.text.u32(m.disp as u64 & 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
#! An instruction of a register and memory on 64 bytes, with a byte after it.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param r The register, which EVEX also names.
|
||||
#! @param m The memory.
|
||||
#! @param imm The byte.
|
||||
def selector::evex_mem_imm(&mut this, op: u32, r: u32, m: mem, imm: u8): void {
|
||||
val base := m.base as u32;
|
||||
this.evex_opcode(op, r, r, base, 0, true);
|
||||
this.b.text.byte((0x80 | ((r & 7) << 3) | (base & 7)) as u8);
|
||||
if (base & 7) == 4 {
|
||||
this.b.text.byte(0x24);
|
||||
}
|
||||
this.b.text.u32(m.disp as u64 & 0xFFFFFFFF);
|
||||
this.b.text.byte(imm);
|
||||
}
|
||||
|
||||
#! Loads an operand of the instruction, 64 bytes in its slot, into a register,
|
||||
#! a quarter at a time, as `read_halves` reads 32: a value is copied into its
|
||||
#! slot 16 bytes at a time, and one load reading four stores would wait for
|
||||
#! them all to reach the cache.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param k Which operand.
|
||||
#! @param z The register.
|
||||
def selector::fetch_wide(&mut this, index: u32, k: u32, z: u32): void {
|
||||
val held := this.read(this.body.arg(index, k), index);
|
||||
if held.kind == place::none {
|
||||
this.evex(z_pxord, z, z, z, 0);
|
||||
return;
|
||||
}
|
||||
val from := this.memory_of(&held);
|
||||
this.evex_mem(z_broadcasti32x4, z, from);
|
||||
for q in 1..4u {
|
||||
var part := from;
|
||||
part.disp += (16 * q) as i32;
|
||||
this.evex_mem_imm(z_inserti32x4, z, part, q as u8);
|
||||
}
|
||||
}
|
||||
|
||||
#! Splats a word across a register of 64 bytes, from a general register.
|
||||
#! @param this The selector.
|
||||
#! @param z The register.
|
||||
#! @param word The word.
|
||||
def selector::splat_wide(&mut this, z: u32, word: u64): void {
|
||||
this.b.text.mov_imm(reg::rax, word);
|
||||
this.evex(z_pbroadcastq_r, z, 0, reg::rax as u32, 0);
|
||||
}
|
||||
|
||||
#! `lanes` of 64 bytes, as the file says.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param held The instruction.
|
||||
def selector::lanes_wide(&mut this, index: u32, held: &inst): void {
|
||||
val operation := held.aux;
|
||||
val lane := (held.imm & 0xFF) as u32;
|
||||
val result := held.result;
|
||||
val real := lane >= lane_f32;
|
||||
val bits := lane_bits(lane);
|
||||
val doubles: u32 = lane == lane_f64 ? 0x10000 | vec_wide : 0;
|
||||
val q: u32 = bits == 64 ? vec_wide : 0;
|
||||
val to_mask := by_width(lane, 0, z_pmovw2m, z_pmovd2m, z_pmovq2m);
|
||||
if operation == lanes_bits {
|
||||
# `vpmov?2m` clears the bits of `k1` past the lanes.
|
||||
this.fetch_wide(index, 0, z16);
|
||||
this.evex(to_mask, 1, 0, z16, 0);
|
||||
val into := this.target(result, reg::rax);
|
||||
this.b.text.vex(bits == 16 ? k_movd : k_movw, false, into as u32, 0, 1);
|
||||
this.put(result, into);
|
||||
return;
|
||||
}
|
||||
if operation == lanes_splat {
|
||||
val value := this.body.arg(index, 0);
|
||||
if lane == lane_f32 {
|
||||
this.evex(z_broadcastss, z16, 0, this.single_reg(value, index, 0) as u32, 0);
|
||||
} else if lane == lane_f64 {
|
||||
this.load_real_into(value, index, 0);
|
||||
this.evex(z_broadcastsd, z16, 0, 0, 0);
|
||||
} else {
|
||||
val r := this.word_reg(value, index, reg::rax);
|
||||
this.evex(by_width(lane, 0, z_pbroadcastw_r, z_pbroadcastd_r, z_pbroadcastq_r), z16, 0, r as u32, 0);
|
||||
}
|
||||
} else if operation == lanes_select {
|
||||
# `k1` set where the mask is; each lane then the second operand's there, and
|
||||
# the third's elsewhere.
|
||||
this.fetch_wide(index, 0, z16);
|
||||
this.fetch_wide(index, 1, z17);
|
||||
this.fetch_wide(index, 2, z18);
|
||||
this.evex(to_mask, 1, 0, z16, 0);
|
||||
val blend := real ? (lane == lane_f64 ? z_blendmpd : z_blendmps) : by_width(lane, 0, z_pblendmw, z_pblendmd, z_pblendmq);
|
||||
this.evex(blend, z16, z18, z17, 1);
|
||||
} else if operation == lanes_fma {
|
||||
# `zmm16 = zmm17 * zmm18 + zmm16`, as `lanes.sakura`'s is.
|
||||
this.fetch_wide(index, 2, z16);
|
||||
this.fetch_wide(index, 0, z17);
|
||||
this.fetch_wide(index, 1, z18);
|
||||
this.evex(vec_vfmadd231ps | (lane == lane_f64 ? vec_wide : 0), z16, z17, z18, 0);
|
||||
} else if operation == lanes_shift_left || operation == lanes_shift_right {
|
||||
# The count in the low word of `zmm17`, the same for every lane; a signed
|
||||
# shift right of 64-bit lanes is AVX-512's own.
|
||||
val count := this.word_reg(this.body.arg(index, 1), index, reg::rax);
|
||||
this.evex(z_pbroadcastq_r, z17, 0, count as u32, 0);
|
||||
this.fetch_wide(index, 0, z16);
|
||||
val op := operation == lanes_shift_left ? by_width(lane, 0, vec_psllw, vec_pslld, vec_psllq)
|
||||
: lane_signed(lane) ? by_width(lane, 0, vec_psraw, vec_psrad, vec_psrad) : by_width(lane, 0, vec_psrlw, vec_psrld, vec_psrlq);
|
||||
this.evex(op | q, z16, z16, z17, 0);
|
||||
} else if operation >= lanes_equal && operation <= lanes_greater_equal {
|
||||
this.compare_wide(index, operation, lane);
|
||||
} else {
|
||||
this.arithmetic_wide(index, operation, lane, doubles, q);
|
||||
}
|
||||
val made := this.p.made_at(result);
|
||||
if made.kind != place::none {
|
||||
this.evex_mem(z_movdqu64_store, z16, this.memory_of(&made));
|
||||
}
|
||||
}
|
||||
|
||||
#! Compares 64 bytes of lanes into a mask: into `k1` by a predicate, of reals
|
||||
#! as SSE's are, the greater ones with their operands swapped, then its lanes.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param operation The comparison.
|
||||
#! @param lane The type of lane.
|
||||
def selector::compare_wide(&mut this, index: u32, operation: u32, lane: u32): void {
|
||||
val bits := lane_bits(lane);
|
||||
this.fetch_wide(index, 0, z16);
|
||||
this.fetch_wide(index, 1, z17);
|
||||
if lane >= lane_f32 {
|
||||
val swapped := operation == lanes_greater || operation == lanes_greater_equal;
|
||||
val predicate: u8 = operation == lanes_equal ? 0 : operation == lanes_not_equal ? 4
|
||||
: operation == lanes_less || operation == lanes_greater ? 1 : 2;
|
||||
val op := z_cmpps | (lane == lane_f64 ? 0x10000 | vec_wide : 0);
|
||||
if swapped {
|
||||
this.evex_imm(op, 1, z17, z16, predicate);
|
||||
} else {
|
||||
this.evex_imm(op, 1, z16, z17, predicate);
|
||||
}
|
||||
} else {
|
||||
# 0 equal, 1 less, 2 at most, 4 not equal, 5 at least, 6 greater.
|
||||
val predicate: u8 = operation == lanes_equal ? 0 : operation == lanes_not_equal ? 4 : operation == lanes_less ? 1
|
||||
: operation == lanes_less_equal ? 2 : operation == lanes_greater ? 6 : 5;
|
||||
val signed := lane_signed(lane);
|
||||
val op := bits == 16 ? (signed ? z_pcmpw : z_pcmpuw) : (signed ? z_pcmpd : z_pcmpud) | (bits == 64 ? vec_wide : 0);
|
||||
this.evex_imm(op, 1, z16, z17, predicate);
|
||||
}
|
||||
this.evex(by_width(lane, 0, z_pmovm2w, z_pmovm2d, z_pmovm2q), z16, 0, 1, 0);
|
||||
}
|
||||
|
||||
#! The arithmetic, bitwise operations, and rounding of 64 bytes of lanes, into
|
||||
#! `zmm16`.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param operation The operation.
|
||||
#! @param lane The type of lane.
|
||||
#! @param doubles What makes an instruction of `f32`s one of `f64`s.
|
||||
#! @param q What makes one of 32-bit integers one of 64.
|
||||
def selector::arithmetic_wide(&mut this, index: u32, operation: u32, lane: u32, doubles: u32, q: u32): void {
|
||||
val real := lane >= lane_f32;
|
||||
this.fetch_wide(index, 0, z16);
|
||||
if operation == lanes_negate {
|
||||
if real {
|
||||
this.splat_wide(z17, sign_bits(lane));
|
||||
this.evex(z_pxord | vec_wide, z16, z16, z17, 0);
|
||||
} else {
|
||||
this.evex(z_pxord, z17, z17, z17, 0);
|
||||
this.evex(by_width(lane, 0, z_psubw, z_psubd, z_psubq), z16, z17, z16, 0);
|
||||
}
|
||||
} else if operation == lanes_not {
|
||||
this.evex_imm(z_pternlogd, z16, z16, z16, 0x0F);
|
||||
} else if operation == lanes_abs {
|
||||
if real {
|
||||
this.splat_wide(z17, ~sign_bits(lane));
|
||||
this.evex(z_pandd | vec_wide, z16, z16, z17, 0);
|
||||
} else if lane_signed(lane) {
|
||||
this.evex(by_width(lane, 0, z_pabsw, z_pabsd, z_pabsq), z16, 0, z16, 0);
|
||||
}
|
||||
} else if operation == lanes_sqrt {
|
||||
this.evex(vec_sqrtps | doubles, z16, 0, z16, 0);
|
||||
} else if operation >= lanes_floor && operation <= lanes_round_even {
|
||||
# Toward -∞, +∞, zero, or the nearest, even on a tie, as a whole number,
|
||||
# with no exception for what is not exact.
|
||||
val mode: u8 = operation == lanes_floor ? 9 : operation == lanes_ceil ? 10 : operation == lanes_trunc ? 11 : 8;
|
||||
this.evex_imm(lane == lane_f64 ? z_rndscalepd : z_rndscaleps, z16, 0, z16, mode);
|
||||
} else {
|
||||
this.fetch_wide(index, 1, z17);
|
||||
var op: u32 = 0;
|
||||
if operation == lanes_add {
|
||||
op = real ? vec_addps | doubles : by_width(lane, 0, z_paddw, z_paddd, z_paddq);
|
||||
} else if operation == lanes_sub {
|
||||
op = real ? vec_subps | doubles : by_width(lane, 0, z_psubw, z_psubd, z_psubq);
|
||||
} else if operation == lanes_mul {
|
||||
op = real ? vec_mulps | doubles : by_width(lane, 0, z_pmullw, z_pmulld, z_pmullq);
|
||||
} else if operation == lanes_div {
|
||||
op = vec_divps | doubles;
|
||||
} else if operation == lanes_and || operation == lanes_or || operation == lanes_xor {
|
||||
op = (operation == lanes_and ? z_pandd : operation == lanes_or ? z_pord : z_pxord) | vec_wide;
|
||||
} else {
|
||||
val least := operation == lanes_min;
|
||||
val signed := lane_signed(lane);
|
||||
op = lane_bits(lane) == 16 ? (signed ? (least ? z_pminsw : z_pmaxsw) : (least ? z_pminuw : z_pmaxuw))
|
||||
: (signed ? (least ? z_pminsd : z_pmaxsd) : (least ? z_pminud : z_pmaxud)) | q;
|
||||
}
|
||||
this.evex(op, z16, z16, z17, 0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user