149393c00Smartynas /* @(#)s_nextafter.c 5.1 93/09/24 */
249393c00Smartynas /*
349393c00Smartynas * ====================================================
449393c00Smartynas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
549393c00Smartynas *
649393c00Smartynas * Developed at SunPro, a Sun Microsystems, Inc. business.
749393c00Smartynas * Permission to use, copy, modify, and distribute this
849393c00Smartynas * software is freely granted, provided that this notice
949393c00Smartynas * is preserved.
1049393c00Smartynas * ====================================================
1149393c00Smartynas */
1249393c00Smartynas
1349393c00Smartynas /* IEEE functions
1449393c00Smartynas * nexttowardf(x,y)
1549393c00Smartynas * return the next machine floating-point number of x in the
1649393c00Smartynas * direction toward y.
1749393c00Smartynas * This is for machines which use the same binary type for double and
1849393c00Smartynas * long double.
1949393c00Smartynas * Special cases:
2049393c00Smartynas */
2149393c00Smartynas
2249393c00Smartynas #include <math.h>
2349393c00Smartynas #include <float.h>
2449393c00Smartynas
2549393c00Smartynas #include "math_private.h"
2649393c00Smartynas
2749393c00Smartynas float
nexttowardf(float x,long double y)2849393c00Smartynas nexttowardf(float x, long double y)
2949393c00Smartynas {
3049393c00Smartynas int32_t hx,hy,ix,iy;
3149393c00Smartynas u_int32_t ly;
3249393c00Smartynas
3349393c00Smartynas GET_FLOAT_WORD(hx,x);
3449393c00Smartynas EXTRACT_WORDS(hy,ly,y);
3549393c00Smartynas ix = hx&0x7fffffff; /* |x| */
3649393c00Smartynas iy = hy&0x7fffffff; /* |y| */
3749393c00Smartynas
3849393c00Smartynas if((ix>0x7f800000) || /* x is nan */
3949393c00Smartynas ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */
4049393c00Smartynas return x+y;
4149393c00Smartynas if((long double) x==y) return y; /* x=y, return y */
4249393c00Smartynas if(ix==0) { /* x == 0 */
4349393c00Smartynas volatile float u;
4449393c00Smartynas SET_FLOAT_WORD(x,(u_int32_t)(hy&0x80000000)|1);/* return +-minsub*/
4549393c00Smartynas u = x;
4649393c00Smartynas u = u * u; /* raise underflow flag */
4749393c00Smartynas return x;
4849393c00Smartynas }
4949393c00Smartynas if(hx>=0) { /* x > 0 */
5049393c00Smartynas if(hy<0||(ix>>23)>(iy>>20)-0x380
5149393c00Smartynas || ((ix>>23)==(iy>>20)-0x380
5249393c00Smartynas && (ix&0x7fffff)>(((hy<<3)|(ly>>29))&0x7fffff))) /* x > y, x -= ulp */
5349393c00Smartynas hx -= 1;
5449393c00Smartynas else /* x < y, x += ulp */
5549393c00Smartynas hx += 1;
5649393c00Smartynas } else { /* x < 0 */
5749393c00Smartynas if(hy>=0||(ix>>23)>(iy>>20)-0x380
5849393c00Smartynas || ((ix>>23)==(iy>>20)-0x380
5949393c00Smartynas && (ix&0x7fffff)>(((hy<<3)|(ly>>29))&0x7fffff))) /* x < y, x -= ulp */
6049393c00Smartynas hx -= 1;
6149393c00Smartynas else /* x > y, x += ulp */
6249393c00Smartynas hx += 1;
6349393c00Smartynas }
6449393c00Smartynas hy = hx&0x7f800000;
6549393c00Smartynas if(hy>=0x7f800000) {
6649393c00Smartynas x = x+x; /* overflow */
6749393c00Smartynas return x;
6849393c00Smartynas }
6949393c00Smartynas if(hy<0x00800000) {
7049393c00Smartynas volatile float u = x*x; /* underflow */
71*7eebeddeSmartynas if(u==x) {
72*7eebeddeSmartynas SET_FLOAT_WORD(x,hx);
73*7eebeddeSmartynas return x;
74*7eebeddeSmartynas }
7549393c00Smartynas }
7649393c00Smartynas SET_FLOAT_WORD(x,hx);
7749393c00Smartynas return x;
7849393c00Smartynas }
79