1 /*
2  * Linux/PA-RISC Project (http://www.parisc-linux.org/)
3  *
4  * Floating-point emulation code
5  *  Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2, or (at your option)
10  *    any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program; if not, write to the Free Software
19  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #ifdef __NO_PA_HDRS
23     PA header file -- do not include this header file for non-PA builds.
24 #endif
25 
26 /*
27  * Some more constants
28  */
29 #define SGL_FX_MAX_EXP 30
30 #define DBL_FX_MAX_EXP 62
31 #define QUAD_FX_MAX_EXP 126
32 
33 #define Dintp1(object) (object)
34 #define Dintp2(object) (object)
35 
36 #define Duintp1(object) (object)
37 #define Duintp2(object) (object)
38 
39 #define Qintp0(object) (object)
40 #define Qintp1(object) (object)
41 #define Qintp2(object) (object)
42 #define Qintp3(object) (object)
43 
44 
45 /*
46  * These macros will be used specifically by the convert instructions.
47  *
48  *
49  * Single format macros
50  */
51 
52 #define Sgl_to_dbl_exponent(src_exponent,dest)			\
53     Deposit_dexponent(dest,src_exponent+(DBL_BIAS-SGL_BIAS))
54 
55 #define Sgl_to_dbl_mantissa(src_mantissa,destA,destB)	\
56     Deposit_dmantissap1(destA,src_mantissa>>3);		\
57     Dmantissap2(destB) = src_mantissa << 29
58 
59 #define Sgl_isinexact_to_fix(sgl_value,exponent)	\
60     ((exponent < (SGL_P - 1)) ?				\
61      (Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) : FALSE)
62 
63 #define Int_isinexact_to_sgl(int_value)	(int_value << 33 - SGL_EXP_LENGTH)
64 
65 #define Sgl_roundnearest_from_int(int_value,sgl_value)			\
66     if (int_value & 1<<(SGL_EXP_LENGTH - 2))   /* round bit */		\
67     	if ((int_value << 34 - SGL_EXP_LENGTH) || Slow(sgl_value))	\
68 		Sall(sgl_value)++
69 
70 #define Dint_isinexact_to_sgl(dint_valueA,dint_valueB)		\
71     ((Dintp1(dint_valueA) << 33 - SGL_EXP_LENGTH) || Dintp2(dint_valueB))
72 
73 #define Sgl_roundnearest_from_dint(dint_valueA,dint_valueB,sgl_value)	\
74     if (Dintp1(dint_valueA) & 1<<(SGL_EXP_LENGTH - 2)) 			\
75     	if ((Dintp1(dint_valueA) << 34 - SGL_EXP_LENGTH) ||		\
76     	Dintp2(dint_valueB) || Slow(sgl_value)) Sall(sgl_value)++
77 
78 #define Dint_isinexact_to_dbl(dint_value) 	\
79     (Dintp2(dint_value) << 33 - DBL_EXP_LENGTH)
80 
81 #define Dbl_roundnearest_from_dint(dint_opndB,dbl_opndA,dbl_opndB) 	\
82     if (Dintp2(dint_opndB) & 1<<(DBL_EXP_LENGTH - 2))			\
83        if ((Dintp2(dint_opndB) << 34 - DBL_EXP_LENGTH) || Dlowp2(dbl_opndB))  \
84           if ((++Dallp2(dbl_opndB))==0) Dallp1(dbl_opndA)++
85 
86 #define Sgl_isone_roundbit(sgl_value,exponent)			\
87     ((Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) >> 31)
88 
89 #define Sgl_isone_stickybit(sgl_value,exponent)		\
90     (exponent < (SGL_P - 2) ?				\
91      Sall(sgl_value) << (SGL_EXP_LENGTH + 2 + exponent) : FALSE)
92 
93 
94 /*
95  * Double format macros
96  */
97 
98 #define Dbl_to_sgl_exponent(src_exponent,dest)			\
99     dest = src_exponent + (SGL_BIAS - DBL_BIAS)
100 
101 #define Dbl_to_sgl_mantissa(srcA,srcB,dest,inexact,guard,sticky,odd)	\
102     Shiftdouble(Dmantissap1(srcA),Dmantissap2(srcB),29,dest); 	\
103     guard = Dbit3p2(srcB);					\
104     sticky = Dallp2(srcB)<<4;					\
105     inexact = guard | sticky;					\
106     odd = Dbit2p2(srcB)
107 
108 #define Dbl_to_sgl_denormalized(srcA,srcB,exp,dest,inexact,guard,sticky,odd,tiny) \
109     Deposit_dexponent(srcA,1);						\
110     tiny = TRUE;							\
111     if (exp >= -2) {							\
112 	if (exp == 0) {							\
113 	    inexact = Dallp2(srcB) << 3;				\
114 	    guard = inexact >> 31;					\
115 	    sticky = inexact << 1;					\
116 	    Shiftdouble(Dmantissap1(srcA),Dmantissap2(srcB),29,dest);	\
117 	    odd = dest << 31;						\
118 	    if (inexact) {						\
119 		switch(Rounding_mode()) {				\
120 		    case ROUNDPLUS:					\
121 			if (Dbl_iszero_sign(srcA)) {			\
122 			    dest++;					\
123 			    if (Sgl_isone_hidden(dest))	\
124 				tiny = FALSE;				\
125 			    dest--;					\
126 			}						\
127 			break;						\
128 		    case ROUNDMINUS:					\
129 			if (Dbl_isone_sign(srcA)) {			\
130 			    dest++;					\
131 			    if (Sgl_isone_hidden(dest))	\
132 				tiny = FALSE;				\
133 			    dest--;					\
134 			}						\
135 			break;						\
136 		    case ROUNDNEAREST:					\
137 			if (guard && (sticky || odd)) {			\
138 			    dest++;					\
139 			    if (Sgl_isone_hidden(dest))	\
140 				tiny = FALSE;				\
141 			    dest--;					\
142 			}						\
143 			break;						\
144 		}							\
145 	    }								\
146 		/* shift right by one to get correct result */		\
147 		guard = odd;						\
148 		sticky = inexact;					\
149 		inexact |= guard;					\
150 		dest >>= 1;						\
151     		Deposit_dsign(srcA,0);					\
152     	        Shiftdouble(Dallp1(srcA),Dallp2(srcB),30,dest);		\
153 	        odd = dest << 31;					\
154 	}								\
155 	else {								\
156     	    inexact = Dallp2(srcB) << (2 + exp);			\
157     	    guard = inexact >> 31;					\
158     	    sticky = inexact << 1; 					\
159     	    Deposit_dsign(srcA,0);					\
160     	    if (exp == -2) dest = Dallp1(srcA);				\
161     	    else Variable_shift_double(Dallp1(srcA),Dallp2(srcB),30-exp,dest); \
162     	    odd = dest << 31;						\
163 	}								\
164     }									\
165     else {								\
166     	Deposit_dsign(srcA,0);						\
167     	if (exp > (1 - SGL_P)) {					\
168     	    dest = Dallp1(srcA) >> (- 2 - exp);				\
169     	    inexact = Dallp1(srcA) << (34 + exp);			\
170     	    guard = inexact >> 31;					\
171     	    sticky = (inexact << 1) | Dallp2(srcB);			\
172     	    inexact |= Dallp2(srcB); 					\
173     	    odd = dest << 31;						\
174     	}								\
175     	else {								\
176     	    dest = 0;							\
177     	    inexact = Dallp1(srcA) | Dallp2(srcB);			\
178     	    if (exp == (1 - SGL_P)) {					\
179     	    	guard = Dhidden(srcA);					\
180     	    	sticky = Dmantissap1(srcA) | Dallp2(srcB); 		\
181     	    }								\
182     	    else {							\
183     	    	guard = 0;						\
184     	    	sticky = inexact;					\
185     	    }								\
186     	    odd = 0;							\
187     	}								\
188     }									\
189     exp = 0
190 
191 #define Dbl_isinexact_to_fix(dbl_valueA,dbl_valueB,exponent)		\
192     (exponent < (DBL_P-33) ? 						\
193      Dallp2(dbl_valueB) || Dallp1(dbl_valueA) << (DBL_EXP_LENGTH+1+exponent) : \
194      (exponent < (DBL_P-1) ? Dallp2(dbl_valueB) << (exponent + (33-DBL_P)) :   \
195       FALSE))
196 
197 #define Dbl_isoverflow_to_int(exponent,dbl_valueA,dbl_valueB)		\
198     ((exponent > SGL_FX_MAX_EXP + 1) || Dsign(dbl_valueA)==0 ||		\
199      Dmantissap1(dbl_valueA)!=0 || (Dallp2(dbl_valueB)>>21)!=0 )
200 
201 #define Dbl_isone_roundbit(dbl_valueA,dbl_valueB,exponent)              \
202     ((exponent < (DBL_P - 33) ?						\
203       Dallp1(dbl_valueA) >> ((30 - DBL_EXP_LENGTH) - exponent) :	\
204       Dallp2(dbl_valueB) >> ((DBL_P - 2) - exponent)) & 1)
205 
206 #define Dbl_isone_stickybit(dbl_valueA,dbl_valueB,exponent)		\
207     (exponent < (DBL_P-34) ? 						\
208      (Dallp2(dbl_valueB) || Dallp1(dbl_valueA)<<(DBL_EXP_LENGTH+2+exponent)) : \
209      (exponent<(DBL_P-2) ? (Dallp2(dbl_valueB) << (exponent + (34-DBL_P))) : \
210       FALSE))
211 
212 
213 /* Int macros */
214 
215 #define Int_from_sgl_mantissa(sgl_value,exponent)	\
216     Sall(sgl_value) = 				\
217     	(unsigned)(Sall(sgl_value) << SGL_EXP_LENGTH)>>(31 - exponent)
218 
219 #define Int_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent)	\
220     Shiftdouble(Dallp1(dbl_valueA),Dallp2(dbl_valueB),22,Dallp1(dbl_valueA)); \
221     if (exponent < 31) Dallp1(dbl_valueA) >>= 30 - exponent;	\
222     else Dallp1(dbl_valueA) <<= 1
223 
224 #define Int_negate(int_value) int_value = -int_value
225 
226 
227 /* Dint macros */
228 
229 #define Dint_from_sgl_mantissa(sgl_value,exponent,dresultA,dresultB)	\
230     {Sall(sgl_value) <<= SGL_EXP_LENGTH;  /*  left-justify  */		\
231     if (exponent <= 31) {						\
232     	Dintp1(dresultA) = 0;						\
233     	Dintp2(dresultB) = (unsigned)Sall(sgl_value) >> (31 - exponent); \
234     }									\
235     else {								\
236     	Dintp1(dresultA) = Sall(sgl_value) >> (63 - exponent);		\
237     	Dintp2(dresultB) = Sall(sgl_value) << (exponent - 31);		\
238     }}
239 
240 
241 #define Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \
242     {if (exponent < 32) {						\
243     	Dintp1(destA) = 0;						\
244     	if (exponent <= 20)						\
245     	    Dintp2(destB) = Dallp1(dbl_valueA) >> 20-exponent;		\
246     	else Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
247 	     52-exponent,Dintp2(destB));					\
248     }									\
249     else {								\
250     	if (exponent <= 52) {						\
251     	    Dintp1(destA) = Dallp1(dbl_valueA) >> 52-exponent;		\
252 	    if (exponent == 52) Dintp2(destB) = Dallp2(dbl_valueB);	\
253 	    else Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
254 	    52-exponent,Dintp2(destB));					\
255         }								\
256     	else {								\
257     	    Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
258 	    84-exponent,Dintp1(destA));					\
259     	    Dintp2(destB) = Dallp2(dbl_valueB) << exponent-52;		\
260     	}								\
261     }}
262 
263 #define Dint_setzero(dresultA,dresultB) 	\
264     Dintp1(dresultA) = 0; 	\
265     Dintp2(dresultB) = 0
266 
267 #define Dint_setone_sign(dresultA,dresultB)		\
268     Dintp1(dresultA) = ~Dintp1(dresultA);		\
269     if ((Dintp2(dresultB) = -Dintp2(dresultB)) == 0) Dintp1(dresultA)++
270 
271 #define Dint_set_minint(dresultA,dresultB)		\
272     Dintp1(dresultA) = (unsigned int)1<<31;		\
273     Dintp2(dresultB) = 0
274 
275 #define Dint_isone_lowp2(dresultB)  (Dintp2(dresultB) & 01)
276 
277 #define Dint_increment(dresultA,dresultB) 		\
278     if ((++Dintp2(dresultB))==0) Dintp1(dresultA)++
279 
280 #define Dint_decrement(dresultA,dresultB) 		\
281     if ((Dintp2(dresultB)--)==0) Dintp1(dresultA)--
282 
283 #define Dint_negate(dresultA,dresultB)			\
284     Dintp1(dresultA) = ~Dintp1(dresultA);		\
285     if ((Dintp2(dresultB) = -Dintp2(dresultB))==0) Dintp1(dresultA)++
286 
287 #define Dint_copyfromptr(src,destA,destB) \
288      Dintp1(destA) = src->wd0;		\
289      Dintp2(destB) = src->wd1
290 #define Dint_copytoptr(srcA,srcB,dest)	\
291     dest->wd0 = Dintp1(srcA);		\
292     dest->wd1 = Dintp2(srcB)
293 
294 
295 /* other macros  */
296 
297 #define Find_ms_one_bit(value, position)	\
298     {						\
299 	int var;				\
300 	for (var=8; var >=1; var >>= 1) {	\
301 	    if (value >> 32 - position)		\
302 		position -= var;		\
303 		else position += var;		\
304 	}					\
305 	if ((value >> 32 - position) == 0)	\
306 	    position--;				\
307 	else position -= 2;			\
308     }
309 
310 
311 /*
312  * Unsigned int macros
313  */
314 #define Duint_copyfromptr(src,destA,destB) \
315     Dint_copyfromptr(src,destA,destB)
316 #define Duint_copytoptr(srcA,srcB,dest)	\
317     Dint_copytoptr(srcA,srcB,dest)
318 
319 #define Suint_isinexact_to_sgl(int_value) \
320     (int_value << 32 - SGL_EXP_LENGTH)
321 
322 #define Sgl_roundnearest_from_suint(suint_value,sgl_value)		\
323     if (suint_value & 1<<(SGL_EXP_LENGTH - 1))   /* round bit */	\
324     	if ((suint_value << 33 - SGL_EXP_LENGTH) || Slow(sgl_value))	\
325 		Sall(sgl_value)++
326 
327 #define Duint_isinexact_to_sgl(duint_valueA,duint_valueB)	\
328     ((Duintp1(duint_valueA) << 32 - SGL_EXP_LENGTH) || Duintp2(duint_valueB))
329 
330 #define Sgl_roundnearest_from_duint(duint_valueA,duint_valueB,sgl_value) \
331     if (Duintp1(duint_valueA) & 1<<(SGL_EXP_LENGTH - 1))		\
332     	if ((Duintp1(duint_valueA) << 33 - SGL_EXP_LENGTH) ||		\
333     	Duintp2(duint_valueB) || Slow(sgl_value)) Sall(sgl_value)++
334 
335 #define Duint_isinexact_to_dbl(duint_value) 	\
336     (Duintp2(duint_value) << 32 - DBL_EXP_LENGTH)
337 
338 #define Dbl_roundnearest_from_duint(duint_opndB,dbl_opndA,dbl_opndB) 	\
339     if (Duintp2(duint_opndB) & 1<<(DBL_EXP_LENGTH - 1))			\
340        if ((Duintp2(duint_opndB) << 33 - DBL_EXP_LENGTH) || Dlowp2(dbl_opndB)) \
341           if ((++Dallp2(dbl_opndB))==0) Dallp1(dbl_opndA)++
342 
343 #define Suint_from_sgl_mantissa(src,exponent,result)	\
344     Sall(result) = (unsigned)(Sall(src) << SGL_EXP_LENGTH)>>(31 - exponent)
345 
346 #define Sgl_isinexact_to_unsigned(sgl_value,exponent)	\
347     Sgl_isinexact_to_fix(sgl_value,exponent)
348 
349 #define Duint_from_sgl_mantissa(sgl_value,exponent,dresultA,dresultB)	\
350   {Sall(sgl_value) <<= SGL_EXP_LENGTH;  /*  left-justify  */		\
351     if (exponent <= 31) {						\
352     	Dintp1(dresultA) = 0;						\
353     	Dintp2(dresultB) = (unsigned)Sall(sgl_value) >> (31 - exponent); \
354     }									\
355     else {								\
356     	Dintp1(dresultA) = Sall(sgl_value) >> (63 - exponent);		\
357     	Dintp2(dresultB) = Sall(sgl_value) << (exponent - 31);		\
358     }									\
359     Sall(sgl_value) >>= SGL_EXP_LENGTH;  /* return to original */	\
360   }
361 
362 #define Duint_setzero(dresultA,dresultB) 	\
363     Dint_setzero(dresultA,dresultB)
364 
365 #define Duint_increment(dresultA,dresultB) Dint_increment(dresultA,dresultB)
366 
367 #define Duint_isone_lowp2(dresultB)  Dint_isone_lowp2(dresultB)
368 
369 #define Suint_from_dbl_mantissa(srcA,srcB,exponent,dest) \
370     Shiftdouble(Dallp1(srcA),Dallp2(srcB),21,dest); \
371     dest = (unsigned)dest >> 31 - exponent
372 
373 #define Dbl_isinexact_to_unsigned(dbl_valueA,dbl_valueB,exponent) \
374     Dbl_isinexact_to_fix(dbl_valueA,dbl_valueB,exponent)
375 
376 #define Duint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \
377     Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB)
378