chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / ieee754 / ldbl-128ibm / 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 <math_ldbl_opt.h>
31 #include <float.h>
32
33 #ifdef __STDC__
34         double __nexttoward(double x, long double y)
35 #else
36         double __nexttoward(x,y)
37         double x;
38         long double y;
39 #endif
40 {
41         int32_t hx,ix;
42         int64_t hy,iy;
43         u_int32_t lx;
44         u_int64_t ly,uly;
45
46         EXTRACT_WORDS(hx,lx,x);
47         GET_LDOUBLE_WORDS64(hy,ly,y);
48         ix = hx&0x7fffffff;             /* |x| */
49         iy = hy&0x7fffffffffffffffLL;   /* |y| */
50         uly = ly&0x7fffffffffffffffLL;  /* |y| */
51
52         if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
53            ((iy>=0x7ff0000000000000LL)&&((iy-0x7ff0000000000000LL)|uly)!=0))
54                                                             /* y is nan */
55            return x+y;
56         if((long double) x==y) return y;        /* x=y, return y */
57         if((ix|lx)==0) {                        /* x == 0 */
58             double u;
59             INSERT_WORDS(x,(u_int32_t)((hy>>32)&0x80000000),1);/* return +-minsub */
60             u = math_opt_barrier (x);
61             u = u * u;
62             math_force_eval (u);                /* raise underflow flag */
63             return x;
64         }
65         if(hx>=0) {                             /* x > 0 */
66             if (hy<0||(ix>>20)>(iy>>52)
67                 || ((ix>>20)==(iy>>52)
68                     && (((((int64_t)hx)<<32)|(lx))>(hy&0x000fffffffffffffLL)
69                         || (((((int64_t)hx)<<32)|(lx))==(hy&0x000fffffffffffffLL)
70                             )))) {      /* x > y, x -= ulp */
71                 if(lx==0) hx -= 1;
72                 lx -= 1;
73             } else {                            /* x < y, x += ulp */
74                 lx += 1;
75                 if(lx==0) hx += 1;
76             }
77         } else {                                /* x < 0 */
78             if (hy>=0||(ix>>20)>(iy>>52)
79                 || ((ix>>20)==(iy>>52)
80                     && (((((int64_t)hx)<<32)|(lx))>(hy&0x000fffffffffffffLL)
81                         || (((((int64_t)hx)<<32)|(lx))==(hy&0x000fffffffffffffLL)
82                            )))) {       /* x < y, x -= ulp */
83                 if(lx==0) hx -= 1;
84                 lx -= 1;
85             } else {                            /* x > y, x += ulp */
86                 lx += 1;
87                 if(lx==0) hx += 1;
88             }
89         }
90         hy = hx&0x7ff00000;
91         if(hy>=0x7ff00000) {
92           x = x+x;      /* overflow  */
93           if (FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1)
94             /* Force conversion to double.  */
95             asm ("" : "+m"(x));
96           return x;
97         }
98         if(hy<0x00100000) {
99             double u = x*x;                     /* underflow */
100             math_force_eval (u);                /* raise underflow flag */
101         }
102         INSERT_WORDS(x,hx,lx);
103         return x;
104 }
105 long_double_symbol (libm, __nexttoward, nexttoward);