mcmp.cpp 848 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Bignum compare
  2. //
  3. // returns
  4. //
  5. // -1 a < b
  6. //
  7. // 0 a = b
  8. //
  9. // 1 a > b
  10. #include "stdafx.h"
  11. #include "defs.h"
  12. int
  13. mcmp(unsigned int *a, unsigned int *b)
  14. {
  15. int i;
  16. if (MSIGN(a) == -1 && MSIGN(b) == 1)
  17. return -1;
  18. if (MSIGN(a) == 1 && MSIGN(b) == -1)
  19. return 1;
  20. // same sign
  21. if (MLENGTH(a) < MLENGTH(b)) {
  22. if (MSIGN(a) == 1)
  23. return -1;
  24. else
  25. return 1;
  26. }
  27. if (MLENGTH(a) > MLENGTH(b)) {
  28. if (MSIGN(a) == 1)
  29. return 1;
  30. else
  31. return -1;
  32. }
  33. // same length
  34. for (i = MLENGTH(a) - 1; i > 0; i--)
  35. if (a[i] != b[i])
  36. break;
  37. if (a[i] < b[i]) {
  38. if (MSIGN(a) == 1)
  39. return -1;
  40. else
  41. return 1;
  42. }
  43. if (a[i] > b[i]) {
  44. if (MSIGN(a) == 1)
  45. return 1;
  46. else
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. int
  52. mcmpint(unsigned int *a, int n)
  53. {
  54. int t;
  55. unsigned int *b;
  56. b = mint(n);
  57. t = mcmp(a, b);
  58. mfree(b);
  59. return t;
  60. }