1 | /* mpz_ui_sub -- Subtract an unsigned one-word integer and an mpz_t. |
---|
2 | |
---|
3 | Copyright 2002 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 2.1 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; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include "gmp.h" |
---|
23 | #include "gmp-impl.h" |
---|
24 | |
---|
25 | void |
---|
26 | mpz_ui_sub (mpz_ptr w, unsigned long int uval, mpz_srcptr v) |
---|
27 | { |
---|
28 | mp_ptr vp, wp; |
---|
29 | mp_size_t vn, wn; |
---|
30 | mp_limb_t cy; |
---|
31 | |
---|
32 | #if GMP_NAIL_BITS != 0 |
---|
33 | if (uval > GMP_NUMB_MAX) |
---|
34 | { |
---|
35 | mpz_t u; |
---|
36 | mp_limb_t ul[2]; |
---|
37 | PTR(u) = ul; |
---|
38 | ul[0] = uval & GMP_NUMB_MASK; |
---|
39 | ul[1] = uval >> GMP_NUMB_BITS; |
---|
40 | SIZ(u) = 2; |
---|
41 | mpz_sub (w, u, v); |
---|
42 | return; |
---|
43 | } |
---|
44 | #endif |
---|
45 | |
---|
46 | vp = PTR(v); |
---|
47 | vn = SIZ(v); |
---|
48 | |
---|
49 | wp = PTR(w); |
---|
50 | |
---|
51 | if (vn > 1) |
---|
52 | { |
---|
53 | wp = MPZ_REALLOC (w, vn); |
---|
54 | vp = PTR(v); |
---|
55 | mpn_sub_1 (wp, vp, vn, (mp_limb_t) uval); |
---|
56 | wn = -(vn - (wp[vn - 1] == 0)); |
---|
57 | } |
---|
58 | else if (vn == 1) |
---|
59 | { |
---|
60 | if (uval >= vp[0]) |
---|
61 | { |
---|
62 | wp[0] = uval - vp[0]; |
---|
63 | wn = wp[0] != 0; |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | wp[0] = vp[0] - uval; |
---|
68 | wn = -1; |
---|
69 | } |
---|
70 | } |
---|
71 | else if (vn == 0) |
---|
72 | { |
---|
73 | wp[0] = uval; |
---|
74 | wn = uval != 0; |
---|
75 | } |
---|
76 | else /* (vn < 0) */ |
---|
77 | { |
---|
78 | vn = -vn; |
---|
79 | wp = MPZ_REALLOC (w, vn + 1); |
---|
80 | vp = PTR(v); |
---|
81 | cy = mpn_add_1 (wp, vp, vn, (mp_limb_t) uval); |
---|
82 | wp[vn] = cy; |
---|
83 | wn = vn + (cy != 0); |
---|
84 | } |
---|
85 | |
---|
86 | SIZ(w) = wn; |
---|
87 | } |
---|