mpsub.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // diff = abs(b1) - abs(b2), i.e., subtract the magnitudes
  13. void
  14. mpmagsub(mpint *b1, mpint *b2, mpint *diff)
  15. {
  16. int n, m, sign;
  17. mpint *t;
  18. // get the sizes right
  19. if(mpmagcmp(b1, b2) < 0){
  20. sign = -1;
  21. t = b1;
  22. b1 = b2;
  23. b2 = t;
  24. } else
  25. sign = 1;
  26. n = b1->top;
  27. m = b2->top;
  28. if(m == 0){
  29. mpassign(b1, diff);
  30. diff->sign = sign;
  31. return;
  32. }
  33. mpbits(diff, n*Dbits);
  34. mpvecsub(b1->p, n, b2->p, m, diff->p);
  35. diff->sign = sign;
  36. diff->top = n;
  37. mpnorm(diff);
  38. }
  39. // diff = b1 - b2
  40. void
  41. mpsub(mpint *b1, mpint *b2, mpint *diff)
  42. {
  43. int sign;
  44. if(b1->sign != b2->sign){
  45. sign = b1->sign;
  46. mpmagadd(b1, b2, diff);
  47. diff->sign = sign;
  48. return;
  49. }
  50. sign = b1->sign;
  51. mpmagsub(b1, b2, diff);
  52. if(diff->top != 0)
  53. diff->sign *= sign;
  54. }