Skip to content

rust: Port util/util_uint64.h (util_mul_div64) to obs-util - #122

Merged
zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/util-uint64
Oct 10, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/util-uint64

Conversation

@swilk19

@swilk19 swilk19 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Ports util_mul_div64 from libobs/util/util_uint64.h to obs-util (obs_util::util_uint64::mul_div64), as item 2 of the media-io part 2 order on #45. audio_frames_to_ns / ns_to_audio_frames in audio-io.h are thin wrappers around it, so it comes before the audio-io.h inlines. No intentional differences.

Header-inline exemption. The function is static inline, so nothing is swapped or exported, there is no ENABLE_RUST_LIBOBS guard and no rust-exports.txt entry, and the header is unmodified. The header defines no struct, so Tier 2 is just the unchanged cmocka test passing OFF and ON. Tier 1 and Tier 3 run against a non-inline oracle_util_mul_div64 wrapper.

Per-platform math. The C does different math per platform, and the port matches each:

  • MSVC x64 (_MSC_VER >= 1920): _umul128 + _udiv128, exact 128-bit. Rust: u128 under cfg(all(target_env = "msvc", target_arch = "x86_64")).
  • Everywhere else: (num / div) * mul + (rem * mul) / div in u64. Rust: wrapping_mul / wrapping_add.

The two differ once rem * mul overflows, so the cmocka test pins those cases per path with the header's own #if, and parity runs against each platform's own C (Windows CI covers the MSVC path).

Exclusions (listed in tests/util_uint64_parity.rs, the README row and here for #45). The port panics on both instead of guessing:

  • div == 0: UB on both paths.
  • On MSVC x64, quotients that do not fit in 64 bits: _udiv128 faults. Off MSVC they wrap, and are compared.

Oracle. oracle/util_uint64.c with its own binding module (src/util_uint64.rs). util_uint64.h uses uint64_t without including <stdint.h>, so the wrapper and the cmocka test include util/c99defs.h first.

Motivation and Context

Item 2 of the order approved on #45 (#45 (comment)). It touches none of the obs-media-io files, so per that comment it doesn't count toward the two-PR limit. The audio-io.h port will use it.

Refs #45

How Has This Been Tested?

Linux x86_64 (WSL2), Rust via soldr.

  • Commits follow the policy: test/cmocka/test_util_uint64.c (C characterization, passes OFF and ON before any Rust), then RED (todo!() core), then GREEN.
  • The cmocka test pins the audio frames <-> ns conversions (1024 and 48000 frames at 48 kHz, 1 frame at 44.1 kHz, a 1e18 ns timestamp), a 30000/1001 frame interval, zero operands, div == 1, and inputs whose num * mul exceeds 64 bits while rem * mul does not (both paths exact). Per path: three rem * mul overflow cases, and off MSVC a quotient past 64 bits wrapping. Characterized, not endorsed.
  • soldr cargo test -p obs-util: Tier 1 ports the cmocka cases 1:1 with the same platform split, plus div == 0 panicking, the MSVC-only overflow panic, and exactness whenever rem * mul fits. Tier 3 sweeps all 16³ combinations of values around the 32- and 64-bit edges and runs proptest (20,000 cases each) over arbitrary u64s, edge-biased u64s, and small-quotient/large-remainder inputs that force rem * mul to overflow.
  • RED→GREEN, checked with soldr's cache disabled and a fresh target dir: at the RED commit util_uint64 fails 9/10 (the passing one is the should_panic div == 0 test) and util_uint64_parity fails 4/4; at head both pass.
  • Mutation check: computing the exact quotient off MSVC fails all four parity tests; dividing the remainder term by div | 1 fails the edge sweep and the edge-biased proptest. Both reverted.
  • The MSVC branch is clippy-clean with --target x86_64-pc-windows-msvc (checked that the branch is actually type-checked); it runs in the Windows Rust Tests job.
  • ./lint and ./unittest: ok. tier2-linux ran test_util_uint64 with ENABLE_RUST_LIBOBS OFF and ON (46/46 each, rebased onto master after plugins: Reconnect Linux capture after PipeWire restarts #109 and rust: Port media-io video-io.h and frame-rate.h inlines to obs-media-io #114). The libobs exported symbols are identical OFF vs ON (1752). HEAD carries the local-gate stamp.
  • macOS and Windows were not run locally; the Rust Tests and rust-tier2 CI jobs cover them.

Types of changes

  • Code cleanup (non-breaking change which makes code smaller or more readable)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.

🤖 Generated with Claude Code

swilk19 and others added 3 commits October 10, 2026 18:22
Pins util_mul_div64 from util/util_uint64.h before the Rust port: the
audio frames <-> ns conversions audio-io.h and obs-source.c use it
for, a 30000/1001 frame interval, zero operands, div 1, and operands
whose num * mul exceeds 64 bits while rem * mul does not, which both
platform paths compute exactly.

The header does the math two ways. MSVC x64 uses _umul128/_udiv128
(exact 128-bit), everything else (num / div) * mul + (rem * mul) / div
in u64. Once rem * mul overflows, the results differ, so those cases
are guarded with the header's own condition and pinned per path. The
non-MSVC path also pins a quotient that does not fit in 64 bits
wrapping; on MSVC x64 _udiv128 faults there, so it is not tested.
Both are characterized, not endorsed. div == 0 is UB on both paths and
is left out.

util_uint64.h uses uint64_t without including <stdint.h>, so the test
includes util/c99defs.h first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 1 and Tier 3 tests for util_mul_div64 against a todo!() core,
obs_util::util_uint64::mul_div64. util_uint64.h is header-inline and
defines no struct, so there is no shim, no layout test and no symbol
swap; Tier 2 is the unchanged characterization test passing OFF/ON.

The oracle wraps the header in a non-inline oracle_util_mul_div64,
including util/c99defs.h first because the header uses uint64_t
without including <stdint.h>.

Tier 1 ports test_util_uint64.c case for case, with the same split
between the MSVC x64 path (exact 128-bit) and the wrapping u64 path,
and adds div == 0 panicking (UB in C) and, on MSVC x64, a quotient
past 64 bits panicking (_udiv128 faults).

Parity sweeps every combination of 16 values around the 32- and
64-bit edges and runs proptest over arbitrary u64s, edge-biased u64s
and inputs with a large remainder, so rem * mul overflows. It runs
against each platform's own C, so Windows CI covers the MSVC path.
Excluded, and listed in the test file: div == 0, and on MSVC x64
quotients that do not fit in 64 bits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
util_mul_div64 gets a Rust equivalent, obs_util::util_uint64::mul_div64,
for Rust ports such as audio-io.h's audio_frames_to_ns and
ns_to_audio_frames. The header is static inline, so nothing is swapped
or exported. No intentional differences.

Like the header, the core picks its math per platform: on MSVC x64
(target_env = "msvc", target_arch = "x86_64") it multiplies and
divides in u128, matching _umul128/_udiv128; everywhere else it
computes (num / div) * mul + (rem * mul) / div with wrapping u64 ops.
Where the C is UB or faults it panics: div == 0, and on MSVC x64 a
quotient that does not fit in 64 bits. Parity excludes exactly those.

Mutation check: computing the exact quotient off MSVC fails every
parity test, and dividing the remainder term by (div | 1) fails the
edge sweep and the edge-biased proptest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=26b4bc2f1b9accd67421acf0f84aec1e94470b17 secs=36 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791670992,"gate":"rust/all/fmt","host":"linux-x86_64","key":"162bd981bc82885ba3fdd4801e4b0542cf6d911831a3566a4e22a21c08f15313","lane":"lint","parents":["95a719e8bcb6d1e4a08cd5ac45f84f5ed89f5c78"],"secs":9,"stamp":"18b3d4b29f200159761b21c6e26986a0","tree":"26b4bc2f1b9accd67421acf0f84aec1e94470b17","v":1,"via":"run"}
Ci-Attestation: {"at":1791670992,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"162bd981bc82885ba3fdd4801e4b0542cf6d911831a3566a4e22a21c08f15313","lane":"lint","parents":["95a719e8bcb6d1e4a08cd5ac45f84f5ed89f5c78"],"secs":9,"stamp":"f92880c8169ef7654be289e9cca5275a","tree":"26b4bc2f1b9accd67421acf0f84aec1e94470b17","v":1,"via":"run"}
Ci-Attestation: {"at":1791670992,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"a75ecddbf3f8b1643cc5c4421abe29a4a40ddd34a9ffd45022589a391753be1c","lane":"rust-test","parents":["95a719e8bcb6d1e4a08cd5ac45f84f5ed89f5c78"],"secs":14,"stamp":"53485b115ea98676f203864e12b04415","tree":"26b4bc2f1b9accd67421acf0f84aec1e94470b17","v":1,"via":"run"}
Ci-Attestation: {"at":1791670992,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"a203c1c8f8d5c3d49733ac29363792f930029fd582faa968232f5e9f5e679e3d","lane":"tier2-linux","parents":["95a719e8bcb6d1e4a08cd5ac45f84f5ed89f5c78"],"secs":12,"stamp":"72328cdf2e4b534330bfab8c2e893e75","tree":"26b4bc2f1b9accd67421acf0f84aec1e94470b17","v":1,"via":"run"}
@zackees
zackees merged commit b3cc16f into obs-rust:master Oct 10, 2026
20 checks passed
@swilk19
swilk19 deleted the port/util-uint64 branch October 10, 2026 22:36
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.

2 participants