Coverage Report

Created: 2024-06-28 06:19

/src/gmp-6.2.1/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 Free Software
4
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
1.88k
#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
2.68k
{
52
2.68k
  mp_srcptr up, vp;
53
2.68k
  mp_ptr wp;
54
2.68k
  mp_size_t usize, vsize, wsize;
55
2.68k
  mp_size_t abs_usize;
56
2.68k
  mp_size_t abs_vsize;
57
58
2.68k
  usize = SIZ(u);
59
2.68k
  vsize = VARIATION SIZ(v);
60
2.68k
  abs_usize = ABS (usize);
61
2.68k
  abs_vsize = ABS (vsize);
62
63
2.68k
  if (abs_usize < abs_vsize)
64
1.92k
    {
65
      /* Swap U and V. */
66
1.92k
      MPZ_SRCPTR_SWAP (u, v);
67
1.92k
      MP_SIZE_T_SWAP (usize, vsize);
68
1.92k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
1.92k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
2.68k
  wsize = abs_usize + 1;
75
2.68k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
2.68k
  up = PTR(u);
79
2.68k
  vp = PTR(v);
80
81
2.68k
  if ((usize ^ vsize) < 0)
82
1.74k
    {
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
1.74k
      if (abs_usize != abs_vsize)
88
1.18k
  {
89
1.18k
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
1.18k
    wsize = abs_usize;
91
1.18k
    MPN_NORMALIZE (wp, wsize);
92
1.18k
    if (usize < 0)
93
976
      wsize = -wsize;
94
1.18k
  }
95
556
      else if (mpn_cmp (up, vp, abs_usize) < 0)
96
515
  {
97
515
    mpn_sub_n (wp, vp, up, abs_usize);
98
515
    wsize = abs_usize;
99
515
    MPN_NORMALIZE (wp, wsize);
100
515
    if (usize >= 0)
101
45
      wsize = -wsize;
102
515
  }
103
41
      else
104
41
  {
105
41
    mpn_sub_n (wp, up, vp, abs_usize);
106
41
    wsize = abs_usize;
107
41
    MPN_NORMALIZE (wp, wsize);
108
41
    if (usize < 0)
109
0
      wsize = -wsize;
110
41
  }
111
1.74k
    }
112
949
  else
113
949
    {
114
      /* U and V have same sign.  Add them.  */
115
949
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
116
949
      wp[abs_usize] = cy_limb;
117
949
      wsize = abs_usize + cy_limb;
118
949
      if (usize < 0)
119
0
  wsize = -wsize;
120
949
    }
121
122
2.68k
  SIZ(w) = wsize;
123
2.68k
}
__gmpz_add
Line
Count
Source
51
809
{
52
809
  mp_srcptr up, vp;
53
809
  mp_ptr wp;
54
809
  mp_size_t usize, vsize, wsize;
55
809
  mp_size_t abs_usize;
56
809
  mp_size_t abs_vsize;
57
58
809
  usize = SIZ(u);
59
809
  vsize = VARIATION SIZ(v);
60
809
  abs_usize = ABS (usize);
61
809
  abs_vsize = ABS (vsize);
62
63
809
  if (abs_usize < abs_vsize)
64
226
    {
65
      /* Swap U and V. */
66
226
      MPZ_SRCPTR_SWAP (u, v);
67
226
      MP_SIZE_T_SWAP (usize, vsize);
68
226
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
226
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
809
  wsize = abs_usize + 1;
75
809
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
809
  up = PTR(u);
79
809
  vp = PTR(v);
80
81
809
  if ((usize ^ vsize) < 0)
82
619
    {
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
619
      if (abs_usize != abs_vsize)
88
149
  {
89
149
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
149
    wsize = abs_usize;
91
149
    MPN_NORMALIZE (wp, wsize);
92
149
    if (usize < 0)
93
0
      wsize = -wsize;
94
149
  }
95
470
      else if (mpn_cmp (up, vp, abs_usize) < 0)
96
470
  {
97
470
    mpn_sub_n (wp, vp, up, abs_usize);
98
470
    wsize = abs_usize;
99
470
    MPN_NORMALIZE (wp, wsize);
100
470
    if (usize >= 0)
101
0
      wsize = -wsize;
102
470
  }
103
0
      else
104
0
  {
105
0
    mpn_sub_n (wp, up, vp, abs_usize);
106
0
    wsize = abs_usize;
107
0
    MPN_NORMALIZE (wp, wsize);
108
0
    if (usize < 0)
109
0
      wsize = -wsize;
110
0
  }
111
619
    }
112
190
  else
113
190
    {
114
      /* U and V have same sign.  Add them.  */
115
190
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
116
190
      wp[abs_usize] = cy_limb;
117
190
      wsize = abs_usize + cy_limb;
118
190
      if (usize < 0)
119
0
  wsize = -wsize;
120
190
    }
121
122
809
  SIZ(w) = wsize;
123
809
}
__gmpz_sub
Line
Count
Source
51
1.88k
{
52
1.88k
  mp_srcptr up, vp;
53
1.88k
  mp_ptr wp;
54
1.88k
  mp_size_t usize, vsize, wsize;
55
1.88k
  mp_size_t abs_usize;
56
1.88k
  mp_size_t abs_vsize;
57
58
1.88k
  usize = SIZ(u);
59
1.88k
  vsize = VARIATION SIZ(v);
60
1.88k
  abs_usize = ABS (usize);
61
1.88k
  abs_vsize = ABS (vsize);
62
63
1.88k
  if (abs_usize < abs_vsize)
64
1.70k
    {
65
      /* Swap U and V. */
66
1.70k
      MPZ_SRCPTR_SWAP (u, v);
67
1.70k
      MP_SIZE_T_SWAP (usize, vsize);
68
1.70k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
1.70k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
1.88k
  wsize = abs_usize + 1;
75
1.88k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
1.88k
  up = PTR(u);
79
1.88k
  vp = PTR(v);
80
81
1.88k
  if ((usize ^ vsize) < 0)
82
1.12k
    {
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
1.12k
      if (abs_usize != abs_vsize)
88
1.03k
  {
89
1.03k
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
1.03k
    wsize = abs_usize;
91
1.03k
    MPN_NORMALIZE (wp, wsize);
92
1.03k
    if (usize < 0)
93
976
      wsize = -wsize;
94
1.03k
  }
95
86
      else if (mpn_cmp (up, vp, abs_usize) < 0)
96
45
  {
97
45
    mpn_sub_n (wp, vp, up, abs_usize);
98
45
    wsize = abs_usize;
99
45
    MPN_NORMALIZE (wp, wsize);
100
45
    if (usize >= 0)
101
45
      wsize = -wsize;
102
45
  }
103
41
      else
104
41
  {
105
41
    mpn_sub_n (wp, up, vp, abs_usize);
106
41
    wsize = abs_usize;
107
41
    MPN_NORMALIZE (wp, wsize);
108
41
    if (usize < 0)
109
0
      wsize = -wsize;
110
41
  }
111
1.12k
    }
112
759
  else
113
759
    {
114
      /* U and V have same sign.  Add them.  */
115
759
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
116
759
      wp[abs_usize] = cy_limb;
117
759
      wsize = abs_usize + cy_limb;
118
759
      if (usize < 0)
119
0
  wsize = -wsize;
120
759
    }
121
122
1.88k
  SIZ(w) = wsize;
123
1.88k
}