1 /* Copyright (C) 1992-2021 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public
6    License as published by the Free Software Foundation; either
7    version 3 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13 
14    You should have received a copy of the GNU General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #ifndef _IEEE754_H
19 
20 #define _IEEE754_H 1
21 
22 #ifndef _GL_GNULIB_HEADER
23 /* Ordinary glibc usage.  */
24 # include <features.h>
25 # include <endian.h>
26 #else
27 /* Gnulib usage.  */
28 #if defined(__DragonFLy__)
29 #include <machine/endian.h>
30 #endif
31 # ifndef __BEGIN_DECLS
32 #  ifdef __cplusplus
33 #   define __BEGIN_DECLS	extern "C" {
34 #   define __END_DECLS		}
35 #  else
36 #   define __BEGIN_DECLS
37 #   define __END_DECLS
38 #  endif
39 # endif
40 # ifndef __FLOAT_WORD_ORDER
41 #ifndef __BYTE_ORDER
42 #  define __LITTLE_ENDIAN	1234
43 #  define __BIG_ENDIAN		4321
44 #  ifdef WORDS_BIGENDIAN
45 #   define __BYTE_ORDER __BIG_ENDIAN
46 #  else
47 #   define __BYTE_ORDER __LITTLE_ENDIAN
48 #  endif
49 #endif
50 #  define __FLOAT_WORD_ORDER __BYTE_ORDER
51 # endif
52 #endif
53 
54 __BEGIN_DECLS
55 
56 union ieee754_float
57   {
58     float f;
59 
60     /* This is the IEEE 754 single-precision format.  */
61     struct
62       {
63 #if	__BYTE_ORDER == __BIG_ENDIAN
64 	unsigned int negative:1;
65 	unsigned int exponent:8;
66 	unsigned int mantissa:23;
67 #endif				/* Big endian.  */
68 #if	__BYTE_ORDER == __LITTLE_ENDIAN
69 	unsigned int mantissa:23;
70 	unsigned int exponent:8;
71 	unsigned int negative:1;
72 #endif				/* Little endian.  */
73       } ieee;
74 
75     /* This format makes it easier to see if a NaN is a signaling NaN.  */
76     struct
77       {
78 #if	__BYTE_ORDER == __BIG_ENDIAN
79 	unsigned int negative:1;
80 	unsigned int exponent:8;
81 	unsigned int quiet_nan:1;
82 	unsigned int mantissa:22;
83 #endif				/* Big endian.  */
84 #if	__BYTE_ORDER == __LITTLE_ENDIAN
85 	unsigned int mantissa:22;
86 	unsigned int quiet_nan:1;
87 	unsigned int exponent:8;
88 	unsigned int negative:1;
89 #endif				/* Little endian.  */
90       } ieee_nan;
91   };
92 
93 #define IEEE754_FLOAT_BIAS	0x7f /* Added to exponent.  */
94 
95 
96 union ieee754_double
97   {
98     double d;
99 
100     /* This is the IEEE 754 double-precision format.  */
101     struct
102       {
103 #if	__BYTE_ORDER == __BIG_ENDIAN
104 	unsigned int negative:1;
105 	unsigned int exponent:11;
106 	/* Together these comprise the mantissa.  */
107 	unsigned int mantissa0:20;
108 	unsigned int mantissa1:32;
109 #endif				/* Big endian.  */
110 #if	__BYTE_ORDER == __LITTLE_ENDIAN
111 # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
112 	unsigned int mantissa0:20;
113 	unsigned int exponent:11;
114 	unsigned int negative:1;
115 	unsigned int mantissa1:32;
116 # else
117 	/* Together these comprise the mantissa.  */
118 	unsigned int mantissa1:32;
119 	unsigned int mantissa0:20;
120 	unsigned int exponent:11;
121 	unsigned int negative:1;
122 # endif
123 #endif				/* Little endian.  */
124       } ieee;
125 
126     /* This format makes it easier to see if a NaN is a signaling NaN.  */
127     struct
128       {
129 #if	__BYTE_ORDER == __BIG_ENDIAN
130 	unsigned int negative:1;
131 	unsigned int exponent:11;
132 	unsigned int quiet_nan:1;
133 	/* Together these comprise the mantissa.  */
134 	unsigned int mantissa0:19;
135 	unsigned int mantissa1:32;
136 #else
137 # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
138 	unsigned int mantissa0:19;
139 	unsigned int quiet_nan:1;
140 	unsigned int exponent:11;
141 	unsigned int negative:1;
142 	unsigned int mantissa1:32;
143 # else
144 	/* Together these comprise the mantissa.  */
145 	unsigned int mantissa1:32;
146 	unsigned int mantissa0:19;
147 	unsigned int quiet_nan:1;
148 	unsigned int exponent:11;
149 	unsigned int negative:1;
150 # endif
151 #endif
152       } ieee_nan;
153   };
154 
155 #define IEEE754_DOUBLE_BIAS	0x3ff /* Added to exponent.  */
156 
157 
158 union ieee854_long_double
159   {
160     long double d;
161 
162     /* This is the IEEE 854 double-extended-precision format.  */
163     struct
164       {
165 #if	__BYTE_ORDER == __BIG_ENDIAN
166 	unsigned int negative:1;
167 	unsigned int exponent:15;
168 	unsigned int empty:16;
169 	unsigned int mantissa0:32;
170 	unsigned int mantissa1:32;
171 #endif
172 #if	__BYTE_ORDER == __LITTLE_ENDIAN
173 # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
174 	unsigned int exponent:15;
175 	unsigned int negative:1;
176 	unsigned int empty:16;
177 	unsigned int mantissa0:32;
178 	unsigned int mantissa1:32;
179 # else
180 	unsigned int mantissa1:32;
181 	unsigned int mantissa0:32;
182 	unsigned int exponent:15;
183 	unsigned int negative:1;
184 	unsigned int empty:16;
185 # endif
186 #endif
187       } ieee;
188 
189     /* This is for NaNs in the IEEE 854 double-extended-precision format.  */
190     struct
191       {
192 #if	__BYTE_ORDER == __BIG_ENDIAN
193 	unsigned int negative:1;
194 	unsigned int exponent:15;
195 	unsigned int empty:16;
196 	unsigned int one:1;
197 	unsigned int quiet_nan:1;
198 	unsigned int mantissa0:30;
199 	unsigned int mantissa1:32;
200 #endif
201 #if	__BYTE_ORDER == __LITTLE_ENDIAN
202 # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
203 	unsigned int exponent:15;
204 	unsigned int negative:1;
205 	unsigned int empty:16;
206 	unsigned int mantissa0:30;
207 	unsigned int quiet_nan:1;
208 	unsigned int one:1;
209 	unsigned int mantissa1:32;
210 # else
211 	unsigned int mantissa1:32;
212 	unsigned int mantissa0:30;
213 	unsigned int quiet_nan:1;
214 	unsigned int one:1;
215 	unsigned int exponent:15;
216 	unsigned int negative:1;
217 	unsigned int empty:16;
218 # endif
219 #endif
220       } ieee_nan;
221   };
222 
223 #define IEEE854_LONG_DOUBLE_BIAS 0x3fff
224 
225 __END_DECLS
226 
227 #endif /* ieee754.h */
228