On Fri Feb 20 16:21:45 2026 +0000, Matteo Bruni wrote:
I had a hard time decoding the modulo here and I kept thinking that sometimes it might not give an exact result. For the former, after the modulo we have a number in the range `[0,freqAdjustDen)` representing the position within the specific input sample. Multiplying it by `dsbfirstep / freqAdjustDen` we compute `idx` in the expected `dsbfirstep` units. For the latter, I guess this works out fine in practice, with `freqAdjustDen` >> `dsbfirstep`. I don't know that there is anything to change here. I guess some kind of comment might be nice, although I'm hard pressed with coming up with a good comment myself. The resulting values should be identical. Here is a proof:
New value: `idx = dsbfirstep - 1 - (freqAcc_start + i * dsb->freqAdjustNum) % dsb->freqAdjustDen * dsbfirstep / dsb->freqAdjustDen` Old value: `idx = dsbfirstep - 1 - (freqAcc_start + i * dsb->freqAdjustNum) * dsbfirstep / dsb->freqAdjustDen % dsbfirstep` Let's denote `S = dsbfirstep, a = freqAcc_start + i * dsb->freqAdjustNum, D = dsb->freqAdjustDen`, so we have to prove that `S - 1 - a % D * S / D == S - 1 - a * S / D % S`. Subtract `S - 1` from both sides and multiply them by `-1`: `a % D * S / D == a * S / D % S`. Rewrite `a` as `a % D + a / D * D`: `(a % D + a / D * D) % D * S / D == (a % D + a / D * D) * S / D % S`. Adding a multiple of `D` doesn't change the modulus: `a % D * S / D == (a % D + a / D * D) * S / D % S`. Expand the parentheses on the right side: `a % D * S / D == a % D * S / D % S + a / D * D * S / D % S`. `a % D` is always less than `D`, so `a % D * S / D` is always less than `S`, which means that we can eliminate the `% S` in the first term on the right side: `a % D * S / D == a % D * S / D + a / D * D * S / D % S`. `D / D` cancels out in the second term on the right side: `a % D * S / D == a % D * S / D + a / D * S % S`. The second term on the right side is zero as it's a multiple of `S` modulo `S`: `a % D * S / D == a % D * S / D`. Q.E.D. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/10146#note_130160