1c6d537c4a5430f68a14f2868f259525c45a8970
[libm] / src / math / nextafterl.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_nextafterl.c */
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 /* IEEE functions
13  *      nextafter(x,y)
14  *      return the next machine floating-point number of x in the
15  *      direction toward y.
16  *   Special cases:
17  */
18
19 #include "libm.h"
20
21 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
22 long double nextafterl(long double x, long double y)
23 {
24         return nextafter(x, y);
25 }
26 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
27 long double nextafterl(long double x, long double y)
28 {
29         volatile long double t;
30         union IEEEl2bits ux, uy;
31
32         ux.e = x;
33         uy.e = y;
34
35         if ((ux.bits.exp == 0x7fff && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl) != 0) ||
36             (uy.bits.exp == 0x7fff && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
37                 return x+y;  /* x or y is nan */
38         if (x == y)
39                 return y;    /* x=y, return y */
40         if (x == 0.0) {
41                 /* return +-minsubnormal */
42                 ux.bits.manh = 0;
43                 ux.bits.manl = 1;
44                 ux.bits.sign = uy.bits.sign;
45                 /* raise underflow flag */
46                 t = ux.e*ux.e;
47                 if (t == ux.e)
48                         return t;
49                 return ux.e;
50         }
51         if(x > 0.0 ^ x < y) {  /* x -= ulp */
52                 if (ux.bits.manl == 0) {
53                         if ((ux.bits.manh&~LDBL_NBIT) == 0)
54                                 ux.bits.exp -= 1;
55                         ux.bits.manh = (ux.bits.manh - 1) | (ux.bits.manh & LDBL_NBIT);
56                 }
57                 ux.bits.manl -= 1;
58         } else {               /* x += ulp */
59                 ux.bits.manl += 1;
60                 if (ux.bits.manl == 0) {
61                         ux.bits.manh = (ux.bits.manh + 1) | (ux.bits.manh & LDBL_NBIT);
62                         if ((ux.bits.manh&~LDBL_NBIT)==0)
63                                 ux.bits.exp += 1;
64                 }
65         }
66         if (ux.bits.exp == 0x7fff)  /* overflow  */
67                 return x+x;
68         if (ux.bits.exp == 0) {     /* underflow */
69                 mask_nbit_l(ux);
70                 /* raise underflow flag */
71                 t = ux.e * ux.e;
72                 if (t != ux.e)
73                         return ux.e;
74         }
75         return ux.e;
76 }
77 #endif