2006-02-18 Joseph S. Myers <joseph@codesourcery.com>
[kopensolaris-gnu/glibc.git] / soft-fp / soft-fp.h
1 /* Software floating-point emulation.
2    Copyright (C) 1997,1998,1999,2000,2002,2003,2005,2006
3         Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5    Contributed by Richard Henderson (rth@cygnus.com),
6                   Jakub Jelinek (jj@ultra.linux.cz),
7                   David S. Miller (davem@redhat.com) and
8                   Peter Maydell (pmaydell@chiark.greenend.org.uk).
9
10    The GNU C Library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2.1 of the License, or (at your option) any later version.
14
15    The GNU C Library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with the GNU C Library; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23    02111-1307 USA.  */
24
25 #ifndef SOFT_FP_H
26 #define SOFT_FP_H
27
28 #ifdef _LIBC
29 #include <sfp-machine.h>
30 #else
31 #include "sfp-machine.h"
32 #endif
33
34 /* Allow sfp-machine to have its own byte order definitions. */
35 #ifndef __BYTE_ORDER
36 #ifdef _LIBC
37 #include <endian.h>
38 #else
39 #error "endianness not defined by sfp-machine.h"
40 #endif
41 #endif
42
43 #define _FP_WORKBITS            3
44 #define _FP_WORK_LSB            ((_FP_W_TYPE)1 << 3)
45 #define _FP_WORK_ROUND          ((_FP_W_TYPE)1 << 2)
46 #define _FP_WORK_GUARD          ((_FP_W_TYPE)1 << 1)
47 #define _FP_WORK_STICKY         ((_FP_W_TYPE)1 << 0)
48
49 #ifndef FP_RND_NEAREST
50 # define FP_RND_NEAREST         0
51 # define FP_RND_ZERO            1
52 # define FP_RND_PINF            2
53 # define FP_RND_MINF            3
54 #endif
55 #ifndef FP_ROUNDMODE
56 # define FP_ROUNDMODE           FP_RND_NEAREST
57 #endif
58
59 /* By default don't care about exceptions. */
60 #ifndef FP_EX_INVALID
61 #define FP_EX_INVALID           0
62 #endif
63 #ifndef FP_EX_OVERFLOW
64 #define FP_EX_OVERFLOW          0
65 #endif
66 #ifndef FP_EX_UNDERFLOW
67 #define FP_EX_UNDERFLOW         0
68 #endif
69 #ifndef FP_EX_DIVZERO
70 #define FP_EX_DIVZERO           0
71 #endif
72 #ifndef FP_EX_INEXACT
73 #define FP_EX_INEXACT           0
74 #endif
75 #ifndef FP_EX_DENORM
76 #define FP_EX_DENORM            0
77 #endif
78
79 #ifdef _FP_DECL_EX
80 #define FP_DECL_EX                                      \
81   int _fex = 0;                                         \
82   _FP_DECL_EX
83 #else
84 #define FP_DECL_EX int _fex = 0
85 #endif
86
87 #ifndef FP_INIT_ROUNDMODE
88 #define FP_INIT_ROUNDMODE do {} while (0)
89 #endif
90
91 #ifndef FP_HANDLE_EXCEPTIONS
92 #define FP_HANDLE_EXCEPTIONS do {} while (0)
93 #endif
94
95 #ifndef FP_INHIBIT_RESULTS
96 /* By default we write the results always.
97  * sfp-machine may override this and e.g.
98  * check if some exceptions are unmasked
99  * and inhibit it in such a case.
100  */
101 #define FP_INHIBIT_RESULTS 0
102 #endif
103
104 #define FP_SET_EXCEPTION(ex)                            \
105   _fex |= (ex)
106
107 #define FP_UNSET_EXCEPTION(ex)                          \
108   _fex &= ~(ex)
109
110 #define FP_CLEAR_EXCEPTIONS                             \
111   _fex = 0
112
113 #define _FP_ROUND_NEAREST(wc, X)                        \
114 do {                                                    \
115     if ((_FP_FRAC_LOW_##wc(X) & 15) != _FP_WORK_ROUND)  \
116       _FP_FRAC_ADDI_##wc(X, _FP_WORK_ROUND);            \
117 } while (0)
118
119 #define _FP_ROUND_ZERO(wc, X)           (void)0
120
121 #define _FP_ROUND_PINF(wc, X)                           \
122 do {                                                    \
123     if (!X##_s && (_FP_FRAC_LOW_##wc(X) & 7))           \
124       _FP_FRAC_ADDI_##wc(X, _FP_WORK_LSB);              \
125 } while (0)
126
127 #define _FP_ROUND_MINF(wc, X)                           \
128 do {                                                    \
129     if (X##_s && (_FP_FRAC_LOW_##wc(X) & 7))            \
130       _FP_FRAC_ADDI_##wc(X, _FP_WORK_LSB);              \
131 } while (0)
132
133 #define _FP_ROUND(wc, X)                        \
134 do {                                            \
135         if (_FP_FRAC_LOW_##wc(X) & 7)           \
136           FP_SET_EXCEPTION(FP_EX_INEXACT);      \
137         switch (FP_ROUNDMODE)                   \
138         {                                       \
139           case FP_RND_NEAREST:                  \
140             _FP_ROUND_NEAREST(wc,X);            \
141             break;                              \
142           case FP_RND_ZERO:                     \
143             _FP_ROUND_ZERO(wc,X);               \
144             break;                              \
145           case FP_RND_PINF:                     \
146             _FP_ROUND_PINF(wc,X);               \
147             break;                              \
148           case FP_RND_MINF:                     \
149             _FP_ROUND_MINF(wc,X);               \
150             break;                              \
151         }                                       \
152 } while (0)
153
154 #define FP_CLS_NORMAL           0
155 #define FP_CLS_ZERO             1
156 #define FP_CLS_INF              2
157 #define FP_CLS_NAN              3
158
159 #define _FP_CLS_COMBINE(x,y)    (((x) << 2) | (y))
160
161 #include "op-1.h"
162 #include "op-2.h"
163 #include "op-4.h"
164 #include "op-8.h"
165 #include "op-common.h"
166
167 /* Sigh.  Silly things longlong.h needs.  */
168 #define UWtype          _FP_W_TYPE
169 #define W_TYPE_SIZE     _FP_W_TYPE_SIZE
170
171 typedef int QItype __attribute__((mode(QI)));
172 typedef int SItype __attribute__((mode(SI)));
173 typedef int DItype __attribute__((mode(DI)));
174 typedef unsigned int UQItype __attribute__((mode(QI)));
175 typedef unsigned int USItype __attribute__((mode(SI)));
176 typedef unsigned int UDItype __attribute__((mode(DI)));
177 #if _FP_W_TYPE_SIZE == 32
178 typedef unsigned int UHWtype __attribute__((mode(HI)));
179 #elif _FP_W_TYPE_SIZE == 64
180 typedef USItype UHWtype;
181 #endif
182
183 #define SI_BITS         (__CHAR_BIT__ * (int)sizeof(SItype))
184 #define DI_BITS         (__CHAR_BIT__ * (int)sizeof(DItype))
185
186 #ifndef umul_ppmm
187 #ifdef _LIBC
188 #include <stdlib/longlong.h>
189 #else
190 #include "longlong.h"
191 #endif
192 #endif
193
194 #ifdef _LIBC
195 #include <stdlib.h>
196 #else
197 extern void abort (void);
198 #endif
199
200 #endif