aea57e30868e7580daa02aca0ef0be705eb4c88d
[jlayton/glibc.git] / sysdeps / libm-ieee754 / s_nextafterl.c
1 /* s_nextafterl.c -- long double version of s_nextafter.c.
2  * Conversion to long double by Ulrich Drepper,
3  * Cygnus Support, drepper@cygnus.com.
4  */
5
6 /*
7  * ====================================================
8  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9  *
10  * Developed at SunPro, a Sun Microsystems, Inc. business.
11  * Permission to use, copy, modify, and distribute this
12  * software is freely granted, provided that this notice
13  * is preserved.
14  * ====================================================
15  */
16
17 #if defined(LIBM_SCCS) && !defined(lint)
18 static char rcsid[] = "$NetBSD: $";
19 #endif
20
21 /* IEEE functions
22  *      nextafterl(x,y)
23  *      return the next machine floating-point number of x in the
24  *      direction toward y.
25  *   Special cases:
26  */
27
28 #include "math.h"
29 #include "math_private.h"
30
31 #ifdef __STDC__
32         long double __nextafterl(long double x, long double y)
33 #else
34         long double __nextafterl(x,y)
35         long double x,y;
36 #endif
37 {
38         int32_t hx,hy,ix,iy;
39         u_int32_t lx,ly,esx,esy;
40
41         GET_LDOUBLE_WORDS(esx,hx,lx,x);
42         GET_LDOUBLE_WORDS(esy,hy,ly,y);
43         ix = esx&0x7fff;                /* |x| */
44         iy = esy&0x7fff;                /* |y| */
45
46         if(((ix==0x7fff)&&((hx|lx)|-(hx|lx))!=0) ||   /* x is nan */
47            ((iy==0x7fff)&&((hy|ly)|-(hy|ly))!=0))     /* y is nan */
48            return x+y;
49         if(x==y) return y;              /* x=y, return y */
50         if((ix|hx|lx)==0) {                     /* x == 0 */
51             SET_LDOUBLE_WORDS(x,esx&0x8000,0,1);/* return +-minsubnormal */
52             y = x*x;
53             if(y==x) return y; else return x;   /* raise underflow flag */
54         }
55         if(esx<0x8000) {                        /* x > 0 */
56             if(ix>iy||((ix==iy) && (hx>hy||((hx==hy)&&(lx>ly))))) {
57               /* x > y, x -= ulp */
58                 if(lx==0) {
59                     if (hx==0) esx -= 1;
60                     hx -= 1;
61                 }
62                 lx -= 1;
63             } else {                            /* x < y, x += ulp */
64                 lx += 1;
65                 if(lx==0) {
66                     hx += 1;
67                     if (hx==0)
68                         esx += 1;
69                 }
70             }
71         } else {                                /* x < 0 */
72             if(esy>=0||(ix>iy||((ix==iy)&&(hx>hy||((hx==hy)&&(lx>ly)))))){
73               /* x < y, x -= ulp */
74                 if(lx==0) {
75                     if (hx==0) esx -= 1;
76                     hx -= 1;
77                 }
78                 lx -= 1;
79             } else {                            /* x > y, x += ulp */
80                 lx += 1;
81                 if(lx==0) {
82                     hx += 1;
83                     if (hx==0) esx += 1;
84                 }
85             }
86         }
87         esy = esx&0x7fff;
88         if(esy==0x7fff) return x+x;     /* overflow  */
89         if(esy==0) {                    /* underflow */
90             y = x*x;
91             if(y!=x) {          /* raise underflow flag */
92                 SET_LDOUBLE_WORDS(y,esx,hx,lx);
93                 return y;
94             }
95         }
96         SET_LDOUBLE_WORDS(x,esx,hx,lx);
97         return x;
98 }
99 weak_alias (__nextafterl, nextafterl)
100 strong_alias (__nextafterl, __nexttowardl)
101 weak_alias (__nextafterl, nexttowardl)