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