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