Repository navigation
obs-graphics: aarch64 vec2 ceil/floor use the native round (#1428) - #1432
Merged
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1428 (parent #1295, follows #1298 / #1394).
What and why
#1394 replaced
f32::ceil/f32::floorinVec2::ceil/floorwith 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, wheref32::flooris a libm call and two 4-byte stores stall store forwarding. On aarch64 they made things worse: C'sfloorfthere is a singlefrintm, and GCC/Clang vectorizevec2_floortoldr 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 singlecfg_select!) istrueontarget_arch = "aarch64".round.rsreturnsx.ceil()/x.floor()when it is set.frintp/frintmquiet signaling NaNs, which is the conventionplatform::round_largealready follows on aarch64, so the C-equivalence contract is unchanged.write_packedwrites the struct directly whenNATIVE_ROUNDis set, so LLVM keeps both lanes in one vector register.aarch64 codegen after the change, the same as the C oracle:
Baseline x86_64 is unchanged:
vec2_floorandvec2_ceilin theport_comparisonbench binary disassemble instruction for instruction the same asorigin/master(194cb8a), with RIP-relative offsets normalized. The constant isfalsethere 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.pyCriterion 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:vec2/floorvec2/ceilvec2/abs(control)vec2/norm(control)Exactness is unchanged and checked on both architectures.
obs-tests-graphicspasses 203/203 on macOS arm64 and on x86_64 Linux. That includesround::exhaustive_matches_libm(all 2³² bit patterns againstf32::ceil/floor, with NaN bits matched to the C oracle's convention on that host) andceil_floor_edge_values_match_c_oracle(signed zeros, subnormals, ±2²³, ±inf, qNaN, sNaN and payload NaNs againstoracle_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_comparisonbench binary built fromorigin/masterand 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×, withvec2/floor1.958 → 1.943 ns andvec2/ceil1.936 → 1.933 ns. The outliers (dstr/cat/40960.62×,bmem/dup_free/40961.07×) are in crates this PR does not touch, running identical instructions, so they are noise.Windows arm64:
obs-graphicscross-checks foraarch64-pc-windows-msvc. MSVC's arm64floorfis alsofrintm, andround_largealready treats Windows as quieting signaling NaNs. The release gate's nativewindows-11-vs2026-armjob 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_ROUNDis false there. Turning onroundssis 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
soldr cargo fmt --check,soldr cargo clippy -p obs-graphics --all-targets -D warningsandsoldr cargo dylint --all -- -p obs-graphics --lib(platform boundary) are clean.unsafe: one new block, theptr::writeinwrite_packed(src/ffi/, an existingunsafe fnwith a// SAFETY:comment, the same pattern asvec2_abs). The safe core gains none.NATIVE_ROUNDis aconstflag inplatform/mod.rs, selected by its owncfg_select!the wayround_largealready is. I can fold it into the top-levelcfg_select!if you'd rather keep a single block.docs/features.mdline: 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