xref: /illumos-gate/usr/src/lib/libc/port/fp/fpparts.h (revision 1f5207b7604fb44407eb4342aff613f7c4508508)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 /*
34  * Macros to pull apart parts of single and  double precision
35  * floating point numbers in IEEE format
36  * Be sure to include /usr/include/values.h before including
37  * this file to get the required definition of _IEEE
38  */
39 
40 #if _IEEE
41 #if defined(__sparc)
42 /* byte order with high order bits at lowest address */
43 
44 /* double precision */
45 typedef  union {
46 	struct {
47 		unsigned  sign	:1;
48 		unsigned  exp	:11;
49 		unsigned  hi	:20;
50 		unsigned  lo	:32;
51 	} fparts;
52 	struct {
53 		unsigned  sign	:1;
54 		unsigned  exp	:11;
55 		unsigned  qnan_bit	:1;
56 		unsigned  hi	:19;
57 		unsigned  lo	:32;
58 	} nparts;
59 	struct {
60 		unsigned hi;
61 		unsigned lo;
62 	} fwords;
63 	double	d;
64 } _dval;
65 
66 /* single precision */
67 typedef  union {
68 	struct {
69 		unsigned sign	:1;
70 		unsigned exp	:8;
71 		unsigned fract	:23;
72 	} fparts;
73 	struct {
74 		unsigned sign	:1;
75 		unsigned exp	:8;
76 		unsigned qnan_bit	:1;
77 		unsigned fract	:22;
78 	} nparts;
79 	unsigned long	fword;
80 	float	f;
81 } _fval;
82 
83 
84 #elif defined(__i386) || defined(__amd64)
85 /* byte order with low order bits at lowest address */
86 
87 /* double precision */
88 typedef  union {
89 	struct {
90 		unsigned  lo	:32;
91 		unsigned  hi	:20;
92 		unsigned  exp	:11;
93 		unsigned  sign	:1;
94 	} fparts;
95 	struct {
96 		unsigned  lo	:32;
97 		unsigned  hi	:19;
98 		unsigned  qnan_bit	:1;
99 		unsigned  exp	:11;
100 		unsigned  sign	:1;
101 	} nparts;
102 	struct {
103 		unsigned  lo	:32;
104 		unsigned  hi	:32;
105 	} fwords;
106 	double	d;
107 } _dval;
108 
109 /* single precision */
110 typedef  union {
111 	struct {
112 		unsigned fract	:23;
113 		unsigned exp	:8;
114 		unsigned sign	:1;
115 	} fparts;
116 	struct {
117 		unsigned fract	:22;
118 		unsigned qnan_bit	:1;
119 		unsigned exp	:8;
120 		unsigned sign	:1;
121 	} nparts;
122 	unsigned long	fword;
123 	float	f;
124 } _fval;
125 #endif
126 
127 /* parts of a double precision floating point number */
128 #define	SIGNBIT(X)	(((_dval *)&(X))->fparts.sign)
129 #define	EXPONENT(X)	(((_dval *)&(X))->fparts.exp)
130 
131 #define	HIFRACTION(X)	(((_dval *)&(X))->fparts.hi)
132 #define	LOFRACTION(X)	(((_dval *)&(X))->fparts.lo)
133 #define	QNANBIT(X)	(((_dval *)&(X))->nparts.qnan_bit)
134 #define	HIWORD(X)	(((_dval *)&(X))->fwords.hi)
135 #define	LOWORD(X)	(((_dval *)&(X))->fwords.lo)
136 
137 #define	MAXEXP	0x7ff /* maximum exponent of double */
138 #define	ISMAXEXP(X)	((EXPONENT(X)) == MAXEXP)
139 
140 /* macros used to create quiet NaNs as return values */
141 #define	SETQNAN(X)	((((_dval *)&(X))->nparts.qnan_bit) = 0x1)
142 #define	HIQNAN(X)	((HIWORD(X)) = 0x7ff80000)
143 #define	LOQNAN(X)	((((_dval *)&(X))->fwords.lo) = 0x0)
144 
145 /* macros used to extract parts of single precision values */
146 #define	FSIGNBIT(X)	(((_fval *)&(X))->fparts.sign)
147 #define	FEXPONENT(X)	(((_fval *)&(X))->fparts.exp)
148 #define	FFRACTION(X)	(((_fval *)&(X))->fparts.fract)
149 
150 #define	FWORD(X)	(((_fval *)&(X))->fword)
151 #define	FQNANBIT(X)	(((_fval *)&(X))->nparts.qnan_bit)
152 #define	MAXEXPF	255 /* maximum exponent of single */
153 #define	FISMAXEXP(X)	((FEXPONENT(X)) == MAXEXPF)
154 
155 #endif  /* _IEEE */
156