Include bits/mathcalls.h for more _FloatN, _FloatNx types.
[glibc.git] / math / s_nextafter.c
blobfbf440e5195b00f3dcddec5b95b9b29cd3cd0209
1 /* @(#)s_nextafter.c 5.1 93/09/24 */
2 /*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
13 #if defined(LIBM_SCCS) && !defined(lint)
14 static char rcsid[] = "$NetBSD: s_nextafter.c,v 1.8 1995/05/10 20:47:58 jtc Exp $";
15 #endif
17 /* IEEE functions
18 * nextafter(x,y)
19 * return the next machine floating-point number of x in the
20 * direction toward y.
21 * Special cases:
24 /* Ugly hack so that the aliasing works. */
25 #define __nexttoward __internal___nexttoward
26 #define nexttoward __internal_nexttoward
28 #include <errno.h>
29 #include <math.h>
30 #include <math_private.h>
31 #include <float.h>
32 #include <libm-alias-double.h>
34 double __nextafter(double x, double y)
36 int32_t hx,hy,ix,iy;
37 uint32_t lx,ly;
39 EXTRACT_WORDS(hx,lx,x);
40 EXTRACT_WORDS(hy,ly,y);
41 ix = hx&0x7fffffff; /* |x| */
42 iy = hy&0x7fffffff; /* |y| */
44 if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
45 ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */
46 return x+y;
47 if(x==y) return y; /* x=y, return y */
48 if((ix|lx)==0) { /* x == 0 */
49 double u;
50 INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */
51 u = math_opt_barrier (x);
52 u = u*u;
53 math_force_eval (u); /* raise underflow flag */
54 return x;
56 if(hx>=0) { /* x > 0 */
57 if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */
58 if(lx==0) hx -= 1;
59 lx -= 1;
60 } else { /* x < y, x += ulp */
61 lx += 1;
62 if(lx==0) hx += 1;
64 } else { /* x < 0 */
65 if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
66 if(lx==0) hx -= 1;
67 lx -= 1;
68 } else { /* x > y, x += ulp */
69 lx += 1;
70 if(lx==0) hx += 1;
73 hy = hx&0x7ff00000;
74 if(hy>=0x7ff00000) {
75 double u = x+x; /* overflow */
76 math_force_eval (u);
77 __set_errno (ERANGE);
79 if(hy<0x00100000) {
80 double u = x*x; /* underflow */
81 math_force_eval (u); /* raise underflow flag */
82 __set_errno (ERANGE);
84 INSERT_WORDS(x,hx,lx);
85 return x;
87 libm_alias_double (__nextafter, nextafter)
88 #ifdef NO_LONG_DOUBLE
89 strong_alias (__nextafter, __nexttowardl)
90 weak_alias (__nexttowardl, nexttowardl)
91 #undef __nexttoward
92 strong_alias (__nextafter, __nexttoward)
93 #undef nexttoward
94 weak_alias (__nextafter, nexttoward)
95 #endif