Replace M_El with lit_e in libm-test.inc
[glibc.git] / sysdeps / ieee754 / ldbl-96 / s_nexttoward.c
blob3d0382eac90b884db4d283d571e496894c57279c
1 /* s_nexttoward.c
2 * Conversion from s_nextafter.c by Ulrich Drepper, Cygnus Support,
3 * drepper@cygnus.com.
4 */
6 /*
7 * ====================================================
8 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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 * ====================================================
17 #if defined(LIBM_SCCS) && !defined(lint)
18 static char rcsid[] = "$NetBSD: $";
19 #endif
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:
28 #include <errno.h>
29 #include <math.h>
30 #include <math_private.h>
31 #include <float.h>
33 double __nexttoward(double x, long double y)
35 int32_t hx,ix,iy;
36 u_int32_t lx,hy,ly,esy;
38 EXTRACT_WORDS(hx,lx,x);
39 GET_LDOUBLE_WORDS(esy,hy,ly,y);
40 ix = hx&0x7fffffff; /* |x| */
41 iy = esy&0x7fff; /* |y| */
43 if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
44 ((iy>=0x7fff)&&(hy|ly)!=0)) /* y is nan */
45 return x+y;
46 if((long double) x==y) return y; /* x=y, return y */
47 if((ix|lx)==0) { /* x == 0 */
48 double u;
49 INSERT_WORDS(x,(esy&0x8000)<<16,1); /* return +-minsub */
50 u = math_opt_barrier (x);
51 u = u * u;
52 math_force_eval (u); /* raise underflow flag */
53 return x;
55 if(hx>=0) { /* x > 0 */
56 if (x > y) { /* x -= ulp */
57 if(lx==0) hx -= 1;
58 lx -= 1;
59 } else { /* x < y, x += ulp */
60 lx += 1;
61 if(lx==0) hx += 1;
63 } else { /* x < 0 */
64 if (x < y) { /* x -= ulp */
65 if(lx==0) hx -= 1;
66 lx -= 1;
67 } else { /* x > y, x += ulp */
68 lx += 1;
69 if(lx==0) hx += 1;
72 hy = hx&0x7ff00000;
73 if(hy>=0x7ff00000) {
74 double u = x+x; /* overflow */
75 math_force_eval (u);
76 __set_errno (ERANGE);
78 if(hy<0x00100000) {
79 double u = x*x; /* underflow */
80 math_force_eval (u); /* raise underflow flag */
81 __set_errno (ERANGE);
83 INSERT_WORDS(x,hx,lx);
84 return x;
86 weak_alias (__nexttoward, nexttoward)