1 /* mpi-bit.c - MPI bit level functions
2 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include "mpi-internal.h"
24 #define A_LIMB_1 ((mpi_limb_t) 1)
27 * Sometimes we have MSL (most significant limbs) which are 0;
28 * this is for some reasons not good, so this function removes them.
30 void mpi_normalize(MPI a)
32 for (; a->nlimbs && !a->d[a->nlimbs - 1]; a->nlimbs--)
35 EXPORT_SYMBOL_GPL(mpi_normalize);
38 * Return the number of bits in A.
40 unsigned mpi_get_nbits(MPI a)
47 mpi_limb_t alimb = a->d[a->nlimbs - 1];
49 n = count_leading_zeros(alimb);
51 n = BITS_PER_MPI_LIMB;
52 n = BITS_PER_MPI_LIMB - n + (a->nlimbs - 1) * BITS_PER_MPI_LIMB;
57 EXPORT_SYMBOL_GPL(mpi_get_nbits);
60 * Test whether bit N is set.
62 int mpi_test_bit(MPI a, unsigned int n)
64 unsigned int limbno, bitno;
67 limbno = n / BITS_PER_MPI_LIMB;
68 bitno = n % BITS_PER_MPI_LIMB;
70 if (limbno >= a->nlimbs)
71 return 0; /* too far left: this is a 0 */
73 return (limb & (A_LIMB_1 << bitno)) ? 1 : 0;
75 EXPORT_SYMBOL_GPL(mpi_test_bit);
80 void mpi_set_bit(MPI a, unsigned int n)
82 unsigned int i, limbno, bitno;
84 limbno = n / BITS_PER_MPI_LIMB;
85 bitno = n % BITS_PER_MPI_LIMB;
87 if (limbno >= a->nlimbs) {
88 for (i = a->nlimbs; i < a->alloced; i++)
90 mpi_resize(a, limbno+1);
93 a->d[limbno] |= (A_LIMB_1<<bitno);
97 * Set bit N of A. and clear all bits above
99 void mpi_set_highbit(MPI a, unsigned int n)
101 unsigned int i, limbno, bitno;
103 limbno = n / BITS_PER_MPI_LIMB;
104 bitno = n % BITS_PER_MPI_LIMB;
106 if (limbno >= a->nlimbs) {
107 for (i = a->nlimbs; i < a->alloced; i++)
109 mpi_resize(a, limbno+1);
110 a->nlimbs = limbno+1;
112 a->d[limbno] |= (A_LIMB_1<<bitno);
113 for (bitno++; bitno < BITS_PER_MPI_LIMB; bitno++)
114 a->d[limbno] &= ~(A_LIMB_1 << bitno);
115 a->nlimbs = limbno+1;
117 EXPORT_SYMBOL_GPL(mpi_set_highbit);
120 * clear bit N of A and all bits above
122 void mpi_clear_highbit(MPI a, unsigned int n)
124 unsigned int limbno, bitno;
126 limbno = n / BITS_PER_MPI_LIMB;
127 bitno = n % BITS_PER_MPI_LIMB;
129 if (limbno >= a->nlimbs)
130 return; /* not allocated, therefore no need to clear bits :-) */
132 for ( ; bitno < BITS_PER_MPI_LIMB; bitno++)
133 a->d[limbno] &= ~(A_LIMB_1 << bitno);
134 a->nlimbs = limbno+1;
140 void mpi_clear_bit(MPI a, unsigned int n)
142 unsigned int limbno, bitno;
144 limbno = n / BITS_PER_MPI_LIMB;
145 bitno = n % BITS_PER_MPI_LIMB;
147 if (limbno >= a->nlimbs)
148 return; /* Don't need to clear this bit, it's far too left. */
149 a->d[limbno] &= ~(A_LIMB_1 << bitno);
151 EXPORT_SYMBOL_GPL(mpi_clear_bit);
155 * Shift A by COUNT limbs to the right
156 * This is used only within the MPI library
158 void mpi_rshift_limbs(MPI a, unsigned int count)
161 mpi_size_t n = a->nlimbs;
169 for (i = 0; i < n - count; i++)
176 * Shift A by N bits to the right.
178 void mpi_rshift(MPI x, MPI a, unsigned int n)
182 unsigned int nlimbs = (n/BITS_PER_MPI_LIMB);
183 unsigned int nbits = (n%BITS_PER_MPI_LIMB);
186 /* In-place operation. */
187 if (nlimbs >= x->nlimbs) {
193 for (i = 0; i < x->nlimbs - nlimbs; i++)
194 x->d[i] = x->d[i+nlimbs];
198 if (x->nlimbs && nbits)
199 mpihelp_rshift(x->d, x->d, x->nlimbs, nbits);
201 /* Copy and shift by more or equal bits than in a limb. */
204 RESIZE_IF_NEEDED(x, xsize);
206 for (i = 0; i < a->nlimbs; i++)
210 if (nlimbs >= x->nlimbs) {
216 for (i = 0; i < x->nlimbs - nlimbs; i++)
217 x->d[i] = x->d[i+nlimbs];
222 if (x->nlimbs && nbits)
223 mpihelp_rshift(x->d, x->d, x->nlimbs, nbits);
225 /* Copy and shift by less than bits in a limb. */
228 RESIZE_IF_NEEDED(x, xsize);
233 mpihelp_rshift(x->d, a->d, x->nlimbs, nbits);
235 /* The rshift helper function is not specified for
236 * NBITS==0, thus we do a plain copy here.
238 for (i = 0; i < x->nlimbs; i++)
243 MPN_NORMALIZE(x->d, x->nlimbs);
245 EXPORT_SYMBOL_GPL(mpi_rshift);
248 * Shift A by COUNT limbs to the left
249 * This is used only within the MPI library
251 void mpi_lshift_limbs(MPI a, unsigned int count)
260 RESIZE_IF_NEEDED(a, n+count);
263 for (i = n-1; i >= 0; i--)
265 for (i = 0; i < count; i++)
271 * Shift A by N bits to the left.
273 void mpi_lshift(MPI x, MPI a, unsigned int n)
275 unsigned int nlimbs = (n/BITS_PER_MPI_LIMB);
276 unsigned int nbits = (n%BITS_PER_MPI_LIMB);
279 return; /* In-place shift with an amount of zero. */
283 unsigned int alimbs = a->nlimbs;
287 RESIZE_IF_NEEDED(x, alimbs+nlimbs+1);
290 MPN_COPY(xp, ap, alimbs);
296 if (nlimbs && !nbits) {
297 /* Shift a full number of limbs. */
298 mpi_lshift_limbs(x, nlimbs);
300 /* We use a very dump approach: Shift left by the number of
301 * limbs plus one and than fix it up by an rshift.
303 mpi_lshift_limbs(x, nlimbs+1);
304 mpi_rshift(x, x, BITS_PER_MPI_LIMB - nbits);
307 MPN_NORMALIZE(x->d, x->nlimbs);