glibc/sysdeps/generic/cmp.c

57 lines
1.7 KiB
C
Raw Normal View History

/* mpn_cmp -- Compare two low-level natural-number integers.
1995-02-18 01:27:10 +00:00
Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
1995-02-18 01:27:10 +00:00
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
2001-07-07 10:13:33 +00:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
1995-02-18 01:27:10 +00:00
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2001-07-07 10:13:33 +00:00
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1995-02-18 01:27:10 +00:00
License for more details.
2001-07-07 10:13:33 +00:00
You should have received a copy of the GNU Lesser General Public License
1995-02-18 01:27:10 +00:00
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
1996-05-07 21:04:52 +00:00
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
1995-02-18 01:27:10 +00:00
#include "gmp.h"
#include "gmp-impl.h"
/* Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
There are no restrictions on the relative sizes of
the two arguments.
Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2. */
int
#if __STDC__
mpn_cmp (mp_srcptr op1_ptr, mp_srcptr op2_ptr, mp_size_t size)
1995-02-18 01:27:10 +00:00
#else
mpn_cmp (op1_ptr, op2_ptr, size)
1995-02-18 01:27:10 +00:00
mp_srcptr op1_ptr;
mp_srcptr op2_ptr;
mp_size_t size;
#endif
{
mp_size_t i;
1996-05-07 21:04:52 +00:00
mp_limb_t op1_word, op2_word;
1995-02-18 01:27:10 +00:00
for (i = size - 1; i >= 0; i--)
{
op1_word = op1_ptr[i];
op2_word = op2_ptr[i];
if (op1_word != op2_word)
goto diff;
}
return 0;
diff:
/* This can *not* be simplified to
op2_word - op2_word
since that expression might give signed overflow. */
return (op1_word > op2_word) ? 1 : -1;
}