xref: /onnv-gate/usr/src/common/mpi/mplogic.c (revision 5697:324be5104707)
1*5697Smcpowers /*
2*5697Smcpowers  *  mplogic.c
3*5697Smcpowers  *
4*5697Smcpowers  *  Bitwise logical operations on MPI values
5*5697Smcpowers  *
6*5697Smcpowers  * ***** BEGIN LICENSE BLOCK *****
7*5697Smcpowers  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
8*5697Smcpowers  *
9*5697Smcpowers  * The contents of this file are subject to the Mozilla Public License Version
10*5697Smcpowers  * 1.1 (the "License"); you may not use this file except in compliance with
11*5697Smcpowers  * the License. You may obtain a copy of the License at
12*5697Smcpowers  * http://www.mozilla.org/MPL/
13*5697Smcpowers  *
14*5697Smcpowers  * Software distributed under the License is distributed on an "AS IS" basis,
15*5697Smcpowers  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16*5697Smcpowers  * for the specific language governing rights and limitations under the
17*5697Smcpowers  * License.
18*5697Smcpowers  *
19*5697Smcpowers  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
20*5697Smcpowers  *
21*5697Smcpowers  * The Initial Developer of the Original Code is
22*5697Smcpowers  * Michael J. Fromberger.
23*5697Smcpowers  * Portions created by the Initial Developer are Copyright (C) 1998
24*5697Smcpowers  * the Initial Developer. All Rights Reserved.
25*5697Smcpowers  *
26*5697Smcpowers  * Contributor(s):
27*5697Smcpowers  *
28*5697Smcpowers  * Alternatively, the contents of this file may be used under the terms of
29*5697Smcpowers  * either the GNU General Public License Version 2 or later (the "GPL"), or
30*5697Smcpowers  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31*5697Smcpowers  * in which case the provisions of the GPL or the LGPL are applicable instead
32*5697Smcpowers  * of those above. If you wish to allow use of your version of this file only
33*5697Smcpowers  * under the terms of either the GPL or the LGPL, and not to allow others to
34*5697Smcpowers  * use your version of this file under the terms of the MPL, indicate your
35*5697Smcpowers  * decision by deleting the provisions above and replace them with the notice
36*5697Smcpowers  * and other provisions required by the GPL or the LGPL. If you do not delete
37*5697Smcpowers  * the provisions above, a recipient may use your version of this file under
38*5697Smcpowers  * the terms of any one of the MPL, the GPL or the LGPL.
39*5697Smcpowers  *
40*5697Smcpowers  * ***** END LICENSE BLOCK ***** */
41*5697Smcpowers /*
42*5697Smcpowers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
43*5697Smcpowers  * Use is subject to license terms.
44*5697Smcpowers  *
45*5697Smcpowers  * Sun elects to use this software under the MPL license.
46*5697Smcpowers  */
47*5697Smcpowers 
48*5697Smcpowers #pragma ident	"%Z%%M%	%I%	%E% SMI"
49*5697Smcpowers 
50*5697Smcpowers /* $Id: mplogic.c,v 1.15 2004/04/27 23:04:36 gerv%gerv.net Exp $ */
51*5697Smcpowers 
52*5697Smcpowers #include "mpi-priv.h"
53*5697Smcpowers #include "mplogic.h"
54*5697Smcpowers 
55*5697Smcpowers /* {{{ Lookup table for population count */
56*5697Smcpowers 
57*5697Smcpowers static unsigned char bitc[] = {
58*5697Smcpowers    0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
59*5697Smcpowers    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
60*5697Smcpowers    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
61*5697Smcpowers    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
62*5697Smcpowers    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
63*5697Smcpowers    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
64*5697Smcpowers    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
65*5697Smcpowers    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
66*5697Smcpowers    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
67*5697Smcpowers    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
68*5697Smcpowers    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
69*5697Smcpowers    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
70*5697Smcpowers    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
71*5697Smcpowers    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
72*5697Smcpowers    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
73*5697Smcpowers    4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
74*5697Smcpowers };
75*5697Smcpowers 
76*5697Smcpowers /* }}} */
77*5697Smcpowers 
78*5697Smcpowers /*
79*5697Smcpowers   mpl_rsh(a, b, d)     - b = a >> d
80*5697Smcpowers   mpl_lsh(a, b, d)     - b = a << d
81*5697Smcpowers  */
82*5697Smcpowers 
83*5697Smcpowers /* {{{ mpl_rsh(a, b, d) */
84*5697Smcpowers 
mpl_rsh(const mp_int * a,mp_int * b,mp_digit d)85*5697Smcpowers mp_err mpl_rsh(const mp_int *a, mp_int *b, mp_digit d)
86*5697Smcpowers {
87*5697Smcpowers   mp_err   res;
88*5697Smcpowers 
89*5697Smcpowers   ARGCHK(a != NULL && b != NULL, MP_BADARG);
90*5697Smcpowers 
91*5697Smcpowers   if((res = mp_copy(a, b)) != MP_OKAY)
92*5697Smcpowers     return res;
93*5697Smcpowers 
94*5697Smcpowers   s_mp_div_2d(b, d);
95*5697Smcpowers 
96*5697Smcpowers   return MP_OKAY;
97*5697Smcpowers 
98*5697Smcpowers } /* end mpl_rsh() */
99*5697Smcpowers 
100*5697Smcpowers /* }}} */
101*5697Smcpowers 
102*5697Smcpowers /* {{{ mpl_lsh(a, b, d) */
103*5697Smcpowers 
mpl_lsh(const mp_int * a,mp_int * b,mp_digit d)104*5697Smcpowers mp_err mpl_lsh(const mp_int *a, mp_int *b, mp_digit d)
105*5697Smcpowers {
106*5697Smcpowers   mp_err   res;
107*5697Smcpowers 
108*5697Smcpowers   ARGCHK(a != NULL && b != NULL, MP_BADARG);
109*5697Smcpowers 
110*5697Smcpowers   if((res = mp_copy(a, b)) != MP_OKAY)
111*5697Smcpowers     return res;
112*5697Smcpowers 
113*5697Smcpowers   return s_mp_mul_2d(b, d);
114*5697Smcpowers 
115*5697Smcpowers } /* end mpl_lsh() */
116*5697Smcpowers 
117*5697Smcpowers /* }}} */
118*5697Smcpowers 
119*5697Smcpowers /*------------------------------------------------------------------------*/
120*5697Smcpowers /*
121*5697Smcpowers   mpl_set_bit
122*5697Smcpowers 
123*5697Smcpowers   Returns MP_OKAY or some error code.
124*5697Smcpowers   Grows a if needed to set a bit to 1.
125*5697Smcpowers  */
mpl_set_bit(mp_int * a,mp_size bitNum,mp_size value)126*5697Smcpowers mp_err mpl_set_bit(mp_int *a, mp_size bitNum, mp_size value)
127*5697Smcpowers {
128*5697Smcpowers   mp_size      ix;
129*5697Smcpowers   mp_err       rv;
130*5697Smcpowers   mp_digit     mask;
131*5697Smcpowers 
132*5697Smcpowers   ARGCHK(a != NULL, MP_BADARG);
133*5697Smcpowers 
134*5697Smcpowers   ix = bitNum / MP_DIGIT_BIT;
135*5697Smcpowers   if (ix + 1 > MP_USED(a)) {
136*5697Smcpowers     rv = s_mp_pad(a, ix + 1);
137*5697Smcpowers     if (rv != MP_OKAY)
138*5697Smcpowers       return rv;
139*5697Smcpowers   }
140*5697Smcpowers 
141*5697Smcpowers   bitNum = bitNum % MP_DIGIT_BIT;
142*5697Smcpowers   mask = (mp_digit)1 << bitNum;
143*5697Smcpowers   if (value)
144*5697Smcpowers     MP_DIGIT(a,ix) |= mask;
145*5697Smcpowers   else
146*5697Smcpowers     MP_DIGIT(a,ix) &= ~mask;
147*5697Smcpowers   s_mp_clamp(a);
148*5697Smcpowers   return MP_OKAY;
149*5697Smcpowers }
150*5697Smcpowers 
151*5697Smcpowers /*
152*5697Smcpowers   mpl_get_bit
153*5697Smcpowers 
154*5697Smcpowers   returns 0 or 1 or some (negative) error code.
155*5697Smcpowers  */
mpl_get_bit(const mp_int * a,mp_size bitNum)156*5697Smcpowers mp_err mpl_get_bit(const mp_int *a, mp_size bitNum)
157*5697Smcpowers {
158*5697Smcpowers   mp_size      bit, ix;
159*5697Smcpowers   mp_err       rv;
160*5697Smcpowers 
161*5697Smcpowers   ARGCHK(a != NULL, MP_BADARG);
162*5697Smcpowers 
163*5697Smcpowers   ix = bitNum / MP_DIGIT_BIT;
164*5697Smcpowers   ARGCHK(ix <= MP_USED(a) - 1, MP_RANGE);
165*5697Smcpowers 
166*5697Smcpowers   bit   = bitNum % MP_DIGIT_BIT;
167*5697Smcpowers   rv = (mp_err)(MP_DIGIT(a, ix) >> bit) & 1;
168*5697Smcpowers   return rv;
169*5697Smcpowers }
170*5697Smcpowers 
171*5697Smcpowers /*
172*5697Smcpowers   mpl_get_bits
173*5697Smcpowers   - Extracts numBits bits from a, where the least significant extracted bit
174*5697Smcpowers   is bit lsbNum.  Returns a negative value if error occurs.
175*5697Smcpowers   - Because sign bit is used to indicate error, maximum number of bits to
176*5697Smcpowers   be returned is the lesser of (a) the number of bits in an mp_digit, or
177*5697Smcpowers   (b) one less than the number of bits in an mp_err.
178*5697Smcpowers   - lsbNum + numbits can be greater than the number of significant bits in
179*5697Smcpowers   integer a, as long as bit lsbNum is in the high order digit of a.
180*5697Smcpowers  */
mpl_get_bits(const mp_int * a,mp_size lsbNum,mp_size numBits)181*5697Smcpowers mp_err mpl_get_bits(const mp_int *a, mp_size lsbNum, mp_size numBits)
182*5697Smcpowers {
183*5697Smcpowers   mp_size    rshift = (lsbNum % MP_DIGIT_BIT);
184*5697Smcpowers   mp_size    lsWndx = (lsbNum / MP_DIGIT_BIT);
185*5697Smcpowers   mp_digit * digit  = MP_DIGITS(a) + lsWndx;
186*5697Smcpowers   mp_digit   mask   = ((1 << numBits) - 1);
187*5697Smcpowers 
188*5697Smcpowers   ARGCHK(numBits < CHAR_BIT * sizeof mask, MP_BADARG);
189*5697Smcpowers   ARGCHK(MP_HOWMANY(lsbNum, MP_DIGIT_BIT) <= MP_USED(a), MP_RANGE);
190*5697Smcpowers 
191*5697Smcpowers   if ((numBits + lsbNum % MP_DIGIT_BIT <= MP_DIGIT_BIT) ||
192*5697Smcpowers       (lsWndx + 1 >= MP_USED(a))) {
193*5697Smcpowers     mask &= (digit[0] >> rshift);
194*5697Smcpowers   } else {
195*5697Smcpowers     mask &= ((digit[0] >> rshift) | (digit[1] << (MP_DIGIT_BIT - rshift)));
196*5697Smcpowers   }
197*5697Smcpowers   return (mp_err)mask;
198*5697Smcpowers }
199*5697Smcpowers 
200*5697Smcpowers /*
201*5697Smcpowers   mpl_significant_bits
202*5697Smcpowers   returns number of significnant bits in abs(a).
203*5697Smcpowers   returns 1 if value is zero.
204*5697Smcpowers  */
mpl_significant_bits(const mp_int * a)205*5697Smcpowers mp_err mpl_significant_bits(const mp_int *a)
206*5697Smcpowers {
207*5697Smcpowers   mp_err bits 	= 0;
208*5697Smcpowers   int    ix;
209*5697Smcpowers 
210*5697Smcpowers   ARGCHK(a != NULL, MP_BADARG);
211*5697Smcpowers 
212*5697Smcpowers   ix = MP_USED(a);
213*5697Smcpowers   for (ix = MP_USED(a); ix > 0; ) {
214*5697Smcpowers     mp_digit d;
215*5697Smcpowers     d = MP_DIGIT(a, --ix);
216*5697Smcpowers     if (d) {
217*5697Smcpowers       while (d) {
218*5697Smcpowers 	++bits;
219*5697Smcpowers 	d >>= 1;
220*5697Smcpowers       }
221*5697Smcpowers       break;
222*5697Smcpowers     }
223*5697Smcpowers   }
224*5697Smcpowers   bits += ix * MP_DIGIT_BIT;
225*5697Smcpowers   if (!bits)
226*5697Smcpowers     bits = 1;
227*5697Smcpowers   return bits;
228*5697Smcpowers }
229*5697Smcpowers 
230*5697Smcpowers /*------------------------------------------------------------------------*/
231*5697Smcpowers /* HERE THERE BE DRAGONS                                                  */
232