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