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 /*
27  * floor(x)
28  * Return x rounded toward -inf to integral value
29  * Method:
30  *      Bit twiddling.
31  * Exception:
32  *      Inexact flag raised if x not equal to floor(x).
33  */
34 
35 #include "fdlibm.h"
36 
37 #ifdef __STDC__
38 static const double huge = 1.0e300;
39 #else
40 static double huge = 1.0e300;
41 #endif
42 
43 #ifdef __STDC__
floor(double x)44         double floor(double x)
45 #else
46         double floor(x)
47         double x;
48 #endif
49 {
50         int i0,i1,j0;
51         unsigned i,j;
52         i0 =  __HI(x);
53         i1 =  __LO(x);
54         j0 = ((i0>>20)&0x7ff)-0x3ff;
55         if(j0<20) {
56             if(j0<0) {  /* raise inexact if x != 0 */
57                 if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
58                     if(i0>=0) {i0=i1=0;}
59                     else if(((i0&0x7fffffff)|i1)!=0)
60                         { i0=0xbff00000;i1=0;}
61                 }
62             } else {
63                 i = (0x000fffff)>>j0;
64                 if(((i0&i)|i1)==0) return x; /* x is integral */
65                 if(huge+x>0.0) {        /* raise inexact flag */
66                     if(i0<0) i0 += (0x00100000)>>j0;
67                     i0 &= (~i); i1=0;
68                 }
69             }
70         } else if (j0>51) {
71             if(j0==0x400) return x+x;   /* inf or NaN */
72             else return x;              /* x is integral */
73         } else {
74             i = ((unsigned)(0xffffffff))>>(j0-20);
75             if((i1&i)==0) return x;     /* x is integral */
76             if(huge+x>0.0) {            /* raise inexact flag */
77                 if(i0<0) {
78                     if(j0==20) i0+=1;
79                     else {
80                         j = i1+(1<<(52-j0));
81                         if(j<i1) i0 +=1 ;       /* got a carry */
82                         i1=j;
83                     }
84                 }
85                 i1 &= (~i);
86             }
87         }
88         __HI(x) = i0;
89         __LO(x) = i1;
90         return x;
91 }
92