1 /* mpz_xor -- Logical xor.
3 Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2005 Free Software
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
25 mpz_xor (mpz_ptr res, mpz_srcptr op1, mpz_srcptr op2)
27 mp_srcptr op1_ptr, op2_ptr;
28 mp_size_t op1_size, op2_size;
30 mp_size_t res_size, res_alloc;
46 if (op1_size >= op2_size)
48 if (ALLOC(res) < op1_size)
50 _mpz_realloc (res, op1_size);
51 /* No overlapping possible: op1_ptr = PTR(op1); */
56 if (res_ptr != op1_ptr)
57 MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size,
59 for (i = op2_size - 1; i >= 0; i--)
60 res_ptr[i] = op1_ptr[i] ^ op2_ptr[i];
65 if (ALLOC(res) < op2_size)
67 _mpz_realloc (res, op2_size);
69 /* No overlapping possible: op2_ptr = PTR(op2); */
73 if (res_ptr != op2_ptr)
74 MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size,
76 for (i = op1_size - 1; i >= 0; i--)
77 res_ptr[i] = op1_ptr[i] ^ op2_ptr[i];
81 MPN_NORMALIZE (res_ptr, res_size);
85 else /* op2_size < 0 */
87 /* Fall through to the code at the end of the function. */
96 /* Both operands are negative, the result will be positive.
98 = ~(OP1 - 1) ^ ~(OP2 - 1) =
99 = (OP1 - 1) ^ (OP2 - 1) */
101 op1_size = -op1_size;
102 op2_size = -op2_size;
104 /* Possible optimization: Decrease mpn_sub precision,
105 as we won't use the entire res of both. */
106 opx = TMP_ALLOC_LIMBS (op1_size);
107 mpn_sub_1 (opx, op1_ptr, op1_size, (mp_limb_t) 1);
110 opx = TMP_ALLOC_LIMBS (op2_size);
111 mpn_sub_1 (opx, op2_ptr, op2_size, (mp_limb_t) 1);
114 res_alloc = MAX (op1_size, op2_size);
115 if (ALLOC(res) < res_alloc)
117 _mpz_realloc (res, res_alloc);
119 /* op1_ptr and op2_ptr point to temporary space. */
122 if (op1_size > op2_size)
124 MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size,
125 op1_size - op2_size);
126 for (i = op2_size - 1; i >= 0; i--)
127 res_ptr[i] = op1_ptr[i] ^ op2_ptr[i];
132 MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size,
133 op2_size - op1_size);
134 for (i = op1_size - 1; i >= 0; i--)
135 res_ptr[i] = op1_ptr[i] ^ op2_ptr[i];
139 MPN_NORMALIZE (res_ptr, res_size);
146 /* We should compute -OP1 ^ OP2. Swap OP1 and OP2 and fall
147 through to the code that handles OP1 ^ -OP2. */
148 MPZ_SRCPTR_SWAP (op1, op2);
149 MPN_SRCPTR_SWAP (op1_ptr,op1_size, op2_ptr,op2_size);
157 /* Operand 2 negative, so will be the result.
158 -(OP1 ^ (-OP2)) = -(OP1 ^ ~(OP2 - 1)) =
159 = ~(OP1 ^ ~(OP2 - 1)) + 1 =
160 = (OP1 ^ (OP2 - 1)) + 1 */
162 op2_size = -op2_size;
164 opx = TMP_ALLOC_LIMBS (op2_size);
165 mpn_sub_1 (opx, op2_ptr, op2_size, (mp_limb_t) 1);
168 res_alloc = MAX (op1_size, op2_size) + 1;
169 if (ALLOC(res) < res_alloc)
171 _mpz_realloc (res, res_alloc);
173 /* op2_ptr points to temporary space. */
177 if (op1_size > op2_size)
179 MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size, op1_size - op2_size);
180 for (i = op2_size - 1; i >= 0; i--)
181 res_ptr[i] = op1_ptr[i] ^ op2_ptr[i];
186 MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size, op2_size - op1_size);
187 for (i = op1_size - 1; i >= 0; i--)
188 res_ptr[i] = op1_ptr[i] ^ op2_ptr[i];
192 cy = mpn_add_1 (res_ptr, res_ptr, res_size, (mp_limb_t) 1);
195 res_ptr[res_size] = cy;
199 MPN_NORMALIZE (res_ptr, res_size);
200 SIZ(res) = -res_size;