Path: blob/master/src/java.base/share/native/libfdlibm/s_rint.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/*26* rint(x)27* Return x rounded to integral value according to the prevailing28* rounding mode.29* Method:30* Using floating addition.31* Exception:32* Inexact flag raised if x not equal to rint(x).33*/3435#include "fdlibm.h"3637#ifdef __STDC__38static const double39#else40static double41#endif42TWO52[2]={434.50359962737049600000e+15, /* 0x43300000, 0x00000000 */44-4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */45};4647#ifdef __STDC__48double rint(double x)49#else50double rint(x)51double x;52#endif53{54int i0,j0,sx;55unsigned i,i1;56double w,t;57i0 = __HI(x);58sx = (i0>>31)&1;59i1 = __LO(x);60j0 = ((i0>>20)&0x7ff)-0x3ff;61if(j0<20) {62if(j0<0) {63if(((i0&0x7fffffff)|i1)==0) return x;64i1 |= (i0&0x0fffff);65i0 &= 0xfffe0000;66i0 |= ((i1|-i1)>>12)&0x80000;67__HI(x)=i0;68w = TWO52[sx]+x;69t = w-TWO52[sx];70i0 = __HI(t);71__HI(t) = (i0&0x7fffffff)|(sx<<31);72return t;73} else {74i = (0x000fffff)>>j0;75if(((i0&i)|i1)==0) return x; /* x is integral */76i>>=1;77if(((i0&i)|i1)!=0) {78if(j0==19) i1 = 0x40000000; else79i0 = (i0&(~i))|((0x20000)>>j0);80}81}82} else if (j0>51) {83if(j0==0x400) return x+x; /* inf or NaN */84else return x; /* x is integral */85} else {86i = ((unsigned)(0xffffffff))>>(j0-20);87if((i1&i)==0) return x; /* x is integral */88i>>=1;89if((i1&i)!=0) i1 = (i1&(~i))|((0x40000000)>>(j0-20));90}91__HI(x) = i0;92__LO(x) = i1;93w = TWO52[sx]+x;94return w-TWO52[sx];95}969798