1#define _FP_DECL(wc, X)			\
2  _FP_I_TYPE X##_c, X##_s, X##_e;	\
3  _FP_FRAC_DECL_##wc(X)
4
5/*
6 * Finish truely unpacking a native fp value by classifying the kind
7 * of fp value and normalizing both the exponent and the fraction.
8 */
9
10#define _FP_UNPACK_CANONICAL(fs, wc, X)					\
11do {									\
12  switch (X##_e)							\
13  {									\
14  default:								\
15    _FP_FRAC_HIGH_##wc(X) |= _FP_IMPLBIT_##fs;				\
16    _FP_FRAC_SLL_##wc(X, _FP_WORKBITS);					\
17    X##_e -= _FP_EXPBIAS_##fs;						\
18    X##_c = FP_CLS_NORMAL;						\
19    break;								\
20									\
21  case 0:								\
22    if (_FP_FRAC_ZEROP_##wc(X))						\
23      X##_c = FP_CLS_ZERO;						\
24    else								\
25      {									\
26	/* a denormalized number */					\
27	_FP_I_TYPE _shift;						\
28	_FP_FRAC_CLZ_##wc(_shift, X);					\
29	_shift -= _FP_FRACXBITS_##fs;					\
30	_FP_FRAC_SLL_##wc(X, (_shift+_FP_WORKBITS));			\
31	X##_e -= _FP_EXPBIAS_##fs - 1 + _shift;				\
32	X##_c = FP_CLS_NORMAL;						\
33      }									\
34    break;								\
35									\
36  case _FP_EXPMAX_##fs:							\
37    if (_FP_FRAC_ZEROP_##wc(X))						\
38      X##_c = FP_CLS_INF;						\
39    else								\
40      /* we don't differentiate between signaling and quiet nans */	\
41      X##_c = FP_CLS_NAN;						\
42    break;								\
43  }									\
44} while (0)
45
46
47/*
48 * Before packing the bits back into the native fp result, take care
49 * of such mundane things as rounding and overflow.  Also, for some
50 * kinds of fp values, the original parts may not have been fully
51 * extracted -- but that is ok, we can regenerate them now.
52 */
53
54#define _FP_PACK_CANONICAL(fs, wc, X)				\
55({int __ret = 0;						\
56  switch (X##_c)						\
57  {								\
58  case FP_CLS_NORMAL:						\
59    X##_e += _FP_EXPBIAS_##fs;					\
60    if (X##_e > 0)						\
61      {								\
62	__ret |= _FP_ROUND(wc, X);				\
63	if (_FP_FRAC_OVERP_##wc(fs, X))				\
64	  {							\
65	    _FP_FRAC_SRL_##wc(X, (_FP_WORKBITS+1));		\
66	    X##_e++;						\
67	  }							\
68	else							\
69	  _FP_FRAC_SRL_##wc(X, _FP_WORKBITS);			\
70	if (X##_e >= _FP_EXPMAX_##fs)				\
71	  {							\
72	    /* overflow to infinity */				\
73	    X##_e = _FP_EXPMAX_##fs;				\
74	    _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);		\
75            __ret |= EFLAG_OVERFLOW;				\
76	  }							\
77      }								\
78    else							\
79      {								\
80	/* we've got a denormalized number */			\
81	X##_e = -X##_e + 1;					\
82	if (X##_e <= _FP_WFRACBITS_##fs)			\
83	  {							\
84	    _FP_FRAC_SRS_##wc(X, X##_e, _FP_WFRACBITS_##fs);	\
85	    _FP_FRAC_SLL_##wc(X, 1);				\
86	    if (_FP_FRAC_OVERP_##wc(fs, X))			\
87	      {							\
88	        X##_e = 1;					\
89	        _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);	\
90	      }							\
91	    else						\
92	      {							\
93		X##_e = 0;					\
94		_FP_FRAC_SRL_##wc(X, _FP_WORKBITS+1);		\
95                __ret |= EFLAG_UNDERFLOW;			\
96	      }							\
97	  }							\
98	else							\
99	  {							\
100	    /* underflow to zero */				\
101	    X##_e = 0;						\
102	    _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);		\
103            __ret |= EFLAG_UNDERFLOW;				\
104	  }							\
105      }								\
106    break;							\
107								\
108  case FP_CLS_ZERO:						\
109    X##_e = 0;							\
110    _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);			\
111    break;							\
112								\
113  case FP_CLS_INF:						\
114    X##_e = _FP_EXPMAX_##fs;					\
115    _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);			\
116    break;							\
117								\
118  case FP_CLS_NAN:						\
119    X##_e = _FP_EXPMAX_##fs;					\
120    if (!_FP_KEEPNANFRACP)					\
121      {								\
122	_FP_FRAC_SET_##wc(X, _FP_NANFRAC_##fs);			\
123	X##_s = 0;						\
124      }								\
125    else							\
126      _FP_FRAC_HIGH_##wc(X) |= _FP_QNANBIT_##fs;		\
127    break;							\
128  }								\
129  __ret;							\
130})
131
132
133/*
134 * Main addition routine.  The input values should be cooked.
135 */
136
137#define _FP_ADD(fs, wc, R, X, Y)					     \
138do {									     \
139  switch (_FP_CLS_COMBINE(X##_c, Y##_c))				     \
140  {									     \
141  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL):			     \
142    {									     \
143      /* shift the smaller number so that its exponent matches the larger */ \
144      _FP_I_TYPE diff = X##_e - Y##_e;					     \
145									     \
146      if (diff < 0)							     \
147	{								     \
148	  diff = -diff;							     \
149	  if (diff <= _FP_WFRACBITS_##fs)				     \
150	    _FP_FRAC_SRS_##wc(X, diff, _FP_WFRACBITS_##fs);		     \
151	  else if (!_FP_FRAC_ZEROP_##wc(X))				     \
152	    _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc);			     \
153	  else								     \
154	    _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);			     \
155	  R##_e = Y##_e;						     \
156	}								     \
157      else								     \
158	{								     \
159	  if (diff > 0)							     \
160	    {								     \
161	      if (diff <= _FP_WFRACBITS_##fs)				     \
162	        _FP_FRAC_SRS_##wc(Y, diff, _FP_WFRACBITS_##fs);		     \
163	      else if (!_FP_FRAC_ZEROP_##wc(Y))				     \
164	        _FP_FRAC_SET_##wc(Y, _FP_MINFRAC_##wc);			     \
165	      else							     \
166	        _FP_FRAC_SET_##wc(Y, _FP_ZEROFRAC_##wc);		     \
167	    }								     \
168	  R##_e = X##_e;						     \
169	}								     \
170									     \
171      R##_c = FP_CLS_NORMAL;						     \
172									     \
173      if (X##_s == Y##_s)						     \
174	{								     \
175	  R##_s = X##_s;						     \
176	  _FP_FRAC_ADD_##wc(R, X, Y);					     \
177	  if (_FP_FRAC_OVERP_##wc(fs, R))				     \
178	    {								     \
179	      _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs);		     \
180	      R##_e++;							     \
181	    }								     \
182	}								     \
183      else								     \
184	{								     \
185	  R##_s = X##_s;						     \
186	  _FP_FRAC_SUB_##wc(R, X, Y);					     \
187	  if (_FP_FRAC_ZEROP_##wc(R))					     \
188	    {								     \
189	      /* return an exact zero */				     \
190	      if (FP_ROUNDMODE == FP_RND_MINF)				     \
191		R##_s |= Y##_s;						     \
192	      else							     \
193		R##_s &= Y##_s;						     \
194	      R##_c = FP_CLS_ZERO;					     \
195	    }								     \
196	  else								     \
197	    {								     \
198	      if (_FP_FRAC_NEGP_##wc(R))				     \
199		{							     \
200		  _FP_FRAC_SUB_##wc(R, Y, X);				     \
201		  R##_s = Y##_s;					     \
202		}							     \
203									     \
204	      /* renormalize after subtraction */			     \
205	      _FP_FRAC_CLZ_##wc(diff, R);				     \
206	      diff -= _FP_WFRACXBITS_##fs;				     \
207	      if (diff)							     \
208		{							     \
209		  R##_e -= diff;					     \
210		  _FP_FRAC_SLL_##wc(R, diff);				     \
211		}							     \
212	    }								     \
213	}								     \
214      break;								     \
215    }									     \
216									     \
217  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN):				     \
218    _FP_CHOOSENAN(fs, wc, R, X, Y);					     \
219    break;								     \
220									     \
221  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO):			     \
222    R##_e = X##_e;							     \
223  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL):			     \
224  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF):				     \
225  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO):				     \
226    _FP_FRAC_COPY_##wc(R, X);						     \
227    R##_s = X##_s;							     \
228    R##_c = X##_c;							     \
229    break;								     \
230									     \
231  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL):			     \
232    R##_e = Y##_e;							     \
233  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN):			     \
234  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN):				     \
235  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN):				     \
236    _FP_FRAC_COPY_##wc(R, Y);						     \
237    R##_s = Y##_s;							     \
238    R##_c = Y##_c;							     \
239    break;								     \
240									     \
241  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF):				     \
242    if (X##_s != Y##_s)							     \
243      {									     \
244	/* +INF + -INF => NAN */					     \
245	_FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs);				     \
246	R##_s = X##_s ^ Y##_s;						     \
247	R##_c = FP_CLS_NAN;						     \
248	break;								     \
249      }									     \
250    /* FALLTHRU */							     \
251									     \
252  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL):			     \
253  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO):				     \
254    R##_s = X##_s;							     \
255    R##_c = FP_CLS_INF;							     \
256    break;								     \
257									     \
258  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF):			     \
259  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF):				     \
260    R##_s = Y##_s;							     \
261    R##_c = FP_CLS_INF;							     \
262    break;								     \
263									     \
264  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO):			     \
265    /* make sure the sign is correct */					     \
266    if (FP_ROUNDMODE == FP_RND_MINF)					     \
267      R##_s = X##_s | Y##_s;						     \
268    else								     \
269      R##_s = X##_s & Y##_s;						     \
270    R##_c = FP_CLS_ZERO;						     \
271    break;								     \
272									     \
273  default:								     \
274    abort();								     \
275  }									     \
276} while (0)
277
278
279
280#define _FP_NEG(fs, wc, R, X)		\
281  do {					\
282    _FP_FRAC_COPY_##wc(R, X);		\
283    R##_c = X##_c;			\
284    R##_e = X##_e;			\
285    R##_s = 1 ^ X##_s;			\
286  } while (0)
287
288
289/*
290 * Main multiplication routine.  The input values should be cooked.
291 */
292
293#define _FP_MUL(fs, wc, R, X, Y)			\
294do {							\
295  R##_s = X##_s ^ Y##_s;				\
296  switch (_FP_CLS_COMBINE(X##_c, Y##_c))		\
297  {							\
298  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL):	\
299    R##_c = FP_CLS_NORMAL;				\
300    R##_e = X##_e + Y##_e + 1;				\
301							\
302    _FP_MUL_MEAT_##fs(R,X,Y);				\
303							\
304    if (_FP_FRAC_OVERP_##wc(fs, R))			\
305      _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs);	\
306    else						\
307      R##_e--;						\
308    break;						\
309							\
310  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN):		\
311    _FP_CHOOSENAN(fs, wc, R, X, Y);			\
312    break;						\
313							\
314  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL):	\
315  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF):		\
316  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO):		\
317    R##_s = X##_s;					\
318							\
319  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF):		\
320  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL):	\
321  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL):	\
322  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO):	\
323    _FP_FRAC_COPY_##wc(R, X);				\
324    R##_c = X##_c;					\
325    break;						\
326							\
327  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN):	\
328  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN):		\
329  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN):		\
330    R##_s = Y##_s;					\
331							\
332  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF):	\
333  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO):	\
334    _FP_FRAC_COPY_##wc(R, Y);				\
335    R##_c = Y##_c;					\
336    break;						\
337							\
338  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO):		\
339  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF):		\
340    R##_c = FP_CLS_NAN;					\
341    _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs);		\
342    break;						\
343							\
344  default:						\
345    abort();						\
346  }							\
347} while (0)
348
349
350/*
351 * Main division routine.  The input values should be cooked.
352 */
353
354#define _FP_DIV(fs, wc, R, X, Y)			\
355do {							\
356  R##_s = X##_s ^ Y##_s;				\
357  switch (_FP_CLS_COMBINE(X##_c, Y##_c))		\
358  {							\
359  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL):	\
360    R##_c = FP_CLS_NORMAL;				\
361    R##_e = X##_e - Y##_e;				\
362							\
363    _FP_DIV_MEAT_##fs(R,X,Y);				\
364    break;						\
365							\
366  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN):		\
367    _FP_CHOOSENAN(fs, wc, R, X, Y);			\
368    break;						\
369							\
370  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL):	\
371  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF):		\
372  case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO):		\
373    R##_s = X##_s;					\
374    _FP_FRAC_COPY_##wc(R, X);				\
375    R##_c = X##_c;					\
376    break;						\
377							\
378  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN):	\
379  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN):		\
380  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN):		\
381    R##_s = Y##_s;					\
382    _FP_FRAC_COPY_##wc(R, Y);				\
383    R##_c = Y##_c;					\
384    break;						\
385							\
386  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF):	\
387  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF):		\
388  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL):	\
389    R##_c = FP_CLS_ZERO;				\
390    break;						\
391							\
392  case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO):	\
393  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO):		\
394  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL):	\
395    R##_c = FP_CLS_INF;					\
396    break;						\
397							\
398  case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF):		\
399  case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO):	\
400    R##_c = FP_CLS_NAN;					\
401    _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs);		\
402    break;						\
403							\
404  default:						\
405    abort();						\
406  }							\
407} while (0)
408
409
410/*
411 * Main differential comparison routine.  The inputs should be raw not
412 * cooked.  The return is -1,0,1 for normal values, 2 otherwise.
413 */
414
415#define _FP_CMP(fs, wc, ret, X, Y, un)					\
416  do {									\
417    /* NANs are unordered */						\
418    if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X))		\
419	|| (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y)))	\
420      {									\
421	ret = un;							\
422      }									\
423    else								\
424      {									\
425        int __x_zero = (!X##_e && _FP_FRAC_ZEROP_##wc(X)) ? 1 : 0;	\
426        int __y_zero = (!Y##_e && _FP_FRAC_ZEROP_##wc(Y)) ? 1 : 0;	\
427									\
428	if (__x_zero && __y_zero)					\
429	  ret = 0;							\
430	else if (__x_zero)						\
431	  ret = Y##_s ? 1 : -1;						\
432	else if (__y_zero)						\
433	  ret = X##_s ? -1 : 1;						\
434	else if (X##_s != Y##_s)					\
435	  ret = X##_s ? -1 : 1;						\
436	else if (X##_e > Y##_e)						\
437	  ret = X##_s ? -1 : 1;						\
438	else if (X##_e < Y##_e)						\
439	  ret = X##_s ? 1 : -1;						\
440	else if (_FP_FRAC_GT_##wc(X, Y))				\
441	  ret = X##_s ? -1 : 1;						\
442	else if (_FP_FRAC_GT_##wc(Y, X))				\
443	  ret = X##_s ? 1 : -1;						\
444	else								\
445	  ret = 0;							\
446      }									\
447  } while (0)
448
449
450/* Simplification for strict equality.  */
451
452#define _FP_CMP_EQ(fs, wc, ret, X, Y)					  \
453  do {									  \
454    /* NANs are unordered */						  \
455    if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X))		  \
456	|| (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y)))	  \
457      {									  \
458	ret = 1;							  \
459      }									  \
460    else								  \
461      {									  \
462	ret = !(X##_e == Y##_e						  \
463		&& _FP_FRAC_EQ_##wc(X, Y)				  \
464		&& (X##_s == Y##_s || !X##_e && _FP_FRAC_ZEROP_##wc(X))); \
465      }									  \
466  } while (0)
467
468/*
469 * Main square root routine.  The input value should be cooked.
470 */
471
472#define _FP_SQRT(fs, wc, R, X)						\
473do {									\
474    _FP_FRAC_DECL_##wc(T); _FP_FRAC_DECL_##wc(S);			\
475    _FP_W_TYPE q;							\
476    switch (X##_c)							\
477    {									\
478    case FP_CLS_NAN:							\
479    	R##_s = 0;							\
480    	R##_c = FP_CLS_NAN;						\
481    	_FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc);			\
482    	break;								\
483    case FP_CLS_INF:							\
484    	if (X##_s)							\
485    	  {								\
486    	    R##_s = 0;							\
487	    R##_c = FP_CLS_NAN; /* sNAN */				\
488    	  }								\
489    	else								\
490    	  {								\
491    	    R##_s = 0;							\
492    	    R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */			\
493    	  }								\
494    	break;								\
495    case FP_CLS_ZERO:							\
496	R##_s = X##_s;							\
497    	R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */			\
498	break;								\
499    case FP_CLS_NORMAL:							\
500    	R##_s = 0;							\
501        if (X##_s)							\
502          {								\
503	    R##_c = FP_CLS_NAN; /* sNAN */				\
504	    break;							\
505          }								\
506    	R##_c = FP_CLS_NORMAL;						\
507        if (X##_e & 1)							\
508          _FP_FRAC_SLL_##wc(X, 1);					\
509        R##_e = X##_e >> 1;						\
510        _FP_FRAC_SET_##wc(S, _FP_ZEROFRAC_##wc);			\
511        _FP_FRAC_SET_##wc(R, _FP_ZEROFRAC_##wc);			\
512        q = _FP_OVERFLOW_##fs;						\
513        _FP_FRAC_SLL_##wc(X, 1);					\
514        _FP_SQRT_MEAT_##wc(R, S, T, X, q);				\
515        _FP_FRAC_SRL_##wc(R, 1);					\
516    }									\
517  } while (0)
518
519/*
520 * Convert from FP to integer
521 */
522
523/* "When a NaN, infinity, large positive argument >= 2147483648.0, or
524 * large negative argument <= -2147483649.0 is converted to an integer,
525 * the invalid_current bit...should be set and fp_exception_IEEE_754 should
526 * be raised. If the floating point invalid trap is disabled, no trap occurs
527 * and a numerical result is generated: if the sign bit of the operand
528 * is 0, the result is 2147483647; if the sign bit of the operand is 1,
529 * the result is -2147483648."
530 * Similarly for conversion to extended ints, except that the boundaries
531 * are >= 2^63, <= -(2^63 + 1), and the results are 2^63 + 1 for s=0 and
532 * -2^63 for s=1.
533 * -- SPARC Architecture Manual V9, Appendix B, which specifies how
534 * SPARCs resolve implementation dependencies in the IEEE-754 spec.
535 * I don't believe that the code below follows this. I'm not even sure
536 * it's right!
537 * It doesn't cope with needing to convert to an n bit integer when there
538 * is no n bit integer type. Fortunately gcc provides long long so this
539 * isn't a problem for sparc32.
540 * I have, however, fixed its NaN handling to conform as above.
541 *         -- PMM 02/1998
542 * NB: rsigned is not 'is r declared signed?' but 'should the value stored
543 * in r be signed or unsigned?'. r is always(?) declared unsigned.
544 * Comments below are mine, BTW -- PMM
545 */
546#define _FP_TO_INT(fs, wc, r, X, rsize, rsigned)			\
547  do {									\
548    switch (X##_c)							\
549      {									\
550      case FP_CLS_NORMAL:						\
551	if (X##_e < 0)							\
552	  {								\
553	  /* case FP_CLS_NAN: see above! */				\
554	  case FP_CLS_ZERO:						\
555	    r = 0;							\
556	  }								\
557	else if (X##_e >= rsize - (rsigned != 0))			\
558	  {	/* overflow */						\
559	  case FP_CLS_NAN:                                              \
560          case FP_CLS_INF:						\
561	    if (rsigned)						\
562	      {								\
563		r = 1;							\
564		r <<= rsize - 1;					\
565		r -= 1 - X##_s;						\
566	      }								\
567	    else							\
568	      {								\
569		r = 0;							\
570		if (!X##_s)						\
571		  r = ~r;						\
572	      }								\
573	  }								\
574	else								\
575	  {								\
576	    if (_FP_W_TYPE_SIZE*wc < rsize)				\
577	      {								\
578		_FP_FRAC_ASSEMBLE_##wc(r, X, rsize);			\
579		r <<= X##_e - _FP_WFRACBITS_##fs;			\
580	      }								\
581	    else							\
582	      {								\
583		if (X##_e >= _FP_WFRACBITS_##fs)			\
584		  _FP_FRAC_SLL_##wc(X, (X##_e - _FP_WFRACBITS_##fs + 1));\
585		else							\
586		  _FP_FRAC_SRL_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 1));\
587		_FP_FRAC_ASSEMBLE_##wc(r, X, rsize);			\
588	      }								\
589	    if (rsigned && X##_s)					\
590	      r = -r;							\
591	  }								\
592	break;								\
593      }									\
594  } while (0)
595
596#define _FP_FROM_INT(fs, wc, X, r, rsize, rtype)			\
597  do {									\
598    if (r)								\
599      {									\
600	X##_c = FP_CLS_NORMAL;						\
601									\
602	if ((X##_s = (r < 0)))						\
603	  r = -r;							\
604	/* Note that `r' is now considered unsigned, so we don't have	\
605	   to worry about the single signed overflow case.  */		\
606									\
607	if (rsize <= _FP_W_TYPE_SIZE)					\
608	  __FP_CLZ(X##_e, r);						\
609	else								\
610	  __FP_CLZ_2(X##_e, (_FP_W_TYPE)(r >> _FP_W_TYPE_SIZE), 	\
611		     (_FP_W_TYPE)r);					\
612	if (rsize < _FP_W_TYPE_SIZE)					\
613		X##_e -= (_FP_W_TYPE_SIZE - rsize);			\
614	X##_e = rsize - X##_e - 1;					\
615									\
616	if (_FP_FRACBITS_##fs < rsize && _FP_WFRACBITS_##fs < X##_e)	\
617	  __FP_FRAC_SRS_1(r, (X##_e - _FP_WFRACBITS_##fs), rsize);	\
618	r &= ~((_FP_W_TYPE)1 << X##_e);					\
619	_FP_FRAC_DISASSEMBLE_##wc(X, ((unsigned rtype)r), rsize);	\
620	_FP_FRAC_SLL_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 1));		\
621      }									\
622    else								\
623      {									\
624	X##_c = FP_CLS_ZERO, X##_s = 0;					\
625      }									\
626  } while (0)
627
628
629#define FP_CONV(dfs,sfs,dwc,swc,D,S)			\
630  do {							\
631    _FP_FRAC_CONV_##dwc##_##swc(dfs, sfs, D, S);	\
632    D##_e = S##_e;					\
633    D##_c = S##_c;					\
634    D##_s = S##_s;					\
635  } while (0)
636
637/*
638 * Helper primitives.
639 */
640
641/* Count leading zeros in a word.  */
642
643#ifndef __FP_CLZ
644#if _FP_W_TYPE_SIZE < 64
645/* this is just to shut the compiler up about shifts > word length -- PMM 02/1998 */
646#define __FP_CLZ(r, x)				\
647  do {						\
648    _FP_W_TYPE _t = (x);			\
649    r = _FP_W_TYPE_SIZE - 1;			\
650    if (_t > 0xffff) r -= 16;			\
651    if (_t > 0xffff) _t >>= 16;			\
652    if (_t > 0xff) r -= 8;			\
653    if (_t > 0xff) _t >>= 8;			\
654    if (_t & 0xf0) r -= 4;			\
655    if (_t & 0xf0) _t >>= 4;			\
656    if (_t & 0xc) r -= 2;			\
657    if (_t & 0xc) _t >>= 2;			\
658    if (_t & 0x2) r -= 1;			\
659  } while (0)
660#else /* not _FP_W_TYPE_SIZE < 64 */
661#define __FP_CLZ(r, x)				\
662  do {						\
663    _FP_W_TYPE _t = (x);			\
664    r = _FP_W_TYPE_SIZE - 1;			\
665    if (_t > 0xffffffff) r -= 32;		\
666    if (_t > 0xffffffff) _t >>= 32;		\
667    if (_t > 0xffff) r -= 16;			\
668    if (_t > 0xffff) _t >>= 16;			\
669    if (_t > 0xff) r -= 8;			\
670    if (_t > 0xff) _t >>= 8;			\
671    if (_t & 0xf0) r -= 4;			\
672    if (_t & 0xf0) _t >>= 4;			\
673    if (_t & 0xc) r -= 2;			\
674    if (_t & 0xc) _t >>= 2;			\
675    if (_t & 0x2) r -= 1;			\
676  } while (0)
677#endif /* not _FP_W_TYPE_SIZE < 64 */
678#endif /* ndef __FP_CLZ */
679
680#define _FP_DIV_HELP_imm(q, r, n, d)		\
681  do {						\
682    q = n / d, r = n % d;			\
683  } while (0)
684