Blame sysdeps/i386/fpu/s_nexttoward.c

Packit 6c4009
/* s_nexttoward.c
Packit 6c4009
 * Special i387 version
Packit 6c4009
 * Conversion from s_nextafter.c by Ulrich Drepper, Cygnus Support,
Packit 6c4009
 * drepper@cygnus.com.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * ====================================================
Packit 6c4009
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Developed at SunPro, a Sun Microsystems, Inc. business.
Packit 6c4009
 * Permission to use, copy, modify, and distribute this
Packit 6c4009
 * software is freely granted, provided that this notice
Packit 6c4009
 * is preserved.
Packit 6c4009
 * ====================================================
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#if defined(LIBM_SCCS) && !defined(lint)
Packit 6c4009
static char rcsid[] = "$NetBSD: $";
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* IEEE functions
Packit 6c4009
 *	nexttoward(x,y)
Packit 6c4009
 *	return the next machine floating-point number of x in the
Packit 6c4009
 *	direction toward y.
Packit 6c4009
 *   Special cases:
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math-barriers.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <float.h>
Packit 6c4009
Packit 6c4009
double __nexttoward(double x, long double y)
Packit 6c4009
{
Packit 6c4009
	int32_t hx,ix,iy;
Packit 6c4009
	uint32_t lx,hy,ly,esy;
Packit 6c4009
Packit 6c4009
	EXTRACT_WORDS(hx,lx,x);
Packit 6c4009
	GET_LDOUBLE_WORDS(esy,hy,ly,y);
Packit 6c4009
	ix = hx&0x7fffffff;		/* |x| */
Packit 6c4009
	iy = esy&0x7fff;		/* |y| */
Packit 6c4009
Packit 6c4009
	/* Intel's extended format has the normally implicit 1 explicit
Packit 6c4009
	   present.  Sigh!  */
Packit 6c4009
	if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
Packit 6c4009
	   ((iy>=0x7fff)&&((hy&0x7fffffff)|ly)!=0))        /* y is nan */
Packit 6c4009
	   return x+y;
Packit 6c4009
	if((long double) x==y) return y;	/* x=y, return y */
Packit 6c4009
	if((ix|lx)==0) {			/* x == 0 */
Packit 6c4009
	    double u;
Packit 6c4009
	    INSERT_WORDS(x,(esy&0x8000)<<16,1); /* return +-minsub */
Packit 6c4009
	    u = math_opt_barrier (x);
Packit 6c4009
	    u = u * u;
Packit 6c4009
	    math_force_eval (u);		/* raise underflow flag */
Packit 6c4009
	    return x;
Packit 6c4009
	}
Packit 6c4009
	if(hx>=0) {				/* x > 0 */
Packit 6c4009
	    if (x > y) {			/* x -= ulp */
Packit 6c4009
		if(lx==0) hx -= 1;
Packit 6c4009
		lx -= 1;
Packit 6c4009
	    } else {				/* x < y, x += ulp */
Packit 6c4009
		lx += 1;
Packit 6c4009
		if(lx==0) hx += 1;
Packit 6c4009
	    }
Packit 6c4009
	} else {				/* x < 0 */
Packit 6c4009
	    if (x < y) {			/* x -= ulp */
Packit 6c4009
		if(lx==0) hx -= 1;
Packit 6c4009
		lx -= 1;
Packit 6c4009
	    } else {				/* x > y, x += ulp */
Packit 6c4009
		lx += 1;
Packit 6c4009
		if(lx==0) hx += 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
	hy = hx&0x7ff00000;
Packit 6c4009
	if(hy>=0x7ff00000) {
Packit 6c4009
	  double u = x+x;			/* overflow  */
Packit 6c4009
	  math_force_eval (u);
Packit 6c4009
	  __set_errno (ERANGE);
Packit 6c4009
	}
Packit 6c4009
	if(hy<0x00100000) {
Packit 6c4009
	    double u = x*x;			/* underflow */
Packit 6c4009
	    math_force_eval (u);		/* raise underflow flag */
Packit 6c4009
	    __set_errno (ERANGE);
Packit 6c4009
	}
Packit 6c4009
	INSERT_WORDS(x,hx,lx);
Packit 6c4009
	return x;
Packit 6c4009
}
Packit 6c4009
weak_alias (__nexttoward, nexttoward)
Packit 6c4009
#ifdef NO_LONG_DOUBLE
Packit 6c4009
strong_alias (__nexttoward, __nexttowardl)
Packit 6c4009
weak_alias (__nexttoward, nexttowardl)
Packit 6c4009
#endif