Support testing "inexact" exceptions in libm-test.inc.
[jlayton/glibc.git] / math / s_nextafter.c
1 /* @(#)s_nextafter.c 5.1 93/09/24 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
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  * ====================================================
11  */
12
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
16
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:
22  */
23
24 /* Ugly hack so that the aliasing works.  */
25 #define __nexttoward __internal___nexttoward
26 #define nexttoward __internal_nexttoward
27
28 #include <math.h>
29 #include <math_private.h>
30 #include <float.h>
31
32 double __nextafter(double x, double y)
33 {
34         int32_t hx,hy,ix,iy;
35         u_int32_t lx,ly;
36
37         EXTRACT_WORDS(hx,lx,x);
38         EXTRACT_WORDS(hy,ly,y);
39         ix = hx&0x7fffffff;             /* |x| */
40         iy = hy&0x7fffffff;             /* |y| */
41
42         if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
43            ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0))     /* y is nan */
44            return x+y;
45         if(x==y) return y;              /* x=y, return y */
46         if((ix|lx)==0) {                        /* x == 0 */
47             double u;
48             INSERT_WORDS(x,hy&0x80000000,1);    /* return +-minsubnormal */
49             u = math_opt_barrier (x);
50             u = u*u;
51             math_force_eval (u);                /* raise underflow flag */
52             return x;
53         }
54         if(hx>=0) {                             /* x > 0 */
55             if(hx>hy||((hx==hy)&&(lx>ly))) {    /* 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;
61             }
62         } else {                                /* x < 0 */
63             if(hy>=0||hx>hy||((hx==hy)&&(lx>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         }
71         hy = hx&0x7ff00000;
72         if(hy>=0x7ff00000) {
73           x = x+x;      /* overflow  */
74           if (FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1)
75             asm ("" : "+m"(x));
76           return x;     /* overflow  */
77         }
78         if(hy<0x00100000) {
79             double u = x*x;                     /* underflow */
80             math_force_eval (u);                /* raise underflow flag */
81         }
82         INSERT_WORDS(x,hx,lx);
83         return x;
84 }
85 weak_alias (__nextafter, nextafter)
86 #ifdef NO_LONG_DOUBLE
87 strong_alias (__nextafter, __nextafterl)
88 weak_alias (__nextafter, nextafterl)
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