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 <errno.h>
24 #include <math.h>
25 #include <math-barriers.h>
26 #include <math_private.h>
27 #include <float.h>
28
__nexttowardf(float x,long double y)29 float __nexttowardf(float x, long double y)
30 {
31 int32_t hx,hy,ix,iy;
32 uint32_t ly;
33
34 GET_FLOAT_WORD(hx,x);
35 EXTRACT_WORDS(hy,ly,y);
36 ix = hx&0x7fffffff; /* |x| */
37 iy = hy&0x7fffffff; /* |y| */
38
39 if((ix>0x7f800000) || /* x is nan */
40 ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */
41 return x+y;
42 if((long double) x==y) return y; /* x=y, return y */
43 if(ix==0) { /* x == 0 */
44 float u;
45 SET_FLOAT_WORD(x,(uint32_t)(hy&0x80000000)|1);/* return +-minsub*/
46 u = math_opt_barrier (x);
47 u = u * u;
48 math_force_eval (u); /* raise underflow flag */
49 return x;
50 }
51 if(hx>=0) { /* x > 0 */
52 if(x > y) /* x -= ulp */
53 hx -= 1;
54 else /* x < y, x += ulp */
55 hx += 1;
56 } else { /* x < 0 */
57 if(x < y) /* x -= ulp */
58 hx -= 1;
59 else /* x > y, x += ulp */
60 hx += 1;
61 }
62 hy = hx&0x7f800000;
63 if(hy>=0x7f800000) {
64 float u = x+x; /* overflow */
65 math_force_eval (u);
66 __set_errno (ERANGE);
67 }
68 if(hy<0x00800000) {
69 float u = x*x; /* underflow */
70 math_force_eval (u); /* raise underflow flag */
71 __set_errno (ERANGE);
72 }
73 SET_FLOAT_WORD(x,hx);
74 return x;
75 }
76 weak_alias (__nexttowardf, nexttowardf)
77