bn_mp_cnt_lsb.c 791 B

12345678910111213141516171819202122232425262728293031323334353637
  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. static const int lnz[16] = {
  6. 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  7. };
  8. /* Counts the number of lsbs which are zero before the first zero bit */
  9. int mp_cnt_lsb(const mp_int *a)
  10. {
  11. int x;
  12. mp_digit q, qq;
  13. /* easy out */
  14. if (MP_IS_ZERO(a)) {
  15. return 0;
  16. }
  17. /* scan lower digits until non-zero */
  18. for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {}
  19. q = a->dp[x];
  20. x *= MP_DIGIT_BIT;
  21. /* now scan this digit until a 1 is found */
  22. if ((q & 1u) == 0u) {
  23. do {
  24. qq = q & 15u;
  25. x += lnz[qq];
  26. q >>= 4;
  27. } while (qq == 0u);
  28. }
  29. return x;
  30. }
  31. #endif