1 /* derived from /netlib/fdlibm */ 2 3 /* @(#)s_nextafter.c 1.3 95/01/18 */ 4 /* 5 * ==================================================== 6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 7 * 8 * Developed at SunSoft, a Sun Microsystems, Inc. business. 9 * Permission to use, copy, modify, and distribute this 10 * software is freely granted, provided that this notice 11 * is preserved. 12 * ==================================================== 13 */ 14 15 /* IEEE functions 16 * nextafter(x,y) 17 * return the next machine floating-point number of x in the 18 * direction toward y. 19 * Special cases: 20 */ 21 22 #include "fdlibm.h" 23 24 double nextafter(double x, double y) 25 { 26 int hx,hy,ix,iy; 27 unsigned lx,ly; 28 29 hx = __HI(x); /* high word of x */ 30 lx = __LO(x); /* low word of x */ 31 hy = __HI(y); /* high word of y */ 32 ly = __LO(y); /* low word of y */ 33 ix = hx&0x7fffffff; /* |x| */ 34 iy = hy&0x7fffffff; /* |y| */ 35 36 if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */ 37 ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */ 38 return x+y; 39 if(x==y) return x; /* x=y, return x */ 40 if((ix|lx)==0) { /* x == 0 */ 41 __HI(x) = hy&0x80000000; /* return +-minsubnormal */ 42 __LO(x) = 1; 43 y = x*x; 44 if(y==x) return y; else return x; /* raise underflow flag */ 45 } 46 if(hx>=0) { /* x > 0 */ 47 if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */ 48 if(lx==0) hx -= 1; 49 lx -= 1; 50 } else { /* x < y, x += ulp */ 51 lx += 1; 52 if(lx==0) hx += 1; 53 } 54 } else { /* x < 0 */ 55 if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */ 56 if(lx==0) hx -= 1; 57 lx -= 1; 58 } else { /* x > y, x += ulp */ 59 lx += 1; 60 if(lx==0) hx += 1; 61 } 62 } 63 hy = hx&0x7ff00000; 64 if(hy>=0x7ff00000) return x+x; /* overflow */ 65 if(hy<0x00100000) { /* underflow */ 66 y = x*x; 67 if(y!=x) { /* raise underflow flag */ 68 __HI(y) = hx; __LO(y) = lx; 69 return y; 70 } 71 } 72 __HI(x) = hx; __LO(x) = lx; 73 return x; 74 } 75