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