mpvecsub.c 935 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "os.h"
  10. #include <mp.h>
  11. #include "dat.h"
  12. // prereq: a >= b, alen >= blen, diff has at least alen digits
  13. void
  14. mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *diff)
  15. {
  16. int i, borrow;
  17. mpdigit x, y;
  18. borrow = 0;
  19. for(i = 0; i < blen; i++){
  20. x = *a++;
  21. y = *b++;
  22. y += borrow;
  23. if(y < borrow)
  24. borrow = 1;
  25. else
  26. borrow = 0;
  27. if(x < y)
  28. borrow++;
  29. *diff++ = x - y;
  30. }
  31. for(; i < alen; i++){
  32. x = *a++;
  33. y = x - borrow;
  34. if(y > x)
  35. borrow = 1;
  36. else
  37. borrow = 0;
  38. *diff++ = y;
  39. }
  40. }