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