1 /* Manipulation of the bit representation of 'long double' quantities.
2    Copyright (C) 1999-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #ifndef _MATH_LDBL_H_
20 #define _MATH_LDBL_H_ 1
21 
22 #include <stdint.h>
23 #include <endian.h>
24 
25 /* A union which permits us to convert between a long double and
26    three 32 bit ints.  */
27 
28 #if __FLOAT_WORD_ORDER == __BIG_ENDIAN
29 
30 typedef union
31 {
32   long double value;
33   struct
34   {
35     int sign_exponent:16;
36     unsigned int empty:16;
37     uint32_t msw;
38     uint32_t lsw;
39   } parts;
40 } ieee_long_double_shape_type;
41 
42 #endif
43 
44 #if __FLOAT_WORD_ORDER == __LITTLE_ENDIAN
45 
46 typedef union
47 {
48   long double value;
49   struct
50   {
51     uint32_t lsw;
52     uint32_t msw;
53     int sign_exponent:16;
54     unsigned int empty:16;
55   } parts;
56 } ieee_long_double_shape_type;
57 
58 #endif
59 
60 /* Get three 32 bit ints from a double.  */
61 
62 #define GET_LDOUBLE_WORDS(exp,ix0,ix1,d)			\
63 do {								\
64   ieee_long_double_shape_type ew_u;				\
65   ew_u.value = (d);						\
66   (exp) = ew_u.parts.sign_exponent;				\
67   (ix0) = ew_u.parts.msw;					\
68   (ix1) = ew_u.parts.lsw;					\
69 } while (0)
70 
71 /* Set a double from two 32 bit ints.  */
72 
73 #define SET_LDOUBLE_WORDS(d,exp,ix0,ix1)			\
74 do {								\
75   ieee_long_double_shape_type iw_u;				\
76   iw_u.parts.sign_exponent = (exp);				\
77   iw_u.parts.msw = (ix0);					\
78   iw_u.parts.lsw = (ix1);					\
79   (d) = iw_u.value;						\
80 } while (0)
81 
82 /* Get the more significant 32 bits of a long double mantissa.  */
83 
84 #define GET_LDOUBLE_MSW(v,d)					\
85 do {								\
86   ieee_long_double_shape_type sh_u;				\
87   sh_u.value = (d);						\
88   (v) = sh_u.parts.msw;						\
89 } while (0)
90 
91 /* Set the more significant 32 bits of a long double mantissa from an int.  */
92 
93 #define SET_LDOUBLE_MSW(d,v)					\
94 do {								\
95   ieee_long_double_shape_type sh_u;				\
96   sh_u.value = (d);						\
97   sh_u.parts.msw = (v);						\
98   (d) = sh_u.value;						\
99 } while (0)
100 
101 /* Get int from the exponent of a long double.  */
102 
103 #define GET_LDOUBLE_EXP(exp,d)					\
104 do {								\
105   ieee_long_double_shape_type ge_u;				\
106   ge_u.value = (d);						\
107   (exp) = ge_u.parts.sign_exponent;				\
108 } while (0)
109 
110 /* Set exponent of a long double from an int.  */
111 
112 #define SET_LDOUBLE_EXP(d,exp)					\
113 do {								\
114   ieee_long_double_shape_type se_u;				\
115   se_u.value = (d);						\
116   se_u.parts.sign_exponent = (exp);				\
117   (d) = se_u.value;						\
118 } while (0)
119 
120 #endif /* math_ldbl.h */
121