#! @file singles.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains which `f32`s an executable works in single precision. The code #! holds every real as an `f64`, and an `f32`'s arithmetic as the `f64`'s, #! rounded to an `f32` where it is read: `fsingle`, or a store of four bytes. #! `addss`, `subss`, `mulss`, `divss`, and `sqrtss` of `f32`s round exactly as #! the `f64`'s, rounded again, do, since an `f64` holds more than twice an #! `f32`'s bits, so the code gives the same bits worked either way. Worked in #! single precision, it is one instruction for three, and the conversions #! around each leave every chain of them. #! #! An operation is worked so where its operands are `f32`s exactly, and every #! reader rounds what it gives. A value is held as an `f32`, its low four bytes, #! where it is one, and every reader takes it so: those, comparisons, and the #! edges giving it to a block whose parameter is held so too. Any other reader #! reads it widened, as `real_reg` gives it; a splat of `f32` lanes takes it #! as it is. What is held so is the same with debugging information as #! without, which describes a variable as an `f32` where most of its values are #! held so, and as the `f64` they are held as otherwise, leaving out where it is #! held the other way. import sakura::compiler; import binary::amd64; module sakura::native; # The instructions of `f32`s, written as the vector instructions are, with the # prefix `F3`, and of `f64`s, with `F2`. const ss_add: u32 = 0x020158; const ss_sub: u32 = 0x02015C; const ss_mul: u32 = 0x020159; const ss_div: u32 = 0x02015E; const ss_sqrt: u32 = 0x020151; const ss_min: u32 = 0x02015D; const ss_max: u32 = 0x02015F; const ss_cmp: u32 = 0x0201C2; const ss_widen: u32 = 0x02015A; const sd_min: u32 = 0x03015D; const sd_max: u32 = 0x03015F; const sd_cmp: u32 = 0x0301C2; const sd_narrow: u32 = 0x03015A; const ss_compare: u32 = 0x00012E; # The predicates of `cmpss` and `cmpsd`, each false where either is not a # number, as `<` and `<=` of reals are. const predicate_lt: u8 = 1; const predicate_le: u8 = 2; #! Works out which operations are worked in single precision, and which values #! are held as `f32`s, as the file says. #! @param this The selector. def selector::find_singles(&mut this): void { val values := this.body.origins.length; this.single.resize(values, false); this.works.resize(this.body.insts.length, false); # Of each real, whether every reader takes it as an `f32`, and whether every # reader rounds it to one, which passing it on does not. var handled := vector::::new(scratch()); var rounded := vector::::new(scratch()); handled.resize(values, true); rounded.resize(values, true); # A negation and a choice pass on what they read as it is, so what they read # is rounded where every reader of what they give rounds it. var passing := vector::::new(scratch()); var any := false; for k in 0..this.p.order.length { var walk := this.body.walk(this.p.order[k]); while walk.more() { val at := walk.next(); val held: inst = this.body.insts[at]; val passes := passes_on(held.kind) && held.count != 0 && this.body.type_of(held.result) == vt::real; if passes { passing.append(at); } for a in 0..held.argc { val value := this.body.arg(at, a); continue if this.body.type_of(value) != vt::real; # A splat or a pack of `f32` lanes rounds what it reads to an `f32`, as a # store of one does. val splats := held.kind == op::lanes && (held.aux == lanes_splat || held.aux == lanes_pack) && (held.imm & 0xFF) as u32 == lane_f32; val rounds := held.kind == op::fsingle || (held.kind == op::store && held.aux as access == access::f32 && a == 1) || splats; val alone := held.kind == op::lanes && (held.imm >> 8) == 4; any = any || rounds || alone; if !rounds && !passes { rounded[value] = false; } if !rounds && !splats && !alone && !takes_single(held.kind) { handled[value] = false; } } for e in 0..this.body.edge_count(at) { for &value in this.body.edges[held.aux + e as u32].args { rounded[value] = false; } } } } return if !any; var unrounding := true; while unrounding { unrounding = false; for &at in passing { continue if rounded[this.body.insts[at].result]; val first: u32 = this.body.insts[at].kind == op::select ? 1 : 0; for a in first..this.body.insts[at].argc { val value := this.body.arg(at, a); if rounded[value] { rounded[value] = false; unrounding = true; } } } } # Which reals are `f32`s exactly: what is read or rounded as one, an # operation worked as one, a constant one is, and a block's parameter every # edge gives one, which each is taken to be until an edge gives another. The # function's own parameters are passed widened, whichever they are. var exact := vector::::new(scratch()); exact.resize(values, false); for k in 0..this.p.order.length { val block: u32 = this.p.order[k]; continue if block == 0; for ¶m in this.body.blocks[block].params { exact[param] = this.body.type_of(param) == vt::real; } } var changed := true; while changed { changed = false; for k in 0..this.p.order.length { var walk := this.body.walk(this.p.order[k]); while walk.more() { val at := walk.next(); val held: inst = this.body.insts[at]; continue if held.count == 0 || this.body.type_of(held.result) != vt::real; val result := held.result; var one := false; match held.kind { op::load => one = held.aux as access == access::f32; op::fsingle => one = true; op::fconst => one = single_constant(held.imm); op::fadd, op::fsub, op::fmul, op::fdiv => { one = rounded[result] && exact[this.body.arg(at, 0)] && exact[this.body.arg(at, 1)]; } op::fsqrt => one = rounded[result] && exact[this.body.arg(at, 0)]; # Negating and choosing round nothing, so what they give is an `f32` # wherever what they read is. op::fneg => one = exact[this.body.arg(at, 0)]; op::select => one = exact[this.body.arg(at, 1)] && exact[this.body.arg(at, 2)]; # One `f32` lane alone, or `f32` lanes added or multiplied together, # worked as `f32`s. op::lanes => { one = (held.imm >> 8) == 4 || ((held.imm & 0xFF) as u32 == lane_f32 && (held.aux == lanes_sum || held.aux == lanes_product || (held.aux & 0xFF) == lanes_lane)); } else => {} } exact[result] = one; this.works[at] = one && held.kind != op::load && held.kind != op::fsingle && held.kind != op::fconst; } } for k in 0..this.p.order.length { val block: u32 = this.p.order[k]; val last: u32 = this.body.blocks[block].last; continue if last == 0; val ending: inst = this.body.insts[last]; for e in 0..this.body.edge_count(last) { val edge := ending.aux + e as u32; val params := &this.body.blocks[this.body.edges[edge].target].params; val args := &this.body.edges[edge].args; for i in 0..args.length { val param: u32 = params[i]; if exact[param] && !exact[args[i] as u64] { exact[param] = false; changed = true; } } } } } # Which are held as `f32`s: those every reader takes so, but for constants, # which are made again where read, and the function's own parameters. A # parameter and what each edge gives it are held alike, a constant of zero's # bits being either's. for v in 1..values { continue if !handled[v] || !exact[v] || this.p.remade[v] || this.body.type_of(v as u32) != vt::real; val made := this.body.defining(v as u32); if made == 0 { this.single[v] = this.body.origins[v].from != 0; } else { val kind := this.body.insts[made].kind; this.single[v] = kind == op::load || kind == op::fsingle || kind == op::lanes || this.works[made]; } } changed = true; while changed { changed = false; for k in 0..this.p.order.length { val block: u32 = this.p.order[k]; val last: u32 = this.body.blocks[block].last; continue if last == 0; val ending: inst = this.body.insts[last]; for e in 0..this.body.edge_count(last) { val edge := ending.aux + e as u32; val params := &this.body.blocks[this.body.edges[edge].target].params; val args := &this.body.edges[edge].args; for i in 0..args.length { val param: u32 = params[i]; val arg: u32 = args[i]; continue if this.body.type_of(param) != vt::real; val given := this.single[arg as u64] || this.zero_constant(arg); if this.single[param] && !given { this.single[param] = false; changed = true; } if !this.single[param] && this.single[arg as u64] { this.single[arg as u64] = false; changed = true; } } } } } } #! Whether an operation takes a real it reads as an `f32` where it is held so: #! the arithmetic and comparisons of reals, negation, and choosing, which widen #! it where they work in double precision. #! @param kind The operation. #! @returns Whether it does. def takes_single(kind: op): bool { return match kind { op::fadd, op::fsub, op::fmul, op::fdiv, op::fsqrt, op::feq, op::fne, op::flt, op::fle, op::fneg, op::select => true, else => false }; } #! Whether an operation gives a real it reads as it is, rounding nothing: #! negation, and choosing between two. #! @param kind The operation. #! @returns Whether it does. def passes_on(kind: op): bool { return kind == op::fneg || kind == op::select; } #! Whether a real an instruction reads may be read as an `f32`: one held so, #! or a constant an `f32` holds exactly. #! @param this The selector. #! @param value The value. #! @returns Whether it may. def selector::single_operand(&this, value: u32): bool { return true if this.single[value]; var bits: u64 = 0; return this.p.remade[value] && this.constant_bits(value, &mut bits) && single_constant(bits); } #! Whether a comparison of two reals is made of `f32`s: both may be read so, and #! one is held so, which reading it widened would convert. #! @param this The selector. #! @param a One operand. #! @param b The other. #! @returns Whether it is. def selector::singles_compared(&this, a: u32, b: u32): bool { return (this.single[a] || this.single[b]) && this.single_operand(a) && this.single_operand(b); } #! Zeroes an SSE register by `xorps` of itself, which the processor knows reads #! nothing: before an instruction writing only its low lanes, as a conversion #! does, which would otherwise wait on what the register held last, chaining #! each use of a scratch register to the one before. #! @param this The selector. #! @param x The register's number. def selector::fresh(&mut this, x: u8): void { this.b.text.packed(vec_xorps, x as u32, x as u32); } #! Widens an `f32` in one SSE register into an `f64` in another, which waits #! on nothing the other held: with VEX, merging the source's own upper lanes. #! @param this The selector. #! @param dst The register written. #! @param src The register read. def selector::widen_into(&mut this, dst: u8, src: u8): void { if this.b.text.avx { this.b.text.vex(ss_widen, false, dst as u32, src as u32, src as u32); return; } if dst != src { this.fresh(dst); } this.b.text.cvtss2sd(dst, src); } #! Rounds an `f64` in one SSE register to an `f32` in another, as `widen_into` #! widens one. #! @param this The selector. #! @param dst The register written. #! @param src The register read. def selector::narrow_into(&mut this, dst: u8, src: u8): void { if this.b.text.avx { this.b.text.vex(sd_narrow, false, dst as u32, src as u32, src as u32); return; } if dst != src { this.fresh(dst); } this.b.text.cvtsd2ss(dst, src); } #! A square root of the low lane of one SSE register into another, as #! `widen_into` converts one. #! @param this The selector. #! @param single Whether it is of an `f32`, or an `f64`. #! @param dst The register written. #! @param src The register read. def selector::root_into(&mut this, single: bool, dst: u8, src: u8): void { val op := single ? ss_sqrt : vec_sqrtsd; if this.b.text.avx { this.b.text.vex(op, false, dst as u32, src as u32, src as u32); return; } if dst != src { this.fresh(dst); } this.b.text.packed(op, dst as u32, src as u32); } #! `ucomiss` of two `f32`s, setting the flags as `ucomisd` does for `f64`s. #! @param this The selector. #! @param x One register. #! @param y The other. def selector::compare_singles(&mut this, x: u8, y: u8): void { if this.b.text.avx { this.b.text.vex(ss_compare, false, x as u32, 0, y as u32); return; } this.b.text.packed(ss_compare, x as u32, y as u32); } #! Whether the bits of an `f64` are an `f32`'s exactly. #! @param bits The bits. #! @returns Whether they are. def single_constant(bits: u64): bool { val real := ((&bits) as *f64)[0]; return (real as f32) as f64 == real; } #! Whether a value is the constant `0`, whose bits are the same as an `f32`'s #! and an `f64`'s. #! @param this The selector. #! @param value The value. #! @returns Whether it is. def selector::zero_constant(&this, value: u32): bool { var bits: u64 = 1; return this.constant_bits(value, &mut bits) && bits == 0; } #! The SSE register an instruction reads a real from as an `f32`: its own where #! it is held as one in a register, and otherwise the scratch, which it is #! loaded or rounded into, being one exactly. #! @param this The selector. #! @param value The value. #! @param index The instruction reading it. #! @param scratch The register it is put in where it is in none. #! @returns The register's number. def selector::single_reg(&mut this, value: u32, index: u32, scratch: u8): u8 { var bits: u64 = 0; if this.p.remade[value] && this.constant_bits(value, &mut bits) { if bits == 0 { this.b.text.packed(vec_xorps, scratch as u32, scratch as u32); return scratch; } val real := ((&bits) as *f64)[0]; val narrow: f32 = real as f32; this.b.text.mov_imm(reg::rdx, ((&narrow) as *u32)[0] as u64); this.b.text.movq_to_xmm(scratch, reg::rdx); return scratch; } if !this.single[value] { val wide := this.real_reg(value, index, scratch); this.narrow_into(scratch, wide); return scratch; } val held := this.read(value, index); match held.kind { place::xmm => return held.number; place::gpr => this.b.text.movq_to_xmm(scratch, held.number as reg); place::none => { this.b.text.mov_imm(reg::rdx, 0); this.b.text.movq_to_xmm(scratch, reg::rdx); } else => this.b.text.movss_load(scratch, this.memory_of(&held)); } return scratch; } #! Widens a real held as an `f32` into a register, as `real_reg` gives every #! real. #! @param this The selector. #! @param held Where it is. #! @param scratch The register. #! @returns The register's number. def selector::widened(&mut this, held: &loc, scratch: u8): u8 { match held.kind { place::xmm => this.widen_into(scratch, held.number); place::gpr => { this.b.text.movq_to_xmm(scratch, held.number as reg); this.b.text.cvtss2sd(scratch, scratch); } place::none => { this.b.text.mov_imm(reg::rdx, 0); this.b.text.movq_to_xmm(scratch, reg::rdx); } else => { this.b.text.movss_load(scratch, this.memory_of(held)); this.b.text.cvtss2sd(scratch, scratch); } } return scratch; } #! The instruction of `f32`s an arithmetic of `f64`s is worked as. #! @param kind The arithmetic. #! @returns The instruction. def single_of(kind: sse): u32 { return match kind { sse::addsd => ss_add, sse::subsd => ss_sub, sse::mulsd => ss_mul, sse::divsd => ss_div, else => ss_sqrt }; } #! An arithmetic of two reals worked in single precision, into the result's #! register where it is held as an `f32` there, and otherwise into `xmm0`, #! widened where it is held as an `f64`. #! @param this The selector. #! @param index The instruction. #! @param kind The arithmetic, as its `f64`s' instruction. #! @param a The first operand. #! @param b The second. #! @param result The result. #! @param commutes Whether the operands may be swapped. def selector::single_arithmetic(&mut this, index: u32, kind: sse, a: u32, b: u32, result: u32, commutes: bool): void { val instruction := single_of(kind); val narrow := this.single[result]; val into := narrow ? this.real_target(result) : 0; if into != 0 && this.is_single_in(a, index, into) { this.b.text.packed(instruction, into as u32, this.single_reg(b, index, 1) as u32); } else if into != 0 && commutes && this.is_single_in(b, index, into) { this.b.text.packed(instruction, into as u32, this.single_reg(a, index, 1) as u32); } else { val second := this.single_reg(b, index, 1); val first := this.single_reg(a, index, 0); if first != 0 { this.b.text.movaps(0, first); } this.b.text.packed(instruction, 0, second as u32); if into != 0 { this.b.text.movaps(into, 0); } } this.put_single(result, into); } #! Puts an `f32` worked in an SSE register where its result goes: as it is #! where it is held so, and widened otherwise. #! @param this The selector. #! @param result The result. #! @param x The register. def selector::put_single(&mut this, result: u32, x: u8): void { if this.single[result] { this.put_real(result, x); return; } val wide := this.real_target(result); this.widen_into(wide, x); this.put_real(result, wide); } #! A square root worked in single precision, as `single_arithmetic` works the #! others. #! @param this The selector. #! @param index The instruction. #! @param a The operand. #! @param result The result. def selector::single_root(&mut this, index: u32, a: u32, result: u32): void { val into := this.single[result] ? this.real_target(result) : 0; this.root_into(true, into, this.single_reg(a, index, 1)); this.put_single(result, into); } #! `fneg` worked on an `f32`: its sign bit flipped by `xorps`. #! @param this The selector. #! @param index The instruction. #! @param a The operand. #! @param result The result. def selector::single_negation(&mut this, index: u32, a: u32, result: u32): void { val into := this.single[result] ? this.real_target(result) : 0; val got := this.single_reg(a, index, into); this.b.text.mov_imm(reg::rax, 0x80000000); this.b.text.movq_to_xmm(1, reg::rax); if got != into { this.b.text.movaps(into, got); } this.b.text.packed(vec_xorps, into as u32, 1); this.put_single(result, into); } #! `fsingle` of a real: a copy of one held as an `f32` already, and otherwise #! the real rounded; where the result is held as an `f64`, the rounded real #! widened again. #! @param this The selector. #! @param index The instruction. #! @param a The operand. #! @param result The result. def selector::round_single(&mut this, index: u32, a: u32, result: u32): void { if this.single[result] { val into := this.real_target(result); val got := this.single_reg(a, index, into); if got != into { this.b.text.movaps(into, got); } this.put_real(result, into); return; } val into := this.real_target(result); if this.single[a] { this.widen_into(into, this.single_reg(a, index, 0)); } else { this.narrow_into(0, this.real_reg(a, index, 0)); this.widen_into(into, 0); } this.put_real(result, into); } #! `select` of reals by a comparison of reals folded into it, with no branch: #! where it chooses the lesser or the greater of what it compares, as `a < b ? b #! : a` does, by `maxss` or `minss`, which give their second operand where #! either is not a number, as the comparison does; otherwise by a mask of #! `cmpss`, all ones where the comparison holds, choosing each by `and`, `andn`, #! and `or`. It works in single precision where both it chooses between are #! `f32`s, and otherwise in double. #! @param this The selector. #! @param index The `select`. #! @param compare The comparison, `flt` or `fle`. #! @param result The result. def selector::choose_real(&mut this, index: u32, compare: u32, result: u32): void { val single := this.works[index]; val x := this.body.arg(compare, 0); val y := this.body.arg(compare, 1); val a := this.body.arg(index, 1); val b := this.body.arg(index, 2); var into := single && !this.single[result] ? 0 : this.real_target(result); val lt := this.body.insts[compare].kind == op::flt; # `x < y ? y : x` is the greater, `maxss` of `y` and `x`, and `x < y ? x : y` # the lesser, `minss` of `x` and `y`: each gives its first where the # comparison holds, and its second otherwise. if lt && ((a == y && b == x) || (a == x && b == y)) { val greater := a == y; val second := single ? this.single_reg(b, index, 1) : this.real_reg(b, index, 1); # The second is read after the first is put where the result goes, so not # there. if second == into { into = 0; } val first := single ? this.single_reg(a, index, into) : this.real_reg(a, index, into); if first != into { this.b.text.movaps(into, first); } val op := single ? (greater ? ss_max : ss_min) : (greater ? sd_max : sd_min); this.b.text.packed(op, into as u32, second as u32); if single { this.put_single(result, into); } else { this.put_real(result, into); } return; } # The mask, in `xmm1`, of `f32`s where both compared are, and of `f64`s # otherwise, but where an `f64` is chosen, whose mask is 8 bytes. val narrow := single && this.single_operand(x) && this.single_operand(y); val left := narrow ? this.single_reg(x, index, 1) : this.real_reg(x, index, 1); if left != 1 { this.b.text.movaps(1, left); } val right := narrow ? this.single_reg(y, index, 2) : this.real_reg(y, index, 2); this.b.text.packed_imm(narrow ? ss_cmp : sd_cmp, 1, right as u32, lt ? predicate_lt : predicate_le); val chosen := single ? this.single_reg(a, index, 0) : this.real_reg(a, index, 0); if chosen != 0 { this.b.text.movaps(0, chosen); } this.b.text.packed(vec_andps, 0, 1); val other := single ? this.single_reg(b, index, 2) : this.real_reg(b, index, 2); this.b.text.packed(vec_andnps, 1, other as u32); this.b.text.packed(vec_orps, 0, 1); if single { this.put_single(result, 0); } else { this.put_real(result, 0); } } #! Whether a value an instruction reads is held as an `f32` in an SSE register. #! @param this The selector. #! @param value The value. #! @param index The instruction reading it. #! @param x The register's number. #! @returns Whether it is. def selector::is_single_in(&this, value: u32, index: u32, x: u8): bool { return false if !this.single[value]; val held := this.read(value, index); return held.kind == place::xmm && held.number == x; }