Coverage Report

Created: 2026-11-12 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gmp/mpz/aors.h
Line
Count
Source
1
/* mpz_add, mpz_sub -- add or subtract integers.
2
3
Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2011, 2012, 2020 Free
4
Software Foundation, Inc.
5
6
This file is part of the GNU MP Library.
7
8
The GNU MP Library is free software; you can redistribute it and/or modify
9
it under the terms of either:
10
11
  * the GNU Lesser General Public License as published by the Free
12
    Software Foundation; either version 3 of the License, or (at your
13
    option) any later version.
14
15
or
16
17
  * the GNU General Public License as published by the Free Software
18
    Foundation; either version 2 of the License, or (at your option) any
19
    later version.
20
21
or both in parallel, as here.
22
23
The GNU MP Library is distributed in the hope that it will be useful, but
24
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
for more details.
27
28
You should have received copies of the GNU General Public License and the
29
GNU Lesser General Public License along with the GNU MP Library.  If not,
30
see https://www.gnu.org/licenses/.  */
31
32
#include "gmp-impl.h"
33
34
35
#ifdef OPERATION_add
36
#define FUNCTION     mpz_add
37
#define VARIATION
38
#endif
39
#ifdef OPERATION_sub
40
#define FUNCTION     mpz_sub
41
7.90k
#define VARIATION    -
42
#endif
43
44
#ifndef FUNCTION
45
Error, need OPERATION_add or OPERATION_sub
46
#endif
47
48
49
void
50
FUNCTION (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
51
37.9k
{
52
37.9k
  mp_srcptr up, vp;
53
37.9k
  mp_ptr wp;
54
37.9k
  mp_size_t usize, vsize, wsize;
55
37.9k
  mp_size_t abs_usize;
56
37.9k
  mp_size_t abs_vsize;
57
58
37.9k
  usize = SIZ(u);
59
37.9k
  vsize = VARIATION SIZ(v);
60
37.9k
  abs_usize = ABS (usize);
61
37.9k
  abs_vsize = ABS (vsize);
62
63
37.9k
  if (abs_usize < abs_vsize)
64
9.25k
    {
65
      /* Swap U and V. */
66
9.25k
      MPZ_SRCPTR_SWAP (u, v);
67
9.25k
      MP_SIZE_T_SWAP (usize, vsize);
68
9.25k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
9.25k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
37.9k
  wsize = abs_usize + 1;
75
37.9k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
37.9k
  up = PTR(u);
79
37.9k
  vp = PTR(v);
80
81
37.9k
  if ((usize ^ vsize) < 0)
82
9.44k
    {
83
      /* U and V have different sign.  Need to compare them to determine
84
   which operand to subtract from which.  */
85
86
      /* This test is right since ABS_USIZE >= ABS_VSIZE.  */
87
9.44k
      if (abs_usize != abs_vsize)
88
4.51k
  {
89
4.51k
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
4.51k
    wsize = abs_usize;
91
4.51k
    MPN_NORMALIZE_NOT_ZERO (wp, wsize);
92
4.51k
    if (usize < 0)
93
3.99k
      wsize = -wsize;
94
4.51k
  }
95
4.92k
      else
96
4.92k
  {
97
4.92k
    int cmp = mpn_cmp (up, vp, abs_usize);
98
4.92k
    if (cmp < 0)
99
4.92k
      {
100
4.92k
        mpn_sub_n (wp, vp, up, abs_usize);
101
4.92k
        wsize = abs_usize;
102
4.92k
        MPN_NORMALIZE_NOT_ZERO (wp, wsize);
103
4.92k
        if (usize >= 0)
104
0
    wsize = -wsize;
105
4.92k
      }
106
0
    else if (cmp > 0)
107
0
      {
108
0
        mpn_sub_n (wp, up, vp, abs_usize);
109
0
        wsize = abs_usize;
110
0
        MPN_NORMALIZE_NOT_ZERO (wp, wsize);
111
0
        if (usize < 0)
112
0
    wsize = -wsize;
113
0
      }
114
0
    else
115
0
      wsize = 0;
116
4.92k
  }
117
9.44k
    }
118
28.4k
  else
119
28.4k
    {
120
      /* U and V have same sign.  Add them.  */
121
28.4k
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
122
28.4k
      wp[abs_usize] = cy_limb;
123
28.4k
      wsize = abs_usize + cy_limb;
124
28.4k
      if (usize < 0)
125
0
  wsize = -wsize;
126
28.4k
    }
127
128
37.9k
  SIZ(w) = wsize;
129
37.9k
}
__gmpz_add
Line
Count
Source
51
30.0k
{
52
30.0k
  mp_srcptr up, vp;
53
30.0k
  mp_ptr wp;
54
30.0k
  mp_size_t usize, vsize, wsize;
55
30.0k
  mp_size_t abs_usize;
56
30.0k
  mp_size_t abs_vsize;
57
58
30.0k
  usize = SIZ(u);
59
30.0k
  vsize = VARIATION SIZ(v);
60
30.0k
  abs_usize = ABS (usize);
61
30.0k
  abs_vsize = ABS (vsize);
62
63
30.0k
  if (abs_usize < abs_vsize)
64
1.43k
    {
65
      /* Swap U and V. */
66
1.43k
      MPZ_SRCPTR_SWAP (u, v);
67
1.43k
      MP_SIZE_T_SWAP (usize, vsize);
68
1.43k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
1.43k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
30.0k
  wsize = abs_usize + 1;
75
30.0k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
30.0k
  up = PTR(u);
79
30.0k
  vp = PTR(v);
80
81
30.0k
  if ((usize ^ vsize) < 0)
82
5.44k
    {
83
      /* U and V have different sign.  Need to compare them to determine
84
   which operand to subtract from which.  */
85
86
      /* This test is right since ABS_USIZE >= ABS_VSIZE.  */
87
5.44k
      if (abs_usize != abs_vsize)
88
522
  {
89
522
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
522
    wsize = abs_usize;
91
522
    MPN_NORMALIZE_NOT_ZERO (wp, wsize);
92
522
    if (usize < 0)
93
0
      wsize = -wsize;
94
522
  }
95
4.92k
      else
96
4.92k
  {
97
4.92k
    int cmp = mpn_cmp (up, vp, abs_usize);
98
4.92k
    if (cmp < 0)
99
4.92k
      {
100
4.92k
        mpn_sub_n (wp, vp, up, abs_usize);
101
4.92k
        wsize = abs_usize;
102
4.92k
        MPN_NORMALIZE_NOT_ZERO (wp, wsize);
103
4.92k
        if (usize >= 0)
104
0
    wsize = -wsize;
105
4.92k
      }
106
0
    else if (cmp > 0)
107
0
      {
108
0
        mpn_sub_n (wp, up, vp, abs_usize);
109
0
        wsize = abs_usize;
110
0
        MPN_NORMALIZE_NOT_ZERO (wp, wsize);
111
0
        if (usize < 0)
112
0
    wsize = -wsize;
113
0
      }
114
0
    else
115
0
      wsize = 0;
116
4.92k
  }
117
5.44k
    }
118
24.5k
  else
119
24.5k
    {
120
      /* U and V have same sign.  Add them.  */
121
24.5k
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
122
24.5k
      wp[abs_usize] = cy_limb;
123
24.5k
      wsize = abs_usize + cy_limb;
124
24.5k
      if (usize < 0)
125
0
  wsize = -wsize;
126
24.5k
    }
127
128
30.0k
  SIZ(w) = wsize;
129
30.0k
}
__gmpz_sub
Line
Count
Source
51
7.90k
{
52
7.90k
  mp_srcptr up, vp;
53
7.90k
  mp_ptr wp;
54
7.90k
  mp_size_t usize, vsize, wsize;
55
7.90k
  mp_size_t abs_usize;
56
7.90k
  mp_size_t abs_vsize;
57
58
7.90k
  usize = SIZ(u);
59
7.90k
  vsize = VARIATION SIZ(v);
60
7.90k
  abs_usize = ABS (usize);
61
7.90k
  abs_vsize = ABS (vsize);
62
63
7.90k
  if (abs_usize < abs_vsize)
64
7.81k
    {
65
      /* Swap U and V. */
66
7.81k
      MPZ_SRCPTR_SWAP (u, v);
67
7.81k
      MP_SIZE_T_SWAP (usize, vsize);
68
7.81k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
7.81k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
7.90k
  wsize = abs_usize + 1;
75
7.90k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
7.90k
  up = PTR(u);
79
7.90k
  vp = PTR(v);
80
81
7.90k
  if ((usize ^ vsize) < 0)
82
3.99k
    {
83
      /* U and V have different sign.  Need to compare them to determine
84
   which operand to subtract from which.  */
85
86
      /* This test is right since ABS_USIZE >= ABS_VSIZE.  */
87
3.99k
      if (abs_usize != abs_vsize)
88
3.99k
  {
89
3.99k
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
3.99k
    wsize = abs_usize;
91
3.99k
    MPN_NORMALIZE_NOT_ZERO (wp, wsize);
92
3.99k
    if (usize < 0)
93
3.99k
      wsize = -wsize;
94
3.99k
  }
95
0
      else
96
0
  {
97
0
    int cmp = mpn_cmp (up, vp, abs_usize);
98
0
    if (cmp < 0)
99
0
      {
100
0
        mpn_sub_n (wp, vp, up, abs_usize);
101
0
        wsize = abs_usize;
102
0
        MPN_NORMALIZE_NOT_ZERO (wp, wsize);
103
0
        if (usize >= 0)
104
0
    wsize = -wsize;
105
0
      }
106
0
    else if (cmp > 0)
107
0
      {
108
0
        mpn_sub_n (wp, up, vp, abs_usize);
109
0
        wsize = abs_usize;
110
0
        MPN_NORMALIZE_NOT_ZERO (wp, wsize);
111
0
        if (usize < 0)
112
0
    wsize = -wsize;
113
0
      }
114
0
    else
115
0
      wsize = 0;
116
0
  }
117
3.99k
    }
118
3.91k
  else
119
3.91k
    {
120
      /* U and V have same sign.  Add them.  */
121
3.91k
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
122
3.91k
      wp[abs_usize] = cy_limb;
123
3.91k
      wsize = abs_usize + cy_limb;
124
3.91k
      if (usize < 0)
125
0
  wsize = -wsize;
126
3.91k
    }
127
128
7.90k
  SIZ(w) = wsize;
129
7.90k
}