Back to index

glibc  2.9
s_floorf.c
Go to the documentation of this file.
00001 /* s_floorf.c -- float version of s_floor.c.
00002  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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: s_floorf.c,v 1.4 1995/05/10 20:47:22 jtc Exp $";
00018 #endif
00019 
00020 /*
00021  * floorf(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 floorf(x).
00027  */
00028 
00029 #include "math.h"
00030 #include "math_private.h"
00031 
00032 #ifdef __STDC__
00033 static const float huge = 1.0e30;
00034 #else
00035 static float huge = 1.0e30;
00036 #endif
00037 
00038 #ifdef __STDC__
00039        float __floorf(float x)
00040 #else
00041        float __floorf(x)
00042        float x;
00043 #endif
00044 {
00045        int32_t i0,j0;
00046        u_int32_t i;
00047        GET_FLOAT_WORD(i0,x);
00048        j0 = ((i0>>23)&0xff)-0x7f;
00049        if(j0<23) {
00050            if(j0<0) {       /* raise inexact if x != 0 */
00051               if(huge+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
00052                   if(i0>=0) {i0=0;} 
00053                   else if((i0&0x7fffffff)!=0)
00054                      { i0=0xbf800000;}
00055               }
00056            } else {
00057               i = (0x007fffff)>>j0;
00058               if((i0&i)==0) return x; /* x is integral */
00059               if(huge+x>(float)0.0) {     /* raise inexact flag */
00060                   if(i0<0) i0 += (0x00800000)>>j0;
00061                   i0 &= (~i);
00062               }
00063            }
00064        } else {
00065            if(j0==0x80) return x+x;       /* inf or NaN */
00066            else return x;          /* x is integral */
00067        }
00068        SET_FLOAT_WORD(x,i0);
00069        return x;
00070 }
00071 weak_alias (__floorf, floorf)