xref: /freebsd/lib/msun/src/s_nextafterl.c (revision 61e21613)
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11 
12 #include <sys/cdefs.h>
13 /* IEEE functions
14  *	nextafter(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 <float.h>
21 
22 #include "fpmath.h"
23 #include "math.h"
24 #include "math_private.h"
25 
26 #if LDBL_MAX_EXP != 0x4000
27 #error "Unsupported long double format"
28 #endif
29 
30 long double
31 nextafterl(long double x, long double y)
32 {
33 	volatile long double t;
34 	union IEEEl2bits ux, uy;
35 
36 	ux.e = x;
37 	uy.e = y;
38 
39 	if ((ux.bits.exp == 0x7fff &&
40 	     ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl) != 0) ||
41 	    (uy.bits.exp == 0x7fff &&
42 	     ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
43 	   return x+y;	/* x or y is nan */
44 	if(x==y) return y;		/* x=y, return y */
45 	if(x==0.0) {
46 	    ux.bits.manh = 0;			/* return +-minsubnormal */
47 	    ux.bits.manl = 1;
48 	    ux.bits.sign = uy.bits.sign;
49 	    t = ux.e*ux.e;
50 	    if(t==ux.e) return t; else return ux.e; /* raise underflow flag */
51 	}
52 	if(x>0.0 ^ x<y) {			/* x -= ulp */
53 	    if(ux.bits.manl==0) {
54 		if ((ux.bits.manh&~LDBL_NBIT)==0)
55 		    ux.bits.exp -= 1;
56 		ux.bits.manh = (ux.bits.manh - 1) | (ux.bits.manh & LDBL_NBIT);
57 	    }
58 	    ux.bits.manl -= 1;
59 	} else {				/* x += ulp */
60 	    ux.bits.manl += 1;
61 	    if(ux.bits.manl==0) {
62 		ux.bits.manh = (ux.bits.manh + 1) | (ux.bits.manh & LDBL_NBIT);
63 		if ((ux.bits.manh&~LDBL_NBIT)==0)
64 		    ux.bits.exp += 1;
65 	    }
66 	}
67 	if(ux.bits.exp==0x7fff) return x+x;	/* overflow  */
68 	if(ux.bits.exp==0) {			/* underflow */
69 	    mask_nbit_l(ux);
70 	    t = ux.e * ux.e;
71 	    if(t!=ux.e)			/* raise underflow flag */
72 		return ux.e;
73 	}
74 	return ux.e;
75 }
76 
77 __strong_reference(nextafterl, nexttowardl);
78