Repository navigation
obs-audio-controls: Take the peak as _mm_max_ps does and read whole groups (#3005) - #3055
Merged
Merged
Conversation
…samples (#3005) The oracle exports get_sample_peak() and get_true_peak() since #3003, so the peak kernels get their own Tier 3 tests, compared bit for bit: a hand case and a property test with NaN, infinities and -0.0. _mm_max_ps returns its second operand when either is NaN; the Rust lanes use f32::max, which returns the other one. With a NaN in a lane's last group the C drops that lane's earlier peak (the final fmaxf skips the NaN), the Rust keeps it: sample_peak: rust 0.5 c 0 for [0, 0, 0, 0] [0.5, 0, 0, 0, NaN, 0, 0, 0] Both new tests fail on the current peak.rs (macOS, simde oracle). Co-Authored-By: Claude Opus 5.5 <[email protected]>
#3005) The three lane maxima of sample_peak() and true_peak() use max_ps(a, b), `a > b ? a : b`, which is one lane of _mm_max_ps: b when either is NaN. f32::max returned the other operand and so kept a peak the C drops. The final hmax stays on f32::max, as the C's fmaxf. The comparison form is also what compilers turn into maxps; the numbers are in the PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
sample_peak() and true_peak() indexed samples[i + lane] against `frames`, so every read kept its own bounds check and the compiler loaded the four lanes one by one. They now walk samples[..frames / 4 * 4].as_chunks::<4>(): the same whole groups the C's `i + 3 < nr_samples` reads, as [f32; 4] arrays, and a panic in exactly the cases the old indexing panicked. The numbers are in the PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
vpacher
marked this pull request as ready for review
October 30, 2026 11:17
Member
Author
|
Ready for review. CI is green ( Auto-merge is off on purpose: please comment before it merges. The assembly is on #3005 (#3005 (comment)). |
zackees
reviewed
Oct 30, 2026
zackees
left a comment
Member
There was a problem hiding this comment.
Coordinator review: LGTM.
- NaN parity:
max_ps(a, b) = a > b ? a : bis one lane of_mm_max_ps(a, b)(it returnsbon NaN). Every call keeps the C's argument order (peakfirst, the new value second, asobs-audio-controls.c.h:321-354does), andhmaxstays onf32::max, matchingfmaxf. The proptest regression (samples = [NaN, 0, 0, 0], where the C'strue_peakreturns NaN) is kept. - Panics unchanged: the old loop panicked exactly when a whole group reached past
samples.len(), that is whenframes / 4 * 4 > len.samples[..frames / 4 * 4]panics in exactly that case.as_chunksis stable on the pinned toolchain. - Tiers: new bit-exact Tier 3 cases against the oracle (a hand case plus
random_peaks_match_cwith NaN, infinities and -0.0) on SSE, simde and MSVC; Tier 2 629/629 against the hybrid libobs. No intrinsics, so nosrc/platform/path is needed. - Numbers: x86
sample_peakgoes 7.15 → 0.93 (GCC) and 3.73 → 0.46 (MSVC).true_peakon aarch64 goes 0.61 → 0.99, but the old lead came from the wrong NaN rule, so parity wins. That is the right trade under testing-policy.md:296.
Queuing. Afterwards #3005 can close and #3009 can mark it done.
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 #3005
What and why
The volume meter's peak kernels (
rust/obs-audio-controls/src/peak.rs) were 7.2 times slower than the SSE C forsample_peakon x86 (#3029; reproduced independently in #3005 (comment) at 7.39x), and they did not match the C when a sample is NaN.f32::max, which returns the other operand when one is NaN._mm_max_ps(a, b)returnsb. With a NaN in a lane's last group the C drops that lane's peak (the finalfmaxfthen skips the NaN); the Rust kept it. Fortrue_peakthe C can return NaN where the Rust returned a number. The lanes now usemax_ps(a, b) = if a > b { a } else { b }, one lane of_mm_max_ps;hmaxstays onf32::max, as the C'sfmaxf.samples[i + lane]againstframes, so each read kept its own bounds check and the lanes were loaded one by one. They now walksamples[..frames / 4 * 4].as_chunks::<4>(): the same whole groups as the C'si + 3 < nr_samples, as[f32; 4]arrays, and a panic in exactly the cases the old indexing panicked.No
std::archcode: the scalar Rust now compiles to the C's instructions on x86 (movups,andps,maxpsper group, no bounds check) and to a compare-and-select (fcmgt.4s+bif.16b) on aarch64, where NEON's ownfmax/fmaxnmhave other NaN rules. So there is nosrc/platform/path and no runtime detection. The assembly is on #3005 (acceptance criterion 1).Numbers
record.py --category volmeter --repeats 3, before = master 700e7a6, after = this branch:--cpu 27)sample_peak/1024true_peak/1024sample_peak/1024true_peak/1024sample_peak/1024true_peak/1024x86, by step: the parity change alone took
sample_peakto 236 ns (3.65) andtrue_peakto 886 ns (0.52); the slice change tooksample_peakthe rest of the way.On aarch64
true_peakloses the lead it had (0.61 → 0.99): it came fromf32::maxbeing onefmaxnm, with the NaN rule that differed from the C. With the C's rule the Rust does the same work as the C (which runs_mm_max_psthrough simde there).Tests
The oracle exports
get_sample_peak()/get_true_peak()since #3029, so the kernels get Tier 3 tests of their own inrust/obs-audio-controls/tests/audio_controls/parity.rs, compared bit for bit: a hand case (nan_in_the_last_group_matches_c) and a property test (random_peaks_match_c, NaN, infinities and -0.0,parity::cases(2000)). The test header no longer says the peak path is cmocka-only.proptest-regressions/parity.txtkeeps the case proptest found on RED (previous = [0, 0, 0, 0], samples = [NaN, 0, 0, 0]: fortrue_peakthe C returns NaN).RED → GREEN
sample_peak: rust 0.5 c 0 for [0, 0, 0, 0] [0.5, 0, 0, 0, NaN, 0, 0, 0]; on x86 the property test also findstrue_peak: rust inf c NaN.max_ps), then 02d0987 (one slice,as_chunks).audio_controls24/24 on macOS, on x86_64 Linux and against the MSVC oracle on Windows.peak.rs). lint passes exceptci/test_analyze_project.py, which fails there on master since ci: port the macOS analysis steps to ci/analyze_project.py, clearing the analyze-project GEN-005 block (#3040) #3043 (jq 1.7, ci: test_analyze_project.py's reference jq program fails with jq 1.7 (Debian 13, macOS) #3051); so the head is not stamped and PR CI runs the full suite.Checklist
master🤖 Generated with Claude Code