beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / ui_sub.c
blob579a30079541e90d95482d3ea6abb4a52afe922b
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 either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
34 void
35 mpz_ui_sub (mpz_ptr w, unsigned long int uval, mpz_srcptr v)
37 mp_ptr vp, wp;
38 mp_size_t vn, wn;
39 mp_limb_t cy;
41 #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */
42 if (uval > GMP_NUMB_MAX)
44 mpz_t u;
45 mp_limb_t ul[2];
46 PTR(u) = ul;
47 ul[0] = uval & GMP_NUMB_MASK;
48 ul[1] = uval >> GMP_NUMB_BITS;
49 SIZ(u) = 2;
50 mpz_sub (w, u, v);
51 return;
53 #endif
55 vp = PTR(v);
56 vn = SIZ(v);
58 wp = PTR(w);
60 if (vn > 1)
62 wp = MPZ_REALLOC (w, vn);
63 vp = PTR(v);
64 mpn_sub_1 (wp, vp, vn, (mp_limb_t) uval);
65 wn = -(vn - (wp[vn - 1] == 0));
67 else if (vn == 1)
69 if (uval >= vp[0])
71 wp[0] = uval - vp[0];
72 wn = wp[0] != 0;
74 else
76 wp[0] = vp[0] - uval;
77 wn = -1;
80 else if (vn == 0)
82 wp[0] = uval;
83 wn = uval != 0;
85 else /* (vn < 0) */
87 vn = -vn;
88 wp = MPZ_REALLOC (w, vn + 1);
89 vp = PTR(v);
90 cy = mpn_add_1 (wp, vp, vn, (mp_limb_t) uval);
91 wp[vn] = cy;
92 wn = vn + (cy != 0);
95 SIZ(w) = wn;