source: trunk/third/gmp/mpz/cdiv_r_ui.c @ 18191

Revision 18191, 2.4 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* mpz_cdiv_r_ui -- Division rounding the quotient towards +infinity.  The
2   remainder gets the opposite sign as the denominator.  In order to make it
3   always fit into the return type, the negative of the true remainder is
4   returned.
5
6Copyright 1994, 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
7
8This file is part of the GNU MP Library.
9
10The GNU MP Library is free software; you can redistribute it and/or modify
11it under the terms of the GNU Lesser General Public License as published by
12the Free Software Foundation; either version 2.1 of the License, or (at your
13option) any later version.
14
15The GNU MP Library is distributed in the hope that it will be useful, but
16WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18License for more details.
19
20You should have received a copy of the GNU Lesser General Public License
21along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
22the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23MA 02111-1307, USA. */
24
25#include "gmp.h"
26#include "gmp-impl.h"
27
28unsigned long int
29mpz_cdiv_r_ui (mpz_ptr rem, mpz_srcptr dividend, unsigned long int divisor)
30{
31  mp_size_t ns, nn;
32  mp_ptr np;
33  mp_limb_t rl;
34
35  if (divisor == 0)
36    DIVIDE_BY_ZERO;
37
38  ns = SIZ(dividend);
39  if (ns == 0)
40    {
41      SIZ(rem) = 0;
42      return 0;
43    }
44
45  nn = ABS(ns);
46  np = PTR(dividend);
47#if GMP_NAIL_BITS != 0
48  if (divisor > GMP_NUMB_MAX)
49    {
50      mp_limb_t dp[2];
51      mp_ptr rp, qp;
52      mp_size_t rn;
53      TMP_DECL (mark);
54
55      MPZ_REALLOC (rem, 2);
56      rp = PTR(rem);
57
58      if (nn == 1)              /* tdiv_qr requirements; tested above for 0 */
59        {
60          rl = np[0];
61          rp[0] = rl;
62        }
63      else
64        {
65          TMP_MARK (mark);
66          dp[0] = divisor & GMP_NUMB_MASK;
67          dp[1] = divisor >> GMP_NUMB_BITS;
68          qp = TMP_ALLOC_LIMBS (nn - 2 + 1);
69          mpn_tdiv_qr (qp, rp, (mp_size_t) 0, np, nn, dp, (mp_size_t) 2);
70          TMP_FREE (mark);
71          rl = rp[0] + (rp[1] << GMP_NUMB_BITS);
72        }
73
74      if (rl != 0 && ns >= 0)
75        {
76          rl = divisor - rl;
77          rp[0] = rl & GMP_NUMB_MASK;
78          rp[1] = rl >> GMP_NUMB_BITS;
79        }
80
81      rn = 1 + (rl > GMP_NUMB_MAX);  rn -= (rp[rn - 1] == 0);
82      SIZ(rem) = -rn;
83    }
84  else
85#endif
86    {
87      rl = mpn_mod_1 (np, nn, (mp_limb_t) divisor);
88      if (rl == 0)
89        SIZ(rem) = 0;
90      else
91        {
92          if (ns >= 0)
93            rl = divisor - rl;
94
95          PTR(rem)[0] = rl;
96          SIZ(rem) = -1;
97        }
98    }
99
100  return rl;
101}
Note: See TracBrowser for help on using the repository browser.