1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LMATH/S_CEIL.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,79 @@
1.4 +/* S_CEIL.C
1.5 + *
1.6 + * Portions Copyright (c) 1993-1999 Nokia Corporation and/or its subsidiary(-ies).
1.7 + * All rights reserved.
1.8 + */
1.9 +
1.10 +
1.11 +/* @(#)s_ceil.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 + * ceil(x)
1.25 + * Return x rounded toward -inf to integral value
1.26 + * Method:
1.27 + * Bit twiddling.
1.28 + * Exception:
1.29 + * Inexact flag raised if x not equal to ceil(x).
1.30 + */
1.31 +
1.32 +#include "FDLIBM.H"
1.33 +
1.34 +static const double huge = 1.0e300;
1.35 +
1.36 +/**
1.37 +Round up a value.
1.38 +Returns the smallest integer that is greater or equal to x
1.39 +@return Ceiling of x.
1.40 +@param x Floating point value
1.41 +*/
1.42 +EXPORT_C double ceil(double x) __SOFTFP
1.43 +{
1.44 + __int32_t i0,i1,j0;
1.45 + __uint32_t i,j;
1.46 + EXTRACT_WORDS(i0,i1,x);
1.47 + j0 = ((i0>>20)&0x7ff)-0x3ff;
1.48 + if(j0<20) {
1.49 + if(j0<0) { /* raise inexact if x != 0 */
1.50 + if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
1.51 + if(i0<0) {i0=0x80000000;i1=0;}
1.52 + else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
1.53 + }
1.54 + } else {
1.55 + i = (0x000fffff)>>j0;
1.56 + if(((i0&i)|i1)==0) return x; /* x is integral */
1.57 + if(huge+x>0.0) { /* raise inexact flag */
1.58 + if(i0>0) i0 += (0x00100000)>>j0;
1.59 + i0 &= (~i); i1=0;
1.60 + }
1.61 + }
1.62 + } else if (j0>51) {
1.63 + if(j0==0x400) return x+x; /* inf or NaN */
1.64 + else return x; /* x is integral */
1.65 + } else {
1.66 + i = ((__uint32_t)(0xffffffff))>>(j0-20);
1.67 + if((i1&i)==0) return x; /* x is integral */
1.68 + if(huge+x>0.0) { /* raise inexact flag */
1.69 + if(i0>0) {
1.70 + if(j0==20) i0+=1;
1.71 + else {
1.72 + j = i1 + (1<<(52-j0));
1.73 + if(j<i1) i0+=1; /* got a carry */
1.74 + i1 = j;
1.75 + }
1.76 + }
1.77 + i1 &= (~i);
1.78 + }
1.79 + }
1.80 + INSERT_WORDS(x,i0,i1);
1.81 + return x;
1.82 +}