IMPORT openssh-9.8p1
[dragonfly.git] / contrib / gmp / mpz / ui_sub.c
blob76d4b823087b5f03042a9d242d9624d82d43914d
1 /* mpz_ui_sub -- Subtract an unsigned one-word integer and an mpz_t.
3 Copyright 2002, 2004 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
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.
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.
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/. */
20 #include "gmp.h"
21 #include "gmp-impl.h"
23 void
24 mpz_ui_sub (mpz_ptr w, unsigned long int uval, mpz_srcptr v)
26 mp_ptr vp, wp;
27 mp_size_t vn, wn;
28 mp_limb_t cy;
30 #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */
31 if (uval > GMP_NUMB_MAX)
33 mpz_t u;
34 mp_limb_t ul[2];
35 PTR(u) = ul;
36 ul[0] = uval & GMP_NUMB_MASK;
37 ul[1] = uval >> GMP_NUMB_BITS;
38 SIZ(u) = 2;
39 mpz_sub (w, u, v);
40 return;
42 #endif
44 vp = PTR(v);
45 vn = SIZ(v);
47 wp = PTR(w);
49 if (vn > 1)
51 wp = MPZ_REALLOC (w, vn);
52 vp = PTR(v);
53 mpn_sub_1 (wp, vp, vn, (mp_limb_t) uval);
54 wn = -(vn - (wp[vn - 1] == 0));
56 else if (vn == 1)
58 if (uval >= vp[0])
60 wp[0] = uval - vp[0];
61 wn = wp[0] != 0;
63 else
65 wp[0] = vp[0] - uval;
66 wn = -1;
69 else if (vn == 0)
71 wp[0] = uval;
72 wn = uval != 0;
74 else /* (vn < 0) */
76 vn = -vn;
77 wp = MPZ_REALLOC (w, vn + 1);
78 vp = PTR(v);
79 cy = mpn_add_1 (wp, vp, vn, (mp_limb_t) uval);
80 wp[vn] = cy;
81 wn = vn + (cy != 0);
84 SIZ(w) = wn;