Home | History | Annotate | Line # | Download | only in fpe
fpu_explode.c revision 1.4
      1  1.4   lukem /*	$NetBSD: fpu_explode.c,v 1.4 2003/07/15 02:43:09 lukem 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.4   lukem 
     52  1.4   lukem #include <sys/cdefs.h>
     53  1.4   lukem __KERNEL_RCSID(0, "$NetBSD: fpu_explode.c,v 1.4 2003/07/15 02:43:09 lukem Exp $");
     54  1.1  briggs 
     55  1.1  briggs #include <sys/types.h>
     56  1.2  briggs #include <sys/systm.h>
     57  1.1  briggs 
     58  1.1  briggs #include "ieee.h"
     59  1.1  briggs #include <machine/reg.h>
     60  1.1  briggs 
     61  1.1  briggs #include "fpu_arith.h"
     62  1.1  briggs #include "fpu_emulate.h"
     63  1.1  briggs 
     64  1.1  briggs 
     65  1.1  briggs /* Conversion to internal format -- note asymmetry. */
     66  1.1  briggs static int	fpu_itof __P((struct fpn *fp, u_int i));
     67  1.1  briggs static int	fpu_stof __P((struct fpn *fp, u_int i));
     68  1.1  briggs static int	fpu_dtof __P((struct fpn *fp, u_int i, u_int j));
     69  1.1  briggs static int	fpu_xtof __P((struct fpn *fp, u_int i, u_int j, u_int k));
     70  1.1  briggs 
     71  1.1  briggs /*
     72  1.1  briggs  * N.B.: in all of the following, we assume the FP format is
     73  1.1  briggs  *
     74  1.1  briggs  *	---------------------------
     75  1.1  briggs  *	| s | exponent | fraction |
     76  1.1  briggs  *	---------------------------
     77  1.1  briggs  *
     78  1.1  briggs  * (which represents -1**s * 1.fraction * 2**exponent), so that the
     79  1.1  briggs  * sign bit is way at the top (bit 31), the exponent is next, and
     80  1.1  briggs  * then the remaining bits mark the fraction.  A zero exponent means
     81  1.1  briggs  * zero or denormalized (0.fraction rather than 1.fraction), and the
     82  1.1  briggs  * maximum possible exponent, 2bias+1, signals inf (fraction==0) or NaN.
     83  1.1  briggs  *
     84  1.1  briggs  * Since the sign bit is always the topmost bit---this holds even for
     85  1.1  briggs  * integers---we set that outside all the *tof functions.  Each function
     86  1.1  briggs  * returns the class code for the new number (but note that we use
     87  1.1  briggs  * FPC_QNAN for all NaNs; fpu_explode will fix this if appropriate).
     88  1.1  briggs  */
     89  1.1  briggs 
     90  1.1  briggs /*
     91  1.1  briggs  * int -> fpn.
     92  1.1  briggs  */
     93  1.1  briggs static int
     94  1.1  briggs fpu_itof(fp, i)
     95  1.1  briggs 	register struct fpn *fp;
     96  1.1  briggs 	register u_int i;
     97  1.1  briggs {
     98  1.1  briggs 
     99  1.1  briggs 	if (i == 0)
    100  1.1  briggs 		return (FPC_ZERO);
    101  1.1  briggs 	/*
    102  1.1  briggs 	 * The value FP_1 represents 2^FP_LG, so set the exponent
    103  1.1  briggs 	 * there and let normalization fix it up.  Convert negative
    104  1.1  briggs 	 * numbers to sign-and-magnitude.  Note that this relies on
    105  1.1  briggs 	 * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
    106  1.1  briggs 	 */
    107  1.1  briggs 	fp->fp_exp = FP_LG;
    108  1.1  briggs 	fp->fp_mant[0] = (int)i < 0 ? -i : i;
    109  1.1  briggs 	fp->fp_mant[1] = 0;
    110  1.1  briggs 	fp->fp_mant[2] = 0;
    111  1.1  briggs 	fpu_norm(fp);
    112  1.1  briggs 	return (FPC_NUM);
    113  1.1  briggs }
    114  1.1  briggs 
    115  1.1  briggs #define	mask(nbits) ((1 << (nbits)) - 1)
    116  1.1  briggs 
    117  1.1  briggs /*
    118  1.1  briggs  * All external floating formats convert to internal in the same manner,
    119  1.1  briggs  * as defined here.  Note that only normals get an implied 1.0 inserted.
    120  1.1  briggs  */
    121  1.1  briggs #define	FP_TOF(exp, expbias, allfrac, f0, f1, f2, f3) \
    122  1.1  briggs 	if (exp == 0) { \
    123  1.1  briggs 		if (allfrac == 0) \
    124  1.1  briggs 			return (FPC_ZERO); \
    125  1.1  briggs 		fp->fp_exp = 1 - expbias; \
    126  1.1  briggs 		fp->fp_mant[0] = f0; \
    127  1.1  briggs 		fp->fp_mant[1] = f1; \
    128  1.1  briggs 		fp->fp_mant[2] = f2; \
    129  1.1  briggs 		fpu_norm(fp); \
    130  1.1  briggs 		return (FPC_NUM); \
    131  1.1  briggs 	} \
    132  1.1  briggs 	if (exp == (2 * expbias + 1)) { \
    133  1.1  briggs 		if (allfrac == 0) \
    134  1.1  briggs 			return (FPC_INF); \
    135  1.1  briggs 		fp->fp_mant[0] = f0; \
    136  1.1  briggs 		fp->fp_mant[1] = f1; \
    137  1.1  briggs 		fp->fp_mant[2] = f2; \
    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.1  briggs 	return (FPC_NUM)
    145  1.1  briggs 
    146  1.1  briggs /*
    147  1.1  briggs  * 32-bit single precision -> fpn.
    148  1.1  briggs  * We assume a single occupies at most (64-FP_LG) bits in the internal
    149  1.1  briggs  * format: i.e., needs at most fp_mant[0] and fp_mant[1].
    150  1.1  briggs  */
    151  1.1  briggs static int
    152  1.1  briggs fpu_stof(fp, i)
    153  1.1  briggs 	register struct fpn *fp;
    154  1.1  briggs 	register u_int i;
    155  1.1  briggs {
    156  1.1  briggs 	register int exp;
    157  1.1  briggs 	register u_int frac, f0, f1;
    158  1.1  briggs #define SNG_SHIFT (SNG_FRACBITS - FP_LG)
    159  1.1  briggs 
    160  1.1  briggs 	exp = (i >> (32 - 1 - SNG_EXPBITS)) & mask(SNG_EXPBITS);
    161  1.1  briggs 	frac = i & mask(SNG_FRACBITS);
    162  1.1  briggs 	f0 = frac >> SNG_SHIFT;
    163  1.1  briggs 	f1 = frac << (32 - SNG_SHIFT);
    164  1.1  briggs 	FP_TOF(exp, SNG_EXP_BIAS, frac, f0, f1, 0, 0);
    165  1.1  briggs }
    166  1.1  briggs 
    167  1.1  briggs /*
    168  1.1  briggs  * 64-bit double -> fpn.
    169  1.1  briggs  * We assume this uses at most (96-FP_LG) bits.
    170  1.1  briggs  */
    171  1.1  briggs static int
    172  1.1  briggs fpu_dtof(fp, i, j)
    173  1.1  briggs 	register struct fpn *fp;
    174  1.1  briggs 	register u_int i, j;
    175  1.1  briggs {
    176  1.1  briggs 	register int exp;
    177  1.1  briggs 	register u_int frac, f0, f1, f2;
    178  1.1  briggs #define DBL_SHIFT (DBL_FRACBITS - 32 - FP_LG)
    179  1.1  briggs 
    180  1.1  briggs 	exp = (i >> (32 - 1 - DBL_EXPBITS)) & mask(DBL_EXPBITS);
    181  1.1  briggs 	frac = i & mask(DBL_FRACBITS - 32);
    182  1.1  briggs 	f0 = frac >> DBL_SHIFT;
    183  1.1  briggs 	f1 = (frac << (32 - DBL_SHIFT)) | (j >> DBL_SHIFT);
    184  1.1  briggs 	f2 = j << (32 - DBL_SHIFT);
    185  1.1  briggs 	frac |= j;
    186  1.1  briggs 	FP_TOF(exp, DBL_EXP_BIAS, frac, f0, f1, f2, 0);
    187  1.1  briggs }
    188  1.1  briggs 
    189  1.1  briggs /*
    190  1.1  briggs  * 96-bit extended -> fpn.
    191  1.1  briggs  */
    192  1.1  briggs static int
    193  1.1  briggs fpu_xtof(fp, i, j, k)
    194  1.1  briggs 	register struct fpn *fp;
    195  1.1  briggs 	register u_int i, j, k;
    196  1.1  briggs {
    197  1.1  briggs 	register int exp;
    198  1.1  briggs 	register u_int frac, f0, f1, f2;
    199  1.1  briggs #define EXT_SHIFT (EXT_FRACBITS - 1 - 32 - FP_LG)
    200  1.1  briggs 
    201  1.1  briggs 	exp = (i >> (32 - 1 - EXT_EXPBITS)) & mask(EXT_EXPBITS);
    202  1.1  briggs 	f0 = j >> EXT_SHIFT;
    203  1.1  briggs 	f1 = (j << (32 - EXT_SHIFT)) | (k >> EXT_SHIFT);
    204  1.1  briggs 	f2 = k << (32 - EXT_SHIFT);
    205  1.1  briggs 	frac = j | k;
    206  1.1  briggs 
    207  1.1  briggs 	/* m68k extended does not imply denormal by exp==0 */
    208  1.1  briggs 	if (exp == 0) {
    209  1.1  briggs 		if (frac == 0)
    210  1.1  briggs 			return (FPC_ZERO);
    211  1.1  briggs 		fp->fp_exp = - EXT_EXP_BIAS;
    212  1.1  briggs 		fp->fp_mant[0] = f0;
    213  1.1  briggs 		fp->fp_mant[1] = f1;
    214  1.1  briggs 		fp->fp_mant[2] = f2;
    215  1.1  briggs 		fpu_norm(fp);
    216  1.1  briggs 		return (FPC_NUM);
    217  1.1  briggs 	}
    218  1.1  briggs 	if (exp == (2 * EXT_EXP_BIAS + 1)) {
    219  1.1  briggs 		if (frac == 0)
    220  1.1  briggs 			return (FPC_INF);
    221  1.1  briggs 		fp->fp_mant[0] = f0;
    222  1.1  briggs 		fp->fp_mant[1] = f1;
    223  1.1  briggs 		fp->fp_mant[2] = f2;
    224  1.1  briggs 		return (FPC_QNAN);
    225  1.1  briggs 	}
    226  1.1  briggs 	fp->fp_exp = exp - EXT_EXP_BIAS;
    227  1.1  briggs 	fp->fp_mant[0] = FP_1 | f0;
    228  1.1  briggs 	fp->fp_mant[1] = f1;
    229  1.1  briggs 	fp->fp_mant[2] = f2;
    230  1.1  briggs 	return (FPC_NUM);
    231  1.1  briggs }
    232  1.1  briggs 
    233  1.1  briggs /*
    234  1.1  briggs  * Explode the contents of a memory operand.
    235  1.1  briggs  */
    236  1.1  briggs void
    237  1.1  briggs fpu_explode(fe, fp, type, space)
    238  1.1  briggs 	register struct fpemu *fe;
    239  1.1  briggs 	register struct fpn *fp;
    240  1.1  briggs 	int type;
    241  1.1  briggs 	register u_int *space;
    242  1.1  briggs {
    243  1.1  briggs 	register u_int s;
    244  1.1  briggs 
    245  1.1  briggs 	s = space[0];
    246  1.1  briggs 	fp->fp_sign = s >> 31;
    247  1.1  briggs 	fp->fp_sticky = 0;
    248  1.1  briggs 	switch (type) {
    249  1.1  briggs 
    250  1.1  briggs 	case FTYPE_BYT:
    251  1.1  briggs 		s >>= 8;
    252  1.1  briggs 	case FTYPE_WRD:
    253  1.1  briggs 		s >>= 16;
    254  1.1  briggs 	case FTYPE_LNG:
    255  1.1  briggs 		s = fpu_itof(fp, s);
    256  1.1  briggs 		break;
    257  1.1  briggs 
    258  1.1  briggs 	case FTYPE_SNG:
    259  1.1  briggs 		s = fpu_stof(fp, s);
    260  1.1  briggs 		break;
    261  1.1  briggs 
    262  1.1  briggs 	case FTYPE_DBL:
    263  1.1  briggs 		s = fpu_dtof(fp, s, space[1]);
    264  1.1  briggs 		break;
    265  1.1  briggs 
    266  1.1  briggs 	case FTYPE_EXT:
    267  1.1  briggs 		s = fpu_xtof(fp, s, space[1], space[2]);
    268  1.1  briggs 		break;
    269  1.1  briggs 
    270  1.1  briggs 	default:
    271  1.1  briggs 		panic("fpu_explode");
    272  1.1  briggs 	}
    273  1.1  briggs 	if (s == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
    274  1.1  briggs 		/*
    275  1.1  briggs 		 * Input is a signalling NaN.  All operations that return
    276  1.1  briggs 		 * an input NaN operand put it through a ``NaN conversion'',
    277  1.1  briggs 		 * which basically just means ``turn on the quiet bit''.
    278  1.1  briggs 		 * We do this here so that all NaNs internally look quiet
    279  1.1  briggs 		 * (we can tell signalling ones by their class).
    280  1.1  briggs 		 */
    281  1.1  briggs 		fp->fp_mant[0] |= FP_QUIETBIT;
    282  1.1  briggs 		fe->fe_fpsr |= FPSR_SNAN;	/* assert SNAN exception */
    283  1.1  briggs 		s = FPC_SNAN;
    284  1.1  briggs 	}
    285  1.1  briggs 	fp->fp_class = s;
    286  1.1  briggs }
    287