[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / gmp / mpz_cmp.c
1 /* mpz_cmp(u,v) -- Compare U, V.  Return postive, zero, or negative
2    based on if U > V, U == V, or U < V.
3
4 Copyright (C) 1991 Free Software Foundation, Inc.
5
6 This file is part of the GNU MP Library.
7
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 The GNU MP Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with the GNU MP Library; see the file COPYING.  If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22 #ifdef BERKELEY_MP
23 #include "mp.h"
24 #endif
25 #include "gmp.h"
26 #include "gmp-impl.h"
27
28 #ifndef BERKELEY_MP
29 int
30 #ifdef __STDC__
31 mpz_cmp (const MP_INT *u, const MP_INT *v)
32 #else
33 mpz_cmp (u, v)
34      const MP_INT *u;
35      const MP_INT *v;
36 #endif
37 #else /* BERKELEY_MP */
38 int
39 #ifdef __STDC__
40 mcmp (const MP_INT *u, const MP_INT *v)
41 #else
42 mcmp (u, v)
43      const MP_INT *u;
44      const MP_INT *v;
45 #endif
46 #endif /* BERKELEY_MP */
47 {
48   mp_size usize = u->size;
49   mp_size vsize = v->size;
50   mp_size size;
51   mp_size i;
52   mp_limb a, b;
53   mp_srcptr up, vp;
54
55   if (usize != vsize)
56     return usize - vsize;
57
58   if (usize == 0)
59     return 0;
60
61   size = ABS (usize);
62
63   up = u->d;
64   vp = v->d;
65
66   i = size - 1;
67   do
68     {
69       a = up[i];
70       b = vp[i];
71       i--;
72       if (i < 0)
73         break;
74     }
75   while (a == b);
76
77   if (a == b)
78     return 0;
79
80   if ((a < b) == (usize < 0))
81     return 1;
82   else
83     return -1;
84 }