summaryrefslogtreecommitdiff
path: root/arch/parisc/math-emu/cnv_float.h
blob: bfcd834f42d0686f01f63c79709ac33c599d3822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
 *
 * Floating-point emulation code
 *  Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
 */

#ifdef __NO_PA_HDRS
    PA header file -- do not include this header file for non-PA builds.
#endif

/*
 * Some more constants
 */
#define SGL_FX_MAX_EXP 30
#define DBL_FX_MAX_EXP 62
#define QUAD_FX_MAX_EXP 126

#define Dintp1(object) (object)
#define Dintp2(object) (object)

#define Duintp1(object) (object)
#define Duintp2(object) (object)

#define Qintp0(object) (object)
#define Qintp1(object) (object)
#define Qintp2(object) (object)
#define Qintp3(object) (object)


/*
 * These macros will be used specifically by the convert instructions.
 *
 *
 * Single format macros
 */

#define Sgl_to_dbl_exponent(src_exponent,dest)			\
    Deposit_dexponent(dest,src_exponent+(DBL_BIAS-SGL_BIAS))

#define Sgl_to_dbl_mantissa(src_mantissa,destA,destB)	\
    Deposit_dmantissap1(destA,src_mantissa>>3);		\
    Dmantissap2(destB) = src_mantissa << 29

#define Sgl_isinexact_to_fix(sgl_value,exponent)	\
    ((exponent < (SGL_P - 1)) ?				\
     (Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) : FALSE)

#define Int_isinexact_to_sgl(int_value)	(int_value << 33 - SGL_EXP_LENGTH)

#define Sgl_roundnearest_from_int(int_value,sgl_value)			\
    if (int_value & 1<<(SGL_EXP_LENGTH - 2))   /* round bit */		\
    	if ((int_value << 34 - SGL_EXP_LENGTH) || Slow(sgl_value))	\
		Sall(sgl_value)++

#define Dint_isinexact_to_sgl(dint_valueA,dint_valueB)		\
    ((Dintp1(dint_valueA) << 33 - SGL_EXP_LENGTH) || Dintp2(dint_valueB))

#define Sgl_roundnearest_from_dint(dint_valueA,dint_valueB,sgl_value)	\
    if (Dintp1(dint_valueA) & 1<<(SGL_EXP_LENGTH - 2)) 			\
    	if ((Dintp1(dint_valueA) << 34 - SGL_EXP_LENGTH) ||		\
    	Dintp2(dint_valueB) || Slow(sgl_value)) Sall(sgl_value)++

#define Dint_isinexact_to_dbl(dint_value) 	\
    (Dintp2(dint_value) << 33 - DBL_EXP_LENGTH)

#define Dbl_roundnearest_from_dint(dint_opndB,dbl_opndA,dbl_opndB) 	\
    if (Dintp2(dint_opndB) & 1<<(DBL_EXP_LENGTH - 2))			\
       if ((Dintp2(dint_opndB) << 34 - DBL_EXP_LENGTH) || Dlowp2(dbl_opndB))  \
          if ((++Dallp2(dbl_opndB))==0) Dallp1(dbl_opndA)++

#define Sgl_isone_roundbit(sgl_value,exponent)			\
    ((Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) >> 31)

#define Sgl_isone_stickybit(sgl_value,exponent)		\
    (exponent < (SGL_P - 2) ?				\
     Sall(sgl_value) << (SGL_EXP_LENGTH + 2 + exponent) : FALSE)


/* 
 * Double format macros
 */

#define Dbl_to_sgl_exponent(src_exponent,dest)			\
    dest = src_exponent + (SGL_BIAS - DBL_BIAS)

#define Dbl_to_sgl_mantissa(srcA,srcB,dest,inexact,guard,sticky,odd)	\
    Shiftdouble(Dmantissap1(srcA),Dmantissap2(srcB),29,dest); 	\
    guard = Dbit3p2(srcB);					\
    sticky = Dallp2(srcB)<<4;					\
    inexact = guard | sticky;					\
    odd = Dbit2p2(srcB)

#define Dbl_to_sgl_denormalized(srcA,srcB,exp,dest,inexact,guard,sticky,odd,tiny) \
    Deposit_dexponent(srcA,1);						\
    tiny = TRUE;							\
    if (exp >= -2) {							\
	if (exp == 0) {							\
	    inexact = Dallp2(srcB) << 3;				\
	    guard = inexact >> 31;					\
	    sticky = inexact << 1;					\
	    Shiftdouble(Dmantissap1(srcA),Dmantissap2(srcB),29,dest);	\
	    odd = dest << 31;						\
	    if (inexact) {						\
		switch(Rounding_mode()) {				\
		    case ROUNDPLUS:					\
			if (Dbl_iszero_sign(srcA)) {			\
			    dest++;					\
			    if (Sgl_isone_hidden(dest))	\
				tiny = FALSE;				\
			    dest--;					\
			}						\
			break;						\
		    case ROUNDMINUS:					\
			if (Dbl_isone_sign(srcA)) {			\
			    dest++;					\
			    if (Sgl_isone_hidden(dest))	\
				tiny = FALSE;				\
			    dest--;					\
			}						\
			break;						\
		    case ROUNDNEAREST:					\
			if (guard && (sticky || odd)) {			\
			    dest++;					\
			    if (Sgl_isone_hidden(dest))	\
				tiny = FALSE;				\
			    dest--;					\
			}						\
			break;						\
		}							\
	    }								\
		/* shift right by one to get correct result */		\
		guard = odd;						\
		sticky = inexact;					\
		inexact |= guard;					\
		dest >>= 1;						\
    		Deposit_dsign(srcA,0);					\
    	        Shiftdouble(Dallp1(srcA),Dallp2(srcB),30,dest);		\
	        odd = dest << 31;					\
	}								\
	else {								\
    	    inexact = Dallp2(srcB) << (2 + exp);			\
    	    guard = inexact >> 31;					\
    	    sticky = inexact << 1; 					\
    	    Deposit_dsign(srcA,0);					\
    	    if (exp == -2) dest = Dallp1(srcA);				\
    	    else Variable_shift_double(Dallp1(srcA),Dallp2(srcB),30-exp,dest); \
    	    odd = dest << 31;						\
	}								\
    }									\
    else {								\
    	Deposit_dsign(srcA,0);						\
    	if (exp > (1 - SGL_P)) {					\
    	    dest = Dallp1(srcA) >> (- 2 - exp);				\
    	    inexact = Dallp1(srcA) << (34 + exp);			\
    	    guard = inexact >> 31;					\
    	    sticky = (inexact << 1) | Dallp2(srcB);			\
    	    inexact |= Dallp2(srcB); 					\
    	    odd = dest << 31;						\
    	}								\
    	else {								\
    	    dest = 0;							\
    	    inexact = Dallp1(srcA) | Dallp2(srcB);			\
    	    if (exp == (1 - SGL_P)) {					\
    	    	guard = Dhidden(srcA);					\
    	    	sticky = Dmantissap1(srcA) | Dallp2(srcB); 		\
    	    }								\
    	    else {							\
    	    	guard = 0;						\
    	    	sticky = inexact;					\
    	    }								\
    	    odd = 0;							\
    	}								\
    }									\
    exp = 0

#define Dbl_isinexact_to_fix(dbl_valueA,dbl_valueB,exponent)		\
    (exponent < (DBL_P-33) ? 						\
     Dallp2(dbl_valueB) || Dallp1(dbl_valueA) << (DBL_EXP_LENGTH+1+exponent) : \
     (exponent < (DBL_P-1) ? Dallp2(dbl_valueB) << (exponent + (33-DBL_P)) :   \
      FALSE))

#define Dbl_isoverflow_to_int(exponent,dbl_valueA,dbl_valueB)		\
    ((exponent > SGL_FX_MAX_EXP + 1) || Dsign(dbl_valueA)==0 ||		\
     Dmantissap1(dbl_valueA)!=0 || (Dallp2(dbl_valueB)>>21)!=0 ) 

#define Dbl_isone_roundbit(dbl_valueA,dbl_valueB,exponent)              \
    ((exponent < (DBL_P - 33) ?						\
      Dallp1(dbl_valueA) >> ((30 - DBL_EXP_LENGTH) - exponent) :	\
      Dallp2(dbl_valueB) >> ((DBL_P - 2) - exponent)) & 1)

#define Dbl_isone_stickybit(dbl_valueA,dbl_valueB,exponent)		\
    (exponent < (DBL_P-34) ? 						\
     (Dallp2(dbl_valueB) || Dallp1(dbl_valueA)<<(DBL_EXP_LENGTH+2+exponent)) : \
     (exponent<(DBL_P-2) ? (Dallp2(dbl_valueB) << (exponent + (34-DBL_P))) : \
      FALSE))


/* Int macros */

#define Int_from_sgl_mantissa(sgl_value,exponent)	\
    Sall(sgl_value) = 				\
    	(unsigned)(Sall(sgl_value) << SGL_EXP_LENGTH)>>(31 - exponent)

#define Int_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent)	\
    Shiftdouble(Dallp1(dbl_valueA),Dallp2(dbl_valueB),22,Dallp1(dbl_valueA)); \
    if (exponent < 31) Dallp1(dbl_valueA) >>= 30 - exponent;	\
    else Dallp1(dbl_valueA) <<= 1

#define Int_negate(int_value) int_value = -int_value


/* Dint macros */

#define Dint_from_sgl_mantissa(sgl_value,exponent,dresultA,dresultB)	\
    {Sall(sgl_value) <<= SGL_EXP_LENGTH;  /*  left-justify  */		\
    if (exponent <= 31) {						\
    	Dintp1(dresultA) = 0;						\
    	Dintp2(dresultB) = (unsigned)Sall(sgl_value) >> (31 - exponent); \
    }									\
    else {								\
    	Dintp1(dresultA) = Sall(sgl_value) >> (63 - exponent);		\
    	Dintp2(dresultB) = Sall(sgl_value) << (exponent - 31);		\
    }}


#define Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \
    {if (exponent < 32) {						\
    	Dintp1(destA) = 0;						\
    	if (exponent <= 20)						\
    	    Dintp2(destB) = Dallp1(dbl_valueA) >> 20-exponent;		\
    	else Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
	     52-exponent,Dintp2(destB));					\
    }									\
    else {								\
    	if (exponent <= 52) {						\
    	    Dintp1(destA) = Dallp1(dbl_valueA) >> 52-exponent;		\
	    if (exponent == 52) Dintp2(destB) = Dallp2(dbl_valueB);	\
	    else Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
	    52-exponent,Dintp2(destB));					\
        }								\
    	else {								\
    	    Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
	    84-exponent,Dintp1(destA));					\
    	    Dintp2(destB) = Dallp2(dbl_valueB) << exponent-52;		\
    	}								\
    }}

#define Dint_setzero(dresultA,dresultB) 	\
    Dintp1(dresultA) = 0; 	\
    Dintp2(dresultB) = 0

#define Dint_setone_sign(dresultA,dresultB)		\
    Dintp1(dresultA) = ~Dintp1(dresultA);		\
    if ((Dintp2(dresultB) = -Dintp2(dresultB)) == 0) Dintp1(dresultA)++

#define Dint_set_minint(dresultA,dresultB)		\
    Dintp1(dresultA) = (unsigned int)1<<31;		\
    Dintp2(dresultB) = 0

#define Dint_isone_lowp2(dresultB)  (Dintp2(dresultB) & 01)

#define Dint_increment(dresultA,dresultB) 		\
    if ((++Dintp2(dresultB))==0) Dintp1(dresultA)++

#define Dint_decrement(dresultA,dresultB) 		\
    if ((Dintp2(dresultB)--)==0) Dintp1(dresultA)--

#define Dint_negate(dresultA,dresultB)			\
    Dintp1(dresultA) = ~Dintp1(dresultA);		\
    if ((Dintp2(dresultB) = -Dintp2(dresultB))==0) Dintp1(dresultA)++

#define Dint_copyfromptr(src,destA,destB) \
     Dintp1(destA) = src->wd0;		\
     Dintp2(destB) = src->wd1
#define Dint_copytoptr(srcA,srcB,dest)	\
    dest->wd0 = Dintp1(srcA);		\
    dest->wd1 = Dintp2(srcB)


/* other macros  */

#define Find_ms_one_bit(value, position)	\
    {						\
	int var;				\
	for (var=8; var >=1; var >>= 1) {	\
	    if (value >> 32 - position)		\
		position -= var;		\
		else position += var;		\
	}					\
	if ((value >> 32 - position) == 0)	\
	    position--;				\
	else position -= 2;			\
    }


/*
 * Unsigned int macros
 */
#define Duint_copyfromptr(src,destA,destB) \
    Dint_copyfromptr(src,destA,destB)
#define Duint_copytoptr(srcA,srcB,dest)	\
    Dint_copytoptr(srcA,srcB,dest)

#define Suint_isinexact_to_sgl(int_value) \
    (int_value << 32 - SGL_EXP_LENGTH)

#define Sgl_roundnearest_from_suint(suint_value,sgl_value)		\
    if (suint_value & 1<<(SGL_EXP_LENGTH - 1))   /* round bit */	\
    	if ((suint_value << 33 - SGL_EXP_LENGTH) || Slow(sgl_value))	\
		Sall(sgl_value)++

#define Duint_isinexact_to_sgl(duint_valueA,duint_valueB)	\
    ((Duintp1(duint_valueA) << 32 - SGL_EXP_LENGTH) || Duintp2(duint_valueB))

#define Sgl_roundnearest_from_duint(duint_valueA,duint_valueB,sgl_value) \
    if (Duintp1(duint_valueA) & 1<<(SGL_EXP_LENGTH - 1))		\
    	if ((Duintp1(duint_valueA) << 33 - SGL_EXP_LENGTH) ||		\
    	Duintp2(duint_valueB) || Slow(sgl_value)) Sall(sgl_value)++

#define Duint_isinexact_to_dbl(duint_value) 	\
    (Duintp2(duint_value) << 32 - DBL_EXP_LENGTH)

#define Dbl_roundnearest_from_duint(duint_opndB,dbl_opndA,dbl_opndB) 	\
    if (Duintp2(duint_opndB) & 1<<(DBL_EXP_LENGTH - 1))			\
       if ((Duintp2(duint_opndB) << 33 - DBL_EXP_LENGTH) || Dlowp2(dbl_opndB)) \
          if ((++Dallp2(dbl_opndB))==0) Dallp1(dbl_opndA)++

#define Suint_from_sgl_mantissa(src,exponent,result)	\
    Sall(result) = (unsigned)(Sall(src) << SGL_EXP_LENGTH)>>(31 - exponent)

#define Sgl_isinexact_to_unsigned(sgl_value,exponent)	\
    Sgl_isinexact_to_fix(sgl_value,exponent)

#define Duint_from_sgl_mantissa(sgl_value,exponent,dresultA,dresultB)	\
  {unsigned int val = Sall(sgl_value) << SGL_EXP_LENGTH;		\
    if (exponent <= 31) {						\
	Dintp1(dresultA) = 0;						\
	Dintp2(dresultB) = val >> (31 - exponent);			\
    }									\
    else {								\
	Dintp1(dresultA) = val >> (63 - exponent);			\
	Dintp2(dresultB) = exponent <= 62 ? val << (exponent - 31) : 0;	\
    }									\
  }

#define Duint_setzero(dresultA,dresultB) 	\
    Dint_setzero(dresultA,dresultB)

#define Duint_increment(dresultA,dresultB) Dint_increment(dresultA,dresultB) 

#define Duint_isone_lowp2(dresultB)  Dint_isone_lowp2(dresultB)

#define Suint_from_dbl_mantissa(srcA,srcB,exponent,dest) \
    Shiftdouble(Dallp1(srcA),Dallp2(srcB),21,dest); \
    dest = (unsigned)dest >> 31 - exponent

#define Dbl_isinexact_to_unsigned(dbl_valueA,dbl_valueB,exponent) \
    Dbl_isinexact_to_fix(dbl_valueA,dbl_valueB,exponent)

#define Duint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \
    Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB)