Blame sysdeps/ieee754/ldbl-96/s_remquol.c

Packit 6c4009
/* Compute remainder and a congruent to the quotient.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <libm-alias-ldouble.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static const long double zero = 0.0;
Packit 6c4009
Packit 6c4009
Packit 6c4009
long double
Packit 6c4009
__remquol (long double x, long double p, int *quo)
Packit 6c4009
{
Packit 6c4009
  int32_t ex,ep,hx,hp;
Packit 6c4009
  uint32_t sx,lx,lp;
Packit 6c4009
  int cquo,qs;
Packit 6c4009
Packit 6c4009
  GET_LDOUBLE_WORDS (ex, hx, lx, x);
Packit 6c4009
  GET_LDOUBLE_WORDS (ep, hp, lp, p);
Packit 6c4009
  sx = ex & 0x8000;
Packit 6c4009
  qs = (sx ^ (ep & 0x8000)) >> 15;
Packit 6c4009
  ep &= 0x7fff;
Packit 6c4009
  ex &= 0x7fff;
Packit 6c4009
Packit 6c4009
  /* Purge off exception values.  */
Packit 6c4009
  if ((ep | hp | lp) == 0)
Packit 6c4009
    return (x * p) / (x * p); 			/* p = 0 */
Packit 6c4009
  if ((ex == 0x7fff)				/* x not finite */
Packit 6c4009
      || ((ep == 0x7fff)			/* p is NaN */
Packit 6c4009
	  && (((hp & 0x7fffffff) | lp) != 0)))
Packit 6c4009
    return (x * p) / (x * p);
Packit 6c4009
Packit 6c4009
  if (ep <= 0x7ffb)
Packit 6c4009
    x = __ieee754_fmodl (x, 8 * p);		/* now x < 8p */
Packit 6c4009
Packit 6c4009
  if (((ex - ep) | (hx - hp) | (lx - lp)) == 0)
Packit 6c4009
    {
Packit 6c4009
      *quo = qs ? -1 : 1;
Packit 6c4009
      return zero * x;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  x  = fabsl (x);
Packit 6c4009
  p  = fabsl (p);
Packit 6c4009
  cquo = 0;
Packit 6c4009
Packit 6c4009
  if (ep <= 0x7ffc && x >= 4 * p)
Packit 6c4009
    {
Packit 6c4009
      x -= 4 * p;
Packit 6c4009
      cquo += 4;
Packit 6c4009
    }
Packit 6c4009
  if (ep <= 0x7ffd && x >= 2 * p)
Packit 6c4009
    {
Packit 6c4009
      x -= 2 * p;
Packit 6c4009
      cquo += 2;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ep < 0x0002)
Packit 6c4009
    {
Packit 6c4009
      if (x + x > p)
Packit 6c4009
	{
Packit 6c4009
	  x -= p;
Packit 6c4009
	  ++cquo;
Packit 6c4009
	  if (x + x >= p)
Packit 6c4009
	    {
Packit 6c4009
	      x -= p;
Packit 6c4009
	      ++cquo;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      long double p_half = 0.5 * p;
Packit 6c4009
      if (x > p_half)
Packit 6c4009
	{
Packit 6c4009
	  x -= p;
Packit 6c4009
	  ++cquo;
Packit 6c4009
	  if (x >= p_half)
Packit 6c4009
	    {
Packit 6c4009
	      x -= p;
Packit 6c4009
	      ++cquo;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  *quo = qs ? -cquo : cquo;
Packit 6c4009
Packit 6c4009
  /* Ensure correct sign of zero result in round-downward mode.  */
Packit 6c4009
  if (x == 0.0L)
Packit 6c4009
    x = 0.0L;
Packit 6c4009
  if (sx)
Packit 6c4009
    x = -x;
Packit 6c4009
  return x;
Packit 6c4009
}
Packit 6c4009
libm_alias_ldouble (__remquo, remquo)