Merge branch 'vendor/GMP' into gcc441
[dragonfly.git] / contrib / gmp / mpn / generic / dc_divrem_n.c
1 /* mpn_dc_divrem_n and auxilliary routines.
2
3    THE FUNCTIONS IN THIS FILE ARE INTERNAL FUNCTIONS WITH MUTABLE
4    INTERFACES.  IT IS ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.
5    IN FACT, IT IS ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A
6    FUTURE GNU MP RELEASE.
7
8
9 Copyright 2000, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
10 Contributed by Paul Zimmermann.
11
12 This file is part of the GNU MP Library.
13
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of the GNU Lesser General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or (at your
17 option) any later version.
18
19 The GNU MP Library is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
22 License for more details.
23
24 You should have received a copy of the GNU Lesser General Public License
25 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
26
27 #include "gmp.h"
28 #include "gmp-impl.h"
29
30 /*
31 [1] Fast Recursive Division, by Christoph Burnikel and Joachim Ziegler,
32     Technical report MPI-I-98-1-022, october 1998.
33     http://www.mpi-sb.mpg.de/~ziegler/TechRep.ps.gz
34 */
35
36 static mp_limb_t mpn_dc_div_3_by_2
37   __GMP_PROTO ((mp_ptr qp, mp_ptr np, mp_srcptr dp, mp_size_t n, mp_ptr scratch));
38 static mp_limb_t mpn_dc_div_2_by_1
39   __GMP_PROTO ((mp_ptr qp, mp_ptr np, mp_srcptr dp, mp_size_t n, mp_ptr scratch));
40
41 /* mpn_dc_divrem_n - Implements algorithm of page 8 in [1]: divides (np,2n)
42    by (dp,n) and puts the quotient in (qp,n), the remainder in (np,n).
43    Returns most significant limb of the quotient, which is 0 or 1.
44    Requires that the most significant bit of the divisor is set.  */
45
46 mp_limb_t
47 mpn_dc_divrem_n (mp_ptr qp, mp_ptr np, mp_srcptr dp, mp_size_t n)
48 {
49   mp_limb_t ret;
50   mp_ptr scratch;
51   TMP_DECL;
52   TMP_MARK;
53
54   scratch = TMP_ALLOC_LIMBS (n);
55   ret = mpn_dc_div_2_by_1 (qp, np, dp, n, scratch);
56
57   TMP_FREE;
58   return ret;
59 }
60
61 static mp_limb_t
62 mpn_dc_div_2_by_1 (mp_ptr qp, mp_ptr np, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
63 {
64   mp_limb_t qhl, cc;
65   mp_size_t n2 = n/2;
66
67   if (n % 2 != 0)
68     {
69       mp_ptr qp1 = qp + 1;
70       qhl = mpn_dc_div_3_by_2 (qp1 + n2, np + 2 + n2, dp + 1, n2, scratch);
71       qhl += mpn_add_1 (qp1 + n2, qp1 + n2, n2,
72                         mpn_dc_div_3_by_2 (qp1, np + 2, dp + 1, n2, scratch));
73
74       cc = mpn_submul_1 (np + 1, qp1, n - 1, dp[0]);
75       cc = mpn_sub_1 (np + n, np + n, 1, cc);
76       if (qhl != 0)
77         cc += mpn_sub_1 (np + n, np + n, 1, dp[0]);
78       while (cc != 0)
79         {
80           qhl -= mpn_sub_1 (qp1, qp1, n - 1, (mp_limb_t) 1);
81           cc -= mpn_add_n (np + 1, np + 1, dp, n);
82         }
83       qhl += mpn_add_1 (qp1, qp1, n - 1,
84                         mpn_sb_divrem_mn (qp, np, n + 1, dp, n));
85     }
86   else
87     {
88       qhl = mpn_dc_div_3_by_2 (qp + n2, np + n2, dp, n2, scratch);
89       qhl += mpn_add_1 (qp + n2, qp + n2, n2,
90                         mpn_dc_div_3_by_2 (qp, np, dp, n2, scratch));
91     }
92   return qhl;
93 }
94
95
96 /* divides (np, 3n) by (dp, 2n) and puts the quotient in (qp, n),
97    the remainder in (np, 2n) */
98
99 static mp_limb_t
100 mpn_dc_div_3_by_2 (mp_ptr qp, mp_ptr np, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
101 {
102   mp_size_t twon = n + n;
103   mp_limb_t qhl, cc;
104
105   if (n < DIV_DC_THRESHOLD)
106     qhl = mpn_sb_divrem_mn (qp, np + n, twon, dp + n, n);
107   else
108     qhl = mpn_dc_div_2_by_1 (qp, np + n, dp + n, n, scratch);
109
110   mpn_mul_n (scratch, qp, dp, n);
111   cc = mpn_sub_n (np, np, scratch, twon);
112
113   if (qhl != 0)
114     cc += mpn_sub_n (np + n, np + n, dp, n);
115   while (cc != 0)
116     {
117       qhl -= mpn_sub_1 (qp, qp, n, (mp_limb_t) 1);
118       cc -= mpn_add_n (np, np, dp, twon);
119     }
120   return qhl;
121 }