/src/gmp-6.2.1/mpz/ui_sub.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* mpz_ui_sub -- Subtract an unsigned one-word integer and an mpz_t. |
2 | | |
3 | | Copyright 2002, 2004, 2015 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 either: |
9 | | |
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. |
13 | | |
14 | | or |
15 | | |
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. |
19 | | |
20 | | or both in parallel, as here. |
21 | | |
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. |
26 | | |
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/. */ |
30 | | |
31 | | #include "gmp-impl.h" |
32 | | |
33 | | void |
34 | | mpz_ui_sub (mpz_ptr w, unsigned long int uval, mpz_srcptr v) |
35 | 70 | { |
36 | 70 | mp_ptr vp, wp; |
37 | 70 | mp_size_t vn, wn; |
38 | 70 | mp_limb_t cy; |
39 | | |
40 | | #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ |
41 | | if (uval > GMP_NUMB_MAX) |
42 | | { |
43 | | mpz_t u; |
44 | | mp_limb_t ul[2]; |
45 | | PTR(u) = ul; |
46 | | ul[0] = uval & GMP_NUMB_MASK; |
47 | | ul[1] = uval >> GMP_NUMB_BITS; |
48 | | SIZ(u) = 2; |
49 | | mpz_sub (w, u, v); |
50 | | return; |
51 | | } |
52 | | #endif |
53 | | |
54 | 70 | vn = SIZ(v); |
55 | | |
56 | 70 | if (vn > 1) |
57 | 31 | { |
58 | 31 | wp = MPZ_REALLOC (w, vn); |
59 | 31 | vp = PTR(v); |
60 | 31 | mpn_sub_1 (wp, vp, vn, (mp_limb_t) uval); |
61 | 31 | wn = -(vn - (wp[vn - 1] == 0)); |
62 | 31 | } |
63 | 39 | else if (vn >= 0) |
64 | 39 | { |
65 | 39 | mp_limb_t vp0; |
66 | 39 | vp0 = PTR (v)[0] & - (mp_limb_t) vn; |
67 | 39 | wp = MPZ_NEWALLOC (w, 1); |
68 | 39 | if (uval >= vp0) |
69 | 34 | { |
70 | 34 | wp[0] = uval - vp0; |
71 | 34 | wn = wp[0] != 0; |
72 | 34 | } |
73 | 5 | else |
74 | 5 | { |
75 | 5 | wp[0] = vp0 - uval; |
76 | 5 | wn = -1; |
77 | 5 | } |
78 | 39 | } |
79 | 0 | else /* (vn < 0) */ |
80 | 0 | { |
81 | 0 | vn = -vn; |
82 | 0 | wp = MPZ_REALLOC (w, vn + 1); |
83 | 0 | vp = PTR(v); |
84 | 0 | cy = mpn_add_1 (wp, vp, vn, (mp_limb_t) uval); |
85 | 0 | wp[vn] = cy; |
86 | 0 | wn = vn + (cy != 0); |
87 | 0 | } |
88 | | |
89 | 70 | SIZ(w) = wn; |
90 | 70 | } |