2 /* @(#)s_floor.c 5.1 93/09/24 */
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
11 * ====================================================
16 <<floor>>, <<floorf>>, <<ceil>>, <<ceilf>>---floor and ceiling
28 double floor(double <[x]>);
29 float floorf(float <[x]>);
30 double ceil(double <[x]>);
31 float ceilf(float <[x]>);
45 <<floor>> and <<floorf>> find
49 the nearest integer less than or equal to <[x]>.
50 <<ceil>> and <<ceilf>> find
54 the nearest integer greater than or equal to <[x]>.
57 <<floor>> and <<ceil>> return the integer result as a double.
58 <<floorf>> and <<ceilf>> return the integer result as a float.
61 <<floor>> and <<ceil>> are ANSI.
62 <<floorf>> and <<ceilf>> are extensions.
69 * Return x rounded toward -inf to integral value
73 * Inexact flag raised if x not equal to floor(x).
78 #ifndef _DOUBLE_IS_32BITS
81 static const double huge
= 1.0e300
;
83 static double huge
= 1.0e300
;
87 double floor(double x
)
95 EXTRACT_WORDS(i0
,i1
,x
);
96 j0
= ((i0
>>20)&0x7ff)-0x3ff;
98 if(j0
<0) { /* raise inexact if x != 0 */
99 if(huge
+x
>0.0) {/* return 0*sign(x) if |x|<1 */
101 else if(((i0
&0x7fffffff)|i1
)!=0)
102 { i0
=0xbff00000;i1
=0;}
105 i
= (0x000fffff)>>j0
;
106 if(((i0
&i
)|i1
)==0) return x
; /* x is integral */
107 if(huge
+x
>0.0) { /* raise inexact flag */
108 if(i0
<0) i0
+= (0x00100000)>>j0
;
113 if(j0
==0x400) return x
+x
; /* inf or NaN */
114 else return x
; /* x is integral */
116 i
= ((__uint32_t
)(0xffffffff))>>(j0
-20);
117 if((i1
&i
)==0) return x
; /* x is integral */
118 if(huge
+x
>0.0) { /* raise inexact flag */
123 if(j
<i1
) i0
+=1 ; /* got a carry */
130 INSERT_WORDS(x
,i0
,i1
);
134 #endif /* _DOUBLE_IS_32BITS */