Algorithm Search⌘K

Proofs: SRT division

These are the human proofs of SRT division, at full length.

Radix-4 SRT division returns the exact quotient and remainder full

This proof shows: correctness.

Setting. x,d∈[1/2,1)x, d \in [1/2, 1), radix 4, digits qj∈{−2,…,2}q_j \in \{-2, \ldots, 2\}, ρ=2/3\rho = 2/3. Write yj=4wj−1y_j = 4 w_{j-1}. The divisor estimate d^=⌊16d⌋/16\hat d = \lfloor 16 d \rfloor / 16 and the remainder estimate y^j=⌊8yj⌋/8\hat y_j = \lfloor 8 y_j \rfloor / 8 satisfy d^≤d<d^+1/16\hat d \le d < \hat d + 1/16 and y^j≤yj<y^j+1/8\hat y_j \le y_j < \hat y_j + 1/8. A cell is the set C(d^,y^)={(y,d):d^≤d<d^+116, y^≤y<y^+18, ∣y∣≤4ρd}C(\hat d, \hat y) = \{(y, d) : \hat d \le d < \hat d + \tfrac{1}{16},\ \hat y \le y < \hat y + \tfrac18,\ |y| \le 4\rho d\}.

Table condition (T). For every nonempty cell the table digit kk satisfies, for all (y,d)(y, d) in the cell, y≥(k−ρ)dy \ge (k - \rho)d unless k=−2k = -2, and y≤(k+ρ)dy \le (k + \rho)d unless k=2k = 2. The implementation checks (T) by evaluating the two linear functions at the corners of each cell in exact rationals; a linear function on a box attains its extremes at corners, and the strict upper edges only make the check conservative.

Lemma 1 (invariant). Under (T), ∣wj∣≤ρd|w_j| \le \rho d for j=0,…,nj = 0, \ldots, n.

Proof. w0=x/4<1/4w_0 = x/4 < 1/4, and ρd≥23⋅12=13\rho d \ge \tfrac23 \cdot \tfrac12 = \tfrac13. Suppose ∣wj−1∣≤ρd|w_{j-1}| \le \rho d. Then ∣yj∣≤4ρd|y_j| \le 4\rho d, so (yj,d)(y_j, d) lies in the cell C(d^,y^j)C(\hat d, \hat y_j), which is therefore nonempty, and the digit k=qjk = q_j read from the table satisfies (T) at (yj,d)(y_j, d). If k≠−2k \ne -2 then wj=yj−kd≥−ρdw_j = y_j - kd \ge -\rho d; if k=−2k = -2 then wj=yj+2d≥−4ρd+2d=−23d=−ρdw_j = y_j + 2d \ge -4\rho d + 2d = -\tfrac23 d = -\rho d. Symmetrically wj≤ρdw_j \le \rho d, using yj≤4ρdy_j \le 4\rho d when k=2k = 2: wj≤83d−2d=ρdw_j \le \tfrac83 d - 2d = \rho d. □\square

Lemma 2 (series). x4d=∑j=1nqj4−j+wn4nd\dfrac{x}{4d} = \sum_{j=1}^{n} q_j 4^{-j} + \dfrac{w_n}{4^n d}.

Proof. From wj=4wj−1−qjdw_j = 4w_{j-1} - q_j d, dividing by 4jd4^j d gives wj−14j−1d=qj4−j+wj4jd\dfrac{w_{j-1}}{4^{j-1} d} = q_j 4^{-j} + \dfrac{w_j}{4^j d}. Summing from j=1j = 1 to nn telescopes to the claim, since w0/d=x/(4d)w_0/d = x/(4d). □\square

Theorem. After line L7, Q=⌊x 4n−1/d⌋Q = \lfloor x\,4^{n-1}/d \rfloor and 0≤w<d0 \le w < d, with x4d=Q4−n+w4−n/d\frac{x}{4d} = Q 4^{-n} + w 4^{-n}/d.

Proof. Line L6 accumulates Q=∑j=1nqj4n−jQ = \sum_{j=1}^n q_j 4^{n-j}, an integer. By Lemma 2, x4n−1/d=Q+wn/dx 4^{n-1}/d = Q + w_n/d, and by Lemma 1, wn/d∈[−23,23]w_n/d \in [-\tfrac23, \tfrac23]. If wn≥0w_n \ge 0 then wn/d∈[0,1)w_n/d \in [0, 1) and QQ is the floor. If wn<0w_n < 0, line L7 replaces (Q,wn)(Q, w_n) by (Q−1,wn+d)(Q - 1, w_n + d), which keeps Q+w/dQ + w/d unchanged and puts w/d∈[13,1)w/d \in [\tfrac13, 1); again QQ is the floor. □\square

Corollary (correct rounding). Let a=ma2eaa = m_a 2^{e_a}, b=mb2ebb = m_b 2^{e_b} with ma,mb∈[1/2,1)m_a, m_b \in [1/2, 1) binary64 significands, n=28n = 28, and suppose a/ba/b is a normal number. Then Q=⌊(ma/mb)254⌋Q = \lfloor (m_a/m_b) 2^{54} \rfloor lies in [253,255)[2^{53}, 2^{55}), and ma/mb=(Q+θ)2−54m_a/m_b = (Q + \theta) 2^{-54} with θ∈[0,1)\theta \in [0, 1) and θ=0\theta = 0 iff w=0w = 0. Let Q=K2s+RQ = K 2^{s} + R with 0≤R<2s0 \le R < 2^s and KK of exactly 53 bits (s∈{1,2}s \in \{1, 2\}). The exact value lies in (K+(R+θ)2−s)2s−54(K + (R + \theta) 2^{-s}) 2^{s - 54}, and round to nearest, ties to even, picks K+1K + 1 iff (R+θ)2−s>1/2(R + \theta) 2^{-s} > 1/2, or =1/2= 1/2 and KK is odd. Since RR is an integer and θ∈[0,1)\theta \in [0, 1), (R+θ)2−s>1/2(R + \theta)2^{-s} > 1/2 iff R>2s−1R > 2^{s-1}, or R=2s−1R = 2^{s-1} and θ>0\theta > 0; and =1/2= 1/2 iff R=2s−1R = 2^{s-1} and θ=0\theta = 0. This is exactly the test in srt_float_divide, and the scaling by 2ea−eb2^{e_a - e_b} is exact for a normal result. □\square

Back to SRT division