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 * ceil(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 ceil(x).
33 */
34
35#include "fdlibm.h"
36
37#ifdef __STDC__
38static const double huge = 1.0e300;
39#else
40static double huge = 1.0e300;
41#endif
42
43#ifdef __STDC__
44        double ceil(double x)
45#else
46        double ceil(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=0x80000000;i1=0;}
59                    else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
60                }
61            } else {
62                i = (0x000fffff)>>j0;
63                if(((i0&i)|i1)==0) return x; /* x is integral */
64                if(huge+x>0.0) {        /* raise inexact flag */
65                    if(i0>0) i0 += (0x00100000)>>j0;
66                    i0 &= (~i); i1=0;
67                }
68            }
69        } else if (j0>51) {
70            if(j0==0x400) return x+x;   /* inf or NaN */
71            else return x;              /* x is integral */
72        } else {
73            i = ((unsigned)(0xffffffff))>>(j0-20);
74            if((i1&i)==0) return x;     /* x is integral */
75            if(huge+x>0.0) {            /* raise inexact flag */
76                if(i0>0) {
77                    if(j0==20) i0+=1;
78                    else {
79                        j = i1 + (1<<(52-j0));
80                        if(j<i1) i0+=1; /* got a carry */
81                        i1 = j;
82                    }
83                }
84                i1 &= (~i);
85            }
86        }
87        __HI(x) = i0;
88        __LO(x) = i1;
89        return x;
90}
91