1 /* $NetBSD: bn_mp_cnt_lsb.c,v 1.1.1.2 2014/04/24 12:45:31 pettai Exp $ */
2
3 #include <tommath.h>
4 #ifdef BN_MP_CNT_LSB_C
5 /* LibTomMath, multiple-precision integer library -- Tom St Denis
6 *
7 * LibTomMath is a library that provides multiple-precision
8 * integer arithmetic as well as number theoretic functionality.
9 *
10 * The library was designed directly after the MPI library by
11 * Michael Fromberger but has been written from scratch with
12 * additional optimizations in place.
13 *
14 * The library is free for all purposes without any express
15 * guarantee it works.
16 *
17 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18 */
19
20 static const int lnz[16] = {
21 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
22 };
23
24 /* Counts the number of lsbs which are zero before the first zero bit */
mp_cnt_lsb(mp_int * a)25 int mp_cnt_lsb(mp_int *a)
26 {
27 int x;
28 mp_digit q, qq;
29
30 /* easy out */
31 if (mp_iszero(a) == 1) {
32 return 0;
33 }
34
35 /* scan lower digits until non-zero */
36 for (x = 0; x < a->used && a->dp[x] == 0; x++);
37 q = a->dp[x];
38 x *= DIGIT_BIT;
39
40 /* now scan this digit until a 1 is found */
41 if ((q & 1) == 0) {
42 do {
43 qq = q & 15;
44 x += lnz[qq];
45 q >>= 4;
46 } while (qq == 0);
47 }
48 return x;
49 }
50
51 #endif
52
53 /* Source: /cvs/libtom/libtommath/bn_mp_cnt_lsb.c,v */
54 /* Revision: 1.4 */
55 /* Date: 2006/12/28 01:25:13 */
56