Home | History | Annotate | Line # | Download | only in fpe
fpu_explode.c revision 1.2.24.2
      1  1.2.24.2      he /*	$NetBSD: fpu_explode.c,v 1.2.24.2 2000/02/06 17:12:32 he Exp $ */
      2       1.1  briggs 
      3       1.1  briggs /*
      4       1.1  briggs  * Copyright (c) 1992, 1993
      5       1.1  briggs  *	The Regents of the University of California.  All rights reserved.
      6       1.1  briggs  *
      7       1.1  briggs  * This software was developed by the Computer Systems Engineering group
      8       1.1  briggs  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9       1.1  briggs  * contributed to Berkeley.
     10       1.1  briggs  *
     11       1.1  briggs  * All advertising materials mentioning features or use of this software
     12       1.1  briggs  * must display the following acknowledgement:
     13       1.1  briggs  *	This product includes software developed by the University of
     14       1.1  briggs  *	California, Lawrence Berkeley Laboratory.
     15       1.1  briggs  *
     16       1.1  briggs  * Redistribution and use in source and binary forms, with or without
     17       1.1  briggs  * modification, are permitted provided that the following conditions
     18       1.1  briggs  * are met:
     19       1.1  briggs  * 1. Redistributions of source code must retain the above copyright
     20       1.1  briggs  *    notice, this list of conditions and the following disclaimer.
     21       1.1  briggs  * 2. Redistributions in binary form must reproduce the above copyright
     22       1.1  briggs  *    notice, this list of conditions and the following disclaimer in the
     23       1.1  briggs  *    documentation and/or other materials provided with the distribution.
     24       1.1  briggs  * 3. All advertising materials mentioning features or use of this software
     25       1.1  briggs  *    must display the following acknowledgement:
     26       1.1  briggs  *	This product includes software developed by the University of
     27       1.1  briggs  *	California, Berkeley and its contributors.
     28       1.1  briggs  * 4. Neither the name of the University nor the names of its contributors
     29       1.1  briggs  *    may be used to endorse or promote products derived from this software
     30       1.1  briggs  *    without specific prior written permission.
     31       1.1  briggs  *
     32       1.1  briggs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33       1.1  briggs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34       1.1  briggs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35       1.1  briggs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36       1.1  briggs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37       1.1  briggs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38       1.1  briggs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39       1.1  briggs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40       1.1  briggs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41       1.1  briggs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42       1.1  briggs  * SUCH DAMAGE.
     43       1.1  briggs  *
     44       1.1  briggs  *	@(#)fpu_explode.c	8.1 (Berkeley) 6/11/93
     45       1.1  briggs  */
     46       1.1  briggs 
     47       1.1  briggs /*
     48       1.1  briggs  * FPU subroutines: `explode' the machine's `packed binary' format numbers
     49       1.1  briggs  * into our internal format.
     50       1.1  briggs  */
     51       1.1  briggs 
     52       1.1  briggs #include <sys/types.h>
     53       1.2  briggs #include <sys/systm.h>
     54       1.1  briggs 
     55       1.1  briggs #include "ieee.h"
     56       1.1  briggs #include <machine/reg.h>
     57       1.1  briggs 
     58       1.1  briggs #include "fpu_arith.h"
     59       1.1  briggs #include "fpu_emulate.h"
     60       1.1  briggs 
     61       1.1  briggs 
     62       1.1  briggs /* Conversion to internal format -- note asymmetry. */
     63       1.1  briggs static int	fpu_itof __P((struct fpn *fp, u_int i));
     64       1.1  briggs static int	fpu_stof __P((struct fpn *fp, u_int i));
     65       1.1  briggs static int	fpu_dtof __P((struct fpn *fp, u_int i, u_int j));
     66       1.1  briggs static int	fpu_xtof __P((struct fpn *fp, u_int i, u_int j, u_int k));
     67       1.1  briggs 
     68       1.1  briggs /*
     69       1.1  briggs  * N.B.: in all of the following, we assume the FP format is
     70       1.1  briggs  *
     71       1.1  briggs  *	---------------------------
     72       1.1  briggs  *	| s | exponent | fraction |
     73       1.1  briggs  *	---------------------------
     74       1.1  briggs  *
     75       1.1  briggs  * (which represents -1**s * 1.fraction * 2**exponent), so that the
     76       1.1  briggs  * sign bit is way at the top (bit 31), the exponent is next, and
     77       1.1  briggs  * then the remaining bits mark the fraction.  A zero exponent means
     78       1.1  briggs  * zero or denormalized (0.fraction rather than 1.fraction), and the
     79       1.1  briggs  * maximum possible exponent, 2bias+1, signals inf (fraction==0) or NaN.
     80       1.1  briggs  *
     81       1.1  briggs  * Since the sign bit is always the topmost bit---this holds even for
     82       1.1  briggs  * integers---we set that outside all the *tof functions.  Each function
     83       1.1  briggs  * returns the class code for the new number (but note that we use
     84       1.1  briggs  * FPC_QNAN for all NaNs; fpu_explode will fix this if appropriate).
     85       1.1  briggs  */
     86       1.1  briggs 
     87       1.1  briggs /*
     88       1.1  briggs  * int -> fpn.
     89       1.1  briggs  */
     90       1.1  briggs static int
     91       1.1  briggs fpu_itof(fp, i)
     92       1.1  briggs 	register struct fpn *fp;
     93       1.1  briggs 	register u_int i;
     94       1.1  briggs {
     95       1.1  briggs 
     96       1.1  briggs 	if (i == 0)
     97       1.1  briggs 		return (FPC_ZERO);
     98       1.1  briggs 	/*
     99       1.1  briggs 	 * The value FP_1 represents 2^FP_LG, so set the exponent
    100       1.1  briggs 	 * there and let normalization fix it up.  Convert negative
    101       1.1  briggs 	 * numbers to sign-and-magnitude.  Note that this relies on
    102       1.1  briggs 	 * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
    103       1.1  briggs 	 */
    104       1.1  briggs 	fp->fp_exp = FP_LG;
    105       1.1  briggs 	fp->fp_mant[0] = (int)i < 0 ? -i : i;
    106       1.1  briggs 	fp->fp_mant[1] = 0;
    107       1.1  briggs 	fp->fp_mant[2] = 0;
    108  1.2.24.2      he 	fp->fp_mant[3] = 0;
    109       1.1  briggs 	fpu_norm(fp);
    110       1.1  briggs 	return (FPC_NUM);
    111       1.1  briggs }
    112       1.1  briggs 
    113       1.1  briggs #define	mask(nbits) ((1 << (nbits)) - 1)
    114       1.1  briggs 
    115       1.1  briggs /*
    116       1.1  briggs  * All external floating formats convert to internal in the same manner,
    117       1.1  briggs  * as defined here.  Note that only normals get an implied 1.0 inserted.
    118       1.1  briggs  */
    119       1.1  briggs #define	FP_TOF(exp, expbias, allfrac, f0, f1, f2, f3) \
    120       1.1  briggs 	if (exp == 0) { \
    121       1.1  briggs 		if (allfrac == 0) \
    122       1.1  briggs 			return (FPC_ZERO); \
    123       1.1  briggs 		fp->fp_exp = 1 - expbias; \
    124       1.1  briggs 		fp->fp_mant[0] = f0; \
    125       1.1  briggs 		fp->fp_mant[1] = f1; \
    126       1.1  briggs 		fp->fp_mant[2] = f2; \
    127  1.2.24.2      he 		fp->fp_mant[3] = f3; \
    128       1.1  briggs 		fpu_norm(fp); \
    129       1.1  briggs 		return (FPC_NUM); \
    130       1.1  briggs 	} \
    131       1.1  briggs 	if (exp == (2 * expbias + 1)) { \
    132       1.1  briggs 		if (allfrac == 0) \
    133       1.1  briggs 			return (FPC_INF); \
    134       1.1  briggs 		fp->fp_mant[0] = f0; \
    135       1.1  briggs 		fp->fp_mant[1] = f1; \
    136       1.1  briggs 		fp->fp_mant[2] = f2; \
    137  1.2.24.2      he 		fp->fp_mant[3] = f3; \
    138       1.1  briggs 		return (FPC_QNAN); \
    139       1.1  briggs 	} \
    140       1.1  briggs 	fp->fp_exp = exp - expbias; \
    141       1.1  briggs 	fp->fp_mant[0] = FP_1 | f0; \
    142       1.1  briggs 	fp->fp_mant[1] = f1; \
    143       1.1  briggs 	fp->fp_mant[2] = f2; \
    144  1.2.24.2      he 	fp->fp_mant[3] = f3; \
    145       1.1  briggs 	return (FPC_NUM)
    146       1.1  briggs 
    147       1.1  briggs /*
    148       1.1  briggs  * 32-bit single precision -> fpn.
    149       1.1  briggs  * We assume a single occupies at most (64-FP_LG) bits in the internal
    150       1.1  briggs  * format: i.e., needs at most fp_mant[0] and fp_mant[1].
    151       1.1  briggs  */
    152       1.1  briggs static int
    153       1.1  briggs fpu_stof(fp, i)
    154       1.1  briggs 	register struct fpn *fp;
    155       1.1  briggs 	register u_int i;
    156       1.1  briggs {
    157       1.1  briggs 	register int exp;
    158       1.1  briggs 	register u_int frac, f0, f1;
    159       1.1  briggs #define SNG_SHIFT (SNG_FRACBITS - FP_LG)
    160       1.1  briggs 
    161       1.1  briggs 	exp = (i >> (32 - 1 - SNG_EXPBITS)) & mask(SNG_EXPBITS);
    162       1.1  briggs 	frac = i & mask(SNG_FRACBITS);
    163       1.1  briggs 	f0 = frac >> SNG_SHIFT;
    164       1.1  briggs 	f1 = frac << (32 - SNG_SHIFT);
    165       1.1  briggs 	FP_TOF(exp, SNG_EXP_BIAS, frac, f0, f1, 0, 0);
    166       1.1  briggs }
    167       1.1  briggs 
    168       1.1  briggs /*
    169       1.1  briggs  * 64-bit double -> fpn.
    170       1.1  briggs  * We assume this uses at most (96-FP_LG) bits.
    171       1.1  briggs  */
    172       1.1  briggs static int
    173       1.1  briggs fpu_dtof(fp, i, j)
    174       1.1  briggs 	register struct fpn *fp;
    175       1.1  briggs 	register u_int i, j;
    176       1.1  briggs {
    177       1.1  briggs 	register int exp;
    178       1.1  briggs 	register u_int frac, f0, f1, f2;
    179       1.1  briggs #define DBL_SHIFT (DBL_FRACBITS - 32 - FP_LG)
    180       1.1  briggs 
    181       1.1  briggs 	exp = (i >> (32 - 1 - DBL_EXPBITS)) & mask(DBL_EXPBITS);
    182       1.1  briggs 	frac = i & mask(DBL_FRACBITS - 32);
    183       1.1  briggs 	f0 = frac >> DBL_SHIFT;
    184       1.1  briggs 	f1 = (frac << (32 - DBL_SHIFT)) | (j >> DBL_SHIFT);
    185       1.1  briggs 	f2 = j << (32 - DBL_SHIFT);
    186       1.1  briggs 	frac |= j;
    187       1.1  briggs 	FP_TOF(exp, DBL_EXP_BIAS, frac, f0, f1, f2, 0);
    188       1.1  briggs }
    189       1.1  briggs 
    190       1.1  briggs /*
    191       1.1  briggs  * 96-bit extended -> fpn.
    192       1.1  briggs  */
    193       1.1  briggs static int
    194       1.1  briggs fpu_xtof(fp, i, j, k)
    195       1.1  briggs 	register struct fpn *fp;
    196       1.1  briggs 	register u_int i, j, k;
    197       1.1  briggs {
    198       1.1  briggs 	register int exp;
    199       1.1  briggs 	register u_int frac, f0, f1, f2;
    200       1.1  briggs #define EXT_SHIFT (EXT_FRACBITS - 1 - 32 - FP_LG)
    201       1.1  briggs 
    202       1.1  briggs 	exp = (i >> (32 - 1 - EXT_EXPBITS)) & mask(EXT_EXPBITS);
    203       1.1  briggs 	f0 = j >> EXT_SHIFT;
    204       1.1  briggs 	f1 = (j << (32 - EXT_SHIFT)) | (k >> EXT_SHIFT);
    205       1.1  briggs 	f2 = k << (32 - EXT_SHIFT);
    206       1.1  briggs 	frac = j | k;
    207       1.1  briggs 
    208       1.1  briggs 	/* m68k extended does not imply denormal by exp==0 */
    209       1.1  briggs 	if (exp == 0) {
    210       1.1  briggs 		if (frac == 0)
    211       1.1  briggs 			return (FPC_ZERO);
    212       1.1  briggs 		fp->fp_exp = - EXT_EXP_BIAS;
    213       1.1  briggs 		fp->fp_mant[0] = f0;
    214       1.1  briggs 		fp->fp_mant[1] = f1;
    215       1.1  briggs 		fp->fp_mant[2] = f2;
    216  1.2.24.2      he 		fp->fp_mant[3] = 0;
    217       1.1  briggs 		fpu_norm(fp);
    218       1.1  briggs 		return (FPC_NUM);
    219       1.1  briggs 	}
    220       1.1  briggs 	if (exp == (2 * EXT_EXP_BIAS + 1)) {
    221       1.1  briggs 		if (frac == 0)
    222       1.1  briggs 			return (FPC_INF);
    223       1.1  briggs 		fp->fp_mant[0] = f0;
    224       1.1  briggs 		fp->fp_mant[1] = f1;
    225       1.1  briggs 		fp->fp_mant[2] = f2;
    226  1.2.24.2      he 		fp->fp_mant[3] = 0;
    227       1.1  briggs 		return (FPC_QNAN);
    228       1.1  briggs 	}
    229       1.1  briggs 	fp->fp_exp = exp - EXT_EXP_BIAS;
    230       1.1  briggs 	fp->fp_mant[0] = FP_1 | f0;
    231       1.1  briggs 	fp->fp_mant[1] = f1;
    232       1.1  briggs 	fp->fp_mant[2] = f2;
    233  1.2.24.2      he 	fp->fp_mant[3] = 0;
    234       1.1  briggs 	return (FPC_NUM);
    235       1.1  briggs }
    236       1.1  briggs 
    237       1.1  briggs /*
    238       1.1  briggs  * Explode the contents of a memory operand.
    239       1.1  briggs  */
    240       1.1  briggs void
    241       1.1  briggs fpu_explode(fe, fp, type, space)
    242       1.1  briggs 	register struct fpemu *fe;
    243       1.1  briggs 	register struct fpn *fp;
    244       1.1  briggs 	int type;
    245       1.1  briggs 	register u_int *space;
    246       1.1  briggs {
    247       1.1  briggs 	register u_int s;
    248       1.1  briggs 
    249       1.1  briggs 	s = space[0];
    250       1.1  briggs 	fp->fp_sign = s >> 31;
    251       1.1  briggs 	fp->fp_sticky = 0;
    252       1.1  briggs 	switch (type) {
    253       1.1  briggs 
    254       1.1  briggs 	case FTYPE_BYT:
    255       1.1  briggs 		s >>= 8;
    256       1.1  briggs 	case FTYPE_WRD:
    257       1.1  briggs 		s >>= 16;
    258       1.1  briggs 	case FTYPE_LNG:
    259       1.1  briggs 		s = fpu_itof(fp, s);
    260       1.1  briggs 		break;
    261       1.1  briggs 
    262       1.1  briggs 	case FTYPE_SNG:
    263       1.1  briggs 		s = fpu_stof(fp, s);
    264       1.1  briggs 		break;
    265       1.1  briggs 
    266       1.1  briggs 	case FTYPE_DBL:
    267       1.1  briggs 		s = fpu_dtof(fp, s, space[1]);
    268       1.1  briggs 		break;
    269       1.1  briggs 
    270       1.1  briggs 	case FTYPE_EXT:
    271       1.1  briggs 		s = fpu_xtof(fp, s, space[1], space[2]);
    272       1.1  briggs 		break;
    273       1.1  briggs 
    274       1.1  briggs 	default:
    275       1.1  briggs 		panic("fpu_explode");
    276       1.1  briggs 	}
    277       1.1  briggs 	if (s == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
    278       1.1  briggs 		/*
    279       1.1  briggs 		 * Input is a signalling NaN.  All operations that return
    280       1.1  briggs 		 * an input NaN operand put it through a ``NaN conversion'',
    281       1.1  briggs 		 * which basically just means ``turn on the quiet bit''.
    282       1.1  briggs 		 * We do this here so that all NaNs internally look quiet
    283       1.1  briggs 		 * (we can tell signalling ones by their class).
    284       1.1  briggs 		 */
    285       1.1  briggs 		fp->fp_mant[0] |= FP_QUIETBIT;
    286       1.1  briggs 		fe->fe_fpsr |= FPSR_SNAN;	/* assert SNAN exception */
    287       1.1  briggs 		s = FPC_SNAN;
    288       1.1  briggs 	}
    289       1.1  briggs 	fp->fp_class = s;
    290       1.1  briggs }
    291