xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/generic/div_qr_2u_pi1.c (revision 6d322f2f4598f0d8a138f10ea648ec4fabe41f8b)
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
6    ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS
7    ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP
8    RELEASE.
9 
10 
11 Copyright 2011 Free Software 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 /* 3/2 loop, for unnormalized divisor. Caller must pass shifted d1 and
34    d0, while {np,nn} is shifted on the fly. */
35 mp_limb_t
36 mpn_div_qr_2u_pi1 (mp_ptr qp, mp_ptr rp, mp_srcptr np, mp_size_t nn,
37 		   mp_limb_t d1, mp_limb_t d0, int shift, mp_limb_t di)
38 {
39   mp_limb_t qh;
40   mp_limb_t r2, r1, r0;
41   mp_size_t i;
42 
43   ASSERT (nn >= 2);
44   ASSERT (d1 & GMP_NUMB_HIGHBIT);
45   ASSERT (shift > 0);
46 
47   r2 = np[nn-1] >> (GMP_LIMB_BITS - shift);
48   r1 = (np[nn-1] << shift) | (np[nn-2] >> (GMP_LIMB_BITS - shift));
49   r0 = np[nn-2] << shift;
50 
51   udiv_qr_3by2 (qh, r2, r1, r2, r1, r0, d1, d0, di);
52 
53   for (i = nn - 2 - 1; i >= 0; i--)
54     {
55       mp_limb_t q;
56       r0 = np[i];
57       r1 |= r0 >> (GMP_LIMB_BITS - shift);
58       r0 <<= shift;
59       udiv_qr_3by2 (q, r2, r1, r2, r1, r0, d1, d0, di);
60       qp[i] = q;
61     }
62 
63   rp[0] = (r1 >> shift) | (r2 << (GMP_LIMB_BITS - shift));
64   rp[1] = r2 >> shift;
65 
66   return qh;
67 }
68