Coverage Report

Created: 2024-11-21 07:03

/src/libgmp/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
20
{
36
20
  mp_ptr vp, wp;
37
20
  mp_size_t vn, wn;
38
20
  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
20
  vn = SIZ(v);
55
56
20
  if (vn > 1)
57
19
    {
58
19
      wp = MPZ_REALLOC (w, vn);
59
19
      vp = PTR(v);
60
19
      mpn_sub_1 (wp, vp, vn, (mp_limb_t) uval);
61
19
      wn = -(vn - (wp[vn - 1] == 0));
62
19
    }
63
1
  else if (vn >= 0)
64
1
    {
65
1
      mp_limb_t vp0;
66
1
      vp0 = PTR (v)[0] & - (mp_limb_t) vn;
67
1
      wp = MPZ_NEWALLOC (w, 1);
68
1
      if (uval >= vp0)
69
1
  {
70
1
    wp[0] = uval - vp0;
71
1
    wn = wp[0] != 0;
72
1
  }
73
0
      else
74
0
  {
75
0
    wp[0] = vp0 - uval;
76
0
    wn = -1;
77
0
  }
78
1
    }
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
20
  SIZ(w) = wn;
90
20
}