1 /* mpz_fdiv_qr_ui -- Division rounding the quotient towards -infinity.
2 The remainder gets the same sign as the denominator.
4 Copyright 1994-1996, 1999, 2001, 2002, 2004, 2012 Free Software Foundation,
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free Software
19 Foundation; either version 2 of the License, or (at your option) any
22 or both in parallel, as here.
24 The GNU MP Library is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 You should have received copies of the GNU General Public License and the
30 GNU Lesser General Public License along with the GNU MP Library. If not,
31 see https://www.gnu.org/licenses/. */
37 mpz_fdiv_qr_ui (mpz_ptr quot
, mpz_ptr rem
, mpz_srcptr dividend
, unsigned long int divisor
)
43 if (UNLIKELY (divisor
== 0))
55 qp
= MPZ_REALLOC (quot
, nn
);
58 #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */
59 if (divisor
> GMP_NUMB_MAX
)
68 if (nn
== 1) /* tdiv_qr requirements; tested above for 0 */
71 qn
= 1; /* a white lie, fixed below */
77 dp
[0] = divisor
& GMP_NUMB_MASK
;
78 dp
[1] = divisor
>> GMP_NUMB_BITS
;
79 mpn_tdiv_qr (qp
, rp
, (mp_size_t
) 0, np
, nn
, dp
, (mp_size_t
) 2);
80 rl
= rp
[0] + (rp
[1] << GMP_NUMB_BITS
);
84 if (rl
!= 0 && ns
< 0)
86 mpn_incr_u (qp
, (mp_limb_t
) 1);
88 rp
[0] = rl
& GMP_NUMB_MASK
;
89 rp
[1] = rl
>> GMP_NUMB_BITS
;
92 qn
-= qp
[qn
- 1] == 0; qn
-= qn
!= 0 && qp
[qn
- 1] == 0;
93 rn
= 1 + (rl
> GMP_NUMB_MAX
); rn
-= (rp
[rn
- 1] == 0);
99 rl
= mpn_divrem_1 (qp
, (mp_size_t
) 0, np
, nn
, (mp_limb_t
) divisor
);
106 mpn_incr_u (qp
, (mp_limb_t
) 1);
113 qn
= nn
- (qp
[nn
- 1] == 0);
116 SIZ(quot
) = ns
>= 0 ? qn
: -qn
;