2.5-18.1
[jlayton/glibc.git] / sysdeps / ieee754 / ldbl-128 / s_nexttoward.c
1 /* s_nexttoward.c
2  * Conversion from s_nextafter.c by Ulrich Drepper, Cygnus Support,
3  * drepper@cygnus.com and Jakub Jelinek, jj@ultra.linux.cz.
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  *      nexttoward(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 #include <float.h>
31
32 #ifdef __STDC__
33         double __nexttoward(double x, long double y)
34 #else
35         double __nexttoward(x,y)
36         double x;
37         long double y;
38 #endif
39 {
40         int32_t hx,ix;
41         int64_t hy,iy;
42         u_int32_t lx;
43         u_int64_t ly;
44
45         EXTRACT_WORDS(hx,lx,x);
46         GET_LDOUBLE_WORDS64(hy,ly,y);
47         ix = hx&0x7fffffff;             /* |x| */
48         iy = hy&0x7fffffffffffffffLL;   /* |y| */
49
50         if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
51            ((iy>=0x7fff000000000000LL)&&((iy-0x7fff000000000000LL)|ly)!=0))
52                                                             /* y is nan */
53            return x+y;
54         if((long double) x==y) return y;        /* x=y, return y */
55         if((ix|lx)==0) {                        /* x == 0 */
56             double u;
57             INSERT_WORDS(x,(u_int32_t)((hy>>32)&0x80000000),1);/* return +-minsub */
58             u = math_opt_barrier (x);
59             u = u * u;
60             math_force_eval (u);                /* raise underflow flag */
61             return x;
62         }
63         if(hx>=0) {                             /* x > 0 */
64             if (hy<0||(ix>>20)>(iy>>48)-0x3c00
65                 || ((ix>>20)==(iy>>48)-0x3c00
66                     && (((((int64_t)hx)<<28)|(lx>>4))>(hy&0x0000ffffffffffffLL)
67                         || (((((int64_t)hx)<<28)|(lx>>4))==(hy&0x0000ffffffffffffLL)
68                             && (lx&0xf)>(ly>>60))))) {  /* x > y, x -= ulp */
69                 if(lx==0) hx -= 1;
70                 lx -= 1;
71             } else {                            /* x < y, x += ulp */
72                 lx += 1;
73                 if(lx==0) hx += 1;
74             }
75         } else {                                /* x < 0 */
76             if (hy>=0||(ix>>20)>(iy>>48)-0x3c00
77                 || ((ix>>20)==(iy>>48)-0x3c00
78                     && (((((int64_t)hx)<<28)|(lx>>4))>(hy&0x0000ffffffffffffLL)
79                         || (((((int64_t)hx)<<28)|(lx>>4))==(hy&0x0000ffffffffffffLL)
80                             && (lx&0xf)>(ly>>60))))) {  /* x < y, x -= ulp */
81                 if(lx==0) hx -= 1;
82                 lx -= 1;
83             } else {                            /* x > y, x += ulp */
84                 lx += 1;
85                 if(lx==0) hx += 1;
86             }
87         }
88         hy = hx&0x7ff00000;
89         if(hy>=0x7ff00000) {
90           x = x+x;      /* overflow  */
91           if (FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1)
92             /* Force conversion to double.  */
93             asm ("" : "+m"(x));
94           return x;
95         }
96         if(hy<0x00100000) {
97             double u = x*x;                     /* underflow */
98             math_force_eval (u);                /* raise underflow flag */
99         }
100         INSERT_WORDS(x,hx,lx);
101         return x;
102 }
103 weak_alias (__nexttoward, nexttoward)