sbin/mount_hammer: Use calloc(3) and cleanups
[dragonfly.git] / contrib / gmp / mpf / eq.c
blobcdbbcb96da4cacaffc8c6475115461edcf402f71
1 /* mpf_eq -- Compare two floats up to a specified bit #.
3 Copyright 1993, 1995, 1996, 2001, 2002, 2008, 2009 Free Software Foundation,
4 Inc.
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/. */
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 #include "longlong.h"
25 int
26 mpf_eq (mpf_srcptr u, mpf_srcptr v, mp_bitcnt_t n_bits)
28 mp_srcptr up, vp, p;
29 mp_size_t usize, vsize, minsize, maxsize, n_limbs, i, size;
30 mp_exp_t uexp, vexp;
31 mp_limb_t diff;
32 int cnt;
34 uexp = u->_mp_exp;
35 vexp = v->_mp_exp;
37 usize = u->_mp_size;
38 vsize = v->_mp_size;
40 /* 1. Are the signs different? */
41 if ((usize ^ vsize) >= 0)
43 /* U and V are both non-negative or both negative. */
44 if (usize == 0)
45 return vsize == 0;
46 if (vsize == 0)
47 return 0;
49 /* Fall out. */
51 else
53 /* Either U or V is negative, but not both. */
54 return 0;
57 /* U and V have the same sign and are both non-zero. */
59 /* 2. Are the exponents different? */
60 if (uexp != vexp)
61 return 0;
63 usize = ABS (usize);
64 vsize = ABS (vsize);
66 up = u->_mp_d;
67 vp = v->_mp_d;
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);
82 #if 0
83 /* Ignore zeros at the low end of U and V. */
84 while (up[0] == 0)
85 up++, usize--;
86 while (vp[0] == 0)
87 vp++, vsize--;
88 #endif
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--)
99 if (up[i] != vp[i])
100 return 0;
103 n_bits -= (maxsize - 1) * GMP_NUMB_BITS;
105 size = maxsize - minsize;
106 if (size != 0)
108 if (up[0] != vp[0])
109 return 0;
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. */
115 if (usize > vsize)
116 p = up - size;
117 else
118 p = vp - size;
120 for (i = size - 1; i > 0; i--)
122 if (p[i] != 0)
123 return 0;
126 diff = p[0];
128 else
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;
138 return diff == 0;