Home | History | Annotate | Line # | Download | only in fpu
fpu_implode.c revision 1.10.16.3
      1  1.10.16.3     skrll /*	$NetBSD: fpu_implode.c,v 1.10.16.3 2004/09/21 13:22:14 skrll 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.10.16.1     skrll  * 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_implode.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: `implode' internal format numbers into the machine's
     45        1.1   deraadt  * `packed binary' format.
     46        1.1   deraadt  */
     47        1.9   darrenr 
     48  1.10.16.1     skrll #include <sys/cdefs.h>
     49  1.10.16.3     skrll __KERNEL_RCSID(0, "$NetBSD: fpu_implode.c,v 1.10.16.3 2004/09/21 13:22:14 skrll Exp $");
     50  1.10.16.1     skrll 
     51        1.9   darrenr #if defined(_KERNEL_OPT)
     52        1.9   darrenr #include "opt_sparc_arch.h"
     53        1.9   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.3  christos 
     66        1.3  christos static int round __P((register struct fpemu *, register struct fpn *));
     67        1.3  christos static int toinf __P((struct fpemu *, int));
     68        1.1   deraadt 
     69        1.1   deraadt /*
     70        1.1   deraadt  * Round a number (algorithm from Motorola MC68882 manual, modified for
     71        1.1   deraadt  * our internal format).  Set inexact exception if rounding is required.
     72        1.1   deraadt  * Return true iff we rounded up.
     73        1.1   deraadt  *
     74        1.1   deraadt  * After rounding, we discard the guard and round bits by shifting right
     75        1.1   deraadt  * 2 bits (a la fpu_shr(), but we do not bother with fp->fp_sticky).
     76        1.1   deraadt  * This saves effort later.
     77        1.1   deraadt  *
     78        1.1   deraadt  * Note that we may leave the value 2.0 in fp->fp_mant; it is the caller's
     79        1.1   deraadt  * responsibility to fix this if necessary.
     80        1.1   deraadt  */
     81        1.1   deraadt static int
     82        1.1   deraadt round(register struct fpemu *fe, register struct fpn *fp)
     83        1.1   deraadt {
     84        1.1   deraadt 	register u_int m0, m1, m2, m3;
     85        1.3  christos 	register int gr, s;
     86        1.1   deraadt 
     87        1.1   deraadt 	m0 = fp->fp_mant[0];
     88        1.1   deraadt 	m1 = fp->fp_mant[1];
     89        1.1   deraadt 	m2 = fp->fp_mant[2];
     90        1.1   deraadt 	m3 = fp->fp_mant[3];
     91        1.1   deraadt 	gr = m3 & 3;
     92        1.1   deraadt 	s = fp->fp_sticky;
     93        1.1   deraadt 
     94        1.1   deraadt 	/* mant >>= FP_NG */
     95        1.1   deraadt 	m3 = (m3 >> FP_NG) | (m2 << (32 - FP_NG));
     96        1.1   deraadt 	m2 = (m2 >> FP_NG) | (m1 << (32 - FP_NG));
     97        1.1   deraadt 	m1 = (m1 >> FP_NG) | (m0 << (32 - FP_NG));
     98        1.1   deraadt 	m0 >>= FP_NG;
     99        1.1   deraadt 
    100        1.1   deraadt 	if ((gr | s) == 0)	/* result is exact: no rounding needed */
    101        1.1   deraadt 		goto rounddown;
    102        1.1   deraadt 
    103        1.1   deraadt 	fe->fe_cx |= FSR_NX;	/* inexact */
    104        1.1   deraadt 
    105        1.1   deraadt 	/* Go to rounddown to round down; break to round up. */
    106        1.1   deraadt 	switch ((fe->fe_fsr >> FSR_RD_SHIFT) & FSR_RD_MASK) {
    107        1.1   deraadt 
    108        1.1   deraadt 	case FSR_RD_RN:
    109        1.1   deraadt 	default:
    110        1.1   deraadt 		/*
    111        1.1   deraadt 		 * Round only if guard is set (gr & 2).  If guard is set,
    112        1.1   deraadt 		 * but round & sticky both clear, then we want to round
    113        1.1   deraadt 		 * but have a tie, so round to even, i.e., add 1 iff odd.
    114        1.1   deraadt 		 */
    115        1.1   deraadt 		if ((gr & 2) == 0)
    116        1.1   deraadt 			goto rounddown;
    117        1.1   deraadt 		if ((gr & 1) || fp->fp_sticky || (m3 & 1))
    118        1.1   deraadt 			break;
    119        1.1   deraadt 		goto rounddown;
    120        1.1   deraadt 
    121        1.1   deraadt 	case FSR_RD_RZ:
    122        1.1   deraadt 		/* Round towards zero, i.e., down. */
    123        1.1   deraadt 		goto rounddown;
    124        1.1   deraadt 
    125        1.1   deraadt 	case FSR_RD_RM:
    126        1.1   deraadt 		/* Round towards -Inf: up if negative, down if positive. */
    127        1.1   deraadt 		if (fp->fp_sign)
    128        1.1   deraadt 			break;
    129        1.1   deraadt 		goto rounddown;
    130        1.1   deraadt 
    131        1.1   deraadt 	case FSR_RD_RP:
    132        1.1   deraadt 		/* Round towards +Inf: up if positive, down otherwise. */
    133        1.1   deraadt 		if (!fp->fp_sign)
    134        1.1   deraadt 			break;
    135        1.1   deraadt 		goto rounddown;
    136        1.1   deraadt 	}
    137        1.1   deraadt 
    138        1.1   deraadt 	/* Bump low bit of mantissa, with carry. */
    139        1.1   deraadt 	FPU_ADDS(m3, m3, 1);
    140        1.1   deraadt 	FPU_ADDCS(m2, m2, 0);
    141        1.1   deraadt 	FPU_ADDCS(m1, m1, 0);
    142        1.1   deraadt 	FPU_ADDC(m0, m0, 0);
    143        1.1   deraadt 	fp->fp_mant[0] = m0;
    144        1.1   deraadt 	fp->fp_mant[1] = m1;
    145        1.1   deraadt 	fp->fp_mant[2] = m2;
    146        1.1   deraadt 	fp->fp_mant[3] = m3;
    147        1.1   deraadt 	return (1);
    148        1.1   deraadt 
    149        1.1   deraadt rounddown:
    150        1.1   deraadt 	fp->fp_mant[0] = m0;
    151        1.1   deraadt 	fp->fp_mant[1] = m1;
    152        1.1   deraadt 	fp->fp_mant[2] = m2;
    153        1.1   deraadt 	fp->fp_mant[3] = m3;
    154        1.1   deraadt 	return (0);
    155        1.1   deraadt }
    156        1.1   deraadt 
    157        1.1   deraadt /*
    158        1.1   deraadt  * For overflow: return true if overflow is to go to +/-Inf, according
    159        1.1   deraadt  * to the sign of the overflowing result.  If false, overflow is to go
    160        1.1   deraadt  * to the largest magnitude value instead.
    161        1.1   deraadt  */
    162        1.1   deraadt static int
    163        1.1   deraadt toinf(struct fpemu *fe, int sign)
    164        1.1   deraadt {
    165        1.1   deraadt 	int inf;
    166        1.1   deraadt 
    167        1.1   deraadt 	/* look at rounding direction */
    168        1.1   deraadt 	switch ((fe->fe_fsr >> FSR_RD_SHIFT) & FSR_RD_MASK) {
    169        1.1   deraadt 
    170        1.1   deraadt 	default:
    171        1.1   deraadt 	case FSR_RD_RN:		/* the nearest value is always Inf */
    172        1.1   deraadt 		inf = 1;
    173        1.1   deraadt 		break;
    174        1.1   deraadt 
    175        1.1   deraadt 	case FSR_RD_RZ:		/* toward 0 => never towards Inf */
    176        1.1   deraadt 		inf = 0;
    177        1.1   deraadt 		break;
    178        1.1   deraadt 
    179        1.1   deraadt 	case FSR_RD_RP:		/* toward +Inf iff positive */
    180        1.1   deraadt 		inf = sign == 0;
    181        1.1   deraadt 		break;
    182        1.1   deraadt 
    183        1.1   deraadt 	case FSR_RD_RM:		/* toward -Inf iff negative */
    184        1.1   deraadt 		inf = sign;
    185        1.1   deraadt 		break;
    186        1.1   deraadt 	}
    187        1.1   deraadt 	return (inf);
    188        1.1   deraadt }
    189        1.1   deraadt 
    190        1.1   deraadt /*
    191        1.1   deraadt  * fpn -> int (int value returned as return value).
    192        1.1   deraadt  *
    193        1.1   deraadt  * N.B.: this conversion always rounds towards zero (this is a peculiarity
    194        1.1   deraadt  * of the SPARC instruction set).
    195        1.1   deraadt  */
    196        1.1   deraadt u_int
    197        1.1   deraadt fpu_ftoi(fe, fp)
    198        1.1   deraadt 	struct fpemu *fe;
    199        1.1   deraadt 	register struct fpn *fp;
    200        1.1   deraadt {
    201        1.1   deraadt 	register u_int i;
    202        1.1   deraadt 	register int sign, exp;
    203        1.1   deraadt 
    204        1.1   deraadt 	sign = fp->fp_sign;
    205        1.1   deraadt 	switch (fp->fp_class) {
    206        1.1   deraadt 
    207        1.1   deraadt 	case FPC_ZERO:
    208        1.1   deraadt 		return (0);
    209        1.1   deraadt 
    210        1.1   deraadt 	case FPC_NUM:
    211        1.1   deraadt 		/*
    212        1.1   deraadt 		 * If exp >= 2^32, overflow.  Otherwise shift value right
    213        1.1   deraadt 		 * into last mantissa word (this will not exceed 0xffffffff),
    214        1.1   deraadt 		 * shifting any guard and round bits out into the sticky
    215        1.1   deraadt 		 * bit.  Then ``round'' towards zero, i.e., just set an
    216        1.1   deraadt 		 * inexact exception if sticky is set (see round()).
    217        1.1   deraadt 		 * If the result is > 0x80000000, or is positive and equals
    218        1.1   deraadt 		 * 0x80000000, overflow; otherwise the last fraction word
    219        1.1   deraadt 		 * is the result.
    220        1.1   deraadt 		 */
    221        1.1   deraadt 		if ((exp = fp->fp_exp) >= 32)
    222        1.1   deraadt 			break;
    223        1.1   deraadt 		/* NB: the following includes exp < 0 cases */
    224        1.1   deraadt 		if (fpu_shr(fp, FP_NMANT - 1 - exp) != 0)
    225        1.1   deraadt 			fe->fe_cx |= FSR_NX;
    226        1.1   deraadt 		i = fp->fp_mant[3];
    227        1.1   deraadt 		if (i >= ((u_int)0x80000000 + sign))
    228        1.1   deraadt 			break;
    229        1.1   deraadt 		return (sign ? -i : i);
    230        1.1   deraadt 
    231        1.1   deraadt 	default:		/* Inf, qNaN, sNaN */
    232        1.1   deraadt 		break;
    233        1.1   deraadt 	}
    234        1.1   deraadt 	/* overflow: replace any inexact exception with invalid */
    235        1.1   deraadt 	fe->fe_cx = (fe->fe_cx & ~FSR_NX) | FSR_NV;
    236        1.1   deraadt 	return (0x7fffffff + sign);
    237        1.1   deraadt }
    238        1.1   deraadt 
    239        1.5       mrg #ifdef SUN4U
    240        1.5       mrg /*
    241        1.5       mrg  * fpn -> extended int (high bits of int value returned as return value).
    242        1.5       mrg  *
    243        1.5       mrg  * N.B.: this conversion always rounds towards zero (this is a peculiarity
    244        1.5       mrg  * of the SPARC instruction set).
    245        1.5       mrg  */
    246        1.5       mrg u_int
    247        1.7       eeh fpu_ftox(fe, fp, res)
    248        1.5       mrg 	struct fpemu *fe;
    249        1.5       mrg 	register struct fpn *fp;
    250        1.5       mrg 	u_int *res;
    251        1.5       mrg {
    252        1.5       mrg 	register u_int64_t i;
    253        1.5       mrg 	register int sign, exp;
    254        1.5       mrg 
    255        1.5       mrg 	sign = fp->fp_sign;
    256        1.5       mrg 	switch (fp->fp_class) {
    257        1.5       mrg 
    258        1.5       mrg 	case FPC_ZERO:
    259        1.5       mrg 		res[1] = 0;
    260        1.5       mrg 		return (0);
    261        1.5       mrg 
    262        1.5       mrg 	case FPC_NUM:
    263        1.5       mrg 		/*
    264        1.5       mrg 		 * If exp >= 2^64, overflow.  Otherwise shift value right
    265        1.5       mrg 		 * into last mantissa word (this will not exceed 0xffffffffffffffff),
    266        1.5       mrg 		 * shifting any guard and round bits out into the sticky
    267        1.5       mrg 		 * bit.  Then ``round'' towards zero, i.e., just set an
    268        1.5       mrg 		 * inexact exception if sticky is set (see round()).
    269        1.5       mrg 		 * If the result is > 0x8000000000000000, or is positive and equals
    270        1.5       mrg 		 * 0x8000000000000000, overflow; otherwise the last fraction word
    271        1.5       mrg 		 * is the result.
    272        1.5       mrg 		 */
    273        1.5       mrg 		if ((exp = fp->fp_exp) >= 64)
    274        1.5       mrg 			break;
    275        1.5       mrg 		/* NB: the following includes exp < 0 cases */
    276        1.5       mrg 		if (fpu_shr(fp, FP_NMANT - 1 - exp) != 0)
    277        1.5       mrg 			fe->fe_cx |= FSR_NX;
    278        1.6   mycroft 		i = ((u_int64_t)fp->fp_mant[2]<<32)|fp->fp_mant[3];
    279        1.5       mrg 		if (i >= ((u_int64_t)0x8000000000000000LL + sign))
    280        1.5       mrg 			break;
    281       1.10       eeh 		if (sign) i = -i;
    282       1.10       eeh 		res[1] = (int)i;
    283       1.10       eeh 		return (i>>32);
    284        1.5       mrg 
    285        1.5       mrg 	default:		/* Inf, qNaN, sNaN */
    286        1.5       mrg 		break;
    287        1.5       mrg 	}
    288        1.5       mrg 	/* overflow: replace any inexact exception with invalid */
    289        1.5       mrg 	fe->fe_cx = (fe->fe_cx & ~FSR_NX) | FSR_NV;
    290        1.5       mrg 	return (0x7fffffffffffffffLL + sign);
    291        1.5       mrg }
    292        1.5       mrg #endif /* SUN4U */
    293        1.5       mrg 
    294        1.1   deraadt /*
    295        1.1   deraadt  * fpn -> single (32 bit single returned as return value).
    296        1.1   deraadt  * We assume <= 29 bits in a single-precision fraction (1.f part).
    297        1.1   deraadt  */
    298        1.1   deraadt u_int
    299        1.1   deraadt fpu_ftos(fe, fp)
    300        1.1   deraadt 	struct fpemu *fe;
    301        1.1   deraadt 	register struct fpn *fp;
    302        1.1   deraadt {
    303        1.1   deraadt 	register u_int sign = fp->fp_sign << 31;
    304        1.1   deraadt 	register int exp;
    305        1.1   deraadt 
    306        1.1   deraadt #define	SNG_EXP(e)	((e) << SNG_FRACBITS)	/* makes e an exponent */
    307        1.1   deraadt #define	SNG_MASK	(SNG_EXP(1) - 1)	/* mask for fraction */
    308        1.1   deraadt 
    309        1.1   deraadt 	/* Take care of non-numbers first. */
    310        1.1   deraadt 	if (ISNAN(fp)) {
    311        1.1   deraadt 		/*
    312        1.1   deraadt 		 * Preserve upper bits of NaN, per SPARC V8 appendix N.
    313        1.1   deraadt 		 * Note that fp->fp_mant[0] has the quiet bit set,
    314        1.1   deraadt 		 * even if it is classified as a signalling NaN.
    315        1.1   deraadt 		 */
    316        1.1   deraadt 		(void) fpu_shr(fp, FP_NMANT - 1 - SNG_FRACBITS);
    317        1.1   deraadt 		exp = SNG_EXP_INFNAN;
    318        1.1   deraadt 		goto done;
    319        1.1   deraadt 	}
    320        1.1   deraadt 	if (ISINF(fp))
    321        1.1   deraadt 		return (sign | SNG_EXP(SNG_EXP_INFNAN));
    322        1.1   deraadt 	if (ISZERO(fp))
    323        1.1   deraadt 		return (sign);
    324        1.1   deraadt 
    325        1.1   deraadt 	/*
    326        1.1   deraadt 	 * Normals (including subnormals).  Drop all the fraction bits
    327        1.1   deraadt 	 * (including the explicit ``implied'' 1 bit) down into the
    328        1.1   deraadt 	 * single-precision range.  If the number is subnormal, move
    329        1.1   deraadt 	 * the ``implied'' 1 into the explicit range as well, and shift
    330        1.1   deraadt 	 * right to introduce leading zeroes.  Rounding then acts
    331        1.1   deraadt 	 * differently for normals and subnormals: the largest subnormal
    332        1.1   deraadt 	 * may round to the smallest normal (1.0 x 2^minexp), or may
    333        1.1   deraadt 	 * remain subnormal.  In the latter case, signal an underflow
    334        1.1   deraadt 	 * if the result was inexact or if underflow traps are enabled.
    335        1.1   deraadt 	 *
    336        1.1   deraadt 	 * Rounding a normal, on the other hand, always produces another
    337        1.1   deraadt 	 * normal (although either way the result might be too big for
    338        1.1   deraadt 	 * single precision, and cause an overflow).  If rounding a
    339        1.1   deraadt 	 * normal produces 2.0 in the fraction, we need not adjust that
    340        1.1   deraadt 	 * fraction at all, since both 1.0 and 2.0 are zero under the
    341        1.1   deraadt 	 * fraction mask.
    342        1.1   deraadt 	 *
    343        1.1   deraadt 	 * Note that the guard and round bits vanish from the number after
    344        1.1   deraadt 	 * rounding.
    345        1.1   deraadt 	 */
    346        1.1   deraadt 	if ((exp = fp->fp_exp + SNG_EXP_BIAS) <= 0) {	/* subnormal */
    347        1.1   deraadt 		/* -NG for g,r; -SNG_FRACBITS-exp for fraction */
    348        1.1   deraadt 		(void) fpu_shr(fp, FP_NMANT - FP_NG - SNG_FRACBITS - exp);
    349        1.1   deraadt 		if (round(fe, fp) && fp->fp_mant[3] == SNG_EXP(1))
    350        1.1   deraadt 			return (sign | SNG_EXP(1) | 0);
    351        1.1   deraadt 		if ((fe->fe_cx & FSR_NX) ||
    352        1.1   deraadt 		    (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
    353        1.1   deraadt 			fe->fe_cx |= FSR_UF;
    354        1.1   deraadt 		return (sign | SNG_EXP(0) | fp->fp_mant[3]);
    355        1.1   deraadt 	}
    356        1.1   deraadt 	/* -FP_NG for g,r; -1 for implied 1; -SNG_FRACBITS for fraction */
    357        1.1   deraadt 	(void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - SNG_FRACBITS);
    358        1.1   deraadt #ifdef DIAGNOSTIC
    359        1.1   deraadt 	if ((fp->fp_mant[3] & SNG_EXP(1 << FP_NG)) == 0)
    360        1.1   deraadt 		panic("fpu_ftos");
    361        1.1   deraadt #endif
    362        1.1   deraadt 	if (round(fe, fp) && fp->fp_mant[3] == SNG_EXP(2))
    363        1.1   deraadt 		exp++;
    364        1.1   deraadt 	if (exp >= SNG_EXP_INFNAN) {
    365        1.1   deraadt 		/* overflow to inf or to max single */
    366        1.1   deraadt 		fe->fe_cx |= FSR_OF | FSR_NX;
    367        1.1   deraadt 		if (toinf(fe, sign))
    368        1.1   deraadt 			return (sign | SNG_EXP(SNG_EXP_INFNAN));
    369        1.1   deraadt 		return (sign | SNG_EXP(SNG_EXP_INFNAN - 1) | SNG_MASK);
    370        1.1   deraadt 	}
    371        1.1   deraadt done:
    372        1.1   deraadt 	/* phew, made it */
    373        1.1   deraadt 	return (sign | SNG_EXP(exp) | (fp->fp_mant[3] & SNG_MASK));
    374        1.1   deraadt }
    375        1.1   deraadt 
    376        1.1   deraadt /*
    377        1.1   deraadt  * fpn -> double (32 bit high-order result returned; 32-bit low order result
    378        1.1   deraadt  * left in res[1]).  Assumes <= 61 bits in double precision fraction.
    379        1.1   deraadt  *
    380        1.1   deraadt  * This code mimics fpu_ftos; see it for comments.
    381        1.1   deraadt  */
    382        1.1   deraadt u_int
    383        1.1   deraadt fpu_ftod(fe, fp, res)
    384        1.1   deraadt 	struct fpemu *fe;
    385        1.1   deraadt 	register struct fpn *fp;
    386        1.1   deraadt 	u_int *res;
    387        1.1   deraadt {
    388        1.1   deraadt 	register u_int sign = fp->fp_sign << 31;
    389        1.1   deraadt 	register int exp;
    390        1.1   deraadt 
    391        1.1   deraadt #define	DBL_EXP(e)	((e) << (DBL_FRACBITS & 31))
    392        1.1   deraadt #define	DBL_MASK	(DBL_EXP(1) - 1)
    393        1.1   deraadt 
    394        1.1   deraadt 	if (ISNAN(fp)) {
    395        1.1   deraadt 		(void) fpu_shr(fp, FP_NMANT - 1 - DBL_FRACBITS);
    396        1.1   deraadt 		exp = DBL_EXP_INFNAN;
    397        1.1   deraadt 		goto done;
    398        1.1   deraadt 	}
    399        1.1   deraadt 	if (ISINF(fp)) {
    400        1.1   deraadt 		sign |= DBL_EXP(DBL_EXP_INFNAN);
    401        1.1   deraadt 		goto zero;
    402        1.1   deraadt 	}
    403        1.1   deraadt 	if (ISZERO(fp)) {
    404        1.1   deraadt zero:		res[1] = 0;
    405        1.1   deraadt 		return (sign);
    406        1.1   deraadt 	}
    407        1.1   deraadt 
    408        1.1   deraadt 	if ((exp = fp->fp_exp + DBL_EXP_BIAS) <= 0) {
    409        1.1   deraadt 		(void) fpu_shr(fp, FP_NMANT - FP_NG - DBL_FRACBITS - exp);
    410        1.1   deraadt 		if (round(fe, fp) && fp->fp_mant[2] == DBL_EXP(1)) {
    411        1.1   deraadt 			res[1] = 0;
    412        1.1   deraadt 			return (sign | DBL_EXP(1) | 0);
    413        1.1   deraadt 		}
    414        1.1   deraadt 		if ((fe->fe_cx & FSR_NX) ||
    415        1.1   deraadt 		    (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
    416        1.1   deraadt 			fe->fe_cx |= FSR_UF;
    417        1.1   deraadt 		exp = 0;
    418        1.1   deraadt 		goto done;
    419        1.1   deraadt 	}
    420        1.1   deraadt 	(void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - DBL_FRACBITS);
    421        1.1   deraadt 	if (round(fe, fp) && fp->fp_mant[2] == DBL_EXP(2))
    422        1.1   deraadt 		exp++;
    423        1.1   deraadt 	if (exp >= DBL_EXP_INFNAN) {
    424        1.1   deraadt 		fe->fe_cx |= FSR_OF | FSR_NX;
    425        1.1   deraadt 		if (toinf(fe, sign)) {
    426        1.1   deraadt 			res[1] = 0;
    427        1.1   deraadt 			return (sign | DBL_EXP(DBL_EXP_INFNAN) | 0);
    428        1.1   deraadt 		}
    429        1.1   deraadt 		res[1] = ~0;
    430        1.1   deraadt 		return (sign | DBL_EXP(DBL_EXP_INFNAN) | DBL_MASK);
    431        1.1   deraadt 	}
    432        1.1   deraadt done:
    433        1.1   deraadt 	res[1] = fp->fp_mant[3];
    434        1.1   deraadt 	return (sign | DBL_EXP(exp) | (fp->fp_mant[2] & DBL_MASK));
    435        1.1   deraadt }
    436        1.1   deraadt 
    437        1.1   deraadt /*
    438        1.1   deraadt  * fpn -> extended (32 bit high-order result returned; low-order fraction
    439        1.1   deraadt  * words left in res[1]..res[3]).  Like ftod, which is like ftos ... but
    440        1.1   deraadt  * our internal format *is* extended precision, plus 2 bits for guard/round,
    441        1.1   deraadt  * so we can avoid a small bit of work.
    442        1.1   deraadt  */
    443        1.1   deraadt u_int
    444        1.7       eeh fpu_ftoq(fe, fp, res)
    445        1.1   deraadt 	struct fpemu *fe;
    446        1.1   deraadt 	register struct fpn *fp;
    447        1.1   deraadt 	u_int *res;
    448        1.1   deraadt {
    449        1.1   deraadt 	register u_int sign = fp->fp_sign << 31;
    450        1.1   deraadt 	register int exp;
    451        1.1   deraadt 
    452        1.1   deraadt #define	EXT_EXP(e)	((e) << (EXT_FRACBITS & 31))
    453        1.1   deraadt #define	EXT_MASK	(EXT_EXP(1) - 1)
    454        1.1   deraadt 
    455        1.1   deraadt 	if (ISNAN(fp)) {
    456        1.1   deraadt 		(void) fpu_shr(fp, 2);	/* since we are not rounding */
    457        1.1   deraadt 		exp = EXT_EXP_INFNAN;
    458        1.1   deraadt 		goto done;
    459        1.1   deraadt 	}
    460        1.1   deraadt 	if (ISINF(fp)) {
    461        1.1   deraadt 		sign |= EXT_EXP(EXT_EXP_INFNAN);
    462        1.1   deraadt 		goto zero;
    463        1.1   deraadt 	}
    464        1.1   deraadt 	if (ISZERO(fp)) {
    465        1.1   deraadt zero:		res[1] = res[2] = res[3] = 0;
    466        1.1   deraadt 		return (sign);
    467        1.1   deraadt 	}
    468        1.1   deraadt 
    469        1.1   deraadt 	if ((exp = fp->fp_exp + EXT_EXP_BIAS) <= 0) {
    470        1.1   deraadt 		(void) fpu_shr(fp, FP_NMANT - FP_NG - EXT_FRACBITS - exp);
    471        1.1   deraadt 		if (round(fe, fp) && fp->fp_mant[0] == EXT_EXP(1)) {
    472        1.1   deraadt 			res[1] = res[2] = res[3] = 0;
    473        1.1   deraadt 			return (sign | EXT_EXP(1) | 0);
    474        1.1   deraadt 		}
    475        1.1   deraadt 		if ((fe->fe_cx & FSR_NX) ||
    476        1.1   deraadt 		    (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
    477        1.1   deraadt 			fe->fe_cx |= FSR_UF;
    478        1.1   deraadt 		exp = 0;
    479        1.1   deraadt 		goto done;
    480        1.1   deraadt 	}
    481        1.1   deraadt 	/* Since internal == extended, no need to shift here. */
    482        1.1   deraadt 	if (round(fe, fp) && fp->fp_mant[0] == EXT_EXP(2))
    483        1.1   deraadt 		exp++;
    484        1.1   deraadt 	if (exp >= EXT_EXP_INFNAN) {
    485        1.1   deraadt 		fe->fe_cx |= FSR_OF | FSR_NX;
    486        1.1   deraadt 		if (toinf(fe, sign)) {
    487        1.1   deraadt 			res[1] = res[2] = res[3] = 0;
    488        1.1   deraadt 			return (sign | EXT_EXP(EXT_EXP_INFNAN) | 0);
    489        1.1   deraadt 		}
    490        1.1   deraadt 		res[1] = res[2] = res[3] = ~0;
    491        1.1   deraadt 		return (sign | EXT_EXP(EXT_EXP_INFNAN) | EXT_MASK);
    492        1.1   deraadt 	}
    493        1.1   deraadt done:
    494        1.1   deraadt 	res[1] = fp->fp_mant[1];
    495        1.1   deraadt 	res[2] = fp->fp_mant[2];
    496        1.1   deraadt 	res[3] = fp->fp_mant[3];
    497        1.1   deraadt 	return (sign | EXT_EXP(exp) | (fp->fp_mant[0] & EXT_MASK));
    498        1.1   deraadt }
    499        1.1   deraadt 
    500        1.1   deraadt /*
    501        1.1   deraadt  * Implode an fpn, writing the result into the given space.
    502        1.1   deraadt  */
    503        1.1   deraadt void
    504        1.1   deraadt fpu_implode(fe, fp, type, space)
    505        1.1   deraadt 	struct fpemu *fe;
    506        1.1   deraadt 	register struct fpn *fp;
    507        1.1   deraadt 	int type;
    508        1.1   deraadt 	register u_int *space;
    509        1.1   deraadt {
    510       1.10       eeh 
    511       1.10       eeh 	DPRINTF(FPE_REG, ("\n imploding: "));
    512       1.10       eeh 	DUMPFPN(FPE_REG, fp);
    513       1.10       eeh 	DPRINTF(FPE_REG, ("\n"));
    514        1.1   deraadt 
    515        1.1   deraadt 	switch (type) {
    516        1.5       mrg 
    517        1.5       mrg #ifdef SUN4U
    518        1.5       mrg 	case FTYPE_LNG:
    519        1.7       eeh 		space[0] = fpu_ftox(fe, fp, space);
    520        1.5       mrg 		break;
    521        1.5       mrg #endif /* SUN4U */
    522        1.1   deraadt 
    523        1.1   deraadt 	case FTYPE_INT:
    524        1.1   deraadt 		space[0] = fpu_ftoi(fe, fp);
    525        1.1   deraadt 		break;
    526        1.1   deraadt 
    527        1.1   deraadt 	case FTYPE_SNG:
    528        1.1   deraadt 		space[0] = fpu_ftos(fe, fp);
    529        1.1   deraadt 		break;
    530        1.1   deraadt 
    531        1.1   deraadt 	case FTYPE_DBL:
    532        1.1   deraadt 		space[0] = fpu_ftod(fe, fp, space);
    533        1.1   deraadt 		break;
    534        1.1   deraadt 
    535        1.1   deraadt 	case FTYPE_EXT:
    536        1.1   deraadt 		/* funky rounding precision options ?? */
    537        1.7       eeh 		space[0] = fpu_ftoq(fe, fp, space);
    538        1.1   deraadt 		break;
    539        1.1   deraadt 
    540        1.1   deraadt 	default:
    541        1.1   deraadt 		panic("fpu_implode");
    542        1.1   deraadt 	}
    543        1.8       eeh #ifdef SUN4U
    544        1.8       eeh 	DPRINTF(FPE_REG, ("fpu_implode: %x %x %x %x\n",
    545        1.8       eeh 		space[0], space[1], space[2], space[3]));
    546        1.8       eeh #else
    547        1.8       eeh 	DPRINTF(FPE_REG, ("fpu_implode: %x %x\n",
    548        1.8       eeh 		space[0], space[1]));
    549        1.8       eeh #endif
    550        1.1   deraadt }
    551