Path: blob/master/src/java.base/share/native/libfdlibm/s_nextafter.c
41149 views
/*1* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* IEEE functions26* nextafter(x,y)27* return the next machine floating-point number of x in the28* direction toward y.29* Special cases:30*/3132#include "fdlibm.h"3334#ifdef __STDC__35double nextafter(double x, double y)36#else37double nextafter(x,y)38double x,y;39#endif40{41int hx,hy,ix,iy;42unsigned lx,ly;4344hx = __HI(x); /* high word of x */45lx = __LO(x); /* low word of x */46hy = __HI(y); /* high word of y */47ly = __LO(y); /* low word of y */48ix = hx&0x7fffffff; /* |x| */49iy = hy&0x7fffffff; /* |y| */5051if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */52((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */53return x+y;54if(x==y) return x; /* x=y, return x */55if((ix|lx)==0) { /* x == 0 */56__HI(x) = hy&0x80000000; /* return +-minsubnormal */57__LO(x) = 1;58y = x*x;59if(y==x) return y; else return x; /* raise underflow flag */60}61if(hx>=0) { /* x > 0 */62if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */63if(lx==0) hx -= 1;64lx -= 1;65} else { /* x < y, x += ulp */66lx += 1;67if(lx==0) hx += 1;68}69} else { /* x < 0 */70if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */71if(lx==0) hx -= 1;72lx -= 1;73} else { /* x > y, x += ulp */74lx += 1;75if(lx==0) hx += 1;76}77}78hy = hx&0x7ff00000;79if(hy>=0x7ff00000) return x+x; /* overflow */80if(hy<0x00100000) { /* underflow */81y = x*x;82if(y!=x) { /* raise underflow flag */83__HI(y) = hx; __LO(y) = lx;84return y;85}86}87__HI(x) = hx; __LO(x) = lx;88return x;89}909192