2 * Basic four-word fraction declaration and manipulation.
4 * When adding quadword support for 32 bit machines, we need
5 * to be a little careful as double multiply uses some of these
7 * _FP_MUL_MEAT_2_wide() uses _FP_FRAC_DECL_4, _FP_FRAC_WORD_4,
8 * _FP_FRAC_ADD_4, _FP_FRAC_SRS_4
9 * _FP_MUL_MEAT_2_gmp() uses _FP_FRAC_SRS_4 (and should use
10 * _FP_FRAC_DECL_4: it appears to be broken and is not used
13 * I've now fixed all the macros that were here from the sparc64 code.
14 * [*none* of the shift macros were correct!] -- PMM 02/1998
16 * The only quadword stuff that remains to be coded is:
17 * 1) the conversion to/from ints, which requires
18 * that we check (in op-common.h) that the following do the right thing
19 * for quadwords: _FP_TO_INT(Q,4,r,X,rsz,rsg), _FP_FROM_INT(Q,4,X,r,rs,rt)
20 * 2) multiply, divide and sqrt, which require:
21 * _FP_MUL_MEAT_4_*(R,X,Y), _FP_DIV_MEAT_4_*(R,X,Y), _FP_SQRT_MEAT_4(R,S,T,X,q),
22 * This also needs _FP_MUL_MEAT_Q and _FP_DIV_MEAT_Q to be defined to
23 * some suitable _FP_MUL_MEAT_4_* macros in sfp-machine.h.
24 * [we're free to choose whatever FP_MUL_MEAT_4_* macros we need for
25 * these; they are used nowhere else. ]
28 #define _FP_FRAC_DECL_4(X) _FP_W_TYPE X##_f[4]
29 #define _FP_FRAC_COPY_4(D,S) \
30 (D##_f[0] = S##_f[0], D##_f[1] = S##_f[1], \
31 D##_f[2] = S##_f[2], D##_f[3] = S##_f[3])
32 /* The _FP_FRAC_SET_n(X,I) macro is intended for use with another
33 * macro such as _FP_ZEROFRAC_n which returns n comma separated values.
34 * The result is that we get an expansion of __FP_FRAC_SET_n(X,I0,I1,I2,I3)
35 * which just assigns the In values to the array X##_f[].
36 * This is why the number of parameters doesn't appear to match
37 * at first glance... -- PMM
39 #define _FP_FRAC_SET_4(X,I) __FP_FRAC_SET_4(X, I)
40 #define _FP_FRAC_HIGH_4(X) (X##_f[3])
41 #define _FP_FRAC_LOW_4(X) (X##_f[0])
42 #define _FP_FRAC_WORD_4(X,w) (X##_f[w])
44 #define _FP_FRAC_SLL_4(X,N) \
46 _FP_I_TYPE _up, _down, _skip, _i; \
47 _skip = (N) / _FP_W_TYPE_SIZE; \
48 _up = (N) % _FP_W_TYPE_SIZE; \
49 _down = _FP_W_TYPE_SIZE - _up; \
50 for (_i = 3; _i > _skip; --_i) \
51 X##_f[_i] = X##_f[_i-_skip] << _up | X##_f[_i-_skip-1] >> _down; \
52 /* bugfixed: was X##_f[_i] <<= _up; -- PMM 02/1998 */ \
53 X##_f[_i] = X##_f[0] << _up; \
54 for (--_i; _i >= 0; --_i) \
58 /* This one was broken too */
59 #define _FP_FRAC_SRL_4(X,N) \
61 _FP_I_TYPE _up, _down, _skip, _i; \
62 _skip = (N) / _FP_W_TYPE_SIZE; \
63 _down = (N) % _FP_W_TYPE_SIZE; \
64 _up = _FP_W_TYPE_SIZE - _down; \
65 for (_i = 0; _i < 3-_skip; ++_i) \
66 X##_f[_i] = X##_f[_i+_skip] >> _down | X##_f[_i+_skip+1] << _up; \
67 X##_f[_i] = X##_f[3] >> _down; \
68 for (++_i; _i < 4; ++_i) \
73 /* Right shift with sticky-lsb.
74 * What this actually means is that we do a standard right-shift,
75 * but that if any of the bits that fall off the right hand side
76 * were one then we always set the LSbit.
78 #define _FP_FRAC_SRS_4(X,N,size) \
80 _FP_I_TYPE _up, _down, _skip, _i; \
82 _skip = (N) / _FP_W_TYPE_SIZE; \
83 _down = (N) % _FP_W_TYPE_SIZE; \
84 _up = _FP_W_TYPE_SIZE - _down; \
85 for (_s = _i = 0; _i < _skip; ++_i) \
87 _s |= X##_f[_i] << _up; \
88 /* s is now != 0 if we want to set the LSbit */ \
89 for (_i = 0; _i < 3-_skip; ++_i) \
90 X##_f[_i] = X##_f[_i+_skip] >> _down | X##_f[_i+_skip+1] << _up; \
91 X##_f[_i] = X##_f[3] >> _down; \
92 for (++_i; _i < 4; ++_i) \
94 /* don't fix the LSB until the very end when we're sure f[0] is stable */ \
95 X##_f[0] |= (_s != 0); \
98 #define _FP_FRAC_ADD_4(R,X,Y) \
99 __FP_FRAC_ADD_4(R##_f[3], R##_f[2], R##_f[1], R##_f[0], \
100 X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
101 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
103 #define _FP_FRAC_SUB_4(R,X,Y) \
104 __FP_FRAC_SUB_4(R##_f[3], R##_f[2], R##_f[1], R##_f[0], \
105 X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
106 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
108 #define _FP_FRAC_ADDI_4(X,I) \
109 __FP_FRAC_ADDI_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], I)
111 #define _FP_ZEROFRAC_4 0,0,0,0
112 #define _FP_MINFRAC_4 0,0,0,1
114 #define _FP_FRAC_ZEROP_4(X) ((X##_f[0] | X##_f[1] | X##_f[2] | X##_f[3]) == 0)
115 #define _FP_FRAC_NEGP_4(X) ((_FP_WS_TYPE)X##_f[3] < 0)
116 #define _FP_FRAC_OVERP_4(fs,X) (X##_f[0] & _FP_OVERFLOW_##fs)
118 #define _FP_FRAC_EQ_4(X,Y) \
119 (X##_f[0] == Y##_f[0] && X##_f[1] == Y##_f[1] \
120 && X##_f[2] == Y##_f[2] && X##_f[3] == Y##_f[3])
122 #define _FP_FRAC_GT_4(X,Y) \
123 (X##_f[3] > Y##_f[3] || \
124 (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
125 (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
126 (X##_f[1] == Y##_f[1] && X##_f[0] > Y##_f[0]) \
131 #define _FP_FRAC_GE_4(X,Y) \
132 (X##_f[3] > Y##_f[3] || \
133 (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
134 (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
135 (X##_f[1] == Y##_f[1] && X##_f[0] >= Y##_f[0]) \
141 #define _FP_FRAC_CLZ_4(R,X) \
145 __FP_CLZ(R,X##_f[3]); \
149 __FP_CLZ(R,X##_f[2]); \
150 R += _FP_W_TYPE_SIZE; \
154 __FP_CLZ(R,X##_f[2]); \
155 R += _FP_W_TYPE_SIZE*2; \
159 __FP_CLZ(R,X##_f[0]); \
160 R += _FP_W_TYPE_SIZE*3; \
165 #define _FP_UNPACK_RAW_4(fs, X, val) \
167 union _FP_UNION_##fs _flo; _flo.flt = (val); \
168 X##_f[0] = _flo.bits.frac0; \
169 X##_f[1] = _flo.bits.frac1; \
170 X##_f[2] = _flo.bits.frac2; \
171 X##_f[3] = _flo.bits.frac3; \
172 X##_e = _flo.bits.exp; \
173 X##_s = _flo.bits.sign; \
176 #define _FP_PACK_RAW_4(fs, val, X) \
178 union _FP_UNION_##fs _flo; \
179 _flo.bits.frac0 = X##_f[0]; \
180 _flo.bits.frac1 = X##_f[1]; \
181 _flo.bits.frac2 = X##_f[2]; \
182 _flo.bits.frac3 = X##_f[3]; \
183 _flo.bits.exp = X##_e; \
184 _flo.bits.sign = X##_s; \
193 #define __FP_FRAC_SET_4(X,I3,I2,I1,I0) \
194 (X##_f[3] = I3, X##_f[2] = I2, X##_f[1] = I1, X##_f[0] = I0)
196 #ifndef __FP_FRAC_ADD_4
197 #define __FP_FRAC_ADD_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
199 r1 = x1 + y1 + (r0 < x0), \
200 r2 = x2 + y2 + (r1 < x1), \
201 r3 = x3 + y3 + (r2 < x2))
204 #ifndef __FP_FRAC_SUB_4
205 #define __FP_FRAC_SUB_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
207 r1 = x1 - y1 - (r0 > x0), \
208 r2 = x2 - y2 - (r1 > x1), \
209 r3 = x3 - y3 - (r2 > x2))
212 #ifndef __FP_FRAC_ADDI_4
213 /* I always wanted to be a lisp programmer :-> */
214 #define __FP_FRAC_ADDI_4(x3,x2,x1,x0,i) \
215 (x3 += ((x2 += ((x1 += ((x0 += i) < x0)) < x1) < x2)))
218 /* Convert FP values between word sizes. This appears to be more
219 * complicated than I'd have expected it to be, so these might be
220 * wrong... These macros are in any case somewhat bogus because they
221 * use information about what various FRAC_n variables look like
222 * internally [eg, that 2 word vars are X_f0 and x_f1]. But so do
223 * the ones in op-2.h and op-1.h.
225 #define _FP_FRAC_CONV_1_4(dfs, sfs, D, S) \
227 _FP_FRAC_SRS_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs), \
228 _FP_WFRACBITS_##sfs); \
232 #define _FP_FRAC_CONV_2_4(dfs, sfs, D, S) \
234 _FP_FRAC_SRS_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs), \
235 _FP_WFRACBITS_##sfs); \
240 /* Assembly/disassembly for converting to/from integral types.
241 * No shifting or overflow handled here.
243 /* Put the FP value X into r, which is an integer of size rsize. */
244 #define _FP_FRAC_ASSEMBLE_4(r, X, rsize) \
246 if (rsize <= _FP_W_TYPE_SIZE) \
248 else if (rsize <= 2*_FP_W_TYPE_SIZE) \
251 r <<= _FP_W_TYPE_SIZE; \
256 /* I'm feeling lazy so we deal with int == 3words (implausible)*/ \
257 /* and int == 4words as a single case. */ \
259 r <<= _FP_W_TYPE_SIZE; \
261 r <<= _FP_W_TYPE_SIZE; \
263 r <<= _FP_W_TYPE_SIZE; \
268 /* "No disassemble Number Five!" */
269 /* move an integer of size rsize into X's fractional part. We rely on
270 * the _f[] array consisting of words of size _FP_W_TYPE_SIZE to avoid
271 * having to mask the values we store into it.
273 #define _FP_FRAC_DISASSEMBLE_4(X, r, rsize) \
276 X##_f[1] = (rsize <= _FP_W_TYPE_SIZE ? 0 : r >> _FP_W_TYPE_SIZE); \
277 X##_f[2] = (rsize <= 2*_FP_W_TYPE_SIZE ? 0 : r >> 2*_FP_W_TYPE_SIZE); \
278 X##_f[3] = (rsize <= 3*_FP_W_TYPE_SIZE ? 0 : r >> 3*_FP_W_TYPE_SIZE); \
281 #define _FP_FRAC_CONV_4_1(dfs, sfs, D, S) \
284 D##_f[1] = D##_f[2] = D##_f[3] = 0; \
285 _FP_FRAC_SLL_4(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs)); \
288 #define _FP_FRAC_CONV_4_2(dfs, sfs, D, S) \
292 D##_f[2] = D##_f[3] = 0; \
293 _FP_FRAC_SLL_4(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs)); \
296 /* FIXME! This has to be written */
297 #define _FP_SQRT_MEAT_4(R, S, T, X, q)