Home | History | Annotate | Line # | Download | only in fpe
      1  1.15   isaki /*	$NetBSD: fpu_implode.c,v 1.15 2013/03/26 11:30:21 isaki 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.7     agc  * 3. Neither the name of the University nor the names of its contributors
     25   1.1  briggs  *    may be used to endorse or promote products derived from this software
     26   1.1  briggs  *    without specific prior written permission.
     27   1.1  briggs  *
     28   1.1  briggs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.1  briggs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.1  briggs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.1  briggs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.1  briggs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.1  briggs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.1  briggs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.1  briggs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.1  briggs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.1  briggs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.1  briggs  * SUCH DAMAGE.
     39   1.1  briggs  *
     40   1.1  briggs  *	@(#)fpu_implode.c	8.1 (Berkeley) 6/11/93
     41   1.1  briggs  */
     42   1.1  briggs 
     43   1.1  briggs /*
     44   1.1  briggs  * FPU subroutines: `implode' internal format numbers into the machine's
     45   1.1  briggs  * `packed binary' format.
     46   1.1  briggs  */
     47   1.6   lukem 
     48   1.6   lukem #include <sys/cdefs.h>
     49  1.15   isaki __KERNEL_RCSID(0, "$NetBSD: fpu_implode.c,v 1.15 2013/03/26 11:30:21 isaki Exp $");
     50   1.1  briggs 
     51   1.1  briggs #include <sys/types.h>
     52   1.2  briggs #include <sys/systm.h>
     53   1.1  briggs 
     54   1.8  kleink #include <machine/ieee.h>
     55   1.1  briggs #include <machine/reg.h>
     56   1.1  briggs 
     57   1.1  briggs #include "fpu_emulate.h"
     58   1.1  briggs #include "fpu_arith.h"
     59   1.1  briggs 
     60   1.1  briggs /* Conversion from internal format -- note asymmetry. */
     61  1.15   isaki static uint32_t	fpu_ftoi(struct fpemu *fe, struct fpn *fp);
     62  1.15   isaki static uint32_t	fpu_ftos(struct fpemu *fe, struct fpn *fp);
     63  1.15   isaki static uint32_t	fpu_ftod(struct fpemu *fe, struct fpn *fp, uint32_t *);
     64  1.15   isaki static uint32_t	fpu_ftox(struct fpemu *fe, struct fpn *fp, uint32_t *);
     65   1.1  briggs 
     66   1.1  briggs /*
     67   1.1  briggs  * Round a number (algorithm from Motorola MC68882 manual, modified for
     68   1.1  briggs  * our internal format).  Set inexact exception if rounding is required.
     69   1.1  briggs  * Return true iff we rounded up.
     70   1.1  briggs  *
     71   1.1  briggs  * After rounding, we discard the guard and round bits by shifting right
     72   1.1  briggs  * 2 bits (a la fpu_shr(), but we do not bother with fp->fp_sticky).
     73   1.1  briggs  * This saves effort later.
     74   1.1  briggs  *
     75   1.1  briggs  * Note that we may leave the value 2.0 in fp->fp_mant; it is the caller's
     76   1.1  briggs  * responsibility to fix this if necessary.
     77   1.1  briggs  */
     78   1.1  briggs int
     79  1.13   isaki fpu_round(struct fpemu *fe, struct fpn *fp)
     80   1.1  briggs {
     81  1.15   isaki 	uint32_t m0, m1, m2;
     82  1.13   isaki 	int gr, s;
     83   1.1  briggs 
     84   1.1  briggs 	m0 = fp->fp_mant[0];
     85   1.1  briggs 	m1 = fp->fp_mant[1];
     86   1.1  briggs 	m2 = fp->fp_mant[2];
     87   1.4  briggs 	gr = m2 & 3;
     88   1.1  briggs 	s = fp->fp_sticky;
     89   1.1  briggs 
     90   1.1  briggs 	/* mant >>= FP_NG */
     91   1.1  briggs 	m2 = (m2 >> FP_NG) | (m1 << (32 - FP_NG));
     92   1.1  briggs 	m1 = (m1 >> FP_NG) | (m0 << (32 - FP_NG));
     93   1.1  briggs 	m0 >>= FP_NG;
     94   1.1  briggs 
     95   1.1  briggs 	if ((gr | s) == 0)	/* result is exact: no rounding needed */
     96   1.1  briggs 		goto rounddown;
     97   1.1  briggs 
     98   1.1  briggs 	fe->fe_fpsr |= FPSR_INEX2;	/* inexact */
     99   1.1  briggs 
    100   1.1  briggs 	/* Go to rounddown to round down; break to round up. */
    101   1.1  briggs 	switch (fe->fe_fpcr & FPCR_ROUND) {
    102   1.1  briggs 
    103   1.1  briggs 	case FPCR_NEAR:
    104   1.1  briggs 	default:
    105   1.1  briggs 		/*
    106   1.1  briggs 		 * Round only if guard is set (gr & 2).  If guard is set,
    107   1.1  briggs 		 * but round & sticky both clear, then we want to round
    108   1.1  briggs 		 * but have a tie, so round to even, i.e., add 1 iff odd.
    109   1.1  briggs 		 */
    110   1.1  briggs 		if ((gr & 2) == 0)
    111   1.1  briggs 			goto rounddown;
    112   1.4  briggs 		if ((gr & 1) || fp->fp_sticky || (m2 & 1))
    113   1.1  briggs 			break;
    114   1.1  briggs 		goto rounddown;
    115   1.1  briggs 
    116   1.1  briggs 	case FPCR_ZERO:
    117   1.1  briggs 		/* Round towards zero, i.e., down. */
    118   1.1  briggs 		goto rounddown;
    119   1.1  briggs 
    120   1.1  briggs 	case FPCR_MINF:
    121   1.1  briggs 		/* Round towards -Inf: up if negative, down if positive. */
    122   1.1  briggs 		if (fp->fp_sign)
    123   1.1  briggs 			break;
    124   1.1  briggs 		goto rounddown;
    125   1.1  briggs 
    126   1.1  briggs 	case FPCR_PINF:
    127   1.1  briggs 		/* Round towards +Inf: up if positive, down otherwise. */
    128   1.1  briggs 		if (!fp->fp_sign)
    129   1.1  briggs 			break;
    130   1.1  briggs 		goto rounddown;
    131   1.1  briggs 	}
    132   1.1  briggs 
    133   1.1  briggs 	/* Bump low bit of mantissa, with carry. */
    134   1.4  briggs 	if (++m2 == 0 && ++m1 == 0)
    135   1.1  briggs 		m0++;
    136   1.4  briggs 	fp->fp_sticky = 0;
    137   1.1  briggs 	fp->fp_mant[0] = m0;
    138   1.1  briggs 	fp->fp_mant[1] = m1;
    139   1.1  briggs 	fp->fp_mant[2] = m2;
    140   1.1  briggs 	return (1);
    141   1.1  briggs 
    142   1.1  briggs rounddown:
    143   1.4  briggs 	fp->fp_sticky = 0;
    144   1.1  briggs 	fp->fp_mant[0] = m0;
    145   1.1  briggs 	fp->fp_mant[1] = m1;
    146   1.1  briggs 	fp->fp_mant[2] = m2;
    147   1.1  briggs 	return (0);
    148   1.1  briggs }
    149   1.1  briggs 
    150   1.1  briggs /*
    151   1.1  briggs  * For overflow: return true if overflow is to go to +/-Inf, according
    152   1.1  briggs  * to the sign of the overflowing result.  If false, overflow is to go
    153   1.1  briggs  * to the largest magnitude value instead.
    154   1.1  briggs  */
    155   1.1  briggs static int
    156   1.1  briggs toinf(struct fpemu *fe, int sign)
    157   1.1  briggs {
    158   1.1  briggs 	int inf;
    159   1.1  briggs 
    160   1.1  briggs 	/* look at rounding direction */
    161   1.1  briggs 	switch (fe->fe_fpcr & FPCR_ROUND) {
    162   1.1  briggs 
    163   1.1  briggs 	default:
    164   1.1  briggs 	case FPCR_NEAR:		/* the nearest value is always Inf */
    165   1.1  briggs 		inf = 1;
    166   1.1  briggs 		break;
    167   1.1  briggs 
    168   1.1  briggs 	case FPCR_ZERO:		/* toward 0 => never towards Inf */
    169   1.1  briggs 		inf = 0;
    170   1.1  briggs 		break;
    171   1.1  briggs 
    172   1.1  briggs 	case FPCR_PINF:		/* toward +Inf iff positive */
    173   1.1  briggs 		inf = (sign == 0);
    174   1.1  briggs 		break;
    175   1.1  briggs 
    176   1.1  briggs 	case FPCR_MINF:		/* toward -Inf iff negative */
    177   1.1  briggs 		inf = sign;
    178   1.1  briggs 		break;
    179   1.1  briggs 	}
    180   1.1  briggs 	return (inf);
    181   1.1  briggs }
    182   1.1  briggs 
    183   1.1  briggs /*
    184   1.1  briggs  * fpn -> int (int value returned as return value).
    185   1.1  briggs  *
    186   1.1  briggs  * N.B.: this conversion always rounds towards zero (this is a peculiarity
    187   1.1  briggs  * of the SPARC instruction set).
    188   1.1  briggs  */
    189  1.15   isaki static uint32_t
    190  1.13   isaki fpu_ftoi(struct fpemu *fe, struct fpn *fp)
    191   1.1  briggs {
    192  1.15   isaki 	uint32_t i;
    193  1.13   isaki 	int sign, exp;
    194   1.1  briggs 
    195   1.1  briggs 	sign = fp->fp_sign;
    196   1.1  briggs 	switch (fp->fp_class) {
    197   1.1  briggs 	case FPC_ZERO:
    198   1.1  briggs 		return (0);
    199   1.1  briggs 
    200   1.1  briggs 	case FPC_NUM:
    201   1.1  briggs 		/*
    202   1.1  briggs 		 * If exp >= 2^32, overflow.  Otherwise shift value right
    203   1.1  briggs 		 * into last mantissa word (this will not exceed 0xffffffff),
    204   1.1  briggs 		 * shifting any guard and round bits out into the sticky
    205   1.1  briggs 		 * bit.  Then ``round'' towards zero, i.e., just set an
    206   1.5     chs 		 * inexact exception if sticky is set (see fpu_round()).
    207   1.1  briggs 		 * If the result is > 0x80000000, or is positive and equals
    208   1.1  briggs 		 * 0x80000000, overflow; otherwise the last fraction word
    209   1.1  briggs 		 * is the result.
    210   1.1  briggs 		 */
    211   1.1  briggs 		if ((exp = fp->fp_exp) >= 32)
    212   1.1  briggs 			break;
    213   1.1  briggs 		/* NB: the following includes exp < 0 cases */
    214  1.12   isaki 		if (fpu_shr(fp, FP_NMANT - 1 - FP_NG - exp) != 0) {
    215  1.12   isaki 			/*
    216  1.12   isaki 			 * m68881/2 do not underflow when
    217  1.12   isaki 			 * converting to integer
    218  1.12   isaki 			 */
    219  1.12   isaki 			;
    220  1.12   isaki 		}
    221   1.5     chs 		fpu_round(fe, fp);
    222   1.4  briggs 		i = fp->fp_mant[2];
    223  1.15   isaki 		if (i >= ((uint32_t)0x80000000 + sign))
    224   1.1  briggs 			break;
    225   1.1  briggs 		return (sign ? -i : i);
    226   1.1  briggs 
    227   1.1  briggs 	default:		/* Inf, qNaN, sNaN */
    228   1.1  briggs 		break;
    229   1.1  briggs 	}
    230   1.1  briggs 	/* overflow: replace any inexact exception with invalid */
    231   1.1  briggs 	fe->fe_fpsr = (fe->fe_fpsr & ~FPSR_INEX2) | FPSR_OPERR;
    232   1.1  briggs 	return (0x7fffffff + sign);
    233   1.1  briggs }
    234   1.1  briggs 
    235   1.1  briggs /*
    236   1.1  briggs  * fpn -> single (32 bit single returned as return value).
    237   1.1  briggs  * We assume <= 29 bits in a single-precision fraction (1.f part).
    238   1.1  briggs  */
    239  1.15   isaki static uint32_t
    240  1.13   isaki fpu_ftos(struct fpemu *fe, struct fpn *fp)
    241   1.1  briggs {
    242  1.15   isaki 	uint32_t sign = fp->fp_sign << 31;
    243  1.13   isaki 	int exp;
    244   1.1  briggs 
    245   1.1  briggs #define	SNG_EXP(e)	((e) << SNG_FRACBITS)	/* makes e an exponent */
    246   1.1  briggs #define	SNG_MASK	(SNG_EXP(1) - 1)	/* mask for fraction */
    247   1.1  briggs 
    248   1.1  briggs 	/* Take care of non-numbers first. */
    249   1.1  briggs 	if (ISNAN(fp)) {
    250   1.1  briggs 		/*
    251   1.1  briggs 		 * Preserve upper bits of NaN, per SPARC V8 appendix N.
    252   1.1  briggs 		 * Note that fp->fp_mant[0] has the quiet bit set,
    253   1.1  briggs 		 * even if it is classified as a signalling NaN.
    254   1.1  briggs 		 */
    255   1.1  briggs 		(void) fpu_shr(fp, FP_NMANT - 1 - SNG_FRACBITS);
    256   1.1  briggs 		exp = SNG_EXP_INFNAN;
    257   1.1  briggs 		goto done;
    258   1.1  briggs 	}
    259   1.1  briggs 	if (ISINF(fp))
    260   1.1  briggs 		return (sign | SNG_EXP(SNG_EXP_INFNAN));
    261   1.1  briggs 	if (ISZERO(fp))
    262   1.1  briggs 		return (sign);
    263   1.1  briggs 
    264   1.1  briggs 	/*
    265   1.1  briggs 	 * Normals (including subnormals).  Drop all the fraction bits
    266   1.1  briggs 	 * (including the explicit ``implied'' 1 bit) down into the
    267   1.1  briggs 	 * single-precision range.  If the number is subnormal, move
    268   1.1  briggs 	 * the ``implied'' 1 into the explicit range as well, and shift
    269   1.1  briggs 	 * right to introduce leading zeroes.  Rounding then acts
    270   1.1  briggs 	 * differently for normals and subnormals: the largest subnormal
    271   1.1  briggs 	 * may round to the smallest normal (1.0 x 2^minexp), or may
    272   1.1  briggs 	 * remain subnormal.  In the latter case, signal an underflow
    273   1.1  briggs 	 * if the result was inexact or if underflow traps are enabled.
    274   1.1  briggs 	 *
    275   1.1  briggs 	 * Rounding a normal, on the other hand, always produces another
    276   1.1  briggs 	 * normal (although either way the result might be too big for
    277   1.1  briggs 	 * single precision, and cause an overflow).  If rounding a
    278   1.1  briggs 	 * normal produces 2.0 in the fraction, we need not adjust that
    279   1.1  briggs 	 * fraction at all, since both 1.0 and 2.0 are zero under the
    280   1.1  briggs 	 * fraction mask.
    281   1.1  briggs 	 *
    282   1.1  briggs 	 * Note that the guard and round bits vanish from the number after
    283   1.1  briggs 	 * rounding.
    284   1.1  briggs 	 */
    285   1.1  briggs 	if ((exp = fp->fp_exp + SNG_EXP_BIAS) <= 0) {	/* subnormal */
    286   1.3      is 		fe->fe_fpsr |= FPSR_UNFL;
    287   1.1  briggs 		/* -NG for g,r; -SNG_FRACBITS-exp for fraction */
    288   1.1  briggs 		(void) fpu_shr(fp, FP_NMANT - FP_NG - SNG_FRACBITS - exp);
    289   1.5     chs 		if (fpu_round(fe, fp) && fp->fp_mant[2] == SNG_EXP(1))
    290   1.1  briggs 			return (sign | SNG_EXP(1) | 0);
    291  1.12   isaki 		if (fe->fe_fpsr & FPSR_INEX2) {
    292  1.12   isaki 			/* mc68881/2 don't underflow when converting */
    293  1.12   isaki 			fe->fe_fpsr |= FPSR_UNFL;
    294  1.12   isaki 		}
    295   1.4  briggs 		return (sign | SNG_EXP(0) | fp->fp_mant[2]);
    296   1.1  briggs 	}
    297   1.1  briggs 	/* -FP_NG for g,r; -1 for implied 1; -SNG_FRACBITS for fraction */
    298   1.1  briggs 	(void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - SNG_FRACBITS);
    299   1.1  briggs #ifdef DIAGNOSTIC
    300   1.4  briggs 	if ((fp->fp_mant[2] & SNG_EXP(1 << FP_NG)) == 0)
    301   1.1  briggs 		panic("fpu_ftos");
    302   1.1  briggs #endif
    303   1.5     chs 	if (fpu_round(fe, fp) && fp->fp_mant[2] == SNG_EXP(2))
    304   1.1  briggs 		exp++;
    305   1.1  briggs 	if (exp >= SNG_EXP_INFNAN) {
    306   1.1  briggs 		/* overflow to inf or to max single */
    307   1.3      is 		fe->fe_fpsr |= FPSR_OPERR | FPSR_INEX2 | FPSR_OVFL;
    308   1.1  briggs 		if (toinf(fe, sign))
    309   1.1  briggs 			return (sign | SNG_EXP(SNG_EXP_INFNAN));
    310   1.1  briggs 		return (sign | SNG_EXP(SNG_EXP_INFNAN - 1) | SNG_MASK);
    311   1.1  briggs 	}
    312   1.1  briggs done:
    313   1.1  briggs 	/* phew, made it */
    314   1.4  briggs 	return (sign | SNG_EXP(exp) | (fp->fp_mant[2] & SNG_MASK));
    315   1.1  briggs }
    316   1.1  briggs 
    317   1.1  briggs /*
    318   1.1  briggs  * fpn -> double (32 bit high-order result returned; 32-bit low order result
    319   1.1  briggs  * left in res[1]).  Assumes <= 61 bits in double precision fraction.
    320   1.1  briggs  *
    321   1.1  briggs  * This code mimics fpu_ftos; see it for comments.
    322   1.1  briggs  */
    323  1.15   isaki static uint32_t
    324  1.15   isaki fpu_ftod(struct fpemu *fe, struct fpn *fp, uint32_t *res)
    325   1.1  briggs {
    326  1.15   isaki 	uint32_t sign = fp->fp_sign << 31;
    327  1.13   isaki 	int exp;
    328   1.1  briggs 
    329   1.1  briggs #define	DBL_EXP(e)	((e) << (DBL_FRACBITS & 31))
    330   1.1  briggs #define	DBL_MASK	(DBL_EXP(1) - 1)
    331   1.1  briggs 
    332   1.1  briggs 	if (ISNAN(fp)) {
    333   1.1  briggs 		(void) fpu_shr(fp, FP_NMANT - 1 - DBL_FRACBITS);
    334   1.1  briggs 		exp = DBL_EXP_INFNAN;
    335   1.1  briggs 		goto done;
    336   1.1  briggs 	}
    337   1.1  briggs 	if (ISINF(fp)) {
    338   1.1  briggs 		sign |= DBL_EXP(DBL_EXP_INFNAN);
    339   1.1  briggs 		res[1] = 0;
    340   1.1  briggs 		return (sign);
    341   1.1  briggs 	}
    342   1.1  briggs 	if (ISZERO(fp)) {
    343   1.1  briggs 		res[1] = 0;
    344   1.1  briggs 		return (sign);
    345   1.1  briggs 	}
    346   1.1  briggs 
    347   1.1  briggs 	if ((exp = fp->fp_exp + DBL_EXP_BIAS) <= 0) {
    348   1.3      is 		fe->fe_fpsr |= FPSR_UNFL;
    349   1.1  briggs 		(void) fpu_shr(fp, FP_NMANT - FP_NG - DBL_FRACBITS - exp);
    350   1.5     chs 		if (fpu_round(fe, fp) && fp->fp_mant[1] == DBL_EXP(1)) {
    351   1.1  briggs 			res[1] = 0;
    352   1.1  briggs 			return (sign | DBL_EXP(1) | 0);
    353   1.1  briggs 		}
    354  1.12   isaki 		if (fe->fe_fpsr & FPSR_INEX2) {
    355  1.12   isaki 			/* mc68881/2 don't underflow when converting */
    356  1.12   isaki 			fe->fe_fpsr |= FPSR_UNFL;
    357  1.12   isaki 		}
    358   1.1  briggs 		exp = 0;
    359   1.1  briggs 		goto done;
    360   1.1  briggs 	}
    361   1.1  briggs 	(void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - DBL_FRACBITS);
    362   1.5     chs 	if (fpu_round(fe, fp) && fp->fp_mant[1] == DBL_EXP(2))
    363   1.1  briggs 		exp++;
    364   1.1  briggs 	if (exp >= DBL_EXP_INFNAN) {
    365   1.3      is 		fe->fe_fpsr |= FPSR_OPERR | FPSR_INEX2 | FPSR_OVFL;
    366   1.1  briggs 		if (toinf(fe, sign)) {
    367   1.1  briggs 			res[1] = 0;
    368   1.1  briggs 			return (sign | DBL_EXP(DBL_EXP_INFNAN) | 0);
    369   1.1  briggs 		}
    370   1.1  briggs 		res[1] = ~0;
    371   1.1  briggs 		return (sign | DBL_EXP(DBL_EXP_INFNAN) | DBL_MASK);
    372   1.1  briggs 	}
    373   1.1  briggs done:
    374   1.4  briggs 	res[1] = fp->fp_mant[2];
    375   1.4  briggs 	return (sign | DBL_EXP(exp) | (fp->fp_mant[1] & DBL_MASK));
    376   1.1  briggs }
    377   1.1  briggs 
    378   1.1  briggs /*
    379   1.1  briggs  * fpn -> 68k extended (32 bit high-order result returned; two 32-bit low
    380   1.1  briggs  * order result left in res[1] & res[2]).  Assumes == 64 bits in extended
    381   1.1  briggs  * precision fraction.
    382   1.1  briggs  *
    383   1.1  briggs  * This code mimics fpu_ftos; see it for comments.
    384   1.1  briggs  */
    385  1.15   isaki static uint32_t
    386  1.15   isaki fpu_ftox(struct fpemu *fe, struct fpn *fp, uint32_t *res)
    387   1.1  briggs {
    388  1.15   isaki 	uint32_t sign = fp->fp_sign << 31;
    389  1.13   isaki 	int exp;
    390   1.1  briggs 
    391   1.1  briggs #define	EXT_EXP(e)	((e) << 16)
    392   1.4  briggs /*
    393   1.4  briggs  * on m68k extended prec, significand does not share the same long
    394   1.4  briggs  * word with exponent
    395   1.4  briggs  */
    396   1.4  briggs #define	EXT_MASK	0
    397   1.4  briggs #define EXT_EXPLICIT1	(1UL << (63 & 31))
    398   1.4  briggs #define EXT_EXPLICIT2	(1UL << (64 & 31))
    399   1.1  briggs 
    400   1.1  briggs 	if (ISNAN(fp)) {
    401   1.4  briggs 		(void) fpu_shr(fp, FP_NMANT - EXT_FRACBITS);
    402   1.1  briggs 		exp = EXT_EXP_INFNAN;
    403   1.1  briggs 		goto done;
    404   1.1  briggs 	}
    405   1.1  briggs 	if (ISINF(fp)) {
    406   1.1  briggs 		sign |= EXT_EXP(EXT_EXP_INFNAN);
    407   1.1  briggs 		res[1] = res[2] = 0;
    408   1.1  briggs 		return (sign);
    409   1.1  briggs 	}
    410   1.1  briggs 	if (ISZERO(fp)) {
    411   1.1  briggs 		res[1] = res[2] = 0;
    412   1.1  briggs 		return (sign);
    413   1.1  briggs 	}
    414   1.1  briggs 
    415   1.4  briggs 	if ((exp = fp->fp_exp + EXT_EXP_BIAS) < 0) {
    416   1.3      is 		fe->fe_fpsr |= FPSR_UNFL;
    417  1.12   isaki 		/*
    418  1.12   isaki 		 * I'm not sure about this <=... exp==0 doesn't mean
    419  1.12   isaki 		 * it's a denormal in extended format
    420  1.12   isaki 		 */
    421   1.1  briggs 		(void) fpu_shr(fp, FP_NMANT - FP_NG - EXT_FRACBITS - exp);
    422   1.5     chs 		if (fpu_round(fe, fp) && fp->fp_mant[1] == EXT_EXPLICIT1) {
    423   1.1  briggs 			res[1] = res[2] = 0;
    424   1.1  briggs 			return (sign | EXT_EXP(1) | 0);
    425   1.1  briggs 		}
    426  1.12   isaki 		if (fe->fe_fpsr & FPSR_INEX2) {
    427  1.12   isaki 			/* mc68881/2 don't underflow */
    428  1.12   isaki 			fe->fe_fpsr |= FPSR_UNFL;
    429  1.12   isaki 		}
    430   1.1  briggs 		exp = 0;
    431   1.1  briggs 		goto done;
    432   1.1  briggs 	}
    433   1.4  briggs #if (FP_NMANT - FP_NG - EXT_FRACBITS) > 0
    434   1.1  briggs 	(void) fpu_shr(fp, FP_NMANT - FP_NG - EXT_FRACBITS);
    435   1.4  briggs #endif
    436  1.14   isaki 	if (fpu_round(fe, fp) && fp->fp_mant[0] == EXT_EXPLICIT2) {
    437   1.1  briggs 		exp++;
    438  1.14   isaki 		fpu_shr(fp, 1);
    439  1.14   isaki 	}
    440   1.1  briggs 	if (exp >= EXT_EXP_INFNAN) {
    441   1.3      is 		fe->fe_fpsr |= FPSR_OPERR | FPSR_INEX2 | FPSR_OVFL;
    442   1.1  briggs 		if (toinf(fe, sign)) {
    443   1.1  briggs 			res[1] = res[2] = 0;
    444   1.1  briggs 			return (sign | EXT_EXP(EXT_EXP_INFNAN) | 0);
    445   1.1  briggs 		}
    446   1.1  briggs 		res[1] = res[2] = ~0;
    447   1.1  briggs 		return (sign | EXT_EXP(EXT_EXP_INFNAN) | EXT_MASK);
    448   1.1  briggs 	}
    449   1.1  briggs done:
    450   1.4  briggs 	res[1] = fp->fp_mant[1];
    451   1.4  briggs 	res[2] = fp->fp_mant[2];
    452   1.1  briggs 	return (sign | EXT_EXP(exp));
    453   1.1  briggs }
    454   1.1  briggs 
    455   1.1  briggs /*
    456   1.1  briggs  * Implode an fpn, writing the result into the given space.
    457   1.1  briggs  */
    458   1.1  briggs void
    459  1.15   isaki fpu_implode(struct fpemu *fe, struct fpn *fp, int type, uint32_t *space)
    460   1.1  briggs {
    461   1.3      is 	/* XXX Dont delete exceptions set here: fe->fe_fpsr &= ~FPSR_EXCP; */
    462   1.1  briggs 
    463   1.1  briggs 	switch (type) {
    464   1.1  briggs 	case FTYPE_LNG:
    465   1.1  briggs 		space[0] = fpu_ftoi(fe, fp);
    466   1.1  briggs 		break;
    467   1.1  briggs 
    468   1.1  briggs 	case FTYPE_SNG:
    469   1.1  briggs 		space[0] = fpu_ftos(fe, fp);
    470   1.1  briggs 		break;
    471   1.1  briggs 
    472   1.1  briggs 	case FTYPE_DBL:
    473   1.1  briggs 		space[0] = fpu_ftod(fe, fp, space);
    474   1.1  briggs 		break;
    475   1.1  briggs 
    476   1.1  briggs 	case FTYPE_EXT:
    477   1.1  briggs 		/* funky rounding precision options ?? */
    478   1.1  briggs 		space[0] = fpu_ftox(fe, fp, space);
    479   1.1  briggs 		break;
    480   1.1  briggs 
    481   1.1  briggs 	default:
    482   1.1  briggs 		panic("fpu_implode");
    483   1.1  briggs 	}
    484   1.1  briggs }
    485