perf(compiler): complete NaN lanes of vector FCVTL and FCVTN inline with IOC for signalling NaNs and DN

This commit is contained in:
Tyler Smith committed 2026-11-12 10:42:08 -05:00
1 parent 52aacde7a7
commit 545da4f097
3 files changed
+102 -37

No files matched your search

@@ -1074,6 +1074,22 @@ pub(super) fn reject_invalid(code: &mut Code) -> usize {
branch(code, Condition::NotZero)
}
/// Accumulate FPSR.IOC from MXCSR invalid in ECX, after the environment is
/// restored: an S/D precision conversion raises it for a signalling NaN
/// input alone, as Arm does.
pub(super) fn commit_invalid_status(code: &mut Code) {
code.asm().test_imm(Size::Dword, Rcx, MXCSR_INVALID as i32);
let valid = branch(code, Condition::Zero);
code.asm().alu_imm8(
Alu::Or,
Size::Dword,
state(core::mem::offset_of!(GuestState, fpsr)),
crate::aarch64::floating::INVALID as i8,
);
let end = code.len();
resolve(code, valid, end);
}
/// Accumulate IXC, UFC, OFC and DZC from ECX's MXCSR status, which all
/// move to FPSR by one shift. With FPCR.FZ, FIZ and AH clear, the host's
/// unflushed result in Arm's rounding mode is the guest's, and its status
@@ -11,8 +11,9 @@ use super::numerical::{
single_vector_arithmetic, test_control, vector_exponent_range, vector_normal_or_zero_range,
};
use super::numerical::{
commit_flush_status, commit_rounded_status, exceptional_lane_mask, flush_subnormal_lanes,
reject_exceptional_lanes, reject_invalid, scalar_ordered_range,
commit_flush_status, commit_invalid_status, commit_rounded_status, exceptional_lane_mask,
flush_subnormal_lanes, minimum_normal_lanes, reject_exceptional_lanes, reject_invalid,
scalar_ordered_range,
};
use super::{Host, commit, encode::guest, packed, read_vector, vector_offset};
use crate::x86_64::asm::FloatLanes;
@@ -24,7 +25,7 @@ use crate::{
aarch64::{
FloatingPointOperation, Instruction, Operation,
floating::{
FPCR_AH, FPCR_AHP, FPCR_EXCEPTION_ENABLE_MASK, FPCR_FIZ, FPCR_FZ, FPCR_NEP,
FPCR_AH, FPCR_AHP, FPCR_DN, FPCR_EXCEPTION_ENABLE_MASK, FPCR_FIZ, FPCR_FZ, FPCR_NEP,
FPCR_ROUNDING_MASK, FPCR_ROUNDING_SHIFT, Format, UNDERFLOW,
precision::PrecisionRounding,
},
@@ -342,29 +343,27 @@ fn rounded_narrow(code: &mut Code, helper: &mut Vec<usize>) -> usize {
/// Vector D to S (FCVTN) with FPCR.FZ, FIZ and AH clear, as
/// [`rounded_narrow`]: subnormal inputs and tiny, overflowing, zero and
/// infinite results are exact on the host with FPSR's status; a NaN, a
/// result exactly the minimum normal and invalid status take `helper`.
/// Returns the jump to the commit, past the other checks.
/// infinite results are exact on the host with FPSR's status. A NaN
/// narrows as Arm's does too (quieted, its payload's top bits kept), the
/// host's invalid status, from a signalling NaN alone, being IOC; DN then
/// replaces it ([`default_nan_lanes`]). A result exactly the minimum normal
/// takes `helper`. Returns the jump to the commit, past the other checks.
fn rounded_vector_narrow(code: &mut Code, helper: &mut Vec<usize>) -> usize {
test_control(code, FPCR_FZ | FPCR_FIZ | FPCR_AH);
let strict = branch(code, Condition::NotZero);
environment_round(code, Rounding::Fpcr);
code.asm().cvtpd2ps(RESULT_VECTOR, Rm::Xmm(SOURCE_VECTOR));
exceptional_lane_mask(
code,
Format::Single,
Shape::Vector64,
RESULT_VECTOR,
EXCEPTIONAL_SCRATCH,
true,
);
let [minimum, scratch, _] = EXCEPTIONAL_SCRATCH;
minimum_normal_lanes(code, Format::Single, RESULT_VECTOR, [minimum, scratch]);
Host::DwordSignMask.register(code, Rax as u8, minimum, TargetCpu::Baseline);
restore_environment(code);
helper.push(reject_exceptional_lanes(
code,
Format::Single,
Shape::Vector64,
));
helper.push(reject_invalid(code));
commit_invalid_status(code);
default_nan_lanes(code);
commit_rounded_status(code);
let done = jump(code);
let strict_start = code.len();
@@ -372,6 +371,28 @@ fn rounded_vector_narrow(code: &mut Code, helper: &mut Vec<usize>) -> usize {
done
}
/// Under FPCR.DN, the default NaN in each NaN S lane of RESULT_VECTOR.
/// Clobbers XMM2, XMM3 and EAX.
fn default_nan_lanes(code: &mut Code) {
const SINGLE_INFINITY: u32 = 0x7f80_0000;
const SINGLE_DEFAULT_NAN: u32 = 0x7fc0_0000;
let [nan, kept, _] = EXCEPTIONAL_SCRATCH;
test_control(code, FPCR_DN);
let propagated = branch(code, Condition::Zero);
packed(code, Host::EqualDword, nan, nan);
ShiftOpcode::RightDword.emit(code, nan, 1);
packed(code, Host::And, nan, RESULT_VECTOR);
dword_constant(code, kept, SINGLE_INFINITY);
packed(code, Host::GreaterDword, nan, kept);
packed(code, Host::MoveAligned, kept, nan);
packed(code, Host::AndNot, kept, RESULT_VECTOR);
dword_constant(code, RESULT_VECTOR, SINGLE_DEFAULT_NAN);
packed(code, Host::And, RESULT_VECTOR, nan);
packed(code, Host::Or, RESULT_VECTOR, kept);
let end = code.len();
resolve(code, propagated, end);
}
/// Branch if any exponent is outside the normal IEEE half range. Keep the
/// normal and finite lane masks for the zero and subnormal path.
fn half_normal_widening_guard(code: &mut Code) -> usize {
@@ -564,7 +585,7 @@ fn emit_widen(code: &mut Code, operand: &PrecisionOperand, upper: bool, pc: Gues
if upper {
ShiftOpcode::RightBytes.emit(code, SOURCE_VECTOR, UPPER_QWORD_BYTES);
}
let widened = vector_widen_with_ah_clear(code, &mut helper);
let widened = vector_widen_with_ah_clear(code);
prepare_vector_input_range(code, Format::Single);
vector_exponent_range(code, Format::Single, Shape::Vector64, SOURCE_VECTOR);
helper.push(branch(code, Condition::NotZero));
@@ -575,13 +596,19 @@ fn emit_widen(code: &mut Code, operand: &PrecisionOperand, upper: bool, pc: Gues
commit(code, operand.destination, RESULT_VECTOR);
}
/// Vector S to D (FCVTL) with FPCR.AH clear, for every input but a NaN
/// (whose DN and signaling status take `helper`): FZ or FIZ flush a
/// subnormal lane first (FZ raising IDC); zeros, infinities, normals and
/// subnormals convert exactly with no status, a subnormal inside the
/// private environment so that the host's denormal status stays out of
/// the caller's MXCSR. Returns the jump to the commit; AH continues past it.
fn vector_widen_with_ah_clear(code: &mut Code, helper: &mut Vec<usize>) -> usize {
/// Vector S to D (FCVTL) with FPCR.AH clear, for every input: FZ or FIZ
/// flush a subnormal lane first (FZ raising IDC); zeros, infinities,
/// normals and subnormals convert exactly with no status, a subnormal
/// inside the private environment so that the host's denormal status stays
/// out of the caller's MXCSR. A NaN widens there as Arm's does (quieted,
/// its payload kept), the host's invalid status, from a signalling NaN
/// alone, being IOC; DN then replaces it. Returns the jump to the commit;
/// AH continues past it.
fn vector_widen_with_ah_clear(code: &mut Code) -> usize {
/// PSHUFD selector widening the low two dwords to qwords.
const LOW_DWORDS_TO_QWORDS: u8 = 0x50;
/// The set top bits that, shifted right once, are the Double default NaN.
const DOUBLE_DEFAULT_NAN_ONES: u8 = 52;
const ACTIVE_LANES: i32 = 0b11;
const SINGLE_INFINITY: u32 = 0x7f80_0000;
const SINGLE_MINIMUM_NORMAL: u32 = 0x0080_0000;
@@ -609,7 +636,7 @@ fn vector_widen_with_ah_clear(code: &mut Code, helper: &mut Vec<usize>) -> usize
packed(code, Host::GreaterDword, lanes, limit);
Host::DwordSignMask.register(code, Rax as u8, lanes, TargetCpu::Baseline);
code.asm().test_imm(Size::Dword, Rax, ACTIVE_LANES);
helper.push(branch(code, Condition::NotZero));
let nan = branch(code, Condition::NotZero);
// Subnormal lanes: a nonzero magnitude below the minimum normal's.
dword_constant(code, lanes, SINGLE_MINIMUM_NORMAL);
packed(code, Host::GreaterDword, lanes, magnitude);
@@ -619,15 +646,37 @@ fn vector_widen_with_ah_clear(code: &mut Code, helper: &mut Vec<usize>) -> usize
Host::DwordSignMask.register(code, Rax as u8, lanes, TargetCpu::Baseline);
code.asm().test_imm(Size::Dword, Rax, ACTIVE_LANES);
let ordinary = branch(code, Condition::Zero);
let special = code.len();
resolve(code, nan, special);
environment_round(code, Rounding::NearestEven);
code.asm().cvtps2pd(RESULT_VECTOR, Rm::Xmm(SOURCE_VECTOR));
restore_environment(code);
let converted = jump(code);
commit_invalid_status(code);
test_control(code, FPCR_DN);
let propagated = branch(code, Condition::Zero);
// The NaN lanes, from the S magnitudes, as qwords.
packed(code, Host::EqualDword, magnitude, magnitude);
ShiftOpcode::RightDword.emit(code, magnitude, 1);
packed(code, Host::And, magnitude, SOURCE_VECTOR);
dword_constant(code, limit, SINGLE_INFINITY);
packed(code, Host::GreaterDword, magnitude, limit);
super::shuffle(code, lanes, magnitude, LOW_DWORDS_TO_QWORDS);
// The Double default NaN, 0x7ff8000000000000.
packed(code, Host::EqualDword, limit, limit);
ShiftOpcode::LeftQword.emit(code, limit, DOUBLE_DEFAULT_NAN_ONES);
ShiftOpcode::RightQword.emit(code, limit, 1);
packed(code, Host::And, limit, lanes);
packed(code, Host::AndNot, lanes, RESULT_VECTOR);
packed(code, Host::Or, lanes, limit);
packed(code, Host::MoveAligned, RESULT_VECTOR, lanes);
let converted = [propagated, jump(code)];
let ordinary_start = code.len();
resolve(code, ordinary, ordinary_start);
code.asm().cvtps2pd(RESULT_VECTOR, Rm::Xmm(SOURCE_VECTOR));
let converted_start = code.len();
resolve(code, converted, converted_start);
for patch in converted {
resolve(code, patch, converted_start);
}
commit_flush_status(code);
let done = jump(code);
let alternative_start = code.len();
@@ -84,6 +84,9 @@ fn floats(format: Format) -> Vec<u64> {
infinity,
infinity | quiet,
infinity | 1,
// NaNs with payloads, signalling and quiet.
infinity | (quiet - 1),
infinity | quiet | quiet >> 3 | 5,
];
if format == Format::Double {
let single_bias = u64::from(Format::Single.exponent_bias());
@@ -191,16 +194,17 @@ fn scalar_conversions_complete_subnormal_tiny_and_flushed_inputs_inline() {
}
}
}
assert!(inline > runs / 2, "{inline} of {runs} inline");
// Scalar NaNs take the helper.
assert!(inline > runs * 2 / 5, "{inline} of {runs} inline");
}
/// FCVTL, FCVTL2, FCVTN and FCVTN2 between S and D lanes over every input
/// class, two distinct classes per vector: with the trap enables and AH
/// clear, widening completes every vector without a NaN inline (zeros,
/// subnormals and infinities included, a subnormal flushed under FZ or
/// FIZ), and with FZ, FIZ and AH clear too, narrowing completes every
/// vector without a NaN or a result exactly the Single minimum normal
/// inline (tiny, subnormal, zero and overflowing results included). Every
/// clear, widening completes every vector inline (zeros, subnormals,
/// infinities and NaNs included, a subnormal flushed under FZ or FIZ), and
/// with FZ, FIZ and AH clear too, narrowing completes every vector without
/// a result exactly the Single minimum normal inline (tiny, subnormal,
/// zero, overflowing and NaN results included). Every
/// inline completion matches the reference, every exit leaves the state
/// unchanged.
#[test]
@@ -269,12 +273,8 @@ fn vector_conversions_complete_zero_subnormal_and_tiny_lanes_inline() {
)
})
.collect();
let nan = lanes
.iter()
.any(|&bits| bits & !source.sign_mask() > source.exponent_mask());
let usable = fpcr & (FPCR_EXCEPTION_ENABLE_MASK | FPCR_AH) == 0
&& !target.data_independent_timing()
&& !nan;
&& !target.data_independent_timing();
let must_inline = usable
&& (source == Format::Single
|| fpcr & (FPCR_FZ | FPCR_FIZ) == 0