Blame stdlib/cmp.c

Packit 6c4009
/* mpn_cmp -- Compare two low-level natural-number integers.
Packit 6c4009
Packit 6c4009
Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
This file is part of the GNU MP Library.
Packit 6c4009
Packit 6c4009
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 6c4009
it under the terms of the GNU Lesser General Public License as published by
Packit 6c4009
the Free Software Foundation; either version 2.1 of the License, or (at your
Packit 6c4009
option) any later version.
Packit 6c4009
Packit 6c4009
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 6c4009
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 6c4009
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
Packit 6c4009
License for more details.
Packit 6c4009
Packit 6c4009
You should have received a copy of the GNU Lesser General Public License
Packit 6c4009
along with the GNU MP Library; see the file COPYING.LIB.  If not, see
Packit 6c4009
<http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <gmp.h>
Packit 6c4009
#include "gmp-impl.h"
Packit 6c4009
Packit 6c4009
/* Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
Packit 6c4009
   There are no restrictions on the relative sizes of
Packit 6c4009
   the two arguments.
Packit 6c4009
   Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2.  */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
mpn_cmp (mp_srcptr op1_ptr, mp_srcptr op2_ptr, mp_size_t size)
Packit 6c4009
{
Packit 6c4009
  mp_size_t i;
Packit 6c4009
  mp_limb_t op1_word, op2_word;
Packit 6c4009
Packit 6c4009
  for (i = size - 1; i >= 0; i--)
Packit 6c4009
    {
Packit 6c4009
      op1_word = op1_ptr[i];
Packit 6c4009
      op2_word = op2_ptr[i];
Packit 6c4009
      if (op1_word != op2_word)
Packit 6c4009
	goto diff;
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
 diff:
Packit 6c4009
  /* This can *not* be simplified to
Packit 6c4009
	op2_word - op2_word
Packit 6c4009
     since that expression might give signed overflow.  */
Packit 6c4009
  return (op1_word > op2_word) ? 1 : -1;
Packit 6c4009
}