1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1988,2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/* Utility functions for Sparc FPU simulator. */
#include <sys/fpu/fpu_simulator.h>
#include <sys/fpu/globals.h>
void
_fp_read_vfreg(
FPU_REGS_TYPE *pf, /* Old freg value. */
uint_t n, /* Want to read register n. */
fp_simd_type *pfpsd)
{
*pf = pfpsd->fp_current_pfregs->fpu_fr.fpu_regs[n];
}
void
_fp_write_vfreg(
FPU_REGS_TYPE *pf, /* New freg value. */
uint_t n, /* Want to read register n. */
fp_simd_type *pfpsd)
{
pfpsd->fp_current_pfregs->fpu_fr.fpu_regs[n] = *pf;
}
void
_fp_read_vdreg(
FPU_DREGS_TYPE *pd, /* Old dreg value. */
uint_t n, /* Want to read register n. */
fp_simd_type *pfpsd)
{
*pd = pfpsd->fp_current_pfregs->fpu_fr.fpu_dregs[n];
}
void
_fp_write_vdreg(
FPU_DREGS_TYPE *pd, /* New dreg value. */
uint_t n, /* Want to read register n. */
fp_simd_type *pfpsd)
{
pfpsd->fp_current_pfregs->fpu_fr.fpu_dregs[n] = *pd;
}
/*
* Normalize a number. Does not affect zeros, infs, or NaNs.
* The number will be normalized to 113 bit extended:
* 0x0001####, 0x########, 0x########, 0x########.
*/
void
fpu_normalize(unpacked *pu)
{
uint_t U, u0, u1, u2, u3, m, n, k;
u0 = pu->significand[0];
u1 = pu->significand[1];
u2 = pu->significand[2];
u3 = pu->significand[3];
if ((*pu).fpclass == fp_normal) {
if ((u0|u1|u2|u3) == 0) {
(*pu).fpclass = fp_zero;
return;
}
while (u0 == 0) {
u0 = u1; u1 = u2; u2 = u3; u3 = 0;
(*pu).exponent = (*pu).exponent - 32;
}
if (u0 >= 0x20000) { /* u3 should be zero */
n = 1; U = u0 >> 1;
while (U >= 0x20000) {
U >>= 1;
n += 1;
}
m = (1 << n)-1;
k = 32-n;
(*pu).exponent += n;
u3 = ((u2&m)<<k)|(u3>>n);
u2 = ((u1&m)<<k)|(u2>>n);
u1 = ((u0&m)<<k)|(u1>>n);
u0 = U;
} else if (u0 < 0x10000) {
n = 1; U = u0 << 1;
while (U < 0x10000) {
U <<= 1;
n += 1;
}
k = 32-n;
m = -(1 << k);
(*pu).exponent -= n;
u0 = (u0<<n)|((u1&m)>>k);
u1 = (u1<<n)|((u2&m)>>k);
u2 = (u2<<n)|((u3&m)>>k);
u3 = (u3<<n);
}
pu->significand[0] = u0;
pu->significand[1] = u1;
pu->significand[2] = u2;
pu->significand[3] = u3;
}
}
/*
* Right shift significand sticky by n bits.
*/
void
fpu_rightshift(unpacked *pu, int n)
{
uint_t m, k, j, u0, u1, u2, u3;
if (n > 113) { /* drastic */
if (((*pu).significand[0] | (*pu).significand[1]
| (*pu).significand[2] | (*pu).significand[3]) == 0) {
/* really zero */
pu->fpclass = fp_zero;
return;
} else {
pu->rounded = 0;
pu->sticky = 1;
pu->significand[3] = 0;
pu->significand[2] = 0;
pu->significand[1] = 0;
pu->significand[0] = 0;
return;
}
}
while (n >= 32) { /* big shift */
pu->sticky |= pu->rounded | (pu->significand[3]&0x7fffffff);
pu->rounded = (*pu).significand[3] >> 31;
(*pu).significand[3] = (*pu).significand[2];
(*pu).significand[2] = (*pu).significand[1];
(*pu).significand[1] = (*pu).significand[0];
(*pu).significand[0] = 0;
n -= 32;
}
if (n > 0) { /* small shift */
u0 = pu->significand[0];
u1 = pu->significand[1];
u2 = pu->significand[2];
u3 = pu->significand[3];
m = (1<<n)-1;
k = 32 - n;
j = (1<<(n-1))-1;
pu->sticky |= pu->rounded | (u3&j);
pu->rounded = (u3&m)>>(n-1);
pu->significand[3] = ((u2&m)<<k)|(u3>>n);
pu->significand[2] = ((u1&m)<<k)|(u2>>n);
pu->significand[1] = ((u0&m)<<k)|(u1>>n);
pu->significand[0] = u0>>n;
}
}
/*
* Set the exception bit in the current exception register.
*/
void
fpu_set_exception(pfpsd, ex)
fp_simd_type *pfpsd; /* Pointer to simulator data */
enum fp_exception_type ex;
{
pfpsd->fp_current_exceptions |= 1 << (int)ex;
}
/*
* Set invalid exception and error nan in *pu
*/
void
fpu_error_nan(pfpsd, pu)
fp_simd_type *pfpsd; /* Pointer to simulator data */
unpacked *pu;
{
fpu_set_exception(pfpsd, fp_invalid);
pu->sign = 0;
pu->significand[0] = 0x7fffffff;
pu->significand[1] = 0xffffffffUL;
pu->significand[2] = 0xffffffffUL;
pu->significand[3] = 0xffffffffUL;
}
/*
* the following fpu_add3wc should be inlined as
* .inline _fpu_add3wc, 3
* ld [%o1], %o4 ! sum = x
* addcc -1, %o3, %g0 ! restore last carry in cc reg
* addxcc %o4, %o2, %o4 ! sum = sum + y + last carry
* st %o4, [%o0] ! *z = sum
* addx %g0, %g0, %o0 ! return new carry
* .end
*/
uint_t
fpu_add3wc(uint_t *z, uint_t x, uint_t y, uint_t carry)
{ /* *z = x + y + carry, set carry; */
if (carry == 0) {
*z = x+y;
return (*z < y);
} else {
*z = x+y+1;
return (*z <= y);
}
}
/*
* The following fpu_sub3wc should be inlined as
* .inline _fpu_sub3wc, 3
* ld [%o1], %o4 ! sum = *x
* addcc -1, %o3, %g0 ! restore last carry in cc reg
* subxcc %o4, %o2, %o4 ! sum = sum - y - last carry
* st %o4, [%o0] ! *x = sum
* addx %g0, %g0, %o0 ! return new carry
* .end
*/
uint_t
fpu_sub3wc(uint_t *z, uint_t x, uint_t y, uint_t carry)
{ /* *z = x - y - carry, set carry; */
if (carry == 0) {
*z = x-y;
return (*z > x);
} else {
*z = x-y-1;
return (*z >= x);
}
}
/*
* the following fpu_neg2wc should be inlined as
* .inline _fpu_neg2wc, 2
* ld [%o1], %o3 ! tmp = *x
* addcc -1, %o2, %g0 ! restore last carry in cc reg
* subxcc %g0, %o3, %o3 ! sum = 0 - tmp - last carry
* st %o3, [%o0] ! *x = sum
* addx %g0, %g0, %o0 ! return new carry
* .end
*/
uint_t
fpu_neg2wc(uint_t *z, uint_t x, uint_t carry)
{ /* *x = 0 - *x - carry, set carry; */
if (carry == 0) {
*z = -x;
return ((*z) != 0);
} else {
*z = -x-1;
return (1);
}
}
int
fpu_cmpli(uint_t *x, uint_t *y, int n)
{ /* compare two uint_t array */
int i;
i = 0;
while (i < n) {
if (x[i] > y[i])
return (1);
else if (x[i] < y[i])
return (-1);
i++;
}
return (0);
}
#ifdef DEBUG
#include <sys/cmn_err.h>
/*
* Print out unpacked record.
*/
void
display_unpacked(pu)
unpacked *pu;
{
(void) printf(" unpacked ");
if (pu->sign)
(void) printf("-");
else
(void) printf("+");
switch (pu->fpclass) {
case fp_zero:
(void) printf("0 ");
break;
case fp_normal:
(void) printf("normal");
break;
case fp_infinity:
(void) printf("Inf ");
break;
case fp_quiet:
case fp_signaling:
(void) printf("nan ");
break;
}
(void) printf(" %X %X %X %X (%X, %X) exponent %X \n",
pu->significand[0], pu->significand[1], pu->significand[2],
pu->significand[3], (pu->rounded != 0),
(pu->sticky != 0), pu->exponent);
}
#endif
|