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 | * __nexttowardfd(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 different binary type for double and |
19 | * long double conditionally, y is long double equal to 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 <math_ldbl_opt.h> |
28 | #include <float.h> |
29 | |
30 | float __nldbl_nexttowardf(float x, double y); |
31 | |
32 | float __nldbl_nexttowardf(float x, double y) |
33 | { |
34 | int32_t hx,hy,ix,iy; |
35 | uint32_t ly; |
36 | |
37 | GET_FLOAT_WORD(hx,x); |
38 | EXTRACT_WORDS(hy,ly,y); |
39 | ix = hx&0x7fffffff; /* |x| */ |
40 | iy = hy&0x7fffffff; /* |y| */ |
41 | |
42 | if((ix>0x7f800000) || /* x is nan */ |
43 | ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */ |
44 | return x+y; |
45 | if((double) x==y) return y; /* x=y, return y */ |
46 | if(ix==0) { /* x == 0 */ |
47 | float u; |
48 | SET_FLOAT_WORD(x,(uint32_t)(hy&0x80000000)|1);/* return +-minsub*/ |
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(x > y) /* x -= ulp */ |
56 | hx -= 1; |
57 | else /* x < y, x += ulp */ |
58 | hx += 1; |
59 | } else { /* x < 0 */ |
60 | if(x < y) /* x -= ulp */ |
61 | hx -= 1; |
62 | else /* x > y, x += ulp */ |
63 | hx += 1; |
64 | } |
65 | hy = hx&0x7f800000; |
66 | if(hy>=0x7f800000) { |
67 | float u = x+x; /* overflow */ |
68 | math_force_eval (u); |
69 | __set_errno (ERANGE); |
70 | } |
71 | if(hy<0x00800000) { |
72 | float u = x*x; /* underflow */ |
73 | math_force_eval (u); /* raise underflow flag */ |
74 | __set_errno (ERANGE); |
75 | } |
76 | SET_FLOAT_WORD(x,hx); |
77 | return x; |
78 | } |
79 | |
80 | #if LONG_DOUBLE_COMPAT(libm, GLIBC_2_1) |
81 | compat_symbol (libm, __nldbl_nexttowardf, nexttowardf, GLIBC_2_1); |
82 | #endif |
83 | |