Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / gmp / mpz / lcm_ui.c
1 /* mpz_lcm_ui -- least common multiple of mpz and ulong.
2
3 Copyright 2001, 2002, 2004 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19
20 #include "gmp.h"
21 #include "gmp-impl.h"
22 #include "longlong.h"
23
24
25 void
26 mpz_lcm_ui (mpz_ptr r, mpz_srcptr u, unsigned long v)
27 {
28   mp_size_t      usize;
29   mp_srcptr      up;
30   mp_ptr         rp;
31   unsigned long  g;
32   mp_limb_t      c;
33
34 #if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
35   if (v > GMP_NUMB_MAX)
36     {
37       mpz_t vz;
38       mp_limb_t vlimbs[2];
39       vlimbs[0] = v & GMP_NUMB_MASK;
40       vlimbs[1] = v >> GMP_NUMB_BITS;
41       PTR(vz) = vlimbs;
42       SIZ(vz) = 2;
43       mpz_lcm (r, u, vz);
44       return;
45     }
46 #endif
47
48   /* result zero if either operand zero */
49   usize = SIZ(u);
50   if (usize == 0 || v == 0)
51     {
52       SIZ(r) = 0;
53       return;
54     }
55   usize = ABS(usize);
56
57   MPZ_REALLOC (r, usize+1);
58
59   up = PTR(u);
60   g = (unsigned long) mpn_gcd_1 (up, usize, (mp_limb_t) v);
61   v /= g;
62
63   rp = PTR(r);
64   c = mpn_mul_1 (rp, up, usize, (mp_limb_t) v);
65   rp[usize] = c;
66   usize += (c != 0);
67   SIZ(r) = usize;
68 }