Coverage Report

Created: 2026-03-31 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gmp/mpz/aors_ui.h
Line
Count
Source
1
/* mpz_add_ui, mpz_sub_ui -- Add or subtract an mpz_t and an unsigned
2
   one-word integer.
3
4
Copyright 1991, 1993, 1994, 1996, 1999-2002, 2004, 2012, 2013, 2015,
5
2020 Free Software Foundation, Inc.
6
7
This file is part of the GNU MP Library.
8
9
The GNU MP Library is free software; you can redistribute it and/or modify
10
it under the terms of either:
11
12
  * the GNU Lesser General Public License as published by the Free
13
    Software Foundation; either version 3 of the License, or (at your
14
    option) any later version.
15
16
or
17
18
  * the GNU General Public License as published by the Free Software
19
    Foundation; either version 2 of the License, or (at your option) any
20
    later version.
21
22
or both in parallel, as here.
23
24
The GNU MP Library is distributed in the hope that it will be useful, but
25
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27
for more details.
28
29
You should have received copies of the GNU General Public License and the
30
GNU Lesser General Public License along with the GNU MP Library.  If not,
31
see https://www.gnu.org/licenses/.  */
32
33
#include "gmp-impl.h"
34
35
36
#ifdef OPERATION_add_ui
37
#define FUNCTION          mpz_add_ui
38
#define FUNCTION2         mpz_add
39
#define VARIATION_CMP     >=
40
#define VARIATION_NEG
41
0
#define VARIATION_UNNEG   -
42
#endif
43
44
#ifdef OPERATION_sub_ui
45
#define FUNCTION          mpz_sub_ui
46
#define FUNCTION2         mpz_sub
47
#define VARIATION_CMP     <
48
46
#define VARIATION_NEG     -
49
#define VARIATION_UNNEG
50
#endif
51
52
#ifndef FUNCTION
53
Error, need OPERATION_add_ui or OPERATION_sub_ui
54
#endif
55
56
57
void
58
FUNCTION (mpz_ptr w, mpz_srcptr u, unsigned long int vval)
59
88.1k
{
60
88.1k
  mp_srcptr up;
61
88.1k
  mp_ptr wp;
62
88.1k
  mp_size_t usize, wsize;
63
88.1k
  mp_size_t abs_usize;
64
65
#if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
66
  if (vval > GMP_NUMB_MAX)
67
    {
68
      mpz_t v;
69
      mp_limb_t vl[2];
70
      PTR(v) = vl;
71
      vl[0] = vval & GMP_NUMB_MASK;
72
      vl[1] = vval >> GMP_NUMB_BITS;
73
      SIZ(v) = 2;
74
      FUNCTION2 (w, u, v);
75
      return;
76
    }
77
#endif
78
79
88.1k
  usize = SIZ (u);
80
88.1k
  if (usize == 0)
81
29
    {
82
29
      MPZ_NEWALLOC (w, 1)[0] = vval;
83
29
      SIZ (w) = VARIATION_NEG (vval != 0);
84
29
      return;
85
29
    }
86
87
88.0k
  abs_usize = ABS (usize);
88
89
88.0k
  if (usize VARIATION_CMP 0)
90
4.93k
    {
91
4.93k
      mp_limb_t cy;
92
93
      /* If not space for W (and possible carry), increase space.  */
94
4.93k
      wp = MPZ_REALLOC (w, abs_usize + 1);
95
      /* These must be after realloc (U may be the same as W).  */
96
4.93k
      up = PTR (u);
97
98
4.93k
      cy = mpn_add_1 (wp, up, abs_usize, (mp_limb_t) vval);
99
4.93k
      wp[abs_usize] = cy;
100
4.93k
      wsize = VARIATION_NEG (abs_usize + cy);
101
4.93k
    }
102
83.1k
  else
103
83.1k
    {
104
      /* If not space for W, increase space.  */
105
83.1k
      wp = MPZ_REALLOC (w, abs_usize);
106
      /* These must be after realloc (U may be the same as W).  */
107
83.1k
      up = PTR (u);
108
109
      /* The signs are different.  Need exact comparison to determine
110
   which operand to subtract from which.  */
111
83.1k
      if (abs_usize == 1 && up[0] < vval)
112
17
  {
113
17
    wp[0] = vval - up[0];
114
17
    wsize = VARIATION_NEG 1;
115
17
  }
116
83.1k
      else
117
83.1k
  {
118
83.1k
    mpn_sub_1 (wp, up, abs_usize, (mp_limb_t) vval);
119
    /* Size can decrease with at most one limb.  */
120
83.1k
    wsize = VARIATION_UNNEG (abs_usize - (wp[abs_usize - 1] == 0));
121
83.1k
  }
122
83.1k
    }
123
124
88.0k
  SIZ (w) = wsize;
125
88.0k
}
__gmpz_add_ui
Line
Count
Source
59
4.93k
{
60
4.93k
  mp_srcptr up;
61
4.93k
  mp_ptr wp;
62
4.93k
  mp_size_t usize, wsize;
63
4.93k
  mp_size_t abs_usize;
64
65
#if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
66
  if (vval > GMP_NUMB_MAX)
67
    {
68
      mpz_t v;
69
      mp_limb_t vl[2];
70
      PTR(v) = vl;
71
      vl[0] = vval & GMP_NUMB_MASK;
72
      vl[1] = vval >> GMP_NUMB_BITS;
73
      SIZ(v) = 2;
74
      FUNCTION2 (w, u, v);
75
      return;
76
    }
77
#endif
78
79
4.93k
  usize = SIZ (u);
80
4.93k
  if (usize == 0)
81
0
    {
82
0
      MPZ_NEWALLOC (w, 1)[0] = vval;
83
0
      SIZ (w) = VARIATION_NEG (vval != 0);
84
0
      return;
85
0
    }
86
87
4.93k
  abs_usize = ABS (usize);
88
89
4.93k
  if (usize VARIATION_CMP 0)
90
4.93k
    {
91
4.93k
      mp_limb_t cy;
92
93
      /* If not space for W (and possible carry), increase space.  */
94
4.93k
      wp = MPZ_REALLOC (w, abs_usize + 1);
95
      /* These must be after realloc (U may be the same as W).  */
96
4.93k
      up = PTR (u);
97
98
4.93k
      cy = mpn_add_1 (wp, up, abs_usize, (mp_limb_t) vval);
99
4.93k
      wp[abs_usize] = cy;
100
4.93k
      wsize = VARIATION_NEG (abs_usize + cy);
101
4.93k
    }
102
0
  else
103
0
    {
104
      /* If not space for W, increase space.  */
105
0
      wp = MPZ_REALLOC (w, abs_usize);
106
      /* These must be after realloc (U may be the same as W).  */
107
0
      up = PTR (u);
108
109
      /* The signs are different.  Need exact comparison to determine
110
   which operand to subtract from which.  */
111
0
      if (abs_usize == 1 && up[0] < vval)
112
0
  {
113
0
    wp[0] = vval - up[0];
114
0
    wsize = VARIATION_NEG 1;
115
0
  }
116
0
      else
117
0
  {
118
0
    mpn_sub_1 (wp, up, abs_usize, (mp_limb_t) vval);
119
    /* Size can decrease with at most one limb.  */
120
0
    wsize = VARIATION_UNNEG (abs_usize - (wp[abs_usize - 1] == 0));
121
0
  }
122
0
    }
123
124
4.93k
  SIZ (w) = wsize;
125
4.93k
}
__gmpz_sub_ui
Line
Count
Source
59
83.1k
{
60
83.1k
  mp_srcptr up;
61
83.1k
  mp_ptr wp;
62
83.1k
  mp_size_t usize, wsize;
63
83.1k
  mp_size_t abs_usize;
64
65
#if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
66
  if (vval > GMP_NUMB_MAX)
67
    {
68
      mpz_t v;
69
      mp_limb_t vl[2];
70
      PTR(v) = vl;
71
      vl[0] = vval & GMP_NUMB_MASK;
72
      vl[1] = vval >> GMP_NUMB_BITS;
73
      SIZ(v) = 2;
74
      FUNCTION2 (w, u, v);
75
      return;
76
    }
77
#endif
78
79
83.1k
  usize = SIZ (u);
80
83.1k
  if (usize == 0)
81
29
    {
82
29
      MPZ_NEWALLOC (w, 1)[0] = vval;
83
29
      SIZ (w) = VARIATION_NEG (vval != 0);
84
29
      return;
85
29
    }
86
87
83.1k
  abs_usize = ABS (usize);
88
89
83.1k
  if (usize VARIATION_CMP 0)
90
0
    {
91
0
      mp_limb_t cy;
92
93
      /* If not space for W (and possible carry), increase space.  */
94
0
      wp = MPZ_REALLOC (w, abs_usize + 1);
95
      /* These must be after realloc (U may be the same as W).  */
96
0
      up = PTR (u);
97
98
0
      cy = mpn_add_1 (wp, up, abs_usize, (mp_limb_t) vval);
99
0
      wp[abs_usize] = cy;
100
0
      wsize = VARIATION_NEG (abs_usize + cy);
101
0
    }
102
83.1k
  else
103
83.1k
    {
104
      /* If not space for W, increase space.  */
105
83.1k
      wp = MPZ_REALLOC (w, abs_usize);
106
      /* These must be after realloc (U may be the same as W).  */
107
83.1k
      up = PTR (u);
108
109
      /* The signs are different.  Need exact comparison to determine
110
   which operand to subtract from which.  */
111
83.1k
      if (abs_usize == 1 && up[0] < vval)
112
17
  {
113
17
    wp[0] = vval - up[0];
114
17
    wsize = VARIATION_NEG 1;
115
17
  }
116
83.1k
      else
117
83.1k
  {
118
83.1k
    mpn_sub_1 (wp, up, abs_usize, (mp_limb_t) vval);
119
    /* Size can decrease with at most one limb.  */
120
83.1k
    wsize = VARIATION_UNNEG (abs_usize - (wp[abs_usize - 1] == 0));
121
83.1k
  }
122
83.1k
    }
123
124
83.1k
  SIZ (w) = wsize;
125
83.1k
}