1/* mpn_div_qr_2u_pi1
2
3   Contributed to the GNU project by Niels M��ller
4
5   THIS FILE CONTAINS INTERNAL FUNCTIONS WITH MUTABLE INTERFACES.  IT IS ONLY
6   SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
7   GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
8
9
10Copyright 2011 Free Software Foundation, Inc.
11
12This file is part of the GNU MP Library.
13
14The GNU MP Library is free software; you can redistribute it and/or modify
15it under the terms of either:
16
17  * the GNU Lesser General Public License as published by the Free
18    Software Foundation; either version 3 of the License, or (at your
19    option) any later version.
20
21or
22
23  * the GNU General Public License as published by the Free Software
24    Foundation; either version 2 of the License, or (at your option) any
25    later version.
26
27or both in parallel, as here.
28
29The GNU MP Library is distributed in the hope that it will be useful, but
30WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
31or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32for more details.
33
34You should have received copies of the GNU General Public License and the
35GNU Lesser General Public License along with the GNU MP Library.  If not,
36see https://www.gnu.org/licenses/.  */
37
38#include "gmp-impl.h"
39#include "longlong.h"
40
41
42/* 3/2 loop, for unnormalized divisor. Caller must pass shifted d1 and
43   d0, while {np,nn} is shifted on the fly. */
44mp_limb_t
45mpn_div_qr_2u_pi1 (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
46		   mp_limb_t d1, mp_limb_t d0, int shift, mp_limb_t di)
47{
48  mp_limb_t qh;
49  mp_limb_t r2, r1, r0;
50  mp_size_t i;
51
52  ASSERT (nn >= 2);
53  ASSERT (d1 & GMP_NUMB_HIGHBIT);
54  ASSERT (shift > 0);
55
56  r2 = np[nn-1] >> (GMP_LIMB_BITS - shift);
57  r1 = (np[nn-1] << shift) | (np[nn-2] >> (GMP_LIMB_BITS - shift));
58  r0 = np[nn-2] << shift;
59
60  udiv_qr_3by2 (qh, r2, r1, r2, r1, r0, d1, d0, di);
61
62  for (i = nn - 2 - 1; i >= 0; i--)
63    {
64      mp_limb_t q;
65      r0 = np[i];
66      r1 |= r0 >> (GMP_LIMB_BITS - shift);
67      r0 <<= shift;
68      udiv_qr_3by2 (q, r2, r1, r2, r1, r0, d1, d0, di);
69      qp[i] = q;
70    }
71
72  rp[0] = (r1 >> shift) | (r2 << (GMP_LIMB_BITS - shift));
73  rp[1] = r2 >> shift;
74
75  return qh;
76}
77