xref: /netbsd-src/external/gpl3/gcc.old/dist/libquadmath/printf/cmp.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1*627f7eb2Smrg /* mpn_cmp -- Compare two low-level natural-number integers.
2*627f7eb2Smrg 
3*627f7eb2Smrg Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
4*627f7eb2Smrg 
5*627f7eb2Smrg This file is part of the GNU MP Library.
6*627f7eb2Smrg 
7*627f7eb2Smrg The GNU MP Library is free software; you can redistribute it and/or modify
8*627f7eb2Smrg it under the terms of the GNU Lesser General Public License as published by
9*627f7eb2Smrg the Free Software Foundation; either version 2.1 of the License, or (at your
10*627f7eb2Smrg option) any later version.
11*627f7eb2Smrg 
12*627f7eb2Smrg The GNU MP Library is distributed in the hope that it will be useful, but
13*627f7eb2Smrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14*627f7eb2Smrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15*627f7eb2Smrg License for more details.
16*627f7eb2Smrg 
17*627f7eb2Smrg You should have received a copy of the GNU Lesser General Public License
18*627f7eb2Smrg along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19*627f7eb2Smrg the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20*627f7eb2Smrg MA 02111-1307, USA. */
21*627f7eb2Smrg 
22*627f7eb2Smrg #include <config.h>
23*627f7eb2Smrg #include "gmp-impl.h"
24*627f7eb2Smrg 
25*627f7eb2Smrg /* Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
26*627f7eb2Smrg    There are no restrictions on the relative sizes of
27*627f7eb2Smrg    the two arguments.
28*627f7eb2Smrg    Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2.  */
29*627f7eb2Smrg 
30*627f7eb2Smrg int
31*627f7eb2Smrg #if __STDC__
mpn_cmp(mp_srcptr op1_ptr,mp_srcptr op2_ptr,mp_size_t size)32*627f7eb2Smrg mpn_cmp (mp_srcptr op1_ptr, mp_srcptr op2_ptr, mp_size_t size)
33*627f7eb2Smrg #else
34*627f7eb2Smrg mpn_cmp (op1_ptr, op2_ptr, size)
35*627f7eb2Smrg      mp_srcptr op1_ptr;
36*627f7eb2Smrg      mp_srcptr op2_ptr;
37*627f7eb2Smrg      mp_size_t size;
38*627f7eb2Smrg #endif
39*627f7eb2Smrg {
40*627f7eb2Smrg   mp_size_t i;
41*627f7eb2Smrg   mp_limb_t op1_word, op2_word;
42*627f7eb2Smrg 
43*627f7eb2Smrg   for (i = size - 1; i >= 0; i--)
44*627f7eb2Smrg     {
45*627f7eb2Smrg       op1_word = op1_ptr[i];
46*627f7eb2Smrg       op2_word = op2_ptr[i];
47*627f7eb2Smrg       if (op1_word != op2_word)
48*627f7eb2Smrg 	goto diff;
49*627f7eb2Smrg     }
50*627f7eb2Smrg   return 0;
51*627f7eb2Smrg  diff:
52*627f7eb2Smrg   /* This can *not* be simplified to
53*627f7eb2Smrg 	op2_word - op2_word
54*627f7eb2Smrg      since that expression might give signed overflow.  */
55*627f7eb2Smrg   return (op1_word > op2_word) ? 1 : -1;
56*627f7eb2Smrg }
57