Blame sysdeps/ieee754/ldbl-128ibm/s_nextupl.c

Packit 6c4009
/* Return the least floating-point number greater than X.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <float.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math_ldbl_opt.h>
Packit 6c4009
Packit 6c4009
/* Return the least floating-point number greater than X.  */
Packit 6c4009
long double
Packit 6c4009
__nextupl (long double x)
Packit 6c4009
{
Packit 6c4009
  int64_t hx, ihx, lx;
Packit 6c4009
  double xhi, xlo, yhi;
Packit 6c4009
Packit 6c4009
  ldbl_unpack (x, &xhi, &xlo;;
Packit 6c4009
  EXTRACT_WORDS64 (hx, xhi);
Packit 6c4009
  EXTRACT_WORDS64 (lx, xlo);
Packit 6c4009
  ihx = hx & 0x7fffffffffffffffLL;
Packit 6c4009
Packit 6c4009
  if (ihx > 0x7ff0000000000000LL)	/* x is nan.  */
Packit 6c4009
    return x + x;		/* Signal the nan.  */
Packit 6c4009
  if (ihx == 0)
Packit 6c4009
    return LDBL_TRUE_MIN;
Packit 6c4009
Packit 6c4009
  long double u;
Packit 6c4009
  if ((hx == 0x7fefffffffffffffLL) && (lx == 0x7c8ffffffffffffeLL))
Packit 6c4009
    return INFINITY;
Packit 6c4009
  if ((uint64_t) hx >= 0xfff0000000000000ULL)
Packit 6c4009
    {
Packit 6c4009
      u = -0x1.fffffffffffff7ffffffffffff8p+1023L;
Packit 6c4009
      return u;
Packit 6c4009
    }
Packit 6c4009
  if (ihx <= 0x0360000000000000LL)
Packit 6c4009
    {				/* x <= LDBL_MIN.  */
Packit 6c4009
      x += LDBL_TRUE_MIN;
Packit 6c4009
      if (x == 0.0L)		/* Handle negative LDBL_TRUE_MIN case.  */
Packit 6c4009
        x = -0.0L;
Packit 6c4009
      return x;
Packit 6c4009
    }
Packit 6c4009
  /* If the high double is an exact power of two and the low
Packit 6c4009
     double is the opposite sign, then 1ulp is one less than
Packit 6c4009
     what we might determine from the high double.  Similarly
Packit 6c4009
     if X is an exact power of two, and negative, because
Packit 6c4009
     making it a little larger will result in the exponent
Packit 6c4009
     decreasing by one and normalisation of the mantissa.   */
Packit 6c4009
  if ((hx & 0x000fffffffffffffLL) == 0
Packit 6c4009
      && ((lx != 0 && lx != 0x8000000000000000LL && (hx ^ lx) < 0)
Packit 6c4009
          || ((lx == 0 || lx == 0x8000000000000000LL) && hx < 0)))
Packit 6c4009
    ihx -= 1LL << 52;
Packit 6c4009
  if (ihx < (106LL << 52))
Packit 6c4009
    {				/* ulp will denormal.  */
Packit 6c4009
      INSERT_WORDS64 (yhi, ihx & (0x7ffLL << 52));
Packit 6c4009
      u = yhi * 0x1p-105;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      INSERT_WORDS64 (yhi, (ihx & (0x7ffLL << 52)) - (105LL << 52));
Packit 6c4009
      u = yhi;
Packit 6c4009
    }
Packit 6c4009
  return x + u;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__nextupl, nextupl)