1 /* $Id: math.c,v 1.1 1999/08/23 19:00:11 cort Exp $
2 * arch/ppc/math-emu/math.c
4 * Copyright (C) 1999 Eddie C. Dost (ecd@atecom.com)
7 #include <linux/config.h>
8 #include <linux/types.h>
9 #include <linux/sched.h>
11 #include <asm/uaccess.h>
12 #include <asm/processor.h>
14 #include "sfp-machine.h"
17 #define FLOATFUNC(x) extern int x(void *, void *, void *, void *)
71 #define OP31 0x1f /* 31 */
72 #define LFS 0x30 /* 48 */
73 #define LFSU 0x31 /* 49 */
74 #define LFD 0x32 /* 50 */
75 #define LFDU 0x33 /* 51 */
76 #define STFS 0x34 /* 52 */
77 #define STFSU 0x35 /* 53 */
78 #define STFD 0x36 /* 54 */
79 #define STFDU 0x37 /* 55 */
80 #define OP59 0x3b /* 59 */
81 #define OP63 0x3f /* 63 */
85 #define LFSX 0x217 /* 535 */
86 #define LFSUX 0x237 /* 567 */
87 #define LFDX 0x257 /* 599 */
88 #define LFDUX 0x277 /* 631 */
89 #define STFSX 0x297 /* 663 */
90 #define STFSUX 0x2b7 /* 695 */
91 #define STFDX 0x2d7 /* 727 */
92 #define STFDUX 0x2f7 /* 759 */
93 #define STFIWX 0x3d7 /* 983 */
97 #define FDIVS 0x012 /* 18 */
98 #define FSUBS 0x014 /* 20 */
99 #define FADDS 0x015 /* 21 */
100 #define FSQRTS 0x016 /* 22 */
101 #define FRES 0x018 /* 24 */
102 #define FMULS 0x019 /* 25 */
103 #define FMSUBS 0x01c /* 28 */
104 #define FMADDS 0x01d /* 29 */
105 #define FNMSUBS 0x01e /* 30 */
106 #define FNMADDS 0x01f /* 31 */
110 #define FDIV 0x012 /* 18 */
111 #define FSUB 0x014 /* 20 */
112 #define FADD 0x015 /* 21 */
113 #define FSQRT 0x016 /* 22 */
114 #define FSEL 0x017 /* 23 */
115 #define FMUL 0x019 /* 25 */
116 #define FRSQRTE 0x01a /* 26 */
117 #define FMSUB 0x01c /* 28 */
118 #define FMADD 0x01d /* 29 */
119 #define FNMSUB 0x01e /* 30 */
120 #define FNMADD 0x01f /* 31 */
123 #define FCMPU 0x000 /* 0 */
124 #define FRSP 0x00c /* 12 */
125 #define FCTIW 0x00e /* 14 */
126 #define FCTIWZ 0x00f /* 15 */
127 #define FCMPO 0x020 /* 32 */
128 #define MTFSB1 0x026 /* 38 */
129 #define FNEG 0x028 /* 40 */
130 #define MCRFS 0x040 /* 64 */
131 #define MTFSB0 0x046 /* 70 */
132 #define FMR 0x048 /* 72 */
133 #define MTFSFI 0x086 /* 134 */
134 #define FNABS 0x088 /* 136 */
135 #define FABS 0x108 /* 264 */
136 #define MFFS 0x247 /* 583 */
137 #define MTFSF 0x2c7 /* 711 */
156 #ifdef CONFIG_MATH_EMULATION
158 record_exception(struct pt_regs
*regs
, int eflag
)
166 if (eflag
& EFLAG_OVERFLOW
)
168 if (eflag
& EFLAG_UNDERFLOW
)
170 if (eflag
& EFLAG_DIVZERO
)
172 if (eflag
& EFLAG_INEXACT
)
174 if (eflag
& EFLAG_VXSNAN
)
175 fpscr
|= FPSCR_VXSNAN
;
176 if (eflag
& EFLAG_VXISI
)
177 fpscr
|= FPSCR_VXISI
;
178 if (eflag
& EFLAG_VXIDI
)
179 fpscr
|= FPSCR_VXIDI
;
180 if (eflag
& EFLAG_VXZDZ
)
181 fpscr
|= FPSCR_VXZDZ
;
182 if (eflag
& EFLAG_VXIMZ
)
183 fpscr
|= FPSCR_VXIMZ
;
184 if (eflag
& EFLAG_VXVC
)
186 if (eflag
& EFLAG_VXSOFT
)
187 fpscr
|= FPSCR_VXSOFT
;
188 if (eflag
& EFLAG_VXSQRT
)
189 fpscr
|= FPSCR_VXSQRT
;
190 if (eflag
& EFLAG_VXCVI
)
191 fpscr
|= FPSCR_VXCVI
;
194 fpscr
&= ~(FPSCR_VX
);
195 if (fpscr
& (FPSCR_VXSNAN
| FPSCR_VXISI
| FPSCR_VXIDI
|
196 FPSCR_VXZDZ
| FPSCR_VXIMZ
| FPSCR_VXVC
|
197 FPSCR_VXSOFT
| FPSCR_VXSQRT
| FPSCR_VXCVI
))
200 fpscr
&= ~(FPSCR_FEX
);
201 if (((fpscr
& FPSCR_VX
) && (fpscr
& FPSCR_VE
)) ||
202 ((fpscr
& FPSCR_OX
) && (fpscr
& FPSCR_OE
)) ||
203 ((fpscr
& FPSCR_UX
) && (fpscr
& FPSCR_UE
)) ||
204 ((fpscr
& FPSCR_ZX
) && (fpscr
& FPSCR_ZE
)) ||
205 ((fpscr
& FPSCR_XX
) && (fpscr
& FPSCR_XE
)))
210 return (fpscr
& FPSCR_FEX
) ? 1 : 0;
212 #endif /* CONFIG_MATH_EMULATION */
215 do_mathemu(struct pt_regs
*regs
)
217 void *op0
= 0, *op1
= 0, *op2
= 0, *op3
= 0;
218 unsigned long pc
= regs
->nip
;
222 #ifdef CONFIG_MATH_EMULATION
223 int (*func
)(void *, void *, void *, void *);
228 if (get_user(insn
, (u32
*)pc
))
231 #ifndef CONFIG_MATH_EMULATION
232 switch (insn
>> 26) {
234 idx
= (insn
>> 16) & 0x1f;
235 sdisp
= (insn
& 0xffff);
236 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
237 op1
= (void *)((idx
? regs
->gpr
[idx
] : 0) + sdisp
);
238 lfd(op0
, op1
, op2
, op3
);
241 idx
= (insn
>> 16) & 0x1f;
242 sdisp
= (insn
& 0xffff);
243 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
244 op1
= (void *)((idx
? regs
->gpr
[idx
] : 0) + sdisp
);
245 lfd(op0
, op1
, op2
, op3
);
246 regs
->gpr
[idx
] = (unsigned long)op1
;
249 idx
= (insn
>> 16) & 0x1f;
250 sdisp
= (insn
& 0xffff);
251 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
252 op1
= (void *)((idx
? regs
->gpr
[idx
] : 0) + sdisp
);
253 stfd(op0
, op1
, op2
, op3
);
256 idx
= (insn
>> 16) & 0x1f;
257 sdisp
= (insn
& 0xffff);
258 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
259 op1
= (void *)((idx
? regs
->gpr
[idx
] : 0) + sdisp
);
260 stfd(op0
, op1
, op2
, op3
);
261 regs
->gpr
[idx
] = (unsigned long)op1
;
264 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
265 op1
= (void *)¤t
->tss
.fpr
[(insn
>> 11) & 0x1f];
266 fmr(op0
, op1
, op2
, op3
);
271 #else /* CONFIG_MATH_EMULATION */
272 switch (insn
>> 26) {
273 case LFS
: func
= lfs
; type
= D
; break;
274 case LFSU
: func
= lfs
; type
= DU
; break;
275 case LFD
: func
= lfd
; type
= D
; break;
276 case LFDU
: func
= lfd
; type
= DU
; break;
277 case STFS
: func
= stfs
; type
= D
; break;
278 case STFSU
: func
= stfs
; type
= DU
; break;
279 case STFD
: func
= stfd
; type
= D
; break;
280 case STFDU
: func
= stfd
; type
= DU
; break;
283 switch ((insn
>> 1) & 0x3ff) {
284 case LFSX
: func
= lfs
; type
= XE
; break;
285 case LFSUX
: func
= lfs
; type
= XEU
; break;
286 case LFDX
: func
= lfd
; type
= XE
; break;
287 case LFDUX
: func
= lfd
; type
= XEU
; break;
288 case STFSX
: func
= stfs
; type
= XE
; break;
289 case STFSUX
: func
= stfs
; type
= XEU
; break;
290 case STFDX
: func
= stfd
; type
= XE
; break;
291 case STFDUX
: func
= stfd
; type
= XEU
; break;
292 case STFIWX
: func
= stfiwx
; type
= XE
; break;
299 switch ((insn
>> 1) & 0x1f) {
300 case FDIVS
: func
= fdivs
; type
= AB
; break;
301 case FSUBS
: func
= fsubs
; type
= AB
; break;
302 case FADDS
: func
= fadds
; type
= AB
; break;
303 case FSQRTS
: func
= fsqrts
; type
= AB
; break;
304 case FRES
: func
= fres
; type
= AB
; break;
305 case FMULS
: func
= fmuls
; type
= AC
; break;
306 case FMSUBS
: func
= fmsubs
; type
= ABC
; break;
307 case FMADDS
: func
= fmadds
; type
= ABC
; break;
308 case FNMSUBS
: func
= fnmsubs
; type
= ABC
; break;
309 case FNMADDS
: func
= fnmadds
; type
= ABC
; break;
317 switch ((insn
>> 1) & 0x1f) {
318 case FDIV
: func
= fdiv
; type
= AB
; break;
319 case FSUB
: func
= fsub
; type
= AB
; break;
320 case FADD
: func
= fadd
; type
= AB
; break;
321 case FSQRT
: func
= fsqrt
; type
= AB
; break;
322 case FSEL
: func
= fsel
; type
= ABC
; break;
323 case FMUL
: func
= fmul
; type
= AC
; break;
324 case FRSQRTE
: func
= frsqrte
; type
= AB
; break;
325 case FMSUB
: func
= fmsub
; type
= ABC
; break;
326 case FMADD
: func
= fmadd
; type
= ABC
; break;
327 case FNMSUB
: func
= fnmsub
; type
= ABC
; break;
328 case FNMADD
: func
= fnmadd
; type
= ABC
; break;
335 switch ((insn
>> 1) & 0x3ff) {
336 case FCMPU
: func
= fcmpu
; type
= XCR
; break;
337 case FRSP
: func
= frsp
; type
= XB
; break;
338 case FCTIW
: func
= fctiw
; type
= XB
; break;
339 case FCTIWZ
: func
= fctiwz
; type
= XB
; break;
340 case FCMPO
: func
= fcmpo
; type
= XCR
; break;
341 case MTFSB1
: func
= mtfsb1
; type
= XCRB
; break;
342 case FNEG
: func
= fneg
; type
= XB
; break;
343 case MCRFS
: func
= mcrfs
; type
= XCRL
; break;
344 case MTFSB0
: func
= mtfsb0
; type
= XCRB
; break;
345 case FMR
: func
= fmr
; type
= XB
; break;
346 case MTFSFI
: func
= mtfsfi
; type
= XCRI
; break;
347 case FNABS
: func
= fnabs
; type
= XB
; break;
348 case FABS
: func
= fabs
; type
= XB
; break;
349 case MFFS
: func
= mffs
; type
= X
; break;
350 case MTFSF
: func
= mtfsf
; type
= XFLB
; break;
362 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
363 op1
= (void *)¤t
->tss
.fpr
[(insn
>> 16) & 0x1f];
364 op2
= (void *)¤t
->tss
.fpr
[(insn
>> 11) & 0x1f];
368 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
369 op1
= (void *)¤t
->tss
.fpr
[(insn
>> 16) & 0x1f];
370 op2
= (void *)¤t
->tss
.fpr
[(insn
>> 6) & 0x1f];
374 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
375 op1
= (void *)¤t
->tss
.fpr
[(insn
>> 16) & 0x1f];
376 op2
= (void *)¤t
->tss
.fpr
[(insn
>> 11) & 0x1f];
377 op3
= (void *)¤t
->tss
.fpr
[(insn
>> 6) & 0x1f];
381 idx
= (insn
>> 16) & 0x1f;
382 sdisp
= (insn
& 0xffff);
383 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
384 op1
= (void *)((idx
? regs
->gpr
[idx
] : 0) + sdisp
);
388 idx
= (insn
>> 16) & 0x1f;
392 sdisp
= (insn
& 0xffff);
393 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
394 op1
= (void *)(regs
->gpr
[idx
] + sdisp
);
398 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
402 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
403 op1
= (void *)¤t
->tss
.fpr
[(insn
>> 16) & 0x1f];
407 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
408 op1
= (void *)¤t
->tss
.fpr
[(insn
>> 11) & 0x1f];
412 idx
= (insn
>> 16) & 0x1f;
416 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
417 op1
= (void *)(regs
->gpr
[idx
] + regs
->gpr
[(insn
>> 11) & 0x1f]);
421 idx
= (insn
>> 16) & 0x1f;
422 op0
= (void *)¤t
->tss
.fpr
[(insn
>> 21) & 0x1f];
423 op1
= (void *)((idx
? regs
->gpr
[idx
] : 0)
424 + regs
->gpr
[(insn
>> 11) & 0x1f]);
428 op0
= (void *)®s
->ccr
;
429 op1
= (void *)((insn
>> 23) & 0x7);
430 op2
= (void *)¤t
->tss
.fpr
[(insn
>> 16) & 0x1f];
431 op3
= (void *)¤t
->tss
.fpr
[(insn
>> 11) & 0x1f];
435 op0
= (void *)®s
->ccr
;
436 op1
= (void *)((insn
>> 23) & 0x7);
437 op2
= (void *)((insn
>> 18) & 0x7);
441 op0
= (void *)((insn
>> 21) & 0x1f);
445 op0
= (void *)((insn
>> 23) & 0x7);
446 op1
= (void *)((insn
>> 12) & 0xf);
450 op0
= (void *)((insn
>> 17) & 0xff);
451 op1
= (void *)¤t
->tss
.fpr
[(insn
>> 11) & 0x1f];
458 eflag
= func(op0
, op1
, op2
, op3
);
461 regs
->ccr
&= ~(0x0f000000);
462 regs
->ccr
|= (__FPU_FPSCR
>> 4) & 0x0f000000;
465 trap
= record_exception(regs
, eflag
);
472 regs
->gpr
[idx
] = (unsigned long)op1
;
478 #endif /* CONFIG_MATH_EMULATION */