Skip to content

obs-graphics: aarch64 vec2 ceil/floor use the native round (#1428) - #1432

Merged
zackees merged 2 commits into
obs-rust:masterfrom
swilk19:fix/1428-aarch64-round
Oct 18, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
swilk19:fix/1428-aarch64-round

Conversation

@swilk19

@swilk19 swilk19 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #1428 (parent #1295, follows #1298 / #1394).

What and why

#1394 replaced f32::ceil/f32::floor in Vec2::ceil/floor with an inline add-2²³ sequence, and made the ffi shims write both lanes as one 8-byte integer store. Both changes fix baseline x86_64, where f32::floor is a libm call and two 4-byte stores stall store forwarding. On aarch64 they made things worse: C's floorf there is a single frintm, and GCC/Clang vectorize vec2_floor to ldr d0; frintm.2s; str d0. Rust ran about 40 instructions per call, 3.48× slower than C on an Apple M2 Pro.

  • platform::NATIVE_ROUND (src/platform/mod.rs, a single cfg_select!) is true on target_arch = "aarch64". round.rs returns x.ceil()/x.floor() when it is set. frintp/frintm quiet signaling NaNs, which is the convention platform::round_large already follows on aarch64, so the C-equivalence contract is unchanged.
  • write_packed writes the struct directly when NATIVE_ROUND is set, so LLVM keeps both lanes in one vector register.

aarch64 codegen after the change, the same as the C oracle:

_vec2_floor:  ldr d0,[x1] ; frintm.2s v0,v0 ; str d0,[x0] ; ret
_vec2_ceil:   ldr d0,[x1] ; frintp.2s v0,v0 ; str d0,[x0] ; ret

Baseline x86_64 is unchanged: vec2_floor and vec2_ceil in the port_comparison bench binary disassemble instruction for instruction the same as origin/master (194cb8a), with RIP-relative offsets normalized. The constant is false there and folds away.

RED → GREEN

RED is the benchmark evidence the #1295 children ask for, since this is a code-generation regression, not a behaviour change. Five paired passes with the record.py Criterion options (--sample-size 30 --warm-up-time 0.1 --measurement-time 0.4 --nresamples 10000) on an Apple M2 Pro (macOS arm64), with the base and PR binaries interleaved in each pass:

Benchmark master Rust / C ns master Rust/C (range) PR Rust / C ns PR Rust/C (range)
vec2/floor 2.989 / 0.860 3.48× (3.47–3.50) 0.859 / 0.858 1.00× (1.00–1.00)
vec2/ceil 2.987 / 0.858 3.48× (3.47–3.50) 0.858 / 0.859 1.00× (1.00–1.01)
vec2/abs (control) 0.860 / 0.865 0.99× 0.860 / 0.860 1.00×
vec2/norm (control) 1.330 / 1.326 1.00× 1.331 / 1.333 1.00×

Exactness is unchanged and checked on both architectures. obs-tests-graphics passes 203/203 on macOS arm64 and on x86_64 Linux. That includes round::exhaustive_matches_libm (all 2³² bit patterns against f32::ceil/floor, with NaN bits matched to the C oracle's convention on that host) and ceil_floor_edge_values_match_c_oracle (signed zeros, subnormals, ±2²³, ±inf, qNaN, sNaN and payload NaNs against oracle_vec2_ceil/floor, bit for bit).

Other platforms

  • x86_64 (baseline, Linux): no code change. I hashed the normalized disassembly of every function in the port_comparison bench binary built from origin/master and from this branch, with addresses and RIP offsets stripped and Rust symbol hashes removed: 3,796 of 3,796 functions are identical. A full 70-case suite run (3 interleaved passes, CPU-pinned, i7-12700KF) puts the median Rust-time change at 1.002×, with vec2/floor 1.958 → 1.943 ns and vec2/ceil 1.936 → 1.933 ns. The outliers (dstr/cat/4096 0.62×, bmem/dup_free/4096 1.07×) are in crates this PR does not touch, running identical instructions, so they are noise.

  • Windows arm64: obs-graphics cross-checks for aarch64-pc-windows-msvc. MSVC's arm64 floorf is also frintm, and round_large already treats Windows as quieting signaling NaNs. The release gate's native windows-11-vs2026-arm job runs the suite. Linux aarch64 also cross-checks; it isn't a shipped target.

  • Intel macOS (x86_64-apple-darwin) and x86_64 with SSE4.1: unchanged. NATIVE_ROUND is false there. Turning on roundss is left out of scope, as noted in the claim: I can't measure it here, and on Linux it would change sNaN bits compared with GCC's inline sequence.

Tiers

  • Tier 1 / Tier 3: existing exhaustive and C-oracle parity tests, run on both architectures as above. No new tests: the change is pure codegen, and the existing suite pins every bit pattern.
  • Tier 2: not applicable. No C, C test or public header change. PR CI runs it anyway.
  • Tier 4: not applicable.
  • soldr cargo fmt --check, soldr cargo clippy -p obs-graphics --all-targets -D warnings and soldr cargo dylint --all -- -p obs-graphics --lib (platform boundary) are clean.
  • unsafe: one new block, the ptr::write in write_packed (src/ffi/, an existing unsafe fn with a // SAFETY: comment, the same pattern as vec2_abs). The safe core gains none.
  • NATIVE_ROUND is a const flag in platform/mod.rs, selected by its own cfg_select! the way round_large already is. I can fold it into the top-level cfg_select! if you'd rather keep a single block.
  • No docs/features.md line: there's no user-visible behaviour change (same as obs-graphics: vec2 ceil/floor round inline instead of calling libm (#1298) #1394).

🤖 Generated with Claude Code

swilk19 and others added 2 commits October 18, 2026 16:01
…1428)

obs-rust#1394 replaced f32::ceil/floor with an inline add-2^23 sequence so
baseline x86_64 would not call libm. On aarch64 f32::ceil/floor are a
single frintp/frintm, which is what C's ceilf/floorf compile to, and the
portable sequence made vec2_floor/vec2_ceil about 3.5x slower than C
(Apple M2 Pro).

platform::NATIVE_ROUND selects the native round on aarch64. frintp and
frintm quiet signaling NaNs, the convention round_large already uses
there. Other targets keep the inline sequence; x86_64 codegen is
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…bs-rust#1428)

write_packed's integer lane join (the x86 store-forwarding fix from
obs-rust#1394) stopped LLVM from using frintm.2s/frintp.2s on aarch64. With the
native round, write the struct directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@swilk19
swilk19 marked this pull request as ready for review October 18, 2026 20:18
@swilk19
swilk19 requested a review from zackees as a code owner October 18, 2026 20:18
@zackees
zackees added this pull request to the merge queue Oct 18, 2026
Merged via the queue into obs-rust:master with commit a0433dd Oct 18, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: graphics: vec2 floor/ceil are 3.5× slower than C on aarch64 after #1394

2 participants