mpright.c 695 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "os.h"
  2. #include <mp.h>
  3. #include "dat.h"
  4. // res = b >> shift
  5. void
  6. mpright(mpint *b, int shift, mpint *res)
  7. {
  8. int d, l, r, i;
  9. mpdigit this, last;
  10. // a negative right shift is a left shift
  11. if(shift < 0){
  12. mpleft(b, -shift, res);
  13. return;
  14. }
  15. if(res != b)
  16. mpbits(res, b->top*Dbits - shift);
  17. d = shift/Dbits;
  18. r = shift - d*Dbits;
  19. l = Dbits - r;
  20. // special case digit shifts
  21. if(r == 0){
  22. for(i = 0; i < b->top-d; i++)
  23. res->p[i] = b->p[i+d];
  24. } else {
  25. last = b->p[d];
  26. for(i = 0; i < b->top-d-1; i++){
  27. this = b->p[i+d+1];
  28. res->p[i] = (this<<l) | (last>>r);
  29. last = this;
  30. }
  31. res->p[i++] = last>>r;
  32. }
  33. while(i > 0 && res->p[i-1] == 0)
  34. i--;
  35. res->top = i;
  36. }