e5f0164ce1c38b895d6fed04ec4d721430d70d30
[platform/upstream/glibc.git] / sysdeps / i386 / fpu / s_nexttoward.c
1 /* s_nexttoward.c
2  * Special i387 version
3  * Conversion from s_nextafter.c by Ulrich Drepper, Cygnus Support,
4  * drepper@cygnus.com.
5  */
6
7 /*
8  * ====================================================
9  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
10  *
11  * Developed at SunPro, a Sun Microsystems, Inc. business.
12  * Permission to use, copy, modify, and distribute this
13  * software is freely granted, provided that this notice
14  * is preserved.
15  * ====================================================
16  */
17
18 #if defined(LIBM_SCCS) && !defined(lint)
19 static char rcsid[] = "$NetBSD: $";
20 #endif
21
22 /* IEEE functions
23  *      nexttoward(x,y)
24  *      return the next machine floating-point number of x in the
25  *      direction toward y.
26  *   Special cases:
27  */
28
29 #include <math.h>
30 #include <math_private.h>
31 #include <float.h>
32
33 double __nexttoward(double x, long double y)
34 {
35         int32_t hx,ix,iy;
36         u_int32_t lx,hy,ly,esy;
37
38         EXTRACT_WORDS(hx,lx,x);
39         GET_LDOUBLE_WORDS(esy,hy,ly,y);
40         ix = hx&0x7fffffff;             /* |x| */
41         iy = esy&0x7fff;                /* |y| */
42
43         /* Intel's extended format has the normally implicit 1 explicit
44            present.  Sigh!  */
45         if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
46            ((iy>=0x7fff)&&((hy&0x7fffffff)|ly)!=0))        /* y is nan */
47            return x+y;
48         if((long double) x==y) return y;        /* x=y, return y */
49         if((ix|lx)==0) {                        /* x == 0 */
50             double u;
51             INSERT_WORDS(x,(esy&0x8000)<<16,1); /* return +-minsub */
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 (esy>=0x8000||((ix>>20)&0x7ff)>iy-0x3c00
59                 || (((ix>>20)&0x7ff)==iy-0x3c00
60                     && (((hx<<11)|(lx>>21))>(hy&0x7fffffff)
61                         || (((hx<<11)|(lx>>21))==(hy&0x7fffffff)
62                             && (lx<<11)>ly)))) {        /* x > y, x -= ulp */
63                 if(lx==0) hx -= 1;
64                 lx -= 1;
65             } else {                            /* x < y, x += ulp */
66                 lx += 1;
67                 if(lx==0) hx += 1;
68             }
69         } else {                                /* x < 0 */
70             if (esy<0x8000||((ix>>20)&0x7ff)>iy-0x3c00
71                 || (((ix>>20)&0x7ff)==iy-0x3c00
72                     && (((hx<<11)|(lx>>21))>(hy&0x7fffffff)
73                         || (((hx<<11)|(lx>>21))==(hy&0x7fffffff)
74                             && (lx<<11)>ly))))  {/* x < y, x -= ulp */
75                 if(lx==0) hx -= 1;
76                 lx -= 1;
77             } else {                            /* x > y, x += ulp */
78                 lx += 1;
79                 if(lx==0) hx += 1;
80             }
81         }
82         hy = hx&0x7ff00000;
83         if(hy>=0x7ff00000) {
84           x = x+x;      /* overflow  */
85           if (FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1)
86             /* Force conversion to double.  */
87             asm ("" : "+m"(x));
88           return x;
89         }
90         if(hy<0x00100000) {
91             double u = x*x;                     /* underflow */
92             math_force_eval (u);                /* raise underflow flag */
93         }
94         INSERT_WORDS(x,hx,lx);
95         return x;
96 }
97 weak_alias (__nexttoward, nexttoward)
98 #ifdef NO_LONG_DOUBLE
99 strong_alias (__nexttoward, __nexttowardl)
100 weak_alias (__nexttoward, nexttowardl)
101 #endif