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 ***** */
42 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
43 * Use is subject to license terms.
45 * Sun elects to use this software under the MPL license.
48 #pragma ident "%Z%%M% %I% %E% SMI"
50 /* $Id: mplogic.c,v 1.15 2004/04/27 23:04:36 gerv%gerv.net Exp $ */
55 /* {{{ Lookup table for population count */
57 static unsigned char bitc
[] = {
58 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
59 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
60 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
61 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
62 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
63 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
64 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
65 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
66 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
67 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
68 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
69 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
70 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
71 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
72 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
73 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
79 mpl_rsh(a, b, d) - b = a >> d
80 mpl_lsh(a, b, d) - b = a << d
83 /* {{{ mpl_rsh(a, b, d) */
85 mp_err
mpl_rsh(const mp_int
*a
, mp_int
*b
, mp_digit d
)
89 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
91 if((res
= mp_copy(a
, b
)) != MP_OKAY
)
102 /* {{{ mpl_lsh(a, b, d) */
104 mp_err
mpl_lsh(const mp_int
*a
, mp_int
*b
, mp_digit d
)
108 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
110 if((res
= mp_copy(a
, b
)) != MP_OKAY
)
113 return s_mp_mul_2d(b
, d
);
115 } /* end mpl_lsh() */
119 /*------------------------------------------------------------------------*/
123 Returns MP_OKAY or some error code.
124 Grows a if needed to set a bit to 1.
126 mp_err
mpl_set_bit(mp_int
*a
, mp_size bitNum
, mp_size value
)
132 ARGCHK(a
!= NULL
, MP_BADARG
);
134 ix
= bitNum
/ MP_DIGIT_BIT
;
135 if (ix
+ 1 > MP_USED(a
)) {
136 rv
= s_mp_pad(a
, ix
+ 1);
141 bitNum
= bitNum
% MP_DIGIT_BIT
;
142 mask
= (mp_digit
)1 << bitNum
;
144 MP_DIGIT(a
,ix
) |= mask
;
146 MP_DIGIT(a
,ix
) &= ~mask
;
154 returns 0 or 1 or some (negative) error code.
156 mp_err
mpl_get_bit(const mp_int
*a
, mp_size bitNum
)
161 ARGCHK(a
!= NULL
, MP_BADARG
);
163 ix
= bitNum
/ MP_DIGIT_BIT
;
164 ARGCHK(ix
<= MP_USED(a
) - 1, MP_RANGE
);
166 bit
= bitNum
% MP_DIGIT_BIT
;
167 rv
= (mp_err
)(MP_DIGIT(a
, ix
) >> bit
) & 1;
173 - Extracts numBits bits from a, where the least significant extracted bit
174 is bit lsbNum. Returns a negative value if error occurs.
175 - Because sign bit is used to indicate error, maximum number of bits to
176 be returned is the lesser of (a) the number of bits in an mp_digit, or
177 (b) one less than the number of bits in an mp_err.
178 - lsbNum + numbits can be greater than the number of significant bits in
179 integer a, as long as bit lsbNum is in the high order digit of a.
181 mp_err
mpl_get_bits(const mp_int
*a
, mp_size lsbNum
, mp_size numBits
)
183 mp_size rshift
= (lsbNum
% MP_DIGIT_BIT
);
184 mp_size lsWndx
= (lsbNum
/ MP_DIGIT_BIT
);
185 mp_digit
* digit
= MP_DIGITS(a
) + lsWndx
;
186 mp_digit mask
= ((1 << numBits
) - 1);
188 ARGCHK(numBits
< CHAR_BIT
* sizeof mask
, MP_BADARG
);
189 ARGCHK(MP_HOWMANY(lsbNum
, MP_DIGIT_BIT
) <= MP_USED(a
), MP_RANGE
);
191 if ((numBits
+ lsbNum
% MP_DIGIT_BIT
<= MP_DIGIT_BIT
) ||
192 (lsWndx
+ 1 >= MP_USED(a
))) {
193 mask
&= (digit
[0] >> rshift
);
195 mask
&= ((digit
[0] >> rshift
) | (digit
[1] << (MP_DIGIT_BIT
- rshift
)));
202 returns number of significnant bits in abs(a).
203 returns 1 if value is zero.
205 mp_err
mpl_significant_bits(const mp_int
*a
)
210 ARGCHK(a
!= NULL
, MP_BADARG
);
213 for (ix
= MP_USED(a
); ix
> 0; ) {
215 d
= MP_DIGIT(a
, --ix
);
224 bits
+= ix
* MP_DIGIT_BIT
;
230 /*------------------------------------------------------------------------*/
231 /* HERE THERE BE DRAGONS */