Algorithm Search⌘K

Proofs: Fast inverse square root

These are the human proofs of Fast inverse square root, at full length.

The fast inverse square root has relative error at most 0.0017525 full

This proof shows: correctness.

Setting. xx is a binary32 float with 2−125≤x≤(2−2−23)21272^{-125} \le x \le (2 - 2^{-23})2^{127}. All floating-point operations are binary32 operations, correctly rounded to nearest, so each one satisfies the standard model fl(aopb)=(aopb)(1+δ)\mathrm{fl}(a \mathbin{op} b) = (a \mathbin{op} b)(1 + \delta), ∣δ∣≤u|\delta| \le u, with u=2−24u = 2^{-24}, when its exact result is in the normal range. R=0x5F3759DFR = \texttt{0x5F3759DF}. We write y0y_0 for the value of line L4, y1y_1 for the result, ε0=y0x−1\varepsilon_0 = y_0\sqrt{x} - 1 and ε1=y1x−1\varepsilon_1 = y_1 \sqrt{x} - 1.

Theorem. −0.0017525≤ε1≤2×10−7-0.0017525 \le \varepsilon_1 \le 2 \times 10^{-7}.

Step 0 (the encoding). A positive normal binary32 float 2E(1+k/223)2^{E}(1 + k/2^{23}), with −126≤E≤127-126 \le E \le 127 and 0≤k<2230 \le k < 2^{23}, has the pattern 223(E+127)+k2^{23}(E + 127) + k. Conversely each pattern in [0x00800000,0x7F7FFFFF][\texttt{0x00800000}, \texttt{0x7F7FFFFF}] is a positive normal float. Line L1 is exact: x/2≥2−126x/2 \ge 2^{-126} is normal, and halving a normal float only lowers its exponent.

Step 1 (scaling). Let i=BITS(x)i = \mathrm{BITS}(x). The domain gives 0x01000000≤i≤0x7F7FFFFF\texttt{0x01000000} \le i \le \texttt{0x7F7FFFFF}, so 0x00800000≤⌊i/2⌋≤0x3FBFFFFF\texttt{0x00800000} \le \lfloor i/2 \rfloor \le \texttt{0x3FBFFFFF} and the pattern g=R−⌊i/2⌋g = R - \lfloor i/2 \rfloor of line L3 lies in [0x1F7759E0,0x5EB759DF][\texttt{0x1F7759E0}, \texttt{0x5EB759DF}], so y0y_0 is a positive normal float. If xx and 4x4x are both in the domain, then BITS(4x)=i+224\mathrm{BITS}(4x) = i + 2^{24} by Step 0, so ⌊BITS(4x)/2⌋=⌊i/2⌋+223\lfloor \mathrm{BITS}(4x)/2 \rfloor = \lfloor i/2 \rfloor + 2^{23} and the pattern of the guess for 4x4x is g−223g - 2^{23}. Both patterns are normal, so by Step 0 the guess for 4x4x is y0/2y_0/2. Hence ε0(4x)=(y0/2)4x−1=ε0(x)\varepsilon_0(4x) = (y_0/2)\sqrt{4x} - 1 = \varepsilon_0(x). Each xx in the domain is 4jx′4^{j} x' for an integer jj and an x′∈[1,4)x' \in [1, 4), with all the intermediate values 4lx′4^{l} x' in the domain (the domain is a union of whole binades from 2−1252^{-125} on). So the set of values of ε0\varepsilon_0 on the domain is the set of its values on the floats of [1,4)[1, 4).

Step 2 (the guess on [1, 4)). Let tA=7821791/223t_A = 7821791/2^{23} and tB=3627487/223t_B = 3627487/2^{23}; these are the fraction fields of 0x3F7759DF\texttt{0x3F7759DF} and 0x3F3759DF\texttt{0x3F3759DF}, both of which have exponent field 126126. Let η=(k/2−⌊k/2⌋)/223∈{0,2−24}\eta = (k/2 - \lfloor k/2 \rfloor)/2^{23} \in \{0, 2^{-24}\}.

  • Piece A: x=1+mx = 1 + m, m=k/223m = k/2^{23}, 0≤k<2230 \le k < 2^{23}. Then i=0x3F800000+ki = \texttt{0x3F800000} + k and g=0x3F7759DF−⌊k/2⌋g = \texttt{0x3F7759DF} - \lfloor k/2 \rfloor. Since ⌊k/2⌋<222<7821791\lfloor k/2 \rfloor < 2^{22} < 7821791, the exponent field stays 126126 and y0=12(1+tA−m/2+η)y_0 = \tfrac12(1 + t_A - m/2 + \eta).
  • Piece B1: x=2(1+m)x = 2(1 + m), m=k/223m = k/2^{23}, 0≤k≤72549750 \le k \le 7254975. Then g=0x3F3759DF−⌊k/2⌋g = \texttt{0x3F3759DF} - \lfloor k/2 \rfloor with ⌊k/2⌋≤3627487\lfloor k/2 \rfloor \le 3627487, so y0=12(1+tB−m/2+η)y_0 = \tfrac12(1 + t_B - m/2 + \eta).
  • Piece B2: x=2(1+m)x = 2(1 + m), 7254976≤k<2237254976 \le k < 2^{23}. Now ⌊k/2⌋>3627487\lfloor k/2 \rfloor > 3627487, the subtraction borrows from the exponent field, which becomes 125125, and the fraction field is 223+3627487−⌊k/2⌋2^{23} + 3627487 - \lfloor k/2 \rfloor. So y0=14(2+tB−m/2+η)y_0 = \tfrac14(2 + t_B - m/2 + \eta).

Drop η\eta and define, for real mm in the closed range of each piece,

gA(m)=12(1+tA−m2)1+m−1,gB1(m)=12(1+tB−m2)2(1+m)−1,gB2(m)=14(2+tB−m2)2(1+m)−1.g_A(m) = \tfrac12(1 + t_A - \tfrac{m}{2})\sqrt{1+m} - 1, \quad g_{B1}(m) = \tfrac12(1 + t_B - \tfrac{m}{2})\sqrt{2(1+m)} - 1, \quad g_{B2}(m) = \tfrac14(2 + t_B - \tfrac{m}{2})\sqrt{2(1+m)} - 1.

Then ε0=g(m)+c ηx\varepsilon_0 = g(m) + c\,\eta\sqrt{x} with c=12c = \tfrac12 on pieces A and B1 and c=14c = \tfrac14 on B2. Since 0≤η≤2−240 \le \eta \le 2^{-24} and x<2\sqrt{x} < 2, we get g(m)≤ε0≤g(m)+6×10−8g(m) \le \varepsilon_0 \le g(m) + 6 \times 10^{-8}.

Each gg has the form L(m)m+1L(m)\sqrt{m + 1} times a constant, minus 1, with LL linear, decreasing and positive on the range. Then (L1+m)′′=2L′(1+m)′+L(1+m)′′<0(L\sqrt{1+m})'' = 2L'(\sqrt{1+m})' + L(\sqrt{1+m})'' < 0, so each gg is concave: its minimum over a closed interval is at an endpoint and its maximum is at the zero of g′g' when that zero is inside. The derivative of (a−m/2)1+m(a - m/2)\sqrt{1+m} is (a−1−3m/2)/(21+m)(a - 1 - 3m/2)/(2\sqrt{1+m}), which vanishes at m=23(a−1)m = \tfrac23(a - 1). This gives the critical points 23tA≈0.6216\tfrac23 t_A \approx 0.6216, 23tB≈0.2883\tfrac23 t_B \approx 0.2883 and 23(1+tB)≈0.9550\tfrac23(1 + t_B) \approx 0.9550, each inside its piece. The values, rounded outward in the ninth decimal, are:

piece range of mm at the left end at the right end at the critical point
A [0,1][0, 1] −0.033784926-0.033784926 0.0128810710.012881071 0.0325086100.032508610
B1 [0,7254975/223][0, 7254975/2^{23}] 0.0128810710.012881071 −0.034375800-0.034375800 0.0339601960.033960196
B2 [7254976/223,1][7254976/2^{23}, 1] −0.034375769-0.034375769 −0.033784926-0.033784926 −0.033593969-0.033593969

So g≥−0.0343758g \ge -0.0343758 everywhere and g≤0.0339602g \le 0.0339602 everywhere, and with the η\eta term,

−0.0343758≤ε0≤0.0339603.-0.0343758 \le \varepsilon_0 \le 0.0339603.

By Step 1 this holds on the whole domain.

Step 3 (an exact Newton step). Let a=1+ε0=y0xa = 1 + \varepsilon_0 = y_0\sqrt{x} and z=(x/2)y02=a2/2z = (x/2) y_0^2 = a^2/2. Then a(1.5−z)=a(3−a2)/2a(1.5 - z) = a(3 - a^2)/2, and with a=1+ε0a = 1 + \varepsilon_0,

a(3−a2)/2=12(1+ε0)(2−2ε0−ε02)=1−32ε02−12ε03=1−12ε02(3+ε0).a(3 - a^2)/2 = \tfrac12(1 + \varepsilon_0)(2 - 2\varepsilon_0 - \varepsilon_0^2) = 1 - \tfrac32 \varepsilon_0^2 - \tfrac12 \varepsilon_0^3 = 1 - \tfrac12 \varepsilon_0^2(3 + \varepsilon_0).

Write εˉ1=−12ε02(3+ε0)\bar\varepsilon_1 = -\tfrac12\varepsilon_0^2(3 + \varepsilon_0). For ε0∈[−0.0343758,0.0339603]\varepsilon_0 \in [-0.0343758, 0.0339603], εˉ1≤0\bar\varepsilon_1 \le 0, and ∣εˉ1∣=12ε02(3+ε0)|\bar\varepsilon_1| = \tfrac12\varepsilon_0^2(3 + \varepsilon_0) is increasing in ∣ε0∣|\varepsilon_0| on each side of 00; its values at the two ends are 0.001752240.00175224 and 0.001749540.00174954. So −0.0017523≤εˉ1≤0-0.0017523 \le \bar\varepsilon_1 \le 0.

Step 4 (the roundings of line L5). All exact intermediate results are normal: x/2x/2 is, (x/2)y0=ax/2(x/2) y_0 = a\sqrt{x}/2 lies in [2−64,264][2^{-64}, 2^{64}], and the other values are near 12\tfrac12 or 11, or near y0y_0. So the standard model gives

w=fl(fl(x2y0)y0)=z(1+θ),∣θ∣≤γ2=2u1−2u,y1=y0(1.5−w)(1+δ3)(1+δ4),w = \mathrm{fl}(\mathrm{fl}(x_2 y_0) y_0) = z(1 + \theta), \quad |\theta| \le \gamma_2 = \frac{2u}{1 - 2u}, \qquad y_1 = y_0 (1.5 - w)(1 + \delta_3)(1 + \delta_4),

with ∣δ3∣,∣δ4∣≤u|\delta_3|, |\delta_4| \le u (Lemma 3.1 of Higham, as stated on the page of the standard model, gives the first bound). Multiply by x\sqrt{x}:

1+ε1=(a(1.5−z)−azθ)(1+δ3)(1+δ4)=(1+εˉ1−azθ)(1+δ3)(1+δ4).1 + \varepsilon_1 = \bigl(a(1.5 - z) - a z \theta\bigr)(1 + \delta_3)(1 + \delta_4) = \bigl(1 + \bar\varepsilon_1 - a z\theta\bigr)(1 + \delta_3)(1 + \delta_4).

Here 0<a≤1.03396030 < a \le 1.0339603 and 0<z=a2/2≤0.5345370 < z = a^2/2 \le 0.534537, so ∣azθ∣≤0.55270⋅γ2≤1.1055u|a z \theta| \le 0.55270 \cdot \gamma_2 \le 1.1055u. With A=1+εˉ1−azθA = 1 + \bar\varepsilon_1 - az\theta, we have 1−0.0017523−1.1055u≤A≤1+1.1055u1 - 0.0017523 - 1.1055u \le A \le 1 + 1.1055u, and (1−u)2≤(1+δ3)(1+δ4)≤(1+u)2(1 - u)^2 \le (1+\delta_3)(1+\delta_4) \le (1+u)^2. Hence

1+ε1≤(1+1.1055u)(1+u)2≤1+3.106u,1+ε1≥(1−0.0017523−1.1055u)(1−2u)≥1−0.0017523−3.106u.1 + \varepsilon_1 \le (1 + 1.1055u)(1 + u)^2 \le 1 + 3.106u, \qquad 1 + \varepsilon_1 \ge (1 - 0.0017523 - 1.1055u)(1 - 2u) \ge 1 - 0.0017523 - 3.106u.

Since u=2−24<5.97×10−8u = 2^{-24} < 5.97 \times 10^{-8}, 3.106u<1.86×10−73.106u < 1.86 \times 10^{-7}. So −0.0017525≤ε1≤2×10−7-0.0017525 \le \varepsilon_1 \le 2 \times 10^{-7}. ■\blacksquare

Remark. The bound is tight up to the rounding term: the float x=2(1+7254974/223)=3.7297205924987793x = 2(1 + 7254974/2^{23}) = 3.7297205924987793 has y0=0.5y_0 = 0.5 exactly, ε0=−0.0343758\varepsilon_0 = -0.0343758 to 7 digits, and ε1=−0.0017523\varepsilon_1 = -0.0017523.

Back to Fast inverse square root