xref: /dragonfly/contrib/gmp/mpf/eq.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /* mpf_eq -- Compare two floats up to a specified bit #.
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino Copyright 1993, 1995, 1996, 2001, 2002, 2008, 2009 Free Software Foundation,
4*86d7f5d3SJohn Marino Inc.
5*86d7f5d3SJohn Marino 
6*86d7f5d3SJohn Marino This file is part of the GNU MP Library.
7*86d7f5d3SJohn Marino 
8*86d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
9*86d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
10*86d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
11*86d7f5d3SJohn Marino option) any later version.
12*86d7f5d3SJohn Marino 
13*86d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
14*86d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15*86d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16*86d7f5d3SJohn Marino License for more details.
17*86d7f5d3SJohn Marino 
18*86d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
19*86d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
20*86d7f5d3SJohn Marino 
21*86d7f5d3SJohn Marino #include "gmp.h"
22*86d7f5d3SJohn Marino #include "gmp-impl.h"
23*86d7f5d3SJohn Marino #include "longlong.h"
24*86d7f5d3SJohn Marino 
25*86d7f5d3SJohn Marino int
mpf_eq(mpf_srcptr u,mpf_srcptr v,mp_bitcnt_t n_bits)26*86d7f5d3SJohn Marino mpf_eq (mpf_srcptr u, mpf_srcptr v, mp_bitcnt_t n_bits)
27*86d7f5d3SJohn Marino {
28*86d7f5d3SJohn Marino   mp_srcptr up, vp, p;
29*86d7f5d3SJohn Marino   mp_size_t usize, vsize, minsize, maxsize, n_limbs, i, size;
30*86d7f5d3SJohn Marino   mp_exp_t uexp, vexp;
31*86d7f5d3SJohn Marino   mp_limb_t diff;
32*86d7f5d3SJohn Marino   int cnt;
33*86d7f5d3SJohn Marino 
34*86d7f5d3SJohn Marino   uexp = u->_mp_exp;
35*86d7f5d3SJohn Marino   vexp = v->_mp_exp;
36*86d7f5d3SJohn Marino 
37*86d7f5d3SJohn Marino   usize = u->_mp_size;
38*86d7f5d3SJohn Marino   vsize = v->_mp_size;
39*86d7f5d3SJohn Marino 
40*86d7f5d3SJohn Marino   /* 1. Are the signs different?  */
41*86d7f5d3SJohn Marino   if ((usize ^ vsize) >= 0)
42*86d7f5d3SJohn Marino     {
43*86d7f5d3SJohn Marino       /* U and V are both non-negative or both negative.  */
44*86d7f5d3SJohn Marino       if (usize == 0)
45*86d7f5d3SJohn Marino 	return vsize == 0;
46*86d7f5d3SJohn Marino       if (vsize == 0)
47*86d7f5d3SJohn Marino 	return 0;
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino       /* Fall out.  */
50*86d7f5d3SJohn Marino     }
51*86d7f5d3SJohn Marino   else
52*86d7f5d3SJohn Marino     {
53*86d7f5d3SJohn Marino       /* Either U or V is negative, but not both.  */
54*86d7f5d3SJohn Marino       return 0;
55*86d7f5d3SJohn Marino     }
56*86d7f5d3SJohn Marino 
57*86d7f5d3SJohn Marino   /* U and V have the same sign and are both non-zero.  */
58*86d7f5d3SJohn Marino 
59*86d7f5d3SJohn Marino   /* 2. Are the exponents different?  */
60*86d7f5d3SJohn Marino   if (uexp != vexp)
61*86d7f5d3SJohn Marino     return 0;
62*86d7f5d3SJohn Marino 
63*86d7f5d3SJohn Marino   usize = ABS (usize);
64*86d7f5d3SJohn Marino   vsize = ABS (vsize);
65*86d7f5d3SJohn Marino 
66*86d7f5d3SJohn Marino   up = u->_mp_d;
67*86d7f5d3SJohn Marino   vp = v->_mp_d;
68*86d7f5d3SJohn Marino 
69*86d7f5d3SJohn Marino   up += usize;			/* point just above most significant limb */
70*86d7f5d3SJohn Marino   vp += vsize;			/* point just above most significant limb */
71*86d7f5d3SJohn Marino 
72*86d7f5d3SJohn Marino   count_leading_zeros (cnt, up[-1]);
73*86d7f5d3SJohn Marino   if ((vp[-1] >> (GMP_LIMB_BITS - 1 - cnt)) != 1)
74*86d7f5d3SJohn Marino     return 0;			/* msb positions different */
75*86d7f5d3SJohn Marino 
76*86d7f5d3SJohn Marino   n_bits += cnt - GMP_NAIL_BITS;
77*86d7f5d3SJohn Marino   n_limbs = (n_bits + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;
78*86d7f5d3SJohn Marino 
79*86d7f5d3SJohn Marino   usize = MIN (usize, n_limbs);
80*86d7f5d3SJohn Marino   vsize = MIN (vsize, n_limbs);
81*86d7f5d3SJohn Marino 
82*86d7f5d3SJohn Marino #if 0
83*86d7f5d3SJohn Marino   /* Ignore zeros at the low end of U and V.  */
84*86d7f5d3SJohn Marino   while (up[0] == 0)
85*86d7f5d3SJohn Marino     up++, usize--;
86*86d7f5d3SJohn Marino   while (vp[0] == 0)
87*86d7f5d3SJohn Marino     vp++, vsize--;
88*86d7f5d3SJohn Marino #endif
89*86d7f5d3SJohn Marino 
90*86d7f5d3SJohn Marino   minsize = MIN (usize, vsize);
91*86d7f5d3SJohn Marino   maxsize = usize + vsize - minsize;
92*86d7f5d3SJohn Marino 
93*86d7f5d3SJohn Marino   up -= minsize;		/* point at most significant common limb */
94*86d7f5d3SJohn Marino   vp -= minsize;		/* point at most significant common limb */
95*86d7f5d3SJohn Marino 
96*86d7f5d3SJohn Marino   /* Compare the most significant part which has explicit limbs for U and V. */
97*86d7f5d3SJohn Marino   for (i = minsize - 1; i > 0; i--)
98*86d7f5d3SJohn Marino     {
99*86d7f5d3SJohn Marino       if (up[i] != vp[i])
100*86d7f5d3SJohn Marino 	return 0;
101*86d7f5d3SJohn Marino     }
102*86d7f5d3SJohn Marino 
103*86d7f5d3SJohn Marino   n_bits -= (maxsize - 1) * GMP_NUMB_BITS;
104*86d7f5d3SJohn Marino 
105*86d7f5d3SJohn Marino   size = maxsize - minsize;
106*86d7f5d3SJohn Marino   if (size != 0)
107*86d7f5d3SJohn Marino     {
108*86d7f5d3SJohn Marino       if (up[0] != vp[0])
109*86d7f5d3SJohn Marino 	return 0;
110*86d7f5d3SJohn Marino 
111*86d7f5d3SJohn Marino       /* Now either U or V has its limbs consumed, i.e, continues with an
112*86d7f5d3SJohn Marino 	 infinite number of implicit zero limbs.  Check that the other operand
113*86d7f5d3SJohn Marino 	 has just zeros in the corresponding, relevant part.  */
114*86d7f5d3SJohn Marino 
115*86d7f5d3SJohn Marino       if (usize > vsize)
116*86d7f5d3SJohn Marino 	p = up - size;
117*86d7f5d3SJohn Marino       else
118*86d7f5d3SJohn Marino 	p = vp - size;
119*86d7f5d3SJohn Marino 
120*86d7f5d3SJohn Marino       for (i = size - 1; i > 0; i--)
121*86d7f5d3SJohn Marino 	{
122*86d7f5d3SJohn Marino 	  if (p[i] != 0)
123*86d7f5d3SJohn Marino 	    return 0;
124*86d7f5d3SJohn Marino 	}
125*86d7f5d3SJohn Marino 
126*86d7f5d3SJohn Marino       diff = p[0];
127*86d7f5d3SJohn Marino     }
128*86d7f5d3SJohn Marino   else
129*86d7f5d3SJohn Marino     {
130*86d7f5d3SJohn Marino       /* Both U or V has its limbs consumed.  */
131*86d7f5d3SJohn Marino 
132*86d7f5d3SJohn Marino       diff = up[0] ^ vp[0];
133*86d7f5d3SJohn Marino     }
134*86d7f5d3SJohn Marino 
135*86d7f5d3SJohn Marino   if (n_bits < GMP_NUMB_BITS)
136*86d7f5d3SJohn Marino     diff >>= GMP_NUMB_BITS - n_bits;
137*86d7f5d3SJohn Marino 
138*86d7f5d3SJohn Marino   return diff == 0;
139*86d7f5d3SJohn Marino }
140