1 /* mpn_sbpi1_divappr_q -- Schoolbook division using the Möller-Granlund 3/2
2 division algorithm, returning approximate quotient. The quotient returned
3 is either correct, or one too large.
5 Contributed to the GNU project by Torbjorn Granlund.
7 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
8 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
9 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
11 Copyright 2007, 2009 Free Software Foundation, Inc.
13 This file is part of the GNU MP Library.
15 The GNU MP Library is free software; you can redistribute it and/or modify
16 it under the terms of either:
18 * the GNU Lesser General Public License as published by the Free
19 Software Foundation; either version 3 of the License, or (at your
20 option) any later version.
24 * the GNU General Public License as published by the Free Software
25 Foundation; either version 2 of the License, or (at your option) any
28 or both in parallel, as here.
30 The GNU MP Library is distributed in the hope that it will be useful, but
31 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
32 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
35 You should have received copies of the GNU General Public License and the
36 GNU Lesser General Public License along with the GNU MP Library. If not,
37 see https://www.gnu.org/licenses/. */
45 mpn_sbpi1_divappr_q (mp_ptr qp
,
46 mp_ptr np
, mp_size_t nn
,
47 mp_srcptr dp
, mp_size_t dn
,
60 ASSERT ((dp
[dn
-1] & GMP_NUMB_HIGHBIT
) != 0);
71 qh
= mpn_cmp (np
- dn
, dp
, dn
) >= 0;
73 mpn_sub_n (np
- dn
, np
- dn
, dp
, dn
);
77 dn
-= 2; /* offset dn by 2 for main division loops,
78 saving two iterations in mpn_submul_1. */
86 for (i
= qn
- (dn
+ 2); i
>= 0; i
--)
89 if (UNLIKELY (n1
== d1
) && np
[1] == d0
)
92 mpn_submul_1 (np
- dn
, dp
, dn
+ 2, q
);
93 n1
= np
[1]; /* update n1, last loop's value will now be invalid */
97 udiv_qr_3by2 (q
, n1
, n0
, n1
, np
[1], np
[0], d1
, d0
, dinv
);
99 cy
= mpn_submul_1 (np
- dn
, dp
, dn
, q
);
102 n0
= (n0
- cy
) & GMP_NUMB_MASK
;
107 if (UNLIKELY (cy
!= 0))
109 n1
+= d1
+ mpn_add_n (np
- dn
, np
- dn
, dp
, dn
+ 1);
117 flag
= ~CNST_LIMB(0);
121 for (i
= dn
; i
> 0; i
--)
124 if (UNLIKELY (n1
>= (d1
& flag
)))
127 cy
= mpn_submul_1 (np
- dn
, dp
, dn
+ 2, q
);
129 if (UNLIKELY (n1
!= cy
))
131 if (n1
< (cy
& flag
))
134 mpn_add_n (np
- dn
, np
- dn
, dp
, dn
+ 2);
143 udiv_qr_3by2 (q
, n1
, n0
, n1
, np
[1], np
[0], d1
, d0
, dinv
);
145 cy
= mpn_submul_1 (np
- dn
, dp
, dn
, q
);
148 n0
= (n0
- cy
) & GMP_NUMB_MASK
;
153 if (UNLIKELY (cy
!= 0))
155 n1
+= d1
+ mpn_add_n (np
- dn
, np
- dn
, dp
, dn
+ 1);
162 /* Truncate operands. */
168 if (UNLIKELY (n1
>= (d1
& flag
)))
171 cy
= mpn_submul_1 (np
, dp
, 2, q
);
173 if (UNLIKELY (n1
!= cy
))
175 if (n1
< (cy
& flag
))
178 add_ssaaaa (np
[1], np
[0], np
[1], np
[0], dp
[1], dp
[0]);
187 udiv_qr_3by2 (q
, n1
, n0
, n1
, np
[1], np
[0], d1
, d0
, dinv
);
196 ASSERT_ALWAYS (np
[1] == n1
);