7bc869a518bd136d080bd52d8b5d292b0d65a7ca
[platform/upstream/glibc.git] / sysdeps / ieee754 / ldbl-128 / s_nextafterl.c
1 /* s_nextafterl.c -- long double version of s_nextafter.c.
2  * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
3  */
4
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15
16 #if defined(LIBM_SCCS) && !defined(lint)
17 static char rcsid[] = "$NetBSD: $";
18 #endif
19
20 /* IEEE functions
21  *      nextafterl(x,y)
22  *      return the next machine floating-point number of x in the
23  *      direction toward y.
24  *   Special cases:
25  */
26
27 #include "math.h"
28 #include <math_private.h>
29
30 #ifdef __STDC__
31         long double __nextafterl(long double x, long double y)
32 #else
33         long double __nextafterl(x,y)
34         long double x,y;
35 #endif
36 {
37         int64_t hx,hy,ix,iy;
38         u_int64_t lx,ly;
39
40         GET_LDOUBLE_WORDS64(hx,lx,x);
41         GET_LDOUBLE_WORDS64(hy,ly,y);
42         ix = hx&0x7fffffffffffffffLL;           /* |x| */
43         iy = hy&0x7fffffffffffffffLL;           /* |y| */
44
45         if(((ix>=0x7fff000000000000LL)&&((ix-0x7fff000000000000LL)|lx)!=0) ||   /* x is nan */
46            ((iy>=0x7fff000000000000LL)&&((iy-0x7fff000000000000LL)|ly)!=0))     /* y is nan */
47            return x+y;
48         if(x==y) return y;              /* x=y, return y */
49         if((ix|lx)==0) {                        /* x == 0 */
50             long double u;
51             SET_LDOUBLE_WORDS64(x,hy&0x8000000000000000ULL,1);/* return +-minsubnormal */
52             u = math_opt_barrier (x);
53             u = u * u;
54             math_force_eval (u);                /* raise underflow flag */
55             return x;
56         }
57         if(hx>=0) {                     /* x > 0 */
58             if(hx>hy||((hx==hy)&&(lx>ly))) {    /* x > y, x -= ulp */
59                 if(lx==0) hx--;
60                 lx--;
61             } else {                            /* x < y, x += ulp */
62                 lx++;
63                 if(lx==0) hx++;
64             }
65         } else {                                /* x < 0 */
66             if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
67                 if(lx==0) hx--;
68                 lx--;
69             } else {                            /* x > y, x += ulp */
70                 lx++;
71                 if(lx==0) hx++;
72             }
73         }
74         hy = hx&0x7fff000000000000LL;
75         if(hy==0x7fff000000000000LL) return x+x;/* overflow  */
76         if(hy==0) {
77             long double u = x*x;                /* underflow */
78             math_force_eval (u);                /* raise underflow flag */
79         }
80         SET_LDOUBLE_WORDS64(x,hx,lx);
81         return x;
82 }
83 weak_alias (__nextafterl, nextafterl)
84 strong_alias (__nextafterl, __nexttowardl)
85 weak_alias (__nextafterl, nexttowardl)