Coverage Report

Created: 2024-06-28 06:19

/src/gmp-6.2.1/mpz/xor.c
Line
Count
Source (jump to first uncovered line)
1
/* mpz_xor -- Logical xor.
2
3
Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2005, 2012,
4
2015-2018 Free 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
void
35
mpz_xor (mpz_ptr res, mpz_srcptr op1, mpz_srcptr op2)
36
33
{
37
33
  mp_srcptr op1_ptr, op2_ptr;
38
33
  mp_size_t op1_size, op2_size;
39
33
  mp_ptr res_ptr;
40
33
  mp_size_t res_size;
41
42
33
  op1_size = SIZ(op1);
43
33
  op2_size = SIZ(op2);
44
45
33
  if (op1_size < op2_size)
46
8
    {
47
8
      MPZ_SRCPTR_SWAP (op1, op2);
48
8
      MP_SIZE_T_SWAP (op1_size, op2_size);
49
8
    }
50
51
33
  op1_ptr = PTR(op1);
52
33
  res_ptr = PTR(res);
53
54
33
  if (op2_size >= 0)
55
33
    {
56
33
      if (res_ptr != op1_ptr)
57
19
  {
58
19
    res_ptr = MPZ_REALLOC (res, op1_size);
59
19
    MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size,
60
19
        op1_size - op2_size);
61
19
  }
62
33
      if (LIKELY (op2_size != 0))
63
29
  mpn_xor_n (res_ptr, op1_ptr, PTR(op2), op2_size);
64
33
      res_size = op1_size;
65
66
33
      MPN_NORMALIZE (res_ptr, res_size);
67
33
      SIZ(res) = res_size;
68
33
    }
69
0
  else
70
0
    {
71
0
      mp_ptr opx;
72
0
      TMP_DECL;
73
74
0
      op2_size = -op2_size;
75
0
      TMP_MARK;
76
0
      if (op1_size < 0)
77
0
  {
78
0
    mp_ptr opy;
79
80
    /* Both operands are negative, the result will be positive.
81
        (-OP1) ^ (-OP2) =
82
       = ~(OP1 - 1) ^ ~(OP2 - 1) =
83
       = (OP1 - 1) ^ (OP2 - 1)  */
84
85
0
    op1_size = -op1_size;
86
87
    /* Possible optimization: Decrease mpn_sub precision,
88
       as we won't use the entire res of both.  */
89
0
    TMP_ALLOC_LIMBS_2 (opx, op1_size, opy, op2_size);
90
0
    mpn_sub_1 (opx, op1_ptr, op1_size, (mp_limb_t) 1);
91
0
    op1_ptr = opx;
92
93
0
    mpn_sub_1 (opy, PTR(op2), op2_size, (mp_limb_t) 1);
94
0
    op2_ptr = opy;
95
96
0
    res_ptr = MPZ_NEWALLOC (res, op2_size);
97
    /* Don't re-read OP1_PTR and OP2_PTR.  They point to temporary
98
       space--never to the space PTR(res) used to point to before
99
       reallocation.  */
100
101
0
    MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size,
102
0
        op2_size - op1_size);
103
0
    mpn_xor_n (res_ptr, op1_ptr, op2_ptr, op1_size);
104
0
    TMP_FREE;
105
0
    res_size = op2_size;
106
107
0
    MPN_NORMALIZE (res_ptr, res_size);
108
0
    SIZ(res) = res_size;
109
0
  }
110
0
      else
111
0
  {
112
    /* Operand 2 negative, so will be the result.
113
       -(OP1 ^ (-OP2)) = -(OP1 ^ ~(OP2 - 1)) =
114
       = ~(OP1 ^ ~(OP2 - 1)) + 1 =
115
       = (OP1 ^ (OP2 - 1)) + 1      */
116
117
0
    res_size = MAX (op1_size, op2_size);
118
0
    res_ptr = MPZ_REALLOC (res, res_size + 1);
119
0
    op1_ptr = PTR(op1);
120
121
0
    opx = TMP_ALLOC_LIMBS (op2_size);
122
0
    mpn_sub_1 (opx, PTR(op2), op2_size, (mp_limb_t) 1);
123
0
    op2_ptr = opx;
124
125
0
    if (res_size == op1_size)
126
0
      {
127
0
        MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size, op1_size - op2_size);
128
0
        mpn_xor_n (res_ptr, op1_ptr, op2_ptr, op2_size);
129
0
      }
130
0
    else
131
0
      {
132
0
        MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size, op2_size - op1_size);
133
0
        if (LIKELY (op1_size != 0))
134
0
    mpn_xor_n (res_ptr, op1_ptr, op2_ptr, op1_size);
135
0
      }
136
0
    TMP_FREE;
137
138
0
    res_ptr[res_size] = 0;
139
0
    MPN_INCR_U (res_ptr, res_size + 1, (mp_limb_t) 1);
140
0
    res_size += res_ptr[res_size];
141
142
0
    MPN_NORMALIZE_NOT_ZERO (res_ptr, res_size);
143
0
    SIZ(res) = -res_size;
144
0
  }
145
0
    }
146
33
}