Skip to content

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

Closed

Description

@swilk19

Part of #1295. Follows #1298 / #1394.

Context and reproduction

Class: performance regression on a release target.

#1394 replaced f32::floor/f32::ceil in the vec2 cores with inline add-and-subtract-2²³ rounding (rust/obs-graphics/src/round.rs). That fixed x86_64 baseline builds, where LLVM lowers f32::floor to a libm call. On aarch64, though, f32::floor and C floorf are a single native instruction (frintm/frintp), and the portable sequence is much slower.

origin/master at 194cb8a38, port_comparison filtered to vec2/(floor|ceil), recorder Criterion settings, 5 passes:

Machine Benchmark Rust ns C ns Rust/C Per-pass range
Apple M2 Pro, macOS arm64 vec2/floor 2.985 0.858 3.48× 3.47–3.48×
Apple M2 Pro, macOS arm64 vec2/ceil 2.984 0.860 3.47× 3.43–3.48×
Xeon Platinum 8358 (Ice Lake), dedicated, 10 × 2 s passes vec2/ceil 4.312 3.543 1.22× 1.21–1.36×
Xeon Platinum 8358 (Ice Lake), dedicated, 10 × 2 s passes vec2/floor 4.245 3.757 1.13× 1.10–1.31×
EPYC Rome (Zen 2), shared vec2/floor / ceil 3.760 / 3.726 3.656 / 3.567 1.03× / 1.04× 0.86–1.10×
i7-12700KF (WSL2) vec2/floor / ceil 1.948 / 1.987 1.869 / 1.875 1.04× / 1.06× 1.03–1.22× / 1.03–1.11×

Location

aarch64 disassembly from the same build:

_oracle_vec2_floor:  ldr d0,[x1] ; frintm.2s v0,v0 ; str d0,[x0] ; ret
_vec2_floor:         ldp s0,s1,[x1] ; fabs ; movi ; ... (40 instructions: 2^23 add/sub,
                     fcmp/fcsel adjust, NaN quieting, copysign via bif) ... ; str x8,[x0] ; ret

On x86_64, GCC inlines a cvttss2si-based truncate-and-adjust sequence for the C original. The Rust version uses addss/subss with ±2²³, which adds two dependent FP adds (about 4 cycles each on Ice Lake). That fits the steady ceil residual on Ice Lake, but this is not confirmed.

Proposed fix and failing-test sketch

  1. On targets with a native single-instruction round (aarch64; also x86_64 when sse4.1 is enabled at compile time), use f32::floor/f32::ceil. Keep the portable sequence for baseline x86_64. The target split belongs in src/platform/ (the single cfg_select! in platform/mod.rs), next to the existing platform::round_large, under the platform-boundary rule. NaN handling should still match: round.rs already says macOS and MSVC quiet signaling NaNs, which frintm does too.
  2. Optional: look at the x86 Ice Lake ceil residual (try a truncation-based sequence matching GCC's), but only if it holds up on a second Intel part.

RED: the existing vec2/floor and vec2/ceil benches on aarch64 (macOS or Linux) at 3.5×. Bit-exactness stays covered by the category suite's exhaustive 2³² check.

Acceptance criteria

Activity

  1. swilk19 commented on Oct 18, 2026

    @swilk19
    ContributorAuthor

    Claiming: #1428 (aarch64 vec2_floor/vec2_ceil)

    Plan, one PR:

    1. RED evidence: the 5-pass M2 Pro numbers in the issue body (3.47–3.48×), rechecked on the PR branch base.
    2. GREEN: on target_arch = "aarch64", ceil_f32/floor_f32 use f32::ceil/f32::floor (single frintp/frintm, which also quiets signaling NaNs, the convention platform::round_large already uses on aarch64). The target choice lives in obs-graphics/src/platform/. Baseline x86_64 keeps obs-graphics: vec2 ceil/floor round inline instead of calling libm (#1298) #1394's inline sequence unchanged, and I'll show its codegen is byte-identical.
    3. Verify: exhaustive 2^32 and C-oracle edge tests on macOS arm64, five paired bench passes on aarch64 and x86_64 (Zen 2, Ice Lake, Alder Lake) before and after.

    Decisions: x86_64 with SSE4.1 (roundss, e.g. the x86_64-apple-darwin default CPU) is left for later. I can't measure it here, and on Linux it would change sNaN bits compared with GCC's inline C sequence. The Ice Lake ceil residual stays as noted in the issue.

    I expect to open the PR today.

  2. added
    claimed
    on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions