(root)/
glibc-2.38/
sysdeps/
ieee754/
ldbl-96/
s_nexttoward.c
       1  /* s_nexttoward.c
       2   * Conversion from s_nextafter.c by Ulrich Drepper, Cygnus Support,
       3   * drepper@cygnus.com.
       4   */
       5  
       6  /*
       7   * ====================================================
       8   * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
       9   *
      10   * Developed at SunPro, a Sun Microsystems, Inc. business.
      11   * Permission to use, copy, modify, and distribute this
      12   * software is freely granted, provided that this notice
      13   * is preserved.
      14   * ====================================================
      15   */
      16  
      17  #if defined(LIBM_SCCS) && !defined(lint)
      18  static char rcsid[] = "$NetBSD: $";
      19  #endif
      20  
      21  /* IEEE functions
      22   *	nexttoward(x,y)
      23   *	return the next machine floating-point number of x in the
      24   *	direction toward y.
      25   *   Special cases:
      26   */
      27  
      28  #include <errno.h>
      29  #include <math.h>
      30  #include <math-barriers.h>
      31  #include <math_private.h>
      32  #include <float.h>
      33  
      34  double __nexttoward(double x, long double y)
      35  {
      36  	int32_t hx,ix,iy;
      37  	uint32_t lx,hy,ly,esy;
      38  
      39  	EXTRACT_WORDS(hx,lx,x);
      40  	GET_LDOUBLE_WORDS(esy,hy,ly,y);
      41  	ix = hx&0x7fffffff;		/* |x| */
      42  	iy = esy&0x7fff;		/* |y| */
      43  
      44  	if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
      45  	   ((iy>=0x7fff)&&(hy|ly)!=0))        /* y is nan */
      46  	   return x+y;
      47  	if((long double) x==y) return y;	/* x=y, return y */
      48  	if((ix|lx)==0) {			/* x == 0 */
      49  	    double u;
      50  	    INSERT_WORDS(x,(esy&0x8000)<<16,1); /* return +-minsub */
      51  	    u = math_opt_barrier (x);
      52  	    u = u * u;
      53  	    math_force_eval (u);		/* raise underflow flag */
      54  	    return x;
      55  	}
      56  	if(hx>=0) {				/* x > 0 */
      57  	    if (x > y) {			/* x -= ulp */
      58  		if(lx==0) hx -= 1;
      59  		lx -= 1;
      60  	    } else {				/* x < y, x += ulp */
      61  		lx += 1;
      62  		if(lx==0) hx += 1;
      63  	    }
      64  	} else {				/* x < 0 */
      65  	    if (x < y) {			/* x -= ulp */
      66  		if(lx==0) hx -= 1;
      67  		lx -= 1;
      68  	    } else {				/* x > y, x += ulp */
      69  		lx += 1;
      70  		if(lx==0) hx += 1;
      71  	    }
      72  	}
      73  	hy = hx&0x7ff00000;
      74  	if(hy>=0x7ff00000) {
      75  	  double u = x+x;			/* overflow  */
      76  	  math_force_eval (u);
      77  	  __set_errno (ERANGE);
      78  	}
      79  	if(hy<0x00100000) {
      80  	    double u = x*x;			/* underflow */
      81  	    math_force_eval (u);		/* raise underflow flag */
      82  	    __set_errno (ERANGE);
      83  	}
      84  	INSERT_WORDS(x,hx,lx);
      85  	return x;
      86  }
      87  weak_alias (__nexttoward, nexttoward)