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