Blame math/s_nextafter.c

Packit 6c4009
/* @(#)s_nextafter.c 5.1 93/09/24 */
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: s_nextafter.c,v 1.8 1995/05/10 20:47:58 jtc Exp $";
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* IEEE functions
Packit 6c4009
 *	nextafter(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
/* Ugly hack so that the aliasing works.  */
Packit 6c4009
#define __nexttoward __internal___nexttoward
Packit 6c4009
#define nexttoward __internal_nexttoward
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
#include <libm-alias-double.h>
Packit 6c4009
Packit 6c4009
double __nextafter(double x, double y)
Packit 6c4009
{
Packit 6c4009
	int32_t hx,hy,ix,iy;
Packit 6c4009
	uint32_t lx,ly;
Packit 6c4009
Packit 6c4009
	EXTRACT_WORDS(hx,lx,x);
Packit 6c4009
	EXTRACT_WORDS(hy,ly,y);
Packit 6c4009
	ix = hx&0x7fffffff;		/* |x| */
Packit 6c4009
	iy = hy&0x7fffffff;		/* |y| */
Packit 6c4009
Packit 6c4009
	if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
Packit 6c4009
	   ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0))     /* y is nan */
Packit 6c4009
	   return x+y;
Packit 6c4009
	if(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,hy&0x80000000,1);	/* return +-minsubnormal */
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(hx>hy||((hx==hy)&&(lx>ly))) {	/* 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(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* 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
libm_alias_double (__nextafter, nextafter)
Packit 6c4009
#ifdef NO_LONG_DOUBLE
Packit 6c4009
strong_alias (__nextafter, __nexttowardl)
Packit 6c4009
weak_alias (__nexttowardl, nexttowardl)
Packit 6c4009
#undef __nexttoward
Packit 6c4009
strong_alias (__nextafter, __nexttoward)
Packit 6c4009
#undef nexttoward
Packit 6c4009
weak_alias (__nextafter, nexttoward)
Packit 6c4009
#endif