OSDN Git Service

*: document __USE_EXTERN_INLINES better;
[uclinux-h8/uClibc.git] / libm / s_ceil.c
1 /* @(#)s_ceil.c 5.1 93/09/24 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12
13 #if defined(LIBM_SCCS) && !defined(lint)
14 static char rcsid[] = "$NetBSD: s_ceil.c,v 1.8 1995/05/10 20:46:53 jtc Exp $";
15 #endif
16
17 /*
18  * ceil(x)
19  * Return x rounded toward -inf to integral value
20  * Method:
21  *      Bit twiddling.
22  * Exception:
23  *      Inexact flag raised if x not equal to ceil(x).
24  */
25
26 #include <features.h>
27 /* Prevent math.h from defining a colliding inline */
28 #undef __USE_EXTERN_INLINES
29 #include "math.h"
30 #include "math_private.h"
31
32 #ifdef __STDC__
33 static const double huge = 1.0e300;
34 #else
35 static double huge = 1.0e300;
36 #endif
37
38 #ifdef __STDC__
39         double ceil(double x)
40 #else
41         double ceil(x)
42         double x;
43 #endif
44 {
45         int32_t i0,i1,j0;
46         u_int32_t i,j;
47         EXTRACT_WORDS(i0,i1,x);
48         j0 = ((i0>>20)&0x7ff)-0x3ff;
49         if(j0<20) {
50             if(j0<0) {  /* raise inexact if x != 0 */
51                 if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
52                     if(i0<0) {i0=0x80000000;i1=0;}
53                     else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
54                 }
55             } else {
56                 i = (0x000fffff)>>j0;
57                 if(((i0&i)|i1)==0) return x; /* x is integral */
58                 if(huge+x>0.0) {        /* raise inexact flag */
59                     if(i0>0) i0 += (0x00100000)>>j0;
60                     i0 &= (~i); i1=0;
61                 }
62             }
63         } else if (j0>51) {
64             if(j0==0x400) return x+x;   /* inf or NaN */
65             else return x;              /* x is integral */
66         } else {
67             i = ((u_int32_t)(0xffffffff))>>(j0-20);
68             if((i1&i)==0) return x;     /* x is integral */
69             if(huge+x>0.0) {            /* raise inexact flag */
70                 if(i0>0) {
71                     if(j0==20) i0+=1;
72                     else {
73                         j = i1 + (1<<(52-j0));
74                         if(j<i1) i0+=1; /* got a carry */
75                         i1 = j;
76                     }
77                 }
78                 i1 &= (~i);
79             }
80         }
81         INSERT_WORDS(x,i0,i1);
82         return x;
83 }
84 libm_hidden_def(ceil)