2 * this doesn't attempt to implement ARM floating-point properties
3 * that aren't visible in the Inferno environment.
4 * all arithmetic is done in double precision.
5 * the FP trap status isn't updated.
8 #include "../port/lib.h"
18 /* undef this if correct kernel r13 isn't in Ureg;
19 * check calculation in fpiarm below
23 #define REG(ur, x) (*(long*)(((char*)(ur))+roff[(x)]))
24 #define FR(ufp, x) (*(Internal*)(ufp)->regs[(x)&7])
26 typedef struct FP2 FP2;
27 typedef struct FP1 FP1;
31 void (*f)(Internal, Internal, Internal*);
36 void (*f)(Internal*, Internal*);
50 #define OFR(X) ((ulong)&((Ureg*)0)->X)
53 OFR(r0), OFR(r1), OFR(r2), OFR(r3),
54 OFR(r4), OFR(r5), OFR(r6), OFR(r7),
55 OFR(r8), OFR(r9), OFR(r10), OFR(r11),
56 OFR(r12), OFR(r13), OFR(r14), OFR(pc),
59 static Internal fpconst[8] = { /* indexed by op&7 */
61 {0, 0x1, 0x00000000, 0x00000000}, /* 0.0 */
62 {0, 0x3FF, 0x00000000, 0x08000000}, /* 1.0 */
63 {0, 0x400, 0x00000000, 0x08000000}, /* 2.0 */
64 {0, 0x400, 0x00000000, 0x0C000000}, /* 3.0 */
65 {0, 0x401, 0x00000000, 0x08000000}, /* 4.0 */
66 {0, 0x401, 0x00000000, 0x0A000000}, /* 5.0 */
67 {0, 0x3FE, 0x00000000, 0x08000000}, /* 0.5 */
68 {0, 0x402, 0x00000000, 0x0A000000}, /* 10.0 */
72 * arm binary operations
76 fadd(Internal m, Internal n, Internal *d)
78 (m.s == n.s? fpiadd: fpisub)(&m, &n, d);
82 fsub(Internal m, Internal n, Internal *d)
85 (m.s == n.s? fpiadd: fpisub)(&m, &n, d);
89 fsubr(Internal m, Internal n, Internal *d)
92 (n.s == m.s? fpiadd: fpisub)(&n, &m, d);
96 fmul(Internal m, Internal n, Internal *d)
102 fdiv(Internal m, Internal n, Internal *d)
108 fdivr(Internal m, Internal n, Internal *d)
114 * arm unary operations
118 fmov(Internal *m, Internal *d)
124 fmovn(Internal *m, Internal *d)
131 fabsf(Internal *m, Internal *d)
138 frnd(Internal *m, Internal *d)
142 (m->s? fsub: fadd)(fpconst[6], *m, d);
146 e = (d->e - ExpBias) + 1;
149 else if(e > FractBits){
151 d->l &= ~((1<<(2*FractBits - e))-1);
155 d->h &= ~((1<<(FractBits-e))-1);
159 static FP1 optab1[16] = { /* Fd := OP Fm */
164 [4] {"SQTF", /*fsqt*/0},
165 /* LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN all `deprecated' */
166 /* URD and NRM aren't implemented */
169 static FP2 optab2[16] = { /* Fd := Fn OP Fm */
173 [3] {"RSUBF", fsubr},
175 [5] {"RDIVF", fdivr},
176 /* POW, RPW deprecated */
177 [8] {"REMF", /*frem*/0},
178 [9] {"FMF", fmul}, /* fast multiply */
179 [10] {"FDV", fdiv}, /* fast divide */
180 [11] {"FRD", fdivr}, /* fast reverse divide */
185 fcmp(Internal *n, Internal *m)
190 if(IsWeird(m) || IsWeird(n)){
191 /* BUG: should trap if not masked */
198 i = fpicmp(&rn, &rm);
208 fld(void (*f)(Internal*, void*), int d, ulong ea, int n, FPsave *ufp)
213 (*f)(&FR(ufp, d), mem);
215 print("MOV%c #%lux, F%d\n", n==8? 'D': 'F', ea, d);
219 fst(void (*f)(void*, Internal*), ulong ea, int s, int n, FPsave *ufp)
227 print("MOV%c F%d,#%lux\n", n==8? 'D': 'F', s, ea);
232 condok(int cc, int c)
237 case 1: /* Z clear */
241 case 3: /* C clear */
245 case 5: /* N clear */
249 case 7: /* V clear */
251 case 8: /* C set and Z clear */
252 return cc&C && (cc&Z) == 0;
253 case 9: /* C clear or Z set */
254 return (cc&C) == 0 || cc&Z;
255 case 10: /* N set and V set, or N clear and V clear */
256 return (~cc&(N|V))==0 || (cc&(N|V)) == 0;
257 case 11: /* N set and V clear, or N clear and V set */
258 return (cc&(N|V))==N || (cc&(N|V))==V;
259 case 12: /* Z clear, and either N set and V set or N clear and V clear */
260 return (cc&Z) == 0 && ((~cc&(N|V))==0 || (cc&(N|V))==0);
261 case 13: /* Z set, or N set and V clear or N clear and V set */
262 return (cc&Z) || (cc&(N|V))==N || (cc&(N|V))==V;
263 case 14: /* always */
265 case 15: /* never (reserved) */
268 return 0; /* not reached */
272 unimp(ulong pc, ulong op)
276 snprint(buf, sizeof(buf), "sys: fp: pc=%lux unimp fp 0x%.8lux", pc, op);
278 print("FPE: %s\n", buf);
284 fpemu(ulong pc, ulong op, Ureg *ur, FPsave *ufp)
289 Internal tmp, *fm, *fn;
291 /* note: would update fault status here if we noted numeric exceptions */
296 if(((op>>25)&7) == 6){
298 unimp(pc, op); /* packed or extended */
301 if((op & (1<<23)) == 0)
311 fld(fpid2i, rd, ea, 8, ufp);
313 fld(fpis2i, rd, ea, 4, ufp);
316 fst(fpii2d, ea, rd, 8, ufp);
318 fst(fpii2s, ea, rd, 4, ufp);
320 if((op & (1<<24)) == 0)
328 * CPRT/transfer, 10.3
336 if(rd == 15 && op & (1<<20)){
349 case 4: /* CMF: Fn :: Fm */
350 case 6: /* CMFE: Fn :: Fm (with exception) */
351 ur->psr &= ~(N|C|Z|V);
352 ur->psr |= fcmp(fn, fm);
354 case 5: /* CNF: Fn :: -Fm */
355 case 7: /* CNFE: Fn :: -Fm (with exception) */
358 ur->psr &= ~(N|C|Z|V);
359 ur->psr |= fcmp(fn, &tmp);
363 print("CMPF %c%d,F%ld =%lux\n", tag, rn, op&7, ur->psr>>28);
368 * other transfer, 10.3
370 switch((op>>20)&0xF){
375 fpiw2i(&FR(ufp, rn), ®(ur, rd));
377 print("MOVW[FD] R%d, F%d\n", rd, rn);
384 fpii2w(®(ur, rd), &tmp);
386 print("MOV[FD]W F%d, R%d =%ld\n", rn, rd, REG(ur, rd));
388 case 2: /* FPSR := Rd */
389 ufp->status = REG(ur, rd);
391 print("MOVW R%d, FPSR\n", rd);
393 case 3: /* Rd := FPSR */
394 REG(ur, rd) = ufp->status;
396 print("MOVW FPSR, R%d\n", rd);
398 case 4: /* FPCR := Rd */
399 ufp->control = REG(ur, rd);
401 print("MOVW R%d, FPCR\n", rd);
403 case 5: /* Rd := FPCR */
404 REG(ur, rd) = ufp->control;
406 print("MOVW FPCR, R%d\n", rd);
416 if(op & (1<<3)){ /* constant */
425 if(op & (1<<15)){ /* monadic */
431 print("%s %c%ld,F%d\n", fp->name, tag, op&7, rd);
432 (*fp->f)(fm, &FR(ufp, rd));
440 print("%s %c%ld,F%d,F%d\n", fp->name, tag, op&7, rn, rd);
441 (*fp->f)(*fm, FR(ufp, rn), &FR(ufp, rd));
446 casemu(ulong pc, ulong op, Ureg *ur)
448 ulong *rp, ro, rn, *rd;
453 ro = rp[op>>16 & 0x7];
454 rn = rp[op>>0 & 0x7];
455 rd = rp + (op>>12 & 0x7);
457 validaddr((ulong)rp, 4, 1);
459 if(*rd = (*rp == ro))
467 ldrex(ulong pc, ulong op, Ureg *ur)
469 ulong *rp, *rd, *addr;
474 rd = rp + (op>>16 & 0x7);
476 validaddr((ulong)addr, 4, 0);
478 rp[op>>12 & 0x7] = *addr;
480 print("ldrex, r%ld = [r%ld]@0x%8.8p = 0x%8.8lux",
481 op>>12 & 0x7, op>>16 & 0x7, addr, rp[op>>12 & 0x7]);
485 strex(ulong pc, ulong op, Ureg *ur)
487 ulong *rp, rn, *rd, *addr;
492 rd = rp + (op>>16 & 0x7);
493 rn = rp[op>>0 & 0x7];
495 validaddr((ulong)addr, 4, 1);
499 print("strex valid, [r%ld]@0x%8.8p = r%ld = 0x%8.8lux",
500 op>>16 & 0x7, addr, op>>0 & 0x7, rn);
503 rp[op>>12 & 0x7] = 0;
506 print("strex invalid, r%ld = 1", op>>16 & 0x7);
507 rp[op>>12 & 0x7] = 1;
515 void (*f)(ulong, ulong, Ureg*);
517 { 0x01900f9f, 0x0ff00fff, ldrex },
518 { 0x01800f90, 0x0ff00ff0, strex },
519 { 0x0ed00100, 0x0ef08100, casemu },
520 { 0x00000000, 0x00000000, nil }
525 * returns the number of FP instructions emulated
535 panic("fpiarm not in a process");
537 /* because all the state is in the proc structure,
538 * it need not be saved/restored
540 if(up->fpstate != FPactive) {
541 // assert(sizeof(Internal) == sizeof(ufp->regs[0]));
542 up->fpstate = FPactive;
544 ufp->status = (0x01<<28)|(1<<12); /* software emulation, alternative C flag */
545 for(n = 0; n < 8; n++)
546 FR(ufp, n) = fpconst[0];
550 print("0x%8.8lux ", ur->pc);
551 validaddr(ur->pc, 4, 0);
552 op = *(ulong*)(ur->pc);
554 if(condok(ur->psr, op>>28)){
555 for(i = 0; specialopc[i].f; i++)
556 if((op & specialopc[i].mask) == specialopc[i].opc)
559 specialopc[i].f(ur->pc, op, ur);
560 else if((op & 0xF00) != 0x100 || o != 0xE && (o&~1) != 0xC)
563 fpemu(ur->pc, op, ur, ufp);
564 }else if((op & 0xF00) != 0x100 || o != 0xE && (o&~1) != 0xC)
568 if(fpemudebug) print("\n");