Skip to content

obs-graphics: vec2 ceil/floor round inline instead of calling libm (#1298) - #1394

Merged
zackees merged 2 commits into
masterfrom
perf/1298-vec2-round
Oct 18, 2026
Merged

zackees merged 2 commits into
masterfrom
perf/1298-vec2-round

Conversation

@zackees

@zackees zackees commented Oct 18, 2026

Copy link
Copy Markdown
Member

Fixes #1298 (parent #1295).

Investigation

x86_64 baseline (SSE2, no SSE4.1), objdump of the port_comparison bench binary:

  • C oracle (GCC): ceilf/floorf are inlined with no libm call and no roundss. Per component: andps abs mask, ucomiss against 2^23 (the branch skips large values, inf and NaN, which come back unchanged), then cvttss2si / cvtsi2ss (truncate), cmpnless + andps 1.0 + addss (adjust), and andnps/orps (copysign). The two lanes are joined with unpcklps and written as one 8-byte movlps store. 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.
  • Rust before: f32::ceil/floor lower to llvm.ceil.f32. Without SSE4.1 there is no roundss, and LLVM has no SSE2 expansion for it, so each call becomes a libcall: two indirect call *ceilf@GOT per vec2_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.rs has ceil_f32/floor_f32. Each one follows GCC's sequence: if |x| < 2^23, round with (x + s) - s where s = copysign(2^23, x), adjust by ±1, then copysign(x) to keep -0.0. Otherwise it returns x unchanged. I used the add trick instead of x as i32 as f32 because Rust's saturating cast adds compares and cmovs 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 annotate showed the rest was a store-forwarding stall. The inline branches made LLVM write dst as two 4-byte movss, and the caller then reads the 8-byte struct back. The ffi shims for vec2_floor/vec2_ceil now write the result with one 8-byte unaligned store (write_packed), as GCC does.

Scope: only Vec2::ceil/floor and their ffi shims. vec3/vec4 ceil/floor (and any other f32::ceil/floor/round/trunc users) can reuse round.rs in follow-ups.

Exactness

  • Tier 1, exhaustive (round::tests::exhaustive_matches_libm): checks all 2^32 f32 bit patterns against f32::ceil/f32::floor bit 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 normal soldr cargo test path (the rust-tests-linux lane): a [profile.dev.package.obs-graphics] opt-level = 3 entry, the same precedent as obs-media-io's exhaustive u32 test, keeps it at about 3 s in dev and in release.
  • Tier 3: the existing proptest parity on random bits still runs. The new ceil_floor_edge_values_match_c_oracle compares 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 against oracle_vec2_ceil/floor, all bit for bit, NaN payloads included.
  • RED: 36eb0bb has an identity stub, and the exhaustive test fails (ceil(0xc4000001) = 0xc4000001, want 0xc4000000). GREEN: 7539053.
  • Tier 2: not applicable (no C test or header change). Tier 4: not applicable.

Numbers

soldr cargo bench -p obs-c-oracle --bench port_comparison -- "vec2/(ceil|floor)", medians on the same machine:

bench Rust before C before Rust after C after ratio after
vec2/floor 8.45 ns 4.00 ns 3.41 ns 3.34 ns 1.02
vec2/ceil 8.37 ns 3.44 ns 3.44 ns 3.39 ns 1.01

Disassembly after: vec2_ceil has no call. Per lane it does andps/ucomiss 2^23, then addss/subss with ±2^23, cmpltss, andps 1.0, addss, and copysign andps/orps. Both lanes are packed with movd/shl/or and written with a single mov %rax,(%rdi).

No change to rust-toolchain, target-cpu or RUSTFLAGS.

@zackees
zackees enabled auto-merge October 18, 2026 04:58
@zackees
zackees force-pushed the perf/1298-vec2-round branch from 7539053 to bfc0321 Compare October 18, 2026 06:04
@zackees
zackees added this pull request to the merge queue Oct 18, 2026
Merged via the queue into master with commit 9f573a9 Oct 18, 2026
23 checks passed
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]>
@zackees
zackees deleted the perf/1298-vec2-round branch October 26, 2026 15:45
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 ceil and floor regress against C code generation

1 participant