1 /* Software floating-point emulation.
2    Definitions for IEEE Half Precision.
3    Copyright (C) 1997-2016 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10 
11    In addition to the permissions in the GNU Lesser General Public
12    License, the Free Software Foundation gives you unlimited
13    permission to link the compiled version of this file into
14    combinations with other programs, and to distribute those
15    combinations without any restriction coming from the use of this
16    file.  (The Lesser General Public License restrictions do apply in
17    other respects; for example, they cover modification of the file,
18    and distribution when not linked into a combine executable.)
19 
20    The GNU C Library is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    Lesser General Public License for more details.
24 
25    You should have received a copy of the GNU Lesser General Public
26    License along with the GNU C Library; if not, see
27    <http://www.gnu.org/licenses/>.  */
28 
29 #ifndef SOFT_FP_HALF_H
30 #define SOFT_FP_HALF_H	1
31 
32 #if _FP_W_TYPE_SIZE < 32
33 # error "Here's a nickel kid.  Go buy yourself a real computer."
34 #endif
35 
36 #define _FP_FRACTBITS_H		(_FP_W_TYPE_SIZE)
37 
38 #define _FP_FRACTBITS_DW_H	(_FP_W_TYPE_SIZE)
39 
40 #define _FP_FRACBITS_H		11
41 #define _FP_FRACXBITS_H		(_FP_FRACTBITS_H - _FP_FRACBITS_H)
42 #define _FP_WFRACBITS_H		(_FP_WORKBITS + _FP_FRACBITS_H)
43 #define _FP_WFRACXBITS_H	(_FP_FRACTBITS_H - _FP_WFRACBITS_H)
44 #define _FP_EXPBITS_H		5
45 #define _FP_EXPBIAS_H		15
46 #define _FP_EXPMAX_H		31
47 
48 #define _FP_QNANBIT_H		((_FP_W_TYPE) 1 << (_FP_FRACBITS_H-2))
49 #define _FP_QNANBIT_SH_H	((_FP_W_TYPE) 1 << (_FP_FRACBITS_H-2+_FP_WORKBITS))
50 #define _FP_IMPLBIT_H		((_FP_W_TYPE) 1 << (_FP_FRACBITS_H-1))
51 #define _FP_IMPLBIT_SH_H	((_FP_W_TYPE) 1 << (_FP_FRACBITS_H-1+_FP_WORKBITS))
52 #define _FP_OVERFLOW_H		((_FP_W_TYPE) 1 << (_FP_WFRACBITS_H))
53 
54 #define _FP_WFRACBITS_DW_H	(2 * _FP_WFRACBITS_H)
55 #define _FP_WFRACXBITS_DW_H	(_FP_FRACTBITS_DW_H - _FP_WFRACBITS_DW_H)
56 #define _FP_HIGHBIT_DW_H	\
57   ((_FP_W_TYPE) 1 << (_FP_WFRACBITS_DW_H - 1) % _FP_W_TYPE_SIZE)
58 
59 /* The implementation of _FP_MUL_MEAT_H and _FP_DIV_MEAT_H should be
60    chosen by the target machine.  */
61 
62 typedef float HFtype __attribute__ ((mode (HF)));
63 
64 union _FP_UNION_H
65 {
66   HFtype flt;
67   struct _FP_STRUCT_LAYOUT
68   {
69 #if __BYTE_ORDER == __BIG_ENDIAN
70     unsigned sign : 1;
71     unsigned exp  : _FP_EXPBITS_H;
72     unsigned frac : _FP_FRACBITS_H - (_FP_IMPLBIT_H != 0);
73 #else
74     unsigned frac : _FP_FRACBITS_H - (_FP_IMPLBIT_H != 0);
75     unsigned exp  : _FP_EXPBITS_H;
76     unsigned sign : 1;
77 #endif
78   } bits __attribute__ ((packed));
79 };
80 
81 #define FP_DECL_H(X)		_FP_DECL (1, X)
82 #define FP_UNPACK_RAW_H(X, val)	_FP_UNPACK_RAW_1 (H, X, (val))
83 #define FP_UNPACK_RAW_HP(X, val)	_FP_UNPACK_RAW_1_P (H, X, (val))
84 #define FP_PACK_RAW_H(val, X)	_FP_PACK_RAW_1 (H, (val), X)
85 #define FP_PACK_RAW_HP(val, X)			\
86   do						\
87     {						\
88       if (!FP_INHIBIT_RESULTS)			\
89 	_FP_PACK_RAW_1_P (H, (val), X);		\
90     }						\
91   while (0)
92 
93 #define FP_UNPACK_H(X, val)			\
94   do						\
95     {						\
96       _FP_UNPACK_RAW_1 (H, X, (val));		\
97       _FP_UNPACK_CANONICAL (H, 1, X);		\
98     }						\
99   while (0)
100 
101 #define FP_UNPACK_HP(X, val)			\
102   do						\
103     {						\
104       _FP_UNPACK_RAW_1_P (H, X, (val));		\
105       _FP_UNPACK_CANONICAL (H, 1, X);		\
106     }						\
107   while (0)
108 
109 #define FP_UNPACK_SEMIRAW_H(X, val)		\
110   do						\
111     {						\
112       _FP_UNPACK_RAW_1 (H, X, (val));		\
113       _FP_UNPACK_SEMIRAW (H, 1, X);		\
114     }						\
115   while (0)
116 
117 #define FP_UNPACK_SEMIRAW_HP(X, val)		\
118   do						\
119     {						\
120       _FP_UNPACK_RAW_1_P (H, X, (val));		\
121       _FP_UNPACK_SEMIRAW (H, 1, X);		\
122     }						\
123   while (0)
124 
125 #define FP_PACK_H(val, X)			\
126   do						\
127     {						\
128       _FP_PACK_CANONICAL (H, 1, X);		\
129       _FP_PACK_RAW_1 (H, (val), X);		\
130     }						\
131   while (0)
132 
133 #define FP_PACK_HP(val, X)			\
134   do						\
135     {						\
136       _FP_PACK_CANONICAL (H, 1, X);		\
137       if (!FP_INHIBIT_RESULTS)			\
138 	_FP_PACK_RAW_1_P (H, (val), X);		\
139     }						\
140   while (0)
141 
142 #define FP_PACK_SEMIRAW_H(val, X)		\
143   do						\
144     {						\
145       _FP_PACK_SEMIRAW (H, 1, X);		\
146       _FP_PACK_RAW_1 (H, (val), X);		\
147     }						\
148   while (0)
149 
150 #define FP_PACK_SEMIRAW_HP(val, X)		\
151   do						\
152     {						\
153       _FP_PACK_SEMIRAW (H, 1, X);		\
154       if (!FP_INHIBIT_RESULTS)			\
155 	_FP_PACK_RAW_1_P (H, (val), X);		\
156     }						\
157   while (0)
158 
159 #define FP_TO_INT_H(r, X, rsz, rsg)	_FP_TO_INT (H, 1, (r), X, (rsz), (rsg))
160 #define FP_TO_INT_ROUND_H(r, X, rsz, rsg)	\
161   _FP_TO_INT_ROUND (H, 1, (r), X, (rsz), (rsg))
162 #define FP_FROM_INT_H(X, r, rs, rt)	_FP_FROM_INT (H, 1, X, (r), (rs), rt)
163 
164 /* HFmode arithmetic is not implemented.  */
165 
166 #define _FP_FRAC_HIGH_H(X)	_FP_FRAC_HIGH_1 (X)
167 #define _FP_FRAC_HIGH_RAW_H(X)	_FP_FRAC_HIGH_1 (X)
168 #define _FP_FRAC_HIGH_DW_H(X)	_FP_FRAC_HIGH_1 (X)
169 
170 #endif /* !SOFT_FP_HALF_H */
171