]> www.ginac.de Git - cln.git/blob - src/base/digitseq/cl_DS_recip.cc
Initial revision
[cln.git] / src / base / digitseq / cl_DS_recip.cc
1 // cl_UDS_recip().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_DS.h"
8
9
10 // Implementation.
11
12 #include "cl_abort.h"
13
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
22 // are used.
23   extern void cl_UDS_recip (const uintD* a_MSDptr, uintC a_len,
24                             uintD* b_MSDptr, uintC b_len);
25 // Method:
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.
35 // Step n = 1:
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.
57 //     Hence
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).
67 //   Rationale:
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). 
78 // Choice of n:
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)
83     {
84       var uintC y_len = b_len+1;
85       var uintC x_len = (a_len <= b_len ? a_len+1 : y_len);
86       var uintD* x_MSDptr;
87       var uintD* y_MSDptr;
88       var uintD* y2_MSDptr;
89       var uintD* y3_MSDptr;
90       CL_ALLOCA_STACK;
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.
96       if (a_len <= b_len)
97         { mspref(x_MSDptr,a_len) =
98             shiftrightcopy_loop_msp(a_MSDptr,x_MSDptr,a_len,1,0);
99         }
100         else
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);
104         }
105       // Step n = 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; }
110           else
111           { var uintD q;
112             var uintD r;
113             var uintD chi;
114             var uintD clo;
115             #if HAVE_DD
116               divuD((uintDD)(-highlowDD(x1,x2)),x1, q=,r=);
117               var uintDD c = muluD(q,x2);
118               chi = highD(c); clo = lowD(c);
119             #else
120               divuD((uintD)(-x1 - (x2>0 ? 1 : 0)),(uintD)(-x2),x1, q=,r=);
121               muluD(q,x2,chi=,clo=);
122             #endif
123             if (clo > 0)
124               chi++;
125             // qd := ceiling(max(chi-r,0)/(x1+1))
126             if (chi > r)
127               { chi -= r;
128                 if (chi > x1)
129                   { q--; }
130                 q--;
131               }
132             mspref(y_MSDptr,0) = 2 + (q>>(intDsize-1));
133             mspref(y_MSDptr,1) = q<<1;
134           }
135       }
136       // Other steps.
137       var int k;
138       integerlength32((uint32)b_len-1,k=);
139       // 2^(k-1) < b_len <= 2^k, so we need k steps.
140       var uintC n = 1;
141       for (; k>0; k--)
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);
154           n = m;
155         }
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);
159     }
160 // Bit complexity (N := b_len): O(M(N)).