xref: /dragonfly/contrib/gmp/mpz/gcd_ui.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /* mpz_gcd_ui -- Calculate the greatest common divisor of two integers.
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino Copyright 1994, 1996, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
4*86d7f5d3SJohn Marino Foundation, Inc.
5*86d7f5d3SJohn Marino 
6*86d7f5d3SJohn Marino This file is part of the GNU MP Library.
7*86d7f5d3SJohn Marino 
8*86d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
9*86d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
10*86d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
11*86d7f5d3SJohn Marino option) any later version.
12*86d7f5d3SJohn Marino 
13*86d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
14*86d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15*86d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16*86d7f5d3SJohn Marino License for more details.
17*86d7f5d3SJohn Marino 
18*86d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
19*86d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
20*86d7f5d3SJohn Marino 
21*86d7f5d3SJohn Marino #include <stdio.h> /* for NULL */
22*86d7f5d3SJohn Marino #include "gmp.h"
23*86d7f5d3SJohn Marino #include "gmp-impl.h"
24*86d7f5d3SJohn Marino 
25*86d7f5d3SJohn Marino unsigned long int
mpz_gcd_ui(mpz_ptr w,mpz_srcptr u,unsigned long int v)26*86d7f5d3SJohn Marino mpz_gcd_ui (mpz_ptr w, mpz_srcptr u, unsigned long int v)
27*86d7f5d3SJohn Marino {
28*86d7f5d3SJohn Marino   mp_size_t un;
29*86d7f5d3SJohn Marino   mp_limb_t res;
30*86d7f5d3SJohn Marino 
31*86d7f5d3SJohn Marino #if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
32*86d7f5d3SJohn Marino   if (v > GMP_NUMB_MAX)
33*86d7f5d3SJohn Marino     {
34*86d7f5d3SJohn Marino       mpz_t vz;
35*86d7f5d3SJohn Marino       mp_limb_t vlimbs[2];
36*86d7f5d3SJohn Marino       vlimbs[0] = v & GMP_NUMB_MASK;
37*86d7f5d3SJohn Marino       vlimbs[1] = v >> GMP_NUMB_BITS;
38*86d7f5d3SJohn Marino       PTR(vz) = vlimbs;
39*86d7f5d3SJohn Marino       SIZ(vz) = 2;
40*86d7f5d3SJohn Marino       mpz_gcd (w, u, vz);
41*86d7f5d3SJohn Marino       /* because v!=0 we will have w<=v hence fitting a ulong */
42*86d7f5d3SJohn Marino       ASSERT (mpz_fits_ulong_p (w));
43*86d7f5d3SJohn Marino       return mpz_get_ui (w);
44*86d7f5d3SJohn Marino     }
45*86d7f5d3SJohn Marino #endif
46*86d7f5d3SJohn Marino 
47*86d7f5d3SJohn Marino   un = ABSIZ(u);
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino   if (un == 0)
50*86d7f5d3SJohn Marino     res = v;
51*86d7f5d3SJohn Marino   else if (v == 0)
52*86d7f5d3SJohn Marino     {
53*86d7f5d3SJohn Marino       if (w != NULL)
54*86d7f5d3SJohn Marino 	{
55*86d7f5d3SJohn Marino 	  if (u != w)
56*86d7f5d3SJohn Marino 	    {
57*86d7f5d3SJohn Marino 	      MPZ_REALLOC (w, un);
58*86d7f5d3SJohn Marino 	      MPN_COPY (PTR(w), PTR(u), un);
59*86d7f5d3SJohn Marino 	    }
60*86d7f5d3SJohn Marino 	  SIZ(w) = un;
61*86d7f5d3SJohn Marino 	}
62*86d7f5d3SJohn Marino       /* Return u if it fits a ulong, otherwise 0. */
63*86d7f5d3SJohn Marino       res = PTR(u)[0];
64*86d7f5d3SJohn Marino       return (un == 1 && res <= ULONG_MAX ? res : 0);
65*86d7f5d3SJohn Marino     }
66*86d7f5d3SJohn Marino   else
67*86d7f5d3SJohn Marino     res = mpn_gcd_1 (PTR(u), un, (mp_limb_t) v);
68*86d7f5d3SJohn Marino 
69*86d7f5d3SJohn Marino   if (w != NULL)
70*86d7f5d3SJohn Marino     {
71*86d7f5d3SJohn Marino       PTR(w)[0] = res;
72*86d7f5d3SJohn Marino       SIZ(w) = res != 0;
73*86d7f5d3SJohn Marino     }
74*86d7f5d3SJohn Marino   return res;
75*86d7f5d3SJohn Marino }
76