ack/mach/proto/fp/compact.c

203 lines
4 KiB
C
Raw Normal View History

1988-08-11 14:50:18 +00:00
/*
1988-04-07 11:40:46 +00:00
(c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Header$ */
1988-04-07 10:57:49 +00:00
/*
COMPACT EXTEND FORMAT INTO FLOAT OF PROPER SIZE
*/
# include "FP_bias.h"
# include "FP_shift.h"
# include "FP_trap.h"
# include "FP_types.h"
# include "get_put.h"
1993-01-05 12:06:58 +00:00
void
1988-04-07 10:57:49 +00:00
compact(f,to,size)
EXTEND *f;
1993-01-05 12:06:58 +00:00
unsigned long *to;
1988-04-07 10:57:49 +00:00
int size;
{
int error = 0;
1993-01-05 12:06:58 +00:00
if (size == sizeof(DOUBLE)) {
/*
* COMPACT EXTENDED INTO DOUBLE
*/
1993-01-05 12:06:58 +00:00
DOUBLE *DBL = (DOUBLE *) (void *) to;
1988-08-04 18:10:34 +00:00
1988-04-07 10:57:49 +00:00
if ((f->m1|(f->m2 & DBL_ZERO)) == 0L) {
1993-01-05 12:06:58 +00:00
zrf8(DBL);
return;
1988-04-07 10:57:49 +00:00
}
f->exp += DBL_BIAS; /* restore proper bias */
if (f->exp > DBL_MAX) {
dbl_over: trap(EFOVFL);
1989-07-25 14:21:09 +00:00
f->exp = DBL_MAX+1;
f->m1 = 0;
f->m2 = 0;
1988-04-07 10:57:49 +00:00
if (error++)
return;
}
else if (f->exp < DBL_MIN) {
1993-01-05 12:06:58 +00:00
b64_rsft(&(f->mantissa));
1989-07-25 14:21:09 +00:00
if (f->exp < 0) {
1993-01-05 12:06:58 +00:00
b64_sft(&(f->mantissa), -f->exp);
1989-07-25 14:21:09 +00:00
f->exp = 0;
}
/* underflow ??? */
1988-04-07 10:57:49 +00:00
}
/* local CAST conversion */
/* because of special format shift only 10 bits */
/* bit shift mantissa 10 bits */
/* first align within words, then do store operation */
1993-01-05 12:06:58 +00:00
DBL->d[0] = f->m1 >> DBL_RUNPACK; /* plus 22 == 32 */
DBL->d[1] = f->m2 >> DBL_RUNPACK; /* plus 22 == 32 */
DBL->d[1] |= (f->m1 << DBL_LUNPACK); /* plus 10 == 32 */
1988-04-07 10:57:49 +00:00
/* if not exact then round to nearest */
/* on a tie, round to even */
1988-04-07 10:57:49 +00:00
1988-08-04 18:10:34 +00:00
#ifdef EXCEPTION_INEXACT
if ((f->m2 & DBL_EXACT) != 0) {
INEXACT();
#endif
if (((f->m2 & DBL_EXACT) > DBL_ROUNDUP)
|| ((f->m2 & DBL_EXACT) == DBL_ROUNDUP
&& (f->m2 & (DBL_ROUNDUP << 1)))) {
1993-01-05 12:06:58 +00:00
DBL->d[1]++; /* rounding up */
if (DBL->d[1] == 0L) { /* carry out */
DBL->d[0]++;
1993-01-05 12:06:58 +00:00
if (f->exp == 0 && (DBL->d[0] & ~DBL_MASK)) {
f->exp++;
}
1993-01-05 12:06:58 +00:00
if (DBL->d[0] & DBL_CARRYOUT) { /* carry out */
if (DBL->d[0] & 01)
DBL->d[1] = CARRYBIT;
DBL->d[0] >>= 1;
1988-04-07 10:57:49 +00:00
f->exp++;
}
}
1988-08-04 18:10:34 +00:00
/* check for overflow */
if (f->exp > DBL_MAX)
goto dbl_over;
1988-04-07 10:57:49 +00:00
}
1988-08-04 18:10:34 +00:00
#ifdef EXCEPTION_INEXACT
1988-04-07 10:57:49 +00:00
}
1988-08-04 18:10:34 +00:00
#endif
1988-04-07 10:57:49 +00:00
/*
1988-08-04 18:10:34 +00:00
* STORE EXPONENT AND SIGN:
*
* 1) clear leading bits (B4-B15)
* 2) shift and store exponent
*/
1988-04-07 10:57:49 +00:00
1993-01-05 12:06:58 +00:00
DBL->d[0] &= DBL_MASK;
DBL->d[0] |=
1988-08-04 18:10:34 +00:00
((long) (f->exp << DBL_EXPSHIFT) << EXP_STORE);
if (f->sign)
1993-01-05 12:06:58 +00:00
DBL->d[0] |= CARRYBIT;
1988-08-04 18:10:34 +00:00
/*
* STORE MANTISSA
*/
1989-10-25 17:15:37 +00:00
#if FL_MSL_AT_LOW_ADDRESS
1993-01-05 12:06:58 +00:00
put4(DBL->d[0], (char *) &DBL->d[0]);
put4(DBL->d[1], (char *) &DBL->d[1]);
1989-10-25 17:15:37 +00:00
#else
{ unsigned long l;
1993-01-05 12:06:58 +00:00
put4(DBL->d[1], (char *) &l);
put4(DBL->d[0], (char *) &DBL->d[1]);
DBL->d[0] = l;
1989-10-25 17:15:37 +00:00
}
#endif
1988-04-07 10:57:49 +00:00
}
else {
/*
* COMPACT EXTENDED INTO FLOAT
*/
1988-08-04 18:10:34 +00:00
SINGLE *SGL;
1988-04-07 10:57:49 +00:00
/* local CAST conversion */
1993-01-05 12:06:58 +00:00
SGL = (SINGLE *) (void *) to;
1988-04-07 10:57:49 +00:00
if ((f->m1 & SGL_ZERO) == 0L) {
1993-01-05 12:06:58 +00:00
*SGL = 0L;
return;
1988-04-07 10:57:49 +00:00
}
f->exp += SGL_BIAS; /* restore bias */
if (f->exp > SGL_MAX) {
sgl_over: trap(EFOVFL);
1989-07-25 14:21:09 +00:00
f->exp = SGL_MAX+1;
f->m1 = 0L;
f->m2 = 0L;
1988-04-07 10:57:49 +00:00
if (error++)
return;
}
else if (f->exp < SGL_MIN) {
1993-01-05 12:06:58 +00:00
b64_rsft(&(f->mantissa));
1989-07-25 14:21:09 +00:00
if (f->exp < 0) {
1993-01-05 12:06:58 +00:00
b64_sft(&(f->mantissa), -f->exp);
1989-07-25 14:21:09 +00:00
f->exp = 0;
}
/* underflow ??? */
1988-04-07 10:57:49 +00:00
}
/* shift mantissa and store */
1993-01-05 12:06:58 +00:00
*SGL = (f->m1 >> SGL_RUNPACK);
1988-04-07 10:57:49 +00:00
/* check for rounding to nearest */
/* on a tie, round to even */
1988-08-04 18:10:34 +00:00
#ifdef EXCEPTION_INEXACT
if (f->m2 != 0 ||
(f->m1 & SGL_EXACT) != 0L) {
INEXACT();
#endif
if (((f->m1 & SGL_EXACT) > SGL_ROUNDUP)
|| ((f->m1 & SGL_EXACT) == SGL_ROUNDUP
&& (f->m1 & (SGL_ROUNDUP << 1)))) {
1993-01-05 12:06:58 +00:00
(*SGL)++;
if (f->exp == 0 && (*SGL & ~SGL_MASK)) {
f->exp++;
}
1988-04-07 10:57:49 +00:00
/* check normal */
1993-01-05 12:06:58 +00:00
if (*SGL & SGL_CARRYOUT) {
*SGL >>= 1;
1988-04-07 10:57:49 +00:00
f->exp++;
}
1988-08-04 18:10:34 +00:00
if (f->exp > SGL_MAX)
goto sgl_over;
1988-04-07 10:57:49 +00:00
}
1988-08-04 18:10:34 +00:00
#ifdef EXCEPTION_INEXACT
1988-04-07 10:57:49 +00:00
}
1988-08-04 18:10:34 +00:00
#endif
1988-04-07 10:57:49 +00:00
/*
1988-08-04 18:10:34 +00:00
* STORE EXPONENT AND SIGN:
*
* 1) clear leading bit of fraction
* 2) shift and store exponent
*/
1988-04-07 10:57:49 +00:00
1993-01-05 12:06:58 +00:00
*SGL &= SGL_MASK; /* B23-B31 are 0 */
*SGL |= ((long) (f->exp << SGL_EXPSHIFT) << EXP_STORE);
1988-08-04 18:10:34 +00:00
if (f->sign)
1993-01-05 12:06:58 +00:00
*SGL |= CARRYBIT;
1988-04-07 10:57:49 +00:00
1988-08-04 18:10:34 +00:00
/*
* STORE MANTISSA
*/
1988-04-07 10:57:49 +00:00
1993-01-05 12:06:58 +00:00
put4(*SGL, (char *) &SGL);
1988-08-04 18:10:34 +00:00
}
1988-04-07 10:57:49 +00:00
}