No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hppa / spmath / fcnvfx.c
blob44c9265a220143793faa4673aea2e431de3ad3cf
1 /* $NetBSD: fcnvfx.c,v 1.3 2005/12/11 12:17:40 christos Exp $ */
3 /* $OpenBSD: fcnvfx.c,v 1.5 2001/03/29 03:58:18 mickey Exp $ */
5 /*
6 * Copyright 1996 1995 by Open Software Foundation, Inc.
7 * All Rights Reserved
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.
27 * pmk1.1
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: fcnvfx.c,v 1.3 2005/12/11 12:17:40 christos 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 * Single Floating-point to Single Fixed-point
55 /*ARGSUSED*/
56 int
57 sgl_to_sgl_fcnvfx(srcptr,dstptr,status)
59 sgl_floating_point *srcptr;
60 int *dstptr;
61 unsigned int *status;
63 register unsigned int src, temp;
64 register int src_exponent, result;
65 register int inexact = false;
67 src = *srcptr;
68 src_exponent = Sgl_exponent(src) - SGL_BIAS;
71 * Test for overflow
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);
86 * Generate result
88 if (src_exponent >= 0) {
89 temp = src;
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);
95 /* check for inexact */
96 if (Sgl_isinexact_to_fix(src,src_exponent)) {
97 inexact = true;
98 /* round result */
99 switch (Rounding_mode()) {
100 case ROUNDPLUS:
101 if (Sgl_iszero_sign(src)) result++;
102 break;
103 case ROUNDMINUS:
104 if (Sgl_isone_sign(src)) result--;
105 break;
106 case ROUNDNEAREST:
107 if (Sgl_isone_roundbit(src,src_exponent)) {
108 if (Sgl_isone_stickybit(src,src_exponent)
109 || (Sgl_isone_lowmantissa(temp))) {
110 if (Sgl_iszero_sign(src)) result++;
111 else result--;
117 else {
118 result = 0;
120 /* check for inexact */
121 if (Sgl_isnotzero_exponentmantissa(src)) {
122 inexact = true;
123 /* round result */
124 switch (Rounding_mode()) {
125 case ROUNDPLUS:
126 if (Sgl_iszero_sign(src)) result++;
127 break;
128 case ROUNDMINUS:
129 if (Sgl_isone_sign(src)) result--;
130 break;
131 case ROUNDNEAREST:
132 if (src_exponent == -1)
133 if (Sgl_isnotzero_mantissa(src)) {
134 if (Sgl_iszero_sign(src)) result++;
135 else result--;
140 *dstptr = result;
141 if (inexact) {
142 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
143 else Set_inexactflag();
145 return(NOEXCEPTION);
149 * Single Floating-point to Double Fixed-point
151 /*ARGSUSED*/
153 sgl_to_dbl_fcnvfx(srcptr,dstptr,status)
155 sgl_floating_point *srcptr;
156 dbl_integer *dstptr;
157 unsigned int *status;
159 register int src_exponent, resultp1;
160 register unsigned int src, temp, resultp2;
161 register int inexact = false;
163 src = *srcptr;
164 src_exponent = Sgl_exponent(src) - SGL_BIAS;
167 * Test for overflow
169 if (src_exponent > DBL_FX_MAX_EXP) {
170 /* check for MININT */
171 if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
172 Sgl_isnotzero_mantissa(src) || Sgl_iszero_sign(src)) {
174 * Since source is a number which cannot be
175 * represented in fixed-point format, return
176 * largest (or smallest) fixed-point number.
178 Sgl_return_overflow_dbl(src,dstptr);
180 Dint_set_minint(resultp1,resultp2);
181 Dint_copytoptr(resultp1,resultp2,dstptr);
182 return(NOEXCEPTION);
185 * Generate result
187 if (src_exponent >= 0) {
188 temp = src;
189 Sgl_clear_signexponent_set_hidden(temp);
190 Dint_from_sgl_mantissa(temp,src_exponent,resultp1,resultp2);
191 if (Sgl_isone_sign(src)) {
192 Dint_setone_sign(resultp1,resultp2);
195 /* check for inexact */
196 if (Sgl_isinexact_to_fix(src,src_exponent)) {
197 inexact = true;
198 /* round result */
199 switch (Rounding_mode()) {
200 case ROUNDPLUS:
201 if (Sgl_iszero_sign(src)) {
202 Dint_increment(resultp1,resultp2);
204 break;
205 case ROUNDMINUS:
206 if (Sgl_isone_sign(src)) {
207 Dint_decrement(resultp1,resultp2);
209 break;
210 case ROUNDNEAREST:
211 if (Sgl_isone_roundbit(src,src_exponent))
212 if (Sgl_isone_stickybit(src,src_exponent) ||
213 (Dint_isone_lowp2(resultp2))) {
214 if (Sgl_iszero_sign(src)) {
215 Dint_increment(resultp1,resultp2);
217 else {
218 Dint_decrement(resultp1,resultp2);
224 else {
225 Dint_setzero(resultp1,resultp2);
227 /* check for inexact */
228 if (Sgl_isnotzero_exponentmantissa(src)) {
229 inexact = true;
230 /* round result */
231 switch (Rounding_mode()) {
232 case ROUNDPLUS:
233 if (Sgl_iszero_sign(src)) {
234 Dint_increment(resultp1,resultp2);
236 break;
237 case ROUNDMINUS:
238 if (Sgl_isone_sign(src)) {
239 Dint_decrement(resultp1,resultp2);
241 break;
242 case ROUNDNEAREST:
243 if (src_exponent == -1)
244 if (Sgl_isnotzero_mantissa(src)) {
245 if (Sgl_iszero_sign(src)) {
246 Dint_increment(resultp1,resultp2);
248 else {
249 Dint_decrement(resultp1,resultp2);
255 Dint_copytoptr(resultp1,resultp2,dstptr);
256 if (inexact) {
257 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
258 else Set_inexactflag();
260 return(NOEXCEPTION);
264 * Double Floating-point to Single Fixed-point
266 /*ARGSUSED*/
268 dbl_to_sgl_fcnvfx(srcptr,dstptr,status)
270 dbl_floating_point *srcptr;
271 int *dstptr;
272 unsigned int *status;
274 register unsigned int srcp1,srcp2, tempp1,tempp2;
275 register int src_exponent, result;
276 register int inexact = false;
278 Dbl_copyfromptr(srcptr,srcp1,srcp2);
279 src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
282 * Test for overflow
284 if (src_exponent > SGL_FX_MAX_EXP) {
285 /* check for MININT */
286 if (Dbl_isoverflow_to_int(src_exponent,srcp1,srcp2)) {
288 * Since source is a number which cannot be
289 * represented in fixed-point format, return
290 * largest (or smallest) fixed-point number.
292 Dbl_return_overflow(srcp1,srcp2,dstptr);
296 * Generate result
298 if (src_exponent >= 0) {
299 tempp1 = srcp1;
300 tempp2 = srcp2;
301 Dbl_clear_signexponent_set_hidden(tempp1);
302 Int_from_dbl_mantissa(tempp1,tempp2,src_exponent);
303 if (Dbl_isone_sign(srcp1) && (src_exponent <= SGL_FX_MAX_EXP))
304 result = -Dbl_allp1(tempp1);
305 else result = Dbl_allp1(tempp1);
307 /* check for inexact */
308 if (Dbl_isinexact_to_fix(srcp1,srcp2,src_exponent)) {
309 inexact = true;
310 /* round result */
311 switch (Rounding_mode()) {
312 case ROUNDPLUS:
313 if (Dbl_iszero_sign(srcp1)) result++;
314 break;
315 case ROUNDMINUS:
316 if (Dbl_isone_sign(srcp1)) result--;
317 break;
318 case ROUNDNEAREST:
319 if (Dbl_isone_roundbit(srcp1,srcp2,src_exponent))
320 if (Dbl_isone_stickybit(srcp1,srcp2,src_exponent) ||
321 (Dbl_isone_lowmantissap1(tempp1))) {
322 if (Dbl_iszero_sign(srcp1)) result++;
323 else result--;
326 /* check for overflow */
327 if ((Dbl_iszero_sign(srcp1) && result < 0) ||
328 (Dbl_isone_sign(srcp1) && result > 0)) {
329 Dbl_return_overflow(srcp1,srcp2,dstptr);
333 else {
334 result = 0;
336 /* check for inexact */
337 if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
338 inexact = true;
339 /* round result */
340 switch (Rounding_mode()) {
341 case ROUNDPLUS:
342 if (Dbl_iszero_sign(srcp1)) result++;
343 break;
344 case ROUNDMINUS:
345 if (Dbl_isone_sign(srcp1)) result--;
346 break;
347 case ROUNDNEAREST:
348 if (src_exponent == -1)
349 if (Dbl_isnotzero_mantissa(srcp1,srcp2)) {
350 if (Dbl_iszero_sign(srcp1)) result++;
351 else result--;
356 *dstptr = result;
357 if (inexact) {
358 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
359 else Set_inexactflag();
361 return(NOEXCEPTION);
365 * Double Floating-point to Double Fixed-point
367 /*ARGSUSED*/
369 dbl_to_dbl_fcnvfx(srcptr,dstptr,status)
371 dbl_floating_point *srcptr;
372 dbl_integer *dstptr;
373 unsigned int *status;
375 register int src_exponent, resultp1;
376 register unsigned int srcp1, srcp2, tempp1, tempp2, resultp2;
377 register int inexact = false;
379 Dbl_copyfromptr(srcptr,srcp1,srcp2);
380 src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
383 * Test for overflow
385 if (src_exponent > DBL_FX_MAX_EXP) {
386 /* check for MININT */
387 if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
388 Dbl_isnotzero_mantissa(srcp1,srcp2) || Dbl_iszero_sign(srcp1)) {
390 * Since source is a number which cannot be
391 * represented in fixed-point format, return
392 * largest (or smallest) fixed-point number.
394 Dbl_return_overflow_dbl(srcp1,srcp2,dstptr);
399 * Generate result
401 if (src_exponent >= 0) {
402 tempp1 = srcp1;
403 tempp2 = srcp2;
404 Dbl_clear_signexponent_set_hidden(tempp1);
405 Dint_from_dbl_mantissa(tempp1,tempp2,src_exponent,
406 resultp1, resultp2);
407 if (Dbl_isone_sign(srcp1)) {
408 Dint_setone_sign(resultp1,resultp2);
411 /* check for inexact */
412 if (Dbl_isinexact_to_fix(srcp1,srcp2,src_exponent)) {
413 inexact = true;
414 /* round result */
415 switch (Rounding_mode()) {
416 case ROUNDPLUS:
417 if (Dbl_iszero_sign(srcp1)) {
418 Dint_increment(resultp1,resultp2);
420 break;
421 case ROUNDMINUS:
422 if (Dbl_isone_sign(srcp1)) {
423 Dint_decrement(resultp1,resultp2);
425 break;
426 case ROUNDNEAREST:
427 if (Dbl_isone_roundbit(srcp1,srcp2,src_exponent))
428 if (Dbl_isone_stickybit(srcp1,srcp2,src_exponent) ||
429 (Dint_isone_lowp2(resultp2))) {
430 if (Dbl_iszero_sign(srcp1)) {
431 Dint_increment(resultp1,resultp2);
433 else {
434 Dint_decrement(resultp1,resultp2);
440 else {
441 Dint_setzero(resultp1,resultp2);
443 /* check for inexact */
444 if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
445 inexact = true;
446 /* round result */
447 switch (Rounding_mode()) {
448 case ROUNDPLUS:
449 if (Dbl_iszero_sign(srcp1)) {
450 Dint_increment(resultp1,resultp2);
452 break;
453 case ROUNDMINUS:
454 if (Dbl_isone_sign(srcp1)) {
455 Dint_decrement(resultp1,resultp2);
457 break;
458 case ROUNDNEAREST:
459 if (src_exponent == -1)
460 if (Dbl_isnotzero_mantissa(srcp1,srcp2)) {
461 if (Dbl_iszero_sign(srcp1)) {
462 Dint_increment(resultp1,resultp2);
464 else {
465 Dint_decrement(resultp1,resultp2);
471 Dint_copytoptr(resultp1,resultp2,dstptr);
472 if (inexact) {
473 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
474 else Set_inexactflag();
476 return(NOEXCEPTION);