1 |
207 |
jeremybenn |
|
2 |
|
|
/* @(#)s_isnan.c 5.1 93/09/24 */
|
3 |
|
|
/*
|
4 |
|
|
* ====================================================
|
5 |
|
|
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
6 |
|
|
*
|
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
|
10 |
|
|
* is preserved.
|
11 |
|
|
* ====================================================
|
12 |
|
|
*/
|
13 |
|
|
|
14 |
|
|
/*
|
15 |
|
|
FUNCTION
|
16 |
|
|
<<fpclassify>>, <<isfinite>>, <<isinf>>, <<isnan>>, and <<isnormal>>--floating-point classification macros; <<finite>>, <<finitef>>, <<isinf>>, <<isinff>>, <<isnan>>, <<isnanf>>--test for exceptional numbers
|
17 |
|
|
|
18 |
|
|
@c C99 (start
|
19 |
|
|
INDEX
|
20 |
|
|
fpclassify
|
21 |
|
|
INDEX
|
22 |
|
|
isfinite
|
23 |
|
|
INDEX
|
24 |
|
|
isinf
|
25 |
|
|
INDEX
|
26 |
|
|
isnan
|
27 |
|
|
INDEX
|
28 |
|
|
isnormal
|
29 |
|
|
@c C99 end)
|
30 |
|
|
@c SUSv2 (start
|
31 |
|
|
INDEX
|
32 |
|
|
isnan
|
33 |
|
|
INDEX
|
34 |
|
|
isinf
|
35 |
|
|
INDEX
|
36 |
|
|
finite
|
37 |
|
|
|
38 |
|
|
INDEX
|
39 |
|
|
isnanf
|
40 |
|
|
INDEX
|
41 |
|
|
isinff
|
42 |
|
|
INDEX
|
43 |
|
|
finitef
|
44 |
|
|
@c SUSv2 end)
|
45 |
|
|
|
46 |
|
|
ANSI_SYNOPSIS
|
47 |
|
|
[C99 standard macros:]
|
48 |
|
|
#include <math.h>
|
49 |
|
|
int fpclassify(real-floating <[x]>);
|
50 |
|
|
int isfinite(real-floating <[x]>);
|
51 |
|
|
int isinf(real-floating <[x]>);
|
52 |
|
|
int isnan(real-floating <[x]>);
|
53 |
|
|
int isnormal(real-floating <[x]>);
|
54 |
|
|
|
55 |
|
|
[Archaic SUSv2 functions:]
|
56 |
|
|
#include <ieeefp.h>
|
57 |
|
|
int isnan(double <[arg]>);
|
58 |
|
|
int isinf(double <[arg]>);
|
59 |
|
|
int finite(double <[arg]>);
|
60 |
|
|
int isnanf(float <[arg]>);
|
61 |
|
|
int isinff(float <[arg]>);
|
62 |
|
|
int finitef(float <[arg]>);
|
63 |
|
|
|
64 |
|
|
DESCRIPTION
|
65 |
|
|
<<fpclassify>>, <<isfinite>>, <<isinf>>, <<isnan>>, and <<isnormal>> are macros
|
66 |
|
|
defined for use in classifying floating-point numbers. This is a help because
|
67 |
|
|
of special "values" like NaN and infinities. In the synopses shown,
|
68 |
|
|
"real-floating" indicates that the argument is an expression of real floating
|
69 |
|
|
type. These function-like macros are C99 and POSIX-compliant, and should be
|
70 |
|
|
used instead of the now-archaic SUSv2 functions.
|
71 |
|
|
|
72 |
|
|
The <<fpclassify>> macro classifies its argument value as NaN, infinite, normal,
|
73 |
|
|
subnormal, zero, or into another implementation-defined category. First, an
|
74 |
|
|
argument represented in a format wider than its semantic type is converted to
|
75 |
|
|
its semantic type. Then classification is based on the type of the argument.
|
76 |
|
|
The <<fpclassify>> macro returns the value of the number classification macro
|
77 |
|
|
appropriate to the value of its argument:
|
78 |
|
|
|
79 |
|
|
o+
|
80 |
|
|
o FP_INFINITE
|
81 |
|
|
<[x]> is either plus or minus infinity;
|
82 |
|
|
o FP_NAN
|
83 |
|
|
<[x]> is "Not A Number" (plus or minus);
|
84 |
|
|
o FP_NORMAL
|
85 |
|
|
<[x]> is a "normal" number (i.e. is none of the other special forms);
|
86 |
|
|
o FP_SUBNORMAL
|
87 |
|
|
<[x]> is too small be stored as a regular normalized number (i.e. loss of precision is likely); or
|
88 |
|
|
o FP_ZERO
|
89 |
|
|
<[x]> is 0 (either plus or minus).
|
90 |
|
|
o-
|
91 |
|
|
|
92 |
|
|
The "<<is>>" set of macros provide a useful set of shorthand ways for
|
93 |
|
|
classifying floating-point numbers, providing the following equivalent
|
94 |
|
|
relations:
|
95 |
|
|
|
96 |
|
|
o+
|
97 |
|
|
o <<isfinite>>(<[x]>)
|
98 |
|
|
returns non-zero if <[x]> is finite. (It is equivalent to
|
99 |
|
|
(<<fpclassify>>(<[x]>) != FP_INFINITE && <<fpclassify>>(<[x]>) != FP_NAN).)
|
100 |
|
|
|
101 |
|
|
o <<isinf>>(<[x]>)
|
102 |
|
|
returns non-zero if <[x]> is infinite. (It is equivalent to
|
103 |
|
|
(<<fpclassify>>(<[x]>) == FP_INFINITE).)
|
104 |
|
|
|
105 |
|
|
o <<isnan>>(<[x]>)
|
106 |
|
|
returns non-zero if <[x]> is NaN. (It is equivalent to
|
107 |
|
|
(<<fpclassify>>(<[x]>) == FP_NAN).)
|
108 |
|
|
|
109 |
|
|
o <<isnormal>>(<[x]>)
|
110 |
|
|
returns non-zero if <[x]> is normal. (It is equivalent to
|
111 |
|
|
(<<fpclassify>>(<[x]>) == FP_NORMAL).)
|
112 |
|
|
o-
|
113 |
|
|
|
114 |
|
|
The archaic SUSv2 functions provide information on the floating-point
|
115 |
|
|
argument supplied.
|
116 |
|
|
|
117 |
|
|
There are five major number formats ("exponent" referring to the
|
118 |
|
|
biased exponent in the binary-encoded number):
|
119 |
|
|
o+
|
120 |
|
|
o zero
|
121 |
|
|
A number which contains all zero bits, excluding the sign bit.
|
122 |
|
|
o subnormal
|
123 |
|
|
A number with a zero exponent but a nonzero fraction.
|
124 |
|
|
o normal
|
125 |
|
|
A number with an exponent and a fraction.
|
126 |
|
|
o infinity
|
127 |
|
|
A number with an all 1's exponent and a zero fraction.
|
128 |
|
|
o NAN
|
129 |
|
|
A number with an all 1's exponent and a nonzero fraction.
|
130 |
|
|
|
131 |
|
|
o-
|
132 |
|
|
|
133 |
|
|
<<isnan>> returns 1 if the argument is a nan. <<isinf>>
|
134 |
|
|
returns 1 if the argument is infinity. <<finite>> returns 1 if the
|
135 |
|
|
argument is zero, subnormal or normal.
|
136 |
|
|
|
137 |
|
|
The <<isnanf>>, <<isinff>> and <<finitef>> functions perform the same
|
138 |
|
|
operations as their <<isnan>>, <<isinf>> and <<finite>>
|
139 |
|
|
counterparts, but on single-precision floating-point numbers.
|
140 |
|
|
|
141 |
|
|
It should be noted that the C99 standard dictates that <<isnan>>
|
142 |
|
|
and <<isinf>> are macros that operate on multiple types of
|
143 |
|
|
floating-point. The SUSv2 standard declares <<isnan>> as
|
144 |
|
|
a function taking double. Newlib has decided to declare
|
145 |
|
|
them both as macros in math.h and as functions in ieeefp.h to
|
146 |
|
|
maintain backward compatibility.
|
147 |
|
|
|
148 |
|
|
RETURNS
|
149 |
|
|
@comment Formatting note: "$@" forces a new line
|
150 |
|
|
The fpclassify macro returns the value corresponding to the appropriate FP_ macro.@*
|
151 |
|
|
The isfinite macro returns nonzero if <[x]> is finite, else 0.@*
|
152 |
|
|
The isinf macro returns nonzero if <[x]> is infinite, else 0.@*
|
153 |
|
|
The isnan macro returns nonzero if <[x]> is an NaN, else 0.@*
|
154 |
|
|
The isnormal macro returns nonzero if <[x]> has a normal value, else 0.
|
155 |
|
|
|
156 |
|
|
PORTABILITY
|
157 |
|
|
math.h macros are C99, POSIX.
|
158 |
|
|
|
159 |
|
|
ieeefp.h funtions are outdated and should be avoided.
|
160 |
|
|
|
161 |
|
|
QUICKREF
|
162 |
|
|
isnan - pure
|
163 |
|
|
QUICKREF
|
164 |
|
|
isinf - pure
|
165 |
|
|
QUICKREF
|
166 |
|
|
finite - pure
|
167 |
|
|
QUICKREF
|
168 |
|
|
isnan - pure
|
169 |
|
|
QUICKREF
|
170 |
|
|
isinf - pure
|
171 |
|
|
QUICKREF
|
172 |
|
|
finite - pure
|
173 |
|
|
*/
|
174 |
|
|
|
175 |
|
|
/*
|
176 |
|
|
* isnan(x) returns 1 is x is nan, else 0;
|
177 |
|
|
* no branching!
|
178 |
|
|
*
|
179 |
|
|
* The C99 standard dictates that isnan is a macro taking
|
180 |
|
|
* multiple floating-point types while the SUSv2 standard
|
181 |
|
|
* notes it is a function taking a double argument. Newlib
|
182 |
|
|
* has chosen to implement it as a macro in <math.h> and
|
183 |
|
|
* declare it as a function in <ieeefp.h>.
|
184 |
|
|
*/
|
185 |
|
|
|
186 |
|
|
#include "fdlibm.h"
|
187 |
|
|
#include <ieeefp.h>
|
188 |
|
|
|
189 |
|
|
#ifndef _DOUBLE_IS_32BITS
|
190 |
|
|
|
191 |
|
|
#ifdef __STDC__
|
192 |
|
|
int isnan(double x)
|
193 |
|
|
#else
|
194 |
|
|
int isnan(x)
|
195 |
|
|
double x;
|
196 |
|
|
#endif
|
197 |
|
|
{
|
198 |
|
|
__int32_t hx,lx;
|
199 |
|
|
EXTRACT_WORDS(hx,lx,x);
|
200 |
|
|
hx &= 0x7fffffff;
|
201 |
|
|
hx |= (__uint32_t)(lx|(-lx))>>31;
|
202 |
|
|
hx = 0x7ff00000 - hx;
|
203 |
|
|
return (int)(((__uint32_t)(hx))>>31);
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
#endif /* _DOUBLE_IS_32BITS */
|