xref: /OK3568_Linux_fs/kernel/arch/parisc/math-emu/fcnvfxt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Linux/PA-RISC Project (http://www.parisc-linux.org/)
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Floating-point emulation code
6*4882a593Smuzhiyun  *  Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * BEGIN_DESC
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *  File:
12*4882a593Smuzhiyun  *	@(#)	pa/spmath/fcnvfxt.c		$Revision: 1.1 $
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *  Purpose:
15*4882a593Smuzhiyun  *	Single Floating-point to Single Fixed-point /w truncated result
16*4882a593Smuzhiyun  *	Single Floating-point to Double Fixed-point /w truncated result
17*4882a593Smuzhiyun  *	Double Floating-point to Single Fixed-point /w truncated result
18*4882a593Smuzhiyun  *	Double Floating-point to Double Fixed-point /w truncated result
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *  External Interfaces:
21*4882a593Smuzhiyun  *	dbl_to_dbl_fcnvfxt(srcptr,nullptr,dstptr,status)
22*4882a593Smuzhiyun  *	dbl_to_sgl_fcnvfxt(srcptr,nullptr,dstptr,status)
23*4882a593Smuzhiyun  *	sgl_to_dbl_fcnvfxt(srcptr,nullptr,dstptr,status)
24*4882a593Smuzhiyun  *	sgl_to_sgl_fcnvfxt(srcptr,nullptr,dstptr,status)
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  *  Internal Interfaces:
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  *  Theory:
29*4882a593Smuzhiyun  *	<<please update with a overview of the operation of this file>>
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * END_DESC
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include "float.h"
36*4882a593Smuzhiyun #include "sgl_float.h"
37*4882a593Smuzhiyun #include "dbl_float.h"
38*4882a593Smuzhiyun #include "cnv_float.h"
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun  *  Convert single floating-point to single fixed-point format
42*4882a593Smuzhiyun  *  with truncated result
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun /*ARGSUSED*/
45*4882a593Smuzhiyun int
sgl_to_sgl_fcnvfxt(sgl_floating_point * srcptr,unsigned int * nullptr,int * dstptr,unsigned int * status)46*4882a593Smuzhiyun sgl_to_sgl_fcnvfxt(
47*4882a593Smuzhiyun 		    sgl_floating_point *srcptr,
48*4882a593Smuzhiyun 		    unsigned int *nullptr,
49*4882a593Smuzhiyun 		    int *dstptr,
50*4882a593Smuzhiyun 		    unsigned int *status)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	register unsigned int src, temp;
53*4882a593Smuzhiyun 	register int src_exponent, result;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	src = *srcptr;
56*4882a593Smuzhiyun 	src_exponent = Sgl_exponent(src) - SGL_BIAS;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	/*
59*4882a593Smuzhiyun 	 * Test for overflow
60*4882a593Smuzhiyun 	 */
61*4882a593Smuzhiyun 	if (src_exponent > SGL_FX_MAX_EXP) {
62*4882a593Smuzhiyun 		/* check for MININT */
63*4882a593Smuzhiyun 		if ((src_exponent > SGL_FX_MAX_EXP + 1) ||
64*4882a593Smuzhiyun 		Sgl_isnotzero_mantissa(src) || Sgl_iszero_sign(src)) {
65*4882a593Smuzhiyun                         if (Sgl_iszero_sign(src)) result = 0x7fffffff;
66*4882a593Smuzhiyun                         else result = 0x80000000;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	                if (Is_invalidtrap_enabled()) {
69*4882a593Smuzhiyun                             return(INVALIDEXCEPTION);
70*4882a593Smuzhiyun                         }
71*4882a593Smuzhiyun                         Set_invalidflag();
72*4882a593Smuzhiyun 			*dstptr = result;
73*4882a593Smuzhiyun 			return(NOEXCEPTION);
74*4882a593Smuzhiyun 		}
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 	/*
77*4882a593Smuzhiyun 	 * Generate result
78*4882a593Smuzhiyun 	 */
79*4882a593Smuzhiyun 	if (src_exponent >= 0) {
80*4882a593Smuzhiyun 		temp = src;
81*4882a593Smuzhiyun 		Sgl_clear_signexponent_set_hidden(temp);
82*4882a593Smuzhiyun 		Int_from_sgl_mantissa(temp,src_exponent);
83*4882a593Smuzhiyun 		if (Sgl_isone_sign(src))  result = -Sgl_all(temp);
84*4882a593Smuzhiyun 		else result = Sgl_all(temp);
85*4882a593Smuzhiyun 		*dstptr = result;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 		/* check for inexact */
88*4882a593Smuzhiyun 		if (Sgl_isinexact_to_fix(src,src_exponent)) {
89*4882a593Smuzhiyun 			if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
90*4882a593Smuzhiyun 			else Set_inexactflag();
91*4882a593Smuzhiyun 		}
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 	else {
94*4882a593Smuzhiyun 		*dstptr = 0;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 		/* check for inexact */
97*4882a593Smuzhiyun 		if (Sgl_isnotzero_exponentmantissa(src)) {
98*4882a593Smuzhiyun 			if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
99*4882a593Smuzhiyun 			else Set_inexactflag();
100*4882a593Smuzhiyun 		}
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 	return(NOEXCEPTION);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun  *  Single Floating-point to Double Fixed-point
107*4882a593Smuzhiyun  */
108*4882a593Smuzhiyun /*ARGSUSED*/
109*4882a593Smuzhiyun int
sgl_to_dbl_fcnvfxt(sgl_floating_point * srcptr,unsigned int * nullptr,dbl_integer * dstptr,unsigned int * status)110*4882a593Smuzhiyun sgl_to_dbl_fcnvfxt(
111*4882a593Smuzhiyun 		    sgl_floating_point *srcptr,
112*4882a593Smuzhiyun 		    unsigned int *nullptr,
113*4882a593Smuzhiyun 		    dbl_integer *dstptr,
114*4882a593Smuzhiyun 		    unsigned int *status)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	register int src_exponent, resultp1;
117*4882a593Smuzhiyun 	register unsigned int src, temp, resultp2;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	src = *srcptr;
120*4882a593Smuzhiyun 	src_exponent = Sgl_exponent(src) - SGL_BIAS;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	/*
123*4882a593Smuzhiyun 	 * Test for overflow
124*4882a593Smuzhiyun 	 */
125*4882a593Smuzhiyun 	if (src_exponent > DBL_FX_MAX_EXP) {
126*4882a593Smuzhiyun 		/* check for MININT */
127*4882a593Smuzhiyun 		if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
128*4882a593Smuzhiyun 		Sgl_isnotzero_mantissa(src) || Sgl_iszero_sign(src)) {
129*4882a593Smuzhiyun                         if (Sgl_iszero_sign(src)) {
130*4882a593Smuzhiyun                               resultp1 = 0x7fffffff;
131*4882a593Smuzhiyun 			      resultp2 = 0xffffffff;
132*4882a593Smuzhiyun 			}
133*4882a593Smuzhiyun                         else {
134*4882a593Smuzhiyun 			    resultp1 = 0x80000000;
135*4882a593Smuzhiyun 			    resultp2 = 0;
136*4882a593Smuzhiyun 			}
137*4882a593Smuzhiyun 	                if (Is_invalidtrap_enabled()) {
138*4882a593Smuzhiyun                             return(INVALIDEXCEPTION);
139*4882a593Smuzhiyun                         }
140*4882a593Smuzhiyun                         Set_invalidflag();
141*4882a593Smuzhiyun     		        Dint_copytoptr(resultp1,resultp2,dstptr);
142*4882a593Smuzhiyun 			return(NOEXCEPTION);
143*4882a593Smuzhiyun 		}
144*4882a593Smuzhiyun 		Dint_set_minint(resultp1,resultp2);
145*4882a593Smuzhiyun 		Dint_copytoptr(resultp1,resultp2,dstptr);
146*4882a593Smuzhiyun 		return(NOEXCEPTION);
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun 	/*
149*4882a593Smuzhiyun 	 * Generate result
150*4882a593Smuzhiyun 	 */
151*4882a593Smuzhiyun 	if (src_exponent >= 0) {
152*4882a593Smuzhiyun 		temp = src;
153*4882a593Smuzhiyun 		Sgl_clear_signexponent_set_hidden(temp);
154*4882a593Smuzhiyun 		Dint_from_sgl_mantissa(temp,src_exponent,resultp1,resultp2);
155*4882a593Smuzhiyun 		if (Sgl_isone_sign(src)) {
156*4882a593Smuzhiyun 			Dint_setone_sign(resultp1,resultp2);
157*4882a593Smuzhiyun 		}
158*4882a593Smuzhiyun 		Dint_copytoptr(resultp1,resultp2,dstptr);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 		/* check for inexact */
161*4882a593Smuzhiyun 		if (Sgl_isinexact_to_fix(src,src_exponent)) {
162*4882a593Smuzhiyun 			if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
163*4882a593Smuzhiyun 			else Set_inexactflag();
164*4882a593Smuzhiyun 		}
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun 	else {
167*4882a593Smuzhiyun 		Dint_setzero(resultp1,resultp2);
168*4882a593Smuzhiyun 		Dint_copytoptr(resultp1,resultp2,dstptr);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 		/* check for inexact */
171*4882a593Smuzhiyun 		if (Sgl_isnotzero_exponentmantissa(src)) {
172*4882a593Smuzhiyun 			if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
173*4882a593Smuzhiyun 			else Set_inexactflag();
174*4882a593Smuzhiyun 		}
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 	return(NOEXCEPTION);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun  *  Double Floating-point to Single Fixed-point
181*4882a593Smuzhiyun  */
182*4882a593Smuzhiyun /*ARGSUSED*/
183*4882a593Smuzhiyun int
dbl_to_sgl_fcnvfxt(dbl_floating_point * srcptr,unsigned int * nullptr,int * dstptr,unsigned int * status)184*4882a593Smuzhiyun dbl_to_sgl_fcnvfxt(
185*4882a593Smuzhiyun 			dbl_floating_point *srcptr,
186*4882a593Smuzhiyun 			unsigned int *nullptr,
187*4882a593Smuzhiyun 			int *dstptr,
188*4882a593Smuzhiyun 			unsigned int *status)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	register unsigned int srcp1, srcp2, tempp1, tempp2;
191*4882a593Smuzhiyun 	register int src_exponent, result;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	Dbl_copyfromptr(srcptr,srcp1,srcp2);
194*4882a593Smuzhiyun 	src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	/*
197*4882a593Smuzhiyun 	 * Test for overflow
198*4882a593Smuzhiyun 	 */
199*4882a593Smuzhiyun 	if (src_exponent > SGL_FX_MAX_EXP) {
200*4882a593Smuzhiyun 		/* check for MININT */
201*4882a593Smuzhiyun 		if (Dbl_isoverflow_to_int(src_exponent,srcp1,srcp2)) {
202*4882a593Smuzhiyun                         if (Dbl_iszero_sign(srcp1)) result = 0x7fffffff;
203*4882a593Smuzhiyun                         else result = 0x80000000;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	                if (Is_invalidtrap_enabled()) {
206*4882a593Smuzhiyun                             return(INVALIDEXCEPTION);
207*4882a593Smuzhiyun                         }
208*4882a593Smuzhiyun                         Set_invalidflag();
209*4882a593Smuzhiyun 			*dstptr = result;
210*4882a593Smuzhiyun 			return(NOEXCEPTION);
211*4882a593Smuzhiyun 		}
212*4882a593Smuzhiyun 	}
213*4882a593Smuzhiyun 	/*
214*4882a593Smuzhiyun 	 * Generate result
215*4882a593Smuzhiyun 	 */
216*4882a593Smuzhiyun 	if (src_exponent >= 0) {
217*4882a593Smuzhiyun 		tempp1 = srcp1;
218*4882a593Smuzhiyun 		tempp2 = srcp2;
219*4882a593Smuzhiyun 		Dbl_clear_signexponent_set_hidden(tempp1);
220*4882a593Smuzhiyun 		Int_from_dbl_mantissa(tempp1,tempp2,src_exponent);
221*4882a593Smuzhiyun 		if (Dbl_isone_sign(srcp1) && (src_exponent <= SGL_FX_MAX_EXP))
222*4882a593Smuzhiyun 			result = -Dbl_allp1(tempp1);
223*4882a593Smuzhiyun 		else result = Dbl_allp1(tempp1);
224*4882a593Smuzhiyun 		*dstptr = result;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 		/* check for inexact */
227*4882a593Smuzhiyun 		if (Dbl_isinexact_to_fix(srcp1,srcp2,src_exponent)) {
228*4882a593Smuzhiyun 			if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
229*4882a593Smuzhiyun 			else Set_inexactflag();
230*4882a593Smuzhiyun 		}
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 	else {
233*4882a593Smuzhiyun 		*dstptr = 0;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 		/* check for inexact */
236*4882a593Smuzhiyun 		if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
237*4882a593Smuzhiyun 			if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
238*4882a593Smuzhiyun 			else Set_inexactflag();
239*4882a593Smuzhiyun 		}
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 	return(NOEXCEPTION);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun /*
245*4882a593Smuzhiyun  *  Double Floating-point to Double Fixed-point
246*4882a593Smuzhiyun  */
247*4882a593Smuzhiyun /*ARGSUSED*/
248*4882a593Smuzhiyun int
dbl_to_dbl_fcnvfxt(dbl_floating_point * srcptr,unsigned int * nullptr,dbl_integer * dstptr,unsigned int * status)249*4882a593Smuzhiyun dbl_to_dbl_fcnvfxt(
250*4882a593Smuzhiyun 			dbl_floating_point *srcptr,
251*4882a593Smuzhiyun 			unsigned int *nullptr,
252*4882a593Smuzhiyun 			dbl_integer *dstptr,
253*4882a593Smuzhiyun 			unsigned int *status)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	register int src_exponent, resultp1;
256*4882a593Smuzhiyun 	register unsigned int srcp1, srcp2, tempp1, tempp2, resultp2;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	Dbl_copyfromptr(srcptr,srcp1,srcp2);
259*4882a593Smuzhiyun 	src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/*
262*4882a593Smuzhiyun 	 * Test for overflow
263*4882a593Smuzhiyun 	 */
264*4882a593Smuzhiyun 	if (src_exponent > DBL_FX_MAX_EXP) {
265*4882a593Smuzhiyun 		/* check for MININT */
266*4882a593Smuzhiyun 		if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
267*4882a593Smuzhiyun 		Dbl_isnotzero_mantissa(srcp1,srcp2) || Dbl_iszero_sign(srcp1)) {
268*4882a593Smuzhiyun                         if (Dbl_iszero_sign(srcp1)) {
269*4882a593Smuzhiyun                               resultp1 = 0x7fffffff;
270*4882a593Smuzhiyun 			      resultp2 = 0xffffffff;
271*4882a593Smuzhiyun 			}
272*4882a593Smuzhiyun                         else {
273*4882a593Smuzhiyun 			    resultp1 = 0x80000000;
274*4882a593Smuzhiyun 			    resultp2 = 0;
275*4882a593Smuzhiyun 			}
276*4882a593Smuzhiyun 	                if (Is_invalidtrap_enabled()) {
277*4882a593Smuzhiyun                             return(INVALIDEXCEPTION);
278*4882a593Smuzhiyun                         }
279*4882a593Smuzhiyun                         Set_invalidflag();
280*4882a593Smuzhiyun     		        Dint_copytoptr(resultp1,resultp2,dstptr);
281*4882a593Smuzhiyun 			return(NOEXCEPTION);
282*4882a593Smuzhiyun 		}
283*4882a593Smuzhiyun 	}
284*4882a593Smuzhiyun 	/*
285*4882a593Smuzhiyun 	 * Generate result
286*4882a593Smuzhiyun 	 */
287*4882a593Smuzhiyun 	if (src_exponent >= 0) {
288*4882a593Smuzhiyun 		tempp1 = srcp1;
289*4882a593Smuzhiyun 		tempp2 = srcp2;
290*4882a593Smuzhiyun 		Dbl_clear_signexponent_set_hidden(tempp1);
291*4882a593Smuzhiyun 		Dint_from_dbl_mantissa(tempp1,tempp2,src_exponent,
292*4882a593Smuzhiyun 		resultp1,resultp2);
293*4882a593Smuzhiyun 		if (Dbl_isone_sign(srcp1)) {
294*4882a593Smuzhiyun 			Dint_setone_sign(resultp1,resultp2);
295*4882a593Smuzhiyun 		}
296*4882a593Smuzhiyun 		Dint_copytoptr(resultp1,resultp2,dstptr);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 		/* check for inexact */
299*4882a593Smuzhiyun 		if (Dbl_isinexact_to_fix(srcp1,srcp2,src_exponent)) {
300*4882a593Smuzhiyun 			if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
301*4882a593Smuzhiyun 			else Set_inexactflag();
302*4882a593Smuzhiyun 		}
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 	else {
305*4882a593Smuzhiyun 		Dint_setzero(resultp1,resultp2);
306*4882a593Smuzhiyun 		Dint_copytoptr(resultp1,resultp2,dstptr);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 		/* check for inexact */
309*4882a593Smuzhiyun 		if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
310*4882a593Smuzhiyun 			if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
311*4882a593Smuzhiyun 			else Set_inexactflag();
312*4882a593Smuzhiyun 		}
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun 	return(NOEXCEPTION);
315*4882a593Smuzhiyun }
316