2005-12-13 Ulrich Drepper <drepper@redhat.com>
[platform/upstream/linaro-glibc.git] / sysdeps / generic / s_nexttowardf.c
1 /* Single precision version of nexttoward.c.
2    Conversion to IEEE single float by Jakub Jelinek, jj@ultra.linux.cz. */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
7  * Developed at SunPro, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  */
13
14 /* IEEE functions
15  *      nexttowardf(x,y)
16  *      return the next machine floating-point number of x in the
17  *      direction toward y.
18  * This is for machines which use the same binary type for double and
19  * long double.
20  *   Special cases:
21  */
22
23 #include "math.h"
24 #include "math_private.h"
25 #include <float.h>
26
27 #ifdef __STDC__
28         float __nexttowardf(float x, long double y)
29 #else
30         float __nexttowardf(x,y)
31         float x;
32         long double y;
33 #endif
34 {
35         int32_t hx,hy,ix,iy;
36         u_int32_t ly;
37
38         GET_FLOAT_WORD(hx,x);
39         EXTRACT_WORDS(hy,ly,y);
40         ix = hx&0x7fffffff;             /* |x| */
41         iy = hy&0x7fffffff;             /* |y| */
42
43         if((ix>0x7f800000) ||                              /* x is nan */
44            ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0))    /* y is nan */
45            return x+y;
46         if((long double) x==y) return y;        /* x=y, return y */
47         if(ix==0) {                             /* x == 0 */
48             float x2;
49             SET_FLOAT_WORD(x,(u_int32_t)(hy&0x80000000)|1);/* return +-minsub*/
50             x2 = x*x;
51             if(x2==x) return x2; else return x; /* raise underflow flag */
52         }
53         if(hx>=0) {                             /* x > 0 */
54             if(hy<0||(ix>>23)>(iy>>20)-0x380
55                || ((ix>>23)==(iy>>20)-0x380
56                    && (ix&0x7fffff)>(((hy<<3)|(ly>>29))&0x7fffff)))     /* x > y, x -= ulp */
57                 hx -= 1;
58             else                                /* x < y, x += ulp */
59                 hx += 1;
60         } else {                                /* x < 0 */
61             if(hy>=0||(ix>>23)>(iy>>20)-0x380
62                || ((ix>>23)==(iy>>20)-0x380
63                    && (ix&0x7fffff)>(((hy<<3)|(ly>>29))&0x7fffff)))     /* x < y, x -= ulp */
64                 hx -= 1;
65             else                                /* x > y, x += ulp */
66                 hx += 1;
67         }
68         hy = hx&0x7f800000;
69         if(hy>=0x7f800000) {
70           x = x+x;      /* overflow  */
71           if (FLT_EVAL_METHOD != 0)
72             /* Force conversion to float.  */
73             asm ("" : "=m"(x) : "m"(x));
74           return x;
75         }
76         if(hy<0x00800000) {             /* underflow */
77             float x2 = x*x;
78             if(x2!=x) {         /* raise underflow flag */
79                 SET_FLOAT_WORD(x2,hx);
80                 return x2;
81             }
82         }
83         SET_FLOAT_WORD(x,hx);
84         return x;
85 }
86 weak_alias (__nexttowardf, nexttowardf)