Home | History | Annotate | Line # | Download | only in fpu
fpu_subr.c revision 1.1
      1  1.1  deraadt /*
      2  1.1  deraadt  * Copyright (c) 1992, 1993
      3  1.1  deraadt  *	The Regents of the University of California.  All rights reserved.
      4  1.1  deraadt  *
      5  1.1  deraadt  * This software was developed by the Computer Systems Engineering group
      6  1.1  deraadt  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      7  1.1  deraadt  * contributed to Berkeley.
      8  1.1  deraadt  *
      9  1.1  deraadt  * All advertising materials mentioning features or use of this software
     10  1.1  deraadt  * must display the following acknowledgement:
     11  1.1  deraadt  *	This product includes software developed by the University of
     12  1.1  deraadt  *	California, Lawrence Berkeley Laboratory.
     13  1.1  deraadt  *
     14  1.1  deraadt  * Redistribution and use in source and binary forms, with or without
     15  1.1  deraadt  * modification, are permitted provided that the following conditions
     16  1.1  deraadt  * are met:
     17  1.1  deraadt  * 1. Redistributions of source code must retain the above copyright
     18  1.1  deraadt  *    notice, this list of conditions and the following disclaimer.
     19  1.1  deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     20  1.1  deraadt  *    notice, this list of conditions and the following disclaimer in the
     21  1.1  deraadt  *    documentation and/or other materials provided with the distribution.
     22  1.1  deraadt  * 3. All advertising materials mentioning features or use of this software
     23  1.1  deraadt  *    must display the following acknowledgement:
     24  1.1  deraadt  *	This product includes software developed by the University of
     25  1.1  deraadt  *	California, Berkeley and its contributors.
     26  1.1  deraadt  * 4. Neither the name of the University nor the names of its contributors
     27  1.1  deraadt  *    may be used to endorse or promote products derived from this software
     28  1.1  deraadt  *    without specific prior written permission.
     29  1.1  deraadt  *
     30  1.1  deraadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     31  1.1  deraadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     32  1.1  deraadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  1.1  deraadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     34  1.1  deraadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35  1.1  deraadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36  1.1  deraadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37  1.1  deraadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     38  1.1  deraadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     39  1.1  deraadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     40  1.1  deraadt  * SUCH DAMAGE.
     41  1.1  deraadt  *
     42  1.1  deraadt  *	@(#)fpu_subr.c	8.1 (Berkeley) 6/11/93
     43  1.1  deraadt  *
     44  1.1  deraadt  * from: Header: fpu_subr.c,v 1.4 92/12/01 08:46:52 torek Exp
     45  1.1  deraadt  * $Id: fpu_subr.c,v 1.1 1993/10/02 10:23:01 deraadt Exp $
     46  1.1  deraadt  */
     47  1.1  deraadt 
     48  1.1  deraadt /*
     49  1.1  deraadt  * FPU subroutines.
     50  1.1  deraadt  */
     51  1.1  deraadt 
     52  1.1  deraadt #include <sys/types.h>
     53  1.1  deraadt 
     54  1.1  deraadt #include <machine/reg.h>
     55  1.1  deraadt 
     56  1.1  deraadt #include <sparc/fpu/fpu_arith.h>
     57  1.1  deraadt #include <sparc/fpu/fpu_emu.h>
     58  1.1  deraadt 
     59  1.1  deraadt /*
     60  1.1  deraadt  * Shift the given number right rsh bits.  Any bits that `fall off' will get
     61  1.1  deraadt  * shoved into the sticky field; we return the resulting sticky.  Note that
     62  1.1  deraadt  * shifting NaNs is legal (this will never shift all bits out); a NaN's
     63  1.1  deraadt  * sticky field is ignored anyway.
     64  1.1  deraadt  */
     65  1.1  deraadt int
     66  1.1  deraadt fpu_shr(register struct fpn *fp, register int rsh)
     67  1.1  deraadt {
     68  1.1  deraadt 	register u_int m0, m1, m2, m3, s;
     69  1.1  deraadt 	register int lsh;
     70  1.1  deraadt 
     71  1.1  deraadt #ifdef DIAGNOSTIC
     72  1.1  deraadt 	if (rsh <= 0 || (fp->fp_class != FPC_NUM && !ISNAN(fp)))
     73  1.1  deraadt 		panic("fpu_rightshift 1");
     74  1.1  deraadt #endif
     75  1.1  deraadt 
     76  1.1  deraadt 	m0 = fp->fp_mant[0];
     77  1.1  deraadt 	m1 = fp->fp_mant[1];
     78  1.1  deraadt 	m2 = fp->fp_mant[2];
     79  1.1  deraadt 	m3 = fp->fp_mant[3];
     80  1.1  deraadt 
     81  1.1  deraadt 	/* If shifting all the bits out, take a shortcut. */
     82  1.1  deraadt 	if (rsh >= FP_NMANT) {
     83  1.1  deraadt #ifdef DIAGNOSTIC
     84  1.1  deraadt 		if ((m0 | m1 | m2 | m3) == 0)
     85  1.1  deraadt 			panic("fpu_rightshift 2");
     86  1.1  deraadt #endif
     87  1.1  deraadt 		fp->fp_mant[0] = 0;
     88  1.1  deraadt 		fp->fp_mant[1] = 0;
     89  1.1  deraadt 		fp->fp_mant[2] = 0;
     90  1.1  deraadt 		fp->fp_mant[3] = 0;
     91  1.1  deraadt #ifdef notdef
     92  1.1  deraadt 		if ((m0 | m1 | m2 | m3) == 0)
     93  1.1  deraadt 			fp->fp_class = FPC_ZERO;
     94  1.1  deraadt 		else
     95  1.1  deraadt #endif
     96  1.1  deraadt 			fp->fp_sticky = 1;
     97  1.1  deraadt 		return (1);
     98  1.1  deraadt 	}
     99  1.1  deraadt 
    100  1.1  deraadt 	/* Squish out full words. */
    101  1.1  deraadt 	s = fp->fp_sticky;
    102  1.1  deraadt 	if (rsh >= 32 * 3) {
    103  1.1  deraadt 		s |= m3 | m2 | m1;
    104  1.1  deraadt 		m3 = m0, m2 = 0, m1 = 0, m0 = 0;
    105  1.1  deraadt 	} else if (rsh >= 32 * 2) {
    106  1.1  deraadt 		s |= m3 | m2;
    107  1.1  deraadt 		m3 = m1, m2 = m0, m1 = 0, m0 = 0;
    108  1.1  deraadt 	} else if (rsh >= 32) {
    109  1.1  deraadt 		s |= m3;
    110  1.1  deraadt 		m3 = m2, m2 = m1, m1 = m0, m0 = 0;
    111  1.1  deraadt 	}
    112  1.1  deraadt 
    113  1.1  deraadt 	/* Handle any remaining partial word. */
    114  1.1  deraadt 	if ((rsh &= 31) != 0) {
    115  1.1  deraadt 		lsh = 32 - rsh;
    116  1.1  deraadt 		s |= m3 << lsh;
    117  1.1  deraadt 		m3 = (m3 >> rsh) | (m2 << lsh);
    118  1.1  deraadt 		m2 = (m2 >> rsh) | (m1 << lsh);
    119  1.1  deraadt 		m1 = (m1 >> rsh) | (m0 << lsh);
    120  1.1  deraadt 		m0 >>= rsh;
    121  1.1  deraadt 	}
    122  1.1  deraadt 	fp->fp_mant[0] = m0;
    123  1.1  deraadt 	fp->fp_mant[1] = m1;
    124  1.1  deraadt 	fp->fp_mant[2] = m2;
    125  1.1  deraadt 	fp->fp_mant[3] = m3;
    126  1.1  deraadt 	fp->fp_sticky = s;
    127  1.1  deraadt 	return (s);
    128  1.1  deraadt }
    129  1.1  deraadt 
    130  1.1  deraadt /*
    131  1.1  deraadt  * Force a number to be normal, i.e., make its fraction have all zero
    132  1.1  deraadt  * bits before FP_1, then FP_1, then all 1 bits.  This is used for denorms
    133  1.1  deraadt  * and (sometimes) for intermediate results.
    134  1.1  deraadt  *
    135  1.1  deraadt  * Internally, this may use a `supernormal' -- a number whose fp_mant
    136  1.1  deraadt  * is greater than or equal to 2.0 -- so as a side effect you can hand it
    137  1.1  deraadt  * a supernormal and it will fix it (provided fp->fp_mant[3] == 0).
    138  1.1  deraadt  */
    139  1.1  deraadt void
    140  1.1  deraadt fpu_norm(register struct fpn *fp)
    141  1.1  deraadt {
    142  1.1  deraadt 	register u_int m0, m1, m2, m3, top, sup, nrm;
    143  1.1  deraadt 	register int lsh, rsh, exp;
    144  1.1  deraadt 
    145  1.1  deraadt 	exp = fp->fp_exp;
    146  1.1  deraadt 	m0 = fp->fp_mant[0];
    147  1.1  deraadt 	m1 = fp->fp_mant[1];
    148  1.1  deraadt 	m2 = fp->fp_mant[2];
    149  1.1  deraadt 	m3 = fp->fp_mant[3];
    150  1.1  deraadt 
    151  1.1  deraadt 	/* Handle severe subnormals with 32-bit moves. */
    152  1.1  deraadt 	if (m0 == 0) {
    153  1.1  deraadt 		if (m1)
    154  1.1  deraadt 			m0 = m1, m1 = m2, m2 = m3, m3 = 0, exp -= 32;
    155  1.1  deraadt 		else if (m2)
    156  1.1  deraadt 			m0 = m2, m1 = m3, m2 = 0, m3 = 0, exp -= 2 * 32;
    157  1.1  deraadt 		else if (m3)
    158  1.1  deraadt 			m0 = m3, m1 = 0, m2 = 0, m3 = 0, exp -= 3 * 32;
    159  1.1  deraadt 		else {
    160  1.1  deraadt 			fp->fp_class = FPC_ZERO;
    161  1.1  deraadt 			return;
    162  1.1  deraadt 		}
    163  1.1  deraadt 	}
    164  1.1  deraadt 
    165  1.1  deraadt 	/* Now fix any supernormal or remaining subnormal. */
    166  1.1  deraadt 	nrm = FP_1;
    167  1.1  deraadt 	sup = nrm << 1;
    168  1.1  deraadt 	if (m0 >= sup) {
    169  1.1  deraadt 		/*
    170  1.1  deraadt 		 * We have a supernormal number.  We need to shift it right.
    171  1.1  deraadt 		 * We may assume m3==0.
    172  1.1  deraadt 		 */
    173  1.1  deraadt 		for (rsh = 1, top = m0 >> 1; top >= sup; rsh++)	/* XXX slow */
    174  1.1  deraadt 			top >>= 1;
    175  1.1  deraadt 		exp += rsh;
    176  1.1  deraadt 		lsh = 32 - rsh;
    177  1.1  deraadt 		m3 = m2 << lsh;
    178  1.1  deraadt 		m2 = (m2 >> rsh) | (m1 << lsh);
    179  1.1  deraadt 		m1 = (m1 >> rsh) | (m0 << lsh);
    180  1.1  deraadt 		m0 = top;
    181  1.1  deraadt 	} else if (m0 < nrm) {
    182  1.1  deraadt 		/*
    183  1.1  deraadt 		 * We have a regular denorm (a subnormal number), and need
    184  1.1  deraadt 		 * to shift it left.
    185  1.1  deraadt 		 */
    186  1.1  deraadt 		for (lsh = 1, top = m0 << 1; top < nrm; lsh++)	/* XXX slow */
    187  1.1  deraadt 			top <<= 1;
    188  1.1  deraadt 		exp -= lsh;
    189  1.1  deraadt 		rsh = 32 - lsh;
    190  1.1  deraadt 		m0 = top | (m1 >> rsh);
    191  1.1  deraadt 		m1 = (m1 << lsh) | (m2 >> rsh);
    192  1.1  deraadt 		m2 = (m2 << lsh) | (m3 >> rsh);
    193  1.1  deraadt 		m3 <<= lsh;
    194  1.1  deraadt 	}
    195  1.1  deraadt 
    196  1.1  deraadt 	fp->fp_exp = exp;
    197  1.1  deraadt 	fp->fp_mant[0] = m0;
    198  1.1  deraadt 	fp->fp_mant[1] = m1;
    199  1.1  deraadt 	fp->fp_mant[2] = m2;
    200  1.1  deraadt 	fp->fp_mant[3] = m3;
    201  1.1  deraadt }
    202  1.1  deraadt 
    203  1.1  deraadt /*
    204  1.1  deraadt  * Concoct a `fresh' Quiet NaN per Appendix N.
    205  1.1  deraadt  * As a side effect, we set NV (invalid) for the current exceptions.
    206  1.1  deraadt  */
    207  1.1  deraadt struct fpn *
    208  1.1  deraadt fpu_newnan(register struct fpemu *fe)
    209  1.1  deraadt {
    210  1.1  deraadt 	register struct fpn *fp;
    211  1.1  deraadt 
    212  1.1  deraadt 	fe->fe_cx = FSR_NV;
    213  1.1  deraadt 	fp = &fe->fe_f3;
    214  1.1  deraadt 	fp->fp_class = FPC_QNAN;
    215  1.1  deraadt 	fp->fp_sign = 0;
    216  1.1  deraadt 	fp->fp_mant[0] = FP_1 - 1;
    217  1.1  deraadt 	fp->fp_mant[1] = fp->fp_mant[2] = fp->fp_mant[3] = ~0;
    218  1.1  deraadt 	return (fp);
    219  1.1  deraadt }
    220