mpleft.c 906 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "os.h"
  2. #include <mp.h>
  3. #include "dat.h"
  4. // res = b << shift
  5. void
  6. mpleft(mpint *b, int shift, mpint *res)
  7. {
  8. int d, l, r, i, otop;
  9. mpdigit this, last;
  10. res->sign = b->sign;
  11. if(b->top==0){
  12. res->top = 0;
  13. return;
  14. }
  15. // a negative left shift is a right shift
  16. if(shift < 0){
  17. mpright(b, -shift, res);
  18. return;
  19. }
  20. // b and res may be the same so remember the old top
  21. otop = b->top;
  22. // shift
  23. mpbits(res, otop*Dbits + shift); // overkill
  24. res->top = DIGITS(otop*Dbits + shift);
  25. d = shift/Dbits;
  26. l = shift - d*Dbits;
  27. r = Dbits - l;
  28. if(l == 0){
  29. for(i = otop-1; i >= 0; i--)
  30. res->p[i+d] = b->p[i];
  31. } else {
  32. last = 0;
  33. for(i = otop-1; i >= 0; i--) {
  34. this = b->p[i];
  35. res->p[i+d+1] = (last<<l) | (this>>r);
  36. last = this;
  37. }
  38. res->p[d] = last<<l;
  39. }
  40. for(i = 0; i < d; i++)
  41. res->p[i] = 0;
  42. // normalize
  43. while(res->top > 0 && res->p[res->top-1] == 0)
  44. res->top--;
  45. }