Coverage Report

Created: 2025-07-23 06:43

/src/gmp/mpz/aors.h
Line
Count
Source (jump to first uncovered line)
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
8.68k
#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
43.2k
{
52
43.2k
  mp_srcptr up, vp;
53
43.2k
  mp_ptr wp;
54
43.2k
  mp_size_t usize, vsize, wsize;
55
43.2k
  mp_size_t abs_usize;
56
43.2k
  mp_size_t abs_vsize;
57
58
43.2k
  usize = SIZ(u);
59
43.2k
  vsize = VARIATION SIZ(v);
60
43.2k
  abs_usize = ABS (usize);
61
43.2k
  abs_vsize = ABS (vsize);
62
63
43.2k
  if (abs_usize < abs_vsize)
64
10.1k
    {
65
      /* Swap U and V. */
66
10.1k
      MPZ_SRCPTR_SWAP (u, v);
67
10.1k
      MP_SIZE_T_SWAP (usize, vsize);
68
10.1k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
10.1k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
43.2k
  wsize = abs_usize + 1;
75
43.2k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
43.2k
  up = PTR(u);
79
43.2k
  vp = PTR(v);
80
81
43.2k
  if ((usize ^ vsize) < 0)
82
10.5k
    {
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
10.5k
      if (abs_usize != abs_vsize)
88
4.81k
  {
89
4.81k
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
4.81k
    wsize = abs_usize;
91
4.81k
    MPN_NORMALIZE_NOT_ZERO (wp, wsize);
92
4.81k
    if (usize < 0)
93
4.30k
      wsize = -wsize;
94
4.81k
  }
95
5.72k
      else
96
5.72k
  {
97
5.72k
    int cmp = mpn_cmp (up, vp, abs_usize);
98
5.72k
    if (cmp < 0)
99
5.72k
      {
100
5.72k
        mpn_sub_n (wp, vp, up, abs_usize);
101
5.72k
        wsize = abs_usize;
102
5.72k
        MPN_NORMALIZE_NOT_ZERO (wp, wsize);
103
5.72k
        if (usize >= 0)
104
0
    wsize = -wsize;
105
5.72k
      }
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
5.72k
  }
117
10.5k
    }
118
32.6k
  else
119
32.6k
    {
120
      /* U and V have same sign.  Add them.  */
121
32.6k
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
122
32.6k
      wp[abs_usize] = cy_limb;
123
32.6k
      wsize = abs_usize + cy_limb;
124
32.6k
      if (usize < 0)
125
0
  wsize = -wsize;
126
32.6k
    }
127
128
43.2k
  SIZ(w) = wsize;
129
43.2k
}
__gmpz_add
Line
Count
Source
51
34.5k
{
52
34.5k
  mp_srcptr up, vp;
53
34.5k
  mp_ptr wp;
54
34.5k
  mp_size_t usize, vsize, wsize;
55
34.5k
  mp_size_t abs_usize;
56
34.5k
  mp_size_t abs_vsize;
57
58
34.5k
  usize = SIZ(u);
59
34.5k
  vsize = VARIATION SIZ(v);
60
34.5k
  abs_usize = ABS (usize);
61
34.5k
  abs_vsize = ABS (vsize);
62
63
34.5k
  if (abs_usize < abs_vsize)
64
1.48k
    {
65
      /* Swap U and V. */
66
1.48k
      MPZ_SRCPTR_SWAP (u, v);
67
1.48k
      MP_SIZE_T_SWAP (usize, vsize);
68
1.48k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
1.48k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
34.5k
  wsize = abs_usize + 1;
75
34.5k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
34.5k
  up = PTR(u);
79
34.5k
  vp = PTR(v);
80
81
34.5k
  if ((usize ^ vsize) < 0)
82
6.23k
    {
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
6.23k
      if (abs_usize != abs_vsize)
88
511
  {
89
511
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
511
    wsize = abs_usize;
91
511
    MPN_NORMALIZE_NOT_ZERO (wp, wsize);
92
511
    if (usize < 0)
93
0
      wsize = -wsize;
94
511
  }
95
5.72k
      else
96
5.72k
  {
97
5.72k
    int cmp = mpn_cmp (up, vp, abs_usize);
98
5.72k
    if (cmp < 0)
99
5.72k
      {
100
5.72k
        mpn_sub_n (wp, vp, up, abs_usize);
101
5.72k
        wsize = abs_usize;
102
5.72k
        MPN_NORMALIZE_NOT_ZERO (wp, wsize);
103
5.72k
        if (usize >= 0)
104
0
    wsize = -wsize;
105
5.72k
      }
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
5.72k
  }
117
6.23k
    }
118
28.2k
  else
119
28.2k
    {
120
      /* U and V have same sign.  Add them.  */
121
28.2k
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
122
28.2k
      wp[abs_usize] = cy_limb;
123
28.2k
      wsize = abs_usize + cy_limb;
124
28.2k
      if (usize < 0)
125
0
  wsize = -wsize;
126
28.2k
    }
127
128
34.5k
  SIZ(w) = wsize;
129
34.5k
}
__gmpz_sub
Line
Count
Source
51
8.68k
{
52
8.68k
  mp_srcptr up, vp;
53
8.68k
  mp_ptr wp;
54
8.68k
  mp_size_t usize, vsize, wsize;
55
8.68k
  mp_size_t abs_usize;
56
8.68k
  mp_size_t abs_vsize;
57
58
8.68k
  usize = SIZ(u);
59
8.68k
  vsize = VARIATION SIZ(v);
60
8.68k
  abs_usize = ABS (usize);
61
8.68k
  abs_vsize = ABS (vsize);
62
63
8.68k
  if (abs_usize < abs_vsize)
64
8.61k
    {
65
      /* Swap U and V. */
66
8.61k
      MPZ_SRCPTR_SWAP (u, v);
67
8.61k
      MP_SIZE_T_SWAP (usize, vsize);
68
8.61k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
8.61k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
8.68k
  wsize = abs_usize + 1;
75
8.68k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
8.68k
  up = PTR(u);
79
8.68k
  vp = PTR(v);
80
81
8.68k
  if ((usize ^ vsize) < 0)
82
4.30k
    {
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
4.30k
      if (abs_usize != abs_vsize)
88
4.30k
  {
89
4.30k
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
4.30k
    wsize = abs_usize;
91
4.30k
    MPN_NORMALIZE_NOT_ZERO (wp, wsize);
92
4.30k
    if (usize < 0)
93
4.30k
      wsize = -wsize;
94
4.30k
  }
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
4.30k
    }
118
4.38k
  else
119
4.38k
    {
120
      /* U and V have same sign.  Add them.  */
121
4.38k
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
122
4.38k
      wp[abs_usize] = cy_limb;
123
4.38k
      wsize = abs_usize + cy_limb;
124
4.38k
      if (usize < 0)
125
0
  wsize = -wsize;
126
4.38k
    }
127
128
8.68k
  SIZ(w) = wsize;
129
8.68k
}