void ARMCore::thumbAddSub(uint2 opcode, uint3 ird, uint3 irn, uint3 irm) { auto& rd = r[ird], rn = r[irn], rm = opcode & 2? (uint32)irm : r[irm]; r[15] += 2; if(opcode & 1) sumf(true, rd = rn-rm, rn, ~rm); // subs else sumf(true, rd = rn+rm, rn, rm); // adds }
void ARMCore::thumbDataImm(uint2 opcode, uint3 ird, uint8 rm) { auto &rd = r[ird], rn = rd; r[15] += 2; if(opcode == 0) bitf(true, rd = rm, {rm, Cf}); // movs if(opcode == 1) sumf(true, rn-rm, rn, ~rm); // cmps if(opcode == 2) sumf(true, rd = rn+rm, rn, rm); // adds if(opcode == 3) sumf(true, rd = rn-rm, rn, ~rm); // subs }
//this will return the index that holds the largest sum int largestSumAtIndex (char string []) { int stringLength = strlen (string); int maxSum = 0; int sum = 0; int i = 0; for (i = 0; i < stringLength - 12; i++) { sum = sumf ((int)string [i], (int)string[i+1], (int)string[i+2], (int)string [i+3], (int)string [i+4], (int)string [i+5], (int)string [i+6], (int)string [i+7], (int)string [i+8], (int)string [i+9], (int)string [i+10], (int)string [i+11], string [i+12]); maxSum = sum > maxSum ? sum : maxSum; } return i; }
void ARMCore::thumbDataLo(uint4 opcode, uint3 ird, uint3 irm) { auto &rd = r[ird], rm = r[irm]; r[15] += 2; if(opcode == 2) { SOut r = lsl(rd, rm); bitf(true, rd = r, r); } // lsls else if(opcode == 3) { SOut r = lsr(rd, rm); bitf(true, rd = r, r); } // lsrs else if(opcode == 4) { SOut r = asr(rd, rm); bitf(true, rd = r, r); } // asrs else if(opcode == 7) { SOut r = ror(rd, rm); bitf(true, rd = r, r); } // rors else if(opcode == 9) sumf(true, rd = -rm, 0, ~rm); // negs else if(opcode == 13) bitf(true, rd = rm * rd, {rm*rd, Cf}); // muls else alu(2*opcode+1, rd, rd, {rm,Cf}); // others are same as ARM }
void ARMCore::thumbDataHi(uint2 opcode, uint4 ird, uint4 irm) { auto &rd = r[ird], rn = rd, rm = r[irm]; r[15] += 2; if(opcode == 0) rd = rn + rm; // add if(opcode == 1) sumf(true, rn-rm, rn, ~rm); // cmps if(opcode == 2) rd = rm; // mov if(opcode == 3) { // bx, blx if(ird & 8) r[14] = r[15] - 4 + 1; return branch(rm & 1, rm); } if(ird == 15) branch(1, r[15]); }
int main(int argc, char *argv[]) { char *configfile = NULL; FILE *fin, *bin; char *linebuf = NULL; size_t buflen = 0; int iterations = 3; int mode = 3; int c; float d; float *loglik; float p; int i, j, k; opterr = 0; while ((c = getopt(argc, argv, "c:n:hp:")) != -1) { switch (c) { case 'c': configfile = optarg; break; case 'h': usage(); exit(EXIT_SUCCESS); case 'n': iterations = atoi(optarg); break; case 'p': mode = atoi(optarg); if (mode != 1 && mode != 2 && mode != 3) { fprintf(stderr, "illegal mode: %d\n", mode); exit(EXIT_FAILURE); } break; case '?': fprintf(stderr, "illegal options\n"); exit(EXIT_FAILURE); default: abort(); } } if (configfile == NULL) { fin = stdin; } else { fin = fopen(configfile, "r"); if (fin == NULL) { handle_error("fopen"); } } i = 0; while ((c = getline(&linebuf, &buflen, fin)) != -1) { if (c <= 1 || linebuf[0] == '#') continue; if (i == 0) { if (sscanf(linebuf, "%d", &nstates) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } prior = (float *) malloc(sizeof(float) * nstates); if (prior == NULL) handle_error("malloc"); trans = (float *) malloc(sizeof(float) * nstates * nstates); if (trans == NULL) handle_error("malloc"); xi = (float *) malloc(sizeof(float) * nstates * nstates); if (xi == NULL) handle_error("malloc"); pi = (float *) malloc(sizeof(float) * nstates); if (pi == NULL) handle_error("malloc"); } else if (i == 1) { if (sscanf(linebuf, "%d", &nobvs) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } obvs = (float *) malloc(sizeof(float) * nstates * nobvs); if (obvs == NULL) handle_error("malloc"); gmm = (float *) malloc(sizeof(float) * nstates * nobvs); if (gmm == NULL) handle_error("malloc"); } else if (i == 2) { /* read initial state probabilities */ bin = fmemopen(linebuf, buflen, "r"); if (bin == NULL) handle_error("fmemopen"); for (j = 0; j < nstates; j++) { if (fscanf(bin, "%f", &d) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } prior[j] = logf(d); } fclose(bin); } else if (i <= 2 + nstates) { /* read state transition probabilities */ bin = fmemopen(linebuf, buflen, "r"); if (bin == NULL) handle_error("fmemopen"); for (j = 0; j < nstates; j++) { if (fscanf(bin, "%f", &d) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } trans[IDX((i - 3),j,nstates)] = logf(d); } fclose(bin); } else if (i <= 2 + nstates * 2) { /* read output probabilities */ bin = fmemopen(linebuf, buflen, "r"); if (bin == NULL) handle_error("fmemopen"); for (j = 0; j < nobvs; j++) { if (fscanf(bin, "%f", &d) != 1) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } obvs[IDX((i - 3 - nstates),j,nobvs)] = logf(d); } fclose(bin); } else if (i == 3 + nstates * 2) { if (sscanf(linebuf, "%d %d", &nseq, &length) != 2) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } data = (int *) malloc (sizeof(int) * nseq * length); if (data == NULL) handle_error("malloc"); } else if (i <= 3 + nstates * 2 + nseq) { /* read data */ bin = fmemopen(linebuf, buflen, "r"); if (bin == NULL) handle_error("fmemopen"); for (j = 0; j < length; j++) { if (fscanf(bin, "%d", &k) != 1 || k < 0 || k >= nobvs) { fprintf(stderr, "config file format error: %d\n", i); freeall(); exit(EXIT_FAILURE); } data[(i - 4 - nstates * 2) * length + j] = k; } fclose(bin); } i++; } fclose(fin); if (linebuf) free(linebuf); if (i < 4 + nstates * 2 + nseq) { fprintf(stderr, "configuration incomplete.\n"); freeall(); exit(EXIT_FAILURE); } if (mode == 3) { loglik = (float *) malloc(sizeof(float) * nseq); if (loglik == NULL) handle_error("malloc"); for (i = 0; i < iterations; i++) { init_count(); for (j = 0; j < nseq; j++) { loglik[j] = forward_backward(data + length * j, length, 1); } p = sumf(loglik, nseq); update_prob(); printf("iteration %d log-likelihood: %.4f\n", i + 1, p); printf("updated parameters:\n"); printf("# initial state probability\n"); for (j = 0; j < nstates; j++) { printf(" %.4f", exp(prior[j])); } printf("\n"); printf("# state transition probability\n"); for (j = 0; j < nstates; j++) { for (k = 0; k < nstates; k++) { printf(" %.4f", exp(trans[IDX(j,k,nstates)])); } printf("\n"); } printf("# state output probility\n"); for (j = 0; j < nstates; j++) { for (k = 0; k < nobvs; k++) { printf(" %.4f", exp(obvs[IDX(j,k,nobvs)])); } printf("\n"); } printf("\n"); } free(loglik); } else if (mode == 2) { for (i = 0; i < nseq; i++) { viterbi(data + length * i, length); } } else if (mode == 1) { loglik = (float *) malloc(sizeof(float) * nseq); if (loglik == NULL) handle_error("malloc"); for (i = 0; i < nseq; i++) { loglik[i] = forward_backward(data + length * i, length, 0); } p = sumf(loglik, nseq); for (i = 0; i < nseq; i++) printf("%.4f\n", loglik[i]); printf("total: %.4f\n", p); free(loglik); } freeall(); return 0; }