| 1 | /* |
| 2 | * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Oracle designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Oracle in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | * or visit www.oracle.com if you need additional information or have any |
| 23 | * questions. |
| 24 | */ |
| 25 | |
| 26 | /* IEEE functions |
| 27 | * nextafter(x,y) |
| 28 | * return the next machine floating-point number of x in the |
| 29 | * direction toward y. |
| 30 | * Special cases: |
| 31 | */ |
| 32 | |
| 33 | #include "fdlibm.h" |
| 34 | |
| 35 | #ifdef __STDC__ |
| 36 | double nextafter(double x, double y) |
| 37 | #else |
| 38 | double nextafter(x,y) |
| 39 | double x,y; |
| 40 | #endif |
| 41 | { |
| 42 | int hx,hy,ix,iy; |
| 43 | unsigned lx,ly; |
| 44 | |
| 45 | hx = __HI(x); /* high word of x */ |
| 46 | lx = __LO(x); /* low word of x */ |
| 47 | hy = __HI(y); /* high word of y */ |
| 48 | ly = __LO(y); /* low word of y */ |
| 49 | ix = hx&0x7fffffff; /* |x| */ |
| 50 | iy = hy&0x7fffffff; /* |y| */ |
| 51 | |
| 52 | if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */ |
| 53 | ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */ |
| 54 | return x+y; |
| 55 | if(x==y) return x; /* x=y, return x */ |
| 56 | if((ix|lx)==0) { /* x == 0 */ |
| 57 | __HI(x) = hy&0x80000000; /* return +-minsubnormal */ |
| 58 | __LO(x) = 1; |
| 59 | y = x*x; |
| 60 | if(y==x) return y; else return x; /* raise underflow flag */ |
| 61 | } |
| 62 | if(hx>=0) { /* x > 0 */ |
| 63 | if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */ |
| 64 | if(lx==0) hx -= 1; |
| 65 | lx -= 1; |
| 66 | } else { /* x < y, x += ulp */ |
| 67 | lx += 1; |
| 68 | if(lx==0) hx += 1; |
| 69 | } |
| 70 | } else { /* x < 0 */ |
| 71 | if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */ |
| 72 | if(lx==0) hx -= 1; |
| 73 | lx -= 1; |
| 74 | } else { /* x > y, x += ulp */ |
| 75 | lx += 1; |
| 76 | if(lx==0) hx += 1; |
| 77 | } |
| 78 | } |
| 79 | hy = hx&0x7ff00000; |
| 80 | if(hy>=0x7ff00000) return x+x; /* overflow */ |
| 81 | if(hy<0x00100000) { /* underflow */ |
| 82 | y = x*x; |
| 83 | if(y!=x) { /* raise underflow flag */ |
| 84 | __HI(y) = hx; __LO(y) = lx; |
| 85 | return y; |
| 86 | } |
| 87 | } |
| 88 | __HI(x) = hx; __LO(x) = lx; |
| 89 | return x; |
| 90 | } |
| 91 | |