xref: /netbsd/external/lgpl3/gmp/dist/mpn/generic/divrem.c (revision 671ea119)
1 /* mpn_divrem -- Divide natural numbers, producing both remainder and
2    quotient.  This is now just a middle layer calling mpn_tdiv_qr.
3 
4 Copyright 1993-1997, 1999-2002, 2005, 2016 Free Software Foundation, Inc.
5 
6 This file is part of the GNU MP Library.
7 
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
10 
11   * the GNU Lesser General Public License as published by the Free
12     Software Foundation; either version 3 of the License, or (at your
13     option) any later version.
14 
15 or
16 
17   * the GNU General Public License as published by the Free Software
18     Foundation; either version 2 of the License, or (at your option) any
19     later version.
20 
21 or both in parallel, as here.
22 
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26 for more details.
27 
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library.  If not,
30 see https://www.gnu.org/licenses/.  */
31 
32 #include "gmp-impl.h"
33 #include "longlong.h"
34 
35 mp_limb_t
mpn_divrem(mp_ptr qp,mp_size_t qxn,mp_ptr np,mp_size_t nn,mp_srcptr dp,mp_size_t dn)36 mpn_divrem (mp_ptr qp, mp_size_t qxn,
37 	    mp_ptr np, mp_size_t nn,
38 	    mp_srcptr dp, mp_size_t dn)
39 {
40   ASSERT (qxn >= 0);
41   ASSERT (nn >= dn);
42   ASSERT (dn >= 1);
43   ASSERT (dp[dn-1] & GMP_NUMB_HIGHBIT);
44   ASSERT (! MPN_OVERLAP_P (np, nn, dp, dn));
45   ASSERT (! MPN_OVERLAP_P (qp, nn-dn+qxn, np, nn) || qp==np+dn+qxn);
46   ASSERT (! MPN_OVERLAP_P (qp, nn-dn+qxn, dp, dn));
47   ASSERT_MPN (np, nn);
48   ASSERT_MPN (dp, dn);
49 
50   if (dn == 1)
51     {
52       mp_limb_t ret;
53       mp_ptr q2p;
54       mp_size_t qn;
55       TMP_DECL;
56 
57       TMP_MARK;
58       q2p = TMP_ALLOC_LIMBS (nn + qxn);
59 
60       np[0] = mpn_divrem_1 (q2p, qxn, np, nn, dp[0]);
61       qn = nn + qxn - 1;
62       MPN_COPY (qp, q2p, qn);
63       ret = q2p[qn];
64 
65       TMP_FREE;
66       return ret;
67     }
68   else if (dn == 2)
69     {
70       return mpn_divrem_2 (qp, qxn, np, nn, dp);
71     }
72   else
73     {
74       mp_ptr q2p;
75       mp_limb_t qhl;
76       mp_size_t qn;
77       TMP_DECL;
78 
79       TMP_MARK;
80       if (UNLIKELY (qxn != 0))
81 	{
82 	  mp_ptr n2p;
83 	  TMP_ALLOC_LIMBS_2 (n2p, nn + qxn,
84 			     q2p, nn - dn + qxn + 1);
85 	  MPN_ZERO (n2p, qxn);
86 	  MPN_COPY (n2p + qxn, np, nn);
87 	  mpn_tdiv_qr (q2p, np, 0L, n2p, nn + qxn, dp, dn);
88 	  qn = nn - dn + qxn;
89 	  MPN_COPY (qp, q2p, qn);
90 	  qhl = q2p[qn];
91 	}
92       else
93 	{
94 	  q2p = TMP_ALLOC_LIMBS (nn - dn + 1);
95 	  mpn_tdiv_qr (q2p, np, 0L, np, nn, dp, dn);
96 	  qn = nn - dn;
97 	  MPN_COPY (qp, q2p, qn);
98 	  qhl = q2p[qn];
99 	}
100       TMP_FREE;
101       return qhl;
102     }
103 }
104