xref: /dflybsd-src/contrib/mpfr/src/eq.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_eq -- Compare two floats up to a specified bit #.
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 1999, 2001, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino 
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino 
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino 
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino 
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino 
234a238c70SJohn Marino 
244a238c70SJohn Marino #include "mpfr-impl.h"
254a238c70SJohn Marino 
264a238c70SJohn Marino /* return non-zero if the first n_bits bits of u, v are equal,
274a238c70SJohn Marino    0 otherwise */
284a238c70SJohn Marino int
mpfr_eq(mpfr_srcptr u,mpfr_srcptr v,unsigned long int n_bits)294a238c70SJohn Marino mpfr_eq (mpfr_srcptr u, mpfr_srcptr v, unsigned long int n_bits)
304a238c70SJohn Marino {
314a238c70SJohn Marino   mpfr_limb_srcptr up, vp;
324a238c70SJohn Marino   mp_size_t usize, vsize, size, i;
334a238c70SJohn Marino   mpfr_exp_t uexp, vexp;
344a238c70SJohn Marino   int k;
354a238c70SJohn Marino 
364a238c70SJohn Marino   if (MPFR_ARE_SINGULAR(u, v))
374a238c70SJohn Marino     {
384a238c70SJohn Marino       if (MPFR_IS_NAN(u) || MPFR_IS_NAN(v))
394a238c70SJohn Marino         return 0; /* non equal */
404a238c70SJohn Marino       else if (MPFR_IS_INF(u) && MPFR_IS_INF(v))
414a238c70SJohn Marino         return (MPFR_SIGN(u) == MPFR_SIGN(v));
424a238c70SJohn Marino       else if (MPFR_IS_ZERO(u) && MPFR_IS_ZERO(v))
434a238c70SJohn Marino         return 1;
444a238c70SJohn Marino       else
454a238c70SJohn Marino         return 0;
464a238c70SJohn Marino     }
474a238c70SJohn Marino 
484a238c70SJohn Marino   /* 1. Are the signs different?  */
494a238c70SJohn Marino   if (MPFR_SIGN(u) != MPFR_SIGN(v))
504a238c70SJohn Marino     return 0;
514a238c70SJohn Marino 
524a238c70SJohn Marino   uexp = MPFR_GET_EXP (u);
534a238c70SJohn Marino   vexp = MPFR_GET_EXP (v);
544a238c70SJohn Marino 
554a238c70SJohn Marino   /* 2. Are the exponents different?  */
564a238c70SJohn Marino   if (uexp != vexp)
574a238c70SJohn Marino     return 0; /* no bit agree */
584a238c70SJohn Marino 
59*ab6d115fSJohn Marino   usize = MPFR_LIMB_SIZE (u);
60*ab6d115fSJohn Marino   vsize = MPFR_LIMB_SIZE (v);
614a238c70SJohn Marino 
624a238c70SJohn Marino   if (vsize > usize) /* exchange u and v */
634a238c70SJohn Marino     {
644a238c70SJohn Marino       up = MPFR_MANT(v);
654a238c70SJohn Marino       vp = MPFR_MANT(u);
664a238c70SJohn Marino       size = vsize;
674a238c70SJohn Marino       vsize = usize;
684a238c70SJohn Marino       usize = size;
694a238c70SJohn Marino     }
704a238c70SJohn Marino   else
714a238c70SJohn Marino     {
724a238c70SJohn Marino       up = MPFR_MANT(u);
734a238c70SJohn Marino       vp = MPFR_MANT(v);
744a238c70SJohn Marino     }
754a238c70SJohn Marino 
764a238c70SJohn Marino   /* now usize >= vsize */
774a238c70SJohn Marino   MPFR_ASSERTD(usize >= vsize);
784a238c70SJohn Marino 
794a238c70SJohn Marino   if (usize > vsize)
804a238c70SJohn Marino     {
814a238c70SJohn Marino       if ((unsigned long) vsize * GMP_NUMB_BITS < n_bits)
824a238c70SJohn Marino         {
834a238c70SJohn Marino           /* check if low min(PREC(u), n_bits) - (vsize * GMP_NUMB_BITS)
844a238c70SJohn Marino              bits from u are non-zero */
854a238c70SJohn Marino           unsigned long remains = n_bits - (vsize * GMP_NUMB_BITS);
864a238c70SJohn Marino           k = usize - vsize - 1;
874a238c70SJohn Marino           while (k >= 0 && remains >= GMP_NUMB_BITS && !up[k])
884a238c70SJohn Marino             {
894a238c70SJohn Marino               k--;
904a238c70SJohn Marino               remains -= GMP_NUMB_BITS;
914a238c70SJohn Marino             }
924a238c70SJohn Marino           /* now either k < 0: all low bits from u are zero
934a238c70SJohn Marino                  or remains < GMP_NUMB_BITS: check high bits from up[k]
944a238c70SJohn Marino                  or up[k] <> 0: different */
954a238c70SJohn Marino           if (k >= 0 && (((remains < GMP_NUMB_BITS) &&
964a238c70SJohn Marino                           (up[k] >> (GMP_NUMB_BITS - remains))) ||
974a238c70SJohn Marino                          (remains >= GMP_NUMB_BITS && up[k])))
984a238c70SJohn Marino             return 0;           /* surely too different */
994a238c70SJohn Marino         }
1004a238c70SJohn Marino       size = vsize;
1014a238c70SJohn Marino     }
1024a238c70SJohn Marino   else
1034a238c70SJohn Marino     {
1044a238c70SJohn Marino       size = usize;
1054a238c70SJohn Marino     }
1064a238c70SJohn Marino 
1074a238c70SJohn Marino   /* now size = min (usize, vsize) */
1084a238c70SJohn Marino 
1094a238c70SJohn Marino   /* If size is too large wrt n_bits, reduce it to look only at the
1104a238c70SJohn Marino      high n_bits bits.
1114a238c70SJohn Marino      Otherwise, if n_bits > size * GMP_NUMB_BITS, reduce n_bits to
1124a238c70SJohn Marino      size * GMP_NUMB_BITS, since the extra low bits of one of the
1134a238c70SJohn Marino      operands have already been check above. */
1144a238c70SJohn Marino   if ((unsigned long) size > 1 + (n_bits - 1) / GMP_NUMB_BITS)
1154a238c70SJohn Marino     size = 1 + (n_bits - 1) / GMP_NUMB_BITS;
1164a238c70SJohn Marino   else if (n_bits > (unsigned long) size * GMP_NUMB_BITS)
1174a238c70SJohn Marino     n_bits = size * GMP_NUMB_BITS;
1184a238c70SJohn Marino 
1194a238c70SJohn Marino   up += usize - size;
1204a238c70SJohn Marino   vp += vsize - size;
1214a238c70SJohn Marino 
1224a238c70SJohn Marino   for (i = size - 1; i > 0 && n_bits >= GMP_NUMB_BITS; i--)
1234a238c70SJohn Marino     {
1244a238c70SJohn Marino       if (up[i] != vp[i])
1254a238c70SJohn Marino         return 0;
1264a238c70SJohn Marino       n_bits -= GMP_NUMB_BITS;
1274a238c70SJohn Marino     }
1284a238c70SJohn Marino 
1294a238c70SJohn Marino   /* now either i=0 or n_bits<GMP_NUMB_BITS */
1304a238c70SJohn Marino 
1314a238c70SJohn Marino   /* since n_bits <= size * GMP_NUMB_BITS before the above for-loop,
1324a238c70SJohn Marino      we have the invariant n_bits <= (i+1) * GMP_NUMB_BITS, thus
1334a238c70SJohn Marino      we always have n_bits <= GMP_NUMB_BITS here */
1344a238c70SJohn Marino   MPFR_ASSERTD(n_bits <= GMP_NUMB_BITS);
1354a238c70SJohn Marino 
1364a238c70SJohn Marino   if (n_bits & (GMP_NUMB_BITS - 1))
1374a238c70SJohn Marino     return (up[i] >> (GMP_NUMB_BITS - (n_bits & (GMP_NUMB_BITS - 1))) ==
1384a238c70SJohn Marino             vp[i] >> (GMP_NUMB_BITS - (n_bits & (GMP_NUMB_BITS - 1))));
1394a238c70SJohn Marino   else
1404a238c70SJohn Marino     return (up[i] == vp[i]);
1414a238c70SJohn Marino }
142