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