1 /* Software floating-point emulation.
2 Copyright (C) 1997-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
10 In addition to the permissions in the GNU Lesser General Public
11 License, the Free Software Foundation gives you unlimited
12 permission to link the compiled version of this file into
13 combinations with other programs, and to distribute those
14 combinations without any restriction coming from the use of this
15 file. (The Lesser General Public License restrictions do apply in
16 other respects; for example, they cover modification of the file,
17 and distribution when not linked into a combine executable.)
19 The GNU C Library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with the GNU C Library; if not, see
26 <https://www.gnu.org/licenses/>. */
32 # include <sfp-machine.h>
33 #elif defined __KERNEL__
34 /* The Linux kernel uses asm/ names for architecture-specific
36 # include <asm/sfp-machine.h>
38 # include "sfp-machine.h"
41 /* Allow sfp-machine to have its own byte order definitions. */
46 # error "endianness not defined by sfp-machine.h"
50 /* For unreachable default cases in switch statements over bitwise OR
51 of FP_CLS_* values. */
52 #if (defined __GNUC__ \
53 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
54 # define _FP_UNREACHABLE __builtin_unreachable ()
56 # define _FP_UNREACHABLE abort ()
59 #if ((defined __GNUC__ \
60 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) \
61 || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L))
62 # define _FP_STATIC_ASSERT(expr, msg) \
63 _Static_assert ((expr), msg)
65 # define _FP_STATIC_ASSERT(expr, msg) \
66 extern int (*__Static_assert_function (void)) \
67 [!!sizeof (struct { int __error_if_negative: (expr) ? 2 : -1; })]
70 /* In the Linux kernel, some architectures have a single function that
71 uses different kinds of unpacking and packing depending on the
72 instruction being emulated, meaning it is not readily visible to
73 the compiler that variables from _FP_DECL and _FP_FRAC_DECL_*
74 macros are only used in cases where they were initialized. */
76 # define _FP_ZERO_INIT = 0
78 # define _FP_ZERO_INIT
81 #define _FP_WORKBITS 3
82 #define _FP_WORK_LSB ((_FP_W_TYPE) 1 << 3)
83 #define _FP_WORK_ROUND ((_FP_W_TYPE) 1 << 2)
84 #define _FP_WORK_GUARD ((_FP_W_TYPE) 1 << 1)
85 #define _FP_WORK_STICKY ((_FP_W_TYPE) 1 << 0)
87 #ifndef FP_RND_NEAREST
88 # define FP_RND_NEAREST 0
89 # define FP_RND_ZERO 1
90 # define FP_RND_PINF 2
91 # define FP_RND_MINF 3
94 # define FP_ROUNDMODE FP_RND_NEAREST
97 /* By default don't care about exceptions. */
99 # define FP_EX_INVALID 0
101 #ifndef FP_EX_OVERFLOW
102 # define FP_EX_OVERFLOW 0
104 #ifndef FP_EX_UNDERFLOW
105 # define FP_EX_UNDERFLOW 0
107 #ifndef FP_EX_DIVZERO
108 # define FP_EX_DIVZERO 0
110 #ifndef FP_EX_INEXACT
111 # define FP_EX_INEXACT 0
114 # define FP_EX_DENORM 0
117 /* Sub-exceptions of "invalid". */
118 /* Signaling NaN operand. */
119 #ifndef FP_EX_INVALID_SNAN
120 # define FP_EX_INVALID_SNAN 0
123 #ifndef FP_EX_INVALID_IMZ
124 # define FP_EX_INVALID_IMZ 0
126 /* fma (Inf, 0, c). */
127 #ifndef FP_EX_INVALID_IMZ_FMA
128 # define FP_EX_INVALID_IMZ_FMA 0
131 #ifndef FP_EX_INVALID_ISI
132 # define FP_EX_INVALID_ISI 0
135 #ifndef FP_EX_INVALID_ZDZ
136 # define FP_EX_INVALID_ZDZ 0
139 #ifndef FP_EX_INVALID_IDI
140 # define FP_EX_INVALID_IDI 0
142 /* sqrt (negative). */
143 #ifndef FP_EX_INVALID_SQRT
144 # define FP_EX_INVALID_SQRT 0
146 /* Invalid conversion to integer. */
147 #ifndef FP_EX_INVALID_CVI
148 # define FP_EX_INVALID_CVI 0
150 /* Invalid comparison. */
151 #ifndef FP_EX_INVALID_VC
152 # define FP_EX_INVALID_VC 0
155 /* _FP_STRUCT_LAYOUT may be defined as an attribute to determine the
156 struct layout variant used for structures where bit-fields are used
157 to access specific parts of binary floating-point numbers. This is
158 required for systems where the default ABI uses struct layout with
159 differences in how consecutive bit-fields are laid out from the
160 default expected by soft-fp. */
161 #ifndef _FP_STRUCT_LAYOUT
162 # define _FP_STRUCT_LAYOUT
166 # define FP_DECL_EX \
170 # define FP_DECL_EX int _fex = 0
173 /* Initialize any machine-specific state used in FP_ROUNDMODE,
174 FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS. */
175 #ifndef FP_INIT_ROUNDMODE
176 # define FP_INIT_ROUNDMODE do {} while (0)
179 /* Initialize any machine-specific state used in
180 FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS. */
181 #ifndef FP_INIT_TRAPPING_EXCEPTIONS
182 # define FP_INIT_TRAPPING_EXCEPTIONS FP_INIT_ROUNDMODE
185 /* Initialize any machine-specific state used in
186 FP_HANDLE_EXCEPTIONS. */
187 #ifndef FP_INIT_EXCEPTIONS
188 # define FP_INIT_EXCEPTIONS FP_INIT_TRAPPING_EXCEPTIONS
191 #ifndef FP_HANDLE_EXCEPTIONS
192 # define FP_HANDLE_EXCEPTIONS do {} while (0)
195 /* Whether to flush subnormal inputs to zero with the same sign. */
196 #ifndef FP_DENORM_ZERO
197 # define FP_DENORM_ZERO 0
200 #ifndef FP_INHIBIT_RESULTS
201 /* By default we write the results always.
202 sfp-machine may override this and e.g.
203 check if some exceptions are unmasked
204 and inhibit it in such a case. */
205 # define FP_INHIBIT_RESULTS 0
208 #define FP_SET_EXCEPTION(ex) \
211 #define FP_CUR_EXCEPTIONS \
214 #ifndef FP_TRAPPING_EXCEPTIONS
215 # define FP_TRAPPING_EXCEPTIONS 0
218 /* A file using soft-fp may define FP_NO_EXCEPTIONS before including
219 soft-fp.h to indicate that, although a macro used there could raise
220 exceptions, or do rounding and potentially thereby raise
221 exceptions, for some arguments, for the particular arguments used
222 in that file no exceptions or rounding can occur. Such a file
223 should not itself use macros relating to handling exceptions and
224 rounding modes; this is only for indirect uses (in particular, in
225 _FP_FROM_INT and the macros it calls). */
226 #ifdef FP_NO_EXCEPTIONS
228 # undef FP_SET_EXCEPTION
229 # define FP_SET_EXCEPTION(ex) do {} while (0)
231 # undef FP_CUR_EXCEPTIONS
232 # define FP_CUR_EXCEPTIONS 0
234 # undef FP_TRAPPING_EXCEPTIONS
235 # define FP_TRAPPING_EXCEPTIONS 0
238 # define FP_ROUNDMODE FP_RND_ZERO
240 # undef _FP_TININESS_AFTER_ROUNDING
241 # define _FP_TININESS_AFTER_ROUNDING 0
245 /* A file using soft-fp may define FP_NO_EXACT_UNDERFLOW before
246 including soft-fp.h to indicate that, although a macro used there
247 could allow for the case of exact underflow requiring the underflow
248 exception to be raised if traps are enabled, for the particular
249 arguments used in that file no exact underflow can occur. */
250 #ifdef FP_NO_EXACT_UNDERFLOW
251 # undef FP_TRAPPING_EXCEPTIONS
252 # define FP_TRAPPING_EXCEPTIONS 0
255 #define _FP_ROUND_NEAREST(wc, X) \
258 if ((_FP_FRAC_LOW_##wc (X) & 15) != _FP_WORK_ROUND) \
259 _FP_FRAC_ADDI_##wc (X, _FP_WORK_ROUND); \
263 #define _FP_ROUND_ZERO(wc, X) (void) 0
265 #define _FP_ROUND_PINF(wc, X) \
268 if (!X##_s && (_FP_FRAC_LOW_##wc (X) & 7)) \
269 _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB); \
273 #define _FP_ROUND_MINF(wc, X) \
276 if (X##_s && (_FP_FRAC_LOW_##wc (X) & 7)) \
277 _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB); \
281 #define _FP_ROUND(wc, X) \
284 if (_FP_FRAC_LOW_##wc (X) & 7) \
286 FP_SET_EXCEPTION (FP_EX_INEXACT); \
287 switch (FP_ROUNDMODE) \
289 case FP_RND_NEAREST: \
290 _FP_ROUND_NEAREST (wc, X); \
293 _FP_ROUND_ZERO (wc, X); \
296 _FP_ROUND_PINF (wc, X); \
299 _FP_ROUND_MINF (wc, X); \
306 #define FP_CLS_NORMAL 0
307 #define FP_CLS_ZERO 1
311 #define _FP_CLS_COMBINE(x, y) (((x) << 2) | (y))
317 #include "op-common.h"
319 /* Sigh. Silly things longlong.h needs. */
320 #define UWtype _FP_W_TYPE
321 #define W_TYPE_SIZE _FP_W_TYPE_SIZE
323 typedef int QItype
__attribute__ ((mode (QI
)));
324 typedef int SItype
__attribute__ ((mode (SI
)));
325 typedef int DItype
__attribute__ ((mode (DI
)));
326 typedef unsigned int UQItype
__attribute__ ((mode (QI
)));
327 typedef unsigned int USItype
__attribute__ ((mode (SI
)));
328 typedef unsigned int UDItype
__attribute__ ((mode (DI
)));
329 #if _FP_W_TYPE_SIZE == 32
330 typedef unsigned int UHWtype
__attribute__ ((mode (HI
)));
331 #elif _FP_W_TYPE_SIZE == 64
332 typedef USItype UHWtype
;
339 #define SI_BITS (__CHAR_BIT__ * (int) sizeof (SItype))
340 #define DI_BITS (__CHAR_BIT__ * (int) sizeof (DItype))
344 # include <stdlib/longlong.h>
346 # include "longlong.h"
350 #endif /* !SOFT_FP_H */