source: trunk/libs/newlib/src/newlib/libm/common/s_isnan.c @ 577

Last change on this file since 577 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 5.8 KB
Line 
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/*
15FUNCTION
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
19INDEX
20        fpclassify
21INDEX
22        isfinite
23INDEX
24        isinf
25INDEX
26        isnan
27INDEX
28        isnormal
29@c C99 end)
30@c SUSv2 (start
31INDEX
32        isnan
33INDEX
34        isinf
35INDEX
36        finite
37
38INDEX
39        isnanf
40INDEX
41        isinff
42INDEX
43        finitef
44@c SUSv2 end)
45
46SYNOPSIS
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
64DESCRIPTION
65<<fpclassify>>, <<isfinite>>, <<isinf>>, <<isnan>>, and <<isnormal>> are macros
66defined for use in classifying floating-point numbers.  This is a help because
67of special "values" like NaN and infinities.  In the synopses shown,
68"real-floating" indicates that the argument is an expression of real floating
69type.  These function-like macros are C99 and POSIX-compliant, and should be
70used instead of the now-archaic SUSv2 functions.
71
72The <<fpclassify>> macro classifies its argument value as NaN, infinite, normal,
73subnormal, zero, or into another implementation-defined category.  First, an
74argument represented in a format wider than its semantic type is converted to
75its semantic type.  Then classification is based on the type of the argument.
76The <<fpclassify>> macro returns the value of the number classification macro
77appropriate to the value of its argument:
78
79o+
80o FP_INFINITE
81        <[x]> is either plus or minus infinity;
82o FP_NAN
83        <[x]> is "Not A Number" (plus or minus);
84o FP_NORMAL
85        <[x]> is a "normal" number (i.e. is none of the other special forms);
86o FP_SUBNORMAL
87        <[x]> is too small be stored as a regular normalized number (i.e. loss of precision is likely); or
88o FP_ZERO
89        <[x]> is 0 (either plus or minus).
90o-
91
92The "<<is>>" set of macros provide a useful set of shorthand ways for
93classifying floating-point numbers, providing the following equivalent
94relations:
95
96o+
97o <<isfinite>>(<[x]>)
98returns non-zero if <[x]> is finite.  (It is equivalent to
99(<<fpclassify>>(<[x]>) != FP_INFINITE  &&  <<fpclassify>>(<[x]>) != FP_NAN).)
100
101o <<isinf>>(<[x]>)
102returns non-zero if <[x]> is infinite.  (It is equivalent to
103(<<fpclassify>>(<[x]>) == FP_INFINITE).)
104
105o <<isnan>>(<[x]>)
106returns non-zero if <[x]> is NaN.  (It is equivalent to
107(<<fpclassify>>(<[x]>) == FP_NAN).)
108
109o <<isnormal>>(<[x]>)
110returns non-zero if <[x]> is normal.  (It is equivalent to
111(<<fpclassify>>(<[x]>) == FP_NORMAL).)
112o-
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
148RETURNS
149@comment Formatting note:  "$@" forces a new line
150The fpclassify macro returns the value corresponding to the appropriate FP_ macro.@*
151The isfinite macro returns nonzero if <[x]> is finite, else 0.@*
152The isinf macro returns nonzero if <[x]> is infinite, else 0.@*
153The isnan macro returns nonzero if <[x]> is an NaN, else 0.@*
154The isnormal macro returns nonzero if <[x]> has a normal value, else 0.
155
156PORTABILITY
157math.h macros are C99, POSIX.1-2001.
158
159The functions originate from BSD; isnan was listed in the X/Open
160Portability Guide and Single Unix Specification, but was dropped when
161the macro was standardized in POSIX.1-2001.
162
163QUICKREF
164        isnan - pure
165QUICKREF
166        isinf - pure
167QUICKREF
168        finite - pure
169QUICKREF
170        isnan - pure
171QUICKREF
172        isinf - pure
173QUICKREF
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 */
Note: See TracBrowser for help on using the repository browser.