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--)
37 * Return the number of bits in A.
39 unsigned mpi_get_nbits(MPI a)
46 mpi_limb_t alimb = a->d[a->nlimbs - 1];
48 n = count_leading_zeros(alimb);
50 n = BITS_PER_MPI_LIMB;
51 n = BITS_PER_MPI_LIMB - n + (a->nlimbs - 1) * BITS_PER_MPI_LIMB;
56 EXPORT_SYMBOL_GPL(mpi_get_nbits);
59 * Test whether bit N is set.
61 int mpi_test_bit(MPI a, unsigned int n)
63 unsigned int limbno, bitno;
66 limbno = n / BITS_PER_MPI_LIMB;
67 bitno = n % BITS_PER_MPI_LIMB;
69 if (limbno >= a->nlimbs)
70 return 0; /* too far left: this is a 0 */
72 return (limb & (A_LIMB_1 << bitno)) ? 1 : 0;
74 EXPORT_SYMBOL_GPL(mpi_test_bit);
79 int mpi_set_bit(MPI a, unsigned int n)
81 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 err = mpi_resize(a, limbno+1);
95 a->d[limbno] |= (A_LIMB_1<<bitno);
98 EXPORT_SYMBOL_GPL(mpi_set_bit);
101 * Shift A by N bits to the right.
103 int mpi_rshift(MPI x, MPI a, unsigned int n)
107 unsigned int nlimbs = (n/BITS_PER_MPI_LIMB);
108 unsigned int nbits = (n%BITS_PER_MPI_LIMB);
112 /* In-place operation. */
113 if (nlimbs >= x->nlimbs) {
119 for (i = 0; i < x->nlimbs - nlimbs; i++)
120 x->d[i] = x->d[i+nlimbs];
124 if (x->nlimbs && nbits)
125 mpihelp_rshift(x->d, x->d, x->nlimbs, nbits);
127 /* Copy and shift by more or equal bits than in a limb. */
130 err = RESIZE_IF_NEEDED(x, xsize);
134 for (i = 0; i < a->nlimbs; i++)
138 if (nlimbs >= x->nlimbs) {
143 for (i = 0; i < x->nlimbs - nlimbs; i++)
144 x->d[i] = x->d[i+nlimbs];
148 if (x->nlimbs && nbits)
149 mpihelp_rshift(x->d, x->d, x->nlimbs, nbits);
151 /* Copy and shift by less than bits in a limb. */
154 err = RESIZE_IF_NEEDED(x, xsize);
161 mpihelp_rshift(x->d, a->d, x->nlimbs, nbits);
163 /* The rshift helper function is not specified for
164 * NBITS==0, thus we do a plain copy here.
166 for (i = 0; i < x->nlimbs; i++)
171 MPN_NORMALIZE(x->d, x->nlimbs);
175 EXPORT_SYMBOL_GPL(mpi_rshift);