2.9
[glibc/nacl-glibc.git] / sysdeps / i386 / fpu / s_nexttoward.c
blob9bd86a3724a80c086386465dcd74f2c2c2fe1b42
1 /* s_nexttoward.c
2 * Special i387 version
3 * Conversion from s_nextafter.c by Ulrich Drepper, Cygnus Support,
4 * drepper@cygnus.com.
5 */
7 /*
8 * ====================================================
9 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
11 * Developed at SunPro, a Sun Microsystems, Inc. business.
12 * Permission to use, copy, modify, and distribute this
13 * software is freely granted, provided that this notice
14 * is preserved.
15 * ====================================================
18 #if defined(LIBM_SCCS) && !defined(lint)
19 static char rcsid[] = "$NetBSD: $";
20 #endif
22 /* IEEE functions
23 * nexttoward(x,y)
24 * return the next machine floating-point number of x in the
25 * direction toward y.
26 * Special cases:
29 #include "math.h"
30 #include <math_private.h>
31 #include <float.h>
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
41 int32_t hx,ix,iy;
42 u_int32_t lx,hy,ly,esy;
44 EXTRACT_WORDS(hx,lx,x);
45 GET_LDOUBLE_WORDS(esy,hy,ly,y);
46 ix = hx&0x7fffffff; /* |x| */
47 iy = esy&0x7fff; /* |y| */
49 /* Intel's extended format has the normally implicit 1 explicit
50 present. Sigh! */
51 if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
52 ((iy>=0x7fff)&&((hy&0x7fffffff)|ly)!=0)) /* 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,(esy&0x8000)<<16,1); /* return +-minsub */
58 u = math_opt_barrier (x);
59 u = u * u;
60 math_force_eval (u); /* raise underflow flag */
61 return x;
63 if(hx>=0) { /* x > 0 */
64 if (esy>=0x8000||((ix>>20)&0x7ff)>iy-0x3c00
65 || (((ix>>20)&0x7ff)==iy-0x3c00
66 && (((hx<<11)|(lx>>21))>(hy&0x7fffffff)
67 || (((hx<<11)|(lx>>21))==(hy&0x7fffffff)
68 && (lx<<11)>ly)))) { /* 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;
75 } else { /* x < 0 */
76 if (esy<0x8000||((ix>>20)&0x7ff)>iy-0x3c00
77 || (((ix>>20)&0x7ff)==iy-0x3c00
78 && (((hx<<11)|(lx>>21))>(hy&0x7fffffff)
79 || (((hx<<11)|(lx>>21))==(hy&0x7fffffff)
80 && (lx<<11)>ly)))) {/* 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;
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;
96 if(hy<0x00100000) {
97 double u = x*x; /* underflow */
98 math_force_eval (u); /* raise underflow flag */
100 INSERT_WORDS(x,hx,lx);
101 return x;
103 weak_alias (__nexttoward, nexttoward)
104 #ifdef NO_LONG_DOUBLE
105 strong_alias (__nexttoward, __nexttowardl)
106 weak_alias (__nexttoward, nexttowardl)
107 #endif