Files
sakurac/native/lanes.sakura
T
2026-12-06 13:52:57 -06:00

1298 lines
48 KiB
Plaintext

#! @file lanes.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains how an executable works the lanes of a `simd` with vector
#! instructions, where `lanes_needs` said the processor running it has them:
#! vectors are values, held in the vector registers past `xmm4` or in their
#! slots, and each operation copies its operands into `xmm0` and those after it,
#! works on them, and puts the result where it goes. 16 bytes are worked with
#! SSE's encodings, 32 with AVX's, and 64 with AVX-512's, as `evex.sakura` says.
#!
#! Code for processors with AVX writes every vector instruction with VEX, so no
#! SSE instruction meets a register's upper half. Other code, which works on 32
#! bytes only where it found AVX, clears the upper halves with `vzeroupper`
#! once no register holds 32 bytes, which SSE's instructions would be slowed by
#! on some processors, and before every call and return.
import sakura::compiler;
import binary::amd64;
module sakura::native;
#! `lanes`: the operation `aux` on the lanes of its operands, giving a vector,
#! or for `lanes_bits` the word of a mask's bits.
#! @param this The selector.
#! @param index The instruction.
#! @param held The instruction.
def selector::lanes(&mut this, index: u32, held: &inst): void {
val operation := held.aux;
val lane := (held.imm & 0xFF) as u32;
val long := (held.imm >> 8) == 32;
val result := held.result;
return if !this.wanted(result);
if (held.imm >> 8) < 16 {
this.lane_alone(index, operation, lane, result);
return;
}
if (held.imm >> 8) == 64 {
this.lanes_wide(index, held);
return;
}
if (operation & 0xFF) == lanes_lane {
this.lane_of(index, operation >> 8, lane, long, result);
return;
}
if (operation & 0xFF) >= lanes_convert && (operation & 0xFF) <= lanes_gather {
this.reshape_lanes(index, held, lane, long);
return;
}
if operation >= lanes_sum && operation <= lanes_largest {
this.reduce_lanes(index, operation, lane, long, result);
} else if operation == lanes_bits {
this.mask_bits(index, held, lane, long);
} else if this.binary_in_place(index, operation, lane, long) {
# Worked in the registers the operands and result are in.
} else if operation == lanes_splat {
this.splat_lane(index, lane, long);
this.put_lanes(result, 0, long);
} else if operation == lanes_pack {
this.pack_lanes(index, held, lane);
this.put_lanes(result, 0, long);
} else if operation == lanes_select {
this.fetch_lanes(index, 0, 0, long);
this.fetch_lanes(index, 1, 1, long);
this.fetch_lanes(index, 2, 2, long);
this.vop(vec_andps, long, 1, 0);
this.vop(vec_andnps, long, 0, 2);
this.vop(vec_orps, long, 0, 1);
this.put_lanes(result, 0, long);
} else if operation == lanes_fma {
# `xmm0 = xmm1 * xmm2 + xmm0`, rounded once. Its NaN is a factor's, `xmm1`'s
# before `xmm2`'s, and otherwise the addend's, even where the product is NaN,
# as `∞ * 0` is, which `std::math::fma` gives worked a lane at a time too.
this.fetch_lanes(index, 2, 0, long);
this.fetch_lanes(index, 0, 1, long);
this.fetch_lanes(index, 1, 2, long);
this.b.text.vex(vec_vfmadd231ps | (lane == lane_f64 ? vec_wide : 0), long, 0, 1, 2);
this.put_lanes(result, 0, long);
} else if operation == lanes_shift_left || operation == lanes_shift_right {
this.shift_lanes(index, operation, lane, long);
this.put_lanes(result, 0, long);
} else if operation == lanes_shuffle {
this.shuffle_lanes(index, lane, long);
this.put_lanes(result, 0, long);
} else if operation >= lanes_equal && operation <= lanes_greater_equal {
this.put_lanes(result, this.compare_lanes(index, operation, lane, long), long);
} else if lane >= lane_f32 && (operation == lanes_min || operation == lanes_max) {
this.put_lanes(result, this.real_extreme(index, operation, lane, long), long);
} else {
this.unary_or_binary(index, operation, lane, long);
this.put_lanes(result, 0, long);
}
this.settle_upper(index, long);
}
# The instructions of one real, written as `binary::amd64` writes vector
# instructions: `vfmadd231ss`, whose `sd` is the same with `W`, and `roundss`
# and `roundsd`, with how they round.
const vec_vfmadd231ss: u32 = 0x0102B9;
const vec_roundss: u32 = 0x01030A;
const vec_roundsd: u32 = 0x01030B;
const vec_sqrtsd: u32 = 0x030151;
#! An operation on one real lane alone, as `std::math` works `fma` and rounding
#! of one number: the instruction of one number on the reals themselves, which
#! splats nothing. An `f32` is worked as one, as the instructions of `f32`s are
#! (see `singles.sakura`), and held as one where every reader takes it so.
#! @param this The selector.
#! @param index The instruction.
#! @param operation The operation.
#! @param lane The type of lane, `f32` or `f64`.
#! @param result The result.
def selector::lane_alone(&mut this, index: u32, operation: u32, lane: u32, result: u32): void {
val single := lane == lane_f32;
# `xmm0` is the addend of `fma`, `xmm1` and `xmm2` its factors, as `lanes` has
# them, and the operand of the others.
val count := lanes_operands(operation);
for k in 0..count {
val x: u8 = operation == lanes_fma ? (k == 2 ? 0 : (k + 1) as u8) : k as u8;
val value := this.body.arg(index, k);
if single {
val got := this.single_reg(value, index, x);
if got != x {
this.b.text.movaps(x, got);
}
} else {
this.load_real_into(value, index, x);
}
}
if operation == lanes_fma {
this.b.text.vex(vec_vfmadd231ss | (single ? 0 : vec_wide), false, 0, 1, 2);
} else if operation == lanes_sqrt {
this.b.text.packed(single ? ss_sqrt : vec_sqrtsd, 0, 0);
} else {
val mode: u8 = operation == lanes_floor ? 9 : operation == lanes_ceil ? 10 : operation == lanes_trunc ? 11 : 8;
this.b.text.packed_imm(single ? vec_roundss : vec_roundsd, 0, 0, mode);
}
if !single || this.single[result] {
this.put_real(result, 0);
return;
}
val wide := this.real_target(result);
this.widen_into(wide, 0);
this.put_real(result, wide);
}
#! `lanes_lane`: one lane of 16 or 32 bytes of 4-byte lanes, moved down into the
#! low lane of the result's register by `pshufd`, from the upper half of 32
#! bytes taken first where it is there: an `f32` held as one, or widened where it
#! is held as an `f64`, and an integer moved into a general register, extended
#! as its type is.
#! @param this The selector.
#! @param index The instruction.
#! @param which Which lane.
#! @param lane The type of lane.
#! @param long Whether the vector is 32 bytes.
#! @param result The result.
def selector::lane_of(&mut this, index: u32, which: u32, lane: u32, long: bool, result: u32): void {
val held := this.read(this.body.arg(index, 0), index);
var from := this.lanes_reg(held, 1, long);
var k := which;
if k >= 4 {
this.b.text.vex_imm(vec_vextractf128, true, from as u32, 0, 0, 1);
from = 0;
k -= 4;
}
if lane != lane_f32 {
var x := from;
if k != 0 {
this.vop1_imm(vec_pshufd, false, 0, from, k as u8);
x = 0;
}
val into := this.target(result, reg::rax);
this.b.text.movq_from_xmm(reg::rax, x);
this.b.text.extend(4, lane_signed(lane), into, reg::rax);
this.put(result, into);
return;
}
val into := this.single[result] ? this.real_target(result) : 0;
if k == 0 {
if from != into {
this.vmove(false, into, from);
}
} else {
this.vop1_imm(vec_pshufd, false, into, from, k as u8);
}
this.put_single(result, into);
}
#! Works an operation of two vectors in the register its result is given,
#! where it has one and the operation is one instruction: with VEX's three
#! registers where the code may use them, or SSE's two, the result's register
#! given the first operand first. The operations are arithmetic and bitwise
#! ones, the least and greatest of integers, and integers' equality, and `>` and
#! `<` of signed ones, which `pcmpgt` gives with its operands either way.
#! @param this The selector.
#! @param index The instruction.
#! @param operation The operation.
#! @param lane The type of lane.
#! @param long Whether it works on 32 bytes.
#! @returns Whether it did, where the scratch registers work it otherwise.
def selector::binary_in_place(&mut this, index: u32, operation: u32, lane: u32, long: bool): bool {
val real := lane >= lane_f32;
# What SSE2 works with a sequence: products of bytes and of 64 bits, and of 32
# without SSE4.1's `pmulld`, which code for x86-64-v3 has, and the least and
# greatest of 64-bit lanes.
val bits := lane_bits(lane);
if !real && ((operation == lanes_mul && (bits == 8 || bits == 64 || (bits == 32 && !long && !this.body.wide)))
|| (bits == 64 && (operation == lanes_min || operation == lanes_max))) {
return false;
}
var op: u32 = 0;
var swapped := false;
var commutes := false;
if operation == lanes_add || operation == lanes_mul || operation == lanes_and || operation == lanes_or
|| operation == lanes_xor {
op = binary_of(operation, lane);
commutes = true;
} else if operation == lanes_sub || operation == lanes_div {
op = binary_of(operation, lane);
} else if !real && (operation == lanes_min || operation == lanes_max) {
op = binary_of(operation, lane);
commutes = true;
} else if !real && operation == lanes_equal {
op = by_width(lane, vec_pcmpeqb, vec_pcmpeqw, vec_pcmpeqd, vec_pcmpeqq);
commutes = true;
} else if !real && lane_signed(lane) && (operation == lanes_greater || operation == lanes_less) {
op = by_width(lane, vec_pcmpgtb, vec_pcmpgtw, vec_pcmpgtd, vec_pcmpgtq);
swapped = operation == lanes_less;
} else {
return false;
}
val made := this.p.made_at(this.body.insts[index].result);
return false if made.kind != place::xmm;
val r := made.number;
val first: u32 = swapped ? 1 : 0;
val a := this.read(this.body.arg(index, first), index);
val b := this.read(this.body.arg(index, 1 - first), index);
if long || this.body.wide {
val x := this.lanes_reg(a, 0, long);
val y := this.lanes_reg(b, 1, long);
this.b.text.vex(op, long, r as u32, x as u32, y as u32);
return true;
}
if b.kind == place::xmm && b.number == r {
return false if !commutes;
this.b.text.packed(op, r as u32, this.lanes_reg(a, 0, long) as u32);
return true;
}
if a.kind != place::xmm || a.number != r {
val x := this.lanes_reg(a, 0, long);
this.vmove(long, r, x);
}
this.b.text.packed(op, r as u32, this.lanes_reg(b, 1, long) as u32);
return true;
}
#! A register holding a vector where it is: its own, or a scratch register it
#! is loaded into.
#! @param this The selector.
#! @param held Where it is.
#! @param scratch The register it is loaded into where it is in none.
#! @param long Whether it is 32 bytes.
#! @returns The register.
def selector::lanes_reg(&mut this, held: loc, scratch: u8, long: bool): u8 {
return held.number if held.kind == place::xmm;
if held.kind == place::none {
this.vop(vec_xorps, long, scratch, scratch);
} else {
this.load_vector(scratch, this.memory_of(&held), long);
}
return scratch;
}
#! Clears the registers' upper halves after an instruction working on 32
#! bytes, in code that may run where SSE's instructions would meet them, once
#! no register holds 32 bytes of lanes.
#! @param this The selector.
#! @param index The instruction.
#! @param long Whether it worked on 32 bytes.
def selector::settle_upper(&mut this, index: u32, long: bool): void {
return if !long || this.body.wide;
val after := 4 * (this.p.slots[index] + 1);
for &part in this.upper_parts {
return if pair_high(part) < after && pair_low(part) > after;
}
this.b.text.vzeroupper();
}
#! Finds where registers hold 32 bytes of lanes, in code that may run without
#! AVX, which clears their upper halves where none does.
#! @param this The selector.
def selector::find_upper_parts(&mut this): void {
this.upper_parts.clear();
this.uses_upper = false;
return if this.body.wide;
for v in 0..this.p.bytes.length {
continue if this.p.bytes[v] != 32;
this.uses_upper = true;
for c in this.p.child_first[v]..this.p.child_first[v + 1] {
if this.p.child_locs[c as u64].kind == place::xmm {
this.upper_parts.append(pair_key(this.p.child_starts[c as u64], this.p.child_ends[c as u64]));
}
}
}
}
#! Copies a vector between places: registers, slots, and the stack.
#! @param this The selector.
#! @param from Where it is.
#! @param to Where it goes.
#! @param long Whether it is 32 bytes.
def selector::move_lanes(&mut this, from: loc, to: loc, long: bool): void {
if to.kind == place::xmm {
if from.kind == place::xmm {
this.vmove(long, to.number, from.number);
} else {
this.load_vector(to.number, this.memory_of(&from), long);
}
return;
}
var x: u8 = 0;
if from.kind == place::xmm {
x = from.number;
} else {
this.load_vector(0, this.memory_of(&from), long);
}
this.store_vector(this.memory_of(&to), x, long);
}
#! Loads a vector from memory into a register.
#! @param this The selector.
#! @param x The register.
#! @param from The memory.
#! @param long Whether it is 32 bytes.
def selector::load_vector(&mut this, x: u8, from: mem, long: bool): void {
if long {
this.b.text.vex_mem(vec_movdqu_load, true, x as u32, from);
} else {
this.b.text.packed_mem(vec_movdqu_load, x as u32, from);
}
}
#! Writes a register's vector to memory.
#! @param this The selector.
#! @param into The memory.
#! @param x The register.
#! @param long Whether it is 32 bytes.
def selector::store_vector(&mut this, into: mem, x: u8, long: bool): void {
if long {
this.b.text.vex_mem(vec_movdqu_store, true, x as u32, into);
} else {
this.b.text.packed_mem(vec_movdqu_store, x as u32, into);
}
}
#! Copies an operand of the instruction, a vector, into a register.
#! @param this The selector.
#! @param index The instruction.
#! @param k Which operand.
#! @param x The register.
#! @param long Whether it is 32 bytes.
def selector::fetch_lanes(&mut this, index: u32, k: u32, x: u8, long: bool): void {
val held := this.read(this.body.arg(index, k), index);
if held.kind == place::xmm {
if held.number != x {
this.vmove(long, x, held.number);
}
return;
}
if held.kind == place::none {
this.vop(vec_xorps, long, x, x);
return;
}
this.load_vector(x, this.memory_of(&held), long);
}
#! Puts a vector a register holds where a result goes.
#! @param this The selector.
#! @param value The result.
#! @param x The register.
#! @param long Whether it is 32 bytes.
def selector::put_lanes(&mut this, value: u32, x: u8, long: bool): void {
val held := this.p.made_at(value);
if held.kind == place::xmm {
if held.number != x {
this.vmove(long, held.number, x);
}
} else if held.kind != place::none {
this.store_vector(this.memory_of(&held), x, long);
}
}
#! `load` of a vector: the lanes at an address.
#! @param this The selector.
#! @param index The instruction.
#! @param held The instruction.
#! @param from The memory.
def selector::load_lanes(&mut this, index: u32, held: &inst, from: mem): void {
val long := held.aux == access::v256 as u32;
val result := held.result;
val made := this.p.made_at(result);
if held.aux == access::v512 as u32 {
if made.kind != place::none {
this.move_wide(from, this.memory_of(&made));
}
return;
}
if made.kind == place::xmm {
this.read_halves(made.number, from, long);
} else if made.kind != place::none {
this.read_halves(0, from, long);
this.store_vector(this.memory_of(&made), 0, long);
}
this.settle_upper(index, long);
}
#! Copies 64 bytes of lanes between memory, 16 bytes at a time, as any
#! processor may, where they are held between the instructions working them.
#! @param this The selector.
#! @param from The memory they are in.
#! @param to The memory they go to.
def selector::move_wide(&mut this, from: mem, to: mem): void {
for k in 0..4u {
var a := from;
var b := to;
a.disp += (16 * k) as i32;
b.disp += (16 * k) as i32;
this.b.text.packed_mem(vec_movdqu_load, 0, a);
this.b.text.packed_mem(vec_movdqu_store, 0, b);
}
}
# Reads 32 bytes of memory a half at a time: values are copied 16 bytes at a
# time, and one load reading two stores waits for both to reach the cache,
# which made AVX's loops twice as slow as SSE's.
def selector::read_halves(&mut this, x: u8, from: mem, long: bool): void {
if !long {
this.load_vector(x, from, false);
return;
}
var upper := from;
upper.disp += 16;
this.b.text.vex_mem(vec_movdqu_load, false, x as u32, from);
this.b.text.vex_mem_imm(vec_vinsertf128, true, x as u32, x as u32, upper, 1);
}
#! `store` of a vector: writes its lanes at an address.
#! @param this The selector.
#! @param index The instruction.
#! @param held The instruction.
#! @param to The memory.
def selector::store_lanes(&mut this, index: u32, held: &inst, to: mem): void {
val long := held.aux == access::v256 as u32;
val value := this.body.arg(index, 1);
val from := this.read(value, index);
if held.aux == access::v512 as u32 {
if from.kind == place::none {
this.b.text.packed(vec_xorps, 0, 0);
for k in 0..4u {
var at := to;
at.disp += (16 * k) as i32;
this.b.text.packed_mem(vec_movdqu_store, 0, at);
}
} else {
this.move_wide(this.memory_of(&from), to);
}
return;
}
var x: u8 = 0;
if from.kind == place::xmm {
x = from.number;
} else if from.kind == place::none {
this.vop(vec_xorps, long, 0, 0);
} else {
this.load_vector(0, this.memory_of(&from), long);
}
this.store_vector(to, x, long);
this.settle_upper(index, long);
}
#! `vzero`: a vector of zero bytes.
#! @param this The selector.
#! @param index The instruction.
#! @param held The instruction.
def selector::zero_lanes(&mut this, index: u32, held: &inst): void {
val result := held.result;
val long := this.body.type_of(result) == vt::v256;
val made := this.p.made_at(result);
if this.body.type_of(result) == vt::v512 {
if made.kind != place::none {
val to := this.memory_of(&made);
this.b.text.packed(vec_xorps, 0, 0);
for k in 0..4u {
var at := to;
at.disp += (16 * k) as i32;
this.b.text.packed_mem(vec_movdqu_store, 0, at);
}
}
return;
}
if made.kind == place::xmm {
this.vop(vec_xorps, long, made.number, made.number);
} else if made.kind != place::none {
this.vop(vec_xorps, long, 0, 0);
this.store_vector(this.memory_of(&made), 0, long);
}
this.settle_upper(index, long);
}
# Shuffles, written as `binary::amd64` writes vector instructions: `shufps` and
# `pshufd` choosing each lane by two bits of a byte, `pshuflw` the low four
# 16-bit lanes so, `unpcklpd` putting a register's low `f64` in both halves,
# `punpcklbw` interleaving bytes, and AVX2's `vpbroadcastb` and `vpbroadcastw`
# splatting a register's low byte or 16 bits.
const vec_shufps: u32 = 0x0001C6;
const vec_pshufd: u32 = 0x010170;
const vec_pshuflw: u32 = 0x030170;
const vec_unpcklpd: u32 = 0x010114;
const vec_punpcklbw: u32 = 0x010160;
const vec_vpbroadcastb: u32 = 0x010278;
const vec_vpbroadcastw: u32 = 0x010279;
#! Loads a vector of constant lanes into a register, from those written after
#! the function's code, each once: a word's bits repeated across the register.
#! @param this The selector.
#! @param x The register.
#! @param word The word.
#! @param long Whether it is 32 bytes.
def selector::load_constant(&mut this, x: u8, word: u64, long: bool): void {
var made: u8[32];
for i in 0..32u {
made[i] = ((word >> (8 * (i % 8))) & 0xFF) as u8;
}
this.load_bytes(x, made, long);
}
#! Loads 16 or 32 constant bytes into a register, as `load_constant` does.
#! @param this The selector.
#! @param x The register.
#! @param made The bytes, of which the first 16 are read where not `long`.
#! @param long Whether it is 32 bytes.
def selector::load_bytes(&mut this, x: u8, made: u8[32], long: bool): void {
var found := this.constants.length / 32;
for k in 0..this.constants.length / 32 {
var same := true;
for i in 0..32u {
same = same && this.constants[32 * k + i] == made[i];
}
if same {
found = k;
break;
}
}
if found == this.constants.length / 32 {
for i in 0..32u {
this.constants.append(made[i]);
}
}
this.load_vector(x, absolute(0), long);
this.constant_uses.append(pair_key((this.b.text.length - 4) as u32, found as u32));
}
#! Writes the vectors of constant lanes the code loads after its tables, and
#! the address of each where the code loads it.
#! @param this The selector.
def selector::write_constants(&mut this): void {
return if this.constant_uses.length == 0;
this.b.text.align_to(32);
val base := this.b.text_base + this.b.text.length;
this.b.text.put(this.constants.span);
for i in 0..this.constant_uses.length {
val use: u64 = this.constant_uses[i];
this.b.text.set_u32(pair_high(use) as u64, base + 32 * pair_low(use) as u64);
}
}
#! The word of a lane's bits repeated across it, where a value is a constant.
#! @param this The selector.
#! @param value The value.
#! @param lane The type of lane.
#! @param word Where the word is written.
#! @returns Whether it is a constant.
def selector::splat_word(&this, value: u32, lane: u32, word: &mut u64): bool {
var bits: u64 = 0;
return false if !this.p.remade[value] || !this.constant_bits(value, &mut bits);
if lane == lane_f32 {
val narrow: f32 = ((&bits) as *f64)[0] as f32;
bits = ((&narrow) as *u32)[0] as u64;
}
val width := lane_bits(lane);
if width < 64 {
bits = bits & ((1u << width) - 1);
}
var made := bits;
var filled := width;
while filled < 64 {
made = made | (made << filled);
filled = filled * 2;
}
word = made;
return true;
}
#! Makes every lane of `xmm0` the lane the instruction's operand is: a constant
#! loaded whole, and otherwise shuffled within the register from where it is.
#! @param this The selector.
#! @param index The instruction.
#! @param lane The type of lane.
#! @param long Whether it is 32 bytes.
def selector::splat_lane(&mut this, index: u32, lane: u32, long: bool): void {
val value := this.body.arg(index, 0);
val bits := lane_bits(lane);
var word: u64 = 0;
if this.splat_word(value, lane, &mut word) {
this.load_constant(0, word, long);
return;
}
# A real is shuffled into every lane within the register: an `f32` as one,
# rounded where it is held as an `f64`, and an `f64` as it is.
if lane == lane_f32 || lane == lane_f64 {
if lane == lane_f32 {
val got := this.single_reg(value, index, 0);
if got != 0 {
this.b.text.movaps(0, got);
}
this.b.text.packed_imm(vec_shufps, 0, 0, 0);
} else {
this.load_real_into(value, index, 0);
this.b.text.packed(vec_unpcklpd, 0, 0);
}
if long {
this.b.text.vex_imm(vec_vinsertf128, true, 0, 0, 0, 1);
}
return;
}
if bits == 32 {
val r := this.word_reg(value, index, reg::rax);
this.b.text.movq_to_xmm(0, r);
this.b.text.packed_imm(vec_pshufd, 0, 0, 0);
if long {
this.b.text.vex_imm(vec_vinsertf128, true, 0, 0, 0, 1);
}
return;
}
# Bytes and 16-bit lanes: AVX2 splats the register's low lane, and SSE2
# doubles a byte into 16 bits, then those into the low four lanes, then
# those into all.
this.b.text.movq_to_xmm(0, this.word_reg(value, index, reg::rax));
if bits < 32 && this.body.wide {
this.b.text.vex(bits == 8 ? vec_vpbroadcastb : vec_vpbroadcastw, long, 0, 0, 0);
return;
}
if bits == 8 {
this.b.text.packed(vec_punpcklbw, 0, 0);
}
if bits < 32 {
this.b.text.packed_imm(vec_pshuflw, 0, 0, 0);
this.b.text.packed_imm(vec_pshufd, 0, 0, 0);
} else {
this.b.text.packed(vec_punpcklqdq, 0, 0);
}
if long {
this.b.text.vex_imm(vec_vinsertf128, true, 0, 0, 0, 1);
}
}
# Interleaving the low lanes of two registers, written as `binary::amd64`
# writes vector instructions: `unpcklps` of `f32`s, `punpckldq` of 32-bit
# integers, and `movlhps`, a register's low 8 bytes into another's high.
const vec_unpcklps: u32 = 0x000114;
const vec_punpckldq: u32 = 0x010162;
const vec_movlhps: u32 = 0x000116;
#! Packs the instruction's operands into the lanes of `xmm0`, lane 0 first: four
#! 4-byte lanes from `xmm0` to `xmm3`, two by two, then the pairs; or two 8-byte
#! ones. An `f32` is packed as one, rounded where it is held as an `f64`.
#! @param this The selector.
#! @param index The instruction.
#! @param held The instruction.
#! @param lane The type of lane.
def selector::pack_lanes(&mut this, index: u32, held: &inst, lane: u32): void {
for k in 0..held.argc {
val value := this.body.arg(index, k);
val x := k as u8;
if lane == lane_f32 {
val got := this.single_reg(value, index, x);
if got != x {
this.b.text.movaps(x, got);
}
} else if lane == lane_f64 {
this.load_real_into(value, index, x);
} else {
this.b.text.movq_to_xmm(x, this.word_reg(value, index, reg::rax));
}
}
if held.argc == 4 {
val pairs := lane == lane_f32 ? vec_unpcklps : vec_punpckldq;
this.b.text.packed(pairs, 0, 1);
this.b.text.packed(pairs, 2, 3);
this.b.text.packed(lane == lane_f32 ? vec_movlhps : vec_punpcklqdq, 0, 2);
} else {
this.b.text.packed(lane == lane_f64 ? vec_movlhps : vec_punpcklqdq, 0, 1);
}
}
# Products of 32-bit lanes into 64; the sums of bytes' differences, which of
# zero sum them; and the upper half of a register of 32 bytes into another,
# AVX2's of integers and AVX's of reals.
const vec_pmuludq: u32 = 0x0101F4;
const vec_psadbw: u32 = 0x0101F6;
const vec_vextracti128: u32 = 0x010339;
const vec_vextractf128: u32 = 0x010319;
# The arithmetic of one real, `addss` and `mulss`, whose `sd` is `F2`'s.
const sd_add: u32 = 0x030158;
const sd_mul: u32 = 0x030159;
# The least or greatest of 64-bit lanes, of `xmm0` and `xmm1`, into `xmm0`:
# which is greater, as `pcmpgtq` says of signed ones and of unsigned ones with
# their top bits flipped, chooses each.
def selector::extreme_wide(&mut this, least: bool, lane: u32, long: bool): void {
this.vmove(long, 2, 0);
if lane_signed(lane) {
this.vop(vec_pcmpgtq, long, 2, 1);
} else {
this.splat_bits(3, sign_bits(lane), long);
this.vmove(long, 4, 1);
this.vop(vec_pxor, long, 2, 3);
this.vop(vec_pxor, long, 4, 3);
this.vop(vec_pcmpgtq, long, 2, 4);
}
# `xmm2` is set where `a > b`: the least takes `b` there, the greatest `a`.
val there: u8 = least ? 1 : 0;
val elsewhere: u8 = least ? 0 : 1;
this.vmove(long, 3, there);
this.vop(vec_pand, long, 3, 2);
this.vop(vec_pandn, long, 2, elsewhere);
this.vop(vec_por, long, 2, 3);
this.vmove(long, 0, 2);
}
#! The lanes of a vector worked together into one of the lane's type: integers
#! halved again and again, each half worked into the other, which adding and
#! the least and greatest of integers may be in any order, bytes summed by
#! `psadbw`; and reals a lane at a time, lane 0 first, each shuffled down into
#! the low lane, as `simd`'s own `sum` and `product` work them, which rounding
#! makes a matter of order.
#! @param this The selector.
#! @param index The instruction.
#! @param operation `lanes_sum`, `lanes_product`, `lanes_smallest`, or `lanes_largest`.
#! @param lane The type of lane.
#! @param long Whether it is 32 bytes.
#! @param result The result.
def selector::reduce_lanes(&mut this, index: u32, operation: u32, lane: u32, long: bool, result: u32): void {
val bits := lane_bits(lane);
this.fetch_lanes(index, 0, 0, long);
if lane >= lane_f32 {
val single := lane == lane_f32;
val each := 16 / (bits / 8);
if long {
this.b.text.vex_imm(vec_vextractf128, true, 0, 0, 2, 1);
}
this.b.text.movaps(1, 0);
val op := operation == lanes_sum ? (single ? ss_add : sd_add) : (single ? ss_mul : sd_mul);
for k in 1..(long ? 2 * each : each) {
val from: u8 = k < each ? 0 : 2;
val at := k % each;
if at == 0 {
this.b.text.movaps(3, from);
} else if single {
this.vop1_imm(vec_pshufd, false, 3, from, at as u8);
} else {
this.vop1_imm(vec_pshufd, false, 3, from, 0xEE);
}
this.b.text.packed(op, 1, 3);
}
if !single || this.single[result] {
this.put_real(result, 1);
return;
}
val wide := this.real_target(result);
this.widen_into(wide, 1);
this.put_real(result, wide);
return;
}
# Integers: the upper 16 bytes into the lower, then halves of those.
if long {
this.b.text.vex_imm(vec_vextracti128, true, 0, 0, 1, 1);
this.reduce_step(operation, lane);
}
if bits == 8 && operation == lanes_sum {
this.b.text.packed(vec_pxor, 1, 1);
this.b.text.packed(vec_psadbw, 0, 1);
this.vop1_imm(vec_pshufd, false, 1, 0, 0x4E);
this.b.text.packed(vec_paddq, 0, 1);
} else {
this.vop1_imm(vec_pshufd, false, 1, 0, 0x4E);
this.reduce_step(operation, lane);
if bits <= 32 {
this.vop1_imm(vec_pshufd, false, 1, 0, 0xB1);
this.reduce_step(operation, lane);
}
if bits <= 16 {
this.b.text.packed(vec_movdqa, 1, 0);
this.shift_by(vec_psrld, false, 1, 16);
this.reduce_step(operation, lane);
}
if bits == 8 {
this.b.text.packed(vec_movdqa, 1, 0);
this.shift_by(vec_psrlw, false, 1, 8);
this.reduce_step(operation, lane);
}
}
val into := this.target(result, reg::rax);
this.b.text.movq_from_xmm(into, 0);
this.put(result, into);
}
# Works `xmm1`'s lanes into `xmm0`'s, of 16 bytes, as a reduction's step does.
def selector::reduce_step(&mut this, operation: u32, lane: u32): void {
if operation == lanes_sum {
this.b.text.packed(by_width(lane, vec_paddb, vec_paddw, vec_paddd, vec_paddq), 0, 1);
} else if lane_bits(lane) == 64 {
this.extreme_wide(operation == lanes_smallest, lane, false);
} else {
this.b.text.packed(binary_of(operation == lanes_smallest ? lanes_min : lanes_max, lane), 0, 1);
}
}
# `op x, y, imm` of an instruction reading only `y`, as `pshufd`, where VEX names
# no other register.
def selector::vop1_imm(&mut this, op: u32, long: bool, x: u8, y: u8, imm: u8): void {
if long || this.b.text.avx {
this.b.text.vex_imm(op, long, x as u32, 0, y as u32, imm);
} else {
this.b.text.packed_imm(op, x as u32, y as u32, imm);
}
}
# Shifts each lane of a register by a constant count, given to the shift in
# `xmm4`.
def selector::shift_by(&mut this, op: u32, long: bool, x: u8, count: u64): void {
this.b.text.mov_imm(reg::rax, count);
this.b.text.movq_to_xmm(4, reg::rax);
this.vop(op, long, x, 4);
}
#! A shift of lanes, by a count the same for every lane, given as a `u32`. 16,
#! 32, and 64-bit lanes have SSE2's instructions; bytes are shifted as 16-bit
#! lanes, then masked to what each byte keeps of itself; and a signed shift
#! right of bytes or 64-bit lanes shifts them as unsigned, then gives each the
#! sign it moved down, flipping and taking away the bit it came to. A signed
#! shift right of more than a lane's bits is one of all but one.
#! @param this The selector.
#! @param index The instruction.
#! @param operation The shift.
#! @param lane The type of lane.
#! @param long Whether it is 32 bytes.
def selector::shift_lanes(&mut this, index: u32, operation: u32, lane: u32, long: bool): void {
val bits := lane_bits(lane);
val left := operation == lanes_shift_left;
val signs := !left && lane_signed(lane) && (bits == 8 || bits == 64);
# The count, a `u32`, in the low word of `xmm1`, the same for every lane.
if signs {
this.load_into(this.body.arg(index, 1), index, reg::rax);
this.b.text.mov_imm(reg::rcx, bits - 1);
this.b.text.alu(alu::cmp, reg::rax, reg::rcx);
this.b.text.cmov(cc::above, reg::rax, reg::rcx);
this.b.text.movq_to_xmm(1, reg::rax);
} else {
this.b.text.movq_to_xmm(1, this.word_reg(this.body.arg(index, 1), index, reg::rax));
}
this.fetch_lanes(index, 0, 0, long);
if bits == 64 && signs {
this.vop(vec_psrlq, long, 0, 1);
this.splat_bits(2, sign_bits(lane), long);
this.vop(vec_psrlq, long, 2, 1);
this.vop(vec_pxor, long, 0, 2);
this.vop(vec_psubq, long, 0, 2);
return;
}
if bits != 8 {
this.vop(shift_of(operation, lane), long, 0, 1);
return;
}
# Bytes: shifted as 16 bits, each keeps the bits `0xFF` shifted so keeps, the
# mask made from a 16-bit lane shifted, then its low byte splat.
this.vop(left ? vec_psllw : vec_psrlw, long, 0, 1);
this.splat_bits(2, 0x00FF00FF00FF00FF, long);
this.vop(left ? vec_psllw : vec_psrlw, long, 2, 1);
this.splat_low_byte(2, long);
this.vop(vec_pand, long, 0, 2);
if signs {
this.splat_bits(3, 0x0080008000800080, long);
this.vop(vec_psrlw, long, 3, 1);
this.splat_low_byte(3, long);
this.vop(vec_pxor, long, 0, 3);
this.vop(vec_psubb, long, 0, 3);
}
}
# Choosing bytes by those of another register, SSSE3's; widening bytes into
# 32- or 64-bit lanes; and choosing 32-bit lanes across 32 bytes, AVX2's.
const vec_pshufb: u32 = 0x010200;
const vec_vpmovzxbd: u32 = 0x010231;
const vec_vpmovzxbq: u32 = 0x010232;
const vec_vpermd: u32 = 0x010236;
#! Lanes in another order: lane `i` is the lane the operand's byte `i` names,
#! each one of the vector's, as `simd`'s `shuffle` checks in a checked program.
#! Of 16 bytes, each
#! lane's number is spread over its bytes, as `pshufb` takes it; of 32, 32-bit
#! lanes' numbers are widened, and 64-bit lanes' made two 32-bit lanes', as
#! `vpermd` takes them.
#! @param this The selector.
#! @param index The instruction.
#! @param lane The type of lane.
#! @param long Whether it is 32 bytes.
def selector::shuffle_lanes(&mut this, index: u32, lane: u32, long: bool): void {
val width := lane_bits(lane) / 8;
this.fetch_lanes(index, 1, 1, false);
this.fetch_lanes(index, 0, 0, long);
if long {
if width == 4 {
this.b.text.vex(vec_vpmovzxbd, true, 1, 0, 1);
} else {
this.b.text.vex(vec_vpmovzxbq, true, 1, 0, 1);
this.shift_by(vec_psllq, true, 1, 1);
this.vop1_imm(vec_pshufd, true, 1, 1, 0xA0);
this.load_constant(2, 0x0000000100000000, true);
this.vop(vec_paddd, true, 1, 2);
}
this.b.text.vex(vec_vpermd, true, 0, 1, 0);
return;
}
if width > 1 {
# Each lane's number into each of its bytes, times its width, plus where
# each byte is within it.
var spread: u8[32];
for i in 0..32u {
spread[i] = ((i % 16) / width) as u8;
}
this.load_bytes(2, spread, false);
this.b.text.packed(vec_pshufb, 1, 2);
this.shift_by(vec_psllw, false, 1, width == 2 ? 1 : width == 4 ? 2 : 3);
var within: u64 = 0;
for i in 0..8u {
within = within | ((i % width) << (8 * i));
}
this.load_constant(2, within, false);
this.b.text.packed(vec_paddb, 1, 2);
}
this.b.text.packed(vec_pshufb, 0, 1);
}
# Makes every byte of a register its lowest, as it is in each 16-bit lane.
def selector::splat_low_byte(&mut this, x: u8, long: bool): void {
this.vop(vec_punpcklbw, long, x, x);
this.vop1_imm(vec_pshuflw, long, x, x, 0);
this.vop1_imm(vec_pshufd, long, x, x, 0);
}
# `op x, y`: `x = x op y`, where VEX names `x` twice.
def selector::vop(&mut this, op: u32, long: bool, x: u8, y: u8): void {
if long {
this.b.text.vex(op, true, x as u32, x as u32, y as u32);
} else {
this.b.text.packed(op, x as u32, y as u32);
}
}
# `op x, y` of an instruction of one operand, as `sqrtps`, where VEX names no
# other.
def selector::vop1(&mut this, op: u32, long: bool, x: u8, y: u8, imm: i32): void {
if long {
this.b.text.vex(op, true, x as u32, 0, y as u32);
} else {
this.b.text.packed(op, x as u32, y as u32);
}
if imm >= 0 {
this.b.text.byte(imm as u8);
}
}
# `op x, y, imm`, as `cmpps` with its predicate.
def selector::vop_imm(&mut this, op: u32, long: bool, x: u8, y: u8, imm: u8): void {
if long {
this.b.text.vex_imm(op, true, x as u32, x as u32, y as u32, imm);
} else {
this.b.text.packed_imm(op, x as u32, y as u32, imm);
}
}
# Copies a register.
def selector::vmove(&mut this, long: bool, x: u8, y: u8): void {
this.vop1(vec_movdqa, long, x, y, -1);
}
# Makes every lane of a register its bits from a word repeated, loaded whole.
def selector::splat_bits(&mut this, x: u8, word: u64, long: bool): void {
this.load_constant(x, word, long);
}
# Sets every bit of a register, which integers on 32 bytes have AVX2 for, and
# reals AVX, comparing by a predicate always true.
def selector::all_ones(&mut this, x: u8, long: bool, real: bool): void {
if long && real {
this.b.text.vex_imm(vec_cmpps, true, x as u32, x as u32, x as u32, 0x0F);
return;
}
this.vop(vec_pcmpeqd, long, x, x);
}
# The words of a lane's top bit, and of all but it, for each width of lane.
def sign_bits(lane: u32): u64 {
val bits := lane_bits(lane);
return bits == 8 ? 0x8080808080808080 : bits == 16 ? 0x8000800080008000
: bits == 32 ? 0x8000000080000000 : 0x8000000000000000;
}
# Which of four instructions, one for each width of lane, a lane takes.
def by_width(lane: u32, eight: u32, sixteen: u32, thirty_two: u32, sixty_four: u32): u32 {
val bits := lane_bits(lane);
return bits == 8 ? eight : bits == 16 ? sixteen : bits == 32 ? thirty_two : sixty_four;
}
# The shift of an operation, as the lane's sign takes it.
def shift_of(operation: u32, lane: u32): u32 {
if operation == lanes_shift_left {
return by_width(lane, 0, vec_psllw, vec_pslld, vec_psllq);
}
return lane_signed(lane) ? by_width(lane, 0, vec_psraw, vec_psrad, 0) : by_width(lane, 0, vec_psrlw, vec_psrld, vec_psrlq);
}
# The arithmetic, bitwise operations, and rounding the rest are: those of two
# operands working into the first, and of one in place, into `xmm0`.
def selector::unary_or_binary(&mut this, index: u32, operation: u32, lane: u32, long: bool): void {
val real := lane >= lane_f32;
val doubles: u32 = lane == lane_f64 ? vec_doubles : 0;
if operation == lanes_negate {
if real {
# A real's sign flips.
this.splat_bits(1, sign_bits(lane), long);
this.fetch_lanes(index, 0, 0, long);
this.vop(vec_xorps | doubles, long, 0, 1);
} else {
this.fetch_lanes(index, 0, 1, long);
this.vop(vec_pxor, long, 0, 0);
this.vop(by_width(lane, vec_psubb, vec_psubw, vec_psubd, vec_psubq), long, 0, 1);
}
} else if operation == lanes_not {
this.all_ones(1, long, real);
this.fetch_lanes(index, 0, 0, long);
this.vop(real ? vec_xorps : vec_pxor, long, 0, 1);
} else if operation == lanes_abs {
if real {
this.splat_bits(1, ~sign_bits(lane), long);
this.fetch_lanes(index, 0, 0, long);
this.vop(vec_andps | doubles, long, 0, 1);
} else {
this.fetch_lanes(index, 0, 0, long);
if lane_signed(lane) && lane_bits(lane) == 64 {
# Each lane's sign, from its high half's, flips it and adds one where set.
this.vop1_imm(vec_pshufd, long, 1, 0, 0xF5);
this.shift_by(vec_psrad, long, 1, 31);
this.vop(vec_pxor, long, 0, 1);
this.vop(vec_psubq, long, 0, 1);
} else if lane_signed(lane) {
this.vop1(by_width(lane, vec_pabsb, vec_pabsw, vec_pabsd, 0), long, 0, 0, -1);
}
}
} else if !real && operation == lanes_mul && lane_bits(lane) != 16 && (lane_bits(lane) != 32 || (!long && !this.body.wide)) {
this.fetch_lanes(index, 0, 0, long);
this.fetch_lanes(index, 1, 1, long);
if lane_bits(lane) == 8 {
# The even bytes' products are the low bytes of the 16-bit lanes', and the
# odd ones', moved down and back, the high bytes.
this.vmove(long, 2, 0);
this.vmove(long, 3, 1);
this.vop(vec_pmullw, long, 0, 1);
this.shift_by(vec_psrlw, long, 2, 8);
this.shift_by(vec_psrlw, long, 3, 8);
this.vop(vec_pmullw, long, 2, 3);
this.shift_by(vec_psllw, long, 2, 8);
this.splat_bits(3, 0x00FF00FF00FF00FF, long);
this.vop(vec_pand, long, 0, 3);
this.vop(vec_por, long, 0, 2);
} else if lane_bits(lane) == 32 {
# The even lanes' products, and the odd ones' moved down, each of 64 bits,
# whose low halves are put back in order.
this.vmove(long, 2, 0);
this.vmove(long, 3, 1);
this.vop(vec_pmuludq, long, 0, 1);
this.shift_by(vec_psrlq, long, 2, 32);
this.shift_by(vec_psrlq, long, 3, 32);
this.vop(vec_pmuludq, long, 2, 3);
this.vop1_imm(vec_pshufd, long, 0, 0, 0x08);
this.vop1_imm(vec_pshufd, long, 2, 2, 0x08);
this.vop(vec_punpckldq, long, 0, 2);
} else {
# The low halves' product, and each high half by the other's low, added
# into the high half.
this.vmove(long, 2, 0);
this.vmove(long, 3, 1);
this.shift_by(vec_psrlq, long, 2, 32);
this.shift_by(vec_psrlq, long, 3, 32);
this.vop(vec_pmuludq, long, 2, 1);
this.vop(vec_pmuludq, long, 3, 0);
this.vop(vec_paddq, long, 2, 3);
this.shift_by(vec_psllq, long, 2, 32);
this.vop(vec_pmuludq, long, 0, 1);
this.vop(vec_paddq, long, 0, 2);
}
} else if !real && lane_bits(lane) == 64 && (operation == lanes_min || operation == lanes_max) {
this.fetch_lanes(index, 0, 0, long);
this.fetch_lanes(index, 1, 1, long);
this.extreme_wide(operation == lanes_min, lane, long);
} else if operation == lanes_round {
# Truncated, then a half or more left of each moves it one away from zero,
# as `math::round` gives it: the part left is exact, and a lane it does not
# move keeps its own sign, as `-0` does.
val doubles: u32 = lane == lane_f64 ? vec_doubles : 0;
val f64s := lane == lane_f64;
this.fetch_lanes(index, 0, 0, long);
this.vop1(f64s ? vec_roundpd : vec_roundps, long, 1, 0, 11);
this.vmove(long, 2, 0);
this.vop(vec_subps | doubles, long, 2, 1);
this.splat_bits(3, ~sign_bits(lane), long);
this.vop(vec_andps | doubles, long, 2, 3);
this.splat_bits(4, f64s ? 0x3FE0000000000000 : 0x3F0000003F000000, long);
this.vop_imm(vec_cmpps | doubles, long, 4, 2, 2);
this.splat_bits(3, sign_bits(lane), long);
this.vop(vec_andps | doubles, long, 3, 0);
this.splat_bits(2, f64s ? 0x3FF0000000000000 : 0x3F8000003F800000, long);
this.vop(vec_orps | doubles, long, 3, 2);
this.vop(vec_addps | doubles, long, 3, 1);
this.vop(vec_andps | doubles, long, 3, 4);
this.vop(vec_andnps | doubles, long, 4, 1);
this.vop(vec_orps | doubles, long, 4, 3);
this.vmove(long, 0, 4);
} else if operation == lanes_sqrt {
this.fetch_lanes(index, 0, 0, long);
this.vop1(vec_sqrtps | doubles, long, 0, 0, -1);
} else if operation >= lanes_floor && operation <= lanes_round_even {
# Rounding toward negative infinity, positive, zero, or the nearest, without
# noting it was inexact.
val mode: i32 = operation == lanes_floor ? 9 : operation == lanes_ceil ? 10 : operation == lanes_trunc ? 11 : 8;
this.fetch_lanes(index, 0, 0, long);
this.vop1(lane == lane_f64 ? vec_roundpd : vec_roundps, long, 0, 0, mode);
} else {
this.fetch_lanes(index, 0, 0, long);
this.fetch_lanes(index, 1, 1, long);
this.vop(binary_of(operation, lane), long, 0, 1);
}
}
# The instruction of an operation of two operands on lanes of a type.
def binary_of(operation: u32, lane: u32): u32 {
val real := lane >= lane_f32;
val doubles: u32 = lane == lane_f64 ? vec_doubles : 0;
if operation == lanes_add {
return real ? vec_addps | doubles : by_width(lane, vec_paddb, vec_paddw, vec_paddd, vec_paddq);
}
if operation == lanes_sub {
return real ? vec_subps | doubles : by_width(lane, vec_psubb, vec_psubw, vec_psubd, vec_psubq);
}
if operation == lanes_mul {
return real ? vec_mulps | doubles : by_width(lane, 0, vec_pmullw, vec_pmulld, 0);
}
return vec_divps | doubles if operation == lanes_div;
# Of reals, as a mask's lanes, the bits are combined as reals', which AVX has
# for 32 bytes.
return (real ? vec_andps : vec_pand) if operation == lanes_and;
return (real ? vec_orps : vec_por) if operation == lanes_or;
return (real ? vec_xorps : vec_pxor) if operation == lanes_xor;
val least := operation == lanes_min;
return match lane {
0 => least ? vec_pminsb : vec_pmaxsb,
1 => least ? vec_pminub : vec_pmaxub,
2 => least ? vec_pminsw : vec_pmaxsw,
3 => least ? vec_pminuw : vec_pmaxuw,
4 => least ? vec_pminsd : vec_pmaxsd,
else => least ? vec_pminud : vec_pmaxud
};
}
# Compares lanes into a mask. Reals compare by a predicate, the greater ones
# with their operands swapped. Integers have only equality and `>`, of signed
# lanes, so unsigned ones flip their top bits first, `<` is `>` swapped, and
# `<=`, `>=`, and `!=` are what their opposite is not. Gives the register
# holding the mask.
def selector::compare_lanes(&mut this, index: u32, operation: u32, lane: u32, long: bool): u8 {
val real := lane >= lane_f32;
val ordering := operation >= lanes_less;
if !real && ordering && !lane_signed(lane) {
this.splat_bits(2, sign_bits(lane), long);
}
this.fetch_lanes(index, 0, 0, long);
this.fetch_lanes(index, 1, 1, long);
if real {
val doubles: u32 = lane == lane_f64 ? vec_doubles : 0;
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;
if swapped {
this.vop_imm(vec_cmpps | doubles, long, 1, 0, predicate);
return 1;
}
this.vop_imm(vec_cmpps | doubles, long, 0, 1, predicate);
return 0;
}
if ordering && !lane_signed(lane) {
this.vop(vec_pxor, long, 0, 2);
this.vop(vec_pxor, long, 1, 2);
}
val equal := by_width(lane, vec_pcmpeqb, vec_pcmpeqw, vec_pcmpeqd, vec_pcmpeqq);
val above := by_width(lane, vec_pcmpgtb, vec_pcmpgtw, vec_pcmpgtd, vec_pcmpgtq);
if operation == lanes_equal {
this.vop(equal, long, 0, 1);
} else if operation == lanes_not_equal {
this.vop(equal, long, 0, 1);
this.all_ones(1, long, false);
this.vop(vec_pxor, long, 0, 1);
} else if operation == lanes_greater {
this.vop(above, long, 0, 1);
} else if operation == lanes_less {
this.vop(above, long, 1, 0);
this.vmove(long, 0, 1);
} else if operation == lanes_less_equal {
this.vop(above, long, 0, 1);
this.all_ones(1, long, false);
this.vop(vec_pxor, long, 0, 1);
} else {
this.vop(above, long, 1, 0);
this.all_ones(0, long, false);
this.vop(vec_pxor, long, 0, 1);
}
return 0;
}
# The least or greatest of real lanes, as IEEE 754's `minimum` and `maximum`,
# which `std::math` gives: `minps` gives the lesser where both are numbers and
# differ, equal lanes give the sign of either zero (`|` of their bits for the
# least, `&` for the greatest), and where either is NaN, `a + b`. Gives the
# register holding the result.
def selector::real_extreme(&mut this, index: u32, operation: u32, lane: u32, long: bool): u8 {
val doubles: u32 = lane == lane_f64 ? vec_doubles : 0;
val least := operation == lanes_min;
this.fetch_lanes(index, 0, 0, long);
this.fetch_lanes(index, 1, 1, long);
this.vmove(long, 2, 0);
this.vop((least ? vec_minps : vec_maxps) | doubles, long, 2, 1);
this.vmove(long, 3, 0);
this.vop_imm(vec_cmpps | doubles, long, 3, 1, 0);
this.vmove(long, 4, 0);
this.vop((least ? vec_orps : vec_andps) | doubles, long, 4, 1);
this.vop(vec_andps | doubles, long, 4, 3);
this.vop(vec_andnps | doubles, long, 3, 2);
this.vop(vec_orps | doubles, long, 3, 4);
this.vmove(long, 4, 0);
this.vop_imm(vec_cmpps | doubles, long, 4, 1, 3);
this.vmove(long, 2, 0);
this.vop(vec_addps | doubles, long, 2, 1);
this.vop(vec_andps | doubles, long, 2, 4);
this.vop(vec_andnps | doubles, long, 4, 3);
this.vop(vec_orps | doubles, long, 4, 2);
return 4;
}
# The bits of the lanes a mask has set, as a word: each lane's top bit, as
# `movmskps` and `pmovmskb` gather them, 16-bit lanes packed into bytes first.
def selector::mask_bits(&mut this, index: u32, held: &inst, lane: u32, long: bool): void {
val bits := lane_bits(lane);
var x: u8 = 0;
if bits == 16 {
this.fetch_lanes(index, 0, 0, long);
this.b.text.packed(vec_packsswb, 0, 0);
} else {
x = this.lanes_reg(this.read(this.body.arg(index, 0), index), 0, long);
}
val op := bits == 32 ? vec_movmskps : bits == 64 ? vec_movmskps | vec_doubles : vec_pmovmskb;
val into := this.target(held.result, reg::rax);
if long {
this.b.text.vex(op, true, into as u32, 0, x as u32);
} else {
this.b.text.packed(op, into as u32, x as u32);
}
if bits == 16 {
this.b.text.alu_imm(alu::band, into, 0xFF);
}
this.put(held.result, into);
}