Back to index

glibc  2.9
s_ceill.c
Go to the documentation of this file.
00001 /* s_ceill.c -- long double version of s_ceil.c.
00002  * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
00003  */
00004 
00005 /*
00006  * ====================================================
00007  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00008  *
00009  * Developed at SunPro, a Sun Microsystems, Inc. business.
00010  * Permission to use, copy, modify, and distribute this
00011  * software is freely granted, provided that this notice
00012  * is preserved.
00013  * ====================================================
00014  */
00015 
00016 #if defined(LIBM_SCCS) && !defined(lint)
00017 static char rcsid[] = "$NetBSD: $";
00018 #endif
00019 
00020 /*
00021  * ceill(x)
00022  * Return x rounded toward -inf to integral value
00023  * Method:
00024  *     Bit twiddling.
00025  * Exception:
00026  *     Inexact flag raised if x not equal to ceil(x).
00027  */
00028 
00029 #include "math.h"
00030 #include "math_private.h"
00031 
00032 #ifdef __STDC__
00033 static const long double huge = 1.0e4930;
00034 #else
00035 static long double huge = 1.0e4930;
00036 #endif
00037 
00038 #ifdef __STDC__
00039        long double __ceill(long double x)
00040 #else
00041        long double __ceill(x)
00042        long double x;
00043 #endif
00044 {
00045        int64_t i0,i1,j0;
00046        u_int64_t i,j;
00047        GET_LDOUBLE_WORDS64(i0,i1,x);
00048        j0 = ((i0>>48)&0x7fff)-0x3fff;
00049        if(j0<48) {
00050            if(j0<0) {       /* raise inexact if x != 0 */
00051               if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
00052                   if(i0<0) {i0=0x8000000000000000ULL;i1=0;}
00053                   else if((i0|i1)!=0) { i0=0x3fff000000000000ULL;i1=0;}
00054               }
00055            } else {
00056               i = (0x0000ffffffffffffULL)>>j0;
00057               if(((i0&i)|i1)==0) return x; /* x is integral */
00058               if(huge+x>0.0) {     /* raise inexact flag */
00059                   if(i0>0) i0 += (0x0001000000000000LL)>>j0;
00060                   i0 &= (~i); i1=0;
00061               }
00062            }
00063        } else if (j0>111) {
00064            if(j0==0x4000) return x+x;     /* inf or NaN */
00065            else return x;          /* x is integral */
00066        } else {
00067            i = -1ULL>>(j0-48);
00068            if((i1&i)==0) return x; /* x is integral */
00069            if(huge+x>0.0) {               /* raise inexact flag */
00070               if(i0>0) {
00071                   if(j0==48) i0+=1;
00072                   else {
00073                      j = i1+(1LL<<(112-j0));
00074                      if(j<i1) i0 +=1 ;    /* got a carry */
00075                      i1=j;
00076                   }
00077               }
00078               i1 &= (~i);
00079            }
00080        }
00081        SET_LDOUBLE_WORDS64(x,i0,i1);
00082        return x;
00083 }
00084 weak_alias (__ceill, ceill)