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
14
15 float
nexttowardf(float x,long double y)16 nexttowardf(float x, long double y)
17 {
18 int32_t hx,ix,iy;
19 u_int32_t hy,ly,esy;
20
21 GET_FLOAT_WORD(hx,x);
22 GET_LDOUBLE_WORDS(esy,hy,ly,y);
23 ix = hx&0x7fffffff; /* |x| */
24 iy = esy&0x7fff; /* |y| */
25 hy &= 0x7fffffff; /* mask off leading 1 */
26
27 if((ix>0x7f800000) || /* x is nan */
28 (iy>=0x7fff&&((hy|ly)!=0))) /* y is nan */
29 return (long double)x+y;
30 if((long double) x==y) return y; /* x=y, return y */
31 if(ix==0) { /* x == 0 */
32 SET_FLOAT_WORD(x,((esy&0x8000)<<16)|1);/* return +-minsub*/
33 force_eval_float(opt_barrier_float(x)*x);
34 return x;
35 }
36 if(hx>=0) { /* x > 0 */
37 if((long double) x > y) { /* x > y, x -= ulp */
38 hx -= 1;
39 } else { /* x < y, x += ulp */
40 hx += 1;
41 }
42 } else { /* x < 0 */
43 if((long double) x < y) { /* x < y, x -= ulp */
44 hx -= 1;
45 } else { /* x > y, x += ulp */
46 hx += 1;
47 }
48 }
49 hy = hx&0x7f800000;
50 if(hy>=0x7f800000)
51 return __math_oflowf(hx<0);
52 SET_FLOAT_WORD(x,hx);
53 if(hy<0x00800000)
54 return __math_denormf(x);
55 return x;
56 }
57