1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
5 * Floating-point emulation code
6 * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
12 * @(#) pa/spmath/fcnvfxt.c $Revision: 1.1 $
15 * Single Floating-point to Single Fixed-point /w truncated result
16 * Single Floating-point to Double Fixed-point /w truncated result
17 * Double Floating-point to Single Fixed-point /w truncated result
18 * Double Floating-point to Double Fixed-point /w truncated result
20 * External Interfaces:
21 * dbl_to_dbl_fcnvfxt(srcptr,nullptr,dstptr,status)
22 * dbl_to_sgl_fcnvfxt(srcptr,nullptr,dstptr,status)
23 * sgl_to_dbl_fcnvfxt(srcptr,nullptr,dstptr,status)
24 * sgl_to_sgl_fcnvfxt(srcptr,nullptr,dstptr,status)
26 * Internal Interfaces:
29 * <<please update with a overview of the operation of this file>>
36 #include "sgl_float.h"
37 #include "dbl_float.h"
38 #include "cnv_float.h"
41 * Convert single floating-point to single fixed-point format
42 * with truncated result
47 sgl_floating_point
*srcptr
,
48 unsigned int *nullptr,
52 register unsigned int src
, temp
;
53 register int src_exponent
, result
;
56 src_exponent
= Sgl_exponent(src
) - SGL_BIAS
;
61 if (src_exponent
> SGL_FX_MAX_EXP
) {
62 /* check for MININT */
63 if ((src_exponent
> SGL_FX_MAX_EXP
+ 1) ||
64 Sgl_isnotzero_mantissa(src
) || Sgl_iszero_sign(src
)) {
65 if (Sgl_iszero_sign(src
)) result
= 0x7fffffff;
66 else result
= 0x80000000;
68 if (Is_invalidtrap_enabled()) {
69 return(INVALIDEXCEPTION
);
79 if (src_exponent
>= 0) {
81 Sgl_clear_signexponent_set_hidden(temp
);
82 Int_from_sgl_mantissa(temp
,src_exponent
);
83 if (Sgl_isone_sign(src
)) result
= -Sgl_all(temp
);
84 else result
= Sgl_all(temp
);
87 /* check for inexact */
88 if (Sgl_isinexact_to_fix(src
,src_exponent
)) {
89 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
90 else Set_inexactflag();
96 /* check for inexact */
97 if (Sgl_isnotzero_exponentmantissa(src
)) {
98 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
99 else Set_inexactflag();
106 * Single Floating-point to Double Fixed-point
111 sgl_floating_point
*srcptr
,
112 unsigned int *nullptr,
114 unsigned int *status
)
116 register int src_exponent
, resultp1
;
117 register unsigned int src
, temp
, resultp2
;
120 src_exponent
= Sgl_exponent(src
) - SGL_BIAS
;
125 if (src_exponent
> DBL_FX_MAX_EXP
) {
126 /* check for MININT */
127 if ((src_exponent
> DBL_FX_MAX_EXP
+ 1) ||
128 Sgl_isnotzero_mantissa(src
) || Sgl_iszero_sign(src
)) {
129 if (Sgl_iszero_sign(src
)) {
130 resultp1
= 0x7fffffff;
131 resultp2
= 0xffffffff;
134 resultp1
= 0x80000000;
137 if (Is_invalidtrap_enabled()) {
138 return(INVALIDEXCEPTION
);
141 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
144 Dint_set_minint(resultp1
,resultp2
);
145 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
151 if (src_exponent
>= 0) {
153 Sgl_clear_signexponent_set_hidden(temp
);
154 Dint_from_sgl_mantissa(temp
,src_exponent
,resultp1
,resultp2
);
155 if (Sgl_isone_sign(src
)) {
156 Dint_setone_sign(resultp1
,resultp2
);
158 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
160 /* check for inexact */
161 if (Sgl_isinexact_to_fix(src
,src_exponent
)) {
162 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
163 else Set_inexactflag();
167 Dint_setzero(resultp1
,resultp2
);
168 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
170 /* check for inexact */
171 if (Sgl_isnotzero_exponentmantissa(src
)) {
172 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
173 else Set_inexactflag();
180 * Double Floating-point to Single Fixed-point
185 dbl_floating_point
*srcptr
,
186 unsigned int *nullptr,
188 unsigned int *status
)
190 register unsigned int srcp1
, srcp2
, tempp1
, tempp2
;
191 register int src_exponent
, result
;
193 Dbl_copyfromptr(srcptr
,srcp1
,srcp2
);
194 src_exponent
= Dbl_exponent(srcp1
) - DBL_BIAS
;
199 if (src_exponent
> SGL_FX_MAX_EXP
) {
200 /* check for MININT */
201 if (Dbl_isoverflow_to_int(src_exponent
,srcp1
,srcp2
)) {
202 if (Dbl_iszero_sign(srcp1
)) result
= 0x7fffffff;
203 else result
= 0x80000000;
205 if (Is_invalidtrap_enabled()) {
206 return(INVALIDEXCEPTION
);
216 if (src_exponent
>= 0) {
219 Dbl_clear_signexponent_set_hidden(tempp1
);
220 Int_from_dbl_mantissa(tempp1
,tempp2
,src_exponent
);
221 if (Dbl_isone_sign(srcp1
) && (src_exponent
<= SGL_FX_MAX_EXP
))
222 result
= -Dbl_allp1(tempp1
);
223 else result
= Dbl_allp1(tempp1
);
226 /* check for inexact */
227 if (Dbl_isinexact_to_fix(srcp1
,srcp2
,src_exponent
)) {
228 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
229 else Set_inexactflag();
235 /* check for inexact */
236 if (Dbl_isnotzero_exponentmantissa(srcp1
,srcp2
)) {
237 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
238 else Set_inexactflag();
245 * Double Floating-point to Double Fixed-point
250 dbl_floating_point
*srcptr
,
251 unsigned int *nullptr,
253 unsigned int *status
)
255 register int src_exponent
, resultp1
;
256 register unsigned int srcp1
, srcp2
, tempp1
, tempp2
, resultp2
;
258 Dbl_copyfromptr(srcptr
,srcp1
,srcp2
);
259 src_exponent
= Dbl_exponent(srcp1
) - DBL_BIAS
;
264 if (src_exponent
> DBL_FX_MAX_EXP
) {
265 /* check for MININT */
266 if ((src_exponent
> DBL_FX_MAX_EXP
+ 1) ||
267 Dbl_isnotzero_mantissa(srcp1
,srcp2
) || Dbl_iszero_sign(srcp1
)) {
268 if (Dbl_iszero_sign(srcp1
)) {
269 resultp1
= 0x7fffffff;
270 resultp2
= 0xffffffff;
273 resultp1
= 0x80000000;
276 if (Is_invalidtrap_enabled()) {
277 return(INVALIDEXCEPTION
);
280 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
287 if (src_exponent
>= 0) {
290 Dbl_clear_signexponent_set_hidden(tempp1
);
291 Dint_from_dbl_mantissa(tempp1
,tempp2
,src_exponent
,
293 if (Dbl_isone_sign(srcp1
)) {
294 Dint_setone_sign(resultp1
,resultp2
);
296 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
298 /* check for inexact */
299 if (Dbl_isinexact_to_fix(srcp1
,srcp2
,src_exponent
)) {
300 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
301 else Set_inexactflag();
305 Dint_setzero(resultp1
,resultp2
);
306 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
308 /* check for inexact */
309 if (Dbl_isnotzero_exponentmantissa(srcp1
,srcp2
)) {
310 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
311 else Set_inexactflag();