Merge branch 'vendor/GCC47'
[dragonfly.git] / lib / libm / src / s_nextafterf.c
1 /* s_nextafterf.c -- float version of s_nextafter.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  *
15  * $NetBSD: s_nextafterf.c,v 1.8 2011/04/18 15:59:09 drochner Exp $
16  */
17
18 #include <math.h>
19 #include "math_private.h"
20
21 float
22 nextafterf(float x, float y)
23 {
24         int32_t hx,hy,ix,iy;
25
26         GET_FLOAT_WORD(hx,x);
27         GET_FLOAT_WORD(hy,y);
28         ix = hx&0x7fffffff;             /* |x| */
29         iy = hy&0x7fffffff;             /* |y| */
30
31         if((ix>0x7f800000) ||   /* x is nan */
32            (iy>0x7f800000))     /* y is nan */
33            return x+y;
34         if(x==y) return y;              /* x=y, return y */
35         if(ix==0) {                             /* x == 0 */
36             SET_FLOAT_WORD(x,(hy&0x80000000)|1);/* return +-minsubnormal */
37             y = x*x;
38             if(y==x) return y; else return x;   /* raise underflow flag */
39         }
40         if(hx>=0) {                             /* x > 0 */
41             if(hx>hy) {                         /* x > y, x -= ulp */
42                 hx -= 1;
43             } else {                            /* x < y, x += ulp */
44                 hx += 1;
45             }
46         } else {                                /* x < 0 */
47             if(hy>=0||hx>hy){                   /* x < y, x -= ulp */
48                 hx -= 1;
49             } else {                            /* x > y, x += ulp */
50                 hx += 1;
51             }
52         }
53         hy = hx&0x7f800000;
54         if(hy>=0x7f800000) return x+x;  /* overflow  */
55         if(hy<0x00800000) {             /* underflow */
56             y = x*x;
57             if(y!=x) {          /* raise underflow flag */
58                 SET_FLOAT_WORD(y,hx);
59                 return y;
60             }
61         }
62         SET_FLOAT_WORD(x,hx);
63         return x;
64 }