1 /* mpn_div_qr_1 -- mpn by limb division.
2 
3    Contributed to the GNU project by Niels Möller and Torbjörn Granlund
4 
5 Copyright 1991, 1993, 1994, 1996, 1998-2000, 2002, 2003, 2013 Free Software
6 Foundation, Inc.
7 
8 This file is part of the GNU MP Library.
9 
10 The GNU MP Library is free software; you can redistribute it and/or modify
11 it under the terms of either:
12 
13   * the GNU Lesser General Public License as published by the Free
14     Software Foundation; either version 3 of the License, or (at your
15     option) any later version.
16 
17 or
18 
19   * the GNU General Public License as published by the Free Software
20     Foundation; either version 2 of the License, or (at your option) any
21     later version.
22 
23 or both in parallel, as here.
24 
25 The GNU MP Library is distributed in the hope that it will be useful, but
26 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
28 for more details.
29 
30 You should have received copies of the GNU General Public License and the
31 GNU Lesser General Public License along with the GNU MP Library.  If not,
32 see https://www.gnu.org/licenses/.  */
33 
34 #include "gmp-impl.h"
35 #include "longlong.h"
36 
37 #ifndef DIV_QR_1_NORM_THRESHOLD
38 #define DIV_QR_1_NORM_THRESHOLD 3
39 #endif
40 #ifndef DIV_QR_1_UNNORM_THRESHOLD
41 #define DIV_QR_1_UNNORM_THRESHOLD 3
42 #endif
43 
44 #if GMP_NAIL_BITS > 0
45 #error Nail bits not supported
46 #endif
47 
48 /* Divides {up, n} by d. Writes the n-1 low quotient limbs at {qp,
49  * n-1}, and the high quotient limb at *qh. Returns remainder. */
50 mp_limb_t
mpn_div_qr_1(mp_ptr qp,mp_limb_t * qh,mp_srcptr up,mp_size_t n,mp_limb_t d)51 mpn_div_qr_1 (mp_ptr qp, mp_limb_t *qh, mp_srcptr up, mp_size_t n,
52 	      mp_limb_t d)
53 {
54   unsigned cnt;
55   mp_limb_t uh;
56 
57   ASSERT (n > 0);
58   ASSERT (d > 0);
59 
60   if (d & GMP_NUMB_HIGHBIT)
61     {
62       /* Normalized case */
63       mp_limb_t dinv, q;
64 
65       uh = up[--n];
66 
67       q = (uh >= d);
68       *qh = q;
69       uh -= (-q) & d;
70 
71       if (BELOW_THRESHOLD (n, DIV_QR_1_NORM_THRESHOLD))
72 	{
73 	  cnt = 0;
74 	plain:
75 	  while (n > 0)
76 	    {
77 	      mp_limb_t ul = up[--n];
78 	      udiv_qrnnd (qp[n], uh, uh, ul, d);
79 	    }
80 	  return uh >> cnt;
81 	}
82       invert_limb (dinv, d);
83       return mpn_div_qr_1n_pi1 (qp, up, n, uh, d, dinv);
84     }
85   else
86     {
87       /* Unnormalized case */
88       mp_limb_t dinv, ul;
89 
90       if (! UDIV_NEEDS_NORMALIZATION
91 	  && BELOW_THRESHOLD (n, DIV_QR_1_UNNORM_THRESHOLD))
92 	{
93 	  uh = up[--n];
94 	  udiv_qrnnd (*qh, uh, CNST_LIMB(0), uh, d);
95 	  cnt = 0;
96 	  goto plain;
97 	}
98 
99       count_leading_zeros (cnt, d);
100       d <<= cnt;
101 
102 #if HAVE_NATIVE_mpn_div_qr_1u_pi1
103       /* FIXME: Call loop doing on-the-fly normalization */
104 #endif
105 
106       /* Shift up front, use qp area for shifted copy. A bit messy,
107 	 since we have only n-1 limbs available, and shift the high
108 	 limb manually. */
109       uh = up[--n];
110       ul = (uh << cnt) | mpn_lshift (qp, up, n, cnt);
111       uh >>= (GMP_LIMB_BITS - cnt);
112 
113       if (UDIV_NEEDS_NORMALIZATION
114 	  && BELOW_THRESHOLD (n, DIV_QR_1_UNNORM_THRESHOLD))
115 	{
116 	  udiv_qrnnd (*qh, uh, uh, ul, d);
117 	  up = qp;
118 	  goto plain;
119 	}
120       invert_limb (dinv, d);
121 
122       udiv_qrnnd_preinv (*qh, uh, uh, ul, d, dinv);
123       return mpn_div_qr_1n_pi1 (qp, qp, n, uh, d, dinv) >> cnt;
124     }
125 }
126