Import 2.3.4pre3
[davej-history.git] / arch / sparc64 / math-emu / op-common.h
blob529f0e4b2d17c1d72e7c55bffe5aae788f24bc35
1 /* Software floating-point emulation. Common operations.
2 Copyright (C) 1997,1998,1999 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).
9 The GNU C Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
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 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with the GNU C Library; see the file COPYING.LIB. If
21 not, write to the Free Software Foundation, Inc.,
22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #define _FP_DECL(wc, X) \
25 _FP_I_TYPE X##_c, X##_s, X##_e; \
26 _FP_FRAC_DECL_##wc(X)
29 * Finish truely unpacking a native fp value by classifying the kind
30 * of fp value and normalizing both the exponent and the fraction.
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; \
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; \
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)
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.
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_SRL_##wc(X, (_FP_WORKBITS+1)); \
93 X##_e++; \
94 } \
95 else \
96 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \
97 if (X##_e >= _FP_EXPMAX_##fs) \
98 { \
99 /* overflow */ \
100 switch (FP_ROUNDMODE) \
102 case FP_RND_NEAREST: \
103 X##_c = FP_CLS_INF; \
104 break; \
105 case FP_RND_PINF: \
106 if (!X##_s) X##_c = FP_CLS_INF; \
107 break; \
108 case FP_RND_MINF: \
109 if (X##_s) X##_c = FP_CLS_INF; \
110 break; \
112 if (X##_c == FP_CLS_INF) \
114 /* Overflow to infinity */ \
115 X##_e = _FP_EXPMAX_##fs; \
116 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
118 else \
120 /* Overflow to maximum normal */ \
121 X##_e = _FP_EXPMAX_##fs - 1; \
122 _FP_FRAC_SET_##wc(X, _FP_MAXFRAC_##wc); \
124 FP_SET_EXCEPTION(FP_EX_OVERFLOW); \
125 FP_SET_EXCEPTION(FP_EX_INEXACT); \
128 else \
130 /* we've got a denormalized number */ \
131 X##_e = -X##_e + 1; \
132 if (X##_e <= _FP_WFRACBITS_##fs) \
134 _FP_FRAC_SRS_##wc(X, X##_e, _FP_WFRACBITS_##fs); \
135 _FP_ROUND(wc, X); \
136 if (_FP_FRAC_HIGH_##fs(X) \
137 & (_FP_OVERFLOW_##fs >> 1)) \
139 X##_e = 1; \
140 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
142 else \
144 X##_e = 0; \
145 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \
146 FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \
149 else \
151 /* underflow to zero */ \
152 X##_e = 0; \
153 if (!_FP_FRAC_ZEROP_##wc(X)) \
155 _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \
156 _FP_ROUND(wc, X); \
157 _FP_FRAC_LOW_##wc(X) >>= (_FP_WORKBITS); \
159 FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \
162 break; \
164 case FP_CLS_ZERO: \
165 X##_e = 0; \
166 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
167 break; \
169 case FP_CLS_INF: \
170 X##_e = _FP_EXPMAX_##fs; \
171 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
172 break; \
174 case FP_CLS_NAN: \
175 X##_e = _FP_EXPMAX_##fs; \
176 if (!_FP_KEEPNANFRACP) \
178 _FP_FRAC_SET_##wc(X, _FP_NANFRAC_##fs); \
179 X##_s = _FP_NANSIGN_##fs; \
181 else \
182 _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_QNANBIT_##fs; \
183 break; \
185 } while (0)
187 /* This one accepts raw argument and not cooked, returns
188 * 1 if X is a signaling NaN.
190 #define _FP_ISSIGNAN(fs, wc, X) \
191 ({ \
192 int __ret = 0; \
193 if (X##_e == _FP_EXPMAX_##fs) \
195 if (!_FP_FRAC_ZEROP_##wc(X) \
196 && !(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs)) \
197 __ret = 1; \
199 __ret; \
207 * Main addition routine. The input values should be cooked.
210 #define _FP_ADD(fs, wc, R, X, Y) \
211 do { \
212 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \
214 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \
216 /* shift the smaller number so that its exponent matches the larger */ \
217 _FP_I_TYPE diff = X##_e - Y##_e; \
219 if (diff < 0) \
221 diff = -diff; \
222 if (diff <= _FP_WFRACBITS_##fs) \
223 _FP_FRAC_SRS_##wc(X, diff, _FP_WFRACBITS_##fs); \
224 else if (!_FP_FRAC_ZEROP_##wc(X)) \
225 _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \
226 R##_e = Y##_e; \
228 else \
230 if (diff > 0) \
232 if (diff <= _FP_WFRACBITS_##fs) \
233 _FP_FRAC_SRS_##wc(Y, diff, _FP_WFRACBITS_##fs); \
234 else if (!_FP_FRAC_ZEROP_##wc(Y)) \
235 _FP_FRAC_SET_##wc(Y, _FP_MINFRAC_##wc); \
237 R##_e = X##_e; \
240 R##_c = FP_CLS_NORMAL; \
242 if (X##_s == Y##_s) \
244 R##_s = X##_s; \
245 _FP_FRAC_ADD_##wc(R, X, Y); \
246 if (_FP_FRAC_OVERP_##wc(fs, R)) \
248 _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs); \
249 R##_e++; \
252 else \
254 R##_s = X##_s; \
255 _FP_FRAC_SUB_##wc(R, X, Y); \
256 if (_FP_FRAC_ZEROP_##wc(R)) \
258 /* return an exact zero */ \
259 if (FP_ROUNDMODE == FP_RND_MINF) \
260 R##_s |= Y##_s; \
261 else \
262 R##_s &= Y##_s; \
263 R##_c = FP_CLS_ZERO; \
265 else \
267 if (_FP_FRAC_NEGP_##wc(R)) \
269 _FP_FRAC_SUB_##wc(R, Y, X); \
270 R##_s = Y##_s; \
273 /* renormalize after subtraction */ \
274 _FP_FRAC_CLZ_##wc(diff, R); \
275 diff -= _FP_WFRACXBITS_##fs; \
276 if (diff) \
278 R##_e -= diff; \
279 _FP_FRAC_SLL_##wc(R, diff); \
283 break; \
286 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \
287 _FP_CHOOSENAN(fs, wc, R, X, Y); \
288 break; \
290 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \
291 R##_e = X##_e; \
292 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \
293 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \
294 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \
295 _FP_FRAC_COPY_##wc(R, X); \
296 R##_s = X##_s; \
297 R##_c = X##_c; \
298 break; \
300 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \
301 R##_e = Y##_e; \
302 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \
303 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \
304 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \
305 _FP_FRAC_COPY_##wc(R, Y); \
306 R##_s = Y##_s; \
307 R##_c = Y##_c; \
308 break; \
310 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \
311 if (X##_s != Y##_s) \
313 /* +INF + -INF => NAN */ \
314 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
315 R##_s = _FP_NANSIGN_##fs; \
316 R##_c = FP_CLS_NAN; \
317 FP_SET_EXCEPTION(FP_EX_INVALID); \
318 break; \
320 /* FALLTHRU */ \
322 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \
323 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \
324 R##_s = X##_s; \
325 R##_c = FP_CLS_INF; \
326 break; \
328 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \
329 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \
330 R##_s = Y##_s; \
331 R##_c = FP_CLS_INF; \
332 break; \
334 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \
335 /* make sure the sign is correct */ \
336 if (FP_ROUNDMODE == FP_RND_MINF) \
337 R##_s = X##_s | Y##_s; \
338 else \
339 R##_s = X##_s & Y##_s; \
340 R##_c = FP_CLS_ZERO; \
341 break; \
343 default: \
344 abort(); \
346 } while (0)
350 * Main negation routine. FIXME -- when we care about setting exception
351 * bits reliably, this will not do. We should examine all of the fp classes.
354 #define _FP_NEG(fs, wc, R, X) \
355 do { \
356 _FP_FRAC_COPY_##wc(R, X); \
357 R##_c = X##_c; \
358 R##_e = X##_e; \
359 R##_s = 1 ^ X##_s; \
360 } while (0)
364 * Main multiplication routine. The input values should be cooked.
367 #define _FP_MUL(fs, wc, R, X, Y) \
368 do { \
369 R##_s = X##_s ^ Y##_s; \
370 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \
372 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \
373 R##_c = FP_CLS_NORMAL; \
374 R##_e = X##_e + Y##_e + 1; \
376 _FP_MUL_MEAT_##fs(R,X,Y); \
378 if (_FP_FRAC_OVERP_##wc(fs, R)) \
379 _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs); \
380 else \
381 R##_e--; \
382 break; \
384 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \
385 _FP_CHOOSENAN(fs, wc, R, X, Y); \
386 break; \
388 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \
389 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \
390 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \
391 R##_s = X##_s; \
393 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \
394 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \
395 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \
396 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \
397 _FP_FRAC_COPY_##wc(R, X); \
398 R##_c = X##_c; \
399 break; \
401 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \
402 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \
403 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \
404 R##_s = Y##_s; \
406 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \
407 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \
408 _FP_FRAC_COPY_##wc(R, Y); \
409 R##_c = Y##_c; \
410 break; \
412 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \
413 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \
414 R##_s = _FP_NANSIGN_##fs; \
415 R##_c = FP_CLS_NAN; \
416 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
417 FP_SET_EXCEPTION(FP_EX_INVALID); \
418 break; \
420 default: \
421 abort(); \
423 } while (0)
427 * Main division routine. The input values should be cooked.
430 #define _FP_DIV(fs, wc, R, X, Y) \
431 do { \
432 R##_s = X##_s ^ Y##_s; \
433 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \
435 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \
436 R##_c = FP_CLS_NORMAL; \
437 R##_e = X##_e - Y##_e; \
439 _FP_DIV_MEAT_##fs(R,X,Y); \
440 break; \
442 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \
443 _FP_CHOOSENAN(fs, wc, R, X, Y); \
444 break; \
446 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \
447 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \
448 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \
449 R##_s = X##_s; \
450 _FP_FRAC_COPY_##wc(R, X); \
451 R##_c = X##_c; \
452 break; \
454 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \
455 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \
456 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \
457 R##_s = Y##_s; \
458 _FP_FRAC_COPY_##wc(R, Y); \
459 R##_c = Y##_c; \
460 break; \
462 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \
463 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \
464 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \
465 R##_c = FP_CLS_ZERO; \
466 break; \
468 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \
469 FP_SET_EXCEPTION(FP_EX_DIVZERO); \
470 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \
471 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \
472 R##_c = FP_CLS_INF; \
473 break; \
475 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \
476 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \
477 R##_s = _FP_NANSIGN_##fs; \
478 R##_c = FP_CLS_NAN; \
479 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
480 FP_SET_EXCEPTION(FP_EX_INVALID); \
481 break; \
483 default: \
484 abort(); \
486 } while (0)
490 * Main differential comparison routine. The inputs should be raw not
491 * cooked. The return is -1,0,1 for normal values, 2 otherwise.
494 #define _FP_CMP(fs, wc, ret, X, Y, un) \
495 do { \
496 /* NANs are unordered */ \
497 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \
498 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))) \
500 ret = un; \
502 else \
504 int __is_zero_x; \
505 int __is_zero_y; \
507 __is_zero_x = (!X##_e && _FP_FRAC_ZEROP_##wc(X)) ? 1 : 0; \
508 __is_zero_y = (!Y##_e && _FP_FRAC_ZEROP_##wc(Y)) ? 1 : 0; \
510 if (__is_zero_x && __is_zero_y) \
511 ret = 0; \
512 else if (__is_zero_x) \
513 ret = Y##_s ? 1 : -1; \
514 else if (__is_zero_y) \
515 ret = X##_s ? -1 : 1; \
516 else if (X##_s != Y##_s) \
517 ret = X##_s ? -1 : 1; \
518 else if (X##_e > Y##_e) \
519 ret = X##_s ? -1 : 1; \
520 else if (X##_e < Y##_e) \
521 ret = X##_s ? 1 : -1; \
522 else if (_FP_FRAC_GT_##wc(X, Y)) \
523 ret = X##_s ? -1 : 1; \
524 else if (_FP_FRAC_GT_##wc(Y, X)) \
525 ret = X##_s ? 1 : -1; \
526 else \
527 ret = 0; \
529 } while (0)
532 /* Simplification for strict equality. */
534 #define _FP_CMP_EQ(fs, wc, ret, X, Y) \
535 do { \
536 /* NANs are unordered */ \
537 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \
538 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))) \
540 ret = 1; \
542 else \
544 ret = !(X##_e == Y##_e \
545 && _FP_FRAC_EQ_##wc(X, Y) \
546 && (X##_s == Y##_s || !X##_e && _FP_FRAC_ZEROP_##wc(X))); \
548 } while (0)
551 * Main square root routine. The input value should be cooked.
554 #define _FP_SQRT(fs, wc, R, X) \
555 do { \
556 _FP_FRAC_DECL_##wc(T); _FP_FRAC_DECL_##wc(S); \
557 _FP_W_TYPE q; \
558 switch (X##_c) \
560 case FP_CLS_NAN: \
561 _FP_FRAC_COPY_##wc(R, X); \
562 R##_s = X##_s; \
563 R##_c = FP_CLS_NAN; \
564 break; \
565 case FP_CLS_INF: \
566 if (X##_s) \
568 R##_s = _FP_NANSIGN_##fs; \
569 R##_c = FP_CLS_NAN; /* NAN */ \
570 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
571 FP_SET_EXCEPTION(FP_EX_INVALID); \
573 else \
575 R##_s = 0; \
576 R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \
578 break; \
579 case FP_CLS_ZERO: \
580 R##_s = X##_s; \
581 R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \
582 break; \
583 case FP_CLS_NORMAL: \
584 R##_s = 0; \
585 if (X##_s) \
587 R##_c = FP_CLS_NAN; /* sNAN */ \
588 R##_s = _FP_NANSIGN_##fs; \
589 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \
590 FP_SET_EXCEPTION(FP_EX_INVALID); \
591 break; \
593 R##_c = FP_CLS_NORMAL; \
594 if (X##_e & 1) \
595 _FP_FRAC_SLL_##wc(X, 1); \
596 R##_e = X##_e >> 1; \
597 _FP_FRAC_SET_##wc(S, _FP_ZEROFRAC_##wc); \
598 _FP_FRAC_SET_##wc(R, _FP_ZEROFRAC_##wc); \
599 q = _FP_OVERFLOW_##fs >> 1; \
600 _FP_SQRT_MEAT_##wc(R, S, T, X, q); \
602 } while (0)
605 * Convert from FP to integer
608 /* RSIGNED can have following values:
609 * 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus
610 * the result is either 0 or (2^rsize)-1 depending on the sign in such case.
611 * 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not, NV is
612 * set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1 depending
613 * on the sign in such case.
614 * -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
615 * set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1 depending
616 * on the sign in such case.
618 #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \
619 do { \
620 switch (X##_c) \
622 case FP_CLS_NORMAL: \
623 if (X##_e < 0) \
625 FP_SET_EXCEPTION(FP_EX_INEXACT); \
626 case FP_CLS_ZERO: \
627 r = 0; \
629 else if (X##_e >= rsize - (rsigned > 0 || X##_s) \
630 || (!rsigned && X##_s)) \
631 { /* overflow */ \
632 case FP_CLS_NAN: \
633 case FP_CLS_INF: \
634 if (rsigned) \
636 r = 1; \
637 r <<= rsize - 1; \
638 r -= 1 - X##_s; \
639 } else { \
640 r = 0; \
641 if (X##_s) \
642 r = ~r; \
644 FP_SET_EXCEPTION(FP_EX_INVALID); \
646 else \
648 if (_FP_W_TYPE_SIZE*wc < rsize) \
650 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \
651 r <<= X##_e - _FP_WFRACBITS_##fs; \
653 else \
655 if (X##_e >= _FP_WFRACBITS_##fs) \
656 _FP_FRAC_SLL_##wc(X, (X##_e - _FP_WFRACBITS_##fs + 1)); \
657 else if (X##_e < _FP_WFRACBITS_##fs - 1) \
659 _FP_FRAC_SRS_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 2), \
660 _FP_WFRACBITS_##fs); \
661 if (_FP_FRAC_LOW_##wc(X) & 1) \
662 FP_SET_EXCEPTION(FP_EX_INEXACT); \
663 _FP_FRAC_SRL_##wc(X, 1); \
665 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \
667 if (rsigned && X##_s) \
668 r = -r; \
670 break; \
672 } while (0)
674 #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \
675 do { \
676 if (r) \
678 X##_c = FP_CLS_NORMAL; \
680 if ((X##_s = (r < 0))) \
681 r = -r; \
683 if (rsize <= _FP_W_TYPE_SIZE) \
684 __FP_CLZ(X##_e, r); \
685 else \
686 __FP_CLZ_2(X##_e, (_FP_W_TYPE)(r >> _FP_W_TYPE_SIZE), \
687 (_FP_W_TYPE)r); \
688 if (rsize < _FP_W_TYPE_SIZE) \
689 X##_e -= (_FP_W_TYPE_SIZE - rsize); \
690 X##_e = rsize - X##_e - 1; \
692 if (_FP_FRACBITS_##fs < rsize && _FP_WFRACBITS_##fs < X##_e) \
693 __FP_FRAC_SRS_1(r, (X##_e - _FP_WFRACBITS_##fs), rsize); \
694 r &= ~((rtype)1 << X##_e); \
695 _FP_FRAC_DISASSEMBLE_##wc(X, ((unsigned rtype)r), rsize); \
696 _FP_FRAC_SLL_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 1)); \
698 else \
700 X##_c = FP_CLS_ZERO, X##_s = 0; \
702 } while (0)
705 #define FP_CONV(dfs,sfs,dwc,swc,D,S) \
706 do { \
707 _FP_FRAC_CONV_##dwc##_##swc(dfs, sfs, D, S); \
708 D##_e = S##_e; \
709 D##_c = S##_c; \
710 D##_s = S##_s; \
711 } while (0)
714 * Helper primitives.
717 /* Count leading zeros in a word. */
719 #ifndef __FP_CLZ
720 #if _FP_W_TYPE_SIZE < 64
721 /* this is just to shut the compiler up about shifts > word length -- PMM 02/1998 */
722 #define __FP_CLZ(r, x) \
723 do { \
724 _FP_W_TYPE _t = (x); \
725 r = _FP_W_TYPE_SIZE - 1; \
726 if (_t > 0xffff) r -= 16; \
727 if (_t > 0xffff) _t >>= 16; \
728 if (_t > 0xff) r -= 8; \
729 if (_t > 0xff) _t >>= 8; \
730 if (_t & 0xf0) r -= 4; \
731 if (_t & 0xf0) _t >>= 4; \
732 if (_t & 0xc) r -= 2; \
733 if (_t & 0xc) _t >>= 2; \
734 if (_t & 0x2) r -= 1; \
735 } while (0)
736 #else /* not _FP_W_TYPE_SIZE < 64 */
737 #define __FP_CLZ(r, x) \
738 do { \
739 _FP_W_TYPE _t = (x); \
740 r = _FP_W_TYPE_SIZE - 1; \
741 if (_t > 0xffffffff) r -= 32; \
742 if (_t > 0xffffffff) _t >>= 32; \
743 if (_t > 0xffff) r -= 16; \
744 if (_t > 0xffff) _t >>= 16; \
745 if (_t > 0xff) r -= 8; \
746 if (_t > 0xff) _t >>= 8; \
747 if (_t & 0xf0) r -= 4; \
748 if (_t & 0xf0) _t >>= 4; \
749 if (_t & 0xc) r -= 2; \
750 if (_t & 0xc) _t >>= 2; \
751 if (_t & 0x2) r -= 1; \
752 } while (0)
753 #endif /* not _FP_W_TYPE_SIZE < 64 */
754 #endif /* ndef __FP_CLZ */
756 #define _FP_DIV_HELP_imm(q, r, n, d) \
757 do { \
758 q = n / d, r = n % d; \
759 } while (0)