1 /* mpf_eq -- Compare two floats up to a specified bit #.
3 Copyright 1993, 1995, 1996, 2001, 2002, 2008, 2009 Free Software Foundation,
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
26 mpf_eq (mpf_srcptr u, mpf_srcptr v, mp_bitcnt_t n_bits)
29 mp_size_t usize, vsize, minsize, maxsize, n_limbs, i, size;
40 /* 1. Are the signs different? */
41 if ((usize ^ vsize) >= 0)
43 /* U and V are both non-negative or both negative. */
53 /* Either U or V is negative, but not both. */
57 /* U and V have the same sign and are both non-zero. */
59 /* 2. Are the exponents different? */
69 up += usize; /* point just above most significant limb */
70 vp += vsize; /* point just above most significant limb */
72 count_leading_zeros (cnt, up[-1]);
73 if ((vp[-1] >> (GMP_LIMB_BITS - 1 - cnt)) != 1)
74 return 0; /* msb positions different */
76 n_bits += cnt - GMP_NAIL_BITS;
77 n_limbs = (n_bits + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;
79 usize = MIN (usize, n_limbs);
80 vsize = MIN (vsize, n_limbs);
83 /* Ignore zeros at the low end of U and V. */
90 minsize = MIN (usize, vsize);
91 maxsize = usize + vsize - minsize;
93 up -= minsize; /* point at most significant common limb */
94 vp -= minsize; /* point at most significant common limb */
96 /* Compare the most significant part which has explicit limbs for U and V. */
97 for (i = minsize - 1; i > 0; i--)
103 n_bits -= (maxsize - 1) * GMP_NUMB_BITS;
105 size = maxsize - minsize;
111 /* Now either U or V has its limbs consumed, i.e, continues with an
112 infinite number of implicit zero limbs. Check that the other operand
113 has just zeros in the corresponding, relevant part. */
120 for (i = size - 1; i > 0; i--)
130 /* Both U or V has its limbs consumed. */
132 diff = up[0] ^ vp[0];
135 if (n_bits < GMP_NUMB_BITS)
136 diff >>= GMP_NUMB_BITS - n_bits;