void set_op_2(data_proc_t *bin_instr, char *operand_2) { uint32_t imm_val; char *reg_m; printf("ops: %s\n", operand_2); switch (operand_2[0]) { case '#': case '=': if (operand_2[2] == 'x') { imm_val = (uint32_t) strtol(operand_2 + 1, NULL, 0); } else { imm_val = (uint32_t) atoi(operand_2 + 1); } op_imm_t immediate; if (extract_bits(imm_val, 31, 25) > 0) { uint32_t bit_0 = extract_bits(imm_val, 0, 1); int rot = 0; while (!bit_0) { imm_val >>= 1; rot++; bit_0 = extract_bits(imm_val, 0, 1); } rot = 32 - rot; if (rot % 2 != 0) { rot >>= 1; imm_val <<= 1; rot++; } else {
void recv(unsigned int *buffer,int len) { assert(len>0); // Just to be sure assert(buffer); // Just to be sure // Extract type int type = extract_bits(buffer[0],1,0); if (type==MSG_TYPE_BARRIER) { assert(len==1); // Length is always 1 if (rank==0) { // Rank 0 counts all barrier_count++; } else { // The other ranks know they can continue now barrier_continue = 1; } } else if (type==MSG_TYPE_BOUNDARY) { assert(len==3); // Length is always 3 // Extract boundary int bound = extract_bits(buffer[0],3,2); // Extract the position on the boundary int pos = buffer[1]; // Extract the value float v; memcpy(&v,&buffer[2],4); // Store element in boundary if (bound == BOUNDARY_TOP) { matrix[curmatrix][POS(pos,0)] = v; } else if (bound == BOUNDARY_RIGHT) { matrix[curmatrix][POS(xdim+1,pos)] = v; } else if (bound == BOUNDARY_BOTTOM) { matrix[curmatrix][POS(pos,ydim+1)] = v; } else if (bound == BOUNDARY_LEFT) { matrix[curmatrix][POS(0,pos)] = v; } } else if (type==MSG_TYPE_RESULT) { assert(len==4); // Length is always 4 // Extract position and value int x = buffer[1]; int y = buffer[2]; float v; memcpy(&v,&buffer[3],4); // Store in the result buffer result[x+y*XSIZE] = v; } }
bool decode_instruction(decoded_prototype_t* proto, size_t index, int* op, int* a, int* b, int* c) { unsigned char* ins; *op = -1; *a = -1; *b = -1; *c = -1; if(index >= proto->numinstructions) return false; ins = proto->code + proto->instructionsize * index; *op = extract_bits(ins, POS_OP, SIZE_OP); /* If the opcode isn't known, then the appropriate fields cannot be extracted. */ if(*op < 0 || *op >= NUM_OPCODES) return false; switch(getOpMode(*op)) { case iABC: *a = extract_bits(ins, POS_A, SIZE_A); *b = extract_bits(ins, POS_B, SIZE_B); *c = extract_bits(ins, POS_C, SIZE_C); break; case iABx: *a = extract_bits(ins, POS_A, SIZE_A); *b = extract_bits(ins, POS_Bx, SIZE_Bx); break; case iAsBx: *a = extract_bits(ins, POS_A, SIZE_A); *b = extract_bits(ins, POS_Bx, SIZE_Bx) - MAXARG_sBx; break; case iAx: *a = extract_bits(ins, POS_Ax, SIZE_Ax); break; } return true; }
void print_pred_table() { int base, offset, byte, count; int i; for (i=0 ; i < predictor.config.num_entries ; i++) { base = i / predictor.config.entries_per_byte; offset = i % predictor.config.entries_per_byte; offset = offset * 2; byte = predictor.pred_table.table[base]; count = extract_bits(byte, offset, offset + predictor.config.bits_per_entry - 1); printf(" %d\t%d\n", i, count); } }
void print_hybrid_pred_table() { int base, offset, byte, count; int i; printf("FINAL CHOOSER CONTENTS\n"); for (i=0 ; i < hybrid_predictor.config.num_entries ; i++) { base = i / hybrid_predictor.config.entries_per_byte; offset = i % hybrid_predictor.config.entries_per_byte; offset = offset * 2; byte = hybrid_predictor.chooser_table.table[base]; count = extract_bits(byte, offset, offset + hybrid_predictor.config.bits_per_entry - 1); printf(" %d\t%d\n", i, count); } printf("FINAL GSHARE CONTENTS\n"); for (i=0 ; i < hybrid_predictor.gshare.config.num_entries ; i++) { base = i / hybrid_predictor.gshare.config.entries_per_byte; offset = i % hybrid_predictor.gshare.config.entries_per_byte; offset = offset * 2; byte = hybrid_predictor.gshare.pred_table.table[base]; count = extract_bits(byte, offset, offset + hybrid_predictor.gshare.config.bits_per_entry - 1); printf(" %d\t%d\n", i, count); } printf("FINAL BIMODAL CONTENTS\n"); for (i=0 ; i < hybrid_predictor.bimodal.config.num_entries ; i++) { base = i / hybrid_predictor.bimodal.config.entries_per_byte; offset = i % hybrid_predictor.bimodal.config.entries_per_byte; offset = offset * 2; byte = hybrid_predictor.bimodal.pred_table.table[base]; count = extract_bits(byte, offset, offset + hybrid_predictor.bimodal.config.bits_per_entry - 1); printf(" %d\t%d\n", i, count); } }
/** * Approach 2: Take advantage of 32 bit division * Uses 32-bit division thrice to compute the result */ uint64_t div_64_by_8_v2(uint64_t number, uint8_t divisor) { uint32_t remainder = 0; uint64_t result = 0; /* Extract 24 bits at a time from right to left, and append it to remainder */ for (int lbit = 63; lbit > 0; lbit -= 24) { int rbit = std::max(lbit - 24 + 1, 0); uint32_t tmp32 = extract_bits(number, lbit, rbit); tmp32 |= (remainder << std::min(24, lbit+1)); uint32_t quotient = tmp32 / divisor; remainder = tmp32 % divisor; result |= ((uint64_t) quotient) << rbit; } return result; }
static sval decode_spr_ins(struct cpu_thread* thr, uval addr, uval32 ins) { struct thread_control_area *tca = get_tca(); uval id = thr->vregs->active_vsave; struct vexc_save_regs *vr = &thr->vregs->vexc_save[id]; sval ret = -1; uval opcode = extract_bits(ins, 0, 6); uval type = extract_bits(ins, 21, 10); uval spr_0_4 = extract_bits(ins, 16, 5); uval spr_5_9 = extract_bits(ins, 11, 5); uval gpr = extract_bits(ins, 6, 5); uval spr = (spr_0_4 << 5) | spr_5_9; /* mfmsr */ if (opcode == 31 && type == 83) { //hprintf("mfmsr r%ld at 0x%lx\n",gpr, addr); mtgpr(thr, gpr, thr->vregs->v_msr); tca->srr0 += sizeof(uval32); return 0; } /* mtmsrd */ if (opcode == 31 && (type == 178 || type == 146)) { uval64 val = mfgpr(thr, gpr); //hprintf("mtmsrd r%ld <- 0x%llx at 0x%lx\n", gpr, val, addr); uval64 chg_mask = ~0ULL; uval l = extract_bits(ins, 15, 1); if (type == 146) { // mtmsr , 32-bits chg_mask = 0xffffffff; } if (l == 1) { chg_mask = (MSR_EE | MSR_RI); } /* These are the only bits we can change here */ val = (val & chg_mask) | (thr->vregs->v_msr & ~chg_mask); set_v_msr(thr, val); val = thr->vregs->v_msr; val |= V_LPAR_MSR_ON; val &= ~V_LPAR_MSR_OFF; tca->srr1 = val; tca->srr0 += sizeof(uval32); return 0; } /* mfspr */ #define SET_GPR(label, src) \ case label: mtgpr(thr, gpr, src); break; if (opcode == 31 && type == 339) { ret = 0; switch (spr) { SET_GPR(SPRN_SRR0, vr->v_srr0); SET_GPR(SPRN_SRR1, vr->v_srr1); SET_GPR(SPRN_PVR, mfpvr()); SET_GPR(SPRN_PIR, mfpir()); case SPRN_DSISR: case SPRN_DAR: mtgpr(thr, gpr, 0); break; case SPRN_HID0: case SPRN_HID1: case SPRN_HID4: case SPRN_HID5: mtgpr(thr, gpr, 0xdeadbeeffeedfaceULL); break; default: ret = -1; break; } if (ret != -1) { tca->srr0 += sizeof(uval32); return ret; } } #define SET_VREG(label, field) \ case label: thr->vregs->field = mfgpr(thr, gpr); break; /* mtspr */ if (opcode == 31 && type == 467) { ret = 0; switch (spr) { SET_VREG(SPRN_SPRG0, v_sprg0); SET_VREG(SPRN_SPRG1, v_sprg1); SET_VREG(SPRN_SPRG2, v_sprg2); SET_VREG(SPRN_SPRG3, v_sprg3); case SPRN_DEC: partition_set_dec(thr, mfgpr(thr, gpr)); thr->vregs->v_dec = mfgpr(thr, gpr); break; case SPRN_SRR0: vr->v_srr0 = mfgpr(thr, gpr); break; case SPRN_SRR1: vr->v_srr1 = mfgpr(thr, gpr); break; case SPRN_DSISR: case SPRN_DAR: break; default: ret = -1; break; } if (ret != -1) { tca->srr0 += sizeof(uval32); return ret; } } /* rfid */ if (opcode == 19 && type == 18) { uval val = vr->v_srr1; set_v_msr(thr, val); val |= V_LPAR_MSR_ON; val &= ~V_LPAR_MSR_OFF; tca->srr1 = val; tca->srr0 = vr->v_srr0; hprintf("rfid: %lx -> %lx\n",addr, vr->v_srr0); return 0; } if (ret == -1) { hprintf("Decode instruction: %ld %ld %ld %ld\n", opcode, type, spr, gpr); } return ret; }
int main(int argc, char **argv) { #if 1 bitstream_t bs = {}; bs.data = malloc(1000000); memset(bs.data, 0, 1000000); { write_bits(&bs, 1, 0b1); ASSERT_EQ(bs.data[0], 0b00000001); ASSERT_EQ(bs.bit, 1); ASSERT_EQ(bs.index, 0); fprintf(stderr, "\n"); write_bits(&bs, 4, 0b1010); ASSERT_EQ(bs.data[0], 0b00010101); ASSERT_EQ(bs.bit, 5); ASSERT_EQ(bs.index, 0); fprintf(stderr, "\n"); write_bits(&bs, 3, 0b101); ASSERT_EQ(bs.data[0], 0b10110101); ASSERT_EQ(bs.bit, 0); ASSERT_EQ(bs.index, 1); fprintf(stderr, "\n"); write_bits(&bs, 5, 0b11011); ASSERT_EQ(bs.data[0], 0b10110101); ASSERT_EQ(bs.data[1], 0b00011011); ASSERT_EQ(bs.bit, 5); ASSERT_EQ(bs.index, 1); fprintf(stderr, "\n"); write_bits(&bs, 5, 0b10110); ASSERT_EQ(bs.data[0], 0b10110101); ASSERT_EQ(bs.data[1], 0b11011011); ASSERT_EQ(bs.data[2], 0b00000010); ASSERT_EQ(bs.bit, 2); ASSERT_EQ(bs.index, 2); fprintf(stderr, "\n"); write_bits(&bs, 20, 0xABCDE); ASSERT_EQ(bs.data[0], 0b10110101); ASSERT_EQ(bs.data[1], 0b11011011); ASSERT_EQ(bs.data[2], 0b01111010); ASSERT_EQ(bs.data[3], 0b11110011); ASSERT_EQ(bs.data[4], 0b00101010); ASSERT_EQ(bs.bit, 6); ASSERT_EQ(bs.index, 4); fprintf(stderr, "\n"); write_bits(&bs, 1, 0); ASSERT_EQ(bs.data[0], 0b10110101); ASSERT_EQ(bs.data[1], 0b11011011); ASSERT_EQ(bs.data[2], 0b01111010); ASSERT_EQ(bs.data[3], 0b11110011); ASSERT_EQ(bs.data[4], 0b00101010); ASSERT_EQ(bs.bit, 7); ASSERT_EQ(bs.index, 4); fprintf(stderr, "\n"); } { ASSERT_EQ(extract_bits(0b11011011, 0, 8), 0b11011011); ASSERT_EQ(extract_bits(0b00000001, 0, 1), 0b1); ASSERT_EQ(extract_bits(0b11111110, 0, 1), 0b0); ASSERT_EQ(extract_bits(0b01111111, 7, 8), 0b0); ASSERT_EQ(extract_bits(0b10000000, 7, 8), 0b1); ASSERT_EQ(extract_bits(0b11011011, 2, 6), 0b0110); fprintf(stderr, "\n"); } bs.index = 0; bs.bit = 0; { ASSERT_EQ(read_bits(&bs, 1), 0b1); ASSERT_EQ(read_bits(&bs, 4), 0b1010); ASSERT_EQ(read_bits(&bs, 3), 0b101); ASSERT_EQ(read_bits(&bs, 5), 0b11011); ASSERT_EQ(read_bits(&bs, 5), 0b10110); ASSERT_EQ(read_bits(&bs, 20), 0xABCDE); ASSERT_EQ(read_bits(&bs, 1), 0); fprintf(stderr, "\n"); } assert_lzw("ababcbababaaaaaaa"); assert_lzw("A circular buffer, or ring buffer, is a FIFO container consisting of a fixed-size buffer and head & tail indices. The head index is incremented as items are added and the tail index when items are removed."); if (failed) { return 1; } #endif #if 1 // void *ibuff = malloc(10000000); // memset(ibuff, 0, 10000000); // void *obuff = malloc(10000000); // memset(obuff, 0, 10000000); // int *ebuff = malloc(10000000); // memset(ebuff, 0, 10000000); size_t rsz = read(STDIN_FILENO, ibuff, 10000000); size_t esz = lzw_encode(ibuff, rsz, ebuff); lzw_decode(ebuff, esz, obuff); write(STDOUT_FILENO, obuff, rsz); fprintf(stderr, "input size: %lu; compressed size: %lu; rate: %f\n", rsz, esz, (float)esz/(float)rsz); #endif }
void print_crypto_params(struct xlp_sec_command *cmd, struct nlm_fmn_msg m) { unsigned long long msg0,msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8; msg0 = cmd->ctrlp->desc0; msg1 = cmd->paramp->desc0; msg2 = cmd->paramp->desc1; msg3 = cmd->paramp->desc2; msg4 = cmd->paramp->desc3; msg5 = cmd->paramp->segment[0][0]; msg6 = cmd->paramp->segment[0][1]; msg7 = m.msg[0]; msg8 = m.msg[1]; printf("msg0 %llx msg1 %llx msg2 %llx msg3 %llx msg4 %llx msg5 %llx" "msg6 %llx msg7 %llx msg8 %llx\n", msg0, msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8); printf("c0: hmac %d htype %d hmode %d ctype %d cmode %d arc4 %x\n", (unsigned int)extract_bits(msg0, 61, 1), (unsigned int)extract_bits(msg0, 52, 8), (unsigned int)extract_bits(msg0, 43, 8), (unsigned int)extract_bits(msg0, 34, 8), (unsigned int)extract_bits(msg0, 25, 8), (unsigned int)extract_bits(msg0, 0, 23)); printf("p0: tls %d hsrc %d hl3 %d enc %d ivl %d hd %llx\n", (unsigned int)extract_bits(msg1, 63, 1), (unsigned int)extract_bits(msg1,62,1), (unsigned int)extract_bits(msg1,60,1), (unsigned int)extract_bits(msg1,59,1), (unsigned int)extract_bits(msg1,41,16), extract_bits(msg1,0,40)); printf("p1: clen %u hl %u\n", (unsigned int)extract_bits(msg2, 32, 32), (unsigned int)extract_bits(msg2,0,32)); printf("p2: ivoff %d cbit %d coff %d hbit %d hclb %d hoff %d\n", (unsigned int)extract_bits(msg3, 45, 17), (unsigned int)extract_bits(msg3, 42,3), (unsigned int)extract_bits(msg3, 22,16), (unsigned int)extract_bits(msg3, 19,3), (unsigned int)extract_bits(msg3, 18,1), (unsigned int)extract_bits(msg3, 0, 16)); printf("p3: desfbid %d tlen %d arc4 %x hmacpad %d\n", (unsigned int)extract_bits(msg4, 48,16), (unsigned int)extract_bits(msg4,11,16), (unsigned int)extract_bits(msg4,6,3), (unsigned int)extract_bits(msg4,5,1)); printf("p4: sflen %d sddr %llx \n", (unsigned int)extract_bits(msg5, 48, 16),extract_bits(msg5, 0, 40)); printf("p5: dflen %d cl3 %d cclob %d cdest %llx \n", (unsigned int)extract_bits(msg6, 48, 16), (unsigned int)extract_bits(msg6, 46, 1), (unsigned int)extract_bits(msg6, 41, 1), extract_bits(msg6, 0, 40)); printf("fmn0: fbid %d dfrlen %d dfrv %d cklen %d cdescaddr %llx\n", (unsigned int)extract_bits(msg7, 48, 16), (unsigned int)extract_bits(msg7,46,2), (unsigned int)extract_bits(msg7,45,1), (unsigned int)extract_bits(msg7,40,5), (extract_bits(msg7,0,34)<< 6)); printf("fmn1: arc4 %d hklen %d pdesclen %d pktdescad %llx\n", (unsigned int)extract_bits(msg8, 63, 1), (unsigned int)extract_bits(msg8,56,5), (unsigned int)extract_bits(msg8,43,12), (extract_bits(msg8,0,34) << 6)); return; }