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#include <math.h>
14
15#include "math_private.h"
16
17float
18nexttowardf(float x, long double y)
19{
20	int32_t hx,ix;
21	int64_t hy,iy;
22	u_int64_t ly;
23
24	GET_FLOAT_WORD(hx,x);
25	GET_LDOUBLE_WORDS64(hy,ly,y);
26	ix = hx&0x7fffffff;		/* |x| */
27	iy = hy&0x7fffffffffffffffLL;	/* |y| */
28
29	if((ix>0x7f800000) ||   /* x is nan */
30	   ((iy>=0x7fff000000000000LL)&&((iy-0x7fff000000000000LL)|ly)!=0))
31				/* y is nan */
32	   return x+y;
33	if((long double) x==y) return y;	/* x=y, return y */
34	if(ix==0) {				/* x == 0 */
35	    volatile float u;
36	    SET_FLOAT_WORD(x,(u_int32_t)((hy>>32)&0x80000000)|1);/* return +-minsub*/
37	    u = x;
38	    u = u * u;				/* raise underflow flag */
39	    return x;
40	}
41	if(hx>=0) {				/* x > 0 */
42	    if(hy<0||(ix>>23)>(iy>>48)-0x3f80
43	       || ((ix>>23)==(iy>>48)-0x3f80
44		   && (ix&0x7fffff)>((hy>>25)&0x7fffff))) {/* x > y, x -= ulp */
45		hx -= 1;
46	    } else {				/* x < y, x += ulp */
47		hx += 1;
48	    }
49	} else {				/* x < 0 */
50	    if(hy>=0||(ix>>23)>(iy>>48)-0x3f80
51	       || ((ix>>23)==(iy>>48)-0x3f80
52		   && (ix&0x7fffff)>((hy>>25)&0x7fffff))) {/* x < y, x -= ulp */
53		hx -= 1;
54	    } else {				/* x > y, x += ulp */
55		hx += 1;
56	    }
57	}
58	hy = hx&0x7f800000;
59	if(hy>=0x7f800000) return x+x;	/* overflow  */
60	if(hy<0x00800000) {
61	    volatile float u = x*x;	/* underflow */
62	}
63	SET_FLOAT_WORD(x,hx);
64	return x;
65}
66