xref: /openbsd/lib/libm/src/ld80/s_nextafterl.c (revision cca36db2)
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 <sys/cdefs.h>
21 #include <math.h>
22 
23 #include "math_private.h"
24 
25 long double
26 nextafterl(long double x, long double y)
27 {
28 	int32_t hx,hy,ix,iy;
29 	u_int32_t lx,ly,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|lx)!=0)) ||   /* x is nan */
37 	    ((iy==0x7fff)&&((hy|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<0x8000) {			/* x > 0 */
48 	    if(ix>iy||((ix==iy) && (hx>hy||((hx==hy)&&(lx>ly))))) {
49 	      /* x > y, x -= ulp */
50 		if(lx==0) {
51 		    if (hx==0) esx -= 1;
52 		    hx -= 1;
53 		}
54 		lx -= 1;
55 	    } else {				/* x < y, x += ulp */
56 		lx += 1;
57 		if(lx==0) {
58 		    hx += 1;
59 		    if (hx==0)
60 			esx += 1;
61 		}
62 	    }
63 	} else {				/* x < 0 */
64 	    if(esy>=0||(ix>iy||((ix==iy)&&(hx>hy||((hx==hy)&&(lx>ly)))))){
65 	      /* x < y, x -= ulp */
66 		if(lx==0) {
67 		    if (hx==0) esx -= 1;
68 		    hx -= 1;
69 		}
70 		lx -= 1;
71 	    } else {				/* x > y, x += ulp */
72 		lx += 1;
73 		if(lx==0) {
74 		    hx += 1;
75 		    if (hx==0) esx += 1;
76 		}
77 	    }
78 	}
79 	esy = esx&0x7fff;
80 	if(esy==0x7fff) return x+x;		/* overflow  */
81 	if(esy==0) {
82 	    volatile long double u = x*x;	/* underflow */
83 	}
84 	SET_LDOUBLE_WORDS(x,esx,hx,lx);
85 	return x;
86 }
87 
88 __weak_alias(nexttowardl, nextafterl);
89