]> git.ipfire.org Git - thirdparty/glibc.git/blob - soft-fp/op-common.h
* soft-fp/Makefile (gcc-single-routines): Add unordsf2,
[thirdparty/glibc.git] / soft-fp / op-common.h
1 /* Software floating-point emulation. Common operations.
2 Copyright (C) 1997,1998,1999,2006 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 The GNU C Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with the GNU C Library; if not, write to the Free
21 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 02111-1307 USA. */
23
24 #define _FP_DECL(wc, X) \
25 _FP_I_TYPE X##_c, X##_s, X##_e; \
26 _FP_FRAC_DECL_##wc(X)
27
28 /*
29 * Finish truely unpacking a native fp value by classifying the kind
30 * of fp value and normalizing both the exponent and the fraction.
31 */
32
33 #define _FP_UNPACK_CANONICAL(fs, wc, X) \
34 do { \
35 switch (X##_e) \
36 { \
37 default: \
38 _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_IMPLBIT_##fs; \
39 _FP_FRAC_SLL_##wc(X, _FP_WORKBITS); \
40 X##_e -= _FP_EXPBIAS_##fs; \
41 X##_c = FP_CLS_NORMAL; \
42 break; \
43 \
44 case 0: \
45 if (_FP_FRAC_ZEROP_##wc(X)) \
46 X##_c = FP_CLS_ZERO; \
47 else \
48 { \
49 /* a denormalized number */ \
50 _FP_I_TYPE _shift; \
51 _FP_FRAC_CLZ_##wc(_shift, X); \
52 _shift -= _FP_FRACXBITS_##fs; \
53 _FP_FRAC_SLL_##wc(X, (_shift+_FP_WORKBITS)); \
54 X##_e -= _FP_EXPBIAS_##fs - 1 + _shift; \
55 X##_c = FP_CLS_NORMAL; \
56 FP_SET_EXCEPTION(FP_EX_DENORM); \
57 } \
58 break; \
59 \
60 case _FP_EXPMAX_##fs: \
61 if (_FP_FRAC_ZEROP_##wc(X)) \
62 X##_c = FP_CLS_INF; \
63 else \
64 { \
65 X##_c = FP_CLS_NAN; \
66 /* Check for signaling NaN */ \
67 if (!(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs)) \
68 FP_SET_EXCEPTION(FP_EX_INVALID); \
69 } \
70 break; \
71 } \
72 } while (0)
73
74 /*
75 * Before packing the bits back into the native fp result, take care
76 * of such mundane things as rounding and overflow. Also, for some
77 * kinds of fp values, the original parts may not have been fully
78 * extracted -- but that is ok, we can regenerate them now.
79 */
80
81 #define _FP_PACK_CANONICAL(fs, wc, X) \
82 do { \
83 switch (X##_c) \
84 { \
85 case FP_CLS_NORMAL: \
86 X##_e += _FP_EXPBIAS_##fs; \
87 if (X##_e > 0) \
88 { \
89 _FP_ROUND(wc, X); \
90 if (_FP_FRAC_OVERP_##wc(fs, X)) \
91 { \
92 _FP_FRAC_CLEAR_OVERP_##wc(fs, X); \
93 X##_e++; \
94 } \
95 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \
96 if (X##_e >= _FP_EXPMAX_##fs) \
97 { \
98 /* overflow */ \
99 switch (FP_ROUNDMODE) \
100 { \
101 case FP_RND_NEAREST: \
102 X##_c = FP_CLS_INF; \
103 break; \
104 case FP_RND_PINF: \
105 if (!X##_s) X##_c = FP_CLS_INF; \
106 break; \
107 case FP_RND_MINF: \
108 if (X##_s) X##_c = FP_CLS_INF; \
109 break; \
110 } \
111 if (X##_c == FP_CLS_INF) \
112 { \
113 /* Overflow to infinity */ \
114 X##_e = _FP_EXPMAX_##fs; \
115 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
116 } \
117 else \
118 { \
119 /* Overflow to maximum normal */ \
120 X##_e = _FP_EXPMAX_##fs - 1; \
121 _FP_FRAC_SET_##wc(X, _FP_MAXFRAC_##wc); \
122 } \
123 FP_SET_EXCEPTION(FP_EX_OVERFLOW); \
124 FP_SET_EXCEPTION(FP_EX_INEXACT); \
125 } \
126 } \
127 else \
128 { \
129 /* we've got a denormalized number */ \
130 X##_e = -X##_e + 1; \
131 if (X##_e <= _FP_WFRACBITS_##fs) \
132 { \
133 _FP_FRAC_SRS_##wc(X, X##_e, _FP_WFRACBITS_##fs); \
134 _FP_ROUND(wc, X); \
135 if (_FP_FRAC_HIGH_##fs(X) \
136 & (_FP_OVERFLOW_##fs >> 1)) \
137 { \
138 X##_e = 1; \
139 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
140 } \
141 else \
142 { \
143 X##_e = 0; \
144 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \
145 FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \
146 } \
147 } \
148 else \
149 { \
150 /* underflow to zero */ \
151 X##_e = 0; \
152 if (!_FP_FRAC_ZEROP_##wc(X)) \
153 { \
154 _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \
155 _FP_ROUND(wc, X); \
156 _FP_FRAC_LOW_##wc(X) >>= (_FP_WORKBITS); \
157 } \
158 FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \
159 } \
160 } \
161 break; \
162 \
163 case FP_CLS_ZERO: \
164 X##_e = 0; \
165 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
166 break; \
167 \
168 case FP_CLS_INF: \
169 X##_e = _FP_EXPMAX_##fs; \
170 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
171 break; \
172 \
173 case FP_CLS_NAN: \
174 X##_e = _FP_EXPMAX_##fs; \
175 if (!_FP_KEEPNANFRACP) \
176 { \
177 _FP_FRAC_SET_##wc(X, _FP_NANFRAC_##fs); \
178 X##_s = _FP_NANSIGN_##fs; \
179 } \
180 else \
181 _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_QNANBIT_##fs; \
182 break; \
183 } \
184 } while (0)
185
186 /* This one accepts raw argument and not cooked, returns
187 * 1 if X is a signaling NaN.
188 */
189 #define _FP_ISSIGNAN(fs, wc, X) \
190 ({ \
191 int __ret = 0; \
192 if (X##_e == _FP_EXPMAX_##fs) \
193 { \
194 if (!_FP_FRAC_ZEROP_##wc(X) \
195 && !(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs)) \
196 __ret = 1; \
197 } \
198 __ret; \
199 })
200
201
202
203
204
205 /*
206 * Main addition routine. The input values should be cooked.
207 */
208
209 #define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP) \
210 do { \
211 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \
212 { \
213 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \
214 { \
215 /* shift the smaller number so that its exponent matches the larger */ \
216 _FP_I_TYPE diff = X##_e - Y##_e; \
217 \
218 if (diff < 0) \
219 { \
220 diff = -diff; \
221 if (diff <= _FP_WFRACBITS_##fs) \
222 _FP_FRAC_SRS_##wc(X, diff, _FP_WFRACBITS_##fs); \
223 else if (!_FP_FRAC_ZEROP_##wc(X)) \
224 _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \
225 R##_e = Y##_e; \
226 } \
227 else \
228 { \
229 if (diff > 0) \
230 { \
231 if (diff <= _FP_WFRACBITS_##fs) \
232 _FP_FRAC_SRS_##wc(Y, diff, _FP_WFRACBITS_##fs); \
233 else if (!_FP_FRAC_ZEROP_##wc(Y)) \
234 _FP_FRAC_SET_##wc(Y, _FP_MINFRAC_##wc); \
235 } \
236 R##_e = X##_e; \
237 } \
238 \
239 R##_c = FP_CLS_NORMAL; \
240 \
241 if (X##_s == Y##_s) \
242 { \
243 R##_s = X##_s; \
244 _FP_FRAC_ADD_##wc(R, X, Y); \
245 if (_FP_FRAC_OVERP_##wc(fs, R)) \
246 { \
247 _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs); \
248 R##_e++; \
249 } \
250 } \
251 else \
252 { \
253 R##_s = X##_s; \
254 _FP_FRAC_SUB_##wc(R, X, Y); \
255 if (_FP_FRAC_ZEROP_##wc(R)) \
256 { \
257 /* return an exact zero */ \
258 if (FP_ROUNDMODE == FP_RND_MINF) \
259 R##_s |= Y##_s; \
260 else \
261 R##_s &= Y##_s; \
262 R##_c = FP_CLS_ZERO; \
263 } \
264 else \
265 { \
266 if (_FP_FRAC_NEGP_##wc(R)) \
267 { \
268 _FP_FRAC_SUB_##wc(R, Y, X); \
269 R##_s = Y##_s; \
270 } \
271 \
272 /* renormalize after subtraction */ \
273 _FP_FRAC_CLZ_##wc(diff, R); \
274 diff -= _FP_WFRACXBITS_##fs; \
275 if (diff) \
276 { \
277 R##_e -= diff; \
278 _FP_FRAC_SLL_##wc(R, diff); \
279 } \
280 } \
281 } \
282 break; \
283 } \
284 \
285 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \
286 _FP_CHOOSENAN(fs, wc, R, X, Y, OP); \
287 break; \
288 \
289 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \
290 R##_e = X##_e; \
291 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \
292 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \
293 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \
294 _FP_FRAC_COPY_##wc(R, X); \
295 R##_s = X##_s; \
296 R##_c = X##_c; \
297 break; \
298 \
299 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \
300 R##_e = Y##_e; \
301 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \
302 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \
303 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \
304 _FP_FRAC_COPY_##wc(R, Y); \
305 R##_s = Y##_s; \
306 R##_c = Y##_c; \
307 break; \
308 \
309 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \
310 if (X##_s != Y##_s) \
311 { \
312 /* +INF + -INF => NAN */ \
313 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
314 R##_s = _FP_NANSIGN_##fs; \
315 R##_c = FP_CLS_NAN; \
316 FP_SET_EXCEPTION(FP_EX_INVALID); \
317 break; \
318 } \
319 /* FALLTHRU */ \
320 \
321 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \
322 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \
323 R##_s = X##_s; \
324 R##_c = FP_CLS_INF; \
325 break; \
326 \
327 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \
328 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \
329 R##_s = Y##_s; \
330 R##_c = FP_CLS_INF; \
331 break; \
332 \
333 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \
334 /* make sure the sign is correct */ \
335 if (FP_ROUNDMODE == FP_RND_MINF) \
336 R##_s = X##_s | Y##_s; \
337 else \
338 R##_s = X##_s & Y##_s; \
339 R##_c = FP_CLS_ZERO; \
340 break; \
341 \
342 default: \
343 abort(); \
344 } \
345 } while (0)
346
347 #define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTERNAL(fs, wc, R, X, Y, '+')
348 #define _FP_SUB(fs, wc, R, X, Y) \
349 do { \
350 if (Y##_c != FP_CLS_NAN) Y##_s ^= 1; \
351 _FP_ADD_INTERNAL(fs, wc, R, X, Y, '-'); \
352 } while (0)
353
354
355 /*
356 * Main negation routine. FIXME -- when we care about setting exception
357 * bits reliably, this will not do. We should examine all of the fp classes.
358 */
359
360 #define _FP_NEG(fs, wc, R, X) \
361 do { \
362 _FP_FRAC_COPY_##wc(R, X); \
363 R##_c = X##_c; \
364 R##_e = X##_e; \
365 R##_s = 1 ^ X##_s; \
366 } while (0)
367
368
369 /*
370 * Main multiplication routine. The input values should be cooked.
371 */
372
373 #define _FP_MUL(fs, wc, R, X, Y) \
374 do { \
375 R##_s = X##_s ^ Y##_s; \
376 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \
377 { \
378 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \
379 R##_c = FP_CLS_NORMAL; \
380 R##_e = X##_e + Y##_e + 1; \
381 \
382 _FP_MUL_MEAT_##fs(R,X,Y); \
383 \
384 if (_FP_FRAC_OVERP_##wc(fs, R)) \
385 _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs); \
386 else \
387 R##_e--; \
388 break; \
389 \
390 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \
391 _FP_CHOOSENAN(fs, wc, R, X, Y, '*'); \
392 break; \
393 \
394 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \
395 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \
396 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \
397 R##_s = X##_s; \
398 \
399 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \
400 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \
401 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \
402 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \
403 _FP_FRAC_COPY_##wc(R, X); \
404 R##_c = X##_c; \
405 break; \
406 \
407 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \
408 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \
409 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \
410 R##_s = Y##_s; \
411 \
412 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \
413 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \
414 _FP_FRAC_COPY_##wc(R, Y); \
415 R##_c = Y##_c; \
416 break; \
417 \
418 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \
419 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \
420 R##_s = _FP_NANSIGN_##fs; \
421 R##_c = FP_CLS_NAN; \
422 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
423 FP_SET_EXCEPTION(FP_EX_INVALID); \
424 break; \
425 \
426 default: \
427 abort(); \
428 } \
429 } while (0)
430
431
432 /*
433 * Main division routine. The input values should be cooked.
434 */
435
436 #define _FP_DIV(fs, wc, R, X, Y) \
437 do { \
438 R##_s = X##_s ^ Y##_s; \
439 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \
440 { \
441 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \
442 R##_c = FP_CLS_NORMAL; \
443 R##_e = X##_e - Y##_e; \
444 \
445 _FP_DIV_MEAT_##fs(R,X,Y); \
446 break; \
447 \
448 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \
449 _FP_CHOOSENAN(fs, wc, R, X, Y, '/'); \
450 break; \
451 \
452 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \
453 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \
454 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \
455 R##_s = X##_s; \
456 _FP_FRAC_COPY_##wc(R, X); \
457 R##_c = X##_c; \
458 break; \
459 \
460 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \
461 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \
462 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \
463 R##_s = Y##_s; \
464 _FP_FRAC_COPY_##wc(R, Y); \
465 R##_c = Y##_c; \
466 break; \
467 \
468 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \
469 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \
470 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \
471 R##_c = FP_CLS_ZERO; \
472 break; \
473 \
474 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \
475 FP_SET_EXCEPTION(FP_EX_DIVZERO); \
476 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \
477 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \
478 R##_c = FP_CLS_INF; \
479 break; \
480 \
481 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \
482 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \
483 R##_s = _FP_NANSIGN_##fs; \
484 R##_c = FP_CLS_NAN; \
485 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
486 FP_SET_EXCEPTION(FP_EX_INVALID); \
487 break; \
488 \
489 default: \
490 abort(); \
491 } \
492 } while (0)
493
494
495 /*
496 * Main differential comparison routine. The inputs should be raw not
497 * cooked. The return is -1,0,1 for normal values, 2 otherwise.
498 */
499
500 #define _FP_CMP(fs, wc, ret, X, Y, un) \
501 do { \
502 /* NANs are unordered */ \
503 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \
504 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))) \
505 { \
506 ret = un; \
507 } \
508 else \
509 { \
510 int __is_zero_x; \
511 int __is_zero_y; \
512 \
513 __is_zero_x = (!X##_e && _FP_FRAC_ZEROP_##wc(X)) ? 1 : 0; \
514 __is_zero_y = (!Y##_e && _FP_FRAC_ZEROP_##wc(Y)) ? 1 : 0; \
515 \
516 if (__is_zero_x && __is_zero_y) \
517 ret = 0; \
518 else if (__is_zero_x) \
519 ret = Y##_s ? 1 : -1; \
520 else if (__is_zero_y) \
521 ret = X##_s ? -1 : 1; \
522 else if (X##_s != Y##_s) \
523 ret = X##_s ? -1 : 1; \
524 else if (X##_e > Y##_e) \
525 ret = X##_s ? -1 : 1; \
526 else if (X##_e < Y##_e) \
527 ret = X##_s ? 1 : -1; \
528 else if (_FP_FRAC_GT_##wc(X, Y)) \
529 ret = X##_s ? -1 : 1; \
530 else if (_FP_FRAC_GT_##wc(Y, X)) \
531 ret = X##_s ? 1 : -1; \
532 else \
533 ret = 0; \
534 } \
535 } while (0)
536
537
538 /* Simplification for strict equality. */
539
540 #define _FP_CMP_EQ(fs, wc, ret, X, Y) \
541 do { \
542 /* NANs are unordered */ \
543 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \
544 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))) \
545 { \
546 ret = 1; \
547 } \
548 else \
549 { \
550 ret = !(X##_e == Y##_e \
551 && _FP_FRAC_EQ_##wc(X, Y) \
552 && (X##_s == Y##_s || !X##_e && _FP_FRAC_ZEROP_##wc(X))); \
553 } \
554 } while (0)
555
556 /* Version to test unordered. */
557
558 #define _FP_CMP_UNORD(fs, wc, ret, X, Y) \
559 do { \
560 ret = ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \
561 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))); \
562 } while (0)
563
564 /*
565 * Main square root routine. The input value should be cooked.
566 */
567
568 #define _FP_SQRT(fs, wc, R, X) \
569 do { \
570 _FP_FRAC_DECL_##wc(T); _FP_FRAC_DECL_##wc(S); \
571 _FP_W_TYPE q; \
572 switch (X##_c) \
573 { \
574 case FP_CLS_NAN: \
575 _FP_FRAC_COPY_##wc(R, X); \
576 R##_s = X##_s; \
577 R##_c = FP_CLS_NAN; \
578 break; \
579 case FP_CLS_INF: \
580 if (X##_s) \
581 { \
582 R##_s = _FP_NANSIGN_##fs; \
583 R##_c = FP_CLS_NAN; /* NAN */ \
584 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
585 FP_SET_EXCEPTION(FP_EX_INVALID); \
586 } \
587 else \
588 { \
589 R##_s = 0; \
590 R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \
591 } \
592 break; \
593 case FP_CLS_ZERO: \
594 R##_s = X##_s; \
595 R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \
596 break; \
597 case FP_CLS_NORMAL: \
598 R##_s = 0; \
599 if (X##_s) \
600 { \
601 R##_c = FP_CLS_NAN; /* sNAN */ \
602 R##_s = _FP_NANSIGN_##fs; \
603 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
604 FP_SET_EXCEPTION(FP_EX_INVALID); \
605 break; \
606 } \
607 R##_c = FP_CLS_NORMAL; \
608 if (X##_e & 1) \
609 _FP_FRAC_SLL_##wc(X, 1); \
610 R##_e = X##_e >> 1; \
611 _FP_FRAC_SET_##wc(S, _FP_ZEROFRAC_##wc); \
612 _FP_FRAC_SET_##wc(R, _FP_ZEROFRAC_##wc); \
613 q = _FP_OVERFLOW_##fs >> 1; \
614 _FP_SQRT_MEAT_##wc(R, S, T, X, q); \
615 } \
616 } while (0)
617
618 /*
619 * Convert from FP to integer
620 */
621
622 /* RSIGNED can have following values:
623 * 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus
624 * the result is either 0 or (2^rsize)-1 depending on the sign in such case.
625 * 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not, NV is
626 * set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1 depending
627 * on the sign in such case.
628 * -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
629 * set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1 depending
630 * on the sign in such case.
631 */
632 #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \
633 do { \
634 switch (X##_c) \
635 { \
636 case FP_CLS_NORMAL: \
637 if (X##_e < 0) \
638 { \
639 FP_SET_EXCEPTION(FP_EX_INEXACT); \
640 case FP_CLS_ZERO: \
641 r = 0; \
642 } \
643 else if (X##_e >= rsize - (rsigned > 0 || X##_s) \
644 || (!rsigned && X##_s)) \
645 { /* overflow */ \
646 case FP_CLS_NAN: \
647 case FP_CLS_INF: \
648 if (rsigned) \
649 { \
650 r = 1; \
651 r <<= rsize - 1; \
652 r -= 1 - X##_s; \
653 } else { \
654 r = 0; \
655 if (X##_s) \
656 r = ~r; \
657 } \
658 FP_SET_EXCEPTION(FP_EX_INVALID); \
659 } \
660 else \
661 { \
662 if (_FP_W_TYPE_SIZE*wc < rsize) \
663 { \
664 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \
665 r <<= X##_e - _FP_WFRACBITS_##fs; \
666 } \
667 else \
668 { \
669 if (X##_e >= _FP_WFRACBITS_##fs) \
670 _FP_FRAC_SLL_##wc(X, (X##_e - _FP_WFRACBITS_##fs + 1)); \
671 else if (X##_e < _FP_WFRACBITS_##fs - 1) \
672 { \
673 _FP_FRAC_SRS_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 2), \
674 _FP_WFRACBITS_##fs); \
675 if (_FP_FRAC_LOW_##wc(X) & 1) \
676 FP_SET_EXCEPTION(FP_EX_INEXACT); \
677 _FP_FRAC_SRL_##wc(X, 1); \
678 } \
679 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \
680 } \
681 if (rsigned && X##_s) \
682 r = -r; \
683 } \
684 break; \
685 } \
686 } while (0)
687
688 #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \
689 do { \
690 if (r) \
691 { \
692 unsigned rtype ur_; \
693 X##_c = FP_CLS_NORMAL; \
694 \
695 if ((X##_s = (r < 0))) \
696 r = -r; \
697 \
698 ur_ = (unsigned rtype) r; \
699 if (rsize <= _FP_W_TYPE_SIZE) \
700 __FP_CLZ(X##_e, ur_); \
701 else \
702 __FP_CLZ_2(X##_e, (_FP_W_TYPE)(ur_ >> _FP_W_TYPE_SIZE), \
703 (_FP_W_TYPE)ur_); \
704 if (rsize < _FP_W_TYPE_SIZE) \
705 X##_e -= (_FP_W_TYPE_SIZE - rsize); \
706 X##_e = rsize - X##_e - 1; \
707 \
708 if (_FP_FRACBITS_##fs < rsize && _FP_WFRACBITS_##fs < X##_e) \
709 __FP_FRAC_SRS_1(ur_, (X##_e - _FP_WFRACBITS_##fs + 1), rsize);\
710 _FP_FRAC_DISASSEMBLE_##wc(X, ur_, rsize); \
711 if ((_FP_WFRACBITS_##fs - X##_e - 1) > 0) \
712 _FP_FRAC_SLL_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 1)); \
713 } \
714 else \
715 { \
716 X##_c = FP_CLS_ZERO, X##_s = 0; \
717 } \
718 } while (0)
719
720
721 #define FP_CONV(dfs,sfs,dwc,swc,D,S) \
722 do { \
723 _FP_FRAC_CONV_##dwc##_##swc(dfs, sfs, D, S); \
724 D##_e = S##_e; \
725 D##_c = S##_c; \
726 D##_s = S##_s; \
727 } while (0)
728
729 /*
730 * Helper primitives.
731 */
732
733 /* Count leading zeros in a word. */
734
735 #ifndef __FP_CLZ
736 /* GCC 3.4 and later provide the builtins for us. */
737 #define __FP_CLZ(r, x) \
738 do { \
739 if (sizeof (_FP_W_TYPE) == sizeof (unsigned int)) \
740 r = __builtin_clz (x); \
741 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long)) \
742 r = __builtin_clzl (x); \
743 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long long)) \
744 r = __builtin_clzll (x); \
745 else \
746 abort (); \
747 } while (0)
748 #endif /* ndef __FP_CLZ */
749
750 #define _FP_DIV_HELP_imm(q, r, n, d) \
751 do { \
752 q = n / d, r = n % d; \
753 } while (0)
754
755
756 /* A restoring bit-by-bit division primitive. */
757
758 #define _FP_DIV_MEAT_N_loop(fs, wc, R, X, Y) \
759 do { \
760 int count = _FP_WFRACBITS_##fs; \
761 _FP_FRAC_DECL_##wc (u); \
762 _FP_FRAC_DECL_##wc (v); \
763 _FP_FRAC_COPY_##wc (u, X); \
764 _FP_FRAC_COPY_##wc (v, Y); \
765 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
766 /* Normalize U and V. */ \
767 _FP_FRAC_SLL_##wc (u, _FP_WFRACXBITS_##fs); \
768 _FP_FRAC_SLL_##wc (v, _FP_WFRACXBITS_##fs); \
769 /* First round. Since the operands are normalized, either the \
770 first or second bit will be set in the fraction. Produce a \
771 normalized result by checking which and adjusting the loop \
772 count and exponent accordingly. */ \
773 if (_FP_FRAC_GE_1 (u, v)) \
774 { \
775 _FP_FRAC_SUB_##wc (u, u, v); \
776 _FP_FRAC_LOW_##wc (R) |= 1; \
777 count--; \
778 } \
779 else \
780 R##_e--; \
781 /* Subsequent rounds. */ \
782 do { \
783 int msb = (_FP_WS_TYPE) _FP_FRAC_HIGH_##wc (u) < 0; \
784 _FP_FRAC_SLL_##wc (u, 1); \
785 _FP_FRAC_SLL_##wc (R, 1); \
786 if (msb || _FP_FRAC_GE_1 (u, v)) \
787 { \
788 _FP_FRAC_SUB_##wc (u, u, v); \
789 _FP_FRAC_LOW_##wc (R) |= 1; \
790 } \
791 } while (--count > 0); \
792 /* If there's anything left in U, the result is inexact. */ \
793 _FP_FRAC_LOW_##wc (R) |= !_FP_FRAC_ZEROP_##wc (u); \
794 } while (0)
795
796 #define _FP_DIV_MEAT_1_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 1, R, X, Y)
797 #define _FP_DIV_MEAT_2_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 2, R, X, Y)
798 #define _FP_DIV_MEAT_4_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 4, R, X, Y)