1/*
2 * Basic two-word fraction declaration and manipulation.
3 */
4
5#define _FP_FRAC_DECL_2(X)	_FP_W_TYPE X##_f0, X##_f1
6#define _FP_FRAC_COPY_2(D,S)	(D##_f0 = S##_f0, D##_f1 = S##_f1)
7#define _FP_FRAC_SET_2(X,I)	__FP_FRAC_SET_2(X, I)
8#define _FP_FRAC_HIGH_2(X)	(X##_f1)
9#define _FP_FRAC_LOW_2(X)	(X##_f0)
10#define _FP_FRAC_WORD_2(X,w)	(X##_f##w)
11
12#define _FP_FRAC_SLL_2(X,N)						\
13  do {									\
14    if ((N) < _FP_W_TYPE_SIZE)						\
15      {									\
16        if (__builtin_constant_p(N) && (N) == 1) 			\
17          {								\
18            X##_f1 = X##_f1 + X##_f1 + (((_FP_WS_TYPE)(X##_f0)) < 0);	\
19            X##_f0 += X##_f0;						\
20          }								\
21        else								\
22          {								\
23	    X##_f1 = X##_f1 << (N) | X##_f0 >> (_FP_W_TYPE_SIZE - (N));	\
24	    X##_f0 <<= (N);						\
25	  }								\
26      }									\
27    else								\
28      {									\
29	X##_f1 = X##_f0 << ((N) - _FP_W_TYPE_SIZE);			\
30	X##_f0 = 0;							\
31      }									\
32  } while (0)
33
34#define _FP_FRAC_SRL_2(X,N)						\
35  do {									\
36    if ((N) < _FP_W_TYPE_SIZE)						\
37      {									\
38	X##_f0 = X##_f0 >> (N) | X##_f1 << (_FP_W_TYPE_SIZE - (N));	\
39	X##_f1 >>= (N);							\
40      }									\
41    else								\
42      {									\
43	X##_f0 = X##_f1 >> ((N) - _FP_W_TYPE_SIZE);			\
44	X##_f1 = 0;							\
45      }									\
46  } while (0)
47
48/* Right shift with sticky-lsb.  */
49#define _FP_FRAC_SRS_2(X,N,sz)						\
50  do {									\
51    if ((N) < _FP_W_TYPE_SIZE)						\
52      {									\
53	X##_f0 = (X##_f1 << (_FP_W_TYPE_SIZE - (N)) | X##_f0 >> (N) |	\
54		  (__builtin_constant_p(N) && (N) == 1			\
55		   ? X##_f0 & 1						\
56		   : (X##_f0 << (_FP_W_TYPE_SIZE - (N))) != 0));	\
57	X##_f1 >>= (N);							\
58      }									\
59    else								\
60      {									\
61	X##_f0 = (X##_f1 >> ((N) - _FP_W_TYPE_SIZE) |			\
62	          (((X##_f1 << (sz - (N))) | X##_f0) != 0));		\
63	X##_f1 = 0;							\
64      }									\
65  } while (0)
66
67#define _FP_FRAC_ADDI_2(X,I) \
68  __FP_FRAC_ADDI_2(X##_f1, X##_f0, I)
69
70#define _FP_FRAC_ADD_2(R,X,Y) \
71  __FP_FRAC_ADD_2(R##_f1, R##_f0, X##_f1, X##_f0, Y##_f1, Y##_f0)
72
73#define _FP_FRAC_SUB_2(R,X,Y) \
74  __FP_FRAC_SUB_2(R##_f1, R##_f0, X##_f1, X##_f0, Y##_f1, Y##_f0)
75
76#define _FP_FRAC_CLZ_2(R,X)	\
77  do {				\
78    if (X##_f1)			\
79      __FP_CLZ(R,X##_f1);	\
80    else 			\
81    {				\
82      __FP_CLZ(R,X##_f0);	\
83      R += _FP_W_TYPE_SIZE;	\
84    }				\
85  } while(0)
86
87/* Predicates */
88#define _FP_FRAC_NEGP_2(X)	((_FP_WS_TYPE)X##_f1 < 0)
89#define _FP_FRAC_ZEROP_2(X)	((X##_f1 | X##_f0) == 0)
90#define _FP_FRAC_OVERP_2(fs,X)	(X##_f1 & _FP_OVERFLOW_##fs)
91#define _FP_FRAC_EQ_2(X, Y)	(X##_f1 == Y##_f1 && X##_f0 == Y##_f0)
92#define _FP_FRAC_GT_2(X, Y)	\
93  ((X##_f1 > Y##_f1) || (X##_f1 == Y##_f1 && X##_f0 > Y##_f0))
94#define _FP_FRAC_GE_2(X, Y)	\
95  ((X##_f1 > Y##_f1) || (X##_f1 == Y##_f1 && X##_f0 >= Y##_f0))
96
97#define _FP_ZEROFRAC_2		0, 0
98#define _FP_MINFRAC_2		0, 1
99
100/*
101 * Internals
102 */
103
104#define __FP_FRAC_SET_2(X,I1,I0)	(X##_f0 = I0, X##_f1 = I1)
105
106#define __FP_CLZ_2(R, xh, xl)	\
107  do {				\
108    if (xh)			\
109      __FP_CLZ(R,xl);		\
110    else 			\
111    {				\
112      __FP_CLZ(R,xl);		\
113      R += _FP_W_TYPE_SIZE;	\
114    }				\
115  } while(0)
116
117
118#undef __FP_FRAC_ADDI_2
119#define __FP_FRAC_ADDI_2(xh, xl, i)	add_ssaaaa(xh, xl, xh, xl, 0, i)
120#undef __FP_FRAC_ADD_2
121#define __FP_FRAC_ADD_2			add_ssaaaa
122#undef __FP_FRAC_SUB_2
123#define __FP_FRAC_SUB_2			sub_ddmmss
124
125
126/*
127 * Unpack the raw bits of a native fp value.  Do not classify or
128 * normalize the data.
129 */
130
131#define _FP_UNPACK_RAW_2(fs, X, val)			\
132  do {							\
133    union _FP_UNION_##fs _flo; _flo.flt = (val);	\
134							\
135    X##_f0 = _flo.bits.frac0;				\
136    X##_f1 = _flo.bits.frac1;				\
137    X##_e  = _flo.bits.exp;				\
138    X##_s  = _flo.bits.sign;				\
139  } while (0)
140
141
142/*
143 * Repack the raw bits of a native fp value.
144 */
145
146#define _FP_PACK_RAW_2(fs, val, X)			\
147  do {							\
148    union _FP_UNION_##fs _flo;				\
149							\
150    _flo.bits.frac0 = X##_f0;				\
151    _flo.bits.frac1 = X##_f1;				\
152    _flo.bits.exp   = X##_e;				\
153    _flo.bits.sign  = X##_s;				\
154							\
155    (val) = _flo.flt;					\
156  } while (0)
157
158
159/*
160 * Multiplication algorithms:
161 */
162
163/* Given a 1W * 1W => 2W primitive, do the extended multiplication.  */
164
165#define _FP_MUL_MEAT_2_wide(fs, R, X, Y, doit)				\
166  do {									\
167    _FP_FRAC_DECL_4(_z); _FP_FRAC_DECL_2(_b); _FP_FRAC_DECL_2(_c);	\
168									\
169    doit(_FP_FRAC_WORD_4(_z,1), _FP_FRAC_WORD_4(_z,0), X##_f0, Y##_f0); \
170    doit(_b_f1, _b_f0, X##_f0, Y##_f1);					\
171    doit(_c_f1, _c_f0, X##_f1, Y##_f0);					\
172    doit(_FP_FRAC_WORD_4(_z,3), _FP_FRAC_WORD_4(_z,2), X##_f1, Y##_f1); \
173									\
174    __FP_FRAC_ADD_4(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2),	\
175		    _FP_FRAC_WORD_4(_z,1),_FP_FRAC_WORD_4(_z,0),	\
176		    0, _b_f1, _b_f0, 0,					\
177		    _FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2),	\
178		    _FP_FRAC_WORD_4(_z,1),_FP_FRAC_WORD_4(_z,0));	\
179    __FP_FRAC_ADD_4(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2),	\
180		    _FP_FRAC_WORD_4(_z,1),_FP_FRAC_WORD_4(_z,0),	\
181		    0, _c_f1, _c_f0, 0,					\
182		    _FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2),	\
183		    _FP_FRAC_WORD_4(_z,1),_FP_FRAC_WORD_4(_z,0));	\
184									\
185    /* Normalize since we know where the msb of the multiplicands	\
186       were (bit B), we know that the msb of the of the product is	\
187       at either 2B or 2B-1.  */					\
188    _FP_FRAC_SRS_4(_z, _FP_WFRACBITS_##fs-1, 2*_FP_WFRACBITS_##fs);	\
189    R##_f0 = _FP_FRAC_WORD_4(_z,0);					\
190    R##_f1 = _FP_FRAC_WORD_4(_z,1);					\
191  } while (0)
192
193/* This next macro appears to be totally broken. Fortunately nowhere
194 * seems to use it :-> The problem is that we define _z[4] but
195 * then use it in _FP_FRAC_SRS_4, which will attempt to access
196 * _z_f[n] which will cause an error. The fix probably involves
197 * declaring it with _FP_FRAC_DECL_4, see previous macro. -- PMM 02/1998
198 */
199#define _FP_MUL_MEAT_2_gmp(fs, R, X, Y)					\
200  do {									\
201    _FP_W_TYPE _x[2], _y[2], _z[4];					\
202    _x[0] = X##_f0; _x[1] = X##_f1;					\
203    _y[0] = Y##_f0; _y[1] = Y##_f1;					\
204									\
205    mpn_mul_n(_z, _x, _y, 2);						\
206									\
207    /* Normalize since we know where the msb of the multiplicands	\
208       were (bit B), we know that the msb of the of the product is	\
209       at either 2B or 2B-1.  */					\
210    _FP_FRAC_SRS_4(_z, _FP_WFRACBITS##_fs-1, 2*_FP_WFRACBITS_##fs);	\
211    R##_f0 = _z[0];							\
212    R##_f1 = _z[1];							\
213  } while (0)
214
215
216/*
217 * Division algorithms:
218 * This seems to be giving me difficulties -- PMM
219 * Look, NetBSD seems to be able to comment algorithms. Can't you?
220 * I've thrown printks at the problem.
221 * This now appears to work, but I still don't really know why.
222 * Also, I don't think the result is properly normalised...
223 */
224
225#define _FP_DIV_MEAT_2_udiv_64(fs, R, X, Y)				\
226  do {									\
227    extern void _fp_udivmodti4(_FP_W_TYPE q[2], _FP_W_TYPE r[2],	\
228			       _FP_W_TYPE n1, _FP_W_TYPE n0,		\
229			       _FP_W_TYPE d1, _FP_W_TYPE d0);		\
230    _FP_W_TYPE _n_f3, _n_f2, _n_f1, _n_f0, _r_f1, _r_f0;		\
231    _FP_W_TYPE _q_f1, _q_f0, _m_f1, _m_f0;				\
232    _FP_W_TYPE _rmem[2], _qmem[2];					\
233    /* I think this check is to ensure that the result is normalised.   \
234     * Assuming X,Y normalised (ie in [1.0,2.0)) X/Y will be in         \
235     * [0.5,2.0). Furthermore, it will be less than 1.0 iff X < Y.      \
236     * In this case we tweak things. (this is based on comments in      \
237     * the NetBSD FPU emulation code. )                                 \
238     * We know X,Y are normalised because we ensure this as part of     \
239     * the unpacking process. -- PMM                                    \
240     */									\
241    if (_FP_FRAC_GT_2(X, Y))						\
242      {									\
243/*	R##_e++; */							\
244	_n_f3 = X##_f1 >> 1;						\
245	_n_f2 = X##_f1 << (_FP_W_TYPE_SIZE - 1) | X##_f0 >> 1;		\
246	_n_f1 = X##_f0 << (_FP_W_TYPE_SIZE - 1);			\
247	_n_f0 = 0;							\
248      }									\
249    else								\
250      {									\
251	R##_e--;							\
252	_n_f3 = X##_f1;							\
253	_n_f2 = X##_f0;							\
254	_n_f1 = _n_f0 = 0;						\
255      }									\
256									\
257    /* Normalize, i.e. make the most significant bit of the 		\
258       denominator set.  CHANGED: - 1 to nothing -- PMM */		\
259    _FP_FRAC_SLL_2(Y, _FP_WFRACXBITS_##fs /* -1 */);			\
260									\
261    /* Do the 256/128 bit division given the 128-bit _fp_udivmodtf4 	\
262       primitive snagged from libgcc2.c.  */				\
263									\
264    _fp_udivmodti4(_qmem, _rmem, _n_f3, _n_f2, 0, Y##_f1);		\
265    _q_f1 = _qmem[0];							\
266    umul_ppmm(_m_f1, _m_f0, _q_f1, Y##_f0);				\
267    _r_f1 = _rmem[0];							\
268    _r_f0 = _n_f1;							\
269    if (_FP_FRAC_GT_2(_m, _r))						\
270      {									\
271	_q_f1--;							\
272	_FP_FRAC_ADD_2(_r, _r, Y);					\
273	if (_FP_FRAC_GE_2(_r, Y) && _FP_FRAC_GT_2(_m, _r))		\
274	  {								\
275	    _q_f1--;							\
276	    _FP_FRAC_ADD_2(_r, _r, Y);					\
277	  }								\
278      }									\
279    _FP_FRAC_SUB_2(_r, _r, _m);						\
280									\
281    _fp_udivmodti4(_qmem, _rmem, _r_f1, _r_f0, 0, Y##_f1);		\
282    _q_f0 = _qmem[0];							\
283    umul_ppmm(_m_f1, _m_f0, _q_f0, Y##_f0);				\
284    _r_f1 = _rmem[0];							\
285    _r_f0 = _n_f0;							\
286    if (_FP_FRAC_GT_2(_m, _r))						\
287      {									\
288	_q_f0--;							\
289	_FP_FRAC_ADD_2(_r, _r, Y);					\
290	if (_FP_FRAC_GE_2(_r, Y) && _FP_FRAC_GT_2(_m, _r))		\
291	  {								\
292	    _q_f0--;							\
293	    _FP_FRAC_ADD_2(_r, _r, Y);					\
294	  }								\
295      }									\
296    _FP_FRAC_SUB_2(_r, _r, _m);						\
297									\
298    R##_f1 = _q_f1;							\
299    R##_f0 = _q_f0 | ((_r_f1 | _r_f0) != 0);				\
300    /* adjust so answer is normalized again. I'm not sure what the 	\
301     * final sz param should be. In practice it's never used since      \
302     * N is 1 which is always going to be < _FP_W_TYPE_SIZE...		\
303     */									\
304    /* _FP_FRAC_SRS_2(R,1,_FP_WFRACBITS_##fs);	*/			\
305  } while (0)
306
307
308#define _FP_DIV_MEAT_2_gmp(fs, R, X, Y)					\
309  do {									\
310    _FP_W_TYPE _x[4], _y[2], _z[4];					\
311    _y[0] = Y##_f0; _y[1] = Y##_f1;					\
312    _x[0] = _x[3] = 0;							\
313    if (_FP_FRAC_GT_2(X, Y))						\
314      {									\
315	R##_e++;							\
316	_x[1] = (X##_f0 << (_FP_WFRACBITS-1 - _FP_W_TYPE_SIZE) |	\
317		 X##_f1 >> (_FP_W_TYPE_SIZE -				\
318			    (_FP_WFRACBITS-1 - _FP_W_TYPE_SIZE)));	\
319	_x[2] = X##_f1 << (_FP_WFRACBITS-1 - _FP_W_TYPE_SIZE);		\
320      }									\
321    else								\
322      {									\
323	_x[1] = (X##_f0 << (_FP_WFRACBITS - _FP_W_TYPE_SIZE) |		\
324		 X##_f1 >> (_FP_W_TYPE_SIZE -				\
325			    (_FP_WFRACBITS - _FP_W_TYPE_SIZE)));	\
326	_x[2] = X##_f1 << (_FP_WFRACBITS - _FP_W_TYPE_SIZE);		\
327      }									\
328									\
329    (void) mpn_divrem (_z, 0, _x, 4, _y, 2);				\
330    R##_f1 = _z[1];							\
331    R##_f0 = _z[0] | ((_x[0] | _x[1]) != 0);				\
332  } while (0)
333
334
335/*
336 * Square root algorithms:
337 * We have just one right now, maybe Newton approximation
338 * should be added for those machines where division is fast.
339 */
340
341#define _FP_SQRT_MEAT_2(R, S, T, X, q)			\
342  do {							\
343    while (q)						\
344      {							\
345        T##_f1 = S##_f1 + q;				\
346        if (T##_f1 <= X##_f1)				\
347          {						\
348            S##_f1 = T##_f1 + q;			\
349            X##_f1 -= T##_f1;				\
350            R##_f1 += q;				\
351          }						\
352        _FP_FRAC_SLL_2(X, 1);				\
353        q >>= 1;					\
354      }							\
355    q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1);		\
356    while (q)						\
357      {							\
358        T##_f0 = S##_f0 + q;				\
359        T##_f1 = S##_f1;				\
360        if (T##_f1 < X##_f1 || 				\
361            (T##_f1 == X##_f1 && T##_f0 < X##_f0))	\
362          {						\
363            S##_f0 = T##_f0 + q;			\
364            if (((_FP_WS_TYPE)T##_f0) < 0 &&		\
365                ((_FP_WS_TYPE)S##_f0) >= 0)		\
366              S##_f1++;					\
367            _FP_FRAC_SUB_2(X, X, T);			\
368            R##_f0 += q;				\
369          }						\
370        _FP_FRAC_SLL_2(X, 1);				\
371        q >>= 1;					\
372      }							\
373  } while (0)
374
375
376/*
377 * Assembly/disassembly for converting to/from integral types.
378 * No shifting or overflow handled here.
379 */
380
381#define _FP_FRAC_ASSEMBLE_2(r, X, rsize)	\
382  do {						\
383    if (rsize <= _FP_W_TYPE_SIZE)		\
384      r = X##_f0;				\
385    else					\
386      {						\
387	r = X##_f1;				\
388	r <<= _FP_W_TYPE_SIZE;			\
389	r += X##_f0;				\
390      }						\
391  } while (0)
392
393#define _FP_FRAC_DISASSEMBLE_2(X, r, rsize)				\
394  do {									\
395    X##_f0 = r;								\
396    X##_f1 = (rsize <= _FP_W_TYPE_SIZE ? 0 : r >> _FP_W_TYPE_SIZE);	\
397  } while (0)
398
399/*
400 * Convert FP values between word sizes
401 */
402
403#define _FP_FRAC_CONV_1_2(dfs, sfs, D, S)				\
404  do {									\
405    _FP_FRAC_SRS_2(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs),	\
406		   _FP_WFRACBITS_##sfs);				\
407    D##_f = S##_f0;							\
408  } while (0)
409
410#define _FP_FRAC_CONV_2_1(dfs, sfs, D, S)				\
411  do {									\
412    D##_f0 = S##_f;							\
413    D##_f1 = 0;								\
414    _FP_FRAC_SLL_2(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs));	\
415  } while (0)
416