#! @file reshape.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the operations on lanes giving lanes of another type or size, as #! `lanes_needs` numbers them past `lanes_convert`: converting each lane, #! widening half a vector's lanes and narrowing two vectors' into one, taking #! 32 bytes apart into halves and joining two halves, and gathering lanes from #! memory. Each is worked in `xmm0` and those after it, as `lanes.sakura`'s #! operations are, the result put where it goes from `xmm0`. import sakura::compiler; import binary::amd64; module sakura::native; # The instructions, written as `binary::amd64` writes vector instructions. # Widening integers, each the signed form and the unsigned 16 on: bytes to 16, # 32, and 64 bits, 16 to 32 and 64, and 32 to 64. const vec_pmovsxbw: u32 = 0x010220; const vec_pmovsxbd: u32 = 0x010221; const vec_pmovsxbq: u32 = 0x010222; const vec_pmovsxwd: u32 = 0x010223; const vec_pmovsxwq: u32 = 0x010224; const vec_pmovsxdq: u32 = 0x010225; const vec_movzx_from_sx: u32 = 0x10; # Packing two registers' lanes into one of half the width, saturating. const vec_packuswb: u32 = 0x010167; const vec_packssdw: u32 = 0x01016B; const vec_packusdw: u32 = 0x01022B; # Converting: `i32` to `f32`, `f32` to `i32` toward zero, `f32` to `f64` and # back, and `i32` to `f64`. const vec_cvtdq2ps: u32 = 0x00015B; const vec_cvttps2dq: u32 = 0x02015B; const vec_cvtps2pd: u32 = 0x00015A; const vec_cvtpd2ps: u32 = 0x01015A; const vec_cvtdq2pd: u32 = 0x0201E6; const vec_cvttpd2dq: u32 = 0x0101E6; # A register's high 8 bytes into another's low, and 64-bit lanes put in an # order across 32 bytes, AVX2's. const vec_movhlps: u32 = 0x000112; const vec_vpermq: u32 = 0x1010300; #! An operation giving lanes of another type or size, as the file says. #! @param this The selector. #! @param index The instruction. #! @param held The instruction. #! @param lane The type of lane it is given. #! @param long Whether what it is given is 32 bytes. def selector::reshape_lanes(&mut this, index: u32, held: &inst, lane: u32, long: bool): void { val operation := held.aux & 0xFF; val target := lanes_target(held.aux); val result := held.result; val out_long := this.body.type_of(result) == vt::v256; if operation == lanes_low { this.fetch_lanes(index, 0, 0, true); } else if operation == lanes_high { this.fetch_lanes(index, 0, 0, true); this.b.text.vex_imm(vec_vextractf128, true, 0, 0, 0, 1); } else if operation == lanes_join { this.fetch_lanes(index, 0, 0, false); this.fetch_lanes(index, 1, 1, false); this.b.text.vex_imm(vec_vinsertf128, true, 0, 0, 1, 1); } else if operation == lanes_widen || operation == lanes_widen_high { this.widen_lanes(index, operation == lanes_widen_high, lane, target, long); } else if operation == lanes_narrow { this.narrow_lanes(index, lane, target, long); } else if operation == lanes_gather { this.gather_lanes(index, lane, long || out_long); } else { this.convert_lanes(index, lane, target, long, out_long); } this.put_lanes(result, 0, out_long); this.settle_upper(index, long || out_long); } # An instruction reading only its second register, as a conversion: with VEX's # form naming no other where the code is written with VEX. def selector::vunary(&mut this, op: u32, long: bool, x: u8, y: u8): void { if long || this.b.text.avx { this.b.text.vex(op, long, x as u32, 0, y as u32); } else { this.b.text.packed(op, x as u32, y as u32); } } # The instruction widening integer lanes of `from` bits to `into` bits, by the # sign or not. def widening(from: u64, into: u64, signed: bool): u32 { val sx := from == 8 ? (into == 16 ? vec_pmovsxbw : into == 32 ? vec_pmovsxbd : vec_pmovsxbq) : from == 16 ? (into == 32 ? vec_pmovsxwd : vec_pmovsxwq) : vec_pmovsxdq; return signed ? sx : sx + vec_movzx_from_sx; } #! Half the lanes of `xmm0` widened to twice their width into `xmm0`: of 16 #! bytes, the first half, or the second moved down; of 32, either half, into #! all 32. Integers keep their value by their sign, as `as` does, and `f32`s #! become `f64`s. #! @param this The selector. #! @param index The instruction. #! @param high Whether it is the second half. #! @param lane The type of lane. #! @param target The type of lane it gives. #! @param long Whether it is 32 bytes. def selector::widen_lanes(&mut this, index: u32, high: bool, lane: u32, target: u32, long: bool): void { this.fetch_lanes(index, 0, 0, long); if long { if high { this.b.text.vex_imm(vec_vextractf128, true, 0, 0, 0, 1); } } else if high { this.b.text.packed(vec_movhlps, 0, 0); } val op := lane >= lane_f32 ? vec_cvtps2pd : widening(lane_bits(lane), lane_bits(target), lane_signed(lane)); this.vunary(op, long, 0, 0); } #! Two vectors' lanes narrowed to half their width into `xmm0`, the first's #! first: integers packed, saturating, those of unsigned lanes held at the #! type's greatest first, and `f64`s rounded to `f32`s. 32 bytes are packed #! within each half, then their 8-byte quarters put back in order. #! @param this The selector. #! @param index The instruction. #! @param lane The type of lane. #! @param target The type of lane it gives. #! @param long Whether each is 32 bytes. def selector::narrow_lanes(&mut this, index: u32, lane: u32, target: u32, long: bool): void { this.fetch_lanes(index, 0, 0, long); this.fetch_lanes(index, 1, 1, long); if lane == lane_f64 { this.vunary(vec_cvtpd2ps, long, 0, 0); this.vunary(vec_cvtpd2ps, long, 1, 1); if long { this.b.text.vex_imm(vec_vinsertf128, true, 0, 0, 1, 1); } else { this.b.text.packed(vec_movlhps, 0, 1); } return; } val bits := lane_bits(lane); val signed := lane_signed(target); if !lane_signed(lane) { # Held at the target's greatest, which packing then keeps. val most: u64 = bits == 16 ? (signed ? 0x007F007F007F007F : 0x00FF00FF00FF00FF) : (signed ? 0x00007FFF00007FFF : 0x0000FFFF0000FFFF); this.splat_bits(2, most, long); this.vop(bits == 16 ? vec_pminuw : vec_pminud, long, 0, 2); this.vop(bits == 16 ? vec_pminuw : vec_pminud, long, 1, 2); } val op := bits == 16 ? (lane_signed(lane) && signed ? vec_packsswb : vec_packuswb) : (lane_signed(lane) && signed ? vec_packssdw : vec_packusdw); this.vop(op, long, 0, 1); if long { this.b.text.vex_imm(vec_vpermq, true, 0, 0, 0, 0xD8); } } #! Each lane converted to another type of as many lanes into `xmm0`, as `as` #! converts it: integers widened as `widen_lanes` widens them, or narrowed, by #! the low bytes of each kept, from 32 bytes to 16; `i32`s to reals by the #! instruction, and `u32`s as their high and low 16 bits, each exact, added #! once; smaller integers to `f32` widened to `i32` first; `f32`s and `f64`s #! to 32-bit integers toward zero, which `simd`'s `convert` gives only lanes #! within `i32`'s range, where the instruction is exact, and an unsigned one #! held at 0 below zero; and `f32`s to `f64`s and back. #! @param this The selector. #! @param index The instruction. #! @param lane The type of lane. #! @param target The type of lane it gives. #! @param long Whether what it is given is 32 bytes. #! @param out_long Whether what it gives is 32 bytes. def selector::convert_lanes(&mut this, index: u32, lane: u32, target: u32, long: bool, out_long: bool): void { val bits := lane_bits(lane); val into := lane_bits(target); val real := lane >= lane_f32; val into_real := target >= lane_f32; this.fetch_lanes(index, 0, 0, long); if !real && !into_real { if into > bits { this.vunary(widening(bits, into, lane_signed(lane)), out_long, 0, 0); } else if into < bits { # Each lane's low bytes, of each half, into that half's low 8 bytes, then # the halves' together. var low: u8[32]; val width := bits / 8; val kept := into / 8; for i in 0..32u { val at := i % 16; low[i] = at < 16 / width * kept ? ((at / kept) * width + at % kept) as u8 : 0x80; } this.b.text.vex_imm(vec_vextracti128, true, 0, 0, 1, 1); this.load_bytes(2, low, false); this.b.text.vex(vec_pshufb, false, 0, 0, 2); this.b.text.vex(vec_pshufb, false, 1, 1, 2); this.b.text.vex(vec_punpcklqdq, false, 0, 0, 1); } return; } if !real && into_real { if target == lane_f64 { this.vunary(vec_cvtdq2pd, true, 0, 0); return; } if bits < 32 { this.vunary(widening(bits, 32, lane_signed(lane)), out_long, 0, 0); } if lane == lane_u32 { # The high 16 bits times 2^16, and the low 16, each exact, added once. this.vmove(out_long, 1, 0); this.shift_by(vec_psrld, out_long, 1, 16); this.splat_bits(2, 0x0000FFFF0000FFFF, out_long); this.vop(vec_pand, out_long, 0, 2); this.vunary(vec_cvtdq2ps, out_long, 0, 0); this.vunary(vec_cvtdq2ps, out_long, 1, 1); this.splat_bits(2, 0x4780000047800000, out_long); this.vop(vec_mulps, out_long, 1, 2); this.vop(vec_addps, out_long, 0, 1); return; } this.vunary(vec_cvtdq2ps, out_long, 0, 0); return; } if real && !into_real { if lane == lane_f32 { this.vunary(vec_cvttps2dq, long, 0, 0); } else { this.b.text.vex(vec_cvttpd2dq, true, 0, 0, 0); } # An unsigned integer holds what is below zero at 0, as `as` does. if !lane_signed(target) { this.vop(vec_pcmpeqd, out_long, 1, 1); this.vmove(out_long, 2, 0); this.vop(vec_pcmpgtd, out_long, 2, 1); this.vop(vec_pand, out_long, 0, 2); } return; } if lane == lane_f32 { this.vunary(vec_cvtps2pd, true, 0, 0); } else { this.b.text.vex(vec_cvtpd2ps, true, 0, 0, 0); } } #! Lanes gathered from memory into `xmm0`, by AVX2's `vpgatherdd` and the rest: #! the address in `rax`, the 32-bit places in `xmm1`, and a mask of every lane #! in `xmm2`, which the instruction clears as it goes. `simd`'s `gather` checks #! each place is within the view first. #! @param this The selector. #! @param index The instruction. #! @param lane The type of lane. #! @param long Whether the lanes gathered are 32 bytes. def selector::gather_lanes(&mut this, index: u32, lane: u32, long: bool): void { val bits := lane_bits(lane); this.load_into(this.body.arg(index, 0), index, reg::rax); this.fetch_lanes(index, 1, 1, bits == 32 && long); this.b.text.vex(vec_pcmpeqd, long, 2, 2, 2); this.b.text.vex(vec_pxor, long, 0, 0, 0); # VEX's three bytes, of the map `0F 38`, the prefix `66`, `W` for 64-bit # lanes, and `xmm2` the mask; then the opcode, `ModRM` naming a `SIB`, and the # `SIB` of the lanes' scale, `xmm1` the index, and `rax` the base. this.b.text.byte(0xC4); this.b.text.byte(0xE2); this.b.text.byte(((bits == 64 ? 0x80 : 0) | ((~2u & 15) << 3) | (long ? 4 : 0) | 1) as u8); this.b.text.byte(lane >= lane_f32 ? 0x92 : 0x90); this.b.text.byte(0x04); this.b.text.byte(((bits == 64 ? 3 : 2) << 6 | 1 << 3) as u8); }