xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/fdiv_qr_ui.c (revision 80d9064ac03cbb6a4174695f0d5b237c8766d3d0)
1 /* mpz_fdiv_qr_ui -- Division rounding the quotient towards -infinity.
2    The remainder gets the same sign as the denominator.
3 
4 Copyright 1994, 1995, 1996, 1999, 2001, 2002, 2004, 2012 Free Software Foundation,
5 Inc.
6 
7 This file is part of the GNU MP Library.
8 
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 The GNU MP Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17 License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
21 
22 #include "gmp.h"
23 #include "gmp-impl.h"
24 
25 unsigned long int
26 mpz_fdiv_qr_ui (mpz_ptr quot, mpz_ptr rem, mpz_srcptr dividend, unsigned long int divisor)
27 {
28   mp_size_t ns, nn, qn;
29   mp_ptr np, qp;
30   mp_limb_t rl;
31 
32   if (UNLIKELY (divisor == 0))
33     DIVIDE_BY_ZERO;
34 
35   ns = SIZ(dividend);
36   if (ns == 0)
37     {
38       SIZ(quot) = 0;
39       SIZ(rem) = 0;
40       return 0;
41     }
42 
43   nn = ABS(ns);
44   qp = MPZ_REALLOC (quot, nn);
45   np = PTR(dividend);
46 
47 #if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
48   if (divisor > GMP_NUMB_MAX)
49     {
50       mp_limb_t dp[2];
51       mp_ptr rp;
52       mp_size_t rn;
53 
54       MPZ_REALLOC (rem, 2);
55       rp = PTR(rem);
56 
57       if (nn == 1)		/* tdiv_qr requirements; tested above for 0 */
58 	{
59 	  qp[0] = 0;
60 	  qn = 1;		/* a white lie, fixed below */
61 	  rl = np[0];
62 	  rp[0] = rl;
63 	}
64       else
65 	{
66 	  dp[0] = divisor & GMP_NUMB_MASK;
67 	  dp[1] = divisor >> GMP_NUMB_BITS;
68 	  mpn_tdiv_qr (qp, rp, (mp_size_t) 0, np, nn, dp, (mp_size_t) 2);
69 	  rl = rp[0] + (rp[1] << GMP_NUMB_BITS);
70 	  qn = nn - 2 + 1;
71 	}
72 
73       if (rl != 0 && ns < 0)
74 	{
75 	  mpn_incr_u (qp, (mp_limb_t) 1);
76 	  rl = divisor - rl;
77 	  rp[0] = rl & GMP_NUMB_MASK;
78 	  rp[1] = rl >> GMP_NUMB_BITS;
79 	}
80 
81       qn -= qp[qn - 1] == 0; qn -= qn != 0 && qp[qn - 1] == 0;
82       rn = 1 + (rl > GMP_NUMB_MAX);  rn -= (rp[rn - 1] == 0);
83       SIZ(rem) = rn;
84     }
85   else
86 #endif
87     {
88       rl = mpn_divrem_1 (qp, (mp_size_t) 0, np, nn, (mp_limb_t) divisor);
89       if (rl == 0)
90 	SIZ(rem) = 0;
91       else
92 	{
93 	  if (ns < 0)
94 	    {
95 	      mpn_incr_u (qp, (mp_limb_t) 1);
96 	      rl = divisor - rl;
97 	    }
98 
99 	  PTR(rem)[0] = rl;
100 	  SIZ(rem) = rl != 0;
101 	}
102       qn = nn - (qp[nn - 1] == 0);
103     }
104 
105   SIZ(quot) = ns >= 0 ? qn : -qn;
106   return rl;
107 }
108