/src/dropbear/libtommath/bn_mp_count_bits.c
Line | Count | Source |
1 | | #include "tommath_private.h" |
2 | | #ifdef BN_MP_COUNT_BITS_C |
3 | | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
4 | | /* SPDX-License-Identifier: Unlicense */ |
5 | | |
6 | | /* returns the number of bits in an int */ |
7 | | int mp_count_bits(const mp_int *a) |
8 | 50.7k | { |
9 | 50.7k | int r; |
10 | 50.7k | mp_digit q; |
11 | | |
12 | | /* shortcut */ |
13 | 50.7k | if (MP_IS_ZERO(a)) { |
14 | 876 | return 0; |
15 | 876 | } |
16 | | |
17 | | /* get number of digits and add that */ |
18 | 49.8k | r = (a->used - 1) * MP_DIGIT_BIT; |
19 | | |
20 | | /* take the last digit and count the bits in it */ |
21 | 49.8k | q = a->dp[a->used - 1]; |
22 | 895k | while (q > 0u) { |
23 | 845k | ++r; |
24 | 845k | q >>= 1u; |
25 | 845k | } |
26 | 49.8k | return r; |
27 | 50.7k | } |
28 | | #endif |