bn_mp_count_bits.c 576 B

12345678910111213141516171819202122232425262728
  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. /* returns the number of bits in an int */
  6. int mp_count_bits(const mp_int *a)
  7. {
  8. int r;
  9. mp_digit q;
  10. /* shortcut */
  11. if (MP_IS_ZERO(a)) {
  12. return 0;
  13. }
  14. /* get number of digits and add that */
  15. r = (a->used - 1) * MP_DIGIT_BIT;
  16. /* take the last digit and count the bits in it */
  17. q = a->dp[a->used - 1];
  18. while (q > 0u) {
  19. ++r;
  20. q >>= 1u;
  21. }
  22. return r;
  23. }
  24. #endif