soft-fp: Make extensions of subnormals from XFmode to TFmode signal underflow if...
[platform/upstream/glibc.git] / soft-fp / soft-fp.h
1 /* Software floating-point emulation.
2    Copyright (C) 1997-2014 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).
8
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.
13
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.)
22
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.
27
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/>.  */
31
32 #ifndef SOFT_FP_H
33 #define SOFT_FP_H
34
35 #ifdef _LIBC
36 # include <sfp-machine.h>
37 #else
38 # include "sfp-machine.h"
39 #endif
40
41 /* Allow sfp-machine to have its own byte order definitions.  */
42 #ifndef __BYTE_ORDER
43 # ifdef _LIBC
44 #  include <endian.h>
45 # else
46 #  error "endianness not defined by sfp-machine.h"
47 # endif
48 #endif
49
50 #define _FP_WORKBITS            3
51 #define _FP_WORK_LSB            ((_FP_W_TYPE) 1 << 3)
52 #define _FP_WORK_ROUND          ((_FP_W_TYPE) 1 << 2)
53 #define _FP_WORK_GUARD          ((_FP_W_TYPE) 1 << 1)
54 #define _FP_WORK_STICKY         ((_FP_W_TYPE) 1 << 0)
55
56 #ifndef FP_RND_NEAREST
57 # define FP_RND_NEAREST         0
58 # define FP_RND_ZERO            1
59 # define FP_RND_PINF            2
60 # define FP_RND_MINF            3
61 #endif
62 #ifndef FP_ROUNDMODE
63 # define FP_ROUNDMODE           FP_RND_NEAREST
64 #endif
65
66 /* By default don't care about exceptions.  */
67 #ifndef FP_EX_INVALID
68 # define FP_EX_INVALID          0
69 #endif
70 #ifndef FP_EX_OVERFLOW
71 # define FP_EX_OVERFLOW         0
72 #endif
73 #ifndef FP_EX_UNDERFLOW
74 # define FP_EX_UNDERFLOW        0
75 #endif
76 #ifndef FP_EX_DIVZERO
77 # define FP_EX_DIVZERO          0
78 #endif
79 #ifndef FP_EX_INEXACT
80 # define FP_EX_INEXACT          0
81 #endif
82 #ifndef FP_EX_DENORM
83 # define FP_EX_DENORM           0
84 #endif
85
86 /* _FP_STRUCT_LAYOUT may be defined as an attribute to determine the
87    struct layout variant used for structures where bit-fields are used
88    to access specific parts of binary floating-point numbers.  This is
89    required for systems where the default ABI uses struct layout with
90    differences in how consecutive bit-fields are laid out from the
91    default expected by soft-fp.  */
92 #ifndef _FP_STRUCT_LAYOUT
93 # define _FP_STRUCT_LAYOUT
94 #endif
95
96 #ifdef _FP_DECL_EX
97 # define FP_DECL_EX                                     \
98   int _fex = 0;                                         \
99   _FP_DECL_EX
100 #else
101 # define FP_DECL_EX int _fex = 0
102 #endif
103
104 /* Initialize any machine-specific state used in FP_ROUNDMODE,
105    FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS.  */
106 #ifndef FP_INIT_ROUNDMODE
107 # define FP_INIT_ROUNDMODE do {} while (0)
108 #endif
109
110 /* Initialize any machine-specific state used in
111    FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS.  */
112 #ifndef FP_INIT_TRAPPING_EXCEPTIONS
113 # define FP_INIT_TRAPPING_EXCEPTIONS FP_INIT_ROUNDMODE
114 #endif
115
116 /* Initialize any machine-specific state used in
117    FP_HANDLE_EXCEPTIONS.  */
118 #ifndef FP_INIT_EXCEPTIONS
119 # define FP_INIT_EXCEPTIONS FP_INIT_TRAPPING_EXCEPTIONS
120 #endif
121
122 #ifndef FP_HANDLE_EXCEPTIONS
123 # define FP_HANDLE_EXCEPTIONS do {} while (0)
124 #endif
125
126 #ifndef FP_INHIBIT_RESULTS
127 /* By default we write the results always.
128    sfp-machine may override this and e.g.
129    check if some exceptions are unmasked
130    and inhibit it in such a case.  */
131 # define FP_INHIBIT_RESULTS 0
132 #endif
133
134 #define FP_SET_EXCEPTION(ex)                            \
135   _fex |= (ex)
136
137 #define FP_CUR_EXCEPTIONS                               \
138   (_fex)
139
140 #ifndef FP_TRAPPING_EXCEPTIONS
141 # define FP_TRAPPING_EXCEPTIONS 0
142 #endif
143
144 /* A file using soft-fp may define FP_NO_EXCEPTIONS before including
145    soft-fp.h to indicate that, although a macro used there could raise
146    exceptions, or do rounding and potentially thereby raise
147    exceptions, for some arguments, for the particular arguments used
148    in that file no exceptions or rounding can occur.  Such a file
149    should not itself use macros relating to handling exceptions and
150    rounding modes; this is only for indirect uses (in particular, in
151    _FP_FROM_INT and the macros it calls).  */
152 #ifdef FP_NO_EXCEPTIONS
153
154 # undef FP_SET_EXCEPTION
155 # define FP_SET_EXCEPTION(ex) do {} while (0)
156
157 # undef FP_CUR_EXCEPTIONS
158 # define FP_CUR_EXCEPTIONS 0
159
160 # undef FP_TRAPPING_EXCEPTIONS
161 # define FP_TRAPPING_EXCEPTIONS 0
162
163 # undef FP_ROUNDMODE
164 # define FP_ROUNDMODE FP_RND_ZERO
165
166 # undef _FP_TININESS_AFTER_ROUNDING
167 # define _FP_TININESS_AFTER_ROUNDING 0
168
169 #endif
170
171 /* A file using soft-fp may define FP_NO_EXACT_UNDERFLOW before
172    including soft-fp.h to indicate that, although a macro used there
173    could allow for the case of exact underflow requiring the underflow
174    exception to be raised if traps are enabled, for the particular
175    arguments used in that file no exact underflow can occur.  */
176 #ifdef FP_NO_EXACT_UNDERFLOW
177 # undef FP_TRAPPING_EXCEPTIONS
178 # define FP_TRAPPING_EXCEPTIONS 0
179 #endif
180
181 #define _FP_ROUND_NEAREST(wc, X)                                \
182   do                                                            \
183     {                                                           \
184       if ((_FP_FRAC_LOW_##wc (X) & 15) != _FP_WORK_ROUND)       \
185         _FP_FRAC_ADDI_##wc (X, _FP_WORK_ROUND);                 \
186     }                                                           \
187   while (0)
188
189 #define _FP_ROUND_ZERO(wc, X)           (void) 0
190
191 #define _FP_ROUND_PINF(wc, X)                           \
192   do                                                    \
193     {                                                   \
194       if (!X##_s && (_FP_FRAC_LOW_##wc (X) & 7))        \
195         _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB);           \
196     }                                                   \
197   while (0)
198
199 #define _FP_ROUND_MINF(wc, X)                   \
200   do                                            \
201     {                                           \
202       if (X##_s && (_FP_FRAC_LOW_##wc (X) & 7)) \
203         _FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB);   \
204     }                                           \
205   while (0)
206
207 #define _FP_ROUND(wc, X)                        \
208   do                                            \
209     {                                           \
210       if (_FP_FRAC_LOW_##wc (X) & 7)            \
211         {                                       \
212           FP_SET_EXCEPTION (FP_EX_INEXACT);     \
213           switch (FP_ROUNDMODE)                 \
214             {                                   \
215             case FP_RND_NEAREST:                \
216               _FP_ROUND_NEAREST (wc, X);        \
217               break;                            \
218             case FP_RND_ZERO:                   \
219               _FP_ROUND_ZERO (wc, X);           \
220               break;                            \
221             case FP_RND_PINF:                   \
222               _FP_ROUND_PINF (wc, X);           \
223               break;                            \
224             case FP_RND_MINF:                   \
225               _FP_ROUND_MINF (wc, X);           \
226               break;                            \
227             }                                   \
228         }                                       \
229     }                                           \
230   while (0)
231
232 #define FP_CLS_NORMAL           0
233 #define FP_CLS_ZERO             1
234 #define FP_CLS_INF              2
235 #define FP_CLS_NAN              3
236
237 #define _FP_CLS_COMBINE(x, y)   (((x) << 2) | (y))
238
239 #include "op-1.h"
240 #include "op-2.h"
241 #include "op-4.h"
242 #include "op-8.h"
243 #include "op-common.h"
244
245 /* Sigh.  Silly things longlong.h needs.  */
246 #define UWtype          _FP_W_TYPE
247 #define W_TYPE_SIZE     _FP_W_TYPE_SIZE
248
249 typedef int QItype __attribute__ ((mode (QI)));
250 typedef int SItype __attribute__ ((mode (SI)));
251 typedef int DItype __attribute__ ((mode (DI)));
252 typedef unsigned int UQItype __attribute__ ((mode (QI)));
253 typedef unsigned int USItype __attribute__ ((mode (SI)));
254 typedef unsigned int UDItype __attribute__ ((mode (DI)));
255 #if _FP_W_TYPE_SIZE == 32
256 typedef unsigned int UHWtype __attribute__ ((mode (HI)));
257 #elif _FP_W_TYPE_SIZE == 64
258 typedef USItype UHWtype;
259 #endif
260
261 #ifndef CMPtype
262 # define CMPtype        int
263 #endif
264
265 #define SI_BITS         (__CHAR_BIT__ * (int) sizeof (SItype))
266 #define DI_BITS         (__CHAR_BIT__ * (int) sizeof (DItype))
267
268 #ifndef umul_ppmm
269 # ifdef _LIBC
270 #  include <stdlib/longlong.h>
271 # else
272 #  include "longlong.h"
273 # endif
274 #endif
275
276 #ifdef _LIBC
277 # include <stdlib.h>
278 #else
279 extern void abort (void);
280 #endif
281
282 #endif