libtommath/mp_cnt_lsb.c

39 lines
797 B
C
Raw Permalink Normal View History

#include "tommath_private.h"
#ifdef MP_CNT_LSB_C
2019-04-07 13:29:11 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2003-07-02 15:39:39 +00:00
2019-10-29 17:41:25 +00:00
static const char lnz[16] = {
2004-04-11 20:46:22 +00:00
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
2003-07-02 15:39:39 +00:00
/* Counts the number of lsbs which are zero before the first zero bit */
int mp_cnt_lsb(const mp_int *a)
2003-07-02 15:39:39 +00:00
{
int x;
2019-10-29 17:41:25 +00:00
mp_digit q;
2003-07-02 15:39:39 +00:00
2003-08-05 01:24:44 +00:00
/* easy out */
2019-10-24 15:43:31 +00:00
if (mp_iszero(a)) {
2003-07-02 15:39:39 +00:00
return 0;
}
/* scan lower digits until non-zero */
2017-10-15 17:57:12 +00:00
for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {}
2003-07-02 15:39:39 +00:00
q = a->dp[x];
2019-04-13 06:46:57 +00:00
x *= MP_DIGIT_BIT;
2003-07-02 15:39:39 +00:00
/* now scan this digit until a 1 is found */
2017-10-15 17:57:12 +00:00
if ((q & 1u) == 0u) {
2019-10-29 17:41:25 +00:00
mp_digit p;
2004-04-11 20:46:22 +00:00
do {
2019-10-29 17:41:25 +00:00
p = q & 15u;
x += lnz[p];
2004-04-11 20:46:22 +00:00
q >>= 4;
2019-10-29 17:41:25 +00:00
} while (p == 0u);
2003-07-02 15:39:39 +00:00
}
return x;
}
2004-10-29 22:07:18 +00:00
#endif