1 /* mpfr_eq -- Compare two floats up to a specified bit #.
3 Copyright 1999, 2001, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR 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 MPFR 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 MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include "mpfr-impl.h"
26 /* return non-zero if the first n_bits bits of u, v are equal,
29 mpfr_eq (mpfr_srcptr u
, mpfr_srcptr v
, unsigned long int n_bits
)
31 mpfr_limb_srcptr up
, vp
;
32 mp_size_t usize
, vsize
, size
, i
;
33 mpfr_exp_t uexp
, vexp
;
36 if (MPFR_ARE_SINGULAR(u
, v
))
38 if (MPFR_IS_NAN(u
) || MPFR_IS_NAN(v
))
39 return 0; /* non equal */
40 else if (MPFR_IS_INF(u
) && MPFR_IS_INF(v
))
41 return (MPFR_SIGN(u
) == MPFR_SIGN(v
));
42 else if (MPFR_IS_ZERO(u
) && MPFR_IS_ZERO(v
))
48 /* 1. Are the signs different? */
49 if (MPFR_SIGN(u
) != MPFR_SIGN(v
))
52 uexp
= MPFR_GET_EXP (u
);
53 vexp
= MPFR_GET_EXP (v
);
55 /* 2. Are the exponents different? */
57 return 0; /* no bit agree */
59 usize
= MPFR_LIMB_SIZE (u
);
60 vsize
= MPFR_LIMB_SIZE (v
);
62 if (vsize
> usize
) /* exchange u and v */
76 /* now usize >= vsize */
77 MPFR_ASSERTD(usize
>= vsize
);
81 if ((unsigned long) vsize
* GMP_NUMB_BITS
< n_bits
)
83 /* check if low min(PREC(u), n_bits) - (vsize * GMP_NUMB_BITS)
84 bits from u are non-zero */
85 unsigned long remains
= n_bits
- (vsize
* GMP_NUMB_BITS
);
86 k
= usize
- vsize
- 1;
87 while (k
>= 0 && remains
>= GMP_NUMB_BITS
&& !up
[k
])
90 remains
-= GMP_NUMB_BITS
;
92 /* now either k < 0: all low bits from u are zero
93 or remains < GMP_NUMB_BITS: check high bits from up[k]
94 or up[k] <> 0: different */
95 if (k
>= 0 && (((remains
< GMP_NUMB_BITS
) &&
96 (up
[k
] >> (GMP_NUMB_BITS
- remains
))) ||
97 (remains
>= GMP_NUMB_BITS
&& up
[k
])))
98 return 0; /* surely too different */
107 /* now size = min (usize, vsize) */
109 /* If size is too large wrt n_bits, reduce it to look only at the
111 Otherwise, if n_bits > size * GMP_NUMB_BITS, reduce n_bits to
112 size * GMP_NUMB_BITS, since the extra low bits of one of the
113 operands have already been check above. */
114 if ((unsigned long) size
> 1 + (n_bits
- 1) / GMP_NUMB_BITS
)
115 size
= 1 + (n_bits
- 1) / GMP_NUMB_BITS
;
116 else if (n_bits
> (unsigned long) size
* GMP_NUMB_BITS
)
117 n_bits
= size
* GMP_NUMB_BITS
;
122 for (i
= size
- 1; i
> 0 && n_bits
>= GMP_NUMB_BITS
; i
--)
126 n_bits
-= GMP_NUMB_BITS
;
129 /* now either i=0 or n_bits<GMP_NUMB_BITS */
131 /* since n_bits <= size * GMP_NUMB_BITS before the above for-loop,
132 we have the invariant n_bits <= (i+1) * GMP_NUMB_BITS, thus
133 we always have n_bits <= GMP_NUMB_BITS here */
134 MPFR_ASSERTD(n_bits
<= GMP_NUMB_BITS
);
136 if (n_bits
& (GMP_NUMB_BITS
- 1))
137 return (up
[i
] >> (GMP_NUMB_BITS
- (n_bits
& (GMP_NUMB_BITS
- 1))) ==
138 vp
[i
] >> (GMP_NUMB_BITS
- (n_bits
& (GMP_NUMB_BITS
- 1))));
140 return (up
[i
] == vp
[i
]);