1/* s_nexttoward.c
2 * Special i387 version
3 * Conversion from s_nextafter.c by Ulrich Drepper, Cygnus Support,
4 * drepper@cygnus.com.
5 */
6
7/*
8 * ====================================================
9 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
10 *
11 * Developed at SunPro, a Sun Microsystems, Inc. business.
12 * Permission to use, copy, modify, and distribute this
13 * software is freely granted, provided that this notice
14 * is preserved.
15 * ====================================================
16 */
17
18#if defined(LIBM_SCCS) && !defined(lint)
19static char rcsid[] = "$NetBSD: $";
20#endif
21
22/* IEEE functions
23 *	nexttoward(x,y)
24 *	return the next machine floating-point number of x in the
25 *	direction toward y.
26 *   Special cases:
27 */
28
29#include "math.h"
30#include "math_private.h"
31
32#ifdef __STDC__
33	double __nexttoward(double x, long double y)
34#else
35	double __nexttoward(x,y)
36	double x;
37	long double y;
38#endif
39{
40	int32_t hx,ix,iy;
41	u_int32_t lx,hy,ly,esy;
42
43	EXTRACT_WORDS(hx,lx,x);
44	GET_LDOUBLE_WORDS(esy,hy,ly,y);
45	ix = hx&0x7fffffff;		/* |x| */
46	iy = esy&0x7fff;		/* |y| */
47
48	/* Intel's extended format has the normally implicit 1 explicit
49	   present.  Sigh!  */
50	if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
51	   ((iy>=0x7fff)&&((hy&0x7fffffff)|ly)!=0))        /* y is nan */
52	   return x+y;
53	if((long double) x==y) return y;	/* x=y, return y */
54	if((ix|lx)==0) {			/* x == 0 */
55	    double x2;
56	    INSERT_WORDS(x,(esy&0x8000)<<16,1); /* return +-minsub */
57	    x2 = x*x;
58	    if(x2==x) return x2; else return x;	/* raise underflow flag */
59	}
60	if(hx>=0) {				/* x > 0 */
61	    if (esy>=0x8000||((ix>>20)&0x7ff)>iy-0x3c00
62		|| (((ix>>20)&0x7ff)==iy-0x3c00
63		    && (((hx<<11)|(lx>>21))>(hy&0x7fffffff)
64			|| (((hx<<11)|(lx>>21))==(hy&0x7fffffff)
65			    && (lx<<11)>ly)))) {	/* 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	} else {				/* x < 0 */
73	    if (esy<0x8000||((ix>>20)&0x7ff)>iy-0x3c00
74		|| (((ix>>20)&0x7ff)==iy-0x3c00
75		    && (((hx<<11)|(lx>>21))>(hy&0x7fffffff)
76			|| (((hx<<11)|(lx>>21))==(hy&0x7fffffff)
77			    && (lx<<11)>ly))))	{/* x < y, x -= ulp */
78		if(lx==0) hx -= 1;
79		lx -= 1;
80	    } else {				/* x > y, x += ulp */
81		lx += 1;
82		if(lx==0) hx += 1;
83	    }
84	}
85	hy = hx&0x7ff00000;
86	if(hy>=0x7ff00000) {
87	  x = x+x;	/* overflow  */
88	  /* Force conversion to double.  */
89	  asm ("" : "=m"(x) : "m"(x));
90	  return x;
91	}
92	if(hy<0x00100000) {		/* underflow */
93	    double x2 = x*x;
94	    if(x2!=x) {		/* raise underflow flag */
95	        INSERT_WORDS(x2,hx,lx);
96		return x2;
97	    }
98	}
99	INSERT_WORDS(x,hx,lx);
100	return x;
101}
102weak_alias (__nexttoward, nexttoward)
103#ifdef NO_LONG_DOUBLE
104strong_alias (__nexttoward, __nexttowardl)
105weak_alias (__nexttoward, nexttowardl)
106#endif
107