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