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