Wheefun I/O Library  0.0.5
Useful I/O Primitives.
ieee754.h
Go to the documentation of this file.
1 /* WFIO - the Wheefun IO Library
2  Copyright (C) 2018 Phillip Kilgore
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even t implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 
17  Additionally, you should have recieved a copy of the GNU Classpath
18  exception, which amends the license to generally permit linking against
19  the software provided herein.
20 */
21 
40 #ifndef WFIO_IEEE754_H
41 #define WFIO_IEEE754_H
42 
43 #include <wfio/type.h>
44 #include <wfio/byteswap.h>
45 #include <wfcnfo/float.h>
46 
47 #ifdef WFIO_DOXYGEN
48 
52 #define WFIO_IEEE754_NATIVE_B16
53 
57 #define WFIO_IEEE754_NATIVE_B32
58 
62 #define WFIO_IEEE754_NATIVE_B64
63 /* Use WFCNFO to determine native support */
64 #else
65 
66 #ifdef WFCNFO_IEEE754_NATIVE_B16
67 #define WFIO_IEEE754_NATIVE_B16
68 #endif
69 
70 #ifdef WFCNFO_IEEE754_NATIVE_B32
71 #define WFIO_IEEE754_NATIVE_B32
72 #endif
73 
74 #ifdef WFCNFO_IEEE754_NATIVE_B64
75 #define WFIO_IEEE754_NATIVE_B64
76 #endif
77 
78 #ifdef WFCNFO_FLT_IS_IEEE754
79 #define WFIO_FLT_IS_IEEE754
80 #endif
81 #endif
82 
83 #if defined(WFIO_IEEE754_NATIVE_B32) && defined(WFIO_IEEE754_NATIVE_B64)
84 
87 #define WFIO_IEEE754_NATIVE
88 #endif
89 
90 
91 
92 #ifdef WFIO_DOXYGEN
93 #undef WFIO_IEEE754_NATIVE_B16
94 #undef WFIO_IEEE754_NATIVE_B32
95 #undef WFIO_IEEE754_NATIVE_B64
96 #undef WFIO_IEEE754_NATIVE
97 #else
98 #define WFIO_MAYBE_INLINE WFIO_DLL
99 #endif
100 
102 
111 typedef union wfio_ieee754_b16_t {
115  struct {
117  wfio_uint16_t significand:10;
119  wfio_uint16_t exponent:5;
121  wfio_uint16_t sign:1;
122  } bits;
127  wfio_sint16_t si;
128 
133  wfio_uint16_t ui;
134 
135  #ifdef WFIO_IEEE754_NATIVE_B16
136  wfio_flt16_t host_;
137  #endif
139 
149 typedef union wfio_ieee754_b32_t {
153  struct {
155  wfio_uint32_t significand:23;
157  wfio_uint32_t exponent:8;
159  wfio_uint32_t sign:1;
160  } bits;
161 
166  wfio_sint32_t si;
167 
172  wfio_uint32_t ui;
173 
174  #ifdef WFIO_IEEE754_NATIVE_B32
175 
183  wfio_flt32_t host_;
184  #endif
186 
196 typedef union wfio_ieee754_b64_t {
200  struct {
202  wfio_uint64_t significand:52;
204  wfio_uint64_t exponent:11;
206  wfio_uint64_t sign:1;
207  } bits;
208 
213  wfio_sint64_t si;
214 
219  wfio_uint64_t ui;
220 
221  #ifdef WFIO_IEEE754_NATIVE_B64
222  wfio_flt64_t host_;
223  #endif
225 
227 
228 /* Determine how to do transformations in situ */
229 #ifdef WFIO_FLT_IS_IEEE754
230 #if WFIO_ENDIAN_HOST_REPORTED==WFIO_ENDIAN_FLT_REPORTED
231 #define WFIO_IEEE754_SET_(R, X, N) (R).host_ = X
232 #define WFIO_IEEE754_GET_(X, N) (X).host_
233 
234 #elif (WFIO_ENDIAN_IS_BIG_ENDIAN | WFIO_ENDIAN_HOST_IS_LIL) == 3
235 #define WFIO_IEEE754_SET_(R, X, N) (R).si = wfio_swapSI##N((\
236  (wfio_ieee754_b##N##_t*) &X)->si)
237 #define WFIO_IEEE754_GET_(X, N) wfio_swapSI32((X).si)
238 #else
239 #include <wfio/endian.h>
240 
241 #define WFIO_IEEE754_SET_(R, X, N) (R).si = wfio_toHostEndianSI##N((\
242  (wfio_ieee754_b##N##_t*) &X)->si, WFIO_ENDIAN_FLT)
243 #define WFIO_IEEE754_GET_(X, N) ((X).si = wfio_fromHostEndianSI##N((X).si,\
244  WFIO_ENDIAN_FLT), (X).host_)
245 #endif
246 #endif
247 
252 #define WFIO_IEEE754_B32_MIN (wfio_ieee854_b32_t) 0x1
253 
258 #define WFIO_IEEE754_B32_MAX (wfio_ieee754_b32_t) 0x7EFFFFFF
259 
264 #define WFIO_IEEE754_B32_INF (wfio_ieee754_b32_t) 0x7F000000
265 
266 #define WFIO_IEEE754_B32_ENAN (wfio_ieee754_b32_t) 0x7FFFFFFF
267 
269 
270 enum {
271  WFIO_IEEE754_B32_ENAN_RAW = 0x7FFFFFFF,
272  WFIO_IEEE754_B64_ENAN_RAW = 0x7FFFFFFFFFFFFFFF,
273 };
274 
275 /* Conversion functions */
276 #ifdef WFIO_IEEE754_NATIVE_B32
278  wfio_flt32_t host, int* error) {
279  register wfio_ieee754_b32_t ret;
280  WFIO_IEEE754_SET_(ret, host, 32);
281  return ret;
282 }
283 
285  wfio_ieee754_b32_t ieee, int* error) {
286  return WFIO_IEEE754_GET_(ieee, 32);
287 }
288 #else
289 
299  int* error);
300 
310  wfio_ieee754_b32_t ieee, int* error);
311 #endif
312 
313 WFIO_DLL size_t wfio_ieee754_toB32V2(const wfio_flt32_t* src,
314  wfio_ieee754_b32_t* dest, size_t len, int* error);
315 
316 
317 WFIO_DLL size_t wfio_ieee754_fromB32V2(const wfio_ieee754_b32_t* src,
318  wfio_flt32_t* dest, size_t len, int* error);
319 
320 /* Literal formation */
321 
322 #ifdef WFIO_IEEE754_NATIVE_B64
324  wfio_flt64_t host, int* error) {
325  register wfio_ieee754_b64_t ret;
326  WFIO_IEEE754_SET_(ret, host, 64);
327  return ret;
328 }
329 
331  wfio_ieee754_b64_t ieee, int* error) {
332  return WFIO_IEEE754_GET_(ieee, 64);
333 }
334 #else
335 
344  int* error);
345 
356  int* error);
357 #endif
358 
359 
360 WFIO_DLL size_t wfio_ieee754_toB64V2(const wfio_flt64_t* src,
361  wfio_ieee754_b64_t* dest, size_t len, int* error);
362 
363 
364 WFIO_DLL size_t wfio_ieee754_fromB64V2(const wfio_ieee754_b64_t* src,
365  wfio_flt64_t* dest, size_t len, int* error);
366 
367 #ifdef WFIO_MAYBE_INLINE
368 #undef WFIO_MAYBE_INLINE
369 #endif
370 
371 #ifdef WFIO_FLT_SET_
372 #undef WFIO_FLT_SET_
373 #undef WFIO_FLT_GET_
374 #endif
375 
377 
378 #endif
union wfio_ieee754_b32_t wfio_ieee754_b32_t
A single-precision IEEE 754 binary floating point value (binary32).
A double-precision IEEE 754 binary floating point value (binary64).
Definition: ieee754.h:196
#define WFIO_MAYBE_INLINE
Potentially mark this symbol for inlining.
Definition: host.h:466
wfio_uint32_t significand
Definition: ieee754.h:155
wfio_uint64_t exponent
Definition: ieee754.h:204
#define WFIO_DLL
Mark the symbol as an element of the library.
Definition: host.h:397
#define WFIO_C_BEGIN
Definition: host.h:477
wfio_uint16_t sign
Definition: ieee754.h:121
wfio_uint16_t exponent
Definition: ieee754.h:119
WFIO_MAYBE_INLINE wfio_flt32_t wfio_ieee754_fromB32(wfio_ieee754_b32_t ieee, int *error)
Convert a 32-bit floating point number from IEEE 754 binary32 format.
wfio_uint64_t sign
Definition: ieee754.h:206
#define WFIO_ALWAYS_INLINE
Mark this symbol to be forced inline.
Definition: host.h:447
wfio_sint64_t si
The raw signed 64-bit integer representation of this number.
Definition: ieee754.h:213
WFIO_MAYBE_INLINE wfio_ieee754_b32_t wfio_ieee754_toB32(wfio_flt32_t host, int *error)
Convert a 32-bit floating point number to IEEE 754 binary32 format.
wfio_uint64_t significand
Definition: ieee754.h:202
Endian manipulation functions.
A single-precision IEEE 754 binary floating point value (binary32).
Definition: ieee754.h:149
wfio_uint16_t ui
The raw unsigned 16-bit integer representation of this number.
Definition: ieee754.h:133
wfio_sint16_t si
The raw signed 16-bit integer representation of this number.
Definition: ieee754.h:127
wfio_sint32_t si
The raw signed 32-bit integer representation of this number.
Definition: ieee754.h:166
wfio_uint32_t ui
The raw unsigned 32-bit integer representation of this number.
Definition: ieee754.h:172
wfio_uint64_t ui
The raw unsigned 64-bit integer representation of this number.
Definition: ieee754.h:219
wfio_uint32_t exponent
Definition: ieee754.h:157
wfio_uint32_t sign
Definition: ieee754.h:159
#define WFIO_C_END
Definition: host.h:485
Byte-swapping functions.
WFIO_MAYBE_INLINE wfio_flt64_t wfio_ieee754_fromB64(wfio_ieee754_b64_t ieee, int *error)
Convert a 64-bit floating point number from IEEE 754 binary64 format.
WFIO_C_BEGIN union wfio_ieee754_b16_t wfio_ieee754_b16_t
A half-precision IEEE 754 binary floating point value (binary16).
wfio_uint16_t significand
Definition: ieee754.h:117
A half-precision IEEE 754 binary floating point value (binary16).
Definition: ieee754.h:111
WFIO_DLL wfio_ieee754_b64_t wfio_ieee754_toB64(wfio_flt64_t host, int *error)
Convert a 64-bit floating point number to IEEE 754 binary64 format.
struct wfio_ieee754_b16_t::@6 bits
The bit pattern for this number.
A structure indicating no definition.
Definition: misctype.h:47
union wfio_ieee754_b64_t wfio_ieee754_b64_t
A double-precision IEEE 754 binary floating point value (binary64).
Shared type definitions.