Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/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
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
20
#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
94
#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
23.8k
{
60
23.8k
  mp_srcptr up;
61
23.8k
  mp_ptr wp;
62
23.8k
  mp_size_t usize, wsize;
63
23.8k
  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
23.8k
  usize = SIZ (u);
80
23.8k
  if (usize == 0)
81
58
    {
82
58
      MPZ_NEWALLOC (w, 1)[0] = vval;
83
58
      SIZ (w) = VARIATION_NEG (vval != 0);
84
58
      return;
85
58
    }
86
87
23.7k
  abs_usize = ABS (usize);
88
89
  /* If not space for W (and possible carry), increase space.  */
90
23.7k
  wp = MPZ_REALLOC (w, abs_usize + 1);
91
92
  /* These must be after realloc (U may be the same as W).  */
93
23.7k
  up = PTR (u);
94
95
23.7k
  if (usize VARIATION_CMP 0)
96
21.8k
    {
97
21.8k
      mp_limb_t cy;
98
21.8k
      cy = mpn_add_1 (wp, up, abs_usize, (mp_limb_t) vval);
99
21.8k
      wp[abs_usize] = cy;
100
21.8k
      wsize = VARIATION_NEG (abs_usize + cy);
101
21.8k
    }
102
1.93k
  else
103
1.93k
    {
104
      /* The signs are different.  Need exact comparison to determine
105
   which operand to subtract from which.  */
106
1.93k
      if (abs_usize == 1 && up[0] < vval)
107
8
  {
108
8
    wp[0] = vval - up[0];
109
8
    wsize = VARIATION_NEG 1;
110
8
  }
111
1.92k
      else
112
1.92k
  {
113
1.92k
    mpn_sub_1 (wp, up, abs_usize, (mp_limb_t) vval);
114
    /* Size can decrease with at most one limb.  */
115
1.92k
    wsize = VARIATION_UNNEG (abs_usize - (wp[abs_usize - 1] == 0));
116
1.92k
  }
117
1.93k
    }
118
119
23.7k
  SIZ (w) = wsize;
120
23.7k
}
__gmpz_add_ui
Line
Count
Source
59
21.8k
{
60
21.8k
  mp_srcptr up;
61
21.8k
  mp_ptr wp;
62
21.8k
  mp_size_t usize, wsize;
63
21.8k
  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
21.8k
  usize = SIZ (u);
80
21.8k
  if (usize == 0)
81
8
    {
82
8
      MPZ_NEWALLOC (w, 1)[0] = vval;
83
8
      SIZ (w) = VARIATION_NEG (vval != 0);
84
8
      return;
85
8
    }
86
87
21.7k
  abs_usize = ABS (usize);
88
89
  /* If not space for W (and possible carry), increase space.  */
90
21.7k
  wp = MPZ_REALLOC (w, abs_usize + 1);
91
92
  /* These must be after realloc (U may be the same as W).  */
93
21.7k
  up = PTR (u);
94
95
21.7k
  if (usize VARIATION_CMP 0)
96
21.7k
    {
97
21.7k
      mp_limb_t cy;
98
21.7k
      cy = mpn_add_1 (wp, up, abs_usize, (mp_limb_t) vval);
99
21.7k
      wp[abs_usize] = cy;
100
21.7k
      wsize = VARIATION_NEG (abs_usize + cy);
101
21.7k
    }
102
25
  else
103
25
    {
104
      /* The signs are different.  Need exact comparison to determine
105
   which operand to subtract from which.  */
106
25
      if (abs_usize == 1 && up[0] < vval)
107
5
  {
108
5
    wp[0] = vval - up[0];
109
5
    wsize = VARIATION_NEG 1;
110
5
  }
111
20
      else
112
20
  {
113
20
    mpn_sub_1 (wp, up, abs_usize, (mp_limb_t) vval);
114
    /* Size can decrease with at most one limb.  */
115
20
    wsize = VARIATION_UNNEG (abs_usize - (wp[abs_usize - 1] == 0));
116
20
  }
117
25
    }
118
119
21.7k
  SIZ (w) = wsize;
120
21.7k
}
__gmpz_sub_ui
Line
Count
Source
59
1.99k
{
60
1.99k
  mp_srcptr up;
61
1.99k
  mp_ptr wp;
62
1.99k
  mp_size_t usize, wsize;
63
1.99k
  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
1.99k
  usize = SIZ (u);
80
1.99k
  if (usize == 0)
81
50
    {
82
50
      MPZ_NEWALLOC (w, 1)[0] = vval;
83
50
      SIZ (w) = VARIATION_NEG (vval != 0);
84
50
      return;
85
50
    }
86
87
1.94k
  abs_usize = ABS (usize);
88
89
  /* If not space for W (and possible carry), increase space.  */
90
1.94k
  wp = MPZ_REALLOC (w, abs_usize + 1);
91
92
  /* These must be after realloc (U may be the same as W).  */
93
1.94k
  up = PTR (u);
94
95
1.94k
  if (usize VARIATION_CMP 0)
96
41
    {
97
41
      mp_limb_t cy;
98
41
      cy = mpn_add_1 (wp, up, abs_usize, (mp_limb_t) vval);
99
41
      wp[abs_usize] = cy;
100
41
      wsize = VARIATION_NEG (abs_usize + cy);
101
41
    }
102
1.90k
  else
103
1.90k
    {
104
      /* The signs are different.  Need exact comparison to determine
105
   which operand to subtract from which.  */
106
1.90k
      if (abs_usize == 1 && up[0] < vval)
107
3
  {
108
3
    wp[0] = vval - up[0];
109
3
    wsize = VARIATION_NEG 1;
110
3
  }
111
1.90k
      else
112
1.90k
  {
113
1.90k
    mpn_sub_1 (wp, up, abs_usize, (mp_limb_t) vval);
114
    /* Size can decrease with at most one limb.  */
115
1.90k
    wsize = VARIATION_UNNEG (abs_usize - (wp[abs_usize - 1] == 0));
116
1.90k
  }
117
1.90k
    }
118
119
1.94k
  SIZ (w) = wsize;
120
1.94k
}