1 | |
---|
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 | 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 <math.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 functions and as macros in math.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.1-2001. |
---|
158 | |
---|
159 | The functions originate from BSD; isnan was listed in the X/Open |
---|
160 | Portability Guide and Single Unix Specification, but was dropped when |
---|
161 | the macro was standardized in POSIX.1-2001. |
---|
162 | |
---|
163 | QUICKREF |
---|
164 | isnan - pure |
---|
165 | QUICKREF |
---|
166 | isinf - pure |
---|
167 | QUICKREF |
---|
168 | finite - pure |
---|
169 | QUICKREF |
---|
170 | isnan - pure |
---|
171 | QUICKREF |
---|
172 | isinf - pure |
---|
173 | QUICKREF |
---|
174 | finite - pure |
---|
175 | */ |
---|
176 | |
---|
177 | /* |
---|
178 | * isnan(x) returns 1 is x is nan, else 0; |
---|
179 | * no branching! |
---|
180 | * |
---|
181 | * The C99 standard dictates that isnan is a macro taking |
---|
182 | * multiple floating-point types while the SUSv2 standard |
---|
183 | * notes it is a function taking a double argument. Newlib |
---|
184 | * has chosen to declare it both as a function and as a macro in |
---|
185 | * <math.h> for compatibility. |
---|
186 | */ |
---|
187 | |
---|
188 | #include "fdlibm.h" |
---|
189 | #include <ieeefp.h> |
---|
190 | |
---|
191 | #ifndef _DOUBLE_IS_32BITS |
---|
192 | |
---|
193 | #undef isnan |
---|
194 | |
---|
195 | #ifdef __STDC__ |
---|
196 | int isnan(double x) |
---|
197 | #else |
---|
198 | int isnan(x) |
---|
199 | double x; |
---|
200 | #endif |
---|
201 | { |
---|
202 | __int32_t hx,lx; |
---|
203 | EXTRACT_WORDS(hx,lx,x); |
---|
204 | hx &= 0x7fffffff; |
---|
205 | hx |= (__uint32_t)(lx|(-lx))>>31; |
---|
206 | hx = 0x7ff00000 - hx; |
---|
207 | return (int)(((__uint32_t)(hx))>>31); |
---|
208 | } |
---|
209 | |
---|
210 | #endif /* _DOUBLE_IS_32BITS */ |
---|