Blame mpf/cmp.c

Packit 5c3484
/* mpf_cmp -- Compare two floats.
Packit 5c3484
Packit 5c3484
Copyright 1993, 1994, 1996, 2001, 2015 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
mpf_cmp (mpf_srcptr u, mpf_srcptr v) __GMP_NOTHROW
Packit 5c3484
{
Packit 5c3484
  mp_srcptr up, vp;
Packit 5c3484
  mp_size_t usize, vsize;
Packit 5c3484
  mp_exp_t uexp, vexp;
Packit 5c3484
  int cmp;
Packit 5c3484
  int usign;
Packit 5c3484
Packit 5c3484
  usize = SIZ(u);
Packit 5c3484
  vsize = SIZ(v);
Packit 5c3484
  usign = usize >= 0 ? 1 : -1;
Packit 5c3484
Packit 5c3484
  /* 1. Are the signs different?  */
Packit 5c3484
  if ((usize ^ vsize) >= 0)
Packit 5c3484
    {
Packit 5c3484
      /* U and V are both non-negative or both negative.  */
Packit 5c3484
      if (usize == 0)
Packit 5c3484
	/* vsize >= 0 */
Packit 5c3484
	return -(vsize != 0);
Packit 5c3484
      if (vsize == 0)
Packit 5c3484
	/* usize >= 0 */
Packit 5c3484
	return usize != 0;
Packit 5c3484
      /* Fall out.  */
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      /* Either U or V is negative, but not both.  */
Packit 5c3484
      return usign;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  /* U and V have the same sign and are both non-zero.  */
Packit 5c3484
Packit 5c3484
  uexp = EXP(u);
Packit 5c3484
  vexp = EXP(v);
Packit 5c3484
Packit 5c3484
  /* 2. Are the exponents different?  */
Packit 5c3484
  if (uexp > vexp)
Packit 5c3484
    return usign;
Packit 5c3484
  if (uexp < vexp)
Packit 5c3484
    return -usign;
Packit 5c3484
Packit 5c3484
  usize = ABS (usize);
Packit 5c3484
  vsize = ABS (vsize);
Packit 5c3484
Packit 5c3484
  up = PTR (u);
Packit 5c3484
  vp = PTR (v);
Packit 5c3484
Packit 5c3484
#define STRICT_MPF_NORMALIZATION 0
Packit 5c3484
#if ! STRICT_MPF_NORMALIZATION
Packit 5c3484
  /* Ignore zeroes at the low end of U and V.  */
Packit 5c3484
  do {
Packit 5c3484
    mp_limb_t tl;
Packit 5c3484
    tl = up[0];
Packit 5c3484
    MPN_STRIP_LOW_ZEROS_NOT_ZERO (up, usize, tl);
Packit 5c3484
    tl = vp[0];
Packit 5c3484
    MPN_STRIP_LOW_ZEROS_NOT_ZERO (vp, vsize, tl);
Packit 5c3484
  } while (0);
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
  if (usize > vsize)
Packit 5c3484
    {
Packit 5c3484
      cmp = mpn_cmp (up + usize - vsize, vp, vsize);
Packit 5c3484
      /* if (cmp == 0) */
Packit 5c3484
      /*	return usign; */
Packit 5c3484
      ++cmp;
Packit 5c3484
    }
Packit 5c3484
  else if (vsize > usize)
Packit 5c3484
    {
Packit 5c3484
      cmp = mpn_cmp (up, vp + vsize - usize, usize);
Packit 5c3484
      /* if (cmp == 0) */
Packit 5c3484
      /*	return -usign; */
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      cmp = mpn_cmp (up, vp, usize);
Packit 5c3484
      if (cmp == 0)
Packit 5c3484
	return 0;
Packit 5c3484
    }
Packit 5c3484
  return cmp > 0 ? usign : -usign;
Packit 5c3484
}