1 /* mpz_ior -- Logical inclusive or.
3 Copyright (C) 1991 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 The GNU MP Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the GNU MP Library; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #define min(l,o) ((l) < (o) ? (l) : (o))
25 #define max(h,i) ((h) > (i) ? (h) : (i))
29 mpz_ior (MP_INT *res, const MP_INT *op1, const MP_INT *op2)
31 mpz_ior (res, op1, op2)
37 mp_srcptr op1_ptr, op2_ptr;
38 mp_size op1_size, op2_size;
54 if (op1_size >= op2_size)
56 if (res->alloc < op1_size)
58 _mpz_realloc (res, op1_size);
64 if (res_ptr != op1_ptr)
65 MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size,
67 for (i = op2_size - 1; i >= 0; i--)
68 res_ptr[i] = op1_ptr[i] | op2_ptr[i];
73 if (res->alloc < op2_size)
75 _mpz_realloc (res, op2_size);
81 if (res_ptr != op2_ptr)
82 MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size,
84 for (i = op1_size - 1; i >= 0; i--)
85 res_ptr[i] = op1_ptr[i] | op2_ptr[i];
92 else /* op2_size < 0 */
93 /* Fall through to the code at the end of the function. */
104 /* Both operands are negative, so will be the result.
105 -((-OP1) | (-OP2)) = -(~(OP1 - 1) | ~(OP2 - 1)) =
106 = ~(~(OP1 - 1) | ~(OP2 - 1)) + 1 =
107 = ((OP1 - 1) & (OP2 - 1)) + 1 */
109 op1_size = -op1_size;
110 op2_size = -op2_size;
112 res_size = min (op1_size, op2_size);
114 /* Possible optimization: Decrease mpn_sub precision,
115 as we won't use the entire res of both. */
116 opx = (mp_ptr) alloca (op1_size * BYTES_PER_MP_LIMB);
117 op1_size += mpn_sub (opx, op1_ptr, op1_size, &one, 1);
120 opx = (mp_ptr) alloca (op2_size * BYTES_PER_MP_LIMB);
121 op2_size += mpn_sub (opx, op2_ptr, op2_size, &one, 1);
124 if (res->alloc < res_size)
126 _mpz_realloc (res, res_size);
128 /* Don't re-read OP1_PTR and OP2_PTR. They point to
129 temporary space--never to the space RES->D used
130 to point to before reallocation. */
133 /* First loop finds the size of the result. */
134 for (i = res_size - 1; i >= 0; i--)
135 if ((op1_ptr[i] & op2_ptr[i]) != 0)
139 /* Second loop computes the real result. */
140 for (i = res_size - 1; i >= 0; i--)
141 res_ptr[i] = op1_ptr[i] & op2_ptr[i];
145 cy = mpn_add (res_ptr, res_ptr, res_size, &one, 1);
148 res_ptr[res_size] = cy;
158 res->size = -res_size;
163 /* We should compute -OP1 | OP2. Swap OP1 and OP2 and fall
164 through to the code that handles OP1 | -OP2. */
165 {const MP_INT *t = op1; op1 = op2; op2 = t;}
166 {mp_srcptr t = op1_ptr; op1_ptr = op2_ptr; op2_ptr = t;}
167 {mp_size t = op1_size; op1_size = op2_size; op2_size = t;}
177 /* Operand 2 negative, so will be the result.
178 -(OP1 | (-OP2)) = -(OP1 | ~(OP2 - 1)) =
179 = ~(OP1 | ~(OP2 - 1)) + 1 =
180 = (~OP1 & (OP2 - 1)) + 1 */
182 op2_size = -op2_size;
184 res_alloc = op2_size;
186 opx = (mp_ptr) alloca (op2_size * BYTES_PER_MP_LIMB);
187 op2_size += mpn_sub (opx, op2_ptr, op2_size, &one, 1);
190 if (res->alloc < res_alloc)
192 _mpz_realloc (res, res_alloc);
195 /* Don't re-read OP2_PTR. It points to temporary space--never
196 to the space RES->D used to point to before reallocation. */
199 if (op1_size >= op2_size)
201 /* We can just ignore the part of OP1 that stretches above OP2,
202 because the result limbs are zero there. */
204 /* First loop finds the size of the result. */
205 for (i = op2_size - 1; i >= 0; i--)
206 if ((~op1_ptr[i] & op2_ptr[i]) != 0)
214 /* Copy the part of OP2 that stretches above OP1, to RES. */
215 MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size,
216 op2_size - op1_size);
219 /* Second loop computes the real result. */
220 for (i = res_size - 1; i >= 0; i--)
221 res_ptr[i] = ~op1_ptr[i] & op2_ptr[i];
225 cy = mpn_add (res_ptr, res_ptr, res_size, &one, 1);
228 res_ptr[res_size] = cy;
238 res->size = -res_size;