1 /* mpf_eq -- Compare two floats up to a specified bit #.
3 Copyright 1993, 1995, 1996, 2001, 2002, 2008, 2009, 2012 Free Software
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 either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
37 mpf_eq (mpf_srcptr u
, mpf_srcptr v
, mp_bitcnt_t n_bits
)
40 mp_size_t usize
, vsize
, minsize
, maxsize
, n_limbs
, i
, size
;
51 /* 1. Are the signs different? */
52 if ((usize
^ vsize
) >= 0)
54 /* U and V are both non-negative or both negative. */
64 /* Either U or V is negative, but not both. */
68 /* U and V have the same sign and are both non-zero. */
70 /* 2. Are the exponents different? */
80 up
+= usize
; /* point just above most significant limb */
81 vp
+= vsize
; /* point just above most significant limb */
83 count_leading_zeros (cnt
, up
[-1]);
84 if ((vp
[-1] >> (GMP_LIMB_BITS
- 1 - cnt
)) != 1)
85 return 0; /* msb positions different */
87 n_bits
+= cnt
- GMP_NAIL_BITS
;
88 n_limbs
= (n_bits
+ GMP_NUMB_BITS
- 1) / GMP_NUMB_BITS
;
90 usize
= MIN (usize
, n_limbs
);
91 vsize
= MIN (vsize
, n_limbs
);
94 /* Ignore zeros at the low end of U and V. */
101 minsize
= MIN (usize
, vsize
);
102 maxsize
= usize
+ vsize
- minsize
;
104 up
-= minsize
; /* point at most significant common limb */
105 vp
-= minsize
; /* point at most significant common limb */
107 /* Compare the most significant part which has explicit limbs for U and V. */
108 for (i
= minsize
- 1; i
> 0; i
--)
114 n_bits
-= (maxsize
- 1) * GMP_NUMB_BITS
;
116 size
= maxsize
- minsize
;
122 /* Now either U or V has its limbs consumed, i.e, continues with an
123 infinite number of implicit zero limbs. Check that the other operand
124 has just zeros in the corresponding, relevant part. */
131 for (i
= size
- 1; i
> 0; i
--)
141 /* Both U or V has its limbs consumed. */
143 diff
= up
[0] ^ vp
[0];
146 if (n_bits
< GMP_NUMB_BITS
)
147 diff
>>= GMP_NUMB_BITS
- n_bits
;