1*49393c00Smartynas /* @(#)s_nextafter.c 5.1 93/09/24 */
2*49393c00Smartynas /*
3*49393c00Smartynas * ====================================================
4*49393c00Smartynas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*49393c00Smartynas *
6*49393c00Smartynas * Developed at SunPro, a Sun Microsystems, Inc. business.
7*49393c00Smartynas * Permission to use, copy, modify, and distribute this
8*49393c00Smartynas * software is freely granted, provided that this notice
9*49393c00Smartynas * is preserved.
10*49393c00Smartynas * ====================================================
11*49393c00Smartynas */
12*49393c00Smartynas
13*49393c00Smartynas #include <math.h>
14*49393c00Smartynas
15*49393c00Smartynas #include "math_private.h"
16*49393c00Smartynas
17*49393c00Smartynas float
nexttowardf(float x,long double y)18*49393c00Smartynas nexttowardf(float x, long double y)
19*49393c00Smartynas {
20*49393c00Smartynas int32_t hx,ix;
21*49393c00Smartynas int64_t hy,iy;
22*49393c00Smartynas u_int64_t ly;
23*49393c00Smartynas
24*49393c00Smartynas GET_FLOAT_WORD(hx,x);
25*49393c00Smartynas GET_LDOUBLE_WORDS64(hy,ly,y);
26*49393c00Smartynas ix = hx&0x7fffffff; /* |x| */
27*49393c00Smartynas iy = hy&0x7fffffffffffffffLL; /* |y| */
28*49393c00Smartynas
29*49393c00Smartynas if((ix>0x7f800000) || /* x is nan */
30*49393c00Smartynas ((iy>=0x7fff000000000000LL)&&((iy-0x7fff000000000000LL)|ly)!=0))
31*49393c00Smartynas /* y is nan */
32*49393c00Smartynas return x+y;
33*49393c00Smartynas if((long double) x==y) return y; /* x=y, return y */
34*49393c00Smartynas if(ix==0) { /* x == 0 */
35*49393c00Smartynas volatile float u;
36*49393c00Smartynas SET_FLOAT_WORD(x,(u_int32_t)((hy>>32)&0x80000000)|1);/* return +-minsub*/
37*49393c00Smartynas u = x;
38*49393c00Smartynas u = u * u; /* raise underflow flag */
39*49393c00Smartynas return x;
40*49393c00Smartynas }
41*49393c00Smartynas if(hx>=0) { /* x > 0 */
42*49393c00Smartynas if(hy<0||(ix>>23)>(iy>>48)-0x3f80
43*49393c00Smartynas || ((ix>>23)==(iy>>48)-0x3f80
44*49393c00Smartynas && (ix&0x7fffff)>((hy>>25)&0x7fffff))) {/* x > y, x -= ulp */
45*49393c00Smartynas hx -= 1;
46*49393c00Smartynas } else { /* x < y, x += ulp */
47*49393c00Smartynas hx += 1;
48*49393c00Smartynas }
49*49393c00Smartynas } else { /* x < 0 */
50*49393c00Smartynas if(hy>=0||(ix>>23)>(iy>>48)-0x3f80
51*49393c00Smartynas || ((ix>>23)==(iy>>48)-0x3f80
52*49393c00Smartynas && (ix&0x7fffff)>((hy>>25)&0x7fffff))) {/* x < y, x -= ulp */
53*49393c00Smartynas hx -= 1;
54*49393c00Smartynas } else { /* x > y, x += ulp */
55*49393c00Smartynas hx += 1;
56*49393c00Smartynas }
57*49393c00Smartynas }
58*49393c00Smartynas hy = hx&0x7f800000;
59*49393c00Smartynas if(hy>=0x7f800000) return x+x; /* overflow */
60*49393c00Smartynas if(hy<0x00800000) {
61*49393c00Smartynas volatile float u = x*x; /* underflow */
62*49393c00Smartynas }
63*49393c00Smartynas SET_FLOAT_WORD(x,hx);
64*49393c00Smartynas return x;
65*49393c00Smartynas }
66