soft-fp: Add _FP_UNREACHABLE.
[glibc.git] / soft-fp / soft-fp.h
blobb247125a39716f7aa026357dde1b0005701704a1
1 /* Software floating-point emulation.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Richard Henderson (rth@cygnus.com),
5 Jakub Jelinek (jj@ultra.linux.cz),
6 David S. Miller (davem@redhat.com) and
7 Peter Maydell (pmaydell@chiark.greenend.org.uk).
9 The GNU C Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 In addition to the permissions in the GNU Lesser General Public
15 License, the Free Software Foundation gives you unlimited
16 permission to link the compiled version of this file into
17 combinations with other programs, and to distribute those
18 combinations without any restriction coming from the use of this
19 file. (The Lesser General Public License restrictions do apply in
20 other respects; for example, they cover modification of the file,
21 and distribution when not linked into a combine executable.)
23 The GNU C Library is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 Lesser General Public License for more details.
28 You should have received a copy of the GNU Lesser General Public
29 License along with the GNU C Library; if not, see
30 <http://www.gnu.org/licenses/>. */
32 #ifndef SOFT_FP_H
33 #define SOFT_FP_H 1
35 #ifdef _LIBC
36 # include <sfp-machine.h>
37 #elif defined __KERNEL__
38 /* The Linux kernel uses asm/ names for architecture-specific
39 files. */
40 # include <asm/sfp-machine.h>
41 #else
42 # include "sfp-machine.h"
43 #endif
45 /* Allow sfp-machine to have its own byte order definitions. */
46 #ifndef __BYTE_ORDER
47 # ifdef _LIBC
48 # include <endian.h>
49 # else
50 # error "endianness not defined by sfp-machine.h"
51 # endif
52 #endif
54 /* For unreachable default cases in switch statements over bitwise OR
55 of FP_CLS_* values. */
56 #if (defined __GNUC__ \
57 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
58 # define _FP_UNREACHABLE __builtin_unreachable ()
59 #else
60 # define _FP_UNREACHABLE abort ()
61 #endif
63 /* In the Linux kernel, some architectures have a single function that
64 uses different kinds of unpacking and packing depending on the
65 instruction being emulated, meaning it is not readily visible to
66 the compiler that variables from _FP_DECL and _FP_FRAC_DECL_*
67 macros are only used in cases where they were initialized. */
68 #ifdef __KERNEL__
69 # define _FP_ZERO_INIT = 0
70 #else
71 # define _FP_ZERO_INIT
72 #endif
74 #define _FP_WORKBITS 3
75 #define _FP_WORK_LSB ((_FP_W_TYPE) 1 << 3)
76 #define _FP_WORK_ROUND ((_FP_W_TYPE) 1 << 2)
77 #define _FP_WORK_GUARD ((_FP_W_TYPE) 1 << 1)
78 #define _FP_WORK_STICKY ((_FP_W_TYPE) 1 << 0)
80 #ifndef FP_RND_NEAREST
81 # define FP_RND_NEAREST 0
82 # define FP_RND_ZERO 1
83 # define FP_RND_PINF 2
84 # define FP_RND_MINF 3
85 #endif
86 #ifndef FP_ROUNDMODE
87 # define FP_ROUNDMODE FP_RND_NEAREST
88 #endif
90 /* By default don't care about exceptions. */
91 #ifndef FP_EX_INVALID
92 # define FP_EX_INVALID 0
93 #endif
94 #ifndef FP_EX_OVERFLOW
95 # define FP_EX_OVERFLOW 0
96 #endif
97 #ifndef FP_EX_UNDERFLOW
98 # define FP_EX_UNDERFLOW 0
99 #endif
100 #ifndef FP_EX_DIVZERO
101 # define FP_EX_DIVZERO 0
102 #endif
103 #ifndef FP_EX_INEXACT
104 # define FP_EX_INEXACT 0
105 #endif
106 #ifndef FP_EX_DENORM
107 # define FP_EX_DENORM 0
108 #endif
110 /* Sub-exceptions of "invalid". */
111 /* Signaling NaN operand. */
112 #ifndef FP_EX_INVALID_SNAN
113 # define FP_EX_INVALID_SNAN 0
114 #endif
115 /* Inf * 0. */
116 #ifndef FP_EX_INVALID_IMZ
117 # define FP_EX_INVALID_IMZ 0
118 #endif
119 /* fma (Inf, 0, c). */
120 #ifndef FP_EX_INVALID_IMZ_FMA
121 # define FP_EX_INVALID_IMZ_FMA 0
122 #endif
123 /* Inf - Inf. */
124 #ifndef FP_EX_INVALID_ISI
125 # define FP_EX_INVALID_ISI 0
126 #endif
127 /* 0 / 0. */
128 #ifndef FP_EX_INVALID_ZDZ
129 # define FP_EX_INVALID_ZDZ 0
130 #endif
131 /* Inf / Inf. */
132 #ifndef FP_EX_INVALID_IDI
133 # define FP_EX_INVALID_IDI 0
134 #endif
135 /* sqrt (negative). */
136 #ifndef FP_EX_INVALID_SQRT
137 # define FP_EX_INVALID_SQRT 0
138 #endif
139 /* Invalid conversion to integer. */
140 #ifndef FP_EX_INVALID_CVI
141 # define FP_EX_INVALID_CVI 0
142 #endif
143 /* Invalid comparison. */
144 #ifndef FP_EX_INVALID_VC
145 # define FP_EX_INVALID_VC 0
146 #endif
148 /* _FP_STRUCT_LAYOUT may be defined as an attribute to determine the
149 struct layout variant used for structures where bit-fields are used
150 to access specific parts of binary floating-point numbers. This is
151 required for systems where the default ABI uses struct layout with
152 differences in how consecutive bit-fields are laid out from the
153 default expected by soft-fp. */
154 #ifndef _FP_STRUCT_LAYOUT
155 # define _FP_STRUCT_LAYOUT
156 #endif
158 #ifdef _FP_DECL_EX
159 # define FP_DECL_EX \
160 int _fex = 0; \
161 _FP_DECL_EX
162 #else
163 # define FP_DECL_EX int _fex = 0
164 #endif
166 /* Initialize any machine-specific state used in FP_ROUNDMODE,
167 FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS. */
168 #ifndef FP_INIT_ROUNDMODE
169 # define FP_INIT_ROUNDMODE do {} while (0)
170 #endif
172 /* Initialize any machine-specific state used in
173 FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS. */
174 #ifndef FP_INIT_TRAPPING_EXCEPTIONS
175 # define FP_INIT_TRAPPING_EXCEPTIONS FP_INIT_ROUNDMODE
176 #endif
178 /* Initialize any machine-specific state used in
179 FP_HANDLE_EXCEPTIONS. */
180 #ifndef FP_INIT_EXCEPTIONS
181 # define FP_INIT_EXCEPTIONS FP_INIT_TRAPPING_EXCEPTIONS
182 #endif
184 #ifndef FP_HANDLE_EXCEPTIONS
185 # define FP_HANDLE_EXCEPTIONS do {} while (0)
186 #endif
188 /* Whether to flush subnormal inputs to zero with the same sign. */
189 #ifndef FP_DENORM_ZERO
190 # define FP_DENORM_ZERO 0
191 #endif
193 #ifndef FP_INHIBIT_RESULTS
194 /* By default we write the results always.
195 sfp-machine may override this and e.g.
196 check if some exceptions are unmasked
197 and inhibit it in such a case. */
198 # define FP_INHIBIT_RESULTS 0
199 #endif
201 #define FP_SET_EXCEPTION(ex) \
202 _fex |= (ex)
204 #define FP_CUR_EXCEPTIONS \
205 (_fex)
207 #ifndef FP_TRAPPING_EXCEPTIONS
208 # define FP_TRAPPING_EXCEPTIONS 0
209 #endif
211 /* A file using soft-fp may define FP_NO_EXCEPTIONS before including
212 soft-fp.h to indicate that, although a macro used there could raise
213 exceptions, or do rounding and potentially thereby raise
214 exceptions, for some arguments, for the particular arguments used
215 in that file no exceptions or rounding can occur. Such a file
216 should not itself use macros relating to handling exceptions and
217 rounding modes; this is only for indirect uses (in particular, in
218 _FP_FROM_INT and the macros it calls). */
219 #ifdef FP_NO_EXCEPTIONS
221 # undef FP_SET_EXCEPTION
222 # define FP_SET_EXCEPTION(ex) do {} while (0)
224 # undef FP_CUR_EXCEPTIONS
225 # define FP_CUR_EXCEPTIONS 0
227 # undef FP_TRAPPING_EXCEPTIONS
228 # define FP_TRAPPING_EXCEPTIONS 0
230 # undef FP_ROUNDMODE
231 # define FP_ROUNDMODE FP_RND_ZERO
233 # undef _FP_TININESS_AFTER_ROUNDING
234 # define _FP_TININESS_AFTER_ROUNDING 0
236 #endif
238 /* A file using soft-fp may define FP_NO_EXACT_UNDERFLOW before
239 including soft-fp.h to indicate that, although a macro used there
240 could allow for the case of exact underflow requiring the underflow
241 exception to be raised if traps are enabled, for the particular
242 arguments used in that file no exact underflow can occur. */
243 #ifdef FP_NO_EXACT_UNDERFLOW
244 # undef FP_TRAPPING_EXCEPTIONS
245 # define FP_TRAPPING_EXCEPTIONS 0
246 #endif
248 #define _FP_ROUND_NEAREST(wc, X) \
249 do \
251 if ((_FP_FRAC_LOW_##wc (X) & 15) != _FP_WORK_ROUND) \
252 _FP_FRAC_ADDI_##wc (X, _FP_WORK_ROUND); \
254 while (0)
256 #define _FP_ROUND_ZERO(wc, X) (void) 0
258 #define _FP_ROUND_PINF(wc, X) \
259 do \
261 if (!X##_s && (_FP_FRAC_LOW_##wc (X) & 7)) \
262 _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB); \
264 while (0)
266 #define _FP_ROUND_MINF(wc, X) \
267 do \
269 if (X##_s && (_FP_FRAC_LOW_##wc (X) & 7)) \
270 _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB); \
272 while (0)
274 #define _FP_ROUND(wc, X) \
275 do \
277 if (_FP_FRAC_LOW_##wc (X) & 7) \
279 FP_SET_EXCEPTION (FP_EX_INEXACT); \
280 switch (FP_ROUNDMODE) \
282 case FP_RND_NEAREST: \
283 _FP_ROUND_NEAREST (wc, X); \
284 break; \
285 case FP_RND_ZERO: \
286 _FP_ROUND_ZERO (wc, X); \
287 break; \
288 case FP_RND_PINF: \
289 _FP_ROUND_PINF (wc, X); \
290 break; \
291 case FP_RND_MINF: \
292 _FP_ROUND_MINF (wc, X); \
293 break; \
297 while (0)
299 #define FP_CLS_NORMAL 0
300 #define FP_CLS_ZERO 1
301 #define FP_CLS_INF 2
302 #define FP_CLS_NAN 3
304 #define _FP_CLS_COMBINE(x, y) (((x) << 2) | (y))
306 #include "op-1.h"
307 #include "op-2.h"
308 #include "op-4.h"
309 #include "op-8.h"
310 #include "op-common.h"
312 /* Sigh. Silly things longlong.h needs. */
313 #define UWtype _FP_W_TYPE
314 #define W_TYPE_SIZE _FP_W_TYPE_SIZE
316 typedef int QItype __attribute__ ((mode (QI)));
317 typedef int SItype __attribute__ ((mode (SI)));
318 typedef int DItype __attribute__ ((mode (DI)));
319 typedef unsigned int UQItype __attribute__ ((mode (QI)));
320 typedef unsigned int USItype __attribute__ ((mode (SI)));
321 typedef unsigned int UDItype __attribute__ ((mode (DI)));
322 #if _FP_W_TYPE_SIZE == 32
323 typedef unsigned int UHWtype __attribute__ ((mode (HI)));
324 #elif _FP_W_TYPE_SIZE == 64
325 typedef USItype UHWtype;
326 #endif
328 #ifndef CMPtype
329 # define CMPtype int
330 #endif
332 #define SI_BITS (__CHAR_BIT__ * (int) sizeof (SItype))
333 #define DI_BITS (__CHAR_BIT__ * (int) sizeof (DItype))
335 #ifndef umul_ppmm
336 # ifdef _LIBC
337 # include <stdlib/longlong.h>
338 # else
339 # include "longlong.h"
340 # endif
341 #endif
343 #ifdef _LIBC
344 # include <stdlib.h>
345 #else
346 extern void abort (void);
347 #endif
349 #endif /* !SOFT_FP_H */