4 * Bitwise logical operations on MPI values
6 * ***** BEGIN LICENSE BLOCK *****
7 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
21 * The Initial Developer of the Original Code is
22 * Michael J. Fromberger.
23 * Portions created by the Initial Developer are Copyright (C) 1998
24 * the Initial Developer. All Rights Reserved.
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
41 /* $Id: mplogic.c,v 1.15 2004/04/27 23:04:36 gerv%gerv.net Exp $ */
46 /* {{{ Lookup table for population count */
48 static unsigned char bitc
[] = {
49 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
50 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
51 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
52 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
53 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
54 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
55 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
56 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
57 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
58 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
59 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
60 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
61 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
62 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
63 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
64 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
69 /*------------------------------------------------------------------------*/
71 mpl_not(a, b) - compute b = ~a
72 mpl_and(a, b, c) - compute c = a & b
73 mpl_or(a, b, c) - compute c = a | b
74 mpl_xor(a, b, c) - compute c = a ^ b
77 /* {{{ mpl_not(a, b) */
79 mp_err
mpl_not(mp_int
*a
, mp_int
*b
)
84 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
86 if((res
= mp_copy(a
, b
)) != MP_OKAY
)
89 /* This relies on the fact that the digit type is unsigned */
90 for(ix
= 0; ix
< USED(b
); ix
++)
91 DIGIT(b
, ix
) = ~DIGIT(b
, ix
);
101 /* {{{ mpl_and(a, b, c) */
103 mp_err
mpl_and(mp_int
*a
, mp_int
*b
, mp_int
*c
)
105 mp_int
*which
, *other
;
109 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
111 if(USED(a
) <= USED(b
)) {
119 if((res
= mp_copy(which
, c
)) != MP_OKAY
)
122 for(ix
= 0; ix
< USED(which
); ix
++)
123 DIGIT(c
, ix
) &= DIGIT(other
, ix
);
129 } /* end mpl_and() */
133 /* {{{ mpl_or(a, b, c) */
135 mp_err
mpl_or(mp_int
*a
, mp_int
*b
, mp_int
*c
)
137 mp_int
*which
, *other
;
141 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
143 if(USED(a
) >= USED(b
)) {
151 if((res
= mp_copy(which
, c
)) != MP_OKAY
)
154 for(ix
= 0; ix
< USED(which
); ix
++)
155 DIGIT(c
, ix
) |= DIGIT(other
, ix
);
163 /* {{{ mpl_xor(a, b, c) */
165 mp_err
mpl_xor(mp_int
*a
, mp_int
*b
, mp_int
*c
)
167 mp_int
*which
, *other
;
171 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
173 if(USED(a
) >= USED(b
)) {
181 if((res
= mp_copy(which
, c
)) != MP_OKAY
)
184 for(ix
= 0; ix
< USED(which
); ix
++)
185 DIGIT(c
, ix
) ^= DIGIT(other
, ix
);
191 } /* end mpl_xor() */
195 /*------------------------------------------------------------------------*/
197 mpl_rsh(a, b, d) - b = a >> d
198 mpl_lsh(a, b, d) - b = a << d
201 /* {{{ mpl_rsh(a, b, d) */
203 mp_err
mpl_rsh(const mp_int
*a
, mp_int
*b
, mp_digit d
)
207 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
209 if((res
= mp_copy(a
, b
)) != MP_OKAY
)
216 } /* end mpl_rsh() */
220 /* {{{ mpl_lsh(a, b, d) */
222 mp_err
mpl_lsh(const mp_int
*a
, mp_int
*b
, mp_digit d
)
226 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
228 if((res
= mp_copy(a
, b
)) != MP_OKAY
)
231 return s_mp_mul_2d(b
, d
);
233 } /* end mpl_lsh() */
237 /*------------------------------------------------------------------------*/
241 Count the number of set bits in the binary representation of a.
242 Returns MP_OKAY and sets 'num' to be the number of such bits, if
243 possible. If num is NULL, the result is thrown away, but it is
244 not considered an error.
246 mpl_num_clear() does basically the same thing for clear bits.
249 /* {{{ mpl_num_set(a, num) */
251 mp_err
mpl_num_set(mp_int
*a
, int *num
)
258 ARGCHK(a
!= NULL
, MP_BADARG
);
260 for(ix
= 0; ix
< USED(a
); ix
++) {
263 for(db
= 0; db
< sizeof(mp_digit
); db
++) {
264 reg
= (unsigned char)(cur
>> (CHAR_BIT
* db
));
275 } /* end mpl_num_set() */
279 /* {{{ mpl_num_clear(a, num) */
281 mp_err
mpl_num_clear(mp_int
*a
, int *num
)
288 ARGCHK(a
!= NULL
, MP_BADARG
);
290 for(ix
= 0; ix
< USED(a
); ix
++) {
293 for(db
= 0; db
< sizeof(mp_digit
); db
++) {
294 reg
= (unsigned char)(cur
>> (CHAR_BIT
* db
));
296 nset
+= bitc
[UCHAR_MAX
- reg
];
306 } /* end mpl_num_clear() */
310 /*------------------------------------------------------------------------*/
314 Determines the bitwise parity of the value given. Returns MP_EVEN
315 if an even number of digits are set, MP_ODD if an odd number are
319 /* {{{ mpl_parity(a) */
321 mp_err
mpl_parity(mp_int
*a
)
327 ARGCHK(a
!= NULL
, MP_BADARG
);
329 for(ix
= 0; ix
< USED(a
); ix
++) {
330 int shft
= (sizeof(mp_digit
) * CHAR_BIT
) / 2;
334 /* Compute parity for current digit */
336 cur
^= (cur
>> shft
);
341 /* XOR with running parity so far */
350 } /* end mpl_parity() */
357 Returns MP_OKAY or some error code.
358 Grows a if needed to set a bit to 1.
360 mp_err
mpl_set_bit(mp_int
*a
, mp_size bitNum
, mp_size value
)
366 ARGCHK(a
!= NULL
, MP_BADARG
);
368 ix
= bitNum
/ MP_DIGIT_BIT
;
369 if (ix
+ 1 > MP_USED(a
)) {
370 rv
= s_mp_pad(a
, ix
+ 1);
375 bitNum
= bitNum
% MP_DIGIT_BIT
;
376 mask
= (mp_digit
)1 << bitNum
;
378 MP_DIGIT(a
,ix
) |= mask
;
380 MP_DIGIT(a
,ix
) &= ~mask
;
388 returns 0 or 1 or some (negative) error code.
390 mp_err
mpl_get_bit(const mp_int
*a
, mp_size bitNum
)
395 ARGCHK(a
!= NULL
, MP_BADARG
);
397 ix
= bitNum
/ MP_DIGIT_BIT
;
398 ARGCHK(ix
<= MP_USED(a
) - 1, MP_RANGE
);
400 bit
= bitNum
% MP_DIGIT_BIT
;
401 rv
= (mp_err
)(MP_DIGIT(a
, ix
) >> bit
) & 1;
407 - Extracts numBits bits from a, where the least significant extracted bit
408 is bit lsbNum. Returns a negative value if error occurs.
409 - Because sign bit is used to indicate error, maximum number of bits to
410 be returned is the lesser of (a) the number of bits in an mp_digit, or
411 (b) one less than the number of bits in an mp_err.
412 - lsbNum + numbits can be greater than the number of significant bits in
413 integer a, as long as bit lsbNum is in the high order digit of a.
415 mp_err
mpl_get_bits(const mp_int
*a
, mp_size lsbNum
, mp_size numBits
)
417 mp_size rshift
= (lsbNum
% MP_DIGIT_BIT
);
418 mp_size lsWndx
= (lsbNum
/ MP_DIGIT_BIT
);
419 mp_digit
* digit
= MP_DIGITS(a
) + lsWndx
;
420 mp_digit mask
= ((1 << numBits
) - 1);
422 ARGCHK(numBits
< CHAR_BIT
* sizeof mask
, MP_BADARG
);
423 ARGCHK(MP_HOWMANY(lsbNum
, MP_DIGIT_BIT
) <= MP_USED(a
), MP_RANGE
);
425 if ((numBits
+ lsbNum
% MP_DIGIT_BIT
<= MP_DIGIT_BIT
) ||
426 (lsWndx
+ 1 >= MP_USED(a
))) {
427 mask
&= (digit
[0] >> rshift
);
429 mask
&= ((digit
[0] >> rshift
) | (digit
[1] << (MP_DIGIT_BIT
- rshift
)));
436 returns number of significnant bits in abs(a).
437 returns 1 if value is zero.
439 mp_err
mpl_significant_bits(const mp_int
*a
)
444 ARGCHK(a
!= NULL
, MP_BADARG
);
447 for (ix
= MP_USED(a
); ix
> 0; ) {
449 d
= MP_DIGIT(a
, --ix
);
458 bits
+= ix
* MP_DIGIT_BIT
;
464 /*------------------------------------------------------------------------*/
465 /* HERE THERE BE DRAGONS */