src/libm/s_floor.c
changeset 11683 48bcba563d9c
parent 6044 35448a5ea044
child 11685 1788f503254d
     1.1 --- a/src/libm/s_floor.c	Sat Nov 04 15:34:14 2017 -0700
     1.2 +++ b/src/libm/s_floor.c	Sat Nov 04 15:53:19 2017 -0700
     1.3 @@ -1,4 +1,3 @@
     1.4 -/* @(#)s_floor.c 5.1 93/09/24 */
     1.5  /*
     1.6   * ====================================================
     1.7   * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
     1.8 @@ -10,11 +9,6 @@
     1.9   * ====================================================
    1.10   */
    1.11  
    1.12 -#if defined(LIBM_SCCS) && !defined(lint)
    1.13 -static const char rcsid[] =
    1.14 -    "$NetBSD: s_floor.c,v 1.8 1995/05/10 20:47:20 jtc Exp $";
    1.15 -#endif
    1.16 -
    1.17  /*
    1.18   * floor(x)
    1.19   * Return x rounded toward -inf to integral value
    1.20 @@ -24,73 +18,54 @@
    1.21   *	Inexact flag raised if x not equal to floor(x).
    1.22   */
    1.23  
    1.24 +/*#include <features.h>*/
    1.25 +/* Prevent math.h from defining a colliding inline */
    1.26 +#undef __USE_EXTERN_INLINES
    1.27  #include "math_libm.h"
    1.28  #include "math_private.h"
    1.29  
    1.30 -#ifdef __STDC__
    1.31 -static const double huge_val = 1.0e300;
    1.32 -#else
    1.33 -static double huge_val = 1.0e300;
    1.34 -#endif
    1.35 +static const double huge = 1.0e300;
    1.36  
    1.37 -libm_hidden_proto(floor)
    1.38 -#ifdef __STDC__
    1.39 -     double floor(double x)
    1.40 -#else
    1.41 -     double floor(x)
    1.42 -     double x;
    1.43 -#endif
    1.44 +double floor(double x)
    1.45  {
    1.46 -    int32_t i0, i1, j0;
    1.47 -    u_int32_t i, j;
    1.48 -    EXTRACT_WORDS(i0, i1, x);
    1.49 -    j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
    1.50 -    if (j0 < 20) {
    1.51 -        if (j0 < 0) {           /* raise inexact if x != 0 */
    1.52 -            if (huge_val + x > 0.0) {       /* return 0*sign(x) if |x|<1 */
    1.53 -                if (i0 >= 0) {
    1.54 -                    i0 = i1 = 0;
    1.55 -                } else if (((i0 & 0x7fffffff) | i1) != 0) {
    1.56 -                    i0 = 0xbff00000;
    1.57 -                    i1 = 0;
    1.58 -                }
    1.59 -            }
    1.60 -        } else {
    1.61 -            i = (0x000fffff) >> j0;
    1.62 -            if (((i0 & i) | i1) == 0)
    1.63 -                return x;       /* x is integral */
    1.64 -            if (huge_val + x > 0.0) {       /* raise inexact flag */
    1.65 -                if (i0 < 0)
    1.66 -                    i0 += (0x00100000) >> j0;
    1.67 -                i0 &= (~i);
    1.68 -                i1 = 0;
    1.69 -            }
    1.70 -        }
    1.71 -    } else if (j0 > 51) {
    1.72 -        if (j0 == 0x400)
    1.73 -            return x + x;       /* inf or NaN */
    1.74 -        else
    1.75 -            return x;           /* x is integral */
    1.76 -    } else {
    1.77 -        i = ((u_int32_t) (0xffffffff)) >> (j0 - 20);
    1.78 -        if ((i1 & i) == 0)
    1.79 -            return x;           /* x is integral */
    1.80 -        if (huge_val + x > 0.0) {   /* raise inexact flag */
    1.81 -            if (i0 < 0) {
    1.82 -                if (j0 == 20)
    1.83 -                    i0 += 1;
    1.84 -                else {
    1.85 -                    j = i1 + (1 << (52 - j0));
    1.86 -                    if (j < (u_int32_t) i1)
    1.87 -                        i0 += 1;        /* got a carry */
    1.88 -                    i1 = j;
    1.89 -                }
    1.90 -            }
    1.91 -            i1 &= (~i);
    1.92 -        }
    1.93 -    }
    1.94 -    INSERT_WORDS(x, i0, i1);
    1.95 -    return x;
    1.96 +	int32_t i0,i1,j0;
    1.97 +	u_int32_t i,j;
    1.98 +	EXTRACT_WORDS(i0,i1,x);
    1.99 +	j0 = ((i0>>20)&0x7ff)-0x3ff;
   1.100 +	if(j0<20) {
   1.101 +	    if(j0<0) { 	/* raise inexact if x != 0 */
   1.102 +		if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
   1.103 +		    if(i0>=0) {i0=i1=0;}
   1.104 +		    else if(((i0&0x7fffffff)|i1)!=0)
   1.105 +			{ i0=0xbff00000;i1=0;}
   1.106 +		}
   1.107 +	    } else {
   1.108 +		i = (0x000fffff)>>j0;
   1.109 +		if(((i0&i)|i1)==0) return x; /* x is integral */
   1.110 +		if(huge+x>0.0) {	/* raise inexact flag */
   1.111 +		    if(i0<0) i0 += (0x00100000)>>j0;
   1.112 +		    i0 &= (~i); i1=0;
   1.113 +		}
   1.114 +	    }
   1.115 +	} else if (j0>51) {
   1.116 +	    if(j0==0x400) return x+x;	/* inf or NaN */
   1.117 +	    else return x;		/* x is integral */
   1.118 +	} else {
   1.119 +	    i = ((u_int32_t)(0xffffffff))>>(j0-20);
   1.120 +	    if((i1&i)==0) return x;	/* x is integral */
   1.121 +	    if(huge+x>0.0) { 		/* raise inexact flag */
   1.122 +		if(i0<0) {
   1.123 +		    if(j0==20) i0+=1;
   1.124 +		    else {
   1.125 +			j = i1+(1<<(52-j0));
   1.126 +			if(j<i1) i0 +=1 ; 	/* got a carry */
   1.127 +			i1=j;
   1.128 +		    }
   1.129 +		}
   1.130 +		i1 &= (~i);
   1.131 +	    }
   1.132 +	}
   1.133 +	INSERT_WORDS(x,i0,i1);
   1.134 +	return x;
   1.135  }
   1.136 -
   1.137  libm_hidden_def(floor)