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