1
/* mpi-bit.c - MPI bit level fucntions
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
25
#include "mpi-internal.h"
29
#ifdef MPI_INTERNAL_NEED_CLZ_TAB
36
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,
37
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,
38
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,
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
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,
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,
48
#define A_LIMB_1 ((mpi_limb_t)1)
52
* Sometimes we have MSL (most significant limbs) which are 0;
53
* this is for some reasons not good, so this function removes them.
56
mpi_normalize( MPI a )
58
if( mpi_is_opaque (a) )
61
for( ; a->nlimbs && !a->d[a->nlimbs-1]; a->nlimbs-- )
68
* Return the number of bits in A.
71
mpi_get_nbits( MPI a )
77
mpi_limb_t alimb = a->d[a->nlimbs-1];
79
count_leading_zeros( n, alimb );
81
n = BITS_PER_MPI_LIMB;
82
n = BITS_PER_MPI_LIMB - n + (a->nlimbs-1) * BITS_PER_MPI_LIMB;
91
* Test whether bit N is set.
94
mpi_test_bit( MPI a, unsigned n )
96
unsigned limbno, bitno;
99
limbno = n / BITS_PER_MPI_LIMB;
100
bitno = n % BITS_PER_MPI_LIMB;
102
if( limbno >= a->nlimbs )
103
return 0; /* too far left: this is a 0 */
105
return (limb & (A_LIMB_1 << bitno))? 1: 0;
113
mpi_set_bit( MPI a, unsigned n )
115
unsigned limbno, bitno;
117
limbno = n / BITS_PER_MPI_LIMB;
118
bitno = n % BITS_PER_MPI_LIMB;
120
if( limbno >= a->nlimbs ) { /* resize */
121
if( a->alloced >= limbno )
122
mpi_resize(a, limbno+1 );
123
a->nlimbs = limbno+1;
125
a->d[limbno] |= (A_LIMB_1<<bitno);
129
* Set bit N of A. and clear all bits above
132
mpi_set_highbit( MPI a, unsigned n )
134
unsigned limbno, bitno;
136
limbno = n / BITS_PER_MPI_LIMB;
137
bitno = n % BITS_PER_MPI_LIMB;
139
if( limbno >= a->nlimbs ) { /* resize */
140
if( a->alloced >= limbno )
141
mpi_resize(a, limbno+1 );
142
a->nlimbs = limbno+1;
144
a->d[limbno] |= (A_LIMB_1<<bitno);
145
for( bitno++; bitno < BITS_PER_MPI_LIMB; bitno++ )
146
a->d[limbno] &= ~(A_LIMB_1 << bitno);
147
a->nlimbs = limbno+1;
151
* clear bit N of A and all bits above
154
mpi_clear_highbit( MPI a, unsigned n )
156
unsigned limbno, bitno;
158
limbno = n / BITS_PER_MPI_LIMB;
159
bitno = n % BITS_PER_MPI_LIMB;
161
if( limbno >= a->nlimbs )
162
return; /* not allocated, so need to clear bits :-) */
164
for( ; bitno < BITS_PER_MPI_LIMB; bitno++ )
165
a->d[limbno] &= ~(A_LIMB_1 << bitno);
166
a->nlimbs = limbno+1;
173
mpi_clear_bit( MPI a, unsigned n )
175
unsigned limbno, bitno;
177
limbno = n / BITS_PER_MPI_LIMB;
178
bitno = n % BITS_PER_MPI_LIMB;
180
if( limbno >= a->nlimbs )
181
return; /* don't need to clear this bit, it's to far to left */
182
a->d[limbno] &= ~(A_LIMB_1 << bitno);
187
* Shift A by N bits to the right
188
* FIXME: should use alloc_limb if X and A are same.
191
mpi_rshift( MPI x, MPI a, unsigned n )
198
RESIZE_IF_NEEDED(x, xsize);
202
mpihelp_rshift( xp, a->d, xsize, n);
203
MPN_NORMALIZE( xp, xsize);
210
* Shift A by COUNT limbs to the left
211
* This is used only within the MPI library
214
mpi_lshift_limbs( MPI a, unsigned int count )
223
RESIZE_IF_NEEDED( a, n+count );
225
for( i = n-1; i >= 0; i-- )
227
for(i=0; i < count; i++ )
234
* Shift A by COUNT limbs to the right
235
* This is used only within the MPI library
238
mpi_rshift_limbs( MPI a, unsigned int count )
241
mpi_size_t n = a->nlimbs;
249
for( i = 0; i < n - count; i++ )