Back to index

glibc  2.9
s_floorl.c
Go to the documentation of this file.
00001 /* s_floorl.c -- long double version of s_floor.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  * floorl(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 floor(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 __floorl(long double x)
00040 #else
00041        long double __floorl(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=i1=0;}
00053                   else if(((i0&0x7fffffffffffffffLL)|i1)!=0)
00054                      { i0=0xbfff000000000000ULL;i1=0;}
00055               }
00056            } else {
00057               i = (0x0000ffffffffffffULL)>>j0;
00058               if(((i0&i)|i1)==0) return x; /* x is integral */
00059               if(huge+x>0.0) {     /* raise inexact flag */
00060                   if(i0<0) i0 += (0x0001000000000000LL)>>j0;
00061                   i0 &= (~i); i1=0;
00062               }
00063            }
00064        } else if (j0>111) {
00065            if(j0==0x4000) return x+x;     /* inf or NaN */
00066            else return x;          /* x is integral */
00067        } else {
00068            i = -1ULL>>(j0-48);
00069            if((i1&i)==0) return x; /* x is integral */
00070            if(huge+x>0.0) {               /* raise inexact flag */
00071               if(i0<0) {
00072                   if(j0==48) i0+=1;
00073                   else {
00074                      j = i1+(1LL<<(112-j0));
00075                      if(j<i1) i0 +=1 ;    /* got a carry */
00076                      i1=j;
00077                   }
00078               }
00079               i1 &= (~i);
00080            }
00081        }
00082        SET_LDOUBLE_WORDS64(x,i0,i1);
00083        return x;
00084 }
00085 weak_alias (__floorl, floorl)