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