1118611Snjl/*
2118611Snjl * Copyright (c) 1992, 1993
3118611Snjl *	The Regents of the University of California.  All rights reserved.
4151937Sjkim *
5118611Snjl * This software was developed by the Computer Systems Engineering group
6118611Snjl * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7118611Snjl * contributed to Berkeley.
8118611Snjl *
9118611Snjl * All advertising materials mentioning features or use of this software
10118611Snjl * must display the following acknowledgement:
11118611Snjl *	This product includes software developed by the University of
12202771Sjkim *	California, Lawrence Berkeley Laboratory.
13118611Snjl *
14118611Snjl * Redistribution and use in source and binary forms, with or without
15118611Snjl * modification, are permitted provided that the following conditions
16118611Snjl * are met:
17118611Snjl * 1. Redistributions of source code must retain the above copyright
18118611Snjl *    notice, this list of conditions and the following disclaimer.
19118611Snjl * 2. Redistributions in binary form must reproduce the above copyright
20118611Snjl *    notice, this list of conditions and the following disclaimer in the
21118611Snjl *    documentation and/or other materials provided with the distribution.
22118611Snjl * 4. Neither the name of the University nor the names of its contributors
23118611Snjl *    may be used to endorse or promote products derived from this software
24118611Snjl *    without specific prior written permission.
25118611Snjl *
26118611Snjl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27118611Snjl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28118611Snjl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29118611Snjl * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30118611Snjl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31118611Snjl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32118611Snjl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33118611Snjl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34118611Snjl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35118611Snjl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36118611Snjl * SUCH DAMAGE.
37118611Snjl *
38118611Snjl *	@(#)fpu_subr.c	8.1 (Berkeley) 6/11/93
39118611Snjl *	$NetBSD: fpu_subr.c,v 1.3 1996/03/14 19:42:01 christos Exp $
40118611Snjl */
41118611Snjl
42118611Snjl#include <sys/cdefs.h>
43118611Snjl__FBSDID("$FreeBSD$");
44118611Snjl
45118611Snjl/*
46118611Snjl * FPU subroutines.
47118611Snjl */
48118611Snjl
49118611Snjl#include <sys/param.h>
50118611Snjl
51118611Snjl#include <machine/frame.h>
52118611Snjl#include <machine/fp.h>
53118611Snjl#include <machine/fsr.h>
54118611Snjl#include <machine/instr.h>
55118611Snjl
56118611Snjl#include "fpu_arith.h"
57118611Snjl#include "fpu_emu.h"
58118611Snjl#include "fpu_extern.h"
59118611Snjl#include "__sparc_utrap_private.h"
60118611Snjl
61118611Snjl/*
62118611Snjl * Shift the given number right rsh bits.  Any bits that `fall off' will get
63118611Snjl * shoved into the sticky field; we return the resulting sticky.  Note that
64118611Snjl * shifting NaNs is legal (this will never shift all bits out); a NaN's
65118611Snjl * sticky field is ignored anyway.
66118611Snjl */
67118611Snjlint
68118611Snjl__fpu_shr(struct fpn *fp, int rsh)
69118611Snjl{
70118611Snjl	u_int m0, m1, m2, m3, s;
71118611Snjl	int lsh;
72118611Snjl
73118611Snjl#ifdef DIAGNOSTIC
74118611Snjl	if (rsh <= 0 || (fp->fp_class != FPC_NUM && !ISNAN(fp)))
75118611Snjl		__utrap_panic("fpu_rightshift 1");
76118611Snjl#endif
77118611Snjl
78118611Snjl	m0 = fp->fp_mant[0];
79118611Snjl	m1 = fp->fp_mant[1];
80118611Snjl	m2 = fp->fp_mant[2];
81118611Snjl	m3 = fp->fp_mant[3];
82118611Snjl
83118611Snjl	/* If shifting all the bits out, take a shortcut. */
84118611Snjl	if (rsh >= FP_NMANT) {
85118611Snjl#ifdef DIAGNOSTIC
86118611Snjl		if ((m0 | m1 | m2 | m3) == 0)
87118611Snjl			__utrap_panic("fpu_rightshift 2");
88118611Snjl#endif
89118611Snjl		fp->fp_mant[0] = 0;
90118611Snjl		fp->fp_mant[1] = 0;
91118611Snjl		fp->fp_mant[2] = 0;
92118611Snjl		fp->fp_mant[3] = 0;
93118611Snjl#ifdef notdef
94118611Snjl		if ((m0 | m1 | m2 | m3) == 0)
95118611Snjl			fp->fp_class = FPC_ZERO;
96118611Snjl		else
97118611Snjl#endif
98118611Snjl			fp->fp_sticky = 1;
99118611Snjl		return (1);
100118611Snjl	}
101118611Snjl
102118611Snjl	/* Squish out full words. */
103118611Snjl	s = fp->fp_sticky;
104118611Snjl	if (rsh >= 32 * 3) {
105118611Snjl		s |= m3 | m2 | m1;
106118611Snjl		m3 = m0, m2 = 0, m1 = 0, m0 = 0;
107118611Snjl	} else if (rsh >= 32 * 2) {
108118611Snjl		s |= m3 | m2;
109118611Snjl		m3 = m1, m2 = m0, m1 = 0, m0 = 0;
110118611Snjl	} else if (rsh >= 32) {
111118611Snjl		s |= m3;
112118611Snjl		m3 = m2, m2 = m1, m1 = m0, m0 = 0;
113118611Snjl	}
114118611Snjl
115118611Snjl	/* Handle any remaining partial word. */
116118611Snjl	if ((rsh &= 31) != 0) {
117118611Snjl		lsh = 32 - rsh;
118118611Snjl		s |= m3 << lsh;
119118611Snjl		m3 = (m3 >> rsh) | (m2 << lsh);
120118611Snjl		m2 = (m2 >> rsh) | (m1 << lsh);
121118611Snjl		m1 = (m1 >> rsh) | (m0 << lsh);
122118611Snjl		m0 >>= rsh;
123118611Snjl	}
124118611Snjl	fp->fp_mant[0] = m0;
125118611Snjl	fp->fp_mant[1] = m1;
126118611Snjl	fp->fp_mant[2] = m2;
127118611Snjl	fp->fp_mant[3] = m3;
128118611Snjl	fp->fp_sticky = s;
129118611Snjl	return (s);
130118611Snjl}
131118611Snjl
132118611Snjl/*
133118611Snjl * Force a number to be normal, i.e., make its fraction have all zero
134118611Snjl * bits before FP_1, then FP_1, then all 1 bits.  This is used for denorms
135118611Snjl * and (sometimes) for intermediate results.
136118611Snjl *
137118611Snjl * Internally, this may use a `supernormal' -- a number whose fp_mant
138118611Snjl * is greater than or equal to 2.0 -- so as a side effect you can hand it
139118611Snjl * a supernormal and it will fix it (provided fp->fp_mant[3] == 0).
140118611Snjl */
141193529Sjkimvoid
142193529Sjkim__fpu_norm(struct fpn *fp)
143193529Sjkim{
144193529Sjkim	u_int m0, m1, m2, m3, top, sup, nrm;
145118611Snjl	int lsh, rsh, exp;
146151937Sjkim
147118611Snjl	exp = fp->fp_exp;
148151937Sjkim	m0 = fp->fp_mant[0];
149151937Sjkim	m1 = fp->fp_mant[1];
150151937Sjkim	m2 = fp->fp_mant[2];
151118611Snjl	m3 = fp->fp_mant[3];
152118611Snjl
153118611Snjl	/* Handle severe subnormals with 32-bit moves. */
154118611Snjl	if (m0 == 0) {
155118611Snjl		if (m1)
156118611Snjl			m0 = m1, m1 = m2, m2 = m3, m3 = 0, exp -= 32;
157118611Snjl		else if (m2)
158118611Snjl			m0 = m2, m1 = m3, m2 = 0, m3 = 0, exp -= 2 * 32;
159151937Sjkim		else if (m3)
160151937Sjkim			m0 = m3, m1 = 0, m2 = 0, m3 = 0, exp -= 3 * 32;
161151937Sjkim		else {
162118611Snjl			fp->fp_class = FPC_ZERO;
163118611Snjl			return;
164118611Snjl		}
165118611Snjl	}
166118611Snjl
167118611Snjl	/* Now fix any supernormal or remaining subnormal. */
168118611Snjl	nrm = FP_1;
169118611Snjl	sup = nrm << 1;
170118611Snjl	if (m0 >= sup) {
171118611Snjl		/*
172118611Snjl		 * We have a supernormal number.  We need to shift it right.
173118611Snjl		 * We may assume m3==0.
174118611Snjl		 */
175118611Snjl		for (rsh = 1, top = m0 >> 1; top >= sup; rsh++)	/* XXX slow */
176118611Snjl			top >>= 1;
177118611Snjl		exp += rsh;
178118611Snjl		lsh = 32 - rsh;
179118611Snjl		m3 = m2 << lsh;
180118611Snjl		m2 = (m2 >> rsh) | (m1 << lsh);
181118611Snjl		m1 = (m1 >> rsh) | (m0 << lsh);
182118611Snjl		m0 = top;
183118611Snjl	} else if (m0 < nrm) {
184118611Snjl		/*
185118611Snjl		 * We have a regular denorm (a subnormal number), and need
186118611Snjl		 * to shift it left.
187118611Snjl		 */
188118611Snjl		for (lsh = 1, top = m0 << 1; top < nrm; lsh++)	/* XXX slow */
189118611Snjl			top <<= 1;
190118611Snjl		exp -= lsh;
191193529Sjkim		rsh = 32 - lsh;
192193529Sjkim		m0 = top | (m1 >> rsh);
193193529Sjkim		m1 = (m1 << lsh) | (m2 >> rsh);
194193529Sjkim		m2 = (m2 << lsh) | (m3 >> rsh);
195193529Sjkim		m3 <<= lsh;
196193529Sjkim	}
197118611Snjl
198193529Sjkim	fp->fp_exp = exp;
199193529Sjkim	fp->fp_mant[0] = m0;
200193529Sjkim	fp->fp_mant[1] = m1;
201193529Sjkim	fp->fp_mant[2] = m2;
202151937Sjkim	fp->fp_mant[3] = m3;
203151937Sjkim}
204151937Sjkim
205118611Snjl/*
206118611Snjl * Concoct a `fresh' Quiet NaN per Appendix N.
207118611Snjl * As a side effect, we set NV (invalid) for the current exceptions.
208118611Snjl */
209118611Snjlstruct fpn *
210118611Snjl__fpu_newnan(struct fpemu *fe)
211118611Snjl{
212118611Snjl	struct fpn *fp;
213151937Sjkim
214151937Sjkim	fe->fe_cx = FSR_NV;
215151937Sjkim	fp = &fe->fe_f3;
216151937Sjkim	fp->fp_class = FPC_QNAN;
217118611Snjl	fp->fp_sign = 0;
218151937Sjkim	fp->fp_mant[0] = FP_1 - 1;
219151937Sjkim	fp->fp_mant[1] = fp->fp_mant[2] = fp->fp_mant[3] = ~0;
220118611Snjl	return (fp);
221151937Sjkim}
222151937Sjkim