2015-11-11 19:49:07 -05:00
|
|
|
#include <tommath_private.h>
|
2004-10-29 18:07:18 -04:00
|
|
|
#ifdef BN_MP_CNT_LSB_C
|
2003-07-02 11:39:39 -04:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
2003-08-04 21:24:44 -04:00
|
|
|
* LibTomMath is a library that provides multiple-precision
|
2003-07-02 11:39:39 -04:00
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
2003-08-04 21:24:44 -04:00
|
|
|
* The library was designed directly after the MPI library by
|
2003-07-02 11:39:39 -04:00
|
|
|
* Michael Fromberger but has been written from scratch with
|
|
|
|
* additional optimizations in place.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*
|
2015-10-30 17:55:29 -04:00
|
|
|
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
2003-07-02 11:39:39 -04:00
|
|
|
*/
|
|
|
|
|
2017-08-29 23:51:11 -04:00
|
|
|
static const int lnz[16] = {
|
2004-04-11 16:46:22 -04:00
|
|
|
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
|
|
|
|
};
|
|
|
|
|
2003-07-02 11:39:39 -04:00
|
|
|
/* Counts the number of lsbs which are zero before the first zero bit */
|
|
|
|
int mp_cnt_lsb(mp_int *a)
|
|
|
|
{
|
|
|
|
int x;
|
2004-04-11 16:46:22 -04:00
|
|
|
mp_digit q, qq;
|
2003-07-02 11:39:39 -04:00
|
|
|
|
2003-08-04 21:24:44 -04:00
|
|
|
/* easy out */
|
2015-10-25 11:34:43 -04:00
|
|
|
if (mp_iszero(a) == MP_YES) {
|
2003-07-02 11:39:39 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* scan lower digits until non-zero */
|
2015-11-13 04:28:23 -05:00
|
|
|
for (x = 0; (x < a->used) && (a->dp[x] == 0); x++) {}
|
2003-07-02 11:39:39 -04:00
|
|
|
q = a->dp[x];
|
|
|
|
x *= DIGIT_BIT;
|
|
|
|
|
|
|
|
/* now scan this digit until a 1 is found */
|
2004-04-11 16:46:22 -04:00
|
|
|
if ((q & 1) == 0) {
|
|
|
|
do {
|
|
|
|
qq = q & 15;
|
|
|
|
x += lnz[qq];
|
|
|
|
q >>= 4;
|
|
|
|
} while (qq == 0);
|
2003-07-02 11:39:39 -04:00
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2004-10-29 18:07:18 -04:00
|
|
|
#endif
|
2005-08-01 12:37:28 -04:00
|
|
|
|
2017-08-28 10:27:26 -04:00
|
|
|
/* ref: $Format:%D$ */
|
|
|
|
/* git commit: $Format:%H$ */
|
|
|
|
/* commit time: $Format:%ai$ */
|