comparison libtommath/bn_mp_count_bits.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_COUNT_BITS_C 2 #ifdef BN_MP_COUNT_BITS_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 * 4 /* SPDX-License-Identifier: Unlicense */
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
7 *
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 /* returns the number of bits in an int */ 6 /* returns the number of bits in an int */
19 int 7 int mp_count_bits(const mp_int *a)
20 mp_count_bits (mp_int * a)
21 { 8 {
22 int r; 9 int r;
23 mp_digit q; 10 mp_digit q;
24 11
25 /* shortcut */ 12 /* shortcut */
26 if (a->used == 0) { 13 if (MP_IS_ZERO(a)) {
27 return 0; 14 return 0;
28 } 15 }
29 16
30 /* get number of digits and add that */ 17 /* get number of digits and add that */
31 r = (a->used - 1) * DIGIT_BIT; 18 r = (a->used - 1) * MP_DIGIT_BIT;
32 19
33 /* take the last digit and count the bits in it */ 20 /* take the last digit and count the bits in it */
34 q = a->dp[a->used - 1]; 21 q = a->dp[a->used - 1];
35 while (q > ((mp_digit) 0)) { 22 while (q > 0u) {
36 ++r; 23 ++r;
37 q >>= ((mp_digit) 1); 24 q >>= 1u;
38 } 25 }
39 return r; 26 return r;
40 } 27 }
41 #endif 28 #endif
42
43 /* ref: $Format:%D$ */
44 /* git commit: $Format:%H$ */
45 /* commit time: $Format:%ai$ */