1 /* $NetBSD: fcnvfxt.c,v 1.1.10.1 2004/08/03 10:35:38 skrll Exp $ */
3 /* $OpenBSD: fcnvfxt.c,v 1.5 2001/03/29 03:58:18 mickey Exp $ */
6 * Copyright 1996 1995 by Open Software Foundation, Inc.
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appears in all copies and
12 * that both the copyright notice and this permission notice appear in
13 * supporting documentation.
15 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
22 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
23 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 * (c) Copyright 1986 HEWLETT-PACKARD COMPANY
32 * To anyone who acknowledges that this file is provided "AS IS"
33 * without any express or implied warranty:
34 * permission to use, copy, modify, and distribute this file
35 * for any purpose is hereby granted without fee, provided that
36 * the above copyright notice and this notice appears in all
37 * copies, and that the name of Hewlett-Packard Company not be
38 * used in advertising or publicity pertaining to distribution
39 * of the software without specific, written prior permission.
40 * Hewlett-Packard Company makes no representations about the
41 * suitability of this software for any purpose.
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: fcnvfxt.c,v 1.1.10.1 2004/08/03 10:35:38 skrll Exp $");
47 #include "../spmath/float.h"
48 #include "../spmath/sgl_float.h"
49 #include "../spmath/dbl_float.h"
50 #include "../spmath/cnv_float.h"
53 * Convert single floating-point to single fixed-point format
54 * with truncated result
58 sgl_to_sgl_fcnvfxt(srcptr
,dstptr
,status
)
60 sgl_floating_point
*srcptr
;
64 register unsigned int src
, temp
;
65 register int src_exponent
, result
;
68 src_exponent
= Sgl_exponent(src
) - SGL_BIAS
;
73 if (src_exponent
> SGL_FX_MAX_EXP
) {
74 /* check for MININT */
75 if ((src_exponent
> SGL_FX_MAX_EXP
+ 1) ||
76 Sgl_isnotzero_mantissa(src
) || Sgl_iszero_sign(src
)) {
78 * Since source is a number which cannot be
79 * represented in fixed-point format, return
80 * largest (or smallest) fixed-point number.
82 Sgl_return_overflow(src
,dstptr
);
88 if (src_exponent
>= 0) {
90 Sgl_clear_signexponent_set_hidden(temp
);
91 Int_from_sgl_mantissa(temp
,src_exponent
);
92 if (Sgl_isone_sign(src
)) result
= -Sgl_all(temp
);
93 else result
= Sgl_all(temp
);
96 /* check for inexact */
97 if (Sgl_isinexact_to_fix(src
,src_exponent
)) {
98 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
99 else Set_inexactflag();
105 /* check for inexact */
106 if (Sgl_isnotzero_exponentmantissa(src
)) {
107 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
108 else Set_inexactflag();
115 * Single Floating-point to Double Fixed-point
119 sgl_to_dbl_fcnvfxt(srcptr
,dstptr
,status
)
121 sgl_floating_point
*srcptr
;
123 unsigned int *status
;
125 register int src_exponent
, resultp1
;
126 register unsigned int src
, temp
, resultp2
;
129 src_exponent
= Sgl_exponent(src
) - SGL_BIAS
;
134 if (src_exponent
> DBL_FX_MAX_EXP
) {
135 /* check for MININT */
136 if ((src_exponent
> DBL_FX_MAX_EXP
+ 1) ||
137 Sgl_isnotzero_mantissa(src
) || Sgl_iszero_sign(src
)) {
139 * Since source is a number which cannot be
140 * represented in fixed-point format, return
141 * largest (or smallest) fixed-point number.
143 Sgl_return_overflow_dbl(src
,dstptr
);
145 Dint_set_minint(resultp1
,resultp2
);
146 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
152 if (src_exponent
>= 0) {
154 Sgl_clear_signexponent_set_hidden(temp
);
155 Dint_from_sgl_mantissa(temp
,src_exponent
,resultp1
,resultp2
);
156 if (Sgl_isone_sign(src
)) {
157 Dint_setone_sign(resultp1
,resultp2
);
159 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
161 /* check for inexact */
162 if (Sgl_isinexact_to_fix(src
,src_exponent
)) {
163 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
164 else Set_inexactflag();
168 Dint_setzero(resultp1
,resultp2
);
169 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
171 /* check for inexact */
172 if (Sgl_isnotzero_exponentmantissa(src
)) {
173 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
174 else Set_inexactflag();
181 * Double Floating-point to Single Fixed-point
185 dbl_to_sgl_fcnvfxt(srcptr
,dstptr
,status
)
187 dbl_floating_point
*srcptr
;
189 unsigned int *status
;
191 register unsigned int srcp1
, srcp2
, tempp1
, tempp2
;
192 register int src_exponent
, result
;
194 Dbl_copyfromptr(srcptr
,srcp1
,srcp2
);
195 src_exponent
= Dbl_exponent(srcp1
) - DBL_BIAS
;
200 if (src_exponent
> SGL_FX_MAX_EXP
) {
201 /* check for MININT */
202 if (Dbl_isoverflow_to_int(src_exponent
,srcp1
,srcp2
)) {
203 Dbl_return_overflow(srcp1
,srcp2
,dstptr
);
209 if (src_exponent
>= 0) {
212 Dbl_clear_signexponent_set_hidden(tempp1
);
213 Int_from_dbl_mantissa(tempp1
,tempp2
,src_exponent
);
214 if (Dbl_isone_sign(srcp1
) && (src_exponent
<= SGL_FX_MAX_EXP
))
215 result
= -Dbl_allp1(tempp1
);
216 else result
= Dbl_allp1(tempp1
);
219 /* check for inexact */
220 if (Dbl_isinexact_to_fix(srcp1
,srcp2
,src_exponent
)) {
221 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
222 else Set_inexactflag();
228 /* check for inexact */
229 if (Dbl_isnotzero_exponentmantissa(srcp1
,srcp2
)) {
230 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
231 else Set_inexactflag();
238 * Double Floating-point to Double Fixed-point
242 dbl_to_dbl_fcnvfxt(srcptr
,dstptr
,status
)
244 dbl_floating_point
*srcptr
;
246 unsigned int *status
;
248 register int src_exponent
, resultp1
;
249 register unsigned int srcp1
, srcp2
, tempp1
, tempp2
, resultp2
;
251 Dbl_copyfromptr(srcptr
,srcp1
,srcp2
);
252 src_exponent
= Dbl_exponent(srcp1
) - DBL_BIAS
;
257 if (src_exponent
> DBL_FX_MAX_EXP
) {
258 /* check for MININT */
259 if ((src_exponent
> DBL_FX_MAX_EXP
+ 1) ||
260 Dbl_isnotzero_mantissa(srcp1
,srcp2
) || Dbl_iszero_sign(srcp1
)) {
262 * Since source is a number which cannot be
263 * represented in fixed-point format, return
264 * largest (or smallest) fixed-point number.
266 Dbl_return_overflow_dbl(srcp1
,srcp2
,dstptr
);
272 if (src_exponent
>= 0) {
275 Dbl_clear_signexponent_set_hidden(tempp1
);
276 Dint_from_dbl_mantissa(tempp1
,tempp2
,src_exponent
,
278 if (Dbl_isone_sign(srcp1
)) {
279 Dint_setone_sign(resultp1
,resultp2
);
281 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
283 /* check for inexact */
284 if (Dbl_isinexact_to_fix(srcp1
,srcp2
,src_exponent
)) {
285 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
286 else Set_inexactflag();
290 Dint_setzero(resultp1
,resultp2
);
291 Dint_copytoptr(resultp1
,resultp2
,dstptr
);
293 /* check for inexact */
294 if (Dbl_isnotzero_exponentmantissa(srcp1
,srcp2
)) {
295 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
296 else Set_inexactflag();