15 // Compute the reciprocal square root of a digit sequence.
16 // Input: UDS a_MSDptr/a_len/.. of length a_len,
18 // [i.e. 1/4*beta^a_len <= a < beta^a_len]
19 // Output: UDS b_MSDptr/b_len+2/.. of length b_len+1 (b_len>1), plus 1 more bit
20 // in the last limb) such that
21 // 1 <= b <= 2 [i.e. beta^b_len <= b <= 2*beta^b_len]
22 // and | 1/sqrt(a) - b | < 1/2*beta^(-b_len).
23 // If a_len > b_len, only the most significant b_len+1 limbs of a are used.
24 extern void cl_UDS_recipsqrt (const uintD* a_MSDptr, uintC a_len,
25 uintD* b_MSDptr, uintC b_len);
27 // Using Newton iteration for computation of x^-1/2.
28 // The Newton iteration for f(y) = x-1/y^2 reads:
29 // y --> y - (x-1/y^2)/(2/y^3) = y + y*(1-x*y^2)/2 =: g(y).
30 // We have T^3-3*T+2 = (T-1)^2*(T+2), hence
31 // 1/sqrt(x) - g(y) = 1/(2*sqrt(x)) * (sqrt(x)*y-1)^2 * (sqrt(x)*y+2).
32 // Hence g(y) <= 1/sqrt(x).
33 // If we choose 0 < y_0 <= 1/sqrt(x), then set y_(n+1) := g(y_n), we will
34 // always have 0 < y_n <= 1/sqrt(x).
36 // 1/sqrt(x) - g(y) = sqrt(x)*(sqrt(x)*y+2)/2 * (1/sqrt(x) - y)^2,
37 // which is >= 0 and < 3/2 * (1/sqrt(x) - y)^2, we have a quadratically
38 // convergent iteration.
39 // For n = 1,2,...,b_len we compute approximations y with 1 <= yn <= 2
40 // and | 1/sqrt(x) - yn | < 1/2*beta^(-n).
42 // Compute the isqrt of the leading two digits of x, yields one digit.
43 // Compute its reciprocal, then do one iteration as below (n=0 -> m=1).
44 // Step n -> m with n < m <= 2*n:
45 // Write x = xm + xr with 0 <= xr < beta^-(m+1).
46 // Set ym' = yn + (yn*(1-xm*yn*yn))/2, round down to a multiple ym
48 // (Actually, compute yn*yn, round up to a multiple of beta^-(m+1), [1]
49 // multiply with xm, round up to a multiple of beta^-(m+1), [2]
50 // subtract from 1, no rounding needed, [2]
51 // multiply with yn, round down to a multiple of beta^-(m+1), [5]
52 // divide by 2, round down to a multiple of beta^-(m+1), [3]
53 // add to yn, no rounding needed. [Max rounding error: ^])
54 // The exact value ym' (no rounding) would satisfy
55 // 0 <= 1/sqrt(xm) - ym' < 3/2 * (1/sqrt(xm) - yn)^2
56 // < 3/8 * beta^(-2*n) by hypothesis,
58 // The rounding errors all go into the same direction, so
59 // 0 <= ym' - ym < 3 * beta^-(m+1) < 1/4 * beta^-m.
60 // Combine both inequalities:
61 // 0 <= 1/sqrt(xm) - ym < 1/2 * beta^-m.
62 // Neglecting xr can introduce a small error in the opposite direction:
63 // 0 <= 1/sqrt(xm) - 1/sqrt(x) = (sqrt(x) - sqrt(xm))/(sqrt(x)*sqrt(xm))
64 // = xr / (sqrt(x)*sqrt(xm)*(sqrt(x)+sqrt(xm)))
65 // <= 4*xr < 4*beta^-(m+1) < 1/2*beta^-m.
66 // Combine both inequalities:
67 // | 1/sqrt(x) - ym | < 1/2 * beta^-m.
68 // (Actually, choosing the opposite rounding direction wouldn't hurt either.)
70 // So that the computation is minimal, e.g. in the case b_len=10:
71 // 1 -> 2 -> 3 -> 5 -> 10 and not 1 -> 2 -> 4 -> 8 -> 10.
72 void cl_UDS_recipsqrt (const uintD* a_MSDptr, uintC a_len,
73 uintD* b_MSDptr, uintC b_len)
75 var uintC y_len = b_len+2;
76 var uintC x_len = (a_len <= b_len ? a_len : b_len+1);
77 var const uintD* const x_MSDptr = a_MSDptr;
83 num_stack_alloc(y_len,y_MSDptr=,);
84 num_stack_alloc(2*y_len,y2_MSDptr=,);
85 num_stack_alloc(2*y_len,y3_MSDptr=,);
86 num_stack_alloc(2*y_len,y4_MSDptr=,);
88 { var uintD x1 = mspref(x_MSDptr,0);
89 var uintD x2 = (a_len > 1 ? mspref(x_MSDptr,1) : 0);
93 isqrtD(x1,x2, y1=,sqrtp=);
96 if (!sqrtp) // want to compute 1/sqrt(x) rounded down
98 goto step1_done; // 1/1.0000 = 1.0000
99 // Set y0|y1 := 2^(2*intDsize)/y1
100 // = 2^intDsize + (2^(2*intDsize)-2^intDsize*y1)/y1.
101 if ((uintD)(-y1) >= y1) {
105 divuD(highlowDD_0((uintD)(-y1)),y1, y1=,);
107 divuD((uintD)(-y1),0,y1, y1=,);
111 mspref(y_MSDptr,0) = y0;
112 mspref(y_MSDptr,1) = y1;
116 integerlength32((uint32)b_len-1,k=);
117 // 2^(k-1) < b_len <= 2^k, so we need k steps, plus one
118 // one more step at the beginning (because step 1 was not complete).
121 { var uintC m = ((b_len-1)>>k)+1; // = ceiling(b_len/2^k)
122 // Compute ym := yn + (yn*(1-xm*yn*yn))/2, rounded.
123 // Storage: at y_MSDptr: (1 + n+1) limbs, yn.
124 // at y2_MSDptr: (2 + 2*n+2) limbs, yn^2.
125 // at y3_MSDptr: (1 + m+1) limbs, xm*yn*yn, 1-xm*yn*yn.
126 // at y4_MSDptr: (2-n + m+n+2) limbs, yn*(1-xm*yn*yn).
127 clear_loop_msp(y_MSDptr mspop (n+2),m-n);
128 cl_UDS_mul_square(y_MSDptr mspop (n+2),n+2,
129 y2_MSDptr mspop 2*(n+2));
130 var uintC xm_len = (m < x_len ? m+1 : x_len);
131 var uintC y2_len = m+2; // = (m+1 <= 2*n+2 ? m+2 : 2*n+3);
132 cl_UDS_mul(x_MSDptr mspop xm_len,xm_len,
133 y2_MSDptr mspop (y2_len+1),y2_len,
134 y3_MSDptr mspop (xm_len+y2_len));
135 if (mspref(y3_MSDptr,0)==0)
137 { neg_loop_lsp(y3_MSDptr mspop (m+2),m+2);
138 mspref(y3_MSDptr,0) += 1;
139 if (test_loop_msp(y3_MSDptr,n)) cl_abort(); // check 0 <= y3 < beta^-(n-1)
140 cl_UDS_mul(y_MSDptr mspop (n+2),n+2,
141 y3_MSDptr mspop (m+2),m+2-n,
142 y4_MSDptr mspop (m+4));
143 shift1right_loop_msp(y4_MSDptr,m+3-n,0);
144 if (addto_loop_lsp(y4_MSDptr mspop (m+3-n),y_MSDptr mspop (m+2),m+3-n))
145 if ((n<1) || inc_loop_lsp(y_MSDptr mspop (n-1),n-1)) cl_abort();
148 // xm*yn*yn >= 1 (this can happen since xm >= xn)
149 { mspref(y3_MSDptr,0) -= 1;
150 if (test_loop_msp(y3_MSDptr,n)) cl_abort(); // check 0 >= y3 > -beta^-(n-1)
151 cl_UDS_mul(y_MSDptr mspop (n+2),n+2,
152 y3_MSDptr mspop (m+2),m+2-n,
153 y4_MSDptr mspop (m+4));
154 shift1right_loop_msp(y4_MSDptr,m+3-n,0);
155 if (subfrom_loop_lsp(y4_MSDptr mspop (m+3-n),y_MSDptr mspop (m+2),m+3-n))
156 if ((n<1) || dec_loop_lsp(y_MSDptr mspop (n-1),n-1)) cl_abort();
159 // n = ceiling(b_len/2^k) limbs of y have now been computed.
161 copy_loop_msp(y_MSDptr,b_MSDptr,b_len+2);
163 // Bit complexity (N := b_len): O(M(N)).