]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/ieee754/ldbl-96/s_nexttoward.c
[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[thirdparty/glibc.git] / sysdeps / ieee754 / ldbl-96 / s_nexttoward.c
1 /* s_nexttoward.c
2 * Conversion from s_nextafter.c by Ulrich Drepper, Cygnus Support,
3 * drepper@cygnus.com.
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,iy;
41 u_int32_t lx,hy,ly,esy;
42
43 EXTRACT_WORDS(hx,lx,x);
44 GET_LDOUBLE_WORDS(esy,hy,ly,y);
45 ix = hx&0x7fffffff; /* |x| */
46 iy = esy&0x7fff; /* |y| */
47
48 if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
49 ((iy>=0x7fff)&&(hy|ly)!=0)) /* y is nan */
50 return x+y;
51 if((long double) x==y) return y; /* x=y, return y */
52 if((ix|lx)==0) { /* x == 0 */
53 double x2;
54 INSERT_WORDS(x,(esy&0x8000)<<16,1); /* return +-minsub */
55 x2 = x*x;
56 if(x2==x) return x2; else return x; /* raise underflow flag */
57 }
58 if(hx>=0) { /* x > 0 */
59 if (esy>=0x8000||((ix>>20)&0x7ff)>iy-0x3c00
60 || (((ix>>20)&0x7ff)==iy-0x3c00
61 && (((hx<<11)|(lx>>21))>(hy&0x7fffffff)
62 || (((hx<<11)|(lx>>21))==(hy&0x7fffffff)
63 && (lx<<11)>ly)))) { /* 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;
69 }
70 } else { /* x < 0 */
71 if (esy<0x8000||((ix>>20)&0x7ff)>iy-0x3c00
72 || (((ix>>20)&0x7ff)==iy-0x3c00
73 && (((hx<<11)|(lx>>21))>(hy&0x7fffffff)
74 || (((hx<<11)|(lx>>21))==(hy&0x7fffffff)
75 && (lx<<11)>ly)))) {/* x < y, x -= ulp */
76 if(lx==0) hx -= 1;
77 lx -= 1;
78 } else { /* x > y, x += ulp */
79 lx += 1;
80 if(lx==0) hx += 1;
81 }
82 }
83 hy = hx&0x7ff00000;
84 if(hy>=0x7ff00000) {
85 x = x+x; /* overflow */
86 if (FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1)
87 /* Force conversion to float. */
88 asm ("" : "=m"(x) : "m"(x));
89 return x;
90 }
91 if(hy<0x00100000) { /* underflow */
92 double x2 = x*x;
93 if(x2!=x) { /* raise underflow flag */
94 INSERT_WORDS(x2,hx,lx);
95 return x2;
96 }
97 }
98 INSERT_WORDS(x,hx,lx);
99 return x;
100 }
101 weak_alias (__nexttoward, nexttoward)