1
/* This file was automatically imported with
2
import_gcry.py. Please don't modify it */
3
/* mpi-bit.c - MPI bit level functions
4
* Copyright (C) 1998, 1999, 2001, 2002, 2006 Free Software Foundation, Inc.
6
* This file is part of Libgcrypt.
8
* Libgcrypt is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU Lesser General Public License as
10
* published by the Free Software Foundation; either version 2.1 of
11
* the License, or (at your option) any later version.
13
* Libgcrypt is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26
#include "mpi-internal.h"
30
#ifdef MPI_INTERNAL_NEED_CLZ_TAB
37
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
38
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
39
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
40
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
41
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
42
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
43
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
44
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
49
#define A_LIMB_1 ((mpi_limb_t)1)
53
* Sometimes we have MSL (most significant limbs) which are 0;
54
* this is for some reasons not good, so this function removes them.
57
_gcry_mpi_normalize( gcry_mpi_t a )
59
if( mpi_is_opaque(a) )
62
for( ; a->nlimbs && !a->d[a->nlimbs-1]; a->nlimbs-- )
69
* Return the number of bits in A.
72
gcry_mpi_get_nbits( gcry_mpi_t a )
76
if( mpi_is_opaque(a) ) {
77
return a->sign; /* which holds the number of bits */
80
_gcry_mpi_normalize( a );
82
mpi_limb_t alimb = a->d[a->nlimbs-1];
84
count_leading_zeros( n, alimb );
86
n = BITS_PER_MPI_LIMB;
87
n = BITS_PER_MPI_LIMB - n + (a->nlimbs-1) * BITS_PER_MPI_LIMB;
96
* Test whether bit N is set.
99
gcry_mpi_test_bit( gcry_mpi_t a, unsigned int n )
101
unsigned int limbno, bitno;
104
limbno = n / BITS_PER_MPI_LIMB;
105
bitno = n % BITS_PER_MPI_LIMB;
107
if( limbno >= a->nlimbs )
108
return 0; /* too far left: this is a 0 */
110
return (limb & (A_LIMB_1 << bitno))? 1: 0;
118
gcry_mpi_set_bit( gcry_mpi_t a, unsigned int n )
120
unsigned int limbno, bitno;
122
limbno = n / BITS_PER_MPI_LIMB;
123
bitno = n % BITS_PER_MPI_LIMB;
125
if ( limbno >= a->nlimbs )
127
mpi_resize (a, limbno+1 );
128
a->nlimbs = limbno+1;
130
a->d[limbno] |= (A_LIMB_1<<bitno);
134
* Set bit N of A. and clear all bits above
137
gcry_mpi_set_highbit( gcry_mpi_t a, unsigned int n )
139
unsigned int limbno, bitno;
141
limbno = n / BITS_PER_MPI_LIMB;
142
bitno = n % BITS_PER_MPI_LIMB;
144
if ( limbno >= a->nlimbs )
146
mpi_resize (a, limbno+1 );
147
a->nlimbs = limbno+1;
149
a->d[limbno] |= (A_LIMB_1<<bitno);
150
for ( bitno++; bitno < BITS_PER_MPI_LIMB; bitno++ )
151
a->d[limbno] &= ~(A_LIMB_1 << bitno);
152
a->nlimbs = limbno+1;
156
* clear bit N of A and all bits above
159
gcry_mpi_clear_highbit( gcry_mpi_t a, unsigned int n )
161
unsigned int limbno, bitno;
163
limbno = n / BITS_PER_MPI_LIMB;
164
bitno = n % BITS_PER_MPI_LIMB;
166
if( limbno >= a->nlimbs )
167
return; /* not allocated, therefore no need to clear bits
170
for( ; bitno < BITS_PER_MPI_LIMB; bitno++ )
171
a->d[limbno] &= ~(A_LIMB_1 << bitno);
172
a->nlimbs = limbno+1;
179
gcry_mpi_clear_bit( gcry_mpi_t a, unsigned int n )
181
unsigned int limbno, bitno;
183
limbno = n / BITS_PER_MPI_LIMB;
184
bitno = n % BITS_PER_MPI_LIMB;
186
if( limbno >= a->nlimbs )
187
return; /* don't need to clear this bit, it's to far to left */
188
a->d[limbno] &= ~(A_LIMB_1 << bitno);
193
* Shift A by COUNT limbs to the right
194
* This is used only within the MPI library
197
_gcry_mpi_rshift_limbs( gcry_mpi_t a, unsigned int count )
200
mpi_size_t n = a->nlimbs;
208
for( i = 0; i < n - count; i++ )
216
* Shift A by N bits to the right.
219
gcry_mpi_rshift ( gcry_mpi_t x, gcry_mpi_t a, unsigned int n )
223
unsigned int nlimbs = (n/BITS_PER_MPI_LIMB);
224
unsigned int nbits = (n%BITS_PER_MPI_LIMB);
228
/* In-place operation. */
229
if ( nlimbs >= x->nlimbs )
237
for (i=0; i < x->nlimbs - nlimbs; i++ )
238
x->d[i] = x->d[i+nlimbs];
243
if ( x->nlimbs && nbits )
244
_gcry_mpih_rshift ( x->d, x->d, x->nlimbs, nbits );
248
/* Copy and shift by more or equal bits than in a limb. */
251
RESIZE_IF_NEEDED (x, xsize);
253
for (i=0; i < a->nlimbs; i++ )
257
if ( nlimbs >= x->nlimbs )
265
for (i=0; i < x->nlimbs - nlimbs; i++ )
266
x->d[i] = x->d[i+nlimbs];
271
if ( x->nlimbs && nbits )
272
_gcry_mpih_rshift ( x->d, x->d, x->nlimbs, nbits );
276
/* Copy and shift by less than bits in a limb. */
279
RESIZE_IF_NEEDED (x, xsize);
285
_gcry_mpih_rshift (x->d, a->d, x->nlimbs, nbits );
288
/* The rshift helper function is not specified for
289
NBITS==0, thus we do a plain copy here. */
290
for (i=0; i < x->nlimbs; i++ )
295
MPN_NORMALIZE (x->d, x->nlimbs);
300
* Shift A by COUNT limbs to the left
301
* This is used only within the MPI library
304
_gcry_mpi_lshift_limbs (gcry_mpi_t a, unsigned int count)
313
RESIZE_IF_NEEDED (a, n+count);
316
for (i = n-1; i >= 0; i--)
318
for (i=0; i < count; i++ )
325
* Shift A by N bits to the left.
328
gcry_mpi_lshift ( gcry_mpi_t x, gcry_mpi_t a, unsigned int n )
330
unsigned int nlimbs = (n/BITS_PER_MPI_LIMB);
331
unsigned int nbits = (n%BITS_PER_MPI_LIMB);
334
return; /* In-place shift with an amount of zero. */
339
unsigned int alimbs = a->nlimbs;
343
RESIZE_IF_NEEDED (x, alimbs+nlimbs+1);
346
MPN_COPY (xp, ap, alimbs);
352
if (nlimbs && !nbits)
354
/* Shift a full number of limbs. */
355
_gcry_mpi_lshift_limbs (x, nlimbs);
359
/* We use a very dump approach: Shift left by the number of
360
limbs plus one and than fix it up by an rshift. */
361
_gcry_mpi_lshift_limbs (x, nlimbs+1);
362
gcry_mpi_rshift (x, x, BITS_PER_MPI_LIMB - nbits);
365
MPN_NORMALIZE (x->d, x->nlimbs);