4 #include "base/cl_sysdep.h"
7 #include "base/digitseq/cl_DS.h"
14 // Compute the reciprocal value of a digit sequence.
15 // Input: UDS a_MSDptr/a_len/.. of length a_len,
16 // with 1/2*beta^a_len <= a < beta^a_len.
17 // Output: UDS b_MSDptr/b_len+2/.. of length b_len+1 (b_len>1), plus 1 more bit
18 // in the last limb, such that
19 // beta^b_len <= b <= 2*beta^b_len and
20 // | beta^(a_len+b_len)/a - b | < 1.
21 // If a_len > b_len, only the most significant b_len limbs + 3 bits of a
23 extern void cl_UDS_recip (const uintD* a_MSDptr, uintC a_len,
24 uintD* b_MSDptr, uintC b_len);
26 // Using Newton/Heron iteration.
27 // Write x = a/beta^a_len and y = b/beta^b_len.
28 // So we start out with 1/2 <= x < 1 and search an y with 1 <= y <= 2
29 // and | 1/x - y | < beta^(-b_len).
30 // For n = 1,2,...,b_len we compute approximations y with 1 <= yn <= 2
31 // and | 1/x - yn | < beta^(-n). The first n limbs of x, plus the
32 // next 3 bits (of the (n+1)st limb) enter the computation of yn. Apart
33 // from that, yn remains valid for any x which shares the same n+1
34 // most significant limbs.
36 // Write x = x1/beta + x2/beta^2 + xr with 0 <= xr < 1/(8*beta).
37 // Divide (beta^2-beta*x1-x2) by x1, gives beta^2-x1*beta-x2 = q*x1+r.
38 // If this division overflows, i.e. q >= beta, then x1 = beta/2, x2 = 0,
39 // and we just return y1 = 2.
40 // Else set qd := ceiling(max(q*x2/beta - r, 0) / (x1+1)) and return
41 // y1 = (beta+q-qd)/beta.
42 // Rationale: Obviously 0 <= qd <= q and 0 <= qd <= 2. We have
43 // beta^2 - beta*(beta+q-qd)*x
44 // <= beta^2 - (beta+q-qd)*(x1 + x2/beta)
45 // = beta^2 - beta*x1 - x2 - q*(x1 + x2/beta) + qd*(x1 + x2/beta)
46 // = q*x1 + r - q*(x1 + x2/beta) + qd*(x1 + x2/beta)
47 // = r - q*x2/beta + qd*(x1 + x2/beta)
48 // if qd=0: <= r <= x1-1 < x1
49 // if qd>0: < r - q*x2/beta + qd*(x1+1) <= x1
50 // hence always < x1 <= beta*x, hence
51 // 1 - x*y1 <= x/beta, hence 1/x - y1 <= 1/beta.
52 // And on the other hand
53 // beta^2 - beta*(beta+q-qd)*x
54 // = beta^2 - (beta+q-qd)*(x1 + x2/beta) - beta*(beta+q-qd)*xr
55 // where the third term is
56 // <= 2*beta^2*xr < beta/4 <= x1/2 <= beta*x/2.
58 // beta^2 - beta*(beta+q-qd)*x >
59 // > beta^2 - (beta+q-qd)*(x1 + x2/beta) - beta*x/2
60 // = r - q*x2/beta + qd*(x1 + x2/beta) - beta*x/2
61 // >= - qd - beta*x/2 > - beta*x, hence
62 // 1 - x*y1 >= -x/beta, hence 1/x - y1 >= -1/beta.
63 // Step n -> m with n < m <= 2*n:
64 // Write x = xm + xr with 0 <= xr < 1/(8*beta^m).
65 // Set ym' = 2*yn - xm*yn*yn,
66 // ym = ym' rounded up to be a multiple of 1/(2*beta^m).
68 // 1/x - ym <= 1/x - ym' = 1/x - 2*yn + (x-xr)*yn*yn
69 // <= 1/x - 2*yn + x*yn*yn = x * (1/x - yn)^2 < x*beta^(-2n)
70 // < beta^(-2n) <= beta^(-m), and
71 // 1/x - ym' = 1/x - 2*yn + (x-xr)*yn*yn
72 // > 1/x - 2*yn + x*yn*yn - 1/(2*beta^m)
73 // = x * (1/x - yn)^2 - 1/(2*beta^m) >= - 1/(2*beta^m), hence
74 // 1/x - ym > 1/x - ym' - 1/(2*beta^m) >= -1/beta^m.
75 // Since it is needed to compute ym as a multiple of 1/(2*beta^m),
76 // not only as a multiple of 1/beta^m, we compute with zn = 2*yn.
77 // The iteration now reads zm = round_up(2*zn - xm*zn*zn/2).
79 // So that the computation is minimal, e.g. in the case b_len=10:
80 // 1 -> 2 -> 3 -> 5 -> 10 and not 1 -> 2 -> 4 -> 8 -> 10.
81 void cl_UDS_recip (const uintD* a_MSDptr, uintC a_len,
82 uintD* b_MSDptr, uintC b_len)
84 var uintC y_len = b_len+1;
85 var uintC x_len = (a_len <= b_len ? a_len+1 : y_len);
91 num_stack_alloc(x_len,x_MSDptr=,);
92 num_stack_alloc(y_len,y_MSDptr=,);
93 num_stack_alloc(2*y_len,y2_MSDptr=,);
94 num_stack_alloc(x_len+2*y_len,y3_MSDptr=,);
95 // Prepare x/2 at x_MSDptr by shifting a right by 1 bit.
97 { mspref(x_MSDptr,a_len) =
98 shiftrightcopy_loop_msp(a_MSDptr,x_MSDptr,a_len,1,0);
101 { mspref(x_MSDptr,b_len) =
102 shiftrightcopy_loop_msp(a_MSDptr,x_MSDptr,b_len,1,0)
103 | ((mspref(a_MSDptr,b_len) & -bit(intDsize-3)) >> 1);
106 { var uintD x1 = mspref(a_MSDptr,0);
107 var uintD x2 = (a_len > 1 ? (mspref(a_MSDptr,1) & -bit(intDsize-3)) : 0);
108 if ((x1 == (uintD)bit(intDsize-1)) && (x2 == 0))
109 { mspref(y_MSDptr,0) = 4; mspref(y_MSDptr,1) = 0; }
116 divuD((uintDD)(-highlowDD(x1,x2)),x1, q=,r=);
117 var uintDD c = muluD(q,x2);
118 chi = highD(c); clo = lowD(c);
120 divuD((uintD)(-x1 - (x2>0 ? 1 : 0)),(uintD)(-x2),x1, q=,r=);
121 muluD(q,x2,chi=,clo=);
125 // qd := ceiling(max(chi-r,0)/(x1+1))
132 mspref(y_MSDptr,0) = 2 + (q>>(intDsize-1));
133 mspref(y_MSDptr,1) = q<<1;
138 integerlengthC(b_len-1,k=);
139 // 2^(k-1) < b_len <= 2^k, so we need k steps.
142 { // n = ceiling(b_len/2^k) limbs of y have already been computed.
143 var uintC m = ((b_len-1)>>(k-1))+1; // = ceiling(b_len/2^(k-1))
144 // Compute zm := 2*zn - round_down(xm/2*zn*zn).
145 cl_UDS_mul_square(y_MSDptr mspop (n+1),n+1,y2_MSDptr mspop 2*(n+1));
146 var uintC xm_len = (m < x_len ? m+1 : x_len);
147 cl_UDS_mul(x_MSDptr mspop xm_len,xm_len,
148 y2_MSDptr mspop 2*(n+1),2*n+1,
149 y3_MSDptr mspop (xm_len+2*n+1));
150 // Round down by just taking the first m+1 limbs at y3_MSDptr.
151 shift1left_loop_lsp(y_MSDptr mspop (n+1),n+1);
152 clear_loop_msp(y_MSDptr mspop (n+1),m-n);
153 subfrom_loop_lsp(y3_MSDptr mspop (m+1),y_MSDptr mspop (m+1),m+1);
156 // All n = b_len limbs of y have been computed. Divide by 2.
157 mspref(b_MSDptr,b_len+1) =
158 shiftrightcopy_loop_msp(y_MSDptr,b_MSDptr,b_len+1,1,0);
160 // Bit complexity (N := b_len): O(M(N)).