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
25 * Single Floating-point Round to Integer
26 * Double Floating-point Round to Integer
27 * Quad Floating-point Round to Integer (returns unimplemented)
29 * External Interfaces:
30 * dbl_frnd(srcptr,nullptr,dstptr,status)
31 * sgl_frnd(srcptr,nullptr,dstptr,status)
38 #include "sgl_float.h"
39 #include "dbl_float.h"
40 #include "cnv_float.h"
43 * Single Floating-point Round to Integer
48 sgl_frnd(sgl_floating_point
*srcptr
,
49 unsigned int *nullptr,
50 sgl_floating_point
*dstptr
,
53 register unsigned int src
, result
;
54 register int src_exponent
;
55 register boolean inexact
= FALSE
;
59 * check source operand for NaN or infinity
61 if ((src_exponent
= Sgl_exponent(src
)) == SGL_INFINITY_EXPONENT
) {
65 if (Sgl_isone_signaling(src
)) {
66 /* trap if INVALIDTRAP enabled */
67 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
73 * return quiet NaN or infinity
81 if ((src_exponent
-= SGL_BIAS
) >= SGL_P
- 1) {
88 if (src_exponent
>= 0) {
89 Sgl_clear_exponent_set_hidden(src
);
91 Sgl_rightshift(result
,(SGL_P
-1) - (src_exponent
));
92 /* check for inexact */
93 if (Sgl_isinexact_to_fix(src
,src_exponent
)) {
96 switch (Rounding_mode()) {
98 if (Sgl_iszero_sign(src
)) Sgl_increment(result
);
101 if (Sgl_isone_sign(src
)) Sgl_increment(result
);
104 if (Sgl_isone_roundbit(src
,src_exponent
))
105 if (Sgl_isone_stickybit(src
,src_exponent
)
106 || (Sgl_isone_lowmantissa(result
)))
107 Sgl_increment(result
);
110 Sgl_leftshift(result
,(SGL_P
-1) - (src_exponent
));
111 if (Sgl_isone_hiddenoverflow(result
))
112 Sgl_set_exponent(result
,src_exponent
+ (SGL_BIAS
+1));
113 else Sgl_set_exponent(result
,src_exponent
+ SGL_BIAS
);
116 result
= src
; /* set sign */
117 Sgl_setzero_exponentmantissa(result
);
118 /* check for inexact */
119 if (Sgl_isnotzero_exponentmantissa(src
)) {
122 switch (Rounding_mode()) {
124 if (Sgl_iszero_sign(src
))
125 Sgl_set_exponent(result
,SGL_BIAS
);
128 if (Sgl_isone_sign(src
))
129 Sgl_set_exponent(result
,SGL_BIAS
);
132 if (src_exponent
== -1)
133 if (Sgl_isnotzero_mantissa(src
))
134 Sgl_set_exponent(result
,SGL_BIAS
);
140 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
141 else Set_inexactflag();
147 * Double Floating-point Round to Integer
153 dbl_floating_point
*srcptr
,
154 unsigned int *nullptr,
155 dbl_floating_point
*dstptr
,
156 unsigned int *status
)
158 register unsigned int srcp1
, srcp2
, resultp1
, resultp2
;
159 register int src_exponent
;
160 register boolean inexact
= FALSE
;
162 Dbl_copyfromptr(srcptr
,srcp1
,srcp2
);
164 * check source operand for NaN or infinity
166 if ((src_exponent
= Dbl_exponent(srcp1
)) == DBL_INFINITY_EXPONENT
) {
170 if (Dbl_isone_signaling(srcp1
)) {
171 /* trap if INVALIDTRAP enabled */
172 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
175 Dbl_set_quiet(srcp1
);
178 * return quiet NaN or infinity
180 Dbl_copytoptr(srcp1
,srcp2
,dstptr
);
186 if ((src_exponent
-= DBL_BIAS
) >= DBL_P
- 1) {
187 Dbl_copytoptr(srcp1
,srcp2
,dstptr
);
193 if (src_exponent
>= 0) {
194 Dbl_clear_exponent_set_hidden(srcp1
);
197 Dbl_rightshift(resultp1
,resultp2
,(DBL_P
-1) - (src_exponent
));
198 /* check for inexact */
199 if (Dbl_isinexact_to_fix(srcp1
,srcp2
,src_exponent
)) {
202 switch (Rounding_mode()) {
204 if (Dbl_iszero_sign(srcp1
))
205 Dbl_increment(resultp1
,resultp2
);
208 if (Dbl_isone_sign(srcp1
))
209 Dbl_increment(resultp1
,resultp2
);
212 if (Dbl_isone_roundbit(srcp1
,srcp2
,src_exponent
))
213 if (Dbl_isone_stickybit(srcp1
,srcp2
,src_exponent
)
214 || (Dbl_isone_lowmantissap2(resultp2
)))
215 Dbl_increment(resultp1
,resultp2
);
218 Dbl_leftshift(resultp1
,resultp2
,(DBL_P
-1) - (src_exponent
));
219 if (Dbl_isone_hiddenoverflow(resultp1
))
220 Dbl_set_exponent(resultp1
,src_exponent
+ (DBL_BIAS
+1));
221 else Dbl_set_exponent(resultp1
,src_exponent
+ DBL_BIAS
);
224 resultp1
= srcp1
; /* set sign */
225 Dbl_setzero_exponentmantissa(resultp1
,resultp2
);
226 /* check for inexact */
227 if (Dbl_isnotzero_exponentmantissa(srcp1
,srcp2
)) {
230 switch (Rounding_mode()) {
232 if (Dbl_iszero_sign(srcp1
))
233 Dbl_set_exponent(resultp1
,DBL_BIAS
);
236 if (Dbl_isone_sign(srcp1
))
237 Dbl_set_exponent(resultp1
,DBL_BIAS
);
240 if (src_exponent
== -1)
241 if (Dbl_isnotzero_mantissa(srcp1
,srcp2
))
242 Dbl_set_exponent(resultp1
,DBL_BIAS
);
246 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
248 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
249 else Set_inexactflag();