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
mpz_xor(mpz_ptr res,mpz_srcptr op1,mpz_srcptr op2)35 mpz_xor (mpz_ptr res, mpz_srcptr op1, mpz_srcptr op2)
36 {
37 mp_srcptr op1_ptr, op2_ptr;
38 mp_size_t op1_size, op2_size;
39 mp_ptr res_ptr;
40 mp_size_t res_size;
41
42 op1_size = SIZ(op1);
43 op2_size = SIZ(op2);
44
45 if (op1_size < op2_size)
46 {
47 MPZ_SRCPTR_SWAP (op1, op2);
48 MP_SIZE_T_SWAP (op1_size, op2_size);
49 }
50
51 op1_ptr = PTR(op1);
52 res_ptr = PTR(res);
53
54 if (op2_size >= 0)
55 {
56 if (res_ptr != op1_ptr)
57 {
58 res_ptr = MPZ_REALLOC (res, op1_size);
59 MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size,
60 op1_size - op2_size);
61 }
62 if (LIKELY (op2_size != 0))
63 mpn_xor_n (res_ptr, op1_ptr, PTR(op2), op2_size);
64 res_size = op1_size;
65
66 MPN_NORMALIZE (res_ptr, res_size);
67 SIZ(res) = res_size;
68 }
69 else
70 {
71 mp_ptr opx;
72 TMP_DECL;
73
74 op2_size = -op2_size;
75 TMP_MARK;
76 if (op1_size < 0)
77 {
78 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 op1_size = -op1_size;
86
87 /* Possible optimization: Decrease mpn_sub precision,
88 as we won't use the entire res of both. */
89 TMP_ALLOC_LIMBS_2 (opx, op1_size, opy, op2_size);
90 mpn_sub_1 (opx, op1_ptr, op1_size, (mp_limb_t) 1);
91 op1_ptr = opx;
92
93 mpn_sub_1 (opy, PTR(op2), op2_size, (mp_limb_t) 1);
94 op2_ptr = opy;
95
96 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 MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size,
102 op2_size - op1_size);
103 mpn_xor_n (res_ptr, op1_ptr, op2_ptr, op1_size);
104 TMP_FREE;
105 res_size = op2_size;
106
107 MPN_NORMALIZE (res_ptr, res_size);
108 SIZ(res) = res_size;
109 }
110 else
111 {
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 res_size = MAX (op1_size, op2_size);
118 res_ptr = MPZ_REALLOC (res, res_size + 1);
119 op1_ptr = PTR(op1);
120
121 opx = TMP_ALLOC_LIMBS (op2_size);
122 mpn_sub_1 (opx, PTR(op2), op2_size, (mp_limb_t) 1);
123 op2_ptr = opx;
124
125 if (res_size == op1_size)
126 {
127 MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size, op1_size - op2_size);
128 mpn_xor_n (res_ptr, op1_ptr, op2_ptr, op2_size);
129 }
130 else
131 {
132 MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size, op2_size - op1_size);
133 if (LIKELY (op1_size != 0))
134 mpn_xor_n (res_ptr, op1_ptr, op2_ptr, op1_size);
135 }
136 TMP_FREE;
137
138 res_ptr[res_size] = 0;
139 MPN_INCR_U (res_ptr, res_size + 1, (mp_limb_t) 1);
140 res_size += res_ptr[res_size];
141
142 MPN_NORMALIZE_NOT_ZERO (res_ptr, res_size);
143 SIZ(res) = -res_size;
144 }
145 }
146 }
147