Use round-to-nearest internally in jn, test with ALL_RM_TEST (bug 18602).
[glibc.git] / sysdeps / ieee754 / ldbl-96 / s_nexttoward.c
blobf7a8b2165adf6c1eda2e10cbedfbfd3305751a43
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 <math.h>
29 #include <math_private.h>
30 #include <float.h>
32 double __nexttoward(double x, long double y)
34 int32_t hx,ix,iy;
35 u_int32_t lx,hy,ly,esy;
37 EXTRACT_WORDS(hx,lx,x);
38 GET_LDOUBLE_WORDS(esy,hy,ly,y);
39 ix = hx&0x7fffffff; /* |x| */
40 iy = esy&0x7fff; /* |y| */
42 if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
43 ((iy>=0x7fff)&&(hy|ly)!=0)) /* y is nan */
44 return x+y;
45 if((long double) x==y) return y; /* x=y, return y */
46 if((ix|lx)==0) { /* x == 0 */
47 double u;
48 INSERT_WORDS(x,(esy&0x8000)<<16,1); /* return +-minsub */
49 u = math_opt_barrier (x);
50 u = u * u;
51 math_force_eval (u); /* raise underflow flag */
52 return x;
54 if(hx>=0) { /* x > 0 */
55 if (x > y) { /* x -= ulp */
56 if(lx==0) hx -= 1;
57 lx -= 1;
58 } else { /* x < y, x += ulp */
59 lx += 1;
60 if(lx==0) hx += 1;
62 } else { /* x < 0 */
63 if (x < y) { /* x -= ulp */
64 if(lx==0) hx -= 1;
65 lx -= 1;
66 } else { /* x > y, x += ulp */
67 lx += 1;
68 if(lx==0) hx += 1;
71 hy = hx&0x7ff00000;
72 if(hy>=0x7ff00000) {
73 x = x+x; /* overflow */
74 if (FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1)
75 /* Force conversion to double. */
76 asm ("" : "+m"(x));
77 return x;
79 if(hy<0x00100000) {
80 double u = x*x; /* underflow */
81 math_force_eval (u); /* raise underflow flag */
83 INSERT_WORDS(x,hx,lx);
84 return x;
86 weak_alias (__nexttoward, nexttoward)