xref: /titanic_51/usr/src/lib/libc/port/fp/finite.c (revision b9bd317cda1afb3a01f4812de73e8cec888cbbd7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*	IEEE recommended functions */
33 
34 #pragma weak _finite = finite
35 #pragma weak _fpclass = fpclass
36 #pragma weak _unordered = unordered
37 
38 #include "lint.h"
39 #include <values.h>
40 #include "fpparts.h"
41 
42 #define	P754_NOFAULT 1		/* avoid generating extra code */
43 #include <ieeefp.h>
44 
45 /*
46  * FINITE(X)
47  * finite(x) returns 1 if x > -inf and x < +inf and 0 otherwise
48  * NaN returns 0
49  */
50 
51 int
52 finite(double x)
53 {
54 	return ((EXPONENT(x) != MAXEXP));
55 }
56 
57 /*
58  * UNORDERED(x,y)
59  * unordered(x,y) returns 1 if x is unordered with y, otherwise
60  * it returns 0; x is unordered with y if either x or y is NAN
61  */
62 
63 int
64 unordered(double x, double y)
65 {
66 	if ((EXPONENT(x) == MAXEXP) && (HIFRACTION(x) || LOFRACTION(x)))
67 		return (1);
68 	if ((EXPONENT(y) == MAXEXP) && (HIFRACTION(y) || LOFRACTION(y)))
69 		return (1);
70 	return (0);
71 }
72 
73 /*
74  * FPCLASS(X)
75  * fpclass(x) returns the floating point class x belongs to
76  */
77 
78 fpclass_t
79 fpclass(double x)
80 {
81 	int	sign, exp;
82 
83 	exp = EXPONENT(x);
84 	sign = SIGNBIT(x);
85 	if (exp == 0) { /* de-normal or zero */
86 		if (HIFRACTION(x) || LOFRACTION(x)) /* de-normal */
87 			return (sign ? FP_NDENORM : FP_PDENORM);
88 		else
89 			return (sign ? FP_NZERO : FP_PZERO);
90 	}
91 	if (exp == MAXEXP) { /* infinity or NaN */
92 		if ((HIFRACTION(x) == 0) && (LOFRACTION(x) == 0)) /* infinity */
93 			return (sign ? FP_NINF : FP_PINF);
94 		else
95 			if (QNANBIT(x))
96 			/* hi-bit of mantissa set - quiet nan */
97 				return (FP_QNAN);
98 			else	return (FP_SNAN);
99 	}
100 	/* if we reach here we have non-zero normalized number */
101 	return (sign ? FP_NNORM : FP_PNORM);
102 }
103