* sysdeps/unix/sysv/linux/sh/pread.c: Copy in mips pread.c.
[glibc.git] / soft-fp / op-1.h
blob9f58ba6a086c6fad7717954a5c2b8cd4805be117
1 /* Software floating-point emulation.
2 Basic one-word fraction declaration and manipulation.
3 Copyright (C) 1997,1998,1999,2006 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).
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.
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.
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. */
25 #define _FP_FRAC_DECL_1(X) _FP_W_TYPE X##_f
26 #define _FP_FRAC_COPY_1(D,S) (D##_f = S##_f)
27 #define _FP_FRAC_SET_1(X,I) (X##_f = I)
28 #define _FP_FRAC_HIGH_1(X) (X##_f)
29 #define _FP_FRAC_LOW_1(X) (X##_f)
30 #define _FP_FRAC_WORD_1(X,w) (X##_f)
32 #define _FP_FRAC_ADDI_1(X,I) (X##_f += I)
33 #define _FP_FRAC_SLL_1(X,N) \
34 do { \
35 if (__builtin_constant_p(N) && (N) == 1) \
36 X##_f += X##_f; \
37 else \
38 X##_f <<= (N); \
39 } while (0)
40 #define _FP_FRAC_SRL_1(X,N) (X##_f >>= N)
42 /* Right shift with sticky-lsb. */
43 #define _FP_FRAC_SRST_1(X,S,N,sz) __FP_FRAC_SRST_1(X##_f, S, N, sz)
44 #define _FP_FRAC_SRS_1(X,N,sz) __FP_FRAC_SRS_1(X##_f, N, sz)
46 #define __FP_FRAC_SRST_1(X,S,N,sz) \
47 do { \
48 S = (__builtin_constant_p(N) && (N) == 1 \
49 ? X & 1 : (X << (_FP_W_TYPE_SIZE - (N))) != 0); \
50 X = X >> (N); \
51 } while (0)
53 #define __FP_FRAC_SRS_1(X,N,sz) \
54 (X = (X >> (N) | (__builtin_constant_p(N) && (N) == 1 \
55 ? X & 1 : (X << (_FP_W_TYPE_SIZE - (N))) != 0)))
57 #define _FP_FRAC_ADD_1(R,X,Y) (R##_f = X##_f + Y##_f)
58 #define _FP_FRAC_SUB_1(R,X,Y) (R##_f = X##_f - Y##_f)
59 #define _FP_FRAC_DEC_1(X,Y) (X##_f -= Y##_f)
60 #define _FP_FRAC_CLZ_1(z, X) __FP_CLZ(z, X##_f)
62 /* Predicates */
63 #define _FP_FRAC_NEGP_1(X) ((_FP_WS_TYPE)X##_f < 0)
64 #define _FP_FRAC_ZEROP_1(X) (X##_f == 0)
65 #define _FP_FRAC_OVERP_1(fs,X) (X##_f & _FP_OVERFLOW_##fs)
66 #define _FP_FRAC_CLEAR_OVERP_1(fs,X) (X##_f &= ~_FP_OVERFLOW_##fs)
67 #define _FP_FRAC_EQ_1(X, Y) (X##_f == Y##_f)
68 #define _FP_FRAC_GE_1(X, Y) (X##_f >= Y##_f)
69 #define _FP_FRAC_GT_1(X, Y) (X##_f > Y##_f)
71 #define _FP_ZEROFRAC_1 0
72 #define _FP_MINFRAC_1 1
73 #define _FP_MAXFRAC_1 (~(_FP_WS_TYPE)0)
76 * Unpack the raw bits of a native fp value. Do not classify or
77 * normalize the data.
80 #define _FP_UNPACK_RAW_1(fs, X, val) \
81 do { \
82 union _FP_UNION_##fs _flo; _flo.flt = (val); \
84 X##_f = _flo.bits.frac; \
85 X##_e = _flo.bits.exp; \
86 X##_s = _flo.bits.sign; \
87 } while (0)
89 #define _FP_UNPACK_RAW_1_P(fs, X, val) \
90 do { \
91 union _FP_UNION_##fs *_flo = \
92 (union _FP_UNION_##fs *)(val); \
94 X##_f = _flo->bits.frac; \
95 X##_e = _flo->bits.exp; \
96 X##_s = _flo->bits.sign; \
97 } while (0)
100 * Repack the raw bits of a native fp value.
103 #define _FP_PACK_RAW_1(fs, val, X) \
104 do { \
105 union _FP_UNION_##fs _flo; \
107 _flo.bits.frac = X##_f; \
108 _flo.bits.exp = X##_e; \
109 _flo.bits.sign = X##_s; \
111 (val) = _flo.flt; \
112 } while (0)
114 #define _FP_PACK_RAW_1_P(fs, val, X) \
115 do { \
116 union _FP_UNION_##fs *_flo = \
117 (union _FP_UNION_##fs *)(val); \
119 _flo->bits.frac = X##_f; \
120 _flo->bits.exp = X##_e; \
121 _flo->bits.sign = X##_s; \
122 } while (0)
126 * Multiplication algorithms:
129 /* Basic. Assuming the host word size is >= 2*FRACBITS, we can do the
130 multiplication immediately. */
132 #define _FP_MUL_MEAT_1_imm(wfracbits, R, X, Y) \
133 do { \
134 R##_f = X##_f * Y##_f; \
135 /* Normalize since we know where the msb of the multiplicands \
136 were (bit B), we know that the msb of the of the product is \
137 at either 2B or 2B-1. */ \
138 _FP_FRAC_SRS_1(R, wfracbits-1, 2*wfracbits); \
139 } while (0)
141 /* Given a 1W * 1W => 2W primitive, do the extended multiplication. */
143 #define _FP_MUL_MEAT_1_wide(wfracbits, R, X, Y, doit) \
144 do { \
145 _FP_W_TYPE _Z_f0, _Z_f1; \
146 doit(_Z_f1, _Z_f0, X##_f, Y##_f); \
147 /* Normalize since we know where the msb of the multiplicands \
148 were (bit B), we know that the msb of the of the product is \
149 at either 2B or 2B-1. */ \
150 _FP_FRAC_SRS_2(_Z, wfracbits-1, 2*wfracbits); \
151 R##_f = _Z_f0; \
152 } while (0)
154 /* Finally, a simple widening multiply algorithm. What fun! */
156 #define _FP_MUL_MEAT_1_hard(wfracbits, R, X, Y) \
157 do { \
158 _FP_W_TYPE _xh, _xl, _yh, _yl, _z_f0, _z_f1, _a_f0, _a_f1; \
160 /* split the words in half */ \
161 _xh = X##_f >> (_FP_W_TYPE_SIZE/2); \
162 _xl = X##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1); \
163 _yh = Y##_f >> (_FP_W_TYPE_SIZE/2); \
164 _yl = Y##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1); \
166 /* multiply the pieces */ \
167 _z_f0 = _xl * _yl; \
168 _a_f0 = _xh * _yl; \
169 _a_f1 = _xl * _yh; \
170 _z_f1 = _xh * _yh; \
172 /* reassemble into two full words */ \
173 if ((_a_f0 += _a_f1) < _a_f1) \
174 _z_f1 += (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2); \
175 _a_f1 = _a_f0 >> (_FP_W_TYPE_SIZE/2); \
176 _a_f0 = _a_f0 << (_FP_W_TYPE_SIZE/2); \
177 _FP_FRAC_ADD_2(_z, _z, _a); \
179 /* normalize */ \
180 _FP_FRAC_SRS_2(_z, wfracbits - 1, 2*wfracbits); \
181 R##_f = _z_f0; \
182 } while (0)
186 * Division algorithms:
189 /* Basic. Assuming the host word size is >= 2*FRACBITS, we can do the
190 division immediately. Give this macro either _FP_DIV_HELP_imm for
191 C primitives or _FP_DIV_HELP_ldiv for the ISO function. Which you
192 choose will depend on what the compiler does with divrem4. */
194 #define _FP_DIV_MEAT_1_imm(fs, R, X, Y, doit) \
195 do { \
196 _FP_W_TYPE _q, _r; \
197 X##_f <<= (X##_f < Y##_f \
198 ? R##_e--, _FP_WFRACBITS_##fs \
199 : _FP_WFRACBITS_##fs - 1); \
200 doit(_q, _r, X##_f, Y##_f); \
201 R##_f = _q | (_r != 0); \
202 } while (0)
204 /* GCC's longlong.h defines a 2W / 1W => (1W,1W) primitive udiv_qrnnd
205 that may be useful in this situation. This first is for a primitive
206 that requires normalization, the second for one that does not. Look
207 for UDIV_NEEDS_NORMALIZATION to tell which your machine needs. */
209 #define _FP_DIV_MEAT_1_udiv_norm(fs, R, X, Y) \
210 do { \
211 _FP_W_TYPE _nh, _nl, _q, _r, _y; \
213 /* Normalize Y -- i.e. make the most significant bit set. */ \
214 _y = Y##_f << _FP_WFRACXBITS_##fs; \
216 /* Shift X op correspondingly high, that is, up one full word. */ \
217 if (X##_f < Y##_f) \
219 R##_e--; \
220 _nl = 0; \
221 _nh = X##_f; \
223 else \
225 _nl = X##_f << (_FP_W_TYPE_SIZE - 1); \
226 _nh = X##_f >> 1; \
229 udiv_qrnnd(_q, _r, _nh, _nl, _y); \
230 R##_f = _q | (_r != 0); \
231 } while (0)
233 #define _FP_DIV_MEAT_1_udiv(fs, R, X, Y) \
234 do { \
235 _FP_W_TYPE _nh, _nl, _q, _r; \
236 if (X##_f < Y##_f) \
238 R##_e--; \
239 _nl = X##_f << _FP_WFRACBITS_##fs; \
240 _nh = X##_f >> _FP_WFRACXBITS_##fs; \
242 else \
244 _nl = X##_f << (_FP_WFRACBITS_##fs - 1); \
245 _nh = X##_f >> (_FP_WFRACXBITS_##fs + 1); \
247 udiv_qrnnd(_q, _r, _nh, _nl, Y##_f); \
248 R##_f = _q | (_r != 0); \
249 } while (0)
253 * Square root algorithms:
254 * We have just one right now, maybe Newton approximation
255 * should be added for those machines where division is fast.
258 #define _FP_SQRT_MEAT_1(R, S, T, X, q) \
259 do { \
260 while (q != _FP_WORK_ROUND) \
262 T##_f = S##_f + q; \
263 if (T##_f <= X##_f) \
265 S##_f = T##_f + q; \
266 X##_f -= T##_f; \
267 R##_f += q; \
269 _FP_FRAC_SLL_1(X, 1); \
270 q >>= 1; \
272 if (X##_f) \
274 if (S##_f < X##_f) \
275 R##_f |= _FP_WORK_ROUND; \
276 R##_f |= _FP_WORK_STICKY; \
278 } while (0)
281 * Assembly/disassembly for converting to/from integral types.
282 * No shifting or overflow handled here.
285 #define _FP_FRAC_ASSEMBLE_1(r, X, rsize) (r = X##_f)
286 #define _FP_FRAC_DISASSEMBLE_1(X, r, rsize) (X##_f = r)
290 * Convert FP values between word sizes
293 #define _FP_FRAC_COPY_1_1(D, S) (D##_f = S##_f)