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