|
1 /* S_FLOOR.C |
|
2 * |
|
3 * Portions Copyright (c) 1993-2005 Nokia Corporation and/or its subsidiary(-ies). |
|
4 * All rights reserved. |
|
5 */ |
|
6 |
|
7 |
|
8 /* @(#)s_floor.c 5.1 93/09/24 */ |
|
9 /* |
|
10 * ==================================================== |
|
11 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
|
12 * |
|
13 * Developed at SunPro, a Sun Microsystems, Inc. business. |
|
14 * Permission to use, copy, modify, and distribute this |
|
15 * software is freely granted, provided that this notice |
|
16 * is preserved. |
|
17 * ==================================================== |
|
18 */ |
|
19 |
|
20 /* |
|
21 FUNCTION |
|
22 <<floor>>, <<floorf>>, <<ceil>>, <<ceilf>>---floor and ceiling |
|
23 INDEX |
|
24 floor |
|
25 INDEX |
|
26 floorf |
|
27 INDEX |
|
28 ceil |
|
29 INDEX |
|
30 ceilf |
|
31 |
|
32 ANSI_SYNOPSIS |
|
33 #include <math.h> |
|
34 double floor(double <[x]>); |
|
35 float floorf(float <[x]>); |
|
36 double ceil(double <[x]>); |
|
37 float ceilf(float <[x]>); |
|
38 |
|
39 TRAD_SYNOPSIS |
|
40 #include <math.h> |
|
41 double floor(<[x]>) |
|
42 double <[x]>; |
|
43 float floorf(<[x]>) |
|
44 float <[x]>; |
|
45 double ceil(<[x]>) |
|
46 double <[x]>; |
|
47 float ceilf(<[x]>) |
|
48 float <[x]>; |
|
49 |
|
50 DESCRIPTION |
|
51 <<floor>> and <<floorf>> find |
|
52 @tex |
|
53 $\lfloor x \rfloor$, |
|
54 @end tex |
|
55 the nearest integer less than or equal to <[x]>. |
|
56 <<ceil>> and <<ceilf>> find |
|
57 @tex |
|
58 $\lceil x\rceil$, |
|
59 @end tex |
|
60 the nearest integer greater than or equal to <[x]>. |
|
61 |
|
62 RETURNS |
|
63 <<floor>> and <<ceil>> return the integer result as a double. |
|
64 <<floorf>> and <<ceilf>> return the integer result as a float. |
|
65 |
|
66 PORTABILITY |
|
67 <<floor>> and <<ceil>> are ANSI. |
|
68 <<floorf>> and <<ceilf>> are extensions. |
|
69 |
|
70 |
|
71 */ |
|
72 |
|
73 /* |
|
74 * floor(x) |
|
75 * Return x rounded toward -inf to integral value |
|
76 * Method: |
|
77 * Bit twiddling. |
|
78 * Exception: |
|
79 * Inexact flag raised if x not equal to floor(x). |
|
80 */ |
|
81 |
|
82 #include "FDLIBM.H" |
|
83 |
|
84 static const double huge = 1.0e300; |
|
85 |
|
86 /** |
|
87 Round down value. |
|
88 Returns the largest integer that is less than or equal to x |
|
89 @return Floor of x. |
|
90 @param x Floating point value |
|
91 */ |
|
92 EXPORT_C double floor(double x) __SOFTFP |
|
93 { |
|
94 __int32_t i0,i1,j0; |
|
95 __uint32_t i,j; |
|
96 EXTRACT_WORDS(i0,i1,x); |
|
97 j0 = ((i0>>20)&0x7ff)-0x3ff; |
|
98 if(j0<20) { |
|
99 if(j0<0) { /* raise inexact if x != 0 */ |
|
100 if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ |
|
101 if(i0>=0) {i0=i1=0;} |
|
102 else if(((i0&0x7fffffff)|i1)!=0) |
|
103 { i0=0xbff00000;i1=0;} |
|
104 } |
|
105 } else { |
|
106 i = (0x000fffff)>>j0; |
|
107 if(((i0&i)|i1)==0) return x; /* x is integral */ |
|
108 if(huge+x>0.0) { /* raise inexact flag */ |
|
109 if(i0<0) i0 += (0x00100000)>>j0; |
|
110 i0 &= (~i); i1=0; |
|
111 } |
|
112 } |
|
113 } else if (j0>51) { |
|
114 if(j0==0x400) return x+x; /* inf or NaN */ |
|
115 else return x; /* x is integral */ |
|
116 } else { |
|
117 i = ((__uint32_t)(0xffffffff))>>(j0-20); |
|
118 if((i1&i)==0) return x; /* x is integral */ |
|
119 if(huge+x>0.0) { /* raise inexact flag */ |
|
120 if(i0<0) { |
|
121 if(j0==20) i0+=1; |
|
122 else { |
|
123 j = i1+(1<<(52-j0)); |
|
124 if(j<i1) i0 +=1 ; /* got a carry */ |
|
125 i1=j; |
|
126 } |
|
127 } |
|
128 i1 &= (~i); |
|
129 } |
|
130 } |
|
131 INSERT_WORDS(x,i0,i1); |
|
132 return x; |
|
133 } |