os/ossrv/genericopenlibs/cstdlib/LMATH/S_FLOOR.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LMATH/S_FLOOR.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,133 @@
     1.4 +/* S_FLOOR.C
     1.5 + * 
     1.6 + * Portions Copyright (c) 1993-2005 Nokia Corporation and/or its subsidiary(-ies).
     1.7 + * All rights reserved.
     1.8 + */
     1.9 +
    1.10 +
    1.11 +/* @(#)s_floor.c 5.1 93/09/24 */
    1.12 +/*
    1.13 + * ====================================================
    1.14 + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
    1.15 + *
    1.16 + * Developed at SunPro, a Sun Microsystems, Inc. business.
    1.17 + * Permission to use, copy, modify, and distribute this
    1.18 + * software is freely granted, provided that this notice 
    1.19 + * is preserved.
    1.20 + * ====================================================
    1.21 + */
    1.22 +
    1.23 +/*
    1.24 +FUNCTION
    1.25 +<<floor>>, <<floorf>>, <<ceil>>, <<ceilf>>---floor and ceiling
    1.26 +INDEX
    1.27 +	floor
    1.28 +INDEX
    1.29 +	floorf
    1.30 +INDEX
    1.31 +	ceil
    1.32 +INDEX
    1.33 +	ceilf
    1.34 +
    1.35 +ANSI_SYNOPSIS
    1.36 +	#include <math.h>
    1.37 +	double floor(double <[x]>);
    1.38 +        float floorf(float <[x]>);
    1.39 +        double ceil(double <[x]>);
    1.40 +        float ceilf(float <[x]>);
    1.41 +
    1.42 +TRAD_SYNOPSIS
    1.43 +	#include <math.h>
    1.44 +        double floor(<[x]>)
    1.45 +	double <[x]>;
    1.46 +        float floorf(<[x]>) 
    1.47 +	float <[x]>;
    1.48 +        double ceil(<[x]>) 
    1.49 +	double <[x]>;
    1.50 +        float ceilf(<[x]>) 
    1.51 +	float <[x]>;
    1.52 +
    1.53 +DESCRIPTION
    1.54 +<<floor>> and <<floorf>> find 
    1.55 +@tex
    1.56 +$\lfloor x \rfloor$, 
    1.57 +@end tex
    1.58 +the nearest integer less than or equal to <[x]>.
    1.59 +<<ceil>> and <<ceilf>> find 
    1.60 +@tex
    1.61 +$\lceil x\rceil$,
    1.62 +@end tex
    1.63 +the nearest integer greater than or equal to <[x]>.
    1.64 +
    1.65 +RETURNS
    1.66 +<<floor>> and <<ceil>> return the integer result as a double.
    1.67 +<<floorf>> and <<ceilf>> return the integer result as a float.
    1.68 +
    1.69 +PORTABILITY
    1.70 +<<floor>> and <<ceil>> are ANSI.
    1.71 +<<floorf>> and <<ceilf>> are extensions.
    1.72 +
    1.73 +
    1.74 +*/
    1.75 +
    1.76 +/*
    1.77 + * floor(x)
    1.78 + * Return x rounded toward -inf to integral value
    1.79 + * Method:
    1.80 + *	Bit twiddling.
    1.81 + * Exception:
    1.82 + *	Inexact flag raised if x not equal to floor(x).
    1.83 + */
    1.84 +
    1.85 +#include "FDLIBM.H"
    1.86 +
    1.87 +static const double huge = 1.0e300;
    1.88 +
    1.89 +/**
    1.90 +Round down value.
    1.91 +Returns the largest integer that is less than or equal to x 
    1.92 +@return   Floor of x.
    1.93 +@param x Floating point value
    1.94 +*/	
    1.95 +EXPORT_C double floor(double x) __SOFTFP
    1.96 +{
    1.97 +	__int32_t i0,i1,j0;
    1.98 +	__uint32_t i,j;
    1.99 +	EXTRACT_WORDS(i0,i1,x);
   1.100 +	j0 = ((i0>>20)&0x7ff)-0x3ff;
   1.101 +	if(j0<20) {
   1.102 +	    if(j0<0) { 	/* raise inexact if x != 0 */
   1.103 +		if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
   1.104 +		    if(i0>=0) {i0=i1=0;} 
   1.105 +		    else if(((i0&0x7fffffff)|i1)!=0)
   1.106 +			{ i0=0xbff00000;i1=0;}
   1.107 +		}
   1.108 +	    } else {
   1.109 +		i = (0x000fffff)>>j0;
   1.110 +		if(((i0&i)|i1)==0) return x; /* x is integral */
   1.111 +		if(huge+x>0.0) {	/* raise inexact flag */
   1.112 +		    if(i0<0) i0 += (0x00100000)>>j0;
   1.113 +		    i0 &= (~i); i1=0;
   1.114 +		}
   1.115 +	    }
   1.116 +	} else if (j0>51) {
   1.117 +	    if(j0==0x400) return x+x;	/* inf or NaN */
   1.118 +	    else return x;		/* x is integral */
   1.119 +	} else {
   1.120 +	    i = ((__uint32_t)(0xffffffff))>>(j0-20);
   1.121 +	    if((i1&i)==0) return x;	/* x is integral */
   1.122 +	    if(huge+x>0.0) { 		/* raise inexact flag */
   1.123 +		if(i0<0) {
   1.124 +		    if(j0==20) i0+=1; 
   1.125 +		    else {
   1.126 +			j = i1+(1<<(52-j0));
   1.127 +			if(j<i1) i0 +=1 ; 	/* got a carry */
   1.128 +			i1=j;
   1.129 +		    }
   1.130 +		}
   1.131 +		i1 &= (~i);
   1.132 +	    }
   1.133 +	}
   1.134 +	INSERT_WORDS(x,i0,i1);
   1.135 +	return x;
   1.136 +}