1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/* mpz_ior -- Logical inclusive or.
Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include "gmp.h"
#include "gmp-impl.h"
void
#if __STDC__
mpz_ior (mpz_ptr res, mpz_srcptr op1, mpz_srcptr op2)
#else
mpz_ior (res, op1, op2)
mpz_ptr res;
mpz_srcptr op1;
mpz_srcptr op2;
#endif
{
mp_srcptr op1_ptr, op2_ptr;
mp_size_t op1_size, op2_size;
mp_ptr res_ptr;
mp_size_t res_size;
mp_size_t i;
TMP_DECL (marker);
TMP_MARK (marker);
op1_size = op1->_mp_size;
op2_size = op2->_mp_size;
op1_ptr = op1->_mp_d;
op2_ptr = op2->_mp_d;
res_ptr = res->_mp_d;
if (op1_size >= 0)
{
if (op2_size >= 0)
{
if (op1_size >= op2_size)
{
if (res->_mp_alloc < op1_size)
{
_mpz_realloc (res, op1_size);
op1_ptr = op1->_mp_d;
op2_ptr = op2->_mp_d;
res_ptr = res->_mp_d;
}
if (res_ptr != op1_ptr)
MPN_COPY (res_ptr + op2_size, op1_ptr + op2_size,
op1_size - op2_size);
for (i = op2_size - 1; i >= 0; i--)
res_ptr[i] = op1_ptr[i] | op2_ptr[i];
res_size = op1_size;
}
else
{
if (res->_mp_alloc < op2_size)
{
_mpz_realloc (res, op2_size);
op1_ptr = op1->_mp_d;
op2_ptr = op2->_mp_d;
res_ptr = res->_mp_d;
}
if (res_ptr != op2_ptr)
MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size,
op2_size - op1_size);
for (i = op1_size - 1; i >= 0; i--)
res_ptr[i] = op1_ptr[i] | op2_ptr[i];
res_size = op2_size;
}
res->_mp_size = res_size;
return;
}
else /* op2_size < 0 */
{
/* Fall through to the code at the end of the function. */
}
}
else
{
if (op2_size < 0)
{
mp_ptr opx;
mp_limb_t cy;
/* Both operands are negative, so will be the result.
-((-OP1) | (-OP2)) = -(~(OP1 - 1) | ~(OP2 - 1)) =
= ~(~(OP1 - 1) | ~(OP2 - 1)) + 1 =
= ((OP1 - 1) & (OP2 - 1)) + 1 */
op1_size = -op1_size;
op2_size = -op2_size;
res_size = MIN (op1_size, op2_size);
/* Possible optimization: Decrease mpn_sub precision,
as we won't use the entire res of both. */
opx = (mp_ptr) TMP_ALLOC (res_size * BYTES_PER_MP_LIMB);
mpn_sub_1 (opx, op1_ptr, res_size, (mp_limb_t) 1);
op1_ptr = opx;
opx = (mp_ptr) TMP_ALLOC (res_size * BYTES_PER_MP_LIMB);
mpn_sub_1 (opx, op2_ptr, res_size, (mp_limb_t) 1);
op2_ptr = opx;
if (res->_mp_alloc < res_size)
{
_mpz_realloc (res, res_size);
res_ptr = res->_mp_d;
/* Don't re-read OP1_PTR and OP2_PTR. They point to
temporary space--never to the space RES->_mp_D used
to point to before reallocation. */
}
/* First loop finds the size of the result. */
for (i = res_size - 1; i >= 0; i--)
if ((op1_ptr[i] & op2_ptr[i]) != 0)
break;
res_size = i + 1;
if (res_size != 0)
{
/* Second loop computes the real result. */
for (i = res_size - 1; i >= 0; i--)
res_ptr[i] = op1_ptr[i] & op2_ptr[i];
cy = mpn_add_1 (res_ptr, res_ptr, res_size, (mp_limb_t) 1);
if (cy)
{
res_ptr[res_size] = cy;
res_size++;
}
}
else
{
res_ptr[0] = 1;
res_size = 1;
}
res->_mp_size = -res_size;
TMP_FREE (marker);
return;
}
else
{
/* We should compute -OP1 | OP2. Swap OP1 and OP2 and fall
through to the code that handles OP1 | -OP2. */
{mpz_srcptr t = op1; op1 = op2; op2 = t;}
{mp_srcptr t = op1_ptr; op1_ptr = op2_ptr; op2_ptr = t;}
{mp_size_t t = op1_size; op1_size = op2_size; op2_size = t;}
}
}
{
mp_ptr opx;
mp_limb_t cy;
mp_size_t res_alloc;
mp_size_t count;
/* Operand 2 negative, so will be the result.
-(OP1 | (-OP2)) = -(OP1 | ~(OP2 - 1)) =
= ~(OP1 | ~(OP2 - 1)) + 1 =
= (~OP1 & (OP2 - 1)) + 1 */
op2_size = -op2_size;
res_alloc = op2_size;
opx = (mp_ptr) TMP_ALLOC (op2_size * BYTES_PER_MP_LIMB);
mpn_sub_1 (opx, op2_ptr, op2_size, (mp_limb_t) 1);
op2_ptr = opx;
if (res->_mp_alloc < res_alloc)
{
_mpz_realloc (res, res_alloc);
op1_ptr = op1->_mp_d;
res_ptr = res->_mp_d;
/* Don't re-read OP2_PTR. It points to temporary space--never
to the space RES->_mp_D used to point to before reallocation. */
}
if (op1_size >= op2_size)
{
/* We can just ignore the part of OP1 that stretches above OP2,
because the result limbs are zero there. */
/* First loop finds the size of the result. */
for (i = op2_size - 1; i >= 0; i--)
if ((~op1_ptr[i] & op2_ptr[i]) != 0)
break;
res_size = i + 1;
count = res_size;
}
else
{
res_size = op2_size;
/* Copy the part of OP2 that stretches above OP1, to RES. */
MPN_COPY (res_ptr + op1_size, op2_ptr + op1_size, op2_size - op1_size);
count = op1_size;
}
if (res_size != 0)
{
/* Second loop computes the real result. */
for (i = count - 1; i >= 0; i--)
res_ptr[i] = ~op1_ptr[i] & op2_ptr[i];
cy = mpn_add_1 (res_ptr, res_ptr, res_size, (mp_limb_t) 1);
if (cy)
{
res_ptr[res_size] = cy;
res_size++;
}
}
else
{
res_ptr[0] = 1;
res_size = 1;
}
res->_mp_size = -res_size;
}
TMP_FREE (marker);
}
|