Line data Source code
1 : #include "tommath_private.h"
2 : #ifdef BN_MP_CNT_LSB_C
3 : /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 : /* SPDX-License-Identifier: Unlicense */
5 :
6 : static const int lnz[16] = {
7 : 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
8 : };
9 :
10 : /* Counts the number of lsbs which are zero before the first zero bit */
11 0 : int mp_cnt_lsb(const mp_int *a)
12 : {
13 : int x;
14 : mp_digit q, qq;
15 :
16 : /* easy out */
17 0 : if (MP_IS_ZERO(a)) {
18 0 : return 0;
19 : }
20 :
21 : /* scan lower digits until non-zero */
22 0 : for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {}
23 0 : q = a->dp[x];
24 0 : x *= MP_DIGIT_BIT;
25 :
26 : /* now scan this digit until a 1 is found */
27 0 : if ((q & 1u) == 0u) {
28 : do {
29 0 : qq = q & 15u;
30 0 : x += lnz[qq];
31 0 : q >>= 4;
32 0 : } while (qq == 0u);
33 : }
34 0 : return x;
35 : }
36 :
37 : #endif
|