Files
sakurac/native/singles.sakura
T
2026-10-28 22:13:49 -05:00

393 lines
14 KiB
Plaintext

#! @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`.
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;
#! 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::<bool>::new(scratch());
var rounded := vector::<bool>::new(scratch());
handled.resize(values, true);
rounded.resize(values, true);
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];
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 {
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;
# 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::<bool>::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 &param 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)];
# 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));
}
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, 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 => true,
else => false
};
}
#! 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] {
this.b.text.cvtsd2ss(scratch, this.real_reg(value, index, scratch));
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.b.text.cvtss2sd(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);
}
}
if narrow {
this.put_real(result, into);
return;
}
val wide := this.real_target(result);
this.b.text.cvtss2sd(wide, 0);
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 narrow := this.single[result];
val into := narrow ? this.real_target(result) : 0;
this.b.text.packed(ss_sqrt, into as u32, this.single_reg(a, index, 1) as u32);
if narrow {
this.put_real(result, into);
return;
}
val wide := this.real_target(result);
this.b.text.cvtss2sd(wide, into);
this.put_real(result, wide);
}
#! `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.b.text.cvtss2sd(into, this.single_reg(a, index, 0));
} else {
this.b.text.cvtsd2ss(0, this.real_reg(a, index, 0));
this.b.text.cvtss2sd(into, 0);
}
this.put_real(result, into);
}
#! 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;
}