51a1e78710f1f9d405c708516daa438d645e5bac
[platform/upstream/gmp.git] / mpn / generic / divrem_2.c
1 /* mpn_divrem_2 -- Divide natural numbers, producing both remainder and
2    quotient.  The divisor is two limbs.
3
4    THIS FILE CONTAINS INTERNAL FUNCTIONS WITH MUTABLE INTERFACES.  IT IS
5    ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS
6    ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP
7    RELEASE.
8
9
10 Copyright 1993, 1994, 1995, 1996, 1999, 2000, 2001, 2002 Free Software
11 Foundation, Inc.
12
13 This file is part of the GNU MP Library.
14
15 The GNU MP Library is free software; you can redistribute it and/or modify
16 it under the terms of the GNU Lesser General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or (at your
18 option) any later version.
19
20 The GNU MP Library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
23 License for more details.
24
25 You should have received a copy of the GNU Lesser General Public License
26 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
27
28 #include "gmp.h"
29 #include "gmp-impl.h"
30 #include "longlong.h"
31
32
33 /* Divide num {np,nn} by den {dp,2} and write the nn-2 least significant
34    quotient limbs at qp and the 2 long remainder at np.  If qxn is non-zero,
35    generate that many fraction bits and append them after the other quotient
36    limbs.  Return the most significant limb of the quotient, this is always 0
37    or 1.
38
39    Preconditions:
40    1. The most significant bit of the divisor must be set.
41    2. qp must either not overlap with the input operands at all, or
42       qp >= np + 2 must hold true.  (This means that it's possible to put
43       the quotient in the high part of {np,nn}, right above the remainder.
44    3. nn >= 2, even if qxn is non-zero.  */
45
46 mp_limb_t
47 mpn_divrem_2 (mp_ptr qp, mp_size_t qxn,
48               mp_ptr np, mp_size_t nn,
49               mp_srcptr dp)
50 {
51   mp_limb_t most_significant_q_limb;
52   mp_size_t i;
53   mp_limb_t r1, r0, d1, d0;
54   gmp_pi1_t di;
55
56   ASSERT (nn >= 2);
57   ASSERT (qxn >= 0);
58   ASSERT (dp[1] & GMP_NUMB_HIGHBIT);
59   ASSERT (! MPN_OVERLAP_P (qp, nn-2+qxn, np, nn) || qp >= np+2);
60   ASSERT_MPN (np, nn);
61   ASSERT_MPN (dp, 2);
62
63   np += nn - 2;
64   d1 = dp[1];
65   d0 = dp[0];
66   r1 = np[1];
67   r0 = np[0];
68
69   most_significant_q_limb = 0;
70   if (r1 >= d1 && (r1 > d1 || r0 >= d0))
71     {
72 #if GMP_NAIL_BITS == 0
73       sub_ddmmss (r1, r0, r1, r0, d1, d0);
74 #else
75       r0 = r0 - d0;
76       r1 = r1 - d1 - (r0 >> GMP_LIMB_BITS - 1);
77       r0 &= GMP_NUMB_MASK;
78 #endif
79       most_significant_q_limb = 1;
80     }
81
82   invert_pi1 (di, d1, d0);
83
84   qp += qxn;
85
86   for (i = nn - 2 - 1; i >= 0; i--)
87     {
88       mp_limb_t n0, q;
89       n0 = np[-1];
90       udiv_qr_3by2 (q, r1, r0, r1, r0, n0, d1, d0, di.inv32);
91       np--;
92       qp[i] = q;
93     }
94
95   if (UNLIKELY (qxn != 0))
96     {
97       qp -= qxn;
98       for (i = qxn - 1; i >= 0; i--)
99         {
100           mp_limb_t q;
101           udiv_qr_3by2 (q, r1, r0, r1, r0, 0, d1, d0, di.inv32);
102           qp[i] = q;
103         }
104     }
105
106   np[1] = r1;
107   np[0] = r0;
108
109   return most_significant_q_limb;
110 }