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 * Single Floating-point Round to Integer
13 * Double Floating-point Round to Integer
14 * Quad Floating-point Round to Integer (returns unimplemented)
16 * External Interfaces:
17 * dbl_frnd(srcptr,nullptr,dstptr,status)
18 * sgl_frnd(srcptr,nullptr,dstptr,status)
25 #include "sgl_float.h"
26 #include "dbl_float.h"
27 #include "cnv_float.h"
30 * Single Floating-point Round to Integer
35 sgl_frnd(sgl_floating_point
*srcptr
,
36 unsigned int *nullptr,
37 sgl_floating_point
*dstptr
,
40 register unsigned int src
, result
;
41 register int src_exponent
;
42 register boolean inexact
= FALSE
;
46 * check source operand for NaN or infinity
48 if ((src_exponent
= Sgl_exponent(src
)) == SGL_INFINITY_EXPONENT
) {
52 if (Sgl_isone_signaling(src
)) {
53 /* trap if INVALIDTRAP enabled */
54 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
60 * return quiet NaN or infinity
68 if ((src_exponent
-= SGL_BIAS
) >= SGL_P
- 1) {
75 if (src_exponent
>= 0) {
76 Sgl_clear_exponent_set_hidden(src
);
78 Sgl_rightshift(result
,(SGL_P
-1) - (src_exponent
));
79 /* check for inexact */
80 if (Sgl_isinexact_to_fix(src
,src_exponent
)) {
83 switch (Rounding_mode()) {
85 if (Sgl_iszero_sign(src
)) Sgl_increment(result
);
88 if (Sgl_isone_sign(src
)) Sgl_increment(result
);
91 if (Sgl_isone_roundbit(src
,src_exponent
))
92 if (Sgl_isone_stickybit(src
,src_exponent
)
93 || (Sgl_isone_lowmantissa(result
)))
94 Sgl_increment(result
);
97 Sgl_leftshift(result
,(SGL_P
-1) - (src_exponent
));
98 if (Sgl_isone_hiddenoverflow(result
))
99 Sgl_set_exponent(result
,src_exponent
+ (SGL_BIAS
+1));
100 else Sgl_set_exponent(result
,src_exponent
+ SGL_BIAS
);
103 result
= src
; /* set sign */
104 Sgl_setzero_exponentmantissa(result
);
105 /* check for inexact */
106 if (Sgl_isnotzero_exponentmantissa(src
)) {
109 switch (Rounding_mode()) {
111 if (Sgl_iszero_sign(src
))
112 Sgl_set_exponent(result
,SGL_BIAS
);
115 if (Sgl_isone_sign(src
))
116 Sgl_set_exponent(result
,SGL_BIAS
);
119 if (src_exponent
== -1)
120 if (Sgl_isnotzero_mantissa(src
))
121 Sgl_set_exponent(result
,SGL_BIAS
);
127 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
128 else Set_inexactflag();
134 * Double Floating-point Round to Integer
140 dbl_floating_point
*srcptr
,
141 unsigned int *nullptr,
142 dbl_floating_point
*dstptr
,
143 unsigned int *status
)
145 register unsigned int srcp1
, srcp2
, resultp1
, resultp2
;
146 register int src_exponent
;
147 register boolean inexact
= FALSE
;
149 Dbl_copyfromptr(srcptr
,srcp1
,srcp2
);
151 * check source operand for NaN or infinity
153 if ((src_exponent
= Dbl_exponent(srcp1
)) == DBL_INFINITY_EXPONENT
) {
157 if (Dbl_isone_signaling(srcp1
)) {
158 /* trap if INVALIDTRAP enabled */
159 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
162 Dbl_set_quiet(srcp1
);
165 * return quiet NaN or infinity
167 Dbl_copytoptr(srcp1
,srcp2
,dstptr
);
173 if ((src_exponent
-= DBL_BIAS
) >= DBL_P
- 1) {
174 Dbl_copytoptr(srcp1
,srcp2
,dstptr
);
180 if (src_exponent
>= 0) {
181 Dbl_clear_exponent_set_hidden(srcp1
);
184 Dbl_rightshift(resultp1
,resultp2
,(DBL_P
-1) - (src_exponent
));
185 /* check for inexact */
186 if (Dbl_isinexact_to_fix(srcp1
,srcp2
,src_exponent
)) {
189 switch (Rounding_mode()) {
191 if (Dbl_iszero_sign(srcp1
))
192 Dbl_increment(resultp1
,resultp2
);
195 if (Dbl_isone_sign(srcp1
))
196 Dbl_increment(resultp1
,resultp2
);
199 if (Dbl_isone_roundbit(srcp1
,srcp2
,src_exponent
))
200 if (Dbl_isone_stickybit(srcp1
,srcp2
,src_exponent
)
201 || (Dbl_isone_lowmantissap2(resultp2
)))
202 Dbl_increment(resultp1
,resultp2
);
205 Dbl_leftshift(resultp1
,resultp2
,(DBL_P
-1) - (src_exponent
));
206 if (Dbl_isone_hiddenoverflow(resultp1
))
207 Dbl_set_exponent(resultp1
,src_exponent
+ (DBL_BIAS
+1));
208 else Dbl_set_exponent(resultp1
,src_exponent
+ DBL_BIAS
);
211 resultp1
= srcp1
; /* set sign */
212 Dbl_setzero_exponentmantissa(resultp1
,resultp2
);
213 /* check for inexact */
214 if (Dbl_isnotzero_exponentmantissa(srcp1
,srcp2
)) {
217 switch (Rounding_mode()) {
219 if (Dbl_iszero_sign(srcp1
))
220 Dbl_set_exponent(resultp1
,DBL_BIAS
);
223 if (Dbl_isone_sign(srcp1
))
224 Dbl_set_exponent(resultp1
,DBL_BIAS
);
227 if (src_exponent
== -1)
228 if (Dbl_isnotzero_mantissa(srcp1
,srcp2
))
229 Dbl_set_exponent(resultp1
,DBL_BIAS
);
233 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
235 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
236 else Set_inexactflag();