dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / common / mpi / mplogic.c
blob8daa7c0a08dc8d31360b867a1313b3dfbb20a706
1 /*
2 * mplogic.c
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
17 * License.
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.
26 * Contributor(s):
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 $ */
52 #include "mpi-priv.h"
53 #include "mplogic.h"
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
76 /* }}} */
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)
87 mp_err res;
89 ARGCHK(a != NULL && b != NULL, MP_BADARG);
91 if((res = mp_copy(a, b)) != MP_OKAY)
92 return res;
94 s_mp_div_2d(b, d);
96 return MP_OKAY;
98 } /* end mpl_rsh() */
100 /* }}} */
102 /* {{{ mpl_lsh(a, b, d) */
104 mp_err mpl_lsh(const mp_int *a, mp_int *b, mp_digit d)
106 mp_err res;
108 ARGCHK(a != NULL && b != NULL, MP_BADARG);
110 if((res = mp_copy(a, b)) != MP_OKAY)
111 return res;
113 return s_mp_mul_2d(b, d);
115 } /* end mpl_lsh() */
117 /* }}} */
119 /*------------------------------------------------------------------------*/
121 mpl_set_bit
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)
128 mp_size ix;
129 mp_err rv;
130 mp_digit mask;
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);
137 if (rv != MP_OKAY)
138 return rv;
141 bitNum = bitNum % MP_DIGIT_BIT;
142 mask = (mp_digit)1 << bitNum;
143 if (value)
144 MP_DIGIT(a,ix) |= mask;
145 else
146 MP_DIGIT(a,ix) &= ~mask;
147 s_mp_clamp(a);
148 return MP_OKAY;
152 mpl_get_bit
154 returns 0 or 1 or some (negative) error code.
156 mp_err mpl_get_bit(const mp_int *a, mp_size bitNum)
158 mp_size bit, ix;
159 mp_err rv;
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;
168 return rv;
172 mpl_get_bits
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);
194 } else {
195 mask &= ((digit[0] >> rshift) | (digit[1] << (MP_DIGIT_BIT - rshift)));
197 return (mp_err)mask;
201 mpl_significant_bits
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)
207 mp_err bits = 0;
208 int ix;
210 ARGCHK(a != NULL, MP_BADARG);
212 ix = MP_USED(a);
213 for (ix = MP_USED(a); ix > 0; ) {
214 mp_digit d;
215 d = MP_DIGIT(a, --ix);
216 if (d) {
217 while (d) {
218 ++bits;
219 d >>= 1;
221 break;
224 bits += ix * MP_DIGIT_BIT;
225 if (!bits)
226 bits = 1;
227 return bits;
230 /*------------------------------------------------------------------------*/
231 /* HERE THERE BE DRAGONS */