cmp.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* mpn_cmp -- Compare two low-level natural-number integers.
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU MP Library.
  4. The GNU MP Library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or (at your
  7. option) any later version.
  8. The GNU MP Library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with the GNU MP Library; see the file COPYING.LIB. If not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <gmp.h>
  16. #include "gmp-impl.h"
  17. /* Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
  18. There are no restrictions on the relative sizes of
  19. the two arguments.
  20. Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2. */
  21. int
  22. mpn_cmp (mp_srcptr op1_ptr, mp_srcptr op2_ptr, mp_size_t size)
  23. {
  24. mp_size_t i;
  25. mp_limb_t op1_word, op2_word;
  26. for (i = size - 1; i >= 0; i--)
  27. {
  28. op1_word = op1_ptr[i];
  29. op2_word = op2_ptr[i];
  30. if (op1_word != op2_word)
  31. goto diff;
  32. }
  33. return 0;
  34. diff:
  35. /* This can *not* be simplified to
  36. op2_word - op2_word
  37. since that expression might give signed overflow. */
  38. return (op1_word > op2_word) ? 1 : -1;
  39. }