OSDN Git Service

Add MS7619SE
[uclinux-h8/uClinux-dist.git] / lib / libm / isnan.c
1 /*                                                      isnan()
2  *                                                      signbit()
3  *                                                      isfinite()
4  *
5  *      Floating point numeric utilities
6  *
7  *
8  *
9  * SYNOPSIS:
10  *
11  * double ceil(), floor(), frexp(), ldexp();
12  * int signbit(), isnan(), isfinite();
13  * double x, y;
14  * int expnt, n;
15  *
16  * y = floor(x);
17  * y = ceil(x);
18  * y = frexp( x, &expnt );
19  * y = ldexp( x, n );
20  * n = signbit(x);
21  * n = isnan(x);
22  * n = isfinite(x);
23  *
24  *
25  *
26  * DESCRIPTION:
27  *
28  * All four routines return a double precision floating point
29  * result.
30  *
31  * floor() returns the largest integer less than or equal to x.
32  * It truncates toward minus infinity.
33  *
34  * ceil() returns the smallest integer greater than or equal
35  * to x.  It truncates toward plus infinity.
36  *
37  * frexp() extracts the exponent from x.  It returns an integer
38  * power of two to expnt and the significand between 0.5 and 1
39  * to y.  Thus  x = y * 2**expn.
40  *
41  * ldexp() multiplies x by 2**n.
42  *
43  * signbit(x) returns 1 if the sign bit of x is 1, else 0.
44  *
45  * These functions are part of the standard C run time library
46  * for many but not all C compilers.  The ones supplied are
47  * written in C for either DEC or IEEE arithmetic.  They should
48  * be used only if your compiler library does not already have
49  * them.
50  *
51  * The IEEE versions assume that denormal numbers are implemented
52  * in the arithmetic.  Some modifications will be required if
53  * the arithmetic has abrupt rather than gradual underflow.
54  */
55 \f
56
57 /*
58 Cephes Math Library Release 2.3:  March, 1995
59 Copyright 1984, 1995 by Stephen L. Moshier
60 */
61
62
63 #include "mconf.h"
64
65 #ifdef UNK
66 /* ceil(), floor(), frexp(), ldexp() may need to be rewritten. */
67 #undef UNK
68 #if BIGENDIAN
69 #define MIEEE 1
70 #else
71 #define IBMPC 1
72 #endif
73 #endif
74
75
76 /* Return 1 if the sign bit of x is 1, else 0.  */
77
78 int signbit(x)
79 double x;
80 {
81 union
82         {
83         double d;
84         short s[4];
85         int i[2];
86         } u;
87
88 u.d = x;
89
90 if( sizeof(int) == 4 )
91         {
92 #ifdef IBMPC
93         return( u.i[1] < 0 );
94 #endif
95 #ifdef DEC
96         return( u.s[3] < 0 );
97 #endif
98 #ifdef MIEEE
99         return( u.i[0] < 0 );
100 #endif
101         }
102 else
103         {
104 #ifdef IBMPC
105         return( u.s[3] < 0 );
106 #endif
107 #ifdef DEC
108         return( u.s[3] < 0 );
109 #endif
110 #ifdef MIEEE
111         return( u.s[0] < 0 );
112 #endif
113         }
114 }
115
116
117 /* Return 1 if x is a number that is Not a Number, else return 0.  */
118
119 int isnan(x)
120 double x;
121 {
122 #ifdef NANS
123 union
124         {
125         double d;
126         unsigned short s[4];
127         unsigned int i[2];
128         } u;
129
130 u.d = x;
131
132 if( sizeof(int) == 4 )
133         {
134 #ifdef IBMPC
135         if( ((u.i[1] & 0x7ff00000) == 0x7ff00000)
136             && (((u.i[1] & 0x000fffff) != 0) || (u.i[0] != 0)))
137                 return 1;
138 #endif
139 #ifdef DEC
140         if( (u.s[1] & 0x7fff) == 0)
141                 {
142                 if( (u.s[2] | u.s[1] | u.s[0]) != 0 )
143                         return(1);
144                 }
145 #endif
146 #ifdef MIEEE
147         if( ((u.i[0] & 0x7ff00000) == 0x7ff00000)
148             && (((u.i[0] & 0x000fffff) != 0) || (u.i[1] != 0)))
149                 return 1;
150 #endif
151         return(0);
152         }
153 else
154         { /* size int not 4 */
155 #ifdef IBMPC
156         if( (u.s[3] & 0x7ff0) == 0x7ff0)
157                 {
158                 if( ((u.s[3] & 0x000f) | u.s[2] | u.s[1] | u.s[0]) != 0 )
159                         return(1);
160                 }
161 #endif
162 #ifdef DEC
163         if( (u.s[3] & 0x7fff) == 0)
164                 {
165                 if( (u.s[2] | u.s[1] | u.s[0]) != 0 )
166                         return(1);
167                 }
168 #endif
169 #ifdef MIEEE
170         if( (u.s[0] & 0x7ff0) == 0x7ff0)
171                 {
172                 if( ((u.s[0] & 0x000f) | u.s[1] | u.s[2] | u.s[3]) != 0 )
173                         return(1);
174                 }
175 #endif
176         return(0);
177         } /* size int not 4 */
178
179 #else
180 /* No NANS.  */
181 return(0);
182 #endif
183 }
184
185
186 /* Return 1 if x is not infinite and is not a NaN.  */
187
188 int isfinite(x)
189 double x;
190 {
191 #ifdef INFINITIES
192 union
193         {
194         double d;
195         unsigned short s[4];
196         unsigned int i[2];
197         } u;
198
199 u.d = x;
200
201 if( sizeof(int) == 4 )
202         {
203 #ifdef IBMPC
204         if( (u.i[1] & 0x7ff00000) != 0x7ff00000)
205                 return 1;
206 #endif
207 #ifdef DEC
208         if( (u.s[3] & 0x7fff) != 0)
209                 return 1;
210 #endif
211 #ifdef MIEEE
212         if( (u.i[0] & 0x7ff00000) != 0x7ff00000)
213                 return 1;
214 #endif
215         return(0);
216         }
217 else
218         {
219 #ifdef IBMPC
220         if( (u.s[3] & 0x7ff0) != 0x7ff0)
221                 return 1;
222 #endif
223 #ifdef DEC
224         if( (u.s[3] & 0x7fff) != 0)
225                 return 1;
226 #endif
227 #ifdef MIEEE
228         if( (u.s[0] & 0x7ff0) != 0x7ff0)
229                 return 1;
230 #endif
231         return(0);
232         }
233 #else
234 /* No INFINITY.  */
235 return(1);
236 #endif
237 }