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
21 #include "mpi-internal.h"
24 const unsigned char __clz_tab
[] = {
25 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
26 5, 5, 5, 5, 5, 5, 5, 5,
27 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
28 6, 6, 6, 6, 6, 6, 6, 6,
29 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
30 7, 7, 7, 7, 7, 7, 7, 7,
31 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
32 7, 7, 7, 7, 7, 7, 7, 7,
33 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
34 8, 8, 8, 8, 8, 8, 8, 8,
35 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
36 8, 8, 8, 8, 8, 8, 8, 8,
37 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
38 8, 8, 8, 8, 8, 8, 8, 8,
39 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
40 8, 8, 8, 8, 8, 8, 8, 8,
43 #define A_LIMB_1 ((mpi_limb_t) 1)
46 * Sometimes we have MSL (most significant limbs) which are 0;
47 * this is for some reasons not good, so this function removes them.
49 void mpi_normalize(MPI a
)
51 for (; a
->nlimbs
&& !a
->d
[a
->nlimbs
- 1]; a
->nlimbs
--)
56 * Return the number of bits in A.
58 unsigned mpi_get_nbits(MPI a
)
65 mpi_limb_t alimb
= a
->d
[a
->nlimbs
- 1];
67 count_leading_zeros(n
, alimb
);
69 n
= BITS_PER_MPI_LIMB
;
70 n
= BITS_PER_MPI_LIMB
- n
+ (a
->nlimbs
- 1) * BITS_PER_MPI_LIMB
;
75 EXPORT_SYMBOL_GPL(mpi_get_nbits
);
78 * Test whether bit N is set.
80 int mpi_test_bit(MPI a
, unsigned n
)
82 unsigned limbno
, bitno
;
85 limbno
= n
/ BITS_PER_MPI_LIMB
;
86 bitno
= n
% BITS_PER_MPI_LIMB
;
88 if (limbno
>= a
->nlimbs
)
89 return 0; /* too far left: this is a 0 */
91 return (limb
& (A_LIMB_1
<< bitno
)) ? 1 : 0;
97 int mpi_set_bit(MPI a
, unsigned n
)
99 unsigned limbno
, bitno
;
101 limbno
= n
/ BITS_PER_MPI_LIMB
;
102 bitno
= n
% BITS_PER_MPI_LIMB
;
104 if (limbno
>= a
->nlimbs
) { /* resize */
105 if (a
->alloced
>= limbno
)
106 if (mpi_resize(a
, limbno
+ 1) < 0)
108 a
->nlimbs
= limbno
+ 1;
110 a
->d
[limbno
] |= (A_LIMB_1
<< bitno
);
115 * Set bit N of A. and clear all bits above
117 int mpi_set_highbit(MPI a
, unsigned n
)
119 unsigned limbno
, bitno
;
121 limbno
= n
/ BITS_PER_MPI_LIMB
;
122 bitno
= n
% BITS_PER_MPI_LIMB
;
124 if (limbno
>= a
->nlimbs
) { /* resize */
125 if (a
->alloced
>= limbno
)
126 if (mpi_resize(a
, limbno
+ 1) < 0)
128 a
->nlimbs
= limbno
+ 1;
130 a
->d
[limbno
] |= (A_LIMB_1
<< bitno
);
131 for (bitno
++; bitno
< BITS_PER_MPI_LIMB
; bitno
++)
132 a
->d
[limbno
] &= ~(A_LIMB_1
<< bitno
);
133 a
->nlimbs
= limbno
+ 1;
138 * clear bit N of A and all bits above
140 void mpi_clear_highbit(MPI a
, unsigned n
)
142 unsigned limbno
, bitno
;
144 limbno
= n
/ BITS_PER_MPI_LIMB
;
145 bitno
= n
% BITS_PER_MPI_LIMB
;
147 if (limbno
>= a
->nlimbs
)
148 return; /* not allocated, so need to clear bits :-) */
150 for (; bitno
< BITS_PER_MPI_LIMB
; bitno
++)
151 a
->d
[limbno
] &= ~(A_LIMB_1
<< bitno
);
152 a
->nlimbs
= limbno
+ 1;
158 void mpi_clear_bit(MPI a
, unsigned n
)
160 unsigned limbno
, bitno
;
162 limbno
= n
/ BITS_PER_MPI_LIMB
;
163 bitno
= n
% BITS_PER_MPI_LIMB
;
165 if (limbno
>= a
->nlimbs
)
166 return; /* don't need to clear this bit, it's to far to left */
167 a
->d
[limbno
] &= ~(A_LIMB_1
<< bitno
);
171 * Shift A by N bits to the right
172 * FIXME: should use alloc_limb if X and A are same.
174 int mpi_rshift(MPI x
, MPI a
, unsigned n
)
181 if (RESIZE_IF_NEEDED(x
, (size_t) xsize
) < 0)
186 mpihelp_rshift(xp
, a
->d
, xsize
, n
);
187 MPN_NORMALIZE(xp
, xsize
);
194 * Shift A by COUNT limbs to the left
195 * This is used only within the MPI library
197 int mpi_lshift_limbs(MPI a
, unsigned int count
)
206 if (RESIZE_IF_NEEDED(a
, n
+ count
) < 0)
209 for (i
= n
- 1; i
>= 0; i
--)
210 ap
[i
+ count
] = ap
[i
];
211 for (i
= 0; i
< count
; i
++)
218 * Shift A by COUNT limbs to the right
219 * This is used only within the MPI library
221 void mpi_rshift_limbs(MPI a
, unsigned int count
)
224 mpi_size_t n
= a
->nlimbs
;
232 for (i
= 0; i
< n
- count
; i
++)
233 ap
[i
] = ap
[i
+ count
];