xref: /openbsd/lib/libm/src/ld80/s_nextafterl.c (revision 73471bf0)
1 /* @(#)s_nextafter.c 5.1 93/09/24 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 
13 /* IEEE functions
14  *	nextafterl(x,y)
15  *	return the next machine floating-point number of x in the
16  *	direction toward y.
17  *   Special cases:
18  */
19 
20 #include <math.h>
21 
22 #include "math_private.h"
23 
24 long double
25 nextafterl(long double x, long double y)
26 {
27 	int32_t hx,hy,ix,iy;
28 	u_int32_t lx,ly;
29 	int32_t esx,esy;
30 
31 	GET_LDOUBLE_WORDS(esx,hx,lx,x);
32 	GET_LDOUBLE_WORDS(esy,hy,ly,y);
33 	ix = esx&0x7fff;		/* |x| */
34 	iy = esy&0x7fff;		/* |y| */
35 
36 	if (((ix==0x7fff)&&(((hx&0x7fffffff)|lx)!=0)) ||   /* x is nan */
37 	    ((iy==0x7fff)&&(((hy&0x7fffffff)|ly)!=0)))     /* y is nan */
38 	   return x+y;
39 	if(x==y) return y;		/* x=y, return y */
40 	if((ix|hx|lx)==0) {			/* x == 0 */
41 	    volatile long double u;
42 	    SET_LDOUBLE_WORDS(x,esy&0x8000,0,1);/* return +-minsubnormal */
43 	    u = x;
44 	    u = u * u;				/* raise underflow flag */
45 	    return x;
46 	}
47 	if(esx>=0) {			/* x > 0 */
48 	    if(esx>esy||((esx==esy) && (hx>hy||((hx==hy)&&(lx>ly))))) {
49 	      /* x > y, x -= ulp */
50 		if(lx==0) {
51 		    if ((hx&0x7fffffff)==0) esx -= 1;
52 		    hx = (hx - 1) | (hx & 0x80000000);
53 		}
54 		lx -= 1;
55 	    } else {				/* x < y, x += ulp */
56 		lx += 1;
57 		if(lx==0) {
58 		    hx = (hx + 1) | (hx & 0x80000000);
59 		    if ((hx&0x7fffffff)==0) esx += 1;
60 		}
61 	    }
62 	} else {				/* x < 0 */
63 	    if(esy>=0||(esx>esy||((esx==esy)&&(hx>hy||((hx==hy)&&(lx>ly)))))){
64 	      /* x < y, x -= ulp */
65 		if(lx==0) {
66 		    if ((hx&0x7fffffff)==0) esx -= 1;
67 		    hx = (hx - 1) | (hx & 0x80000000);
68 		}
69 		lx -= 1;
70 	    } else {				/* x > y, x += ulp */
71 		lx += 1;
72 		if(lx==0) {
73 		    hx = (hx + 1) | (hx & 0x80000000);
74 		    if ((hx&0x7fffffff)==0) esx += 1;
75 		}
76 	    }
77 	}
78 	esy = esx&0x7fff;
79 	if(esy==0x7fff) return x+x;		/* overflow  */
80 	if(esy==0) {
81 	    volatile long double u = x*x;	/* underflow */
82 	    if(u==x) {
83 		SET_LDOUBLE_WORDS(x,esx,hx,lx);
84 		return x;
85 	    }
86 	}
87 	SET_LDOUBLE_WORDS(x,esx,hx,lx);
88 	return x;
89 }
90 DEF_STD(nextafterl);
91 MAKE_UNUSED_CLONE(nexttowardl, nextafterl);
92