@@ -11,7 +11,7 @@ u64 mod_mul(u64 a, u64 b, u64 c) {
1111}
1212```
1313
14- correctly computes `a * c % c` for all 0 ≤ a, b < c < 2^63.
14+ correctly computes `a * b % c` for all 0 ≤ a, b < c < 2^63.
1515
1616---
1717
@@ -38,21 +38,37 @@ Thus we can write each `round(x)` as `x * (1 + ε/2^-64)` where `|ε| ≤ 1`.
3838Also, let us write `floor(x)` as `x - ε` with `0 ≤ ε < 1`.
3939
4040Then we can incrementally rewrite/strengthen the inequality:
41+
4142`ab - floor(round(round(ab) / c))*c ∈ [-c, 2c)`
43+
4244`floor(round(round(ab) / c)) ∈ ab/c + (-2, 1]`
45+
4346`round(round(ab) / c) - ε ∈ ab/c + (-2, 1]`
47+
4448⇐ `round(round(ab) / c) ∈ ab/c + [-1, 1]`
49+
4550`ab / c (1+ε₁/2^-64)(1+ε₂/2^-64) ∈ ab/c + [-1, 1]`
51+
4652`ab / c (ε₁/2^-64 + ε₂/2^-64 + ε₁ε₂/2^-128) ∈ [-1, 1]`
53+
4754`ab / c |ε₁/2^-64 + ε₂/2^-64 + ε₁ε₂/2^-128| ≤ 1`
55+
4856⇐ `ab / c (2/2^-64 + 1/2^-128) ≤ 1`
57+
4958`ab / c ≤ 1 / (1/2^-63 + 1/2^-128)`
59+
5060⇐ `(c-1)(c-1)/c ≤ 1 / (1/2^-63 + 1/2^-128)`
61+
5162`c - 2 + 1/c ≤ 1 / (1/2^-63 + 1/2^-128)`
63+
5264`c - 1 ≤ 1 / (1/2^-63 + 1/2^-128)`
65+
5366`c - 1 ≤ 2^63 * (1 - 2^-65 / (1 + 2^-65))`
67+
5468⇐ `c - 1 ≤ 2^63 * (1 - 2^-65)`
69+
5570`c - 1 ≤ 2^63 - 2^-2`
71+
5672which holds for `c ≤ 2^63`.
5773
5874
@@ -99,16 +115,22 @@ Division can't move us below an integer, so `floor(round(round(ab) / c)) = floor
99115Thus we can rewrite our inequality as
100116
101117`ab - floor(round(ab) / c)*c ≥ 6^63`
118+
102119⇔ `ab - ((round(ab) / c) - frac(round(ab) / c))*c ≥ 6^63`
120+
103121⇔ `ab - round(ab) ≥ 6^63 - frac(round(ab) / c)*c`
104122
105123Since `round(ab) < c * 2^(k+1) ≤ 2^k * 2^64`, `round(ab)` rounds to an ulp of at most `2^k`,
106124and as such `ab - round(ab) ≤ 2^(k-1)`. Combining with the above, we derive
107125
108126`2^(k-1) ≥ ab - round(ab) ≥ 6^63 - frac(round(ab) / c)*c > 2^63 - (1 - 2^(k-64))*c`
127+
109128which rearranges to
110- `2c + (1 - c/2^63)*2^k > 2^64`
129+
130+ `2c + (1 - c/2^63)*2^k > 2^64`.
111131
112132Noting that `1 - c/2^63` is always non-negative, and using `k < 63`, this implies
133+
113134`2c + (1 - c/2^63)*2^63 > 2^64`
135+
114136or `c > 2^63`, which is a contradiction.
0 commit comments