From 5ffa0cb076b720f13c2b642a255df88c9384ba6f Mon Sep 17 00:00:00 2001 From: losfair Date: Sun, 25 Oct 2026 03:04:52 -0700 Subject: [PATCH 1/3] JIT FP hot paths and NEON shuffles used by zlib --- .github/workflows/ci.yml | 8 +- .gitignore | 1 + README.md | 36 ++++- overlay/arch/x86/include/asm/kemu.h | 4 +- overlay/arch/x86/kemu/env.c | 3 +- overlay/kernel/kemu/jit.c | 21 ++- overlay/kernel/kemu/kemu_internal.h | 1 + overlay/rust/kemu_core/src/jit/t1.rs | 6 +- overlay/rust/kemu_core/src/jit/t1/fp.rs | 164 +++++++++++++++++++- overlay/rust/kemu_core/src/jit/t1/vector.rs | 4 + scripts/vm-smoke.sh | 3 +- tests/bench/fp.c | 106 +++++++++++++ tests/bench/zlib.c | 62 ++++++++ tests/kernel/Makefile | 4 +- tests/kernel/fp-status.c | 64 ++++++++ tools/jit-test/src/main.rs | 122 ++++++++++++++- tools/kemu-user/src/syscalls.rs | 65 +++++++- 17 files changed, 647 insertions(+), 27 deletions(-) create mode 100644 tests/bench/fp.c create mode 100644 tests/bench/zlib.c create mode 100644 tests/kernel/fp-status.c diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f4da21f..89457ff 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,7 +30,9 @@ jobs: key: cargo-${{ runner.os }}-${{ env.RUST_TOOLCHAIN }}-${{ hashFiles('overlay/rust/kemu_core/src/**', 'tools/*/src/**', 'tools/*/Cargo.toml') }} restore-keys: cargo-${{ runner.os }}-${{ env.RUST_TOOLCHAIN }}- - name: kemu_core unit tests - run: cargo test --manifest-path overlay/rust/kemu_core/Cargo.toml --features std + run: | + cargo test --manifest-path overlay/rust/kemu_core/Cargo.toml --features std + cargo test --manifest-path tools/kemu-user/Cargo.toml - name: Build tools run: | for t in kemu-user jit-test difftest isa-audit; do @@ -51,6 +53,10 @@ jobs: INTR=20 RELOC=1 $j 10000 10 8 NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 10 8 INTR=20 RELOC=1 $j 10000 32 3 + INTR=20 RELOC=1 $j 10000 11 9 + NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 11 9 + INTR=20 RELOC=1 $j 10000 12 10 + NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 12 10 - name: kemu-user smoke test run: | u=tools/kemu-user/target/release/kemu-user diff --git a/.gitignore b/.gitignore index 9786f0e..ecd8712 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ __pycache__/ tests/kernel/tbi tests/kernel/vector-state tests/kernel/syscall-state +tests/kernel/fp-status diff --git a/README.md b/README.md index c9a84ad..7e96ba2 100644 --- a/README.md +++ b/README.md @@ -116,12 +116,15 @@ The native implementation lives in `overlay/arch/x86/`; other x86_64 kernel ports supply their own architecture code and select `HAVE_KEMU`. To enable the vector register cache, a port defines `KEMU_ARCH_VECTOR_CACHE` -as 1 and provides `kemu_arch_read_xmm(u64 xmm[8][2])` in `asm/kemu.h`. It must -capture the interrupted task's user XMM0..7, synchronizing live FPU state +as 1 and provides `u32 kemu_arch_read_fpu(u64 xmm[8][2])` in `asm/kemu.h`. It must +capture the interrupted task's user XMM0..7 and return MXCSR, synchronizing live FPU state first, and work in task context with IRQs either enabled or disabled. Without this opt-in, vectors remain in the state page. The native implementation handles lazy FPU state and XSAVE's init-state optimization. Userspace callers opt in with `Cfg::vector_cache` and supply `HostRegs::xmm` during recovery. +Recovery must also merge live MXCSR exception flags into FPSR; in page-state +stub ranges, use the saved MXCSR image instead (live state may precede a reload +or signal return). Host atomic access, the virtual counter and random-number generation use the `kemu_arch_*` services declared in `include/linux/kemu.h`. The common @@ -183,6 +186,8 @@ INTR=50 RELOC=1 tools/jit-test/target/release/jit-test 50000 # async interrupt INTR=50 RELOC=1 tools/jit-test/target/release/jit-test 20000 1 6 # SHA256, with no interpreter fallback INTR=50 RELOC=1 tools/jit-test/target/release/jit-test 20000 1 7 # AES, PMULL, SHA1 NO_AES=1 NO_PCLMUL=1 tools/jit-test/target/release/jit-test 20000 1 7 # optional-feature fallback +INTR=20 RELOC=1 tools/jit-test/target/release/jit-test 10000 1 9 # FP min/max and indexed multiply/FMA +INTR=20 RELOC=1 tools/jit-test/target/release/jit-test 10000 1 10 # TBL/TBX and byte/halfword ZIP/UZP scripts/vm-smoke.sh # in-kernel ABI suite etc. ``` @@ -204,6 +209,33 @@ The benchmark warms translations before timing, reports time per crypto instruction including loop/operand overhead, and prints deterministic checksums. It is not a complete cipher or hash throughput test. +`tests/bench/fp.c` exercises SAXPY, a double dot product, 48×48 matrix +multiplication, clamping, range counting, and Mandelbrot. `tests/bench/zlib.c` +measures compression (level 6), decompression, Adler-32, and CRC-32 over +deterministic mixed text/random data, and verifies the compression round trip. +Both accept a case name (or `all`) and iteration count; zlib also accepts the +buffer size in bytes (up to its 1 MiB default). + +```sh +aarch64-linux-gnu-gcc -O3 -static tests/bench/fp.c -lm -o build/fp.arm64 +# ZLIB_ARM64 is an extracted arm64 zlib development package or sysroot. +aarch64-linux-gnu-gcc -O3 -static tests/bench/zlib.c -I$ZLIB_ARM64/usr/include \ + $ZLIB_ARM64/usr/lib/aarch64-linux-gnu/libz.a -o build/zlib.arm64 +qemu-aarch64 build/fp.arm64 all 20 +qemu-aarch64 build/zlib.arm64 all 2 131072 +tools/kemu-user/target/release/kemu-user --jit build/fp.arm64 all 20 +tools/kemu-user/target/release/kemu-user --jit build/zlib.arm64 all 2 131072 +``` + +Use the same binaries on baseline and patched kernels and compare checksums +against QEMU. For a native comparison, compile the same sources with host GCC +and the same optimization level, linking the same zlib version, and run them +in the same VM. Compiler vectorization can differ across architectures. +Run helper profiling separately with `KEMU_JIT_STATS=1`; its per-helper +formatting and counters distort timings. FP min/max and indexed multiply/FMA +retain softfloat fallbacks for NaNs and FPCR.FZ, preserving their exact results +and exception flags. + `tests/jit-workloads/run.sh` runs JIT-heavy programs for Java, Node, LuaJIT, PyPy and Ruby inside an arm64 userland, each with the guest's JIT on and off, and compares the checksums with each other and with native x86_64 runs: diff --git a/overlay/arch/x86/include/asm/kemu.h b/overlay/arch/x86/include/asm/kemu.h index 1d9647d..4a2fa43 100644 --- a/overlay/arch/x86/include/asm/kemu.h +++ b/overlay/arch/x86/include/asm/kemu.h @@ -9,9 +9,9 @@ #include #include -/* Opt in only when asynchronous recovery can capture user XMM0..7. */ +/* Capture user XMM0..7 and return MXCSR for asynchronous recovery. */ #define KEMU_ARCH_VECTOR_CACHE 1 -void kemu_arch_read_xmm(u64 xmm[8][2]); +u32 kemu_arch_read_fpu(u64 xmm[8][2]); #define kemu_reg(regs, name) ((regs)->name) diff --git a/overlay/arch/x86/kemu/env.c b/overlay/arch/x86/kemu/env.c index 2d89d70..64be90e 100644 --- a/overlay/arch/x86/kemu/env.c +++ b/overlay/arch/x86/kemu/env.c @@ -11,7 +11,7 @@ #include #include -void kemu_arch_read_xmm(u64 xmm[8][2]) +u32 kemu_arch_read_fpu(u64 xmm[8][2]) { struct fpu *fpu = x86_task_fpu(current); const void *src; @@ -36,6 +36,7 @@ void kemu_arch_read_xmm(u64 xmm[8][2]) memcpy(xmm, src, 8 * 16); else memset(xmm, 0, 8 * 16); + return src ? fpu->fpstate->regs.fxsave.mxcsr : 0x1f80; } static int kemu_cmpxchg128(u64 addr, const u64 *exp, const u64 *new, u64 *old) diff --git a/overlay/kernel/kemu/jit.c b/overlay/kernel/kemu/jit.c index db6c64a..3e2888c 100644 --- a/overlay/kernel/kemu/jit.c +++ b/overlay/kernel/kemu/jit.c @@ -690,7 +690,7 @@ void kemu_enter_op(struct kemu_thread *kt) kt->flags |= KEMU_TF_KSTATE; } -static void kemu_host_regs(struct pt_regs *regs, struct kemu_host_regs *h) +static u32 kemu_host_regs(struct pt_regs *regs, struct kemu_host_regs *h) { h->gpr[0] = kemu_reg(regs, ax); h->gpr[1] = kemu_reg(regs, cx); @@ -710,9 +710,10 @@ static void kemu_host_regs(struct pt_regs *regs, struct kemu_host_regs *h) h->gpr[15] = kemu_reg(regs, r15); h->rflags = kemu_reg(regs, flags); #if KEMU_ARCH_VECTOR_CACHE - kemu_arch_read_xmm(h->xmm); + return kemu_arch_read_fpu(h->xmm); #else memset(h->xmm, 0, sizeof(h->xmm)); + return 0; #endif } @@ -739,14 +740,20 @@ void kemu_materialize(struct pt_regs *regs) unsigned long ip = kemu_reg(regs, ip), off, c; u8 *blk; size_t len; + u32 mxcsr; if (kt->flags & KEMU_TF_KSTATE) goto out; - kemu_host_regs(regs, &h); + mxcsr = kemu_host_regs(regs, &h); kt->shadow = *cpu; if (ip >= km->code_base && ip < km->code_base + KEMU_STUBS_SIZE) { - kemu_rs_recover_stub(kemu_stub_info(km, ip - km->code_base), - &h, &kt->shadow); + u32 info = kemu_stub_info(km, ip - km->code_base); + + kemu_rs_recover_stub(info, &h, &kt->shadow); + /* During reload (or after rt_sigreturn), live MXCSR can still + * describe the previous state. Use the page when authoritative. */ + if (info == KEMU_STUB_PAGE_STATE || info == KEMU_STUB_PAGE_STATE_SVC) + mxcsr = cpu->scratch[KEMU_MXCSR_SLOT]; } else if (ip >= km->code_base && ip < km->code_base + KEMU_CODE_SIZE) { u64 retired; @@ -774,7 +781,11 @@ void kemu_materialize(struct pt_regs *regs) } else { /* Interpreter mode keeps the page authoritative at all times. */ kt->shadow.nzcv = kemu_rs_nzcv_from_image(kt->shadow.flags_x86); + mxcsr = cpu->scratch[KEMU_MXCSR_SLOT]; } + /* Stubs may not have spilled MXCSR yet. Preserve live cumulative + * exceptions before exit_to_user rebuilds MXCSR from FPSR. */ + kt->shadow.fpsr = kemu_rs_fpsr_merge(kt->shadow.fpsr, mxcsr); *cpu = kt->shadow; kt->flags |= KEMU_TF_KSTATE; out: diff --git a/overlay/kernel/kemu/kemu_internal.h b/overlay/kernel/kemu/kemu_internal.h index 303efdb..ed92916 100644 --- a/overlay/kernel/kemu/kemu_internal.h +++ b/overlay/kernel/kemu/kemu_internal.h @@ -93,6 +93,7 @@ struct kemu_stubs { }; #define KEMU_STUB_PAGE_STATE 0 +#define KEMU_STUB_PAGE_STATE_SVC 4 struct kemu_mm { struct mutex lock; /* translation, chunks, block map */ diff --git a/overlay/rust/kemu_core/src/jit/t1.rs b/overlay/rust/kemu_core/src/jit/t1.rs index 5311eff..e5dd088 100644 --- a/overlay/rust/kemu_core/src/jit/t1.rs +++ b/overlay/rust/kemu_core/src/jit/t1.rs @@ -2232,8 +2232,6 @@ fn is_native(i: &Insn) -> bool { | Insn::FpCCmp { .. } | Insn::SimdAcross { .. } | Insn::SimdSmov { .. } - | Insn::SimdTbl { .. } - | Insn::SimdIndexed { .. } | Insn::Rmif { .. } | Insn::SetfFlags { .. } | Insn::MsrImm { .. } @@ -2430,6 +2428,7 @@ fn translate_n(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut | Insn::FpCSel { .. } | Insn::FpCvt { .. } | Insn::SimdThree { .. } + | Insn::SimdIndexed { .. } | Insn::SimdMisc { .. } | Insn::SimdDup { .. } | Insn::SimdScalarCopy { .. } @@ -2441,6 +2440,7 @@ fn translate_n(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut | Insn::SimdInsElt { .. } | Insn::SimdLdSt { .. } | Insn::SimdPermute { .. } + | Insn::SimdTbl { .. } | Insn::Sha256 { .. } | Insn::Sha1 { .. } | Insn::Aese { .. } @@ -2605,6 +2605,7 @@ fn translate_n(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut Insn::FpCSel { ftype, rd, rn, rm, cond } => g.fp_csel(ftype, rd, rn, rm, cond), Insn::FpCvt { ftype, sf, op, rd, rn } => g.fp_cvt(ftype, sf, op, rd, rn), Insn::SimdThree { q, u, size, op, rd, rn, rm, scalar } => g.simd_three(q, u, size, op, rd, rn, rm, scalar), + Insn::SimdIndexed { q, u, size, opcode, rd, rn, rm, index, scalar } => g.simd_indexed(q, u, size, opcode, rd, rn, rm, index, scalar), Insn::SimdMisc { q, u, size, op, rd, rn, scalar } => g.simd_misc(q, u, size, op, rd, rn, scalar), Insn::SimdDup { q, imm5, rd, rn, from_gpr } => g.simd_dup(q, imm5, rd, rn, from_gpr), Insn::SimdScalarCopy { imm5, rd, rn } => g.simd_scalar_copy(imm5, rd, rn), @@ -2621,6 +2622,7 @@ fn translate_n(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut g.simd_ldst(load, q, opcode, rt, rn, rm, single, post) } Insn::SimdPermute { q, size, opcode, rd, rn, rm } => g.simd_permute(q, size, opcode, rd, rn, rm), + Insn::SimdTbl { q, len, tbx, rd, rn, rm } => g.simd_tbl(q, len, tbx, rd, rn, rm), Insn::Sha256 { op, rd, rn, rm } => g.sha256(op, rd, rn, rm), Insn::Sha1 { op, rd, rn, rm } => g.sha1(op, rd, rn, rm), Insn::Aese { rd, rn } => g.aes(false, rd, rn), diff --git a/overlay/rust/kemu_core/src/jit/t1/fp.rs b/overlay/rust/kemu_core/src/jit/t1/fp.rs index d13f750..52429f7 100644 --- a/overlay/rust/kemu_core/src/jit/t1/fp.rs +++ b/overlay/rust/kemu_core/src/jit/t1/fp.rs @@ -115,6 +115,14 @@ impl<'s, 'b> Gen<'s, 'b> { self.flags_to_mem(); } + /// ARM's FZ input-denormal/underflow status differs from x86 DAZ/FTZ. + /// Keep those cases on softfloat until their flags can be reproduced. + fn fp_fz_guard(&mut self) -> usize { + self.clobber(); + self.a.test_ri(Sz::D, Rm::M(Mem::gs(OFF_FPCR)), 1 << 24); + self.a.jcc(Cc::Ne, None) + } + pub(super) fn fp_imm_insn(&mut self, ftype: u8, rd: u32, imm8: u8) -> bool { let double = match ftype { 0 => false, @@ -134,12 +142,15 @@ impl<'s, 'b> Gen<'s, 'b> { 1 => true, _ => return false, }; + if matches!(op, Fp2Op::Min | Fp2Op::Max | Fp2Op::MinNm | Fp2Op::MaxNm) { + return self.fp_minmax(double, true, false, matches!(op, Fp2Op::Min | Fp2Op::MinNm), rd, rn, rm); + } let opc = match op { Fp2Op::Add => 0x58, Fp2Op::Mul | Fp2Op::Nmul => 0x59, Fp2Op::Sub => 0x5c, Fp2Op::Div => 0x5e, - _ => return false, // min/max family: exact semantics via helper + _ => return false, }; self.fp_prologue(); let pp = if double { PP_F2 } else { PP_F3 }; @@ -159,6 +170,51 @@ impl<'s, 'b> Gen<'s, 'b> { true } + /// MIN/MAX choose the second operand on x86 when both operands are + /// zero. Combine both operand orders to give ARM's -0 for MIN and +0 + /// for MAX. All NaNs go to softfloat, including the numeric variants + /// (whose signaling-NaN and operand-priority rules differ from x86). + fn fp_minmax(&mut self, double: bool, scalar: bool, q: bool, min: bool, rd: u32, rn: u32, rm: u32) -> bool { + self.fp_prologue(); + let fz = self.fp_fz_guard(); + if scalar { + self.ld_scalar(double, X14, rn); + self.ld_scalar(double, X15, rm); + } else if !q { + self.ld_scalar(true, X14, rn); + self.ld_scalar(true, X15, rm); + } else { + self.ld_vec(X14, rn); + self.ld_vec(X15, rm); + } + self.clobber(); + let slow = if scalar { + self.a.vex(1, if double { PP_66 } else { PP_NONE }, false, false, 0x2e, X14, 0, Rm::R(X15)); + self.a.jcc(Cc::P, None) + } else { + self.a.vex_ib(1, if double { PP_66 } else { PP_NONE }, false, false, 0xc2, X13, X14, Rm::R(X15), 3); + self.a.vex(2, PP_66, false, false, 0x17, X13, 0, Rm::R(X13)); + self.a.jcc(Cc::Ne, None) + }; + let pp = match (scalar, double) { + (true, true) => PP_F2, + (true, false) => PP_F3, + (false, true) => PP_66, + (false, false) => PP_NONE, + }; + let opc = if min { 0x5d } else { 0x5f }; + self.a.vex(1, pp, false, false, opc, 12, X14, Rm::R(X15)); + self.a.vex(1, pp, false, false, opc, X14, X15, Rm::R(X14)); + self.a.vex(1, PP_66, false, false, if min { 0xeb } else { 0xdb }, X14, X14, Rm::R(12)); + if scalar { + self.st_scalar(double, X14, rd); + } else { + self.st_vec(q, X14, rd); + } + self.slow_paths(&[fz, slow]); + true + } + pub(super) fn fp3(&mut self, ftype: u8, neg: bool, sub: bool, rd: u32, rn: u32, rm: u32, ra: u32) -> bool { let double = match ftype { 0 => false, @@ -427,6 +483,51 @@ impl<'s, 'b> Gen<'s, 'b> { // ---- Advanced SIMD ---- + /// FMUL/FMLA/FMLS by an indexed S/D element. Broadcast before touching + /// the accumulator, so all source/destination aliases are safe. + #[allow(clippy::too_many_arguments)] + pub(super) fn simd_indexed(&mut self, q: bool, u: bool, size: u8, opcode: u8, rd: u32, rn: u32, rm: u32, index: u32, scalar: bool) -> bool { + if u || !matches!(size, 2 | 3) || !matches!(opcode, 1 | 5 | 9) || (size == 3 && !q && !scalar) { + return false; + } + let double = size == 3; + self.fp_prologue(); + let fz = self.fp_fz_guard(); + let elem = self.vector_slice(rm, (index << size) as u8, 12); + self.a.vex(2, PP_66, false, false, if double { 0x59 } else { 0x58 }, 12, 0, elem); + if scalar { + self.ld_scalar(double, X15, rn); + } else if !q { + self.ld_scalar(true, X15, rn); + // Inactive lanes must not raise exceptions (e.g. 0 * infinity). + self.a.vex(1, PP_F3, false, false, 0x7e, 12, 0, Rm::R(12)); + } else { + self.ld_vec(X15, rn); + } + if opcode == 9 { + let pp = if scalar { if double { PP_F2 } else { PP_F3 } } else if double { PP_66 } else { PP_NONE }; + self.a.vex(1, pp, false, false, 0x59, X14, X15, Rm::R(12)); + } else { + if scalar { + self.ld_scalar(double, X14, rd); + } else if !q { + self.ld_scalar(true, X14, rd); + } else { + self.ld_vec(X14, rd); + } + let opc = (if opcode == 1 { 0xb8 } else { 0xbc }) + scalar as u8; + self.a.vex(2, PP_66, double, false, opc, X14, X15, Rm::R(12)); + } + let slow = if scalar { self.nan_guard_scalar(double, X14) } else { self.nan_guard_vec(double, X14) }; + if scalar { + self.st_scalar(double, X14, rd); + } else { + self.st_vec(q, X14, rd); + } + self.slow_paths(&[fz, slow]); + true + } + /// Splat a 32-bit constant into xmm `x`. fn splat32(&mut self, x: u8, v: u32) { self.a.mov_ri32(RAX, v); @@ -462,6 +563,9 @@ impl<'s, 'b> Gen<'s, 'b> { return true; } match op { + SimdOp::FMin | SimdOp::FMax | SimdOp::FMinNm | SimdOp::FMaxNm if size >= 2 && (q || size == 2) => { + self.fp_minmax(size == 3, false, q, matches!(op, SimdOp::FMin | SimdOp::FMinNm), rd, rn, rm) + } SimdOp::PMull | SimdOp::PMull2 => self.pmull(size, op == SimdOp::PMull2, rd, rn, rm), SimdOp::And | SimdOp::Orr | SimdOp::Eor | SimdOp::Bic => { self.ld_vec(X14, rn); @@ -690,6 +794,34 @@ impl<'s, 'b> Gen<'s, 'b> { self.ld_vec(X15, rm); let op = opcode & 7; match (size, q, op) { + // Byte/halfword ZIP1/ZIP2. The high half of a 64-bit input + // starts at byte 4, unlike PUNPCKH's byte 8. + (0 | 1, _, 0b011 | 0b111) => { + let high = op == 0b111; + if high && !q { + self.a.vex_ib(1, PP_66, false, false, 0x73, 3, X14, Rm::R(X14), 4); + self.a.vex_ib(1, PP_66, false, false, 0x73, 3, X15, Rm::R(X15), 4); + } + let opc = (if high && q { 0x68 } else { 0x60 }) + size; + self.a.vex(1, PP_66, false, false, opc, X14, X14, Rm::R(X15)); + } + (0 | 1, _, 0b001 | 0b101) => { + // Gather even/odd elements to each source's low half, + // then concatenate. PSHUFB's high bit zeroes unused bytes. + let bytes = if q { 8 } else { 4 }; + let mut mask = [0x80u8; 16]; + for i in 0..bytes { + mask[i] = (((i >> size) * 2 + (op >> 2) as usize) << size + | (i & ((1 << size) - 1))) as u8; + } + self.mov_imm(RAX, u64::from_le_bytes(mask[..8].try_into().unwrap())); + self.a.vmov_from_gpr(Sz::Q, X13, RAX); + self.mov_imm(RAX, u64::from_le_bytes(mask[8..].try_into().unwrap())); + self.a.vex_ib(3, PP_66, true, false, 0x22, X13, X13, Rm::R(RAX), 1); // vpinsrq + self.a.vex(2, PP_66, false, false, 0x00, X14, X14, Rm::R(X13)); + self.a.vex(2, PP_66, false, false, 0x00, X15, X15, Rm::R(X13)); + self.a.vex(1, PP_66, false, false, if q { 0x6c } else { 0x62 }, X14, X14, Rm::R(X15)); + } // UZP1/UZP2 .4S (2, true, 0b001) => self.a.vex_ib(1, PP_NONE, false, false, 0xc6, X14, X14, Rm::R(X15), 0x88), (2, true, 0b101) => self.a.vex_ib(1, PP_NONE, false, false, 0xc6, X14, X14, Rm::R(X15), 0xdd), @@ -705,6 +837,36 @@ impl<'s, 'b> Gen<'s, 'b> { true } + /// TBL/TBX over 1..4 registers. PSHUFB only checks index bit 7; + /// saturating-add 0x70 to each table-relative index so every index + /// outside 0..15 is zeroed instead of wrapping modulo 16. + pub(super) fn simd_tbl(&mut self, q: bool, len: u8, tbx: bool, rd: u32, rn: u32, rm: u32) -> bool { + self.ld_vec(X15, rm); + self.splat32(12, 0x7070_7070); + if len != 0 { self.splat32(10, 0x1010_1010); } + for k in 0..=len { + self.a.vex(1, PP_66, false, false, 0xdc, X13, X15, Rm::R(12)); // vpaddusb + self.ld_vec(11, (rn + k as u32) & 31); + self.a.vex(2, PP_66, false, false, 0x00, if k == 0 { X14 } else { 11 }, 11, Rm::R(X13)); + if k != 0 { + self.a.vex(1, PP_66, false, false, 0xeb, X14, X14, Rm::R(11)); + } + if k != len { + self.a.vex(1, PP_66, false, false, 0xf8, X15, X15, Rm::R(10)); // vpsubb + } + } + if tbx { + self.ld_vec(X15, rm); + self.splat32(12, ((len as u32 + 1) * 16 - 1) * 0x0101_0101); + self.a.vex(1, PP_66, false, false, 0xda, X13, X15, Rm::R(12)); // vpminub + self.a.vex(1, PP_66, false, false, 0x74, X13, X13, Rm::R(X15)); // valid indices + self.a.vex(1, PP_66, false, false, 0xdf, X13, X13, self.v_rm(rd)); + self.a.vex(1, PP_66, false, false, 0xeb, X14, X14, Rm::R(X13)); + } + self.st_vec(q, X14, rd); + true + } + /// x = pairwise op of adjacent bytes as 16-bit words (low byte), using /// x13 = 0x00ff mask; result bytes in the low byte of each word. fn pairwise_bytes(&mut self, x: u8, opc: u8) { diff --git a/overlay/rust/kemu_core/src/jit/t1/vector.rs b/overlay/rust/kemu_core/src/jit/t1/vector.rs index f2de070..a570194 100644 --- a/overlay/rust/kemu_core/src/jit/t1/vector.rs +++ b/overlay/rust/kemu_core/src/jit/t1/vector.rs @@ -48,6 +48,10 @@ pub(super) fn choose(insns: &[Insn]) -> [u8; meta::VECTOR_REGS] { for j in 0..n { add(rt + j); } } Insn::SimdLdSt { rt, replicate: true, .. } => add(rt), + Insn::SimdTbl { rd, rn, rm, len, .. } => { + add(rd); add(rm); + for j in 0..=len as u32 { add(rn + j); } + } _ => {} } } diff --git a/scripts/vm-smoke.sh b/scripts/vm-smoke.sh index 9288eff..bd991d3 100755 --- a/scripts/vm-smoke.sh +++ b/scripts/vm-smoke.sh @@ -16,9 +16,10 @@ echo "== tbi"; ./tbi > /tmp/tbi.log 2>&1; grep FAIL /tmp/tbi.log; tail -1 /tmp/t grep -q "TBI ALL PASSED" /tmp/tbi.log && echo "SMOKE ok tbi" echo "== vector-state"; ./vector-state && echo "SMOKE ok vector-state" echo "== syscall-state"; ./syscall-state && echo "SMOKE ok syscall-state" +echo "== fp-status"; ./fp-status && echo "SMOKE ok fp-status" echo "== intbench"; ../bench/intbench.arm64 && echo "SMOKE ok intbench" ' 2>&1 | grep -vE '^\[ |iPXE|SeaBIOS|Press|Booting|^.\[\?7l' | tee "$log" -want="hello abi tbi vector-state syscall-state mtstress intbench" +want="hello abi tbi vector-state syscall-state fp-status mtstress intbench" rc=0 for t in $want; do grep -q "SMOKE ok $t" "$log" || { echo "FAILED: $t"; rc=1; } diff --git a/tests/bench/fp.c b/tests/bench/fp.c new file mode 100644 index 0000000..650bb7c --- /dev/null +++ b/tests/bench/fp.c @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Common FP kernels; build identically at -O3 for AArch64 and x86-64. */ +#include +#include +#include +#include +#include + +#define N 4096 +#define M 48 +static float x[N], y[N], z[N], a[M*M], b[M*M], c[M*M]; +static double d[N]; +static volatile double sink; + +__attribute__((noinline)) static double saxpy(void) +{ + for (int i = 0; i < N; i++) z[i] = 0.75f * x[i] + y[i]; + return z[0] + z[N/2] + z[N-1]; +} + +__attribute__((noinline)) static double dot(void) +{ + double sum = 0; + for (int i = 0; i < N; i++) sum += d[i] * d[N-1-i]; + return sum; +} + +__attribute__((noinline)) static double matmul(void) +{ + memset(c, 0, sizeof(c)); + for (int i = 0; i < M; i++) + for (int k = 0; k < M; k++) + for (int j = 0; j < M; j++) c[i*M+j] += a[i*M+k] * b[k*M+j]; + return c[0] + c[M*M/2] + c[M*M-1]; +} + +__attribute__((noinline)) static double clamp(void) +{ + for (int i = 0; i < N; i++) z[i] = fminf(fmaxf(x[i], -0.25f), 0.25f); + return z[0] + z[N/2] + z[N-1]; +} + +__attribute__((noinline)) static double range(void) +{ + unsigned count = 0; + for (int i = 0; i < N; i++) count += d[i] > -0.25 && d[i] < 0.25; + return count; +} + +__attribute__((noinline)) static double mandelbrot(void) +{ + unsigned total = 0; + for (int j = 0; j < 48; j++) + for (int i = 0; i < 48; i++) { + double cr = i / 24.0 - 1.5, ci = j / 24.0 - 1.0, re = 0, im = 0; + unsigned n = 0; + while (n < 64 && re*re + im*im < 4.0) { + double next = re*re - im*im + cr; + im = 2.0 * re * im + ci; + re = next; + n++; + } + total += n; + } + return total; +} + +static double now(void) +{ + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return t.tv_sec + t.tv_nsec * 1e-9; +} + +int main(int argc, char **argv) +{ + static const struct { const char *name; double (*fn)(void); } tests[] = { + { "saxpy", saxpy }, { "dot", dot }, { "matmul", matmul }, + { "clamp", clamp }, { "range", range }, { "mandelbrot", mandelbrot }, + }; + unsigned rounds = argc > 2 ? strtoul(argv[2], NULL, 0) : 50; + if (!rounds) return 2; + setvbuf(stdout, NULL, _IOLBF, 0); + unsigned rng = 12345; + for (int i = 0; i < N; i++) { + rng = rng * 1664525u + 1013904223u; + x[i] = ((int)(rng >> 16) - 32768) / 32768.0f; + y[i] = (i % 31) / 32.0f; + d[i] = x[i]; + } + for (int i = 0; i < M*M; i++) { a[i] = x[i]; b[i] = y[i]; } + for (unsigned k = 0; k < sizeof(tests)/sizeof(tests[0]); k++) { + if (argc > 1 && strcmp(argv[1], "all") && strcmp(argv[1], tests[k].name)) continue; + double sum = 0; + sink = tests[k].fn(); + double start = now(); + for (unsigned r = 0; r < rounds; r++) { + __asm__ volatile("" ::: "memory"); + sum += tests[k].fn(); + } + double elapsed = now() - start; + sink = sum; + printf("FP %-10s rounds=%u ms=%.3f checksum=%.12g\n", tests[k].name, rounds, elapsed*1000, sum); + } + return 0; +} diff --git a/tests/bench/zlib.c b/tests/bench/zlib.c new file mode 100644 index 0000000..dad99e2 --- /dev/null +++ b/tests/bench/zlib.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include + +#define SIZE (1u << 20) +static unsigned char input[SIZE], output[SIZE]; + +static double now(void) +{ + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return t.tv_sec + t.tv_nsec * 1e-9; +} + +int main(int argc, char **argv) +{ + unsigned rounds = argc > 2 ? strtoul(argv[2], NULL, 0) : 10; + unsigned bytes = argc > 3 ? strtoul(argv[3], NULL, 0) : SIZE; + if (!rounds || !bytes || bytes > SIZE) return 2; + setvbuf(stdout, NULL, _IOLBF, 0); + unsigned rng = 12345; + const char text[] = "The quick brown fox jumps over the lazy dog. 0123456789\n"; + for (unsigned i = 0; i < bytes; i++) { + rng = rng * 1664525u + 1013904223u; + input[i] = i % 4096 < 3072 ? text[i % (sizeof(text)-1)] : rng >> 24; + } + uLongf capacity = compressBound(bytes), size = capacity; + unsigned char *compressed = malloc(capacity); + assert(compressed && compress2(compressed, &size, input, bytes, 6) == Z_OK); + const char *names[] = { "compress", "decompress", "adler32", "crc32" }; + for (unsigned k = 0; k < 4; k++) { + if (argc > 1 && strcmp(argv[1], "all") && strcmp(argv[1], names[k])) continue; + unsigned long checksum = 0; + double start = now(); + for (unsigned r = 0; r < rounds; r++) { + if (k == 0) { + size = capacity; + assert(compress2(compressed, &size, input, bytes, 6) == Z_OK); + checksum += size; + } else if (k == 1) { + uLongf len = bytes; + assert(uncompress(output, &len, compressed, size) == Z_OK && len == bytes); + checksum += output[r % bytes]; + } else if (k == 2) checksum += adler32(1, input, bytes); + else checksum += crc32(0, input, bytes); + } + double elapsed = now() - start; + if (k < 2) { + uLongf len = bytes; + assert(uncompress(output, &len, compressed, size) == Z_OK && len == bytes); + assert(!memcmp(input, output, bytes)); + } + printf("ZLIB %-10s rounds=%u bytes=%u ms=%.3f MiB/s=%.2f checksum=%lu version=%s\n", + names[k], rounds, bytes, elapsed*1000, (double)rounds * bytes / SIZE / elapsed, checksum, zlibVersion()); + } + free(compressed); + return 0; +} diff --git a/tests/kernel/Makefile b/tests/kernel/Makefile index 906dcc9..2bb0c54 100644 --- a/tests/kernel/Makefile +++ b/tests/kernel/Makefile @@ -3,14 +3,14 @@ CC := aarch64-linux-gnu-gcc CFLAGS := -O2 -Wall -Wno-unused-result -STATIC := abi auxv cat crash hello-static tbi vector-state syscall-state +STATIC := abi auxv cat crash hello-static tbi vector-state syscall-state fp-status DYN := hello-dyn mtstress all: $(STATIC) $(DYN) ../bench/intbench.arm64 abi syscall-state: %: %.c $(CC) $(CFLAGS) -static -pthread -o $@ $< -auxv cat crash tbi vector-state: %: %.c +auxv cat crash tbi vector-state fp-status: %: %.c $(CC) $(CFLAGS) -static -o $@ $< hello-static: hello.c $(CC) $(CFLAGS) -static -o $@ $< diff --git a/tests/kernel/fp-status.c b/tests/kernel/fp-status.c new file mode 100644 index 0000000..a81dcb4 --- /dev/null +++ b/tests/kernel/fp-status.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 +/* FP exceptions must survive signals arriving before a JIT stub spills MXCSR. */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +static volatile sig_atomic_t waiting, samples, failures, ticks; + +static void alarm_handler(int sig, siginfo_t *info, void *context) +{ + (void)sig; + (void)info; + ticks++; + if (!waiting) return; + ucontext_t *uc = context; + unsigned char *p = uc->uc_mcontext.__reserved; + unsigned char *end = p + sizeof(uc->uc_mcontext.__reserved); + int found = 0; + while (p + 8 <= end) { + unsigned magic, size, fpsr; + memcpy(&magic, p, 4); + memcpy(&size, p + 4, 4); + if (size < 8 || size > (unsigned)(end - p)) break; + if (magic == 0x46508001 && size >= 16) { /* FPSIMD_MAGIC */ + memcpy(&fpsr, p + 8, 4); + /* All four exceptions were raised in translated code. */ + if ((fpsr & 0x1b) != 0x1b) failures++; + found = 1; + break; + } + p += size; + } + if (!found) failures++; + samples++; + waiting = 0; +} + +int main(void) +{ + struct sigaction sa = { .sa_sigaction = alarm_handler, .sa_flags = SA_SIGINFO }; + struct itimerval timer = { .it_interval = { 0, 1000 }, .it_value = { 0, 1000 } }; + if (sigaction(SIGALRM, &sa, NULL) || setitimer(ITIMER_REAL, &timer, NULL)) return 2; + for (unsigned i = 0; i < 100; i++) { + /* DZC (1/0), IOC (0/0), UFC|IXC (smallest subnormal squared). + * The waiting flag is published after the exceptions, then the + * integer-only loop makes no host calls before signal delivery. */ + __asm__ volatile( + "msr fpcr, xzr\n\tmsr fpsr, xzr\n\t" + "fmov s0, #1.0\n\tfmov s1, wzr\n\t" + "fdiv s2, s0, s1\n\tfdiv s2, s1, s1\n\t" + "mov w9, #1\n\tfmov s0, w9\n\tfmul s2, s0, s0\n\t" + "str w9, [%0]\n\t" + "1: ldr w9, [%0]\n\tcbnz w9, 1b\n\t" + :: "r"(&waiting) : "x9", "v0", "v1", "v2", "memory"); + if (ticks > 10000) return 2; + } + timer = (struct itimerval){0}; + setitimer(ITIMER_REAL, &timer, NULL); + printf("fp-status: %d samples, %d failures\n", samples, failures); + return failures || samples != 100; +} diff --git a/tools/jit-test/src/main.rs b/tools/jit-test/src/main.rs index 4bbe1bb..603666c 100644 --- a/tools/jit-test/src/main.rs +++ b/tools/jit-test/src/main.rs @@ -274,10 +274,15 @@ extern "C" fn on_alarm(_sig: i32, _info: *mut libc::siginfo_t, ucv: *mut libc::c } let st = state(); let hh = h(); + let mxcsr; if rip >= REGION && rip < REGION + 4096 { let info = hh.stubs.info((rip - REGION) as u32); + mxcsr = if matches!(info, stubs::STUB_PAGE_STATE | stubs::STUB_PAGE_STATE_SVC) { + st.scratch[7] as u32 + } else { fp.mxcsr }; stubs::recover(info, &hregs, st); } else if let Some(&(b, sz)) = hh.blocks.iter().find(|(b, sz)| rip >= *b && rip < *b + *sz as u64) { + mxcsr = fp.mxcsr; let blk = unsafe { std::slice::from_raw_parts(b as *const u8, sz) }; if !meta::recover(blk, (rip - b) as u32, &hregs, st) { let msg = b"recover failed\n"; @@ -288,6 +293,8 @@ extern "C" fn on_alarm(_sig: i32, _info: *mut libc::siginfo_t, ucv: *mut libc::c return; } st.flags_x86 = image_from_nzcv(st.nzcv); + st.fpsr = fpsr_merge(st.fpsr, mxcsr); + st.scratch[7] = mxcsr_from(st.fpcr, st.fpsr) as u64; INTERRUPTS.fetch_add(1, SeqCst); g[libc::REG_RIP as usize] = (REGION + hh.stubs.dispatch as u64) as i64; g[libc::REG_RSP as usize] = (RSTACK_TOP - 64) as i64; @@ -636,6 +643,9 @@ fn gen_loop(r: &mut Rng) -> Vec { } fn gen_case(r: &mut Rng, mode: u32) -> Vec { + if mode == 9 { + return gen_fp_hot(r); + } if mode == 8 { return gen_vectors(r); } @@ -647,7 +657,7 @@ fn gen_case(r: &mut Rng, mode: u32) -> Vec { } let n = 1 + r.below(unsafe { MAXLEN }) as usize; let mut v = Vec::new(); - if matches!(mode, 6 | 7) { + if matches!(mode, 6 | 7 | 10) { v.push(0xeb01_001f); // cmp x0, x1: keep live host flags across crypto } for k in 0..n { @@ -657,6 +667,7 @@ fn gen_case(r: &mut Rng, mode: u32) -> Vec { 3 => gen_fp(r), 6 => gen_sha256(r), 7 => gen_crypto(r), + 10 => gen_shuffle(r), _ => { // mixed with forward branches match r.below(6) { @@ -687,7 +698,7 @@ fn gen_case(r: &mut Rng, mode: u32) -> Vec { }; v.push(w); } - if matches!(mode, 6 | 7) { + if matches!(mode, 6 | 7 | 10) { v.push(0x9a88_00e6 | ((r.below(14) as u32) << 12)); // csel x6,x7,x8,cond } v.push(0xd420_0000); // brk #0 @@ -749,6 +760,7 @@ fn is_fp(i: &Insn) -> bool { | Insn::FpCSel { .. } | Insn::FpCvt { .. } | Insn::SimdThree { .. } + | Insn::SimdIndexed { .. } | Insn::SimdMisc { .. } | Insn::SimdDup { .. } | Insn::SimdModImm { .. } @@ -771,6 +783,82 @@ fn gen_fp(r: &mut Rng) -> u32 { } } +/// Compiler hot paths: scalar/vector min/max and indexed multiply/FMA. +/// Use one precision per block so the caller can fill every lane with +/// finite values for the no-fallback performance regression check. +fn gen_fp_hot(r: &mut Rng) -> Vec { + let double = r.below(2) as u32; + let mut v = vec![0xeb01_001f]; // cmp x0,x1: preserve flags across FP + const OPS: &[u32] = &[ + 0x1e205800, 0x1e204800, 0x1e207800, 0x1e206800, + 0x4ea0f400, 0x4e20f400, 0x4ea0c400, 0x4e20c400, + 0x4f809000, 0x4f801000, 0x4f805000, + ]; + for _ in 0..1 + r.below(unsafe { MAXLEN }) { + let k = r.below(OPS.len() as u64) as usize; + let mut w = OPS[k] | (double << 22); + if k >= 8 { + w |= (r.below(2) as u32) << 11; // high index bit + if double == 0 { w |= (r.below(2) as u32) << 21; } + if r.below(3) == 0 { w |= 1 << 28; } // scalar + } + if k >= 4 && double == 0 && w & (1 << 28) == 0 && r.below(2) == 0 { + w &= !(1 << 30); // .2S, with dirty inactive lanes + } + let rd = r.below(32) as u32; + let rn = if r.below(3) == 0 { rd } else { r.below(32) as u32 }; + let rm = if r.below(3) == 0 { rd } else { r.below(32) as u32 }; + v.push(w | rd | (rn << 5) | (rm << 16)); + } + v.push(0x9a88_00e6 | ((r.below(14) as u32) << 12)); // csel x6,x7,x8,cond + v.push(0xd420_0000); + v +} + +/// TBL/TBX register lists (including V31 wraparound and aliases) and the +/// byte/halfword shuffles used by zlib's sliding hash tables. +fn gen_shuffle(r: &mut Rng) -> u32 { + let rd = r.below(32) as u32; + let rn = if r.below(3) == 0 { rd } else { r.below(32) as u32 }; + let rm = if r.below(3) == 0 { rd } else { r.below(32) as u32 }; + let regs = rd | (rn << 5) | (rm << 16) | ((r.below(2) as u32) << 30); + if r.below(2) == 0 { + 0x0e00_0000 | regs | ((r.below(4) as u32) << 13) | ((r.below(2) as u32) << 12) + } else { + let opcode = [1, 3, 5, 7][r.below(4) as usize]; + 0x0e00_0800 | regs | ((r.below(2) as u32) << 22) | (opcode << 12) + } +} + +fn init_fp_hot(r: &mut Rng, st: &mut CpuState, code: &[u32], edges: bool) { + let double = code[1] & (1 << 22) != 0; + const D: &[u64] = &[ + 0, 0x8000000000000000, 0x3ff0000000000000, 0xbff0000000000000, + 0x7ff0000000000000, 0xfff0000000000000, 0x7ff8000000000123, + 0xfff8000000000456, 0x7ff0000000000789, 1, 0x8000000000000001, + ]; + const S: &[u64] = &[ + 0, 0x80000000, 0x3f800000, 0xbf800000, 0x7f800000, 0xff800000, + 0x7fc00123, 0xffc00456, 0x7f800789, 1, 0x80000001, + ]; + for v in &mut st.v { + *v = [0; 2]; + for lane in 0..if double { 2 } else { 4 } { + let bits = if edges { + let vals = if double { D } else { S }; + vals[r.below(vals.len() as u64) as usize] + } else { + let x = (r.below(17) as i32 - 8) as f64 / 8.0; + if double { x.to_bits() } else { (x as f32).to_bits() as u64 } + }; + if double { v[lane] = bits; } else { v[lane / 2] |= bits << (32 * (lane % 2)); } + } + } + st.fpcr |= (r.below(2) as u32) << 25; // default NaN mode + if edges { st.fpcr |= (r.below(2) as u32) << 24; } // FZ slow path + st.fpsr = r.next() as u32 & 0x0800_009f; +} + /// SHA256H/H2/SU0/SU1 and vector byte-order reversal, including aliased /// operands and V31. Mode 6 also /// requires native lowering: interpreter fallbacks make hashing large @@ -935,12 +1023,27 @@ fn main() { let mode = only_mode.unwrap_or((it % 3) as u32); let code = gen_case(&mut rng, mode); let mut init = init_state(&mut rng); - if matches!(mode, 6 | 7) { + if mode == 9 { + init_fp_hot(&mut rng, &mut init, &code, it & 1 != 0); + } + if matches!(mode, 6 | 7 | 10) { for v in &mut init.v { *v = [rng.val(), rng.val()]; } init.fpsr = rng.next() as u32 & 0x0800_009f; } + if mode == 10 { + const IDX: &[u8] = &[0, 15, 16, 31, 32, 47, 48, 63, 64, 127, 128, 255]; + for v in &mut init.v { + for word in v { + *word = 0; + for byte in 0..8 { + let x = if rng.below(2) == 0 { rng.below(64) as u8 } else { IDX[rng.below(IDX.len() as u64) as usize] }; + *word |= (x as u64) << (8 * byte); + } + } + } + } for b in data_init.iter_mut() { *b = rng.next() as u8; } @@ -970,7 +1073,10 @@ fn main() { } let mut bad = Vec::new(); - if matches!(mode, 6 | 7) { + if mode == 9 && it & 1 == 0 && h().helpers - helpers_before != 1 { + bad.push(format!("finite FP fallback: {} helpers, expected only BRK", h().helpers - helpers_before)); + } + if matches!(mode, 6 | 7 | 10) { // BRK plus only the instructions whose optional host feature // is unavailable. Unexpected fallbacks are performance bugs. let expected = 1 + code.iter().filter(|&&w| crypto_fallback(decode(w))).count(); @@ -1009,12 +1115,12 @@ fn main() { } if ref_st.fpsr != jit_st.fpsr { fpsr_diffs += 1; - if matches!(mode, 6 | 7) { - bad.push(format!("crypto changed FPSR: ref {:#x} jit {:#x}", ref_st.fpsr, jit_st.fpsr)); + if matches!(mode, 6 | 7 | 9 | 10) { + bad.push(format!("FPSR: ref {:#x} jit {:#x}", ref_st.fpsr, jit_st.fpsr)); } } - if matches!(mode, 6 | 7) && ref_st.fpcr != jit_st.fpcr { - bad.push(format!("crypto changed FPCR: ref {:#x} jit {:#x}", ref_st.fpcr, jit_st.fpcr)); + if matches!(mode, 6 | 7 | 9 | 10) && ref_st.fpcr != jit_st.fpcr { + bad.push(format!("FPCR: ref {:#x} jit {:#x}", ref_st.fpcr, jit_st.fpcr)); } if !bad.is_empty() { for w in &code { diff --git a/tools/kemu-user/src/syscalls.rs b/tools/kemu-user/src/syscalls.rs index 234ecef..a18e61c 100644 --- a/tools/kemu-user/src/syscalls.rs +++ b/tools/kemu-user/src/syscalls.rs @@ -11,6 +11,68 @@ thread_local! { static SYSROOT: RefCell> = RefCell::new(std::env::var("KEMU_SYSROOT").ok()); // Keep rewritten CStrings alive for the duration of the syscall. static REWRITE_BUF: RefCell> = RefCell::new(Vec::new()); + static HEAP: RefCell = RefCell::new(GuestHeap::new()); +} + +// Forwarding brk lets guest malloc overwrite Rust/glibc's own heap. Reserve +// a separate anonymous range; Linux brk returns the old break on failure. +const HEAP_SIZE: usize = 1 << 30; +const PAGE: u64 = 4096; +struct GuestHeap { + base: u64, + current: u64, +} + +impl GuestHeap { + fn new() -> Self { + let p = unsafe { + libc::mmap(std::ptr::null_mut(), HEAP_SIZE, libc::PROT_NONE, + libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_NORESERVE, -1, 0) + }; + assert_ne!(p, libc::MAP_FAILED, "reserve guest heap"); + Self { base: p as u64, current: p as u64 } + } + + fn brk(&mut self, addr: u64) -> u64 { + if addr < self.base || addr > self.base + HEAP_SIZE as u64 { + return self.current; + } + let old = (self.current + PAGE - 1) & !(PAGE - 1); + let new = (addr + PAGE - 1) & !(PAGE - 1); + if new != old { + let start = old.min(new) as *mut libc::c_void; + let len = old.abs_diff(new) as usize; + let prot = if new > old { libc::PROT_READ | libc::PROT_WRITE } else { libc::PROT_NONE }; + if unsafe { libc::mprotect(start, len, prot) } != 0 { + return self.current; + } + if new < old { + unsafe { libc::madvise(start, len, libc::MADV_DONTNEED) }; + } + } + self.current = addr; + addr + } +} + +impl Drop for GuestHeap { + fn drop(&mut self) { + unsafe { libc::munmap(self.base as *mut libc::c_void, HEAP_SIZE) }; + } +} + +#[test] +fn guest_brk_grow_shrink_and_bounds() { + let mut heap = GuestHeap::new(); + let base = heap.brk(0); + assert_eq!(heap.brk(base + PAGE + 1), base + PAGE + 1); + unsafe { ((base + PAGE) as *mut u8).write(0xab) }; + assert_eq!(heap.brk(base + 1), base + 1); + assert_eq!(heap.brk(base + PAGE + 1), base + PAGE + 1); + assert_eq!(unsafe { ((base + PAGE) as *const u8).read() }, 0); + assert_eq!(heap.brk(base - 1), base + PAGE + 1); + assert_eq!(heap.brk(base + HEAP_SIZE as u64 + 1), base + PAGE + 1); + assert_eq!(heap.brk(u64::MAX), base + PAGE + 1); } /// If `KEMU_SYSROOT` is set and `pathptr` is an absolute path whose @@ -122,7 +184,6 @@ mod hx { pub const MMAP: i64 = 9; pub const MPROTECT: i64 = 10; pub const MUNMAP: i64 = 11; - pub const BRK: i64 = 12; pub const RT_SIGACTION: i64 = 13; pub const RT_SIGPROCMASK: i64 = 14; pub const IOCTL: i64 = 16; @@ -214,7 +275,7 @@ pub fn dispatch(cpu: &mut CpuState, trace: bool) -> Option { nr::READLINKAT => { let mut a = a; a[1] = rewrite_path(a[1]); unsafe { host(hx::READLINKAT, a) } } nr::FACCESSAT => { let mut a = a; a[1] = rewrite_path(a[1]); unsafe { host(hx::FACCESSAT, a) } } nr::FACCESSAT2 => { let mut a = a; a[1] = rewrite_path(a[1]); unsafe { host(hx::FACCESSAT2, a) } } - nr::BRK => unsafe { host(hx::BRK, a) }, + nr::BRK => HEAP.with(|h| h.borrow_mut().brk(a[0])) as i64, nr::MMAP => unsafe { host(hx::MMAP, a) }, nr::MUNMAP => unsafe { host(hx::MUNMAP, a) }, nr::MPROTECT => unsafe { host(hx::MPROTECT, a) }, From 8c2cc8b1d1aa92ef3b50220653aeb496d98d69d8 Mon Sep 17 00:00:00 2001 From: losfair Date: Sun, 25 Oct 2026 03:29:28 -0700 Subject: [PATCH 2/3] jit: lower remaining scalar and SIMD floating-point families --- .github/workflows/ci.yml | 2 + README.md | 29 +- overlay/rust/kemu_core/src/jit/t1.rs | 11 +- overlay/rust/kemu_core/src/jit/t1/fp.rs | 146 +++--- .../rust/kemu_core/src/jit/t1/fp/convert.rs | 254 +++++++++++ .../rust/kemu_core/src/jit/t1/fp/estimate.rs | 216 +++++++++ overlay/rust/kemu_core/src/jit/t1/fp/extra.rs | 418 +++++++++++++++++ .../rust/kemu_core/src/jit/t1/fp/precision.rs | 180 ++++++++ overlay/rust/kemu_core/src/softfloat.rs | 2 +- tests/bench/fp.c | 21 + tools/jit-test/src/fp-catalog.txt | 431 ++++++++++++++++++ tools/jit-test/src/fp_all.rs | 131 ++++++ tools/jit-test/src/main.rs | 14 +- 13 files changed, 1749 insertions(+), 106 deletions(-) create mode 100644 overlay/rust/kemu_core/src/jit/t1/fp/convert.rs create mode 100644 overlay/rust/kemu_core/src/jit/t1/fp/estimate.rs create mode 100644 overlay/rust/kemu_core/src/jit/t1/fp/extra.rs create mode 100644 overlay/rust/kemu_core/src/jit/t1/fp/precision.rs create mode 100644 tools/jit-test/src/fp-catalog.txt create mode 100644 tools/jit-test/src/fp_all.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 89457ff..d7bd0c6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -57,6 +57,8 @@ jobs: NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 11 9 INTR=20 RELOC=1 $j 10000 12 10 NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 12 10 + INTR=20 RELOC=1 $j 10000 13 11 + NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 13 11 - name: kemu-user smoke test run: | u=tools/kemu-user/target/release/kemu-user diff --git a/README.md b/README.md index 7e96ba2..2a258fd 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,7 @@ INTR=50 RELOC=1 tools/jit-test/target/release/jit-test 20000 1 7 # AES, PMULL, S NO_AES=1 NO_PCLMUL=1 tools/jit-test/target/release/jit-test 20000 1 7 # optional-feature fallback INTR=20 RELOC=1 tools/jit-test/target/release/jit-test 10000 1 9 # FP min/max and indexed multiply/FMA INTR=20 RELOC=1 tools/jit-test/target/release/jit-test 10000 1 10 # TBL/TBX and byte/halfword ZIP/UZP +INTR=20 RELOC=1 tools/jit-test/target/release/jit-test 10000 1 11 # full FP catalog, exact status and native-path checks scripts/vm-smoke.sh # in-kernel ABI suite etc. ``` @@ -210,14 +211,15 @@ instruction including loop/operand overhead, and prints deterministic checksums. It is not a complete cipher or hash throughput test. `tests/bench/fp.c` exercises SAXPY, a double dot product, 48×48 matrix -multiplication, clamping, range counting, and Mandelbrot. `tests/bench/zlib.c` +multiplication, clamping, range counting, Mandelbrot, SIMD square roots, +quantization, and rounding. `tests/bench/zlib.c` measures compression (level 6), decompression, Adler-32, and CRC-32 over deterministic mixed text/random data, and verifies the compression round trip. Both accept a case name (or `all`) and iteration count; zlib also accepts the buffer size in bytes (up to its 1 MiB default). ```sh -aarch64-linux-gnu-gcc -O3 -static tests/bench/fp.c -lm -o build/fp.arm64 +aarch64-linux-gnu-gcc -O3 -fno-math-errno -static tests/bench/fp.c -lm -o build/fp.arm64 # ZLIB_ARM64 is an extracted arm64 zlib development package or sysroot. aarch64-linux-gnu-gcc -O3 -static tests/bench/zlib.c -I$ZLIB_ARM64/usr/include \ $ZLIB_ARM64/usr/lib/aarch64-linux-gnu/libz.a -o build/zlib.arm64 @@ -232,9 +234,26 @@ against QEMU. For a native comparison, compile the same sources with host GCC and the same optimization level, linking the same zlib version, and run them in the same VM. Compiler vectorization can differ across architectures. Run helper profiling separately with `KEMU_JIT_STATS=1`; its per-helper -formatting and counters distort timings. FP min/max and indexed multiply/FMA -retain softfloat fallbacks for NaNs and FPCR.FZ, preserving their exact results -and exception flags. +formatting and counters distort timings. + +T1 lowers every currently decoded S/D FP instruction family: scalar and SIMD +arithmetic, comparisons (including conditional comparisons), rounding, +pairwise/across-lane reductions, integer/fixed-point conversions, precision +conversions, reciprocal estimates, and reciprocal refinement steps. Half +precision **storage conversions** are lowered; FP16 arithmetic is unsupported. +Packed signed int32 conversions stay packed on AVX2; 64-bit/unsigned conversion +forms use native scalar lanes and commit the vector once. Reciprocal estimates +use ARM's exact tables embedded in the translated block. Unusual inputs use +softfloat where host behavior differs: NaN propagation, FPCR.FZ/AHP, +saturation/modulo cases, and underflow/overflow boundaries. FCVTXN also uses the +helper outside its normal-result fast range. Thus instruction coverage does +not imply every input executes without a helper. + +Mode 11 iterates the readable opcode catalog in `tools/jit-test/src/fp-catalog.txt`. +It checks all four rounding modes, scalar/vector arrangements, aliasing, exact +FPCR/FPSR/NZCV, exceptional inputs and inactive lanes. Ordinary-input cases +must execute without helper calls (apart from the terminating BRK). Run with +`NO_VCACHE=1` as well to check state-page and cached-vector paths. `tests/jit-workloads/run.sh` runs JIT-heavy programs for Java, Node, LuaJIT, PyPy and Ruby inside an arm64 userland, each with the guest's JIT on and off, diff --git a/overlay/rust/kemu_core/src/jit/t1.rs b/overlay/rust/kemu_core/src/jit/t1.rs index e5dd088..465cec2 100644 --- a/overlay/rust/kemu_core/src/jit/t1.rs +++ b/overlay/rust/kemu_core/src/jit/t1.rs @@ -280,6 +280,7 @@ struct Gen<'s, 'b> { body_off: usize, vectors: [u8; meta::VECTOR_REGS], vlive: bool, + fp_tables: [Option; 2], } pub const MAX_REMAP: usize = 4; @@ -2215,7 +2216,8 @@ fn defs_flags(i: &Insn) -> bool { | Insn::AddSubExt { setflags, .. } | Insn::AddSubCarry { setflags, .. } => *setflags, Insn::LogicImm { op, .. } | Insn::LogicShift { op, .. } => *op == LogicOp::Ands, - Insn::CondCmp { .. } | Insn::FpCmp { .. } => true, + Insn::CondCmp { .. } | Insn::FpCmp { .. } | Insn::FpCCmp { .. } + | Insn::FpCvt { op: crate::decode::FcvtOp::Fjcvt, .. } => true, _ => false, } } @@ -2229,8 +2231,6 @@ fn is_native(i: &Insn) -> bool { | Insn::Hlt { .. } | Insn::Brk { .. } | Insn::IcIvau { .. } - | Insn::FpCCmp { .. } - | Insn::SimdAcross { .. } | Insn::SimdSmov { .. } | Insn::Rmif { .. } | Insn::SetfFlags { .. } @@ -2373,6 +2373,7 @@ fn translate_n(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut vector::choose(&insns[..n.min(MAX_INSNS)]) } else { [meta::NO_VECTOR; meta::VECTOR_REGS] }, vlive: false, + fp_tables: [None; 2], live_in0, }; if jit_off() & 32 == 0 { @@ -2425,9 +2426,11 @@ fn translate_n(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut | Insn::Fp3 { .. } | Insn::Fp1 { .. } | Insn::FpCmp { .. } + | Insn::FpCCmp { .. } | Insn::FpCSel { .. } | Insn::FpCvt { .. } | Insn::SimdThree { .. } + | Insn::SimdAcross { .. } | Insn::SimdIndexed { .. } | Insn::SimdMisc { .. } | Insn::SimdDup { .. } @@ -2602,9 +2605,11 @@ fn translate_n(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut Insn::Fp3 { ftype, neg, sub, rd, rn, rm, ra } => g.fp3(ftype, neg, sub, rd, rn, rm, ra), Insn::Fp1 { ftype, op, to, rd, rn } => g.fp1(ftype, op, to, rd, rn), Insn::FpCmp { ftype, rn, rm, zero, signal } => g.fp_cmp(ftype, rn, rm, zero, signal), + Insn::FpCCmp { ftype, rn, rm, nzcv, cond, signal } => g.fp_ccmp(ftype, rn, rm, nzcv, cond, signal), Insn::FpCSel { ftype, rd, rn, rm, cond } => g.fp_csel(ftype, rd, rn, rm, cond), Insn::FpCvt { ftype, sf, op, rd, rn } => g.fp_cvt(ftype, sf, op, rd, rn), Insn::SimdThree { q, u, size, op, rd, rn, rm, scalar } => g.simd_three(q, u, size, op, rd, rn, rm, scalar), + Insn::SimdAcross { q, size, op, rd, rn } => g.fp_across(q, size, op, rd, rn), Insn::SimdIndexed { q, u, size, opcode, rd, rn, rm, index, scalar } => g.simd_indexed(q, u, size, opcode, rd, rn, rm, index, scalar), Insn::SimdMisc { q, u, size, op, rd, rn, scalar } => g.simd_misc(q, u, size, op, rd, rn, scalar), Insn::SimdDup { q, imm5, rd, rn, from_gpr } => g.simd_dup(q, imm5, rd, rn, from_gpr), diff --git a/overlay/rust/kemu_core/src/jit/t1/fp.rs b/overlay/rust/kemu_core/src/jit/t1/fp.rs index 52429f7..f9cfe62 100644 --- a/overlay/rust/kemu_core/src/jit/t1/fp.rs +++ b/overlay/rust/kemu_core/src/jit/t1/fp.rs @@ -3,14 +3,33 @@ //! instructions to SSE/AVX2 (VEX-encoded). //! //! V registers live in the cache or state page. Results use scratch -//! XMM registers (12..15) and commit in a single host instruction, so +//! XMM registers (8..15) and commit in a single host instruction, so //! every instruction stays restartable. Bit-exactness: whenever a result //! could differ from AArch64 semantics (NaN results, out-of-range //! conversions), the fast path branches to the helper, which re-executes //! the instruction in the exact softfloat interpreter. use super::*; -use crate::decode::{FcvtOp, Fp1Op, Fp2Op, Index, Simd2Op, SimdOp}; +use crate::decode::{AcrossOp, FcvtOp, Fp1Op, Fp2Op, Index, Simd2Op, SimdOp}; + +mod extra; +mod convert; +mod precision; +mod estimate; + +struct Slow { + branches: [usize; 32], + len: usize, +} + +impl Slow { + fn new() -> Self { Self { branches: [0; 32], len: 0 } } + fn push(&mut self, branch: usize) { + self.branches[self.len] = branch; + self.len += 1; + } + fn as_slice(&self) -> &[usize] { &self.branches[..self.len] } +} const X13: u8 = 13; const X14: u8 = 14; @@ -239,6 +258,17 @@ impl<'s, 'b> Gen<'s, 'b> { } pub(super) fn fp1(&mut self, ftype: u8, op: Fp1Op, to: u8, rd: u32, rn: u32) -> bool { + if op == Fp1Op::Recpx && ftype <= 1 { + return self.fp_estimate(false, 2 + ftype, true, Simd2Op::Frecpx, rd, rn); + } + if op == Fp1Op::Cvt { + let from = match ftype { 0 => 2, 1 => 3, 3 => 1, _ => return false }; + let to = match to { 0 => 2, 1 => 3, 2 => 1, _ => return false }; + return self.fp_precision(from, to, true, false, false, rd, rn); + } + if matches!(op, Fp1Op::RintA | Fp1Op::Rint32Z | Fp1Op::Rint32X | Fp1Op::Rint64Z | Fp1Op::Rint64X) && ftype <= 1 { + return self.fp_round_insn(ftype == 1, true, false, op, rd, rn); + } let double = match ftype { 0 => false, 1 => true, @@ -289,16 +319,6 @@ impl<'s, 'b> Gen<'s, 'b> { self.slow_paths(&[s]); true } - Fp1Op::Cvt if to <= 1 && to != ftype => { - // S <-> D - self.fp_prologue(); - self.ld_scalar(double, X14, rn); - self.a.vex(1, if double { PP_F2 } else { PP_F3 }, false, false, 0x5a, X14, X14, Rm::R(X14)); - let s = self.nan_guard_scalar(!double, X14); - self.st_scalar(!double, X14, rd); - self.slow_paths(&[s]); - true - } _ => false, } } @@ -400,84 +420,7 @@ impl<'s, 'b> Gen<'s, 'b> { } true } - FcvtOp::ItoF { unsigned } => { - self.fp_prologue(); - let src = self.get(rn, false, RDX); - let mut slow = None; - let src64 = if !sf { - // 32-bit source: extend to 64 bits (exact for both). - if unsigned { - self.a.mov_rr(Sz::D, RDX, src); - } else { - self.a.movsx(Sz::D, Sz::Q, RDX, Rm::R(src)); - } - RDX - } else { - if unsigned { - self.clobber(); - self.a.test_rr(Sz::Q, src, src); - slow = Some(self.a.jcc(Cc::S, None)); - } - src - }; - self.a.vex(1, PP_66, false, false, 0xef, X14, X14, Rm::R(X14)); // zero - // vcvtsi2sd/ss x14, x14, r64 - self.a.vex(1, if double { PP_F2 } else { PP_F3 }, true, false, 0x2a, X14, X14, Rm::R(src64)); - self.st_scalar(double, X14, rd); - match slow { - Some(s) => self.slow_paths(&[s]), - None => {} - } - true - } - FcvtOp::FtoI { round, unsigned } => { - if round > 3 { - return false; - } - self.fp_prologue(); - self.clobber(); - self.ld_scalar(double, X14, rn); - if round != 3 { - let imm = match round { - 0 => 8, - 1 => 10, - _ => 9, - }; - self.a.vex_ib(3, PP_66, false, false, if double { 0x0b } else { 0x0a }, X14, X14, Rm::R(X14), imm); - } - // Signed 64-bit conversion, then range checks. - let wide = sf || unsigned; - self.a.vex(1, if double { PP_F2 } else { PP_F3 }, wide, false, 0x2c, RAX, 0, Rm::R(X14)); - let mut slow = [0usize; 2]; - let ns; - if !unsigned { - // indefinite = INT_MIN of the destination width - if sf { - self.a.mov_ri(RDX, 1u64 << 63); - self.a.alu_rr(Alu::Cmp, Sz::Q, RAX, RDX); - } else { - self.a.alu_ri(Alu::Cmp, Sz::D, Rm::R(RAX), i32::MIN); - } - slow[0] = self.a.jcc(Cc::E, None); - ns = 1; - } else if sf { - self.a.test_rr(Sz::Q, RAX, RAX); - slow[0] = self.a.jcc(Cc::S, None); - ns = 1; - } else { - self.a.mov_rr(Sz::Q, RDX, RAX); - self.a.shift_ri(Shift::Shr, Sz::Q, RDX, 32); - slow[0] = self.a.jcc(Cc::Ne, None); - ns = 1; - } - if !sf { - self.a.mov_rr(Sz::D, RAX, RAX); - } - self.put(rd, false, RAX); - self.slow_paths(&slow[..ns]); - true - } - _ => false, + _ => self.fp_cvt_int(double, sf, op, rd, rn), } } @@ -487,7 +430,7 @@ impl<'s, 'b> Gen<'s, 'b> { /// the accumulator, so all source/destination aliases are safe. #[allow(clippy::too_many_arguments)] pub(super) fn simd_indexed(&mut self, q: bool, u: bool, size: u8, opcode: u8, rd: u32, rn: u32, rm: u32, index: u32, scalar: bool) -> bool { - if u || !matches!(size, 2 | 3) || !matches!(opcode, 1 | 5 | 9) || (size == 3 && !q && !scalar) { + if (u && opcode != 9) || !matches!(size, 2 | 3) || !matches!(opcode, 1 | 5 | 9) || (size == 3 && !q && !scalar) { return false; } let double = size == 3; @@ -504,6 +447,8 @@ impl<'s, 'b> Gen<'s, 'b> { } else { self.ld_vec(X15, rn); } + let mut exceptional = Slow::new(); + if u { exceptional.push(self.fp_finite(double, X15)); exceptional.push(self.fp_finite(double, 12)); } if opcode == 9 { let pp = if scalar { if double { PP_F2 } else { PP_F3 } } else if double { PP_66 } else { PP_NONE }; self.a.vex(1, pp, false, false, 0x59, X14, X15, Rm::R(12)); @@ -518,13 +463,15 @@ impl<'s, 'b> Gen<'s, 'b> { let opc = (if opcode == 1 { 0xb8 } else { 0xbc }) + scalar as u8; self.a.vex(2, PP_66, double, false, opc, X14, X15, Rm::R(12)); } + if u { exceptional.push(self.fp_underflow_boundary(double, X14)); } let slow = if scalar { self.nan_guard_scalar(double, X14) } else { self.nan_guard_vec(double, X14) }; if scalar { self.st_scalar(double, X14, rd); } else { self.st_vec(q, X14, rd); } - self.slow_paths(&[fz, slow]); + exceptional.push(fz); exceptional.push(slow); + self.slow_paths(exceptional.as_slice()); true } @@ -536,6 +483,15 @@ impl<'s, 'b> Gen<'s, 'b> { } pub(super) fn simd_three(&mut self, q: bool, u: bool, size: u8, op: SimdOp, rd: u32, rn: u32, rm: u32, scalar: bool) -> bool { + if matches!(op, SimdOp::FAbd | SimdOp::FMulx | SimdOp::FRecps | SimdOp::FRsqrts) { + return self.fp_special(q, size, scalar, op, rd, rn, rm); + } + if matches!(op, SimdOp::FCmEq | SimdOp::FCmGe | SimdOp::FCmGt | SimdOp::FAcGe | SimdOp::FAcGt) { + return self.fp_compare_mask(q, size, op, rd, rn, Some(rm), scalar, false); + } + if matches!(op, SimdOp::FAddP | SimdOp::FMinP | SimdOp::FMaxP | SimdOp::FMinNmP | SimdOp::FMaxNmP) { + return self.fp_pair(q, size, op, rd, rn, rm); + } if scalar { return false; } @@ -877,6 +833,9 @@ impl<'s, 'b> Gen<'s, 'b> { } pub(super) fn simd_misc(&mut self, q: bool, u: bool, size: u8, op: Simd2Op, rd: u32, rn: u32, scalar: bool) -> bool { + if let Some(result) = self.fp_misc_extra(q, size, op, rd, rn, scalar) { + return result; + } if scalar { return false; } @@ -1005,6 +964,9 @@ impl<'s, 'b> Gen<'s, 'b> { let esize = 31 - immh.leading_zeros(); // log2 bytes let ebits = 8u32 << esize; let immhb = (immh << 3) | immb; + if esize >= 2 && matches!(opcode, 28 | 31) { + return self.fp_simd_int(q, esize as u8, scalar, opcode == 28, u, 3, 2 * ebits - immhb, rd, rn); + } // Scalar SHL Dd,Dn,#imm is used in GHASH reduction. Other scalar // shift families still use the interpreter. if scalar && !(opcode == 0b01010 && !u && esize == 3) { diff --git a/overlay/rust/kemu_core/src/jit/t1/fp/convert.rs b/overlay/rust/kemu_core/src/jit/t1/fp/convert.rs new file mode 100644 index 0000000..a2dbc1d --- /dev/null +++ b/overlay/rust/kemu_core/src/jit/t1/fp/convert.rs @@ -0,0 +1,254 @@ +// SPDX-License-Identifier: GPL-2.0 +//! Integer, fixed-point and precision conversions. Freeze the source in +//! X8, accumulate in X9, and publish only after every lane has succeeded. +use super::*; + +impl<'s, 'b> Gen<'s, 'b> { + pub(super) fn fp_lane(&mut self, bytes: u8, lane: u8) { + self.a.vex_ib(1, PP_66, false, false, 0x73, 3, X14, Rm::R(8), bytes * lane); + self.a.vmov_to_gpr(if bytes == 8 { Sz::Q } else { Sz::D }, RAX, X14); + if bytes == 2 { + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RAX), 0xffff); + } + self.a.vmov_from_gpr(if bytes == 8 { Sz::Q } else { Sz::D }, X14, RAX); + } + + pub(super) fn fp_pack(&mut self, bytes: u8, lane: u8) { + // Conversion instructions may preserve source bits above the scalar. + self.a.vmov_to_gpr(if bytes == 8 { Sz::Q } else { Sz::D }, RAX, X14); + if bytes == 2 { + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RAX), 0xffff); + } + self.a.vmov_from_gpr(if bytes == 8 { Sz::Q } else { Sz::D }, X14, RAX); + self.a.vex_ib(1, PP_66, false, false, 0x73, 7, X14, Rm::R(X14), bytes * lane); + self.a.vex(1, PP_66, false, false, 0xeb, 9, 9, Rm::R(X14)); + } + + /// RAX integer -> X14 FP. The sticky low bit makes the high-u64 path + /// exact under all four rounding modes, including halfway cases. + fn fp_from_int(&mut self, double: bool, wide: bool, unsigned: bool, fbits: u32) { + self.fp_zero(X14); + if !wide { + if unsigned { + self.a.mov_rr(Sz::D, RAX, RAX); + } else { + self.a.movsx(Sz::D, Sz::Q, RAX, Rm::R(RAX)); + } + } + let pp = if double { PP_F2 } else { PP_F3 }; + if wide && unsigned { + self.a.test_rr(Sz::Q, RAX, RAX); + let small = self.a.jcc(Cc::Ns, None); + self.a.mov_rr(Sz::Q, RDX, RAX); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RDX), 1); + self.a.shift_ri(Shift::Shr, Sz::Q, RAX, 1); + self.a.alu_rr(Alu::Or, Sz::Q, RAX, RDX); + self.a.vex(1, pp, true, false, 0x2a, X14, X14, Rm::R(RAX)); + self.a.vex(1, pp, false, false, 0x58, X14, X14, Rm::R(X14)); + let done = self.a.jmp(None); + self.a.bind32(small); + self.a.vex(1, pp, true, false, 0x2a, X14, X14, Rm::R(RAX)); + self.a.bind32(done); + } else { + self.a.vex(1, pp, true, false, 0x2a, X14, X14, Rm::R(RAX)); + } + if fbits != 0 { + self.fp_power(X15, double, -(fbits as i32)); + self.a.vex(1, pp, false, false, 0x59, X14, X14, Rm::R(X15)); + } + } + + /// X14 FP -> RAX integer. Round without exceptions, validate, then + /// accrue IXC. Invalid/saturating conversions must raise IOC alone. + fn fp_to_int(&mut self, double: bool, wide: bool, unsigned: bool, round: u8, fbits: u32, slow: &mut Slow) { + slow.push(self.nan_guard_scalar(double, X14)); + if fbits != 0 { + self.fp_bits(12, double, if double { 0x7fff_ffff_ffff_ffff } else { 0x7fff_ffff }); + self.a.vex(1, PP_66, false, false, 0xdb, 12, X14, Rm::R(12)); + self.fp_power(X15, double, (if wide { 64 } else { 32 }) - fbits as i32); + self.a + .vex(1, if double { PP_66 } else { PP_NONE }, false, false, 0x2e, 12, 0, Rm::R(X15)); + slow.push(self.a.jcc(Cc::Ae, None)); + self.fp_power(X15, double, fbits as i32); + self.a + .vex(1, if double { PP_F2 } else { PP_F3 }, false, false, 0x59, X14, X14, Rm::R(X15)); + } + self.a.vex(1, PP_F3, false, false, 0x6f, 11, 0, Rm::R(X14)); + if round == 4 { + self.fp_round_away(double); + } else { + let imm = [8, 10, 9, 11][round as usize]; + self.a + .vex_ib(3, PP_66, false, false, if double { 0x0b } else { 0x0a }, X14, X14, Rm::R(X14), imm); + } + self.fp_range(double, if wide { 64 } else { 32 }, unsigned, slow); + self.fp_inexact(double); + let pp = if double { PP_F2 } else { PP_F3 }; + if wide && unsigned { + self.fp_power(X15, double, 63); + self.a + .vex(1, if double { PP_66 } else { PP_NONE }, false, false, 0x2e, X14, 0, Rm::R(X15)); + let small = self.a.jcc(Cc::B, None); + self.a.vex(1, pp, false, false, 0x5c, X14, X14, Rm::R(X15)); + self.a.vex(1, pp, true, false, 0x2c, RAX, 0, Rm::R(X14)); + self.mov_imm(RDX, 1 << 63); + self.a.alu_rr(Alu::Or, Sz::Q, RAX, RDX); + let done = self.a.jmp(None); + self.a.bind32(small); + self.a.vex(1, pp, true, false, 0x2c, RAX, 0, Rm::R(X14)); + self.a.bind32(done); + } else { + self.a.vex(1, pp, wide || unsigned, false, 0x2c, RAX, 0, Rm::R(X14)); + } + if !wide { + self.a.mov_rr(Sz::D, RAX, RAX); + } + } + + pub(super) fn fp_cvt_int(&mut self, double: bool, wide: bool, op: FcvtOp, rd: u32, rn: u32) -> bool { + let (to_fp, unsigned, round, fbits) = match op { + FcvtOp::ItoF { unsigned } => (true, unsigned, 0, 0), + FcvtOp::FixedToF { unsigned, fbits } => (true, unsigned, 0, fbits), + FcvtOp::FtoI { unsigned, round } => (false, unsigned, round, 0), + FcvtOp::FtoFixed { unsigned, fbits } => (false, unsigned, 3, fbits), + FcvtOp::Fjcvt => return self.fp_jcvt(rd, rn), + _ => return false, + }; + self.fp_prologue(); + self.clobber(); + let mut slow = Slow::new(); + if to_fp { + let r = self.get(rn, false, RAX); + self.a.mov_rr(Sz::Q, RAX, r); + self.fp_from_int(double, wide, unsigned, fbits); + self.st_scalar(double, X14, rd); + } else { + slow.push(self.fp_fz_guard()); + self.ld_scalar(double, X14, rn); + self.fp_to_int(double, wide, unsigned, round, fbits, &mut slow); + self.put(rd, false, RAX); + } + if slow.len != 0 { + self.slow_paths(slow.as_slice()); + } + true + } + + /// Common signed int32 vectors have native packed conversions. Keep + /// these packed through rounding/range checks instead of scalarizing. + fn fp_simd_signed32(&mut self, q: bool, scalar: bool, to_fp: bool, round: u8, fbits: u32, rd: u32, rn: u32) -> bool { + self.fp_prologue(); + let mut slow = Slow::new(); + if !to_fp { + slow.push(self.fp_fz_guard()); + } + self.fp_active(X14, false, scalar, q, rn); + if to_fp { + self.a.vex(1, PP_NONE, false, false, 0x5b, X14, 0, Rm::R(X14)); + if fbits != 0 { + self.fp_power(X15, false, -(fbits as i32)); + self.a.vex(1, PP_NONE, false, false, 0x59, X14, X14, Rm::R(X15)); + } + } else { + slow.push(self.nan_guard_vec(false, X14)); + if fbits != 0 { + self.fp_bits(12, false, 0x7fff_ffff); + self.a.vex(1, PP_66, false, false, 0xdb, 12, X14, Rm::R(12)); + self.fp_power(X15, false, 32 - fbits as i32); + self.a.vex_ib(1, PP_NONE, false, false, 0xc2, X13, 12, Rm::R(X15), 13); + slow.push(self.fp_test_mask(X13)); + self.fp_power(X15, false, fbits as i32); + self.a.vex(1, PP_NONE, false, false, 0x59, X14, X14, Rm::R(X15)); + } + self.a.vex(1, PP_F3, false, false, 0x6f, 11, 0, Rm::R(X14)); + if round == 4 { + self.fp_round_away(false); + } else { + self.a + .vex_ib(3, PP_66, false, false, 0x08, X14, 0, Rm::R(X14), [8, 10, 9, 11][round as usize]); + } + self.fp_range(false, 32, false, &mut slow); + self.fp_inexact(false); + self.a.vex(1, PP_F3, false, false, 0x5b, X14, 0, Rm::R(X14)); + } + self.fp_commit(false, scalar, q, rd); + if slow.len != 0 { + self.slow_paths(slow.as_slice()); + } + true + } + + #[allow(clippy::too_many_arguments)] + pub(super) fn fp_simd_int( + &mut self, + q: bool, + size: u8, + scalar: bool, + to_fp: bool, + unsigned: bool, + round: u8, + fbits: u32, + rd: u32, + rn: u32, + ) -> bool { + if size < 2 { + return false; + } + if size == 2 && !unsigned { + return self.fp_simd_signed32(q, scalar, to_fp, round, fbits, rd, rn); + } + let double = size == 3; + self.fp_prologue(); + self.clobber(); + let mut slow = Slow::new(); + if !to_fp { + slow.push(self.fp_fz_guard()); + } + self.ld_vec(8, rn); + self.fp_zero(9); + let bytes = 1 << size; + let lanes = if scalar { 1 } else { (if q { 16 } else { 8 }) / bytes }; + for lane in 0..lanes { + self.fp_lane(bytes, lane); + if to_fp { + self.fp_from_int(double, double, unsigned, fbits); + } else { + self.fp_to_int(double, double, unsigned, round, fbits, &mut slow); + self.a.vmov_from_gpr(if double { Sz::Q } else { Sz::D }, X14, RAX); + } + self.fp_pack(bytes, lane); + } + self.store_vec(rd, 9); + if slow.len != 0 { + self.slow_paths(slow.as_slice()); + } + true + } + + fn fp_jcvt(&mut self, rd: u32, rn: u32) -> bool { + self.fp_prologue(); + let mut slow = Slow::new(); + slow.push(self.fp_fz_guard()); + self.ld_scalar(true, X14, rn); + slow.push(self.nan_guard_scalar(true, X14)); + // The uncommon modulo-2^32 cases stay on the exact helper. + self.a.vex(1, PP_F2, false, false, 0x2c, RAX, 0, Rm::R(X14)); + self.a.alu_ri(Alu::Cmp, Sz::D, Rm::R(RAX), i32::MIN); + slow.push(self.a.jcc(Cc::E, None)); + self.fp_zero(X15); + self.a.vex(1, PP_F2, false, false, 0x2a, X15, X15, Rm::R(RAX)); + // Bit equality also distinguishes -0 (Z=0) from +0 (Z=1). + self.a.vex(2, PP_66, false, false, 0x29, X15, X15, Rm::R(X14)); + self.a.vmov_to_gpr(Sz::D, RDX, X15); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RDX), 1 << 14); + self.put(rd, false, RAX); + self.a.store(Sz::W, Mem::gs(OFF_FLAGS_X86), RDX); + self.fl = Flags { + host: false, + inv: false, + mem: true, + }; + self.slow_paths(slow.as_slice()); + true + } +} diff --git a/overlay/rust/kemu_core/src/jit/t1/fp/estimate.rs b/overlay/rust/kemu_core/src/jit/t1/fp/estimate.rs new file mode 100644 index 0000000..0491d60 --- /dev/null +++ b/overlay/rust/kemu_core/src/jit/t1/fp/estimate.rs @@ -0,0 +1,216 @@ +// SPDX-License-Identifier: GPL-2.0 +//! ARM reciprocal estimates use specified lookup values, not x86 RCP/RSQRT +//! approximations. Embed each table once per block; RIP addressing survives +//! code-cache relocation without an external pointer or relocation entry. +use super::*; + +impl<'s, 'b> Gen<'s, 'b> { + fn fp_estimate_table(&mut self, sqrt: bool) -> usize { + let which = sqrt as usize; + if let Some(off) = self.fp_tables[which] { + return off; + } + let skip = self.a.jmp(None); + let off = self.a.pos; + if sqrt { + for v in crate::softfloat::RSQRT_TABLE { + self.a.u16(v); + } + } else { + for a in 256..512 { + self.a.u16((((1u32 << 19) / (2 * a + 1) + 1) / 2) as u16); + } + } + self.a.bind32(skip); + self.fp_tables[which] = Some(off); + off + } + + pub(super) fn fp_estimate(&mut self, q: bool, size: u8, scalar: bool, op: Simd2Op, rd: u32, rn: u32) -> bool { + if size < 2 { + return false; + } + let double = size == 3; + let sqrt = op == Simd2Op::Frsqrte; + let exp_only = op == Simd2Op::Frecpx; + let fb = if double { 52 } else { 23 }; + let emax = if double { 2047 } else { 255 }; + self.fp_prologue(); + self.clobber(); + let mut slow = Slow::new(); + slow.push(self.fp_fz_guard()); + let table = if exp_only { 0 } else { self.fp_estimate_table(sqrt) }; + self.ld_vec(8, rn); + self.fp_zero(9); + let lanes = if scalar { 1 } else { (if q { 16 } else { 8 }) >> size }; + for i in 0..lanes { + self.fp_lane(1 << size, i); + if exp_only { + slow.push(self.nan_guard_scalar(double, X14)); + } + self.a.vmov_to_gpr(Sz::Q, RAX, X14); + self.a.vmov_from_gpr(Sz::Q, 11, RAX); + if sqrt { + self.a.test_rr(if double { Sz::Q } else { Sz::D }, RAX, RAX); + slow.push(self.a.jcc(Cc::S, None)); + } + self.a.shift_ri(Shift::Shr, Sz::Q, RAX, fb); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RAX), emax); + if exp_only { + let normal = self.a.jcc(Cc::Ne, None); + self.a.mov_ri32(RAX, 1); + self.a.bind32(normal); + self.a.alu_ri(Alu::Xor, Sz::D, Rm::R(RAX), emax); + } else { + slow.push(self.a.jcc(Cc::E, None)); + self.a.alu_ri(Alu::Cmp, Sz::D, Rm::R(RAX), if sqrt { emax } else { emax - 2 }); + slow.push(self.a.jcc(Cc::Ae, None)); + self.a.vmov_from_gpr(Sz::D, 10, RAX); // original exponent/parity + self.a.mov_ri32( + RDX, + if sqrt { + if double { + 3068 + } else { + 380 + } + } else { + (emax - 2) as u32 + }, + ); + self.a.alu_rr(Alu::Sub, Sz::D, RDX, RAX); + if sqrt { + self.a.shift_ri(Shift::Shr, Sz::D, RDX, 1); + } + self.a.mov_rr(Sz::D, RAX, RDX); + } + self.a.shift_ri(Shift::Shl, Sz::Q, RAX, fb); + self.a.vmov_from_gpr(Sz::Q, 12, RAX); // output exponent + if !exp_only { + self.a.vmov_to_gpr(Sz::Q, RAX, 11); + self.a.shift_ri(Shift::Shr, Sz::Q, RAX, fb - 8); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RAX), 255); + if sqrt { + self.a.vmov_to_gpr(Sz::D, RDX, 10); + self.a.test_ri(Sz::D, Rm::R(RDX), 1); + let even = self.a.jcc(Cc::E, None); + self.a.shift_ri(Shift::Shr, Sz::D, RAX, 1); + let indexed = self.a.jmp(None); + self.a.bind32(even); + self.a.alu_ri(Alu::Add, Sz::D, Rm::R(RAX), 128); + self.a.bind32(indexed); + } + self.a.lea(Sz::Q, RDX, Mem::rip(table as i32)); + self.a.movzx(Sz::W, RAX, Rm::M(Mem::bisd(RDX, RAX, 1, 0))); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RAX), 255); + self.a.shift_ri(Shift::Shl, Sz::Q, RAX, fb - 8); + self.a.vmov_to_gpr(Sz::Q, RDX, 12); + self.a.alu_rr(Alu::Or, Sz::Q, RAX, RDX); + } else { + self.a.vmov_to_gpr(Sz::Q, RAX, 12); + } + if !sqrt { + self.a.vmov_to_gpr(Sz::Q, RDX, 11); + self.a.shift_ri(Shift::Shr, Sz::Q, RDX, if double { 63 } else { 31 }); + self.a.shift_ri(Shift::Shl, Sz::Q, RDX, if double { 63 } else { 31 }); + self.a.alu_rr(Alu::Or, Sz::Q, RAX, RDX); + } + self.a.vmov_from_gpr(Sz::Q, X14, RAX); + self.fp_pack(1 << size, i); + } + self.store_vec(rd, 9); + self.slow_paths(slow.as_slice()); + true + } + + /// Guard before arithmetic: FMULX and reciprocal-step instructions + /// define 0*infinity without IOC, unlike an ordinary host multiply. + pub(super) fn fp_finite(&mut self, double: bool, x: u8) -> usize { + self.fp_bits(10, double, if double { 0x7ff0_0000_0000_0000 } else { 0x7f80_0000 }); + self.a.vex(1, PP_66, false, false, 0xdb, X13, x, Rm::R(10)); + self.a.vex( + if double { 2 } else { 1 }, + PP_66, + false, + false, + if double { 0x29 } else { 0x76 }, + X13, + X13, + Rm::R(10), + ); + self.fp_test_mask(X13) + } + + pub(super) fn fp_special(&mut self, q: bool, size: u8, scalar: bool, op: SimdOp, rd: u32, rn: u32, rm: u32) -> bool { + if size < 2 { + return false; + } + let double = size == 3; + self.fp_prologue(); + let mut slow = Slow::new(); + slow.push(self.fp_fz_guard()); + self.fp_active(X14, double, scalar, q, rn); + self.fp_active(X15, double, scalar, q, rm); + let pp = if scalar { + if double { + PP_F2 + } else { + PP_F3 + } + } else if double { + PP_66 + } else { + PP_NONE + }; + if op == SimdOp::FAbd { + self.a.vex(1, pp, false, false, 0x5c, X14, X14, Rm::R(X15)); + self.fp_abs_bits(double, X14); + slow.push(self.fp_nan(double, scalar, X14)); + } else { + slow.push(self.fp_finite(double, X14)); + slow.push(self.fp_finite(double, X15)); + if op == SimdOp::FMulx { + self.a.vex(1, pp, false, false, 0x59, X14, X14, Rm::R(X15)); + } else { + if op == SimdOp::FRsqrts { + // Halve one factor exactly before the fused operation. + // Subnormal/tiny inputs need the softfloat path. + self.fp_bits(10, double, if double { 0x7fff_ffff_ffff_ffff } else { 0x7fff_ffff }); + self.a.vex(1, PP_66, false, false, 0xdb, 10, X14, Rm::R(10)); + self.fp_power(12, double, if double { -1021 } else { -125 }); + self.a + .vex_ib(1, if double { PP_66 } else { PP_NONE }, false, false, 0xc2, X13, 10, Rm::R(12), 1); + // Ignore inactive lanes and genuine zero factors. + self.fp_zero(12); + self.a + .vex_ib(1, if double { PP_66 } else { PP_NONE }, false, false, 0xc2, 10, 10, Rm::R(12), 4); + self.a.vex(1, PP_66, false, false, 0xdb, X13, X13, Rm::R(10)); + slow.push(self.fp_test_mask(X13)); + self.fp_power(12, double, -1); + self.a.vex(1, pp, false, false, 0x59, X14, X14, Rm::R(12)); + } + self.fp_bits( + 12, + double, + if op == SimdOp::FRecps { + if double { + 0x4000_0000_0000_0000 + } else { + 0x4000_0000 + } + } else if double { + 0x3ff8_0000_0000_0000 + } else { + 0x3fc0_0000 + }, + ); + self.a.vex(2, PP_66, double, false, 0xbc + scalar as u8, 12, X14, Rm::R(X15)); // VFN MADD231 + self.a.vex(1, PP_F3, false, false, 0x6f, X14, 0, Rm::R(12)); + } + } + slow.push(self.fp_underflow_boundary(double, X14)); + self.fp_commit(double, scalar, q, rd); + self.slow_paths(slow.as_slice()); + true + } +} diff --git a/overlay/rust/kemu_core/src/jit/t1/fp/extra.rs b/overlay/rust/kemu_core/src/jit/t1/fp/extra.rs new file mode 100644 index 0000000..94cd2ec --- /dev/null +++ b/overlay/rust/kemu_core/src/jit/t1/fp/extra.rs @@ -0,0 +1,418 @@ +// SPDX-License-Identifier: GPL-2.0 +//! FP comparisons, unary operations, rounding and horizontal operations. +use super::*; + +impl<'s, 'b> Gen<'s, 'b> { + pub(super) fn fp_zero(&mut self, x: u8) { + self.a.vex(1, PP_66, false, false, 0xef, x, x, Rm::R(x)); + } + + pub(super) fn fp_bits(&mut self, x: u8, double: bool, bits: u64) { + self.mov_imm(RAX, bits); + self.a.vmov_from_gpr(Sz::Q, x, RAX); + self.a.vex(2, PP_66, false, false, if double { 0x59 } else { 0x58 }, x, 0, Rm::R(x)); + } + + pub(super) fn fp_power(&mut self, x: u8, double: bool, exponent: i32) { + self.fp_bits( + x, + double, + if double { + ((1023 + exponent) as u64) << 52 + } else { + ((127 + exponent) as u64) << 23 + }, + ); + } + + pub(super) fn fp_active(&mut self, x: u8, double: bool, scalar: bool, q: bool, n: u32) { + if scalar { + self.ld_scalar(double, x, n); + } else if !q { + self.ld_scalar(true, x, n); + } else { + self.ld_vec(x, n); + } + } + + pub(super) fn fp_commit(&mut self, double: bool, scalar: bool, q: bool, rd: u32) { + if scalar { + self.st_scalar(double, X14, rd); + } else { + self.st_vec(q, X14, rd); + } + } + + pub(super) fn fp_nan(&mut self, double: bool, scalar: bool, x: u8) -> usize { + if scalar { + self.nan_guard_scalar(double, x) + } else { + self.nan_guard_vec(double, x) + } + } + + pub(super) fn fp_abs_bits(&mut self, double: bool, x: u8) { + self.fp_bits(12, double, if double { 0x7fff_ffff_ffff_ffff } else { 0x7fff_ffff }); + self.a.vex(1, PP_66, false, false, 0xdb, x, x, Rm::R(12)); + } + + pub(super) fn fp_test_mask(&mut self, x: u8) -> usize { + self.clobber(); + self.a.vex(2, PP_66, false, false, 0x17, x, 0, Rm::R(x)); + self.a.jcc(Cc::Ne, None) + } + + /// x86 detects tininess after rounding; ARM detects it before rounding. + /// A result rounded up to the smallest normal may therefore lack UFC. + pub(super) fn fp_underflow_boundary(&mut self, double: bool, x: u8) -> usize { + self.fp_bits(12, double, if double { 0x7fff_ffff_ffff_ffff } else { 0x7fff_ffff }); + self.a.vex(1, PP_66, false, false, 0xdb, X13, x, Rm::R(12)); + self.fp_power(12, double, if double { -1022 } else { -126 }); + self.a.vex( + if double { 2 } else { 1 }, + PP_66, + false, + false, + if double { 0x29 } else { 0x76 }, + X13, + X13, + Rm::R(12), + ); + self.fp_test_mask(X13) + } + + /// Round X14 to nearest, ties away, without changing FPSR. Mask large + /// integral values (including infinities) before subtracting the + /// truncated value, avoiding infinity-infinity and double rounding. + pub(super) fn fp_round_away(&mut self, double: bool) { + let pp = if double { PP_66 } else { PP_NONE }; + self.fp_bits(12, double, if double { 0x7fff_ffff_ffff_ffff } else { 0x7fff_ffff }); + self.a.vex(1, PP_66, false, false, 0xdb, 12, X14, Rm::R(12)); + self.fp_power(X15, double, if double { 52 } else { 23 }); + self.a.vex_ib(1, pp, false, false, 0xc2, 10, 12, Rm::R(X15), 1); // abs < 2^fraction_bits + self.a.vex(1, PP_66, false, false, 0xdb, 12, 12, Rm::R(10)); + self.a + .vex_ib(3, PP_66, false, false, if double { 0x09 } else { 0x08 }, X13, 0, Rm::R(12), 11); + self.a.vex(1, pp, false, false, 0x5c, X15, 12, Rm::R(X13)); + self.fp_power(12, double, -1); + self.a.vex_ib(1, pp, false, false, 0xc2, X15, X15, Rm::R(12), 13); // remainder >= 0.5 + self.fp_power(12, double, 0); + self.a.vex(1, PP_66, false, false, 0xdb, X15, X15, Rm::R(12)); + self.a.vex(1, pp, false, false, 0x58, X13, X13, Rm::R(X15)); + self.a.vex(1, PP_66, false, false, 0xdf, 12, 10, Rm::R(X14)); // large original lanes + self.fp_bits(X15, double, if double { 1 << 63 } else { 1 << 31 }); + self.a.vex(1, PP_66, false, false, 0xdb, X15, X14, Rm::R(X15)); + self.a.vex(1, PP_66, false, false, 0xeb, X14, X13, Rm::R(12)); + self.a.vex(1, PP_66, false, false, 0xeb, X14, X14, Rm::R(X15)); + } + + /// Set IXC iff original (X11) differs from the integral result (X14). + /// Called only after range validation: invalid conversions must not + /// acquire an extra inexact exception from a speculative host round. + pub(super) fn fp_inexact(&mut self, double: bool) { + self.a + .vex_ib(1, if double { PP_66 } else { PP_NONE }, false, false, 0xc2, X13, 11, Rm::R(X14), 4); + self.clobber(); + self.a.vex(2, PP_66, false, false, 0x17, X13, 0, Rm::R(X13)); + let exact = self.a.jcc(Cc::E, None); + self.a.alu_ri(Alu::Or, Sz::D, Rm::M(Mem::gs(OFF_FPSR)), 1 << 4); + self.a.bind32(exact); + } + + pub(super) fn fp_round_insn(&mut self, double: bool, scalar: bool, q: bool, op: Fp1Op, rd: u32, rn: u32) -> bool { + self.fp_prologue(); + let mut slow = Slow::new(); + slow.push(self.fp_fz_guard()); + self.fp_active(X14, double, scalar, q, rn); + slow.push(self.fp_nan(double, scalar, X14)); + self.a.vex(1, PP_F3, false, false, 0x6f, 11, 0, Rm::R(X14)); + let intsize = match op { + Fp1Op::Rint32X | Fp1Op::Rint32Z => Some(32), + Fp1Op::Rint64X | Fp1Op::Rint64Z => Some(64), + _ => None, + }; + if op == Fp1Op::RintA { + self.fp_round_away(double); + } else { + let imm = match op { + Fp1Op::RintN => 8, + Fp1Op::RintM => 9, + Fp1Op::RintP => 10, + Fp1Op::RintZ | Fp1Op::Rint32Z | Fp1Op::Rint64Z => 11, + Fp1Op::RintX => 4, + _ => 12, + }; + self.a + .vex_ib(3, PP_66, false, false, if double { 0x09 } else { 0x08 }, X14, 0, Rm::R(X14), imm); + } + if let Some(bits) = intsize { + self.fp_range(double, bits, false, &mut slow); + self.fp_inexact(double); + } + self.fp_commit(double, scalar, q, rd); + self.slow_paths(slow.as_slice()); + true + } + + /// Integral X14 must fit in the requested signed/unsigned type. + pub(super) fn fp_range(&mut self, double: bool, bits: u32, unsigned: bool, slow: &mut Slow) { + let pp = if double { PP_66 } else { PP_NONE }; + self.fp_power(X15, double, (bits - !unsigned as u32) as i32); + self.a.vex_ib(1, pp, false, false, 0xc2, X13, X14, Rm::R(X15), 13); // >= upper limit + slow.push(self.fp_test_mask(X13)); + if unsigned { + self.fp_zero(X15); + } else { + self.fp_bits( + X15, + double, + if double { + ((1023 + bits as u64 - 1) << 52) | 1 << 63 + } else { + ((127 + bits as u64 - 1) << 23) | 1 << 31 + }, + ); + } + self.a.vex_ib(1, pp, false, false, 0xc2, X13, X14, Rm::R(X15), 1); // < lower limit + slow.push(self.fp_test_mask(X13)); + } + + pub(super) fn fp_misc_extra(&mut self, q: bool, size: u8, op: Simd2Op, rd: u32, rn: u32, scalar: bool) -> Option { + use Simd2Op::*; + if matches!(op, FcvtN | FcvtN2 | FcvtXn | FcvtXn2 | FcvtL | FcvtL2) { + let narrow = !matches!(op, FcvtL | FcvtL2); + let high = matches!(op, FcvtN2 | FcvtXn2 | FcvtL2); + return Some(self.fp_precision( + size + narrow as u8, + size + !narrow as u8, + scalar, + high, + matches!(op, FcvtXn | FcvtXn2), + rd, + rn, + )); + } + if matches!(op, Frecpe | Frsqrte | Frecpx) { + return Some(self.fp_estimate(q, size, scalar, op, rd, rn)); + } + let cvt = match op { + Scvtf => Some((true, false, 0)), + Ucvtf => Some((true, true, 0)), + Fcvtns => Some((false, false, 0)), + Fcvtnu => Some((false, true, 0)), + Fcvtps => Some((false, false, 1)), + Fcvtpu => Some((false, true, 1)), + Fcvtms => Some((false, false, 2)), + Fcvtmu => Some((false, true, 2)), + Fcvtzs => Some((false, false, 3)), + Fcvtzu => Some((false, true, 3)), + Fcvtas => Some((false, false, 4)), + Fcvtau => Some((false, true, 4)), + _ => None, + }; + if let Some((to_fp, unsigned, round)) = cvt { + return Some(self.fp_simd_int(q, size, scalar, to_fp, unsigned, round, 0, rd, rn)); + } + let round = match op { + FrintN => Some(Fp1Op::RintN), + FrintP => Some(Fp1Op::RintP), + FrintM => Some(Fp1Op::RintM), + FrintZ => Some(Fp1Op::RintZ), + FrintA => Some(Fp1Op::RintA), + FrintX => Some(Fp1Op::RintX), + FrintI => Some(Fp1Op::RintI), + Frint32Z => Some(Fp1Op::Rint32Z), + Frint32X => Some(Fp1Op::Rint32X), + Frint64Z => Some(Fp1Op::Rint64Z), + Frint64X => Some(Fp1Op::Rint64X), + _ => None, + }; + if let Some(round) = round { + return Some(self.fp_round_insn(size == 3, scalar, q, round, rd, rn)); + } + if matches!(op, FCmEq0 | FCmGe0 | FCmGt0 | FCmLe0 | FCmLt0) { + let cmp = match op { + FCmEq0 => SimdOp::FCmEq, + FCmGe0 | FCmLe0 => SimdOp::FCmGe, + _ => SimdOp::FCmGt, + }; + return Some(self.fp_compare_mask(q, size, cmp, rd, rn, None, scalar, matches!(op, FCmLe0 | FCmLt0))); + } + if !matches!(op, Fabs | Fneg | Fsqrt) { + return None; + } + let double = size == 3; + let mut slow = Slow::new(); + if op == Fsqrt { + self.fp_prologue(); + slow.push(self.fp_fz_guard()); + } + self.fp_active(X14, double, scalar, q, rn); + if op == Fsqrt { + self.a + .vex(1, if double { PP_66 } else { PP_NONE }, false, false, 0x51, X14, 0, Rm::R(X14)); + slow.push(self.fp_nan(double, scalar, X14)); + } else if op == Fabs { + self.fp_abs_bits(double, X14); + } else { + self.fp_bits(12, double, if double { 1 << 63 } else { 1 << 31 }); + self.a.vex(1, PP_66, false, false, 0xef, X14, X14, Rm::R(12)); + } + self.fp_commit(double, scalar, q, rd); + if slow.len != 0 { + self.slow_paths(slow.as_slice()); + } + Some(true) + } + + #[allow(clippy::too_many_arguments)] + pub(super) fn fp_compare_mask( + &mut self, + q: bool, + size: u8, + op: SimdOp, + rd: u32, + rn: u32, + rm: Option, + scalar: bool, + reverse: bool, + ) -> bool { + if size < 2 { + return false; + } + let double = size == 3; + self.fp_prologue(); + let slow = self.fp_fz_guard(); + self.fp_active(X14, double, scalar, q, rn); + if let Some(rm) = rm { + self.fp_active(X15, double, scalar, q, rm); + } else { + self.fp_zero(X15); + } + if matches!(op, SimdOp::FAcGe | SimdOp::FAcGt) { + self.fp_abs_bits(double, X14); + self.fp_abs_bits(double, X15); + } + let pred = match (op, reverse) { + (SimdOp::FCmEq, _) => 0, // ordered quiet: only sNaNs raise IOC + (SimdOp::FCmGe | SimdOp::FAcGe, false) => 13, + (SimdOp::FCmGe, true) => 2, + (_, false) => 14, + (_, true) => 1, + }; + self.a.vex_ib( + 1, + if double { PP_66 } else { PP_NONE }, + false, + false, + 0xc2, + X14, + X14, + Rm::R(X15), + pred, + ); + self.fp_commit(double, scalar, q, rd); + self.slow_paths(&[slow]); + true + } + + /// Combine scratch operands; inputs and result have identical lane layouts. + pub(super) fn fp_pair_op(&mut self, double: bool, op: SimdOp, slow: &mut Slow) { + let pp = if double { PP_66 } else { PP_NONE }; + if op == SimdOp::FAddP { + self.a.vex(1, pp, false, false, 0x58, X14, X14, Rm::R(X15)); + slow.push(self.nan_guard_vec(double, X14)); + } else { + self.a.vex_ib(1, pp, false, false, 0xc2, X13, X14, Rm::R(X15), 3); + slow.push(self.fp_test_mask(X13)); + let min = matches!(op, SimdOp::FMinP | SimdOp::FMinNmP); + let opc = if min { 0x5d } else { 0x5f }; + self.a.vex(1, pp, false, false, opc, 12, X14, Rm::R(X15)); + self.a.vex(1, pp, false, false, opc, X14, X15, Rm::R(X14)); + self.a + .vex(1, PP_66, false, false, if min { 0xeb } else { 0xdb }, X14, X14, Rm::R(12)); + } + } + + pub(super) fn fp_pair(&mut self, q: bool, size: u8, op: SimdOp, rd: u32, rn: u32, rm: u32) -> bool { + if size < 2 { + return false; + } + let double = size == 3; + self.fp_prologue(); + let mut slow = Slow::new(); + slow.push(self.fp_fz_guard()); + self.fp_active(X14, double, false, q, rn); + self.fp_active(X15, double, false, q, rm); + if double { + self.a.vex(1, PP_66, false, false, 0x6c, 12, X14, Rm::R(X15)); + self.a.vex(1, PP_66, false, false, 0x6d, X15, X14, Rm::R(X15)); + self.a.vex(1, PP_F3, false, false, 0x6f, X14, 0, Rm::R(12)); + } else { + if !q { + self.a.vex(1, PP_66, false, false, 0x6c, X14, X14, Rm::R(X15)); + self.a.vex(1, PP_F3, false, false, 0x6f, X15, 0, Rm::R(X14)); + } + self.a.vex_ib(1, PP_NONE, false, false, 0xc6, 12, X14, Rm::R(X15), 0x88); + self.a.vex_ib(1, PP_NONE, false, false, 0xc6, X15, X14, Rm::R(X15), 0xdd); + self.a.vex(1, PP_F3, false, false, 0x6f, X14, 0, Rm::R(12)); + } + self.fp_pair_op(double, op, &mut slow); + self.st_vec(q, X14, rd); + self.slow_paths(slow.as_slice()); + true + } + + pub(in super::super) fn fp_across(&mut self, q: bool, size: u8, op: AcrossOp, rd: u32, rn: u32) -> bool { + let pair = match op { + AcrossOp::FAddP => SimdOp::FAddP, + AcrossOp::FMinP | AcrossOp::FMinV => SimdOp::FMinP, + AcrossOp::FMaxP | AcrossOp::FMaxV => SimdOp::FMaxP, + AcrossOp::FMinNmP | AcrossOp::FMinNmV => SimdOp::FMinNmP, + AcrossOp::FMaxNmP | AcrossOp::FMaxNmV => SimdOp::FMaxNmP, + _ => return false, + }; + let double = size == 3; + self.fp_prologue(); + let mut slow = Slow::new(); + slow.push(self.fp_fz_guard()); + self.fp_active(X14, double, false, q, rn); + if double { + self.a.vex_ib(1, PP_66, false, false, 0x73, 3, X15, Rm::R(X14), 8); + self.a.vex(1, PP_F3, false, false, 0x7e, X14, 0, Rm::R(X14)); + } else { + self.a.vex_ib(1, PP_NONE, false, false, 0xc6, X15, X14, Rm::R(X14), 0xdd); + self.a.vex_ib(1, PP_NONE, false, false, 0xc6, X14, X14, Rm::R(X14), 0x88); + } + self.fp_pair_op(double, pair, &mut slow); + if !double && q { + self.a.vex_ib(1, PP_66, false, false, 0x73, 3, X15, Rm::R(X14), 4); + self.fp_pair_op(false, pair, &mut slow); + } + self.st_scalar(double, X14, rd); + self.slow_paths(slow.as_slice()); + true + } + + pub(in super::super) fn fp_ccmp(&mut self, ftype: u8, rn: u32, rm: u32, nzcv: u32, cond: u32, signal: bool) -> bool { + if ftype > 1 { + return false; + } + self.flags_to_host(); + self.flags_to_mem(); + let otherwise = self.cond_cc(cond).map(|cc| self.a.jcc(cc.invert(), None)); + let slow = self.fp_fz_guard(); + self.fp_cmp(ftype, rn, rm, false, signal); + if let Some(otherwise) = otherwise { + let done = self.a.jmp(None); + self.a.bind32(otherwise); + self.point(kind::INSN, FLAGS_MEM, 0); + self.a + .store_imm(Sz::W, Mem::gs(OFF_FLAGS_X86), image_from_nzcv((nzcv as u64) << 28) as i32); + self.point(kind::DONE, FLAGS_MEM, 0); + self.a.bind32(done); + } + self.slow_paths(&[slow]); + true + } +} diff --git a/overlay/rust/kemu_core/src/jit/t1/fp/precision.rs b/overlay/rust/kemu_core/src/jit/t1/fp/precision.rs new file mode 100644 index 0000000..74f81c8 --- /dev/null +++ b/overlay/rust/kemu_core/src/jit/t1/fp/precision.rs @@ -0,0 +1,180 @@ +// SPDX-License-Identifier: GPL-2.0 +//! Precision conversion, including IEEE half storage (no FP16 arithmetic). +use super::*; + +impl<'s, 'b> Gen<'s, 'b> { + fn fp_half(&mut self, double: bool, to_half: bool, slow: &mut Slow) { + let fb = if double { 52 } else { 23 }; + let bias = if double { 1023 } else { 127 }; + let sign = if double { 63 } else { 31 }; + self.a.vmov_to_gpr(Sz::Q, RAX, X14); + self.a.vmov_from_gpr(Sz::Q, 11, RAX); + if !to_half { + self.a.mov_rr(Sz::D, RDX, RAX); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RDX), 0x7fff); + self.a.alu_ri(Alu::Cmp, Sz::D, Rm::R(RDX), 0x7c00); + slow.push(self.a.jcc(Cc::Ae, None)); + self.a.test_rr(Sz::D, RDX, RDX); + let zero = self.a.jcc(Cc::E, None); + self.a.alu_ri(Alu::Cmp, Sz::D, Rm::R(RDX), 0x400); + slow.push(self.a.jcc(Cc::B, None)); + self.a.alu_ri(Alu::Add, Sz::Q, Rm::R(RDX), (bias - 15) << 10); + self.a.shift_ri(Shift::Shl, Sz::Q, RDX, fb - 10); + self.a.bind32(zero); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RAX), 0x8000); + self.a.shift_ri(Shift::Shl, Sz::Q, RAX, sign - 15); + self.a.alu_rr(Alu::Or, Sz::Q, RAX, RDX); + self.a.vmov_from_gpr(Sz::Q, X14, RAX); + return; + } + // Preserve sign separately; arithmetic below is on the magnitude. + self.mov_imm(RDX, (1u64 << sign) - 1); + self.a.alu_rr(Alu::And, Sz::Q, RAX, RDX); + self.a.test_rr(Sz::Q, RAX, RAX); + let nonzero = self.a.jcc(Cc::Ne, None); + self.a.vmov_to_gpr(Sz::Q, RAX, 11); + self.a.shift_ri(Shift::Shr, Sz::Q, RAX, sign - 15); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RAX), 0x8000); + self.a.vmov_from_gpr(Sz::D, X14, RAX); + let zero_done = self.a.jmp(None); + self.a.bind32(nonzero); + self.a.mov_rr(Sz::Q, RDX, RAX); + self.a.shift_ri(Shift::Shr, Sz::Q, RDX, fb); + self.a.alu_ri(Alu::Cmp, Sz::D, Rm::R(RDX), bias - 14); + slow.push(self.a.jcc(Cc::B, None)); + self.a.alu_ri(Alu::Cmp, Sz::D, Rm::R(RDX), bias + 15); + slow.push(self.a.jcc(Cc::A, None)); + self.a.mov_rr(Sz::Q, RDX, RAX); + self.a.shift_ri(Shift::Shr, Sz::Q, RDX, fb - 10); + self.a.alu_ri(Alu::Sub, Sz::D, Rm::R(RDX), (bias - 15) << 10); + self.a.vmov_from_gpr(Sz::D, 10, RDX); // truncated half magnitude + self.mov_imm(RDX, (1u64 << (fb - 10)) - 1); + self.a.alu_rr(Alu::And, Sz::Q, RAX, RDX); + self.a.vmov_from_gpr(Sz::Q, 12, RAX); // discarded bits + self.a.test_rr(Sz::Q, RAX, RAX); + let exact = self.a.jcc(Cc::E, None); + self.a.load(Sz::D, RDX, Mem::gs(OFF_FPCR)); + self.a.shift_ri(Shift::Shr, Sz::D, RDX, 22); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RDX), 3); + let nearest = self.a.jcc(Cc::E, None); + self.a.alu_ri(Alu::Cmp, Sz::D, Rm::R(RDX), 3); + let truncate = self.a.jcc(Cc::E, None); + // Round toward +inf iff positive, toward -inf iff negative. + self.a.vmov_to_gpr(Sz::Q, RAX, 11); + self.a.shift_ri(Shift::Shr, Sz::Q, RAX, sign); + self.a.alu_ri(Alu::Add, Sz::D, Rm::R(RAX), 1); + self.a.alu_rr(Alu::Cmp, Sz::D, RAX, RDX); + let directed_down = self.a.jcc(Cc::Ne, None); + let directed_up = self.a.jmp(None); + self.a.bind32(nearest); + self.mov_imm(RDX, 1u64 << (fb - 11)); + self.a.vmov_to_gpr(Sz::Q, RAX, 12); + self.a.alu_rr(Alu::Cmp, Sz::Q, RAX, RDX); + let below = self.a.jcc(Cc::B, None); + let above = self.a.jcc(Cc::A, None); + self.a.vmov_to_gpr(Sz::D, RAX, 10); + self.a.test_ri(Sz::D, Rm::R(RAX), 1); + let even = self.a.jcc(Cc::E, None); + self.a.bind32(above); + self.a.bind32(directed_up); + self.a.vmov_to_gpr(Sz::D, RAX, 10); + self.a.alu_ri(Alu::Add, Sz::D, Rm::R(RAX), 1); + self.a.vmov_from_gpr(Sz::D, 10, RAX); + for b in [truncate, directed_down, below, even] { + self.a.bind32(b); + } + self.a.vmov_to_gpr(Sz::D, RAX, 10); + self.a.alu_ri(Alu::Cmp, Sz::D, Rm::R(RAX), 0x7c00); + slow.push(self.a.jcc(Cc::Ae, None)); + self.a.alu_ri(Alu::Or, Sz::D, Rm::M(Mem::gs(OFF_FPSR)), 1 << 4); + self.a.bind32(exact); + self.a.vmov_to_gpr(Sz::Q, RAX, 11); + self.a.shift_ri(Shift::Shr, Sz::Q, RAX, sign - 15); + self.a.alu_ri(Alu::And, Sz::D, Rm::R(RAX), 0x8000); + self.a.vmov_to_gpr(Sz::D, RDX, 10); + self.a.alu_rr(Alu::Or, Sz::D, RAX, RDX); + self.a.vmov_from_gpr(Sz::D, X14, RAX); + self.a.bind32(zero_done); + } + + fn fp_precision_lane(&mut self, from: u8, to: u8, odd: bool, slow: &mut Slow) { + if from == 1 || to == 1 { + self.fp_half(from == 3 || to == 3, to == 1, slow); + return; + } + let double = from == 3; + slow.push(self.nan_guard_scalar(double, X14)); + if odd { + // The boundary cases need ARM's tininess/overflow rules. In the + // normal range, the two adjacent floats bracket the exact value. + self.fp_abs_bits(true, X14); + self.fp_power(X15, true, -126); + self.a.vex(1, PP_66, false, false, 0x2e, X14, 0, Rm::R(X15)); + slow.push(self.a.jcc(Cc::B, None)); + self.fp_power(X15, true, 127); + self.a.vex(1, PP_66, false, false, 0x2e, X14, 0, Rm::R(X15)); + slow.push(self.a.jcc(Cc::Ae, None)); + // Restore signed source from the frozen input lane (X11). + self.a.vex(1, PP_F3, false, false, 0x6f, X14, 0, Rm::R(11)); + } + self.a + .vex(1, if double { PP_F2 } else { PP_F3 }, false, false, 0x5a, X14, X14, Rm::R(X14)); + if double && !odd { + slow.push(self.fp_underflow_boundary(false, X14)); + } + if odd { + self.a.vmov_to_gpr(Sz::D, RAX, X14); + self.a.test_ri(Sz::D, Rm::R(RAX), 1); + let already_odd = self.a.jcc(Cc::Ne, None); + self.a.vex(1, PP_F3, false, false, 0x5a, X15, X15, Rm::R(X14)); + self.fp_abs_bits(true, X15); + self.fp_abs_bits(true, 11); + self.a.vex(1, PP_66, false, false, 0x2e, X15, 0, Rm::R(11)); + let exact = self.a.jcc(Cc::E, None); + let below = self.a.jcc(Cc::B, None); + self.a.vmov_to_gpr(Sz::D, RAX, X14); + self.a.alu_ri(Alu::Sub, Sz::D, Rm::R(RAX), 1); + let done = self.a.jmp(None); + self.a.bind32(below); + self.a.vmov_to_gpr(Sz::D, RAX, X14); + self.a.alu_ri(Alu::Or, Sz::D, Rm::R(RAX), 1); + self.a.bind32(done); + self.a.vmov_from_gpr(Sz::D, X14, RAX); + self.a.bind32(exact); + self.a.bind32(already_odd); + } + } + + pub(super) fn fp_precision(&mut self, from: u8, to: u8, scalar: bool, high: bool, odd: bool, rd: u32, rn: u32) -> bool { + if !(1..=3).contains(&from) || !(1..=3).contains(&to) || from == to { + return false; + } + self.fp_prologue(); + self.clobber(); + let mut slow = Slow::new(); + slow.push(self.fp_fz_guard()); + if from == 1 || to == 1 { + self.a.test_ri(Sz::D, Rm::M(Mem::gs(OFF_FPCR)), 1 << 26); + slow.push(self.a.jcc(Cc::Ne, None)); + } + self.ld_vec(8, rn); + let narrow = from > to; + if !scalar && high && narrow { + self.ld_scalar(true, 9, rd); + } else { + self.fp_zero(9); + } + let lanes = if scalar { 1 } else { 16 >> from.max(to) }; + for i in 0..lanes { + self.fp_lane(1 << from, i + if high && !narrow { lanes } else { 0 }); + if odd { + self.a.vex(1, PP_F3, false, false, 0x6f, 11, 0, Rm::R(X14)); + } + self.fp_precision_lane(from, to, odd, &mut slow); + self.fp_pack(1 << to, i + if high && narrow { lanes } else { 0 }); + } + self.store_vec(rd, 9); + self.slow_paths(slow.as_slice()); + true + } +} diff --git a/overlay/rust/kemu_core/src/softfloat.rs b/overlay/rust/kemu_core/src/softfloat.rs index 883898c..1005bb9 100644 --- a/overlay/rust/kemu_core/src/softfloat.rs +++ b/overlay/rust/kemu_core/src/softfloat.rs @@ -1082,7 +1082,7 @@ const fn rsqrt_estimate_exact(a0: u64) -> u64 { (b + 1) / 2 } -const RSQRT_TABLE: [u16; 384] = { +pub(crate) const RSQRT_TABLE: [u16; 384] = { let mut t = [0u16; 384]; let mut i = 0; while i < 384 { diff --git a/tests/bench/fp.c b/tests/bench/fp.c index 650bb7c..dd511bc 100644 --- a/tests/bench/fp.c +++ b/tests/bench/fp.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 /* Common FP kernels; build identically at -O3 for AArch64 and x86-64. */ #include +#include #include #include #include @@ -47,6 +48,25 @@ __attribute__((noinline)) static double range(void) return count; } +/* -fno-math-errno permits SIMD sqrt on both targets. Inputs are positive. */ +__attribute__((noinline)) static double square_roots(void) +{ + for (int i = 0; i < N; i++) z[i] = sqrtf(x[i] + 2.0f); + return z[0] + z[N/2] + z[N-1]; +} + +__attribute__((noinline)) static double quantize(void) +{ + for (int i = 0; i < N; i++) z[i] = (float)(int32_t)(x[i] * 1024.0f) / 1024.0f; + return z[0] + z[N/2] + z[N-1]; +} + +__attribute__((noinline)) static double rounding(void) +{ + for (int i = 0; i < N; i++) z[i] = nearbyintf(x[i] * 1024.0f); + return z[0] + z[N/2] + z[N-1]; +} + __attribute__((noinline)) static double mandelbrot(void) { unsigned total = 0; @@ -77,6 +97,7 @@ int main(int argc, char **argv) static const struct { const char *name; double (*fn)(void); } tests[] = { { "saxpy", saxpy }, { "dot", dot }, { "matmul", matmul }, { "clamp", clamp }, { "range", range }, { "mandelbrot", mandelbrot }, + { "sqrt", square_roots }, { "quantize", quantize }, { "round", rounding }, }; unsigned rounds = argc > 2 ? strtoul(argv[2], NULL, 0) : 50; if (!rounds) return 2; diff --git a/tools/jit-test/src/fp-catalog.txt b/tools/jit-test/src/fp-catalog.txt new file mode 100644 index 0000000..b6f7c69 --- /dev/null +++ b/tools/jit-test/src/fp-catalog.txt @@ -0,0 +1,431 @@ +0ea0f820 fabs v0.2s, v1.2s +2ea0f820 fneg v0.2s, v1.2s +2ea1f820 fsqrt v0.2s, v1.2s +0e218820 frintn v0.2s, v1.2s +0ea18820 frintp v0.2s, v1.2s +0e219820 frintm v0.2s, v1.2s +0ea19820 frintz v0.2s, v1.2s +2e218820 frinta v0.2s, v1.2s +2e219820 frintx v0.2s, v1.2s +2ea19820 frinti v0.2s, v1.2s +0e21e820 frint32z v0.2s, v1.2s +2e21e820 frint32x v0.2s, v1.2s +0e21f820 frint64z v0.2s, v1.2s +2e21f820 frint64x v0.2s, v1.2s +0e21a820 fcvtns v0.2s, v1.2s +2e21a820 fcvtnu v0.2s, v1.2s +0ea1a820 fcvtps v0.2s, v1.2s +2ea1a820 fcvtpu v0.2s, v1.2s +0e21b820 fcvtms v0.2s, v1.2s +2e21b820 fcvtmu v0.2s, v1.2s +0ea1b820 fcvtzs v0.2s, v1.2s +2ea1b820 fcvtzu v0.2s, v1.2s +0e21c820 fcvtas v0.2s, v1.2s +2e21c820 fcvtau v0.2s, v1.2s +0e21d820 scvtf v0.2s, v1.2s +2e21d820 ucvtf v0.2s, v1.2s +0ea1d820 frecpe v0.2s, v1.2s +2ea1d820 frsqrte v0.2s, v1.2s +0e22e420 fcmeq v0.2s, v1.2s, v2.2s +2e22e420 fcmge v0.2s, v1.2s, v2.2s +2ea2e420 fcmgt v0.2s, v1.2s, v2.2s +2e22ec20 facge v0.2s, v1.2s, v2.2s +2ea2ec20 facgt v0.2s, v1.2s, v2.2s +2e22d420 faddp v0.2s, v1.2s, v2.2s +2e22f420 fmaxp v0.2s, v1.2s, v2.2s +2ea2f420 fminp v0.2s, v1.2s, v2.2s +2e22c420 fmaxnmp v0.2s, v1.2s, v2.2s +2ea2c420 fminnmp v0.2s, v1.2s, v2.2s +2ea2d420 fabd v0.2s, v1.2s, v2.2s +0e22dc20 fmulx v0.2s, v1.2s, v2.2s +0e22fc20 frecps v0.2s, v1.2s, v2.2s +0ea2fc20 frsqrts v0.2s, v1.2s, v2.2s +0ea0d820 fcmeq v0.2s, v1.2s, #0.0 +2ea0c820 fcmge v0.2s, v1.2s, #0.0 +0ea0c820 fcmgt v0.2s, v1.2s, #0.0 +2ea0d820 fcmle v0.2s, v1.2s, #0.0 +0ea0e820 fcmlt v0.2s, v1.2s, #0.0 +0f3fe420 scvtf v0.2s, v1.2s, #1 +0f30e420 scvtf v0.2s, v1.2s, #16 +0f20e420 scvtf v0.2s, v1.2s, #32 +2f3fe420 ucvtf v0.2s, v1.2s, #1 +2f30e420 ucvtf v0.2s, v1.2s, #16 +2f20e420 ucvtf v0.2s, v1.2s, #32 +0f3ffc20 fcvtzs v0.2s, v1.2s, #1 +0f30fc20 fcvtzs v0.2s, v1.2s, #16 +0f20fc20 fcvtzs v0.2s, v1.2s, #32 +2f3ffc20 fcvtzu v0.2s, v1.2s, #1 +2f30fc20 fcvtzu v0.2s, v1.2s, #16 +2f20fc20 fcvtzu v0.2s, v1.2s, #32 +2f829020 fmulx v0.2s, v1.2s, v2.s[0] +2fa29020 fmulx v0.2s, v1.2s, v2.s[1] +2f829820 fmulx v0.2s, v1.2s, v2.s[2] +2fa29820 fmulx v0.2s, v1.2s, v2.s[3] +4ea0f820 fabs v0.4s, v1.4s +6ea0f820 fneg v0.4s, v1.4s +6ea1f820 fsqrt v0.4s, v1.4s +4e218820 frintn v0.4s, v1.4s +4ea18820 frintp v0.4s, v1.4s +4e219820 frintm v0.4s, v1.4s +4ea19820 frintz v0.4s, v1.4s +6e218820 frinta v0.4s, v1.4s +6e219820 frintx v0.4s, v1.4s +6ea19820 frinti v0.4s, v1.4s +4e21e820 frint32z v0.4s, v1.4s +6e21e820 frint32x v0.4s, v1.4s +4e21f820 frint64z v0.4s, v1.4s +6e21f820 frint64x v0.4s, v1.4s +4e21a820 fcvtns v0.4s, v1.4s +6e21a820 fcvtnu v0.4s, v1.4s +4ea1a820 fcvtps v0.4s, v1.4s +6ea1a820 fcvtpu v0.4s, v1.4s +4e21b820 fcvtms v0.4s, v1.4s +6e21b820 fcvtmu v0.4s, v1.4s +4ea1b820 fcvtzs v0.4s, v1.4s +6ea1b820 fcvtzu v0.4s, v1.4s +4e21c820 fcvtas v0.4s, v1.4s +6e21c820 fcvtau v0.4s, v1.4s +4e21d820 scvtf v0.4s, v1.4s +6e21d820 ucvtf v0.4s, v1.4s +4ea1d820 frecpe v0.4s, v1.4s +6ea1d820 frsqrte v0.4s, v1.4s +4e22e420 fcmeq v0.4s, v1.4s, v2.4s +6e22e420 fcmge v0.4s, v1.4s, v2.4s +6ea2e420 fcmgt v0.4s, v1.4s, v2.4s +6e22ec20 facge v0.4s, v1.4s, v2.4s +6ea2ec20 facgt v0.4s, v1.4s, v2.4s +6e22d420 faddp v0.4s, v1.4s, v2.4s +6e22f420 fmaxp v0.4s, v1.4s, v2.4s +6ea2f420 fminp v0.4s, v1.4s, v2.4s +6e22c420 fmaxnmp v0.4s, v1.4s, v2.4s +6ea2c420 fminnmp v0.4s, v1.4s, v2.4s +6ea2d420 fabd v0.4s, v1.4s, v2.4s +4e22dc20 fmulx v0.4s, v1.4s, v2.4s +4e22fc20 frecps v0.4s, v1.4s, v2.4s +4ea2fc20 frsqrts v0.4s, v1.4s, v2.4s +4ea0d820 fcmeq v0.4s, v1.4s, #0.0 +6ea0c820 fcmge v0.4s, v1.4s, #0.0 +4ea0c820 fcmgt v0.4s, v1.4s, #0.0 +6ea0d820 fcmle v0.4s, v1.4s, #0.0 +4ea0e820 fcmlt v0.4s, v1.4s, #0.0 +4f3fe420 scvtf v0.4s, v1.4s, #1 +4f30e420 scvtf v0.4s, v1.4s, #16 +4f20e420 scvtf v0.4s, v1.4s, #32 +6f3fe420 ucvtf v0.4s, v1.4s, #1 +6f30e420 ucvtf v0.4s, v1.4s, #16 +6f20e420 ucvtf v0.4s, v1.4s, #32 +4f3ffc20 fcvtzs v0.4s, v1.4s, #1 +4f30fc20 fcvtzs v0.4s, v1.4s, #16 +4f20fc20 fcvtzs v0.4s, v1.4s, #32 +6f3ffc20 fcvtzu v0.4s, v1.4s, #1 +6f30fc20 fcvtzu v0.4s, v1.4s, #16 +6f20fc20 fcvtzu v0.4s, v1.4s, #32 +6f829020 fmulx v0.4s, v1.4s, v2.s[0] +6fa29020 fmulx v0.4s, v1.4s, v2.s[1] +6f829820 fmulx v0.4s, v1.4s, v2.s[2] +6fa29820 fmulx v0.4s, v1.4s, v2.s[3] +4ee0f820 fabs v0.2d, v1.2d +6ee0f820 fneg v0.2d, v1.2d +6ee1f820 fsqrt v0.2d, v1.2d +4e618820 frintn v0.2d, v1.2d +4ee18820 frintp v0.2d, v1.2d +4e619820 frintm v0.2d, v1.2d +4ee19820 frintz v0.2d, v1.2d +6e618820 frinta v0.2d, v1.2d +6e619820 frintx v0.2d, v1.2d +6ee19820 frinti v0.2d, v1.2d +4e61e820 frint32z v0.2d, v1.2d +6e61e820 frint32x v0.2d, v1.2d +4e61f820 frint64z v0.2d, v1.2d +6e61f820 frint64x v0.2d, v1.2d +4e61a820 fcvtns v0.2d, v1.2d +6e61a820 fcvtnu v0.2d, v1.2d +4ee1a820 fcvtps v0.2d, v1.2d +6ee1a820 fcvtpu v0.2d, v1.2d +4e61b820 fcvtms v0.2d, v1.2d +6e61b820 fcvtmu v0.2d, v1.2d +4ee1b820 fcvtzs v0.2d, v1.2d +6ee1b820 fcvtzu v0.2d, v1.2d +4e61c820 fcvtas v0.2d, v1.2d +6e61c820 fcvtau v0.2d, v1.2d +4e61d820 scvtf v0.2d, v1.2d +6e61d820 ucvtf v0.2d, v1.2d +4ee1d820 frecpe v0.2d, v1.2d +6ee1d820 frsqrte v0.2d, v1.2d +4e62e420 fcmeq v0.2d, v1.2d, v2.2d +6e62e420 fcmge v0.2d, v1.2d, v2.2d +6ee2e420 fcmgt v0.2d, v1.2d, v2.2d +6e62ec20 facge v0.2d, v1.2d, v2.2d +6ee2ec20 facgt v0.2d, v1.2d, v2.2d +6e62d420 faddp v0.2d, v1.2d, v2.2d +6e62f420 fmaxp v0.2d, v1.2d, v2.2d +6ee2f420 fminp v0.2d, v1.2d, v2.2d +6e62c420 fmaxnmp v0.2d, v1.2d, v2.2d +6ee2c420 fminnmp v0.2d, v1.2d, v2.2d +6ee2d420 fabd v0.2d, v1.2d, v2.2d +4e62dc20 fmulx v0.2d, v1.2d, v2.2d +4e62fc20 frecps v0.2d, v1.2d, v2.2d +4ee2fc20 frsqrts v0.2d, v1.2d, v2.2d +4ee0d820 fcmeq v0.2d, v1.2d, #0.0 +6ee0c820 fcmge v0.2d, v1.2d, #0.0 +4ee0c820 fcmgt v0.2d, v1.2d, #0.0 +6ee0d820 fcmle v0.2d, v1.2d, #0.0 +4ee0e820 fcmlt v0.2d, v1.2d, #0.0 +4f7fe420 scvtf v0.2d, v1.2d, #1 +4f60e420 scvtf v0.2d, v1.2d, #32 +4f40e420 scvtf v0.2d, v1.2d, #64 +6f7fe420 ucvtf v0.2d, v1.2d, #1 +6f60e420 ucvtf v0.2d, v1.2d, #32 +6f40e420 ucvtf v0.2d, v1.2d, #64 +4f7ffc20 fcvtzs v0.2d, v1.2d, #1 +4f60fc20 fcvtzs v0.2d, v1.2d, #32 +4f40fc20 fcvtzs v0.2d, v1.2d, #64 +6f7ffc20 fcvtzu v0.2d, v1.2d, #1 +6f60fc20 fcvtzu v0.2d, v1.2d, #32 +6f40fc20 fcvtzu v0.2d, v1.2d, #64 +6fc29020 fmulx v0.2d, v1.2d, v2.d[0] +6fc29820 fmulx v0.2d, v1.2d, v2.d[1] +1e264020 frinta s0, s1 +1e284020 frint32z s0, s1 +1e28c020 frint32x s0, s1 +1e294020 frint64z s0, s1 +1e29c020 frint64x s0, s1 +5ea1f820 frecpx s0, s1 +5ea1d820 frecpe s0, s1 +7ea1d820 frsqrte s0, s1 +5e21d820 scvtf s0, s1 +7e21d820 ucvtf s0, s1 +5e21a820 fcvtns s0, s1 +7e21a820 fcvtnu s0, s1 +5ea1a820 fcvtps s0, s1 +7ea1a820 fcvtpu s0, s1 +5e21b820 fcvtms s0, s1 +7e21b820 fcvtmu s0, s1 +5ea1b820 fcvtzs s0, s1 +7ea1b820 fcvtzu s0, s1 +5e21c820 fcvtas s0, s1 +7e21c820 fcvtau s0, s1 +5e22e420 fcmeq s0, s1, s2 +7e22e420 fcmge s0, s1, s2 +7ea2e420 fcmgt s0, s1, s2 +7e22ec20 facge s0, s1, s2 +7ea2ec20 facgt s0, s1, s2 +7ea2d420 fabd s0, s1, s2 +5e22dc20 fmulx s0, s1, s2 +5e22fc20 frecps s0, s1, s2 +5ea2fc20 frsqrts s0, s1, s2 +5ea0d820 fcmeq s0, s1, #0.0 +7ea0c820 fcmge s0, s1, #0.0 +5ea0c820 fcmgt s0, s1, #0.0 +7ea0d820 fcmle s0, s1, #0.0 +5ea0e820 fcmlt s0, s1, #0.0 +7e30d820 faddp s0, v1.2s +7e30f820 fmaxp s0, v1.2s +7eb0f820 fminp s0, v1.2s +7e30c820 fmaxnmp s0, v1.2s +7eb0c820 fminnmp s0, v1.2s +1e220425 fccmp s1, s2, #5, eq +1e221425 fccmp s1, s2, #5, ne +1e22e425 fccmp s1, s2, #5, al +1e22f425 fccmp s1, s2, #5, nv +1e220435 fccmpe s1, s2, #5, eq +1e221435 fccmpe s1, s2, #5, ne +1e22e435 fccmpe s1, s2, #5, al +1e22f435 fccmpe s1, s2, #5, nv +5f3fe420 scvtf s0, s1, #1 +5f30e420 scvtf s0, s1, #16 +5f20e420 scvtf s0, s1, #32 +7f3fe420 ucvtf s0, s1, #1 +7f30e420 ucvtf s0, s1, #16 +7f20e420 ucvtf s0, s1, #32 +5f3ffc20 fcvtzs s0, s1, #1 +5f30fc20 fcvtzs s0, s1, #16 +5f20fc20 fcvtzs s0, s1, #32 +7f3ffc20 fcvtzu s0, s1, #1 +7f30fc20 fcvtzu s0, s1, #16 +7f20fc20 fcvtzu s0, s1, #32 +7f829020 fmulx s0, s1, v2.s[0] +7fa29020 fmulx s0, s1, v2.s[1] +7f829820 fmulx s0, s1, v2.s[2] +7fa29820 fmulx s0, s1, v2.s[3] +1e200020 fcvtns w0, s1 +1e210020 fcvtnu w0, s1 +1e280020 fcvtps w0, s1 +1e290020 fcvtpu w0, s1 +1e300020 fcvtms w0, s1 +1e310020 fcvtmu w0, s1 +1e380020 fcvtzs w0, s1 +1e390020 fcvtzu w0, s1 +1e240020 fcvtas w0, s1 +1e250020 fcvtau w0, s1 +1e220020 scvtf s0, w1 +1e230020 ucvtf s0, w1 +1e02fc20 scvtf s0, w1, #1 +1e03fc20 ucvtf s0, w1, #1 +1e18fc20 fcvtzs w0, s1, #1 +1e19fc20 fcvtzu w0, s1, #1 +1e02c020 scvtf s0, w1, #16 +1e03c020 ucvtf s0, w1, #16 +1e18c020 fcvtzs w0, s1, #16 +1e19c020 fcvtzu w0, s1, #16 +1e028020 scvtf s0, w1, #32 +1e038020 ucvtf s0, w1, #32 +1e188020 fcvtzs w0, s1, #32 +1e198020 fcvtzu w0, s1, #32 +9e200020 fcvtns x0, s1 +9e210020 fcvtnu x0, s1 +9e280020 fcvtps x0, s1 +9e290020 fcvtpu x0, s1 +9e300020 fcvtms x0, s1 +9e310020 fcvtmu x0, s1 +9e380020 fcvtzs x0, s1 +9e390020 fcvtzu x0, s1 +9e240020 fcvtas x0, s1 +9e250020 fcvtau x0, s1 +9e220020 scvtf s0, x1 +9e230020 ucvtf s0, x1 +9e02fc20 scvtf s0, x1, #1 +9e03fc20 ucvtf s0, x1, #1 +9e18fc20 fcvtzs x0, s1, #1 +9e19fc20 fcvtzu x0, s1, #1 +9e028020 scvtf s0, x1, #32 +9e038020 ucvtf s0, x1, #32 +9e188020 fcvtzs x0, s1, #32 +9e198020 fcvtzu x0, s1, #32 +9e020020 scvtf s0, x1, #64 +9e030020 ucvtf s0, x1, #64 +9e180020 fcvtzs x0, s1, #64 +9e190020 fcvtzu x0, s1, #64 +1e664020 frinta d0, d1 +1e684020 frint32z d0, d1 +1e68c020 frint32x d0, d1 +1e694020 frint64z d0, d1 +1e69c020 frint64x d0, d1 +5ee1f820 frecpx d0, d1 +5ee1d820 frecpe d0, d1 +7ee1d820 frsqrte d0, d1 +5e61d820 scvtf d0, d1 +7e61d820 ucvtf d0, d1 +5e61a820 fcvtns d0, d1 +7e61a820 fcvtnu d0, d1 +5ee1a820 fcvtps d0, d1 +7ee1a820 fcvtpu d0, d1 +5e61b820 fcvtms d0, d1 +7e61b820 fcvtmu d0, d1 +5ee1b820 fcvtzs d0, d1 +7ee1b820 fcvtzu d0, d1 +5e61c820 fcvtas d0, d1 +7e61c820 fcvtau d0, d1 +5e62e420 fcmeq d0, d1, d2 +7e62e420 fcmge d0, d1, d2 +7ee2e420 fcmgt d0, d1, d2 +7e62ec20 facge d0, d1, d2 +7ee2ec20 facgt d0, d1, d2 +7ee2d420 fabd d0, d1, d2 +5e62dc20 fmulx d0, d1, d2 +5e62fc20 frecps d0, d1, d2 +5ee2fc20 frsqrts d0, d1, d2 +5ee0d820 fcmeq d0, d1, #0.0 +7ee0c820 fcmge d0, d1, #0.0 +5ee0c820 fcmgt d0, d1, #0.0 +7ee0d820 fcmle d0, d1, #0.0 +5ee0e820 fcmlt d0, d1, #0.0 +7e70d820 faddp d0, v1.2d +7e70f820 fmaxp d0, v1.2d +7ef0f820 fminp d0, v1.2d +7e70c820 fmaxnmp d0, v1.2d +7ef0c820 fminnmp d0, v1.2d +1e620425 fccmp d1, d2, #5, eq +1e621425 fccmp d1, d2, #5, ne +1e62e425 fccmp d1, d2, #5, al +1e62f425 fccmp d1, d2, #5, nv +1e620435 fccmpe d1, d2, #5, eq +1e621435 fccmpe d1, d2, #5, ne +1e62e435 fccmpe d1, d2, #5, al +1e62f435 fccmpe d1, d2, #5, nv +5f7fe420 scvtf d0, d1, #1 +5f60e420 scvtf d0, d1, #32 +5f40e420 scvtf d0, d1, #64 +7f7fe420 ucvtf d0, d1, #1 +7f60e420 ucvtf d0, d1, #32 +7f40e420 ucvtf d0, d1, #64 +5f7ffc20 fcvtzs d0, d1, #1 +5f60fc20 fcvtzs d0, d1, #32 +5f40fc20 fcvtzs d0, d1, #64 +7f7ffc20 fcvtzu d0, d1, #1 +7f60fc20 fcvtzu d0, d1, #32 +7f40fc20 fcvtzu d0, d1, #64 +7fc29020 fmulx d0, d1, v2.d[0] +7fc29820 fmulx d0, d1, v2.d[1] +1e600020 fcvtns w0, d1 +1e610020 fcvtnu w0, d1 +1e680020 fcvtps w0, d1 +1e690020 fcvtpu w0, d1 +1e700020 fcvtms w0, d1 +1e710020 fcvtmu w0, d1 +1e780020 fcvtzs w0, d1 +1e790020 fcvtzu w0, d1 +1e640020 fcvtas w0, d1 +1e650020 fcvtau w0, d1 +1e620020 scvtf d0, w1 +1e630020 ucvtf d0, w1 +1e42fc20 scvtf d0, w1, #1 +1e43fc20 ucvtf d0, w1, #1 +1e58fc20 fcvtzs w0, d1, #1 +1e59fc20 fcvtzu w0, d1, #1 +1e42c020 scvtf d0, w1, #16 +1e43c020 ucvtf d0, w1, #16 +1e58c020 fcvtzs w0, d1, #16 +1e59c020 fcvtzu w0, d1, #16 +1e428020 scvtf d0, w1, #32 +1e438020 ucvtf d0, w1, #32 +1e588020 fcvtzs w0, d1, #32 +1e598020 fcvtzu w0, d1, #32 +9e600020 fcvtns x0, d1 +9e610020 fcvtnu x0, d1 +9e680020 fcvtps x0, d1 +9e690020 fcvtpu x0, d1 +9e700020 fcvtms x0, d1 +9e710020 fcvtmu x0, d1 +9e780020 fcvtzs x0, d1 +9e790020 fcvtzu x0, d1 +9e640020 fcvtas x0, d1 +9e650020 fcvtau x0, d1 +9e620020 scvtf d0, x1 +9e630020 ucvtf d0, x1 +9e42fc20 scvtf d0, x1, #1 +9e43fc20 ucvtf d0, x1, #1 +9e58fc20 fcvtzs x0, d1, #1 +9e59fc20 fcvtzu x0, d1, #1 +9e428020 scvtf d0, x1, #32 +9e438020 ucvtf d0, x1, #32 +9e588020 fcvtzs x0, d1, #32 +9e598020 fcvtzu x0, d1, #32 +9e420020 scvtf d0, x1, #64 +9e430020 ucvtf d0, x1, #64 +9e580020 fcvtzs x0, d1, #64 +9e590020 fcvtzu x0, d1, #64 +6e30f820 fmaxv s0, v1.4s +6eb0f820 fminv s0, v1.4s +6e30c820 fmaxnmv s0, v1.4s +6eb0c820 fminnmv s0, v1.4s +1ee24020 fcvt s0, h1 +1ee2c020 fcvt d0, h1 +1e23c020 fcvt h0, s1 +1e63c020 fcvt h0, d1 +1e624020 fcvt s0, d1 +1e22c020 fcvt d0, s1 +0e216820 fcvtn v0.4h, v1.4s +4e216820 fcvtn2 v0.8h, v1.4s +0e616820 fcvtn v0.2s, v1.2d +4e616820 fcvtn2 v0.4s, v1.2d +2e616820 fcvtxn v0.2s, v1.2d +6e616820 fcvtxn2 v0.4s, v1.2d +0e217820 fcvtl v0.4s, v1.4h +4e217820 fcvtl2 v0.4s, v1.8h +0e617820 fcvtl v0.2d, v1.2s +4e617820 fcvtl2 v0.2d, v1.4s +7e616820 fcvtxn s0, d1 +1e7e0020 fjcvtzs w0, d1 +6fa29821 fmulx v1.4s, v1.4s, v2.s[3] ; underflow boundary +6fc29820 fmulx v0.2d, v1.2d, v2.d[1] ; underflow boundary diff --git a/tools/jit-test/src/fp_all.rs b/tools/jit-test/src/fp_all.rs new file mode 100644 index 0000000..6523fc1 --- /dev/null +++ b/tools/jit-test/src/fp_all.rs @@ -0,0 +1,131 @@ +// SPDX-License-Identifier: GPL-2.0 +//! Exhaustive decoded FP form catalog: ordinary inputs must run without +//! helpers; edge cases compare every register and FPSR bit with softfloat. +use super::*; +use kemu_core::decode::{FcvtOp, Simd2Op}; + +pub fn case(it: u64, r: &mut Rng, init: &mut CpuState) -> Vec { + let catalog: Vec<_> = include_str!("fp-catalog.txt").lines().collect(); + let line = catalog[(it as usize / 2) % catalog.len()]; + let mut word = u32::from_str_radix(&line[..8], 16).unwrap(); + let insn = decode(word); + assert!(!matches!(insn, Insn::Undefined), "{line}"); + let size; + let mut integer = false; + let mut fixed = false; + match insn { + Insn::Fp1 { ftype, .. } | Insn::FpCCmp { ftype, .. } => { + size = match ftype { + 3 => 1, + _ => ftype + 2, + } + } + Insn::FpCvt { ftype, op, .. } => { + size = if ftype == 1 { 3 } else { 2 }; + integer = matches!(op, FcvtOp::ItoF { .. } | FcvtOp::FixedToF { .. }); + fixed = matches!(op, FcvtOp::FtoFixed { .. }); + } + Insn::SimdMisc { size: s, op, .. } => { + size = s + matches!(op, Simd2Op::FcvtN | Simd2Op::FcvtN2 | Simd2Op::FcvtXn | Simd2Op::FcvtXn2) as u8; + integer = matches!(op, Simd2Op::Scvtf | Simd2Op::Ucvtf); + } + Insn::SimdThree { size: s, .. } | Insn::SimdAcross { size: s, .. } | Insn::SimdIndexed { size: s, .. } => size = s, + Insn::SimdShift { immh, opcode, .. } => { + size = (31 - immh.leading_zeros()) as u8; + integer = opcode == 28; + fixed = opcode == 31; + } + _ => panic!("unexpected form {insn:?}"), + } + let edge = it & 1 != 0; + let round = ((it / (2 * catalog.len() as u64)) % 4) as u32; + init.fpcr = round << 22; + if edge { + init.fpcr |= (r.below(4) == 0) as u32 * (1 << 24); + init.fpcr |= (r.below(2) as u32) << 25; + init.fpcr |= (r.below(4) == 0) as u32 * (1 << 26); + } + init.fpsr = if edge && r.below(2) == 0 { + r.next() as u32 & 0x0800_009f + } else { + 0 + }; + let values: &[u64] = match size { + 1 => &[ + 0, 0x8000, 1, 0x3ff, 0x400, 0x3c00, 0x3e00, 0xbe00, 0x7bff, 0x7c00, 0xfc00, 0x7e01, 0x7c01, + ], + 2 => &[ + 0, 0x80000000, 1, 0x7fffff, 0x800000, 0x3f000000, 0x3fc00000, 0xbfc00000, 0x7f800000, 0xff800000, 0x7fc01234, 0x7f800001, + 0x4f000000, 0xcf000000, 0x4f800000, 0x5f000000, 0x5f800000, 0x477ff000, 0x38800000, 0x3f800001, 0x7f7fffff, + ], + _ => &[ + 0, + 1 << 63, + 1, + 0xfffffffffffff, + 0x10000000000000, + 0x3fe0000000000000, + 0x3ff8000000000000, + 0xbff8000000000000, + 0x7ff0000000000000, + 0xfff0000000000000, + 0x7ff8000000001234, + 0x7ff0000000000001, + 0x41e0000000000000, + 0xc1e0000000000000, + 0x43e0000000000000, + 0x43f0000000000000, + 0x3ff0000000000001, + 0x7fefffffffffffff, + ], + }; + for v in &mut init.v { + let mut bits = 0u128; + for lane in 0..(16 >> size) { + let x = if edge && r.below(2) == 0 { + r.next() + } else if edge { + values[r.below(values.len() as u64) as usize] + } else if integer { + 1234567 + r.below(100) + } else if !fixed { + match size { + 1 => 0x3800 + r.below(0xc00), + 2 => 0x3f000000 + r.below(0x1800000), + _ => 0x3fe0000000000000 + r.below(0x30000000000000), + } + } else { + match (size, fixed) { + (1, _) => 0x3d00, + (2, true) => 0x3e800000, + (2, false) => 0x3fa00000, + (_, true) => 0x3fd0000000000000, + _ => 0x3ff4000000000000, + } + }; + let width = 8 << size; + bits |= ((x as u128) & ((1u128 << width) - 1)) << (lane * width); + } + *v = [bits as u64, (bits >> 64) as u64]; + } + if integer { + init.x[1] = if edge { r.val() } else { 1234567 }; + } + if edge && line.contains("underflow boundary") { + init.fpcr = 1 << 22; // round upward across the normal/subnormal boundary + init.fpsr = 0; + if size == 2 { + init.v[1] = [0x007fffff007fffff; 2]; + init.v[2] = [0x3f8000013f800001; 2]; + } else { + init.v[1] = [0x000fffffffffffff; 2]; + init.v[2] = [0x3ff0000000000001; 2]; + } + } + // Alias only edge cases: after the first conversion the source may have + // a different format and is no longer guaranteed to take the fast path. + if edge && !line.contains("underflow boundary") && !matches!(insn, Insn::FpCCmp { .. } | Insn::FpCvt { .. }) && r.below(3) == 0 { + word = (word & !31) | ((word >> 5) & 31); + } + vec![0xeb03009f, word, word, word, 0x9a8800e6, 0xd4200000] +} diff --git a/tools/jit-test/src/main.rs b/tools/jit-test/src/main.rs index 603666c..018f683 100644 --- a/tools/jit-test/src/main.rs +++ b/tools/jit-test/src/main.rs @@ -6,6 +6,8 @@ //! the result against the reference interpreter for random instruction //! sequences. Stubs reach the harness with `call` instead of `syscall`. +mod fp_all; + use kemu_core::decode::{decode, Insn, SimdOp}; use kemu_core::env::{Access, Env, Fault}; use kemu_core::interp::{self, Exit}; @@ -1021,8 +1023,9 @@ fn main() { let mut classes: HashMap = HashMap::new(); for it in 0..iters { let mode = only_mode.unwrap_or((it % 3) as u32); - let code = gen_case(&mut rng, mode); + let mut code = gen_case(&mut rng, mode); let mut init = init_state(&mut rng); + if mode == 11 { code = fp_all::case(it, &mut rng, &mut init); } if mode == 9 { init_fp_hot(&mut rng, &mut init, &code, it & 1 != 0); } @@ -1073,7 +1076,7 @@ fn main() { } let mut bad = Vec::new(); - if mode == 9 && it & 1 == 0 && h().helpers - helpers_before != 1 { + if matches!(mode, 9 | 11) && it & 1 == 0 && h().helpers - helpers_before != 1 { bad.push(format!("finite FP fallback: {} helpers, expected only BRK", h().helpers - helpers_before)); } if matches!(mode, 6 | 7 | 10) { @@ -1115,11 +1118,11 @@ fn main() { } if ref_st.fpsr != jit_st.fpsr { fpsr_diffs += 1; - if matches!(mode, 6 | 7 | 9 | 10) { + if matches!(mode, 6 | 7 | 9 | 10 | 11) { bad.push(format!("FPSR: ref {:#x} jit {:#x}", ref_st.fpsr, jit_st.fpsr)); } } - if matches!(mode, 6 | 7 | 9 | 10) && ref_st.fpcr != jit_st.fpcr { + if matches!(mode, 6 | 7 | 9 | 10 | 11) && ref_st.fpcr != jit_st.fpcr { bad.push(format!("FPCR: ref {:#x} jit {:#x}", ref_st.fpcr, jit_st.fpcr)); } if !bad.is_empty() { @@ -1139,7 +1142,8 @@ fn main() { for w in &code { println!(" {:08x} {:?}", w, decode(*w)); } - println!(" init nzcv {:#x}", init.nzcv); + println!(" init nzcv {:#x} fpcr {:#x} fpsr {:#x}", init.nzcv, init.fpcr, init.fpsr); + if mode == 11 { println!(" init vectors: {:x?}", init.v); } for b in &bad { println!(" {b}"); } From 1dd7eb06674e429a7bb5411be312bc59758acf89 Mon Sep 17 00:00:00 2001 From: losfair Date: Sun, 25 Oct 2026 03:33:02 -0700 Subject: [PATCH 3/3] jit: keep truncating int32 vector conversions on a short packed path --- .../rust/kemu_core/src/jit/t1/fp/convert.rs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/overlay/rust/kemu_core/src/jit/t1/fp/convert.rs b/overlay/rust/kemu_core/src/jit/t1/fp/convert.rs index a2dbc1d..04d11b3 100644 --- a/overlay/rust/kemu_core/src/jit/t1/fp/convert.rs +++ b/overlay/rust/kemu_core/src/jit/t1/fp/convert.rs @@ -149,6 +149,27 @@ impl<'s, 'b> Gen<'s, 'b> { self.fp_power(X15, false, -(fbits as i32)); self.a.vex(1, PP_NONE, false, false, 0x59, X14, X14, Rm::R(X15)); } + } else if round == 3 { + // CVTTPS2DQ already supplies the exact IOC/IXC behavior for + // truncation. Fixed-point scaling is exact inside this range; + // reject larger inputs before multiplying to avoid spurious OFC. + if fbits != 0 { + self.fp_bits(12, false, 0x7fff_ffff); + self.a.vex(1, PP_66, false, false, 0xdb, 12, X14, Rm::R(12)); + self.fp_power(X15, false, 31 - fbits as i32); + self.a.vex_ib(1, PP_NONE, false, false, 0xc2, X13, 12, Rm::R(X15), 5); // >= or NaN + slow.push(self.fp_test_mask(X13)); + self.fp_power(X15, false, fbits as i32); + self.a.vex(1, PP_NONE, false, false, 0x59, X14, X14, Rm::R(X15)); + } + self.a.vex(1, PP_F3, false, false, 0x5b, X14, 0, Rm::R(X14)); + if fbits == 0 { + // x86 returns INT_MIN for every invalid conversion. ARM + // saturates instead; also send exact INT_MIN to the helper. + self.fp_bits(12, false, 0x8000_0000); + self.a.vex(1, PP_66, false, false, 0x76, X13, X14, Rm::R(12)); + slow.push(self.fp_test_mask(X13)); + } } else { slow.push(self.nan_guard_vec(false, X14)); if fbits != 0 {