Fix math_private.h multiple include guards.
[glibc.git] / soft-fp / extended.h
blob428f9178a6f3348dafdcc9f53b59983430fd855b
1 /* Software floating-point emulation.
2 Definitions for IEEE Extended Precision.
3 Copyright (C) 1999-2015 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Jakub Jelinek (jj@ultra.linux.cz).
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU Lesser General Public
13 License, the Free Software Foundation gives you unlimited
14 permission to link the compiled version of this file into
15 combinations with other programs, and to distribute those
16 combinations without any restriction coming from the use of this
17 file. (The Lesser General Public License restrictions do apply in
18 other respects; for example, they cover modification of the file,
19 and distribution when not linked into a combine executable.)
21 The GNU C Library is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 Lesser General Public License for more details.
26 You should have received a copy of the GNU Lesser General Public
27 License along with the GNU C Library; if not, see
28 <http://www.gnu.org/licenses/>. */
30 #ifndef SOFT_FP_EXTENDED_H
31 #define SOFT_FP_EXTENDED_H 1
33 #if _FP_W_TYPE_SIZE < 32
34 # error "Here's a nickel, kid. Go buy yourself a real computer."
35 #endif
37 #if _FP_W_TYPE_SIZE < 64
38 # define _FP_FRACTBITS_E (4*_FP_W_TYPE_SIZE)
39 # define _FP_FRACTBITS_DW_E (8*_FP_W_TYPE_SIZE)
40 #else
41 # define _FP_FRACTBITS_E (2*_FP_W_TYPE_SIZE)
42 # define _FP_FRACTBITS_DW_E (4*_FP_W_TYPE_SIZE)
43 #endif
45 #define _FP_FRACBITS_E 64
46 #define _FP_FRACXBITS_E (_FP_FRACTBITS_E - _FP_FRACBITS_E)
47 #define _FP_WFRACBITS_E (_FP_WORKBITS + _FP_FRACBITS_E)
48 #define _FP_WFRACXBITS_E (_FP_FRACTBITS_E - _FP_WFRACBITS_E)
49 #define _FP_EXPBITS_E 15
50 #define _FP_EXPBIAS_E 16383
51 #define _FP_EXPMAX_E 32767
53 #define _FP_QNANBIT_E \
54 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-2) % _FP_W_TYPE_SIZE)
55 #define _FP_QNANBIT_SH_E \
56 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-2+_FP_WORKBITS) % _FP_W_TYPE_SIZE)
57 #define _FP_IMPLBIT_E \
58 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-1) % _FP_W_TYPE_SIZE)
59 #define _FP_IMPLBIT_SH_E \
60 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-1+_FP_WORKBITS) % _FP_W_TYPE_SIZE)
61 #define _FP_OVERFLOW_E \
62 ((_FP_W_TYPE) 1 << (_FP_WFRACBITS_E % _FP_W_TYPE_SIZE))
64 #define _FP_WFRACBITS_DW_E (2 * _FP_WFRACBITS_E)
65 #define _FP_WFRACXBITS_DW_E (_FP_FRACTBITS_DW_E - _FP_WFRACBITS_DW_E)
66 #define _FP_HIGHBIT_DW_E \
67 ((_FP_W_TYPE) 1 << (_FP_WFRACBITS_DW_E - 1) % _FP_W_TYPE_SIZE)
69 typedef float XFtype __attribute__ ((mode (XF)));
71 #if _FP_W_TYPE_SIZE < 64
73 union _FP_UNION_E
75 XFtype flt;
76 struct _FP_STRUCT_LAYOUT
78 # if __BYTE_ORDER == __BIG_ENDIAN
79 unsigned long pad1 : _FP_W_TYPE_SIZE;
80 unsigned long pad2 : (_FP_W_TYPE_SIZE - 1 - _FP_EXPBITS_E);
81 unsigned long sign : 1;
82 unsigned long exp : _FP_EXPBITS_E;
83 unsigned long frac1 : _FP_W_TYPE_SIZE;
84 unsigned long frac0 : _FP_W_TYPE_SIZE;
85 # else
86 unsigned long frac0 : _FP_W_TYPE_SIZE;
87 unsigned long frac1 : _FP_W_TYPE_SIZE;
88 unsigned exp : _FP_EXPBITS_E;
89 unsigned sign : 1;
90 # endif /* not bigendian */
91 } bits __attribute__ ((packed));
95 # define FP_DECL_E(X) _FP_DECL (4, X)
97 # define FP_UNPACK_RAW_E(X, val) \
98 do \
99 { \
100 union _FP_UNION_E FP_UNPACK_RAW_E_flo; \
101 FP_UNPACK_RAW_E_flo.flt = (val); \
103 X##_f[2] = 0; \
104 X##_f[3] = 0; \
105 X##_f[0] = FP_UNPACK_RAW_E_flo.bits.frac0; \
106 X##_f[1] = FP_UNPACK_RAW_E_flo.bits.frac1; \
107 X##_e = FP_UNPACK_RAW_E_flo.bits.exp; \
108 X##_s = FP_UNPACK_RAW_E_flo.bits.sign; \
110 while (0)
112 # define FP_UNPACK_RAW_EP(X, val) \
113 do \
115 union _FP_UNION_E *FP_UNPACK_RAW_EP_flo \
116 = (union _FP_UNION_E *) (val); \
118 X##_f[2] = 0; \
119 X##_f[3] = 0; \
120 X##_f[0] = FP_UNPACK_RAW_EP_flo->bits.frac0; \
121 X##_f[1] = FP_UNPACK_RAW_EP_flo->bits.frac1; \
122 X##_e = FP_UNPACK_RAW_EP_flo->bits.exp; \
123 X##_s = FP_UNPACK_RAW_EP_flo->bits.sign; \
125 while (0)
127 # define FP_PACK_RAW_E(val, X) \
128 do \
130 union _FP_UNION_E FP_PACK_RAW_E_flo; \
132 if (X##_e) \
133 X##_f[1] |= _FP_IMPLBIT_E; \
134 else \
135 X##_f[1] &= ~(_FP_IMPLBIT_E); \
136 FP_PACK_RAW_E_flo.bits.frac0 = X##_f[0]; \
137 FP_PACK_RAW_E_flo.bits.frac1 = X##_f[1]; \
138 FP_PACK_RAW_E_flo.bits.exp = X##_e; \
139 FP_PACK_RAW_E_flo.bits.sign = X##_s; \
141 (val) = FP_PACK_RAW_E_flo.flt; \
143 while (0)
145 # define FP_PACK_RAW_EP(val, X) \
146 do \
148 if (!FP_INHIBIT_RESULTS) \
150 union _FP_UNION_E *FP_PACK_RAW_EP_flo \
151 = (union _FP_UNION_E *) (val); \
153 if (X##_e) \
154 X##_f[1] |= _FP_IMPLBIT_E; \
155 else \
156 X##_f[1] &= ~(_FP_IMPLBIT_E); \
157 FP_PACK_RAW_EP_flo->bits.frac0 = X##_f[0]; \
158 FP_PACK_RAW_EP_flo->bits.frac1 = X##_f[1]; \
159 FP_PACK_RAW_EP_flo->bits.exp = X##_e; \
160 FP_PACK_RAW_EP_flo->bits.sign = X##_s; \
163 while (0)
165 # define FP_UNPACK_E(X, val) \
166 do \
168 FP_UNPACK_RAW_E (X, (val)); \
169 _FP_UNPACK_CANONICAL (E, 4, X); \
171 while (0)
173 # define FP_UNPACK_EP(X, val) \
174 do \
176 FP_UNPACK_RAW_EP (X, (val)); \
177 _FP_UNPACK_CANONICAL (E, 4, X); \
179 while (0)
181 # define FP_UNPACK_SEMIRAW_E(X, val) \
182 do \
184 FP_UNPACK_RAW_E (X, (val)); \
185 _FP_UNPACK_SEMIRAW (E, 4, X); \
187 while (0)
189 # define FP_UNPACK_SEMIRAW_EP(X, val) \
190 do \
192 FP_UNPACK_RAW_EP (X, (val)); \
193 _FP_UNPACK_SEMIRAW (E, 4, X); \
195 while (0)
197 # define FP_PACK_E(val, X) \
198 do \
200 _FP_PACK_CANONICAL (E, 4, X); \
201 FP_PACK_RAW_E ((val), X); \
203 while (0)
205 # define FP_PACK_EP(val, X) \
206 do \
208 _FP_PACK_CANONICAL (E, 4, X); \
209 FP_PACK_RAW_EP ((val), X); \
211 while (0)
213 # define FP_PACK_SEMIRAW_E(val, X) \
214 do \
216 _FP_PACK_SEMIRAW (E, 4, X); \
217 FP_PACK_RAW_E ((val), X); \
219 while (0)
221 # define FP_PACK_SEMIRAW_EP(val, X) \
222 do \
224 _FP_PACK_SEMIRAW (E, 4, X); \
225 FP_PACK_RAW_EP ((val), X); \
227 while (0)
229 # define FP_ISSIGNAN_E(X) _FP_ISSIGNAN (E, 4, X)
230 # define FP_NEG_E(R, X) _FP_NEG (E, 4, R, X)
231 # define FP_ADD_E(R, X, Y) _FP_ADD (E, 4, R, X, Y)
232 # define FP_SUB_E(R, X, Y) _FP_SUB (E, 4, R, X, Y)
233 # define FP_MUL_E(R, X, Y) _FP_MUL (E, 4, R, X, Y)
234 # define FP_DIV_E(R, X, Y) _FP_DIV (E, 4, R, X, Y)
235 # define FP_SQRT_E(R, X) _FP_SQRT (E, 4, R, X)
236 # define FP_FMA_E(R, X, Y, Z) _FP_FMA (E, 4, 8, R, X, Y, Z)
238 /* Square root algorithms:
239 We have just one right now, maybe Newton approximation
240 should be added for those machines where division is fast.
241 This has special _E version because standard _4 square
242 root would not work (it has to start normally with the
243 second word and not the first), but as we have to do it
244 anyway, we optimize it by doing most of the calculations
245 in two UWtype registers instead of four. */
247 # define _FP_SQRT_MEAT_E(R, S, T, X, q) \
248 do \
250 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
251 _FP_FRAC_SRL_4 (X, (_FP_WORKBITS)); \
252 while (q) \
254 T##_f[1] = S##_f[1] + (q); \
255 if (T##_f[1] <= X##_f[1]) \
257 S##_f[1] = T##_f[1] + (q); \
258 X##_f[1] -= T##_f[1]; \
259 R##_f[1] += (q); \
261 _FP_FRAC_SLL_2 (X, 1); \
262 (q) >>= 1; \
264 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
265 while (q) \
267 T##_f[0] = S##_f[0] + (q); \
268 T##_f[1] = S##_f[1]; \
269 if (T##_f[1] < X##_f[1] \
270 || (T##_f[1] == X##_f[1] \
271 && T##_f[0] <= X##_f[0])) \
273 S##_f[0] = T##_f[0] + (q); \
274 S##_f[1] += (T##_f[0] > S##_f[0]); \
275 _FP_FRAC_DEC_2 (X, T); \
276 R##_f[0] += (q); \
278 _FP_FRAC_SLL_2 (X, 1); \
279 (q) >>= 1; \
281 _FP_FRAC_SLL_4 (R, (_FP_WORKBITS)); \
282 if (X##_f[0] | X##_f[1]) \
284 if (S##_f[1] < X##_f[1] \
285 || (S##_f[1] == X##_f[1] \
286 && S##_f[0] < X##_f[0])) \
287 R##_f[0] |= _FP_WORK_ROUND; \
288 R##_f[0] |= _FP_WORK_STICKY; \
291 while (0)
293 # define FP_CMP_E(r, X, Y, un, ex) _FP_CMP (E, 4, (r), X, Y, (un), (ex))
294 # define FP_CMP_EQ_E(r, X, Y, ex) _FP_CMP_EQ (E, 4, (r), X, Y, (ex))
295 # define FP_CMP_UNORD_E(r, X, Y, ex) _FP_CMP_UNORD (E, 4, (r), X, Y, (ex))
297 # define FP_TO_INT_E(r, X, rsz, rsg) _FP_TO_INT (E, 4, (r), X, (rsz), (rsg))
298 # define FP_TO_INT_ROUND_E(r, X, rsz, rsg) \
299 _FP_TO_INT_ROUND (E, 4, (r), X, (rsz), (rsg))
300 # define FP_FROM_INT_E(X, r, rs, rt) _FP_FROM_INT (E, 4, X, (r), (rs), rt)
302 # define _FP_FRAC_HIGH_E(X) (X##_f[2])
303 # define _FP_FRAC_HIGH_RAW_E(X) (X##_f[1])
305 # define _FP_FRAC_HIGH_DW_E(X) (X##_f[4])
307 #else /* not _FP_W_TYPE_SIZE < 64 */
308 union _FP_UNION_E
310 XFtype flt;
311 struct _FP_STRUCT_LAYOUT
313 # if __BYTE_ORDER == __BIG_ENDIAN
314 _FP_W_TYPE pad : (_FP_W_TYPE_SIZE - 1 - _FP_EXPBITS_E);
315 unsigned sign : 1;
316 unsigned exp : _FP_EXPBITS_E;
317 _FP_W_TYPE frac : _FP_W_TYPE_SIZE;
318 # else
319 _FP_W_TYPE frac : _FP_W_TYPE_SIZE;
320 unsigned exp : _FP_EXPBITS_E;
321 unsigned sign : 1;
322 # endif
323 } bits;
326 # define FP_DECL_E(X) _FP_DECL (2, X)
328 # define FP_UNPACK_RAW_E(X, val) \
329 do \
331 union _FP_UNION_E FP_UNPACK_RAW_E_flo; \
332 FP_UNPACK_RAW_E_flo.flt = (val); \
334 X##_f0 = FP_UNPACK_RAW_E_flo.bits.frac; \
335 X##_f1 = 0; \
336 X##_e = FP_UNPACK_RAW_E_flo.bits.exp; \
337 X##_s = FP_UNPACK_RAW_E_flo.bits.sign; \
339 while (0)
341 # define FP_UNPACK_RAW_EP(X, val) \
342 do \
344 union _FP_UNION_E *FP_UNPACK_RAW_EP_flo \
345 = (union _FP_UNION_E *) (val); \
347 X##_f0 = FP_UNPACK_RAW_EP_flo->bits.frac; \
348 X##_f1 = 0; \
349 X##_e = FP_UNPACK_RAW_EP_flo->bits.exp; \
350 X##_s = FP_UNPACK_RAW_EP_flo->bits.sign; \
352 while (0)
354 # define FP_PACK_RAW_E(val, X) \
355 do \
357 union _FP_UNION_E FP_PACK_RAW_E_flo; \
359 if (X##_e) \
360 X##_f0 |= _FP_IMPLBIT_E; \
361 else \
362 X##_f0 &= ~(_FP_IMPLBIT_E); \
363 FP_PACK_RAW_E_flo.bits.frac = X##_f0; \
364 FP_PACK_RAW_E_flo.bits.exp = X##_e; \
365 FP_PACK_RAW_E_flo.bits.sign = X##_s; \
367 (val) = FP_PACK_RAW_E_flo.flt; \
369 while (0)
371 # define FP_PACK_RAW_EP(fs, val, X) \
372 do \
374 if (!FP_INHIBIT_RESULTS) \
376 union _FP_UNION_E *FP_PACK_RAW_EP_flo \
377 = (union _FP_UNION_E *) (val); \
379 if (X##_e) \
380 X##_f0 |= _FP_IMPLBIT_E; \
381 else \
382 X##_f0 &= ~(_FP_IMPLBIT_E); \
383 FP_PACK_RAW_EP_flo->bits.frac = X##_f0; \
384 FP_PACK_RAW_EP_flo->bits.exp = X##_e; \
385 FP_PACK_RAW_EP_flo->bits.sign = X##_s; \
388 while (0)
391 # define FP_UNPACK_E(X, val) \
392 do \
394 FP_UNPACK_RAW_E (X, (val)); \
395 _FP_UNPACK_CANONICAL (E, 2, X); \
397 while (0)
399 # define FP_UNPACK_EP(X, val) \
400 do \
402 FP_UNPACK_RAW_EP (X, (val)); \
403 _FP_UNPACK_CANONICAL (E, 2, X); \
405 while (0)
407 # define FP_UNPACK_SEMIRAW_E(X, val) \
408 do \
410 FP_UNPACK_RAW_E (X, (val)); \
411 _FP_UNPACK_SEMIRAW (E, 2, X); \
413 while (0)
415 # define FP_UNPACK_SEMIRAW_EP(X, val) \
416 do \
418 FP_UNPACK_RAW_EP (X, (val)); \
419 _FP_UNPACK_SEMIRAW (E, 2, X); \
421 while (0)
423 # define FP_PACK_E(val, X) \
424 do \
426 _FP_PACK_CANONICAL (E, 2, X); \
427 FP_PACK_RAW_E ((val), X); \
429 while (0)
431 # define FP_PACK_EP(val, X) \
432 do \
434 _FP_PACK_CANONICAL (E, 2, X); \
435 FP_PACK_RAW_EP ((val), X); \
437 while (0)
439 # define FP_PACK_SEMIRAW_E(val, X) \
440 do \
442 _FP_PACK_SEMIRAW (E, 2, X); \
443 FP_PACK_RAW_E ((val), X); \
445 while (0)
447 # define FP_PACK_SEMIRAW_EP(val, X) \
448 do \
450 _FP_PACK_SEMIRAW (E, 2, X); \
451 FP_PACK_RAW_EP ((val), X); \
453 while (0)
455 # define FP_ISSIGNAN_E(X) _FP_ISSIGNAN (E, 2, X)
456 # define FP_NEG_E(R, X) _FP_NEG (E, 2, R, X)
457 # define FP_ADD_E(R, X, Y) _FP_ADD (E, 2, R, X, Y)
458 # define FP_SUB_E(R, X, Y) _FP_SUB (E, 2, R, X, Y)
459 # define FP_MUL_E(R, X, Y) _FP_MUL (E, 2, R, X, Y)
460 # define FP_DIV_E(R, X, Y) _FP_DIV (E, 2, R, X, Y)
461 # define FP_SQRT_E(R, X) _FP_SQRT (E, 2, R, X)
462 # define FP_FMA_E(R, X, Y, Z) _FP_FMA (E, 2, 4, R, X, Y, Z)
464 /* Square root algorithms:
465 We have just one right now, maybe Newton approximation
466 should be added for those machines where division is fast.
467 We optimize it by doing most of the calculations
468 in one UWtype registers instead of two, although we don't
469 have to. */
470 # define _FP_SQRT_MEAT_E(R, S, T, X, q) \
471 do \
473 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
474 _FP_FRAC_SRL_2 (X, (_FP_WORKBITS)); \
475 while (q) \
477 T##_f0 = S##_f0 + (q); \
478 if (T##_f0 <= X##_f0) \
480 S##_f0 = T##_f0 + (q); \
481 X##_f0 -= T##_f0; \
482 R##_f0 += (q); \
484 _FP_FRAC_SLL_1 (X, 1); \
485 (q) >>= 1; \
487 _FP_FRAC_SLL_2 (R, (_FP_WORKBITS)); \
488 if (X##_f0) \
490 if (S##_f0 < X##_f0) \
491 R##_f0 |= _FP_WORK_ROUND; \
492 R##_f0 |= _FP_WORK_STICKY; \
495 while (0)
497 # define FP_CMP_E(r, X, Y, un, ex) _FP_CMP (E, 2, (r), X, Y, (un), (ex))
498 # define FP_CMP_EQ_E(r, X, Y, ex) _FP_CMP_EQ (E, 2, (r), X, Y, (ex))
499 # define FP_CMP_UNORD_E(r, X, Y, ex) _FP_CMP_UNORD (E, 2, (r), X, Y, (ex))
501 # define FP_TO_INT_E(r, X, rsz, rsg) _FP_TO_INT (E, 2, (r), X, (rsz), (rsg))
502 # define FP_TO_INT_ROUND_E(r, X, rsz, rsg) \
503 _FP_TO_INT_ROUND (E, 2, (r), X, (rsz), (rsg))
504 # define FP_FROM_INT_E(X, r, rs, rt) _FP_FROM_INT (E, 2, X, (r), (rs), rt)
506 # define _FP_FRAC_HIGH_E(X) (X##_f1)
507 # define _FP_FRAC_HIGH_RAW_E(X) (X##_f0)
509 # define _FP_FRAC_HIGH_DW_E(X) (X##_f[2])
511 #endif /* not _FP_W_TYPE_SIZE < 64 */
513 #endif /* !SOFT_FP_EXTENDED_H */