diff --git a/CHANGELOG.md b/CHANGELOG.md index 78da0a4..9d5d620 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ remains unsupported. - The native ARM64 DBT now runs the 32- and 64-bit register and immediate forms of ADC and SBB itself instead of falling back to the shared interpreter (#195). The guest CF is moved into the host carry before ADCS or SBCS, inverted for SBB because SBCS subtracts NOT C while SBB subtracts CF. Memory forms and 8- and 16-bit forms still use the shared semantics. `tests/x86_carry_chain.rs` runs 256-bit add and subtract chains of 64- and 32-bit limbs, in one block and split one block per limb, against widening `u128`/`i128` reference arithmetic in both engines, plus immediate 128-bit increments and decrements and the `sbb reg, reg` carry mask. Locally verified on Apple M5 only. - Removed the temporary region-list allocation from each instruction-byte fetch while preserving lookup order, execute-permission checks and error behavior (part of #104). - Release and bench builds now use fat link-time optimization with one codegen unit (#116). On a local Apple M5, `dbt_bench` medians over five alternating runs dropped by about 6 % for the interpreter (903 to 845 µs) and about 9 % for cold DBT (1.155 to 1.046 ms), while warm DBT (cache hits) became about 2 % slower (21.83-22.08 to 22.29-22.54 µs over four further runs). The `xenora` binary shrank from 2.73 to 2.11 MB. A clean release build of the workspace takes about 14 s instead of 6 s. Debug and test builds are unchanged. +- The ARM64 assembler uses one 32-bit `MOVN` for zero-extended constants such as `0xFFFF1234` that previously took two wide moves (#89). On an Apple M1 Pro, a synthetic workload with 4,096 affected loads emitted 163,920 native bytes, down from 180,304 (9.1%). The native tests verify the encoding and the full 64-bit register result. - The ARM64 assembler loads 64-bit constants with the fewest MOVZ or MOVN plus MOVK instructions (#92). It no longer emits MOVK for zero halfwords and starts from MOVN when more halfwords are 0xFFFF than zero, so small negative values take one instruction instead of four. On Apple M5 the DBT benchmark emits 245,168 instead of 290,184 native bytes for its 626 blocks (-15.5%) and the 1,048 integer fixture instructions 128,724 instead of 145,732 (-11.7%); benchmark run times did not change measurably. New tests check the sequences against LLVM encodings and execute 1,056 of them on the host through the JIT. - Resolve the tiled BC1 mip and array layer once per linearization and copy contiguous block runs within each tile row. Partial edge blocks are retained and padding slots are skipped. Closes #349. - Changed the default report folder of `xenora probe` and `xenora imports sync` to `diagnostics/probe`, which Git ignores (#364). Anyone relying on the earlier default has to pass `--report`. diff --git a/crates/loader/src/arm64.rs b/crates/loader/src/arm64.rs index 078ce86..3e7026c 100644 --- a/crates/loader/src/arm64.rs +++ b/crates/loader/src/arm64.rs @@ -633,15 +633,22 @@ impl Arm64Assembler { self.emit(0x92800000 | (hw << 21) | ((imm16 as u32) << 5) | (xd as u32 & 0x1f)); } - /// Load a full 64-bit constant into register Xd with the fewest MOVZ or - /// MOVN plus MOVK instructions: one for each halfword that differs from - /// the background the first instruction leaves, and always at least one. - /// MOVZ leaves zero halfwords and MOVN leaves 0xFFFF ones, so MOVN is used - /// when more halfwords are 0xFFFF than zero. + /// Load a full 64-bit constant into Xd with the fewest wide moves. + /// A 32-bit MOVN leaves one low halfword set to 0xFFFF and zero-extends + /// the result into Xd. For other values, select a 64-bit MOVZ or MOVN + /// background and use MOVK for the remaining halfwords. pub fn load_imm_64(&mut self, xd: u8, val: u64) { let halves: [u16; 4] = std::array::from_fn(|i| (val >> (16 * i)) as u16); let zeros = halves.iter().filter(|&&h| h == 0).count(); let ones = halves.iter().filter(|&&h| h == 0xffff).count(); + // With only the top two halfwords zero, a 32-bit MOVN saves one + // instruction when either of the lower halfwords is all ones. + if zeros == 2 && ones != 0 && val >> 32 == 0 { + let index = usize::from(halves[1] != u16::MAX); + let imm16 = u32::from(!halves[index]); + self.emit(0x1280_0000 | ((index as u32) << 21) | (imm16 << 5) | u32::from(xd & 0x1f)); + return; + } let background = if ones > zeros { 0xffff } else { 0 }; // The first halfword that differs from the background, or halfword 0 // when none does (val is 0 or all ones). @@ -883,12 +890,14 @@ mod tests { } /// Constants at the halfword boundaries the MOVZ/MOVN choice depends on. - const IMMEDIATE_EDGES: [u64; 16] = [ + const IMMEDIATE_EDGES: [u64; 18] = [ 0, 1, 0xffff, 0x1_0000, 0x8000_0000, + 0xffff_1234, + 0x1234_ffff, 0xffff_ffff, 0x1_0000_0000, 0x1234_0000_0000_0000, @@ -904,10 +913,13 @@ mod tests { #[test] fn immediate_sequences_match_llvm_encodings() { - // llvm-mc -triple=aarch64-apple-macos -show-encoding. + // A64 words from llvm-mc and Apple Clang's integrated assembler. let cases: &[(u64, &[u32])] = &[ (0, &[0xd2800000]), // movz x0, #0 (u64::MAX, &[0x92800000]), // movn x0, #0 + (0xffff_1234, &[0x129db960]), // movn w0, #0xedcb + (0x1234_ffff, &[0x12bdb960]), // movn w0, #0xedcb, lsl #16 + (0xffff_ffff, &[0x12800000]), // movn w0, #0 (0xffff_ffff_ffff_1234, &[0x929db960]), // movn x0, #0xedcb (0x1234_0000_0000_0000, &[0xd2e24680]), // movz x0, #0x1234, lsl #48 (0xffff_0000_ffff_ffff, &[0x92dfffe0]), // movn x0, #0xffff, lsl #32 @@ -930,9 +942,16 @@ mod tests { ); } - /// Fewest MOVZ/MOVN/MOVK instructions for `value`: every halfword that is - /// neither zero (after MOVZ) nor 0xFFFF (after MOVN), and at least one. + /// A 32-bit MOVN can write a zero-extended value if its low word's + /// complement fits in a single halfword. Otherwise count the halfwords + /// that differ from either 64-bit initializer's background. fn shortest_wide_move(value: u64) -> usize { + if let Ok(low) = u32::try_from(value) { + let complement = !low; + if complement <= u32::from(u16::MAX) || complement.trailing_zeros() >= 16 { + return 1; + } + } let halves = (0..4).map(|i| (value >> (16 * i)) & 0xffff); let not_zero = halves.clone().filter(|&h| h != 0).count(); let not_ones = halves.filter(|&h| h != 0xffff).count(); @@ -964,9 +983,12 @@ mod tests { for value in values { for register in [X0, X9] { let mut asm = Arm64Assembler::new(); + // Seed the destination so a W-register load must clear the high bits. + asm.movn_64(register, 0, 0); + let start = asm.current_offset(); asm.load_imm_64(register, value); assert_eq!( - asm.instructions().len(), + asm.current_offset() - start, shortest_wide_move(value), "{value:#x}" );