Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/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 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
711k
#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
713k
{
52
713k
  mp_srcptr up, vp;
53
713k
  mp_ptr wp;
54
713k
  mp_size_t usize, vsize, wsize;
55
713k
  mp_size_t abs_usize;
56
713k
  mp_size_t abs_vsize;
57
58
713k
  usize = SIZ(u);
59
713k
  vsize = VARIATION SIZ(v);
60
713k
  abs_usize = ABS (usize);
61
713k
  abs_vsize = ABS (vsize);
62
63
713k
  if (abs_usize < abs_vsize)
64
39.7k
    {
65
      /* Swap U and V. */
66
39.7k
      MPZ_SRCPTR_SWAP (u, v);
67
39.7k
      MP_SIZE_T_SWAP (usize, vsize);
68
39.7k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
39.7k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
713k
  wsize = abs_usize + 1;
75
713k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
713k
  up = PTR(u);
79
713k
  vp = PTR(v);
80
81
713k
  if ((usize ^ vsize) < 0)
82
532k
    {
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
532k
      if (abs_usize != abs_vsize)
88
53.1k
  {
89
53.1k
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
53.1k
    wsize = abs_usize;
91
53.1k
    MPN_NORMALIZE (wp, wsize);
92
53.1k
    if (usize < 0)
93
28.9k
      wsize = -wsize;
94
53.1k
  }
95
479k
      else if (mpn_cmp (up, vp, abs_usize) < 0)
96
279k
  {
97
279k
    mpn_sub_n (wp, vp, up, abs_usize);
98
279k
    wsize = abs_usize;
99
279k
    MPN_NORMALIZE (wp, wsize);
100
279k
    if (usize >= 0)
101
204k
      wsize = -wsize;
102
279k
  }
103
200k
      else
104
200k
  {
105
200k
    mpn_sub_n (wp, up, vp, abs_usize);
106
200k
    wsize = abs_usize;
107
200k
    MPN_NORMALIZE (wp, wsize);
108
200k
    if (usize < 0)
109
59.7k
      wsize = -wsize;
110
200k
  }
111
532k
    }
112
181k
  else
113
181k
    {
114
      /* U and V have same sign.  Add them.  */
115
181k
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
116
181k
      wp[abs_usize] = cy_limb;
117
181k
      wsize = abs_usize + cy_limb;
118
181k
      if (usize < 0)
119
95.9k
  wsize = -wsize;
120
181k
    }
121
122
713k
  SIZ(w) = wsize;
123
713k
}
__gmpz_add
Line
Count
Source
51
2.52k
{
52
2.52k
  mp_srcptr up, vp;
53
2.52k
  mp_ptr wp;
54
2.52k
  mp_size_t usize, vsize, wsize;
55
2.52k
  mp_size_t abs_usize;
56
2.52k
  mp_size_t abs_vsize;
57
58
2.52k
  usize = SIZ(u);
59
2.52k
  vsize = VARIATION SIZ(v);
60
2.52k
  abs_usize = ABS (usize);
61
2.52k
  abs_vsize = ABS (vsize);
62
63
2.52k
  if (abs_usize < abs_vsize)
64
192
    {
65
      /* Swap U and V. */
66
192
      MPZ_SRCPTR_SWAP (u, v);
67
192
      MP_SIZE_T_SWAP (usize, vsize);
68
192
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
192
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
2.52k
  wsize = abs_usize + 1;
75
2.52k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
2.52k
  up = PTR(u);
79
2.52k
  vp = PTR(v);
80
81
2.52k
  if ((usize ^ vsize) < 0)
82
609
    {
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
609
      if (abs_usize != abs_vsize)
88
154
  {
89
154
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
154
    wsize = abs_usize;
91
154
    MPN_NORMALIZE (wp, wsize);
92
154
    if (usize < 0)
93
49
      wsize = -wsize;
94
154
  }
95
455
      else if (mpn_cmp (up, vp, abs_usize) < 0)
96
398
  {
97
398
    mpn_sub_n (wp, vp, up, abs_usize);
98
398
    wsize = abs_usize;
99
398
    MPN_NORMALIZE (wp, wsize);
100
398
    if (usize >= 0)
101
72
      wsize = -wsize;
102
398
  }
103
57
      else
104
57
  {
105
57
    mpn_sub_n (wp, up, vp, abs_usize);
106
57
    wsize = abs_usize;
107
57
    MPN_NORMALIZE (wp, wsize);
108
57
    if (usize < 0)
109
38
      wsize = -wsize;
110
57
  }
111
609
    }
112
1.92k
  else
113
1.92k
    {
114
      /* U and V have same sign.  Add them.  */
115
1.92k
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
116
1.92k
      wp[abs_usize] = cy_limb;
117
1.92k
      wsize = abs_usize + cy_limb;
118
1.92k
      if (usize < 0)
119
112
  wsize = -wsize;
120
1.92k
    }
121
122
2.52k
  SIZ(w) = wsize;
123
2.52k
}
__gmpz_sub
Line
Count
Source
51
711k
{
52
711k
  mp_srcptr up, vp;
53
711k
  mp_ptr wp;
54
711k
  mp_size_t usize, vsize, wsize;
55
711k
  mp_size_t abs_usize;
56
711k
  mp_size_t abs_vsize;
57
58
711k
  usize = SIZ(u);
59
711k
  vsize = VARIATION SIZ(v);
60
711k
  abs_usize = ABS (usize);
61
711k
  abs_vsize = ABS (vsize);
62
63
711k
  if (abs_usize < abs_vsize)
64
39.6k
    {
65
      /* Swap U and V. */
66
39.6k
      MPZ_SRCPTR_SWAP (u, v);
67
39.6k
      MP_SIZE_T_SWAP (usize, vsize);
68
39.6k
      MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69
39.6k
    }
70
71
  /* True: ABS_USIZE >= ABS_VSIZE.  */
72
73
  /* If not space for w (and possible carry), increase space.  */
74
711k
  wsize = abs_usize + 1;
75
711k
  wp = MPZ_REALLOC (w, wsize);
76
77
  /* These must be after realloc (u or v may be the same as w).  */
78
711k
  up = PTR(u);
79
711k
  vp = PTR(v);
80
81
711k
  if ((usize ^ vsize) < 0)
82
531k
    {
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
531k
      if (abs_usize != abs_vsize)
88
53.0k
  {
89
53.0k
    mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90
53.0k
    wsize = abs_usize;
91
53.0k
    MPN_NORMALIZE (wp, wsize);
92
53.0k
    if (usize < 0)
93
28.8k
      wsize = -wsize;
94
53.0k
  }
95
478k
      else if (mpn_cmp (up, vp, abs_usize) < 0)
96
278k
  {
97
278k
    mpn_sub_n (wp, vp, up, abs_usize);
98
278k
    wsize = abs_usize;
99
278k
    MPN_NORMALIZE (wp, wsize);
100
278k
    if (usize >= 0)
101
204k
      wsize = -wsize;
102
278k
  }
103
200k
      else
104
200k
  {
105
200k
    mpn_sub_n (wp, up, vp, abs_usize);
106
200k
    wsize = abs_usize;
107
200k
    MPN_NORMALIZE (wp, wsize);
108
200k
    if (usize < 0)
109
59.7k
      wsize = -wsize;
110
200k
  }
111
531k
    }
112
179k
  else
113
179k
    {
114
      /* U and V have same sign.  Add them.  */
115
179k
      mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
116
179k
      wp[abs_usize] = cy_limb;
117
179k
      wsize = abs_usize + cy_limb;
118
179k
      if (usize < 0)
119
95.8k
  wsize = -wsize;
120
179k
    }
121
122
711k
  SIZ(w) = wsize;
123
711k
}