2 * Routines to emulate some Altivec/VMX instructions, specifically
3 * those that can trap when given denormalized operands in Java mode.
5 #include <linux/kernel.h>
6 #include <linux/errno.h>
7 #include <linux/sched.h>
8 #include <asm/ptrace.h>
9 #include <asm/processor.h>
10 #include <asm/uaccess.h>
12 /* Functions in vector.S */
13 extern void vaddfp(vector128
*dst
, vector128
*a
, vector128
*b
);
14 extern void vsubfp(vector128
*dst
, vector128
*a
, vector128
*b
);
15 extern void vmaddfp(vector128
*dst
, vector128
*a
, vector128
*b
, vector128
*c
);
16 extern void vnmsubfp(vector128
*dst
, vector128
*a
, vector128
*b
, vector128
*c
);
17 extern void vrefp(vector128
*dst
, vector128
*src
);
18 extern void vrsqrtefp(vector128
*dst
, vector128
*src
);
19 extern void vexptep(vector128
*dst
, vector128
*src
);
21 static unsigned int exp2s
[8] = {
33 * Computes an estimate of 2^x. The `s' argument is the 32-bit
34 * single-precision floating-point representation of x.
36 static unsigned int eexp2(unsigned int s
)
39 unsigned int mant
, frac
;
41 /* extract exponent field from input */
42 exp
= ((s
>> 23) & 0xff) - 127;
44 /* check for NaN input */
45 if (exp
== 128 && (s
& 0x7fffff) != 0)
46 return s
| 0x400000; /* return QNaN */
47 /* 2^-big = 0, 2^+big = +Inf */
48 return (s
& 0x80000000)? 0: 0x7f800000; /* 0 or +Inf */
51 return 0x3f800000; /* 1.0 */
53 /* convert to fixed point integer in 9.23 representation */
54 pwr
= (s
& 0x7fffff) | 0x800000;
62 /* extract integer part, which becomes exponent part of result */
63 exp
= (pwr
>> 23) + 126;
69 /* table lookup on top 3 bits of fraction to get mantissa */
70 mant
= exp2s
[(pwr
>> 20) & 7];
72 /* linear interpolation using remaining 20 bits of fraction */
73 asm("mulhwu %0,%1,%2" : "=r" (frac
)
74 : "r" (pwr
<< 12), "r" (0x172b83ff));
75 asm("mulhwu %0,%1,%2" : "=r" (frac
) : "r" (frac
), "r" (mant
));
79 return mant
+ (exp
<< 23);
81 /* denormalized result */
83 mant
+= 1 << (exp
- 1);
88 * Computes an estimate of log_2(x). The `s' argument is the 32-bit
89 * single-precision floating-point representation of x.
91 static unsigned int elog2(unsigned int s
)
93 int exp
, mant
, lz
, frac
;
97 if (exp
== 0x7f800000) { /* Inf or NaN */
99 s
|= 0x400000; /* turn NaN into QNaN */
102 if ((exp
| mant
) == 0) /* +0 or -0 */
103 return 0xff800000; /* return -Inf */
107 asm("cntlzw %0,%1" : "=r" (lz
) : "r" (mant
));
109 exp
= (-118 - lz
) << 23;
115 if (mant
>= 0xb504f3) { /* 2^0.5 * 2^23 */
116 exp
|= 0x400000; /* 0.5 * 2^23 */
117 asm("mulhwu %0,%1,%2" : "=r" (mant
)
118 : "r" (mant
), "r" (0xb504f334)); /* 2^-0.5 * 2^32 */
120 if (mant
>= 0x9837f0) { /* 2^0.25 * 2^23 */
121 exp
|= 0x200000; /* 0.25 * 2^23 */
122 asm("mulhwu %0,%1,%2" : "=r" (mant
)
123 : "r" (mant
), "r" (0xd744fccb)); /* 2^-0.25 * 2^32 */
125 if (mant
>= 0x8b95c2) { /* 2^0.125 * 2^23 */
126 exp
|= 0x100000; /* 0.125 * 2^23 */
127 asm("mulhwu %0,%1,%2" : "=r" (mant
)
128 : "r" (mant
), "r" (0xeac0c6e8)); /* 2^-0.125 * 2^32 */
130 if (mant
> 0x800000) { /* 1.0 * 2^23 */
131 /* calculate (mant - 1) * 1.381097463 */
132 /* 1.381097463 == 0.125 / (2^0.125 - 1) */
133 asm("mulhwu %0,%1,%2" : "=r" (frac
)
134 : "r" ((mant
- 0x800000) << 1), "r" (0xb0c7cd3a));
137 s
= exp
& 0x80000000;
141 asm("cntlzw %0,%1" : "=r" (lz
) : "r" (exp
));
147 s
+= ((lz
+ 126) << 23) + exp
;
154 static int ctsxs(unsigned int x
, int scale
, unsigned int *vscrp
)
158 exp
= (x
>> 23) & 0xff;
160 if (exp
== 255 && mant
!= 0)
161 return 0; /* NaN -> 0 */
162 exp
= exp
- 127 + scale
;
164 return 0; /* round towards zero */
166 /* saturate, unless the result would be -2^31 */
167 if (x
+ (scale
<< 23) != 0xcf000000)
169 return (x
& 0x80000000)? 0x80000000: 0x7fffffff;
172 mant
= (mant
<< 7) >> (30 - exp
);
173 return (x
& 0x80000000)? -mant
: mant
;
176 static unsigned int ctuxs(unsigned int x
, int scale
, unsigned int *vscrp
)
181 exp
= (x
>> 23) & 0xff;
183 if (exp
== 255 && mant
!= 0)
184 return 0; /* NaN -> 0 */
185 exp
= exp
- 127 + scale
;
187 return 0; /* round towards zero */
188 if (x
& 0x80000000) {
189 /* negative => saturate to 0 */
199 mant
= (mant
<< 8) >> (31 - exp
);
203 /* Round to floating integer, towards 0 */
204 static unsigned int rfiz(unsigned int x
)
208 exp
= ((x
>> 23) & 0xff) - 127;
209 if (exp
== 128 && (x
& 0x7fffff) != 0)
210 return x
| 0x400000; /* NaN -> make it a QNaN */
212 return x
; /* it's an integer already (or Inf) */
214 return x
& 0x80000000; /* |x| < 1.0 rounds to 0 */
215 return x
& ~(0x7fffff >> exp
);
218 /* Round to floating integer, towards +/- Inf */
219 static unsigned int rfii(unsigned int x
)
223 exp
= ((x
>> 23) & 0xff) - 127;
224 if (exp
== 128 && (x
& 0x7fffff) != 0)
225 return x
| 0x400000; /* NaN -> make it a QNaN */
227 return x
; /* it's an integer already (or Inf) */
228 if ((x
& 0x7fffffff) == 0)
229 return x
; /* +/-0 -> +/-0 */
231 /* 0 < |x| < 1.0 rounds to +/- 1.0 */
232 return (x
& 0x80000000) | 0x3f800000;
233 mask
= 0x7fffff >> exp
;
234 /* mantissa overflows into exponent - that's OK,
235 it can't overflow into the sign bit */
236 return (x
+ mask
) & ~mask
;
239 /* Round to floating integer, to nearest */
240 static unsigned int rfin(unsigned int x
)
244 exp
= ((x
>> 23) & 0xff) - 127;
245 if (exp
== 128 && (x
& 0x7fffff) != 0)
246 return x
| 0x400000; /* NaN -> make it a QNaN */
248 return x
; /* it's an integer already (or Inf) */
250 return x
& 0x80000000; /* |x| < 0.5 -> +/-0 */
252 /* 0.5 <= |x| < 1.0 rounds to +/- 1.0 */
253 return (x
& 0x80000000) | 0x3f800000;
254 half
= 0x400000 >> exp
;
255 /* add 0.5 to the magnitude and chop off the fraction bits */
256 return (x
+ half
) & ~(0x7fffff >> exp
);
260 emulate_altivec(struct pt_regs
*regs
)
262 unsigned int instr
, i
;
263 unsigned int va
, vb
, vc
, vd
;
266 if (get_user(instr
, (unsigned int __user
*) regs
->nip
))
268 if ((instr
>> 26) != 4)
269 return -EINVAL
; /* not an altivec instruction */
270 vd
= (instr
>> 21) & 0x1f;
271 va
= (instr
>> 16) & 0x1f;
272 vb
= (instr
>> 11) & 0x1f;
273 vc
= (instr
>> 6) & 0x1f;
275 vrs
= current
->thread
.vr
;
276 switch (instr
& 0x3f) {
280 vaddfp(&vrs
[vd
], &vrs
[va
], &vrs
[vb
]);
283 vsubfp(&vrs
[vd
], &vrs
[va
], &vrs
[vb
]);
286 vrefp(&vrs
[vd
], &vrs
[vb
]);
288 case 5: /* vrsqrtefp */
289 vrsqrtefp(&vrs
[vd
], &vrs
[vb
]);
291 case 6: /* vexptefp */
292 for (i
= 0; i
< 4; ++i
)
293 vrs
[vd
].u
[i
] = eexp2(vrs
[vb
].u
[i
]);
295 case 7: /* vlogefp */
296 for (i
= 0; i
< 4; ++i
)
297 vrs
[vd
].u
[i
] = elog2(vrs
[vb
].u
[i
]);
300 for (i
= 0; i
< 4; ++i
)
301 vrs
[vd
].u
[i
] = rfin(vrs
[vb
].u
[i
]);
304 for (i
= 0; i
< 4; ++i
)
305 vrs
[vd
].u
[i
] = rfiz(vrs
[vb
].u
[i
]);
308 for (i
= 0; i
< 4; ++i
) {
309 u32 x
= vrs
[vb
].u
[i
];
310 x
= (x
& 0x80000000)? rfiz(x
): rfii(x
);
315 for (i
= 0; i
< 4; ++i
) {
316 u32 x
= vrs
[vb
].u
[i
];
317 x
= (x
& 0x80000000)? rfii(x
): rfiz(x
);
321 case 14: /* vctuxs */
322 for (i
= 0; i
< 4; ++i
)
323 vrs
[vd
].u
[i
] = ctuxs(vrs
[vb
].u
[i
], va
,
324 ¤t
->thread
.vscr
.u
[3]);
326 case 15: /* vctsxs */
327 for (i
= 0; i
< 4; ++i
)
328 vrs
[vd
].u
[i
] = ctsxs(vrs
[vb
].u
[i
], va
,
329 ¤t
->thread
.vscr
.u
[3]);
335 case 46: /* vmaddfp */
336 vmaddfp(&vrs
[vd
], &vrs
[va
], &vrs
[vb
], &vrs
[vc
]);
338 case 47: /* vnmsubfp */
339 vnmsubfp(&vrs
[vd
], &vrs
[va
], &vrs
[vb
], &vrs
[vc
]);