Path: blob/master/src/java.base/share/native/libfdlibm/s_floor.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* floor(x)27* Return x rounded toward -inf to integral value28* Method:29* Bit twiddling.30* Exception:31* Inexact flag raised if x not equal to floor(x).32*/3334#include "fdlibm.h"3536#ifdef __STDC__37static const double huge = 1.0e300;38#else39static double huge = 1.0e300;40#endif4142#ifdef __STDC__43double floor(double x)44#else45double floor(x)46double x;47#endif48{49int i0,i1,j0;50unsigned i,j;51i0 = __HI(x);52i1 = __LO(x);53j0 = ((i0>>20)&0x7ff)-0x3ff;54if(j0<20) {55if(j0<0) { /* raise inexact if x != 0 */56if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */57if(i0>=0) {i0=i1=0;}58else if(((i0&0x7fffffff)|i1)!=0)59{ i0=0xbff00000;i1=0;}60}61} else {62i = (0x000fffff)>>j0;63if(((i0&i)|i1)==0) return x; /* x is integral */64if(huge+x>0.0) { /* raise inexact flag */65if(i0<0) i0 += (0x00100000)>>j0;66i0 &= (~i); i1=0;67}68}69} else if (j0>51) {70if(j0==0x400) return x+x; /* inf or NaN */71else return x; /* x is integral */72} else {73i = ((unsigned)(0xffffffff))>>(j0-20);74if((i1&i)==0) return x; /* x is integral */75if(huge+x>0.0) { /* raise inexact flag */76if(i0<0) {77if(j0==20) i0+=1;78else {79j = i1+(1<<(52-j0));80if(j<i1) i0 +=1 ; /* got a carry */81i1=j;82}83}84i1 &= (~i);85}86}87__HI(x) = i0;88__LO(x) = i1;89return x;90}919293