2 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
4 * Floating-point emulation code
5 * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
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)
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 PA header file -- do not include this header file for non-PA builds.
29 #define SGL_FX_MAX_EXP 30
30 #define DBL_FX_MAX_EXP 62
31 #define QUAD_FX_MAX_EXP 126
33 #define Dintp1(object) (object)
34 #define Dintp2(object) (object)
36 #define Duintp1(object) (object)
37 #define Duintp2(object) (object)
39 #define Qintp0(object) (object)
40 #define Qintp1(object) (object)
41 #define Qintp2(object) (object)
42 #define Qintp3(object) (object)
46 * These macros will be used specifically by the convert instructions.
49 * Single format macros
52 #define Sgl_to_dbl_exponent(src_exponent,dest) \
53 Deposit_dexponent(dest,src_exponent+(DBL_BIAS-SGL_BIAS))
55 #define Sgl_to_dbl_mantissa(src_mantissa,destA,destB) \
56 Deposit_dmantissap1(destA,src_mantissa>>3); \
57 Dmantissap2(destB) = src_mantissa << 29
59 #define Sgl_isinexact_to_fix(sgl_value,exponent) \
60 ((exponent < (SGL_P - 1)) ? \
61 (Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) : FALSE)
63 #define Int_isinexact_to_sgl(int_value) ((int_value << 33 - SGL_EXP_LENGTH) != 0)
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) != 0) || Slow(sgl_value)) \
70 #define Dint_isinexact_to_sgl(dint_valueA,dint_valueB) \
71 (((Dintp1(dint_valueA) << 33 - SGL_EXP_LENGTH) != 0) || Dintp2(dint_valueB))
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) != 0) || \
76 Dintp2(dint_valueB) || Slow(sgl_value)) Sall(sgl_value)++
78 #define Dint_isinexact_to_dbl(dint_value) \
79 (Dintp2(dint_value) << 33 - DBL_EXP_LENGTH)
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)++
86 #define Sgl_isone_roundbit(sgl_value,exponent) \
87 ((Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) >> 31)
89 #define Sgl_isone_stickybit(sgl_value,exponent) \
90 (exponent < (SGL_P - 2) ? \
91 Sall(sgl_value) << (SGL_EXP_LENGTH + 2 + exponent) : FALSE)
95 * Double format macros
98 #define Dbl_to_sgl_exponent(src_exponent,dest) \
99 dest = src_exponent + (SGL_BIAS - DBL_BIAS)
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; \
108 #define Dbl_to_sgl_denormalized(srcA,srcB,exp,dest,inexact,guard,sticky,odd,tiny) \
109 Deposit_dexponent(srcA,1); \
113 inexact = Dallp2(srcB) << 3; \
114 guard = inexact >> 31; \
115 sticky = inexact << 1; \
116 Shiftdouble(Dmantissap1(srcA),Dmantissap2(srcB),29,dest); \
119 switch(Rounding_mode()) { \
121 if (Dbl_iszero_sign(srcA)) { \
123 if (Sgl_isone_hidden(dest)) \
129 if (Dbl_isone_sign(srcA)) { \
131 if (Sgl_isone_hidden(dest)) \
137 if (guard && (sticky || odd)) { \
139 if (Sgl_isone_hidden(dest)) \
146 /* shift right by one to get correct result */ \
151 Deposit_dsign(srcA,0); \
152 Shiftdouble(Dallp1(srcA),Dallp2(srcB),30,dest); \
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); \
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); \
177 inexact = Dallp1(srcA) | Dallp2(srcB); \
178 if (exp == (1 - SGL_P)) { \
179 guard = Dhidden(srcA); \
180 sticky = Dmantissap1(srcA) | Dallp2(srcB); \
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)) : \
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 )
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)
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))) : \
215 #define Int_from_sgl_mantissa(sgl_value,exponent) \
217 (unsigned)(Sall(sgl_value) << SGL_EXP_LENGTH)>>(31 - exponent)
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
224 #define Int_negate(int_value) int_value = -int_value
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); \
236 Dintp1(dresultA) = Sall(sgl_value) >> (63 - exponent); \
237 Dintp2(dresultB) = Sall(sgl_value) << (exponent - 31); \
241 #define Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \
242 {if (exponent < 32) { \
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)); \
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)); \
257 Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
258 84-exponent,Dintp1(destA)); \
259 Dintp2(destB) = Dallp2(dbl_valueB) << exponent-52; \
263 #define Dint_setzero(dresultA,dresultB) \
264 Dintp1(dresultA) = 0; \
267 #define Dint_setone_sign(dresultA,dresultB) \
268 Dintp1(dresultA) = ~Dintp1(dresultA); \
269 if ((Dintp2(dresultB) = -Dintp2(dresultB)) == 0) Dintp1(dresultA)++
271 #define Dint_set_minint(dresultA,dresultB) \
272 Dintp1(dresultA) = (unsigned int)1<<31; \
275 #define Dint_isone_lowp2(dresultB) (Dintp2(dresultB) & 01)
277 #define Dint_increment(dresultA,dresultB) \
278 if ((++Dintp2(dresultB))==0) Dintp1(dresultA)++
280 #define Dint_decrement(dresultA,dresultB) \
281 if ((Dintp2(dresultB)--)==0) Dintp1(dresultA)--
283 #define Dint_negate(dresultA,dresultB) \
284 Dintp1(dresultA) = ~Dintp1(dresultA); \
285 if ((Dintp2(dresultB) = -Dintp2(dresultB))==0) Dintp1(dresultA)++
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)
297 #define Find_ms_one_bit(value, position) \
300 for (var=8; var >=1; var >>= 1) { \
301 if (value >> 32 - position) \
303 else position += var; \
305 if ((value >> 32 - position) == 0) \
307 else position -= 2; \
312 * Unsigned int macros
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)
319 #define Suint_isinexact_to_sgl(int_value) \
320 (int_value << 32 - SGL_EXP_LENGTH)
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)) \
327 #define Duint_isinexact_to_sgl(duint_valueA,duint_valueB) \
328 ((Duintp1(duint_valueA) << 32 - SGL_EXP_LENGTH) || Duintp2(duint_valueB))
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)++
335 #define Duint_isinexact_to_dbl(duint_value) \
336 (Duintp2(duint_value) << 32 - DBL_EXP_LENGTH)
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)++
343 #define Suint_from_sgl_mantissa(src,exponent,result) \
344 Sall(result) = (unsigned)(Sall(src) << SGL_EXP_LENGTH)>>(31 - exponent)
346 #define Sgl_isinexact_to_unsigned(sgl_value,exponent) \
347 Sgl_isinexact_to_fix(sgl_value,exponent)
349 #define Duint_from_sgl_mantissa(sgl_value,exponent,dresultA,dresultB) \
350 {unsigned int val = Sall(sgl_value) << SGL_EXP_LENGTH; \
351 if (exponent <= 31) { \
352 Dintp1(dresultA) = 0; \
353 Dintp2(dresultB) = val >> (31 - exponent); \
356 Dintp1(dresultA) = val >> (63 - exponent); \
357 Dintp2(dresultB) = exponent <= 62 ? val << (exponent - 31) : 0; \
361 #define Duint_setzero(dresultA,dresultB) \
362 Dint_setzero(dresultA,dresultB)
364 #define Duint_increment(dresultA,dresultB) Dint_increment(dresultA,dresultB)
366 #define Duint_isone_lowp2(dresultB) Dint_isone_lowp2(dresultB)
368 #define Suint_from_dbl_mantissa(srcA,srcB,exponent,dest) \
369 Shiftdouble(Dallp1(srcA),Dallp2(srcB),21,dest); \
370 dest = (unsigned)dest >> 31 - exponent
372 #define Dbl_isinexact_to_unsigned(dbl_valueA,dbl_valueB,exponent) \
373 Dbl_isinexact_to_fix(dbl_valueA,dbl_valueB,exponent)
375 #define Duint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \
376 Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB)