Repository navigation
obs-graphics: vec2 ceil/floor round inline instead of calling libm (#1298) - #1394
Merged
Merged
Conversation
zackees
enabled auto-merge
October 18, 2026 04:58
zackees
force-pushed
the
perf/1298-vec2-round
branch
from
October 18, 2026 06:04
7539053 to
bfc0321
Compare
This was referenced Oct 18, 2026
This was referenced Oct 18, 2026
swilk19
added a commit
to swilk19/obs-studio
that referenced
this pull request
Oct 18, 2026
…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
added a commit
to swilk19/obs-studio
that referenced
this pull request
Oct 18, 2026
…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]>
swilk19
added a commit
to swilk19/obs-studio
that referenced
this pull request
Oct 18, 2026
…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]>
chriscoveries
pushed a commit
to chriscoveries/obsrust-fork
that referenced
this pull request
Oct 18, 2026
…1428) (obs-rust#1432) * obs-graphics: aarch64 vec2 ceil/floor use the native round (obs-rust#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]> * obs-graphics: aarch64 vec2 ceil/floor keep both lanes in one vector (obs-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]> --------- 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 #1298 (parent #1295).
Investigation
x86_64 baseline (SSE2, no SSE4.1), objdump of the
port_comparisonbench binary:ceilf/floorfare inlined with no libm call and noroundss. Per component:andpsabs mask,ucomissagainst 2^23 (the branch skips large values, inf and NaN, which come back unchanged), thencvttss2si/cvtsi2ss(truncate),cmpnless+andps 1.0+addss(adjust), andandnps/orps(copysign). The two lanes are joined withunpcklpsand written as one 8-bytemovlpsstore. GCC can do this because it assumes no signaling NaNs by default (-fno-signaling-nans), so it doesn't have to quiet an sNaN the way libm does.f32::ceil/floorlower tollvm.ceil.f32. Without SSE4.1 there is noroundss, and LLVM has no SSE2 expansion for it, so each call becomes a libcall: two indirectcall *ceilf@GOTpervec2_ceil, with spills around them.Design
Option (a), portable code with no target or cfg dependence, so the platform boundary doesn't apply. The new
rust/obs-graphics/src/round.rshasceil_f32/floor_f32. Each one follows GCC's sequence: if|x| < 2^23, round with(x + s) - swheres = copysign(2^23, x), adjust by ±1, thencopysign(x)to keep -0.0. Otherwise it returnsxunchanged. I used the add trick instead ofx as i32 as f32because Rust's saturating cast adds compares andcmovs that C's(int)doesn't have.Removing the libm calls alone only brought ceil/floor from ~8.4 ns to ~5.8 ns.
perf annotateshowed the rest was a store-forwarding stall. The inline branches made LLVM writedstas two 4-bytemovss, and the caller then reads the 8-byte struct back. The ffi shims forvec2_floor/vec2_ceilnow write the result with one 8-byte unaligned store (write_packed), as GCC does.Scope: only
Vec2::ceil/floorand their ffi shims. vec3/vec4 ceil/floor (and any otherf32::ceil/floor/round/truncusers) can reuseround.rsin follow-ups.Exactness
round::tests::exhaustive_matches_libm): checks all 2^32f32bit patterns againstf32::ceil/f32::floorbit for bit for every non-NaN input (±0, subnormals, ±inf, 2^23..2^31 and beyond, the i32 limits). Every NaN must come back with its own input bits, which is the C behaviour. The work is split over 64 threads. It runs in the normalsoldr cargo testpath (therust-tests-linuxlane): a[profile.dev.package.obs-graphics] opt-level = 3entry, the same precedent as obs-media-io's exhaustive u32 test, keeps it at about 3 s in dev and in release.ceil_floor_edge_values_match_c_oraclecompares 22 edge bit patterns (signed zeros, smallest subnormals, ±0.5, ±(1-ulp), ±1.5, ±(2^23-1/2), ±2^23, ±2^31, ±inf, qNaN, sNaN, negative NaN with a payload) on both lanes againstoracle_vec2_ceil/floor, all bit for bit, NaN payloads included.ceil(0xc4000001) = 0xc4000001, want 0xc4000000). GREEN: 7539053.Numbers
soldr cargo bench -p obs-c-oracle --bench port_comparison -- "vec2/(ceil|floor)", medians on the same machine:Disassembly after:
vec2_ceilhas nocall. Per lane it doesandps/ucomiss2^23, thenaddss/subsswith ±2^23,cmpltss,andps 1.0,addss, and copysignandps/orps. Both lanes are packed withmovd/shl/orand written with a singlemov %rax,(%rdi).No change to rust-toolchain, target-cpu or RUSTFLAGS.