bn_mp_cnt_lsb.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <tommath.h>
  2. #ifdef BN_MP_CNT_LSB_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis
  4. *
  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, tomstdenis@gmail.com, http://math.libtomcrypt.com
  16. */
  17. static const int lnz[16] = {
  18. 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  19. };
  20. /* Counts the number of lsbs which are zero before the first zero bit */
  21. int mp_cnt_lsb(mp_int *a)
  22. {
  23. int x;
  24. mp_digit q, qq;
  25. /* easy out */
  26. if (mp_iszero(a) == 1) {
  27. return 0;
  28. }
  29. /* scan lower digits until non-zero */
  30. for (x = 0; x < a->used && a->dp[x] == 0; x++);
  31. q = a->dp[x];
  32. x *= DIGIT_BIT;
  33. /* now scan this digit until a 1 is found */
  34. if ((q & 1) == 0) {
  35. do {
  36. qq = q & 15;
  37. x += lnz[qq];
  38. q >>= 4;
  39. } while (qq == 0);
  40. }
  41. return x;
  42. }
  43. #endif
  44. /* $Source: /cvs/libtom/libtommath/bn_mp_cnt_lsb.c,v $ */
  45. /* $Revision: 1.3 $ */
  46. /* $Date: 2006/03/31 14:18:44 $ */