]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libc/sparc64/fpu/fpu_explode.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libc / sparc64 / fpu / fpu_explode.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  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)fpu_explode.c       8.1 (Berkeley) 6/11/93
39  *      $NetBSD: fpu_explode.c,v 1.5 2000/08/03 18:32:08 eeh Exp $
40  */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 /*
46  * FPU subroutines: `explode' the machine's `packed binary' format numbers
47  * into our internal format.
48  */
49
50 #include <sys/param.h>
51
52 #include <machine/frame.h>
53 #include <machine/fp.h>
54 #include <machine/fsr.h>
55 #include <machine/ieee.h>
56 #include <machine/instr.h>
57
58 #include "fpu_arith.h"
59 #include "fpu_emu.h"
60 #include "fpu_extern.h"
61 #include "__sparc_utrap_private.h"
62
63 /*
64  * N.B.: in all of the following, we assume the FP format is
65  *
66  *      ---------------------------
67  *      | s | exponent | fraction |
68  *      ---------------------------
69  *
70  * (which represents -1**s * 1.fraction * 2**exponent), so that the
71  * sign bit is way at the top (bit 31), the exponent is next, and
72  * then the remaining bits mark the fraction.  A zero exponent means
73  * zero or denormalized (0.fraction rather than 1.fraction), and the
74  * maximum possible exponent, 2bias+1, signals inf (fraction==0) or NaN.
75  *
76  * Since the sign bit is always the topmost bit---this holds even for
77  * integers---we set that outside all the *tof functions.  Each function
78  * returns the class code for the new number (but note that we use
79  * FPC_QNAN for all NaNs; fpu_explode will fix this if appropriate).
80  */
81
82 /*
83  * int -> fpn.
84  */
85 int
86 __fpu_itof(fp, i)
87         struct fpn *fp;
88         u_int i;
89 {
90
91         if (i == 0)
92                 return (FPC_ZERO);
93         /*
94          * The value FP_1 represents 2^FP_LG, so set the exponent
95          * there and let normalization fix it up.  Convert negative
96          * numbers to sign-and-magnitude.  Note that this relies on
97          * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
98          */
99         fp->fp_exp = FP_LG;
100         /*
101          * The sign bit decides whether i should be interpreted as
102          * a signed or unsigned entity.
103          */
104         if (fp->fp_sign && (int)i < 0)
105                 fp->fp_mant[0] = -i;
106         else
107                 fp->fp_mant[0] = i;
108         fp->fp_mant[1] = 0;
109         fp->fp_mant[2] = 0;
110         fp->fp_mant[3] = 0;
111         __fpu_norm(fp);
112         return (FPC_NUM);
113 }
114
115 /*
116  * 64-bit int -> fpn.
117  */
118 int
119 __fpu_xtof(fp, i)
120         struct fpn *fp;
121         u_int64_t i;
122 {
123
124         if (i == 0)
125                 return (FPC_ZERO);
126         /*
127          * The value FP_1 represents 2^FP_LG, so set the exponent
128          * there and let normalization fix it up.  Convert negative
129          * numbers to sign-and-magnitude.  Note that this relies on
130          * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
131          */
132         fp->fp_exp = FP_LG2;
133         /*
134          * The sign bit decides whether i should be interpreted as
135          * a signed or unsigned entity.
136          */
137         if (fp->fp_sign && (int64_t)i < 0)
138                 *((int64_t*)fp->fp_mant) = -i;
139         else
140                 *((int64_t*)fp->fp_mant) = i;
141         fp->fp_mant[2] = 0;
142         fp->fp_mant[3] = 0;
143         __fpu_norm(fp);
144         return (FPC_NUM);
145 }
146
147 #define mask(nbits) ((1L << (nbits)) - 1)
148
149 /*
150  * All external floating formats convert to internal in the same manner,
151  * as defined here.  Note that only normals get an implied 1.0 inserted.
152  */
153 #define FP_TOF(exp, expbias, allfrac, f0, f1, f2, f3) \
154         if (exp == 0) { \
155                 if (allfrac == 0) \
156                         return (FPC_ZERO); \
157                 fp->fp_exp = 1 - expbias; \
158                 fp->fp_mant[0] = f0; \
159                 fp->fp_mant[1] = f1; \
160                 fp->fp_mant[2] = f2; \
161                 fp->fp_mant[3] = f3; \
162                 __fpu_norm(fp); \
163                 return (FPC_NUM); \
164         } \
165         if (exp == (2 * expbias + 1)) { \
166                 if (allfrac == 0) \
167                         return (FPC_INF); \
168                 fp->fp_mant[0] = f0; \
169                 fp->fp_mant[1] = f1; \
170                 fp->fp_mant[2] = f2; \
171                 fp->fp_mant[3] = f3; \
172                 return (FPC_QNAN); \
173         } \
174         fp->fp_exp = exp - expbias; \
175         fp->fp_mant[0] = FP_1 | f0; \
176         fp->fp_mant[1] = f1; \
177         fp->fp_mant[2] = f2; \
178         fp->fp_mant[3] = f3; \
179         return (FPC_NUM)
180
181 /*
182  * 32-bit single precision -> fpn.
183  * We assume a single occupies at most (64-FP_LG) bits in the internal
184  * format: i.e., needs at most fp_mant[0] and fp_mant[1].
185  */
186 int
187 __fpu_stof(fp, i)
188         struct fpn *fp;
189         u_int i;
190 {
191         int exp;
192         u_int frac, f0, f1;
193 #define SNG_SHIFT (SNG_FRACBITS - FP_LG)
194
195         exp = (i >> (32 - 1 - SNG_EXPBITS)) & mask(SNG_EXPBITS);
196         frac = i & mask(SNG_FRACBITS);
197         f0 = frac >> SNG_SHIFT;
198         f1 = frac << (32 - SNG_SHIFT);
199         FP_TOF(exp, SNG_EXP_BIAS, frac, f0, f1, 0, 0);
200 }
201
202 /*
203  * 64-bit double -> fpn.
204  * We assume this uses at most (96-FP_LG) bits.
205  */
206 int
207 __fpu_dtof(fp, i, j)
208         struct fpn *fp;
209         u_int i, j;
210 {
211         int exp;
212         u_int frac, f0, f1, f2;
213 #define DBL_SHIFT (DBL_FRACBITS - 32 - FP_LG)
214
215         exp = (i >> (32 - 1 - DBL_EXPBITS)) & mask(DBL_EXPBITS);
216         frac = i & mask(DBL_FRACBITS - 32);
217         f0 = frac >> DBL_SHIFT;
218         f1 = (frac << (32 - DBL_SHIFT)) | (j >> DBL_SHIFT);
219         f2 = j << (32 - DBL_SHIFT);
220         frac |= j;
221         FP_TOF(exp, DBL_EXP_BIAS, frac, f0, f1, f2, 0);
222 }
223
224 /*
225  * 128-bit extended -> fpn.
226  */
227 int
228 __fpu_qtof(fp, i, j, k, l)
229         struct fpn *fp;
230         u_int i, j, k, l;
231 {
232         int exp;
233         u_int frac, f0, f1, f2, f3;
234 #define EXT_SHIFT (-(EXT_FRACBITS - 3 * 32 - FP_LG))    /* left shift! */
235
236         /*
237          * Note that ext and fpn `line up', hence no shifting needed.
238          */
239         exp = (i >> (32 - 1 - EXT_EXPBITS)) & mask(EXT_EXPBITS);
240         frac = i & mask(EXT_FRACBITS - 3 * 32);
241         f0 = (frac << EXT_SHIFT) | (j >> (32 - EXT_SHIFT));
242         f1 = (j << EXT_SHIFT) | (k >> (32 - EXT_SHIFT));
243         f2 = (k << EXT_SHIFT) | (l >> (32 - EXT_SHIFT));
244         f3 = l << EXT_SHIFT;
245         frac |= j | k | l;
246         FP_TOF(exp, EXT_EXP_BIAS, frac, f0, f1, f2, f3);
247 }
248
249 /*
250  * Explode the contents of a / regpair / regquad.
251  * If the input is a signalling NaN, an NV (invalid) exception
252  * will be set.  (Note that nothing but NV can occur until ALU
253  * operations are performed.)
254  */
255 void
256 __fpu_explode(fe, fp, type, reg)
257         struct fpemu *fe;
258         struct fpn *fp;
259         int type, reg;
260 {
261         u_int32_t s, *sp;
262         u_int64_t l[2];
263
264         if (type == FTYPE_LNG || type == FTYPE_DBL || type == FTYPE_EXT) {
265                 l[0] = __fpu_getreg64(reg & ~1);
266                 sp = (u_int32_t *)l;
267                 fp->fp_sign = sp[0] >> 31;
268         } else {
269                 s = __fpu_getreg(reg);
270                 fp->fp_sign = s >> 31;
271         }
272         fp->fp_sticky = 0;
273         switch (type) {
274         case FTYPE_LNG:
275                 s = __fpu_xtof(fp, l[0]);
276                 break;
277
278         case FTYPE_INT:
279                 s = __fpu_itof(fp, s);
280                 break;
281
282         case FTYPE_SNG:
283                 s = __fpu_stof(fp, s);
284                 break;
285
286         case FTYPE_DBL:
287                 s = __fpu_dtof(fp, sp[0], sp[1]);
288                 break;
289
290         case FTYPE_EXT:
291                 l[1] = __fpu_getreg64((reg & ~1) + 2);
292                 s = __fpu_qtof(fp, sp[0], sp[1], sp[2], sp[3]);
293                 break;
294
295         default:
296                 __utrap_panic("fpu_explode");
297         }
298
299         if (s == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
300                 /*
301                  * Input is a signalling NaN.  All operations that return
302                  * an input NaN operand put it through a ``NaN conversion'',
303                  * which basically just means ``turn on the quiet bit''.
304                  * We do this here so that all NaNs internally look quiet
305                  * (we can tell signalling ones by their class).
306                  */
307                 fp->fp_mant[0] |= FP_QUIETBIT;
308                 fe->fe_cx = FSR_NV;     /* assert invalid operand */
309                 s = FPC_SNAN;
310         }
311         fp->fp_class = s;
312         DPRINTF(FPE_REG, ("fpu_explode: %%%c%d => ", (type == FTYPE_LNG) ? 'x' :
313                 ((type == FTYPE_INT) ? 'i' :
314                         ((type == FTYPE_SNG) ? 's' :
315                                 ((type == FTYPE_DBL) ? 'd' :
316                                         ((type == FTYPE_EXT) ? 'q' : '?')))),
317                 reg));
318         DUMPFPN(FPE_REG, fp);
319         DPRINTF(FPE_REG, ("\n"));
320 }