Beispiel #1
0
uint32_t get_regnum(uint32_t instr, sisa_opnd_t opnd)
{
    uint32_t opnd_regnum = INVALID_GPR;
    switch (opnd.type) { 
        case SISA_OPND_TYPE_GPR:
        case SISA_OPND_TYPE_GPR_RW:
        case SISA_OPND_TYPE_GPRSET:
        case SISA_OPND_TYPE_SHIFT_REG:
        case SISA_OPND_TYPE_ROTATE_REG:
            opnd_regnum = get_bits_value(instr, opnd.start_bit, opnd.end_bit);
            break;
        case SISA_OPND_TYPE_GPR_CONST:
            opnd_regnum = opnd.bit1;
            break;
        case SISA_OPND_TYPE_GPR_MERGE:
            opnd_regnum = get_bits_value(instr, opnd.start_bit, opnd.end_bit);
            if (get_bit_value(instr, opnd.bit1)) {
                opnd_regnum += 8;
            }   
            break;
        default:
            break;
    }       
    return opnd_regnum;
}
Beispiel #2
0
// sample the output pins to the current rb record
static inline void apply(const sample_t *s, const hal_delayline_t *hd)
{
    int i;
    for (i = 0; i < hd->nsamples; i++)	{
	// because of the union the values and pins must be properly
	// dereferenced against their type
	switch (hd->pintype[i])  {
	case HAL_BIT:
	    set_bit_value(hd->pins_out[i], get_bit_value(&s->value[i]));
	    break;
	case HAL_FLOAT:
	    set_float_value(hd->pins_out[i], get_float_value(&s->value[i]));
	    break;
	case HAL_S32:
	    set_s32_value(hd->pins_out[i], get_s32_value(&s->value[i]));
	    break;
	case HAL_U32:
	    set_u32_value(hd->pins_out[i], get_u32_value(&s->value[i]));
	    break;
	case HAL_S64:
	    set_s64_value(hd->pins_out[i], get_s64_value(&s->value[i]));
	    break;
	case HAL_U64:
	    set_u64_value(hd->pins_out[i], get_u64_value(&s->value[i]));
	    break;
	case HAL_TYPE_MAX:
	case HAL_TYPE_UNSPECIFIED:
	    // an error - should fail loudly TBD
	    ;
	}
    }
}
Beispiel #3
0
bool is_fixed_oprand(sisa_opcode_t op, uint32_t start_bit, uint32_t end_bit)
{
    uint32_t idx;
    uint32_t match_msk = glb_instr_tlb[op].match_msk;
    //uint32_t neg_msk = glb_instr_tlb[op].neg_msk;
    for (idx = start_bit; idx <= end_bit; idx++) {
        //if ((get_bit_value(match_msk, idx) == 1) || (get_bit_value(neg_msk, idx) == 1)) {
        if (get_bit_value(match_msk, idx) == 1) {
            return true;
        }
    }
    return false;
}
Beispiel #4
0
int main() {
    write_montgomery(message, r2modm, modulus);
    montgomery();
    read_r();

    for (i = 0; i < SIZE; i++) {
        mont_message[i] = r[i];
    }

    for (i = 0; i < SIZE; i++) {
        a[i] = rmodm[i];
    }

    for (i = 0; i < 2; i++) {
        for (j = 0; j < 8; j++) {
            write_montgomery(a, a, modulus);
            montgomery();
            read_r();

            for (i = 0; i < SIZE; i++) {
                a[i] = r[i];
            }

            if (get_bit_value(enc_exp[i], 7-j) == 1) {
                write_montgomery(a, mont_message, modulus);
                montgomery();
                read_r();

                for (i = 0; i < SIZE; i++) {
                    a[i] = r[i];
                }
            }      
        }    
    }

    write_montgomery(a, one, modulus);
    montgomery();
    read_r();

    for (i = 0; i < SIZE; i++) {
        a[i] = r[i];
    }

    terminate();
    return 0;
}
Beispiel #5
0
// write_sample_to_ring() will sample the pins and add them as a struct sample_t
// into the ring.
static void write_sample_to_ring(void *arg, long period)
{
    int j;
    ringbuffer_t *rb = (ringbuffer_t *) arg;
    hal_delayline_t *hd = rb->scratchpad;    // per-instance HAL data
    sample_t *s;

    // if pin_delay > max_delay then use max_delay, otherwise
    // use delay pin value

    // if time has increased, then take action by setting the input_ts to the
    // result of output_ts + delay. Otherwise do nothing. The situation of
    // decreasing the time will be acted upon in read_sample_to_ring()
    if ( *(hd->pin_delay) > (hal_u32_t)( hd->input_ts - hd->output_ts)) {
	if ( *(hd->pin_delay) > hd->max_delay) {
	    hd->delay = hd->max_delay;
	}
	else {
	    hd->delay = *(hd->pin_delay);
	}
	// set the new timestamp
	hd->input_ts = hd->output_ts + (__u64)(hd->delay);
    }

    if (!*(hd->enable)) {
	// skip if not sampling, just put the input
	// values into the output values
	for (j = 0; j < hd->nsamples; j++) {
	    switch (hd->pintype[j])
		{
		case HAL_BIT:
		    set_bit_value(hd->pins_out[j], get_bit_value(hd->pins_in[j]));
		    break;
		case HAL_FLOAT:
		    set_float_value(hd->pins_out[j], get_float_value(hd->pins_in[j]));
		    break;
		case HAL_S32:
		    set_s32_value(hd->pins_out[j], get_s32_value(hd->pins_in[j]));
		    break;
		case HAL_U32:
		    set_u32_value(hd->pins_out[j], get_u32_value(hd->pins_in[j]));
		    break;
		case HAL_S64:
		    set_s64_value(hd->pins_out[j], get_s64_value(hd->pins_in[j]));
		    break;
		case HAL_U64:
		    set_u64_value(hd->pins_out[j], get_u64_value(hd->pins_in[j]));
		    break;
		case HAL_TYPE_MAX:
		case HAL_TYPE_UNSPECIFIED:
		    // an error - should fail loudly TBD
		    ;
		}
	}
	goto DONE;
    }

    // use non-copying write:
    // allocate space in the ringbuffer and retrieve a pointer to it
    if (record_write_begin(rb, (void ** )&s, hd->sample_size)) {
	*(hd->write_fail) += 1;
	goto DONE;
    }

    // deposit record directly in rb memory
    s->timestamp = hd->input_ts;
    for (j = 0; j < hd->nsamples; j++) {
	switch (hd->pintype[j])
	    {
	    case HAL_BIT:
		set_bit_value(&s->value[j], get_bit_value(hd->pins_in[j]));
		break;
	    case HAL_FLOAT:
		set_float_value(&s->value[j], get_float_value(hd->pins_in[j]));
		break;
	    case HAL_S32:
		set_s32_value(&s->value[j], get_s32_value(hd->pins_in[j]));
		break;
	    case HAL_U32:
		set_u32_value(&s->value[j], get_u32_value(hd->pins_in[j]));
		break;
	    case HAL_S64:
		set_s64_value(&s->value[j], get_s64_value(hd->pins_in[j]));
		break;
	    case HAL_U64:
		set_u64_value(&s->value[j], get_u64_value(hd->pins_in[j]));
		break;
	    case HAL_TYPE_MAX:
	    case HAL_TYPE_UNSPECIFIED:
		// an error - should fail loudly TBD
		;
	    }
    }

    // commit the write given the actual write size (which is the same
    // as given in record_write_begin in our case). This makes the
    // record actually visible on the read side (advances pointer)
    if (record_write_end(rb, s, hd->sample_size))
	*(hd->write_fail) += 1;

 DONE:
    hd->input_ts++;
}
Beispiel #6
0
// 生成request请求消息,实现了片断选择算法,17为一个request消息的固定长度
int create_req_slice_msg(Peer *node)
{
	int index, begin, length = 16*1024;
	int i, count = 0;

	if(node == NULL)  
		return -1;
	// 如果被peer阻塞或对peer不感兴趣,就没有必要生成request消息
	if(node->peer_choking==1 || node->am_interested==0 )  
		return -1;

	// 如果之前向该peer发送过请求,则根据之前的请求构造新请求
	// 遵守一条原则:同一个piece的所有slice应该从同一个peer处下载
	Request_piece *p = node->Request_piece_head, *q = NULL;
	if(p != NULL) {//如果p!=NULL说明有向该peer构造过请求
		while(p->next != NULL)  { 
			p = p->next; 
		} // 定位到node->Request_piece最后一个结点处

		// 一个piece的最后一个slice的起始下标
		int last_begin = piece_length - 16*1024;//piece_length通常为256KB即262144字节
		// 如果是最后一个piece,last_begin特殊处理
		if(p->index == last_piece_index) {
			last_begin = (last_piece_count - 1) * 16 * 1024;
		}
		
		// 当前piece还有未请求的slice,则构造请求消息
		if(p->begin < last_begin) {
			index = p->index;
			begin = p->begin + 16*1024; //为什么要+16K ?
			count = 0;

			while(begin!=piece_length && count<1) {
				// 如果是最后一个piece的最后一个slice,确定length
				if(p->index == last_piece_index) {
					if( begin == (last_piece_count - 1) * 16 * 1024 )
						length = last_slice_len;
				}

				create_request_msg(index,begin,length,node);
				
				q = (Request_piece *)malloc(sizeof(Request_piece));
				if(q == NULL) { 
					printf("%s:%d error\n",__FILE__,__LINE__);
					return -1;
				}
				/* 插入本次请求到链表*/
				q->index  = index;
				q->begin  = begin;
				q->length = length;
				q->next   = NULL;
				p->next   = q;
				p         = q;

				begin += 16*1024;
				count++;
			}
			
			return 0;  // 构造完毕,就返回
		}
	}

	// 然后去btcache_head中寻找这样的piece:它没有下载完,但它不在任何peer的
	// request消息队列中,应该优先下载这样的piece,出现这样的piece的原因是:
	// 从一个peer处下载一个piece,还没下载完,那个peer就将我们choke了或下线了

	// 但是测试结果表明, 以这种方式这种方式创建rquest请求执行效率并不高
	// 如果直接丢弃未下载完成的piece,则没有必要进行这种生成请求的方式
	// int ret = create_req_slice_msg_from_btcache(node);
	// if(ret == 0) return 0;

	// 生成随机数
	if(get_rand_numbers(pieces_length/20) == -1) {
		printf("%s:%d error\n",__FILE__,__LINE__);
		return -1;
	}
	// 随机选择一个piece的下标,该下标所代表的piece应该没有向任何peer请求过
	for(i = 0; i < pieces_length/20; i++) {
		index = rand_num[i];

		// 判断对于以index为下标的piece,peer是否拥有
		if( get_bit_value(&(node->bitmap),index) != 1)  continue;
		// 判断对于以index为下标的piece,是否已经下载//这个if和上一个if换一下吧
		if( get_bit_value(bitmap,index) == 1) continue;

		// 判断对于以index为下标的piece,是否已经请求过了
		Peer          *peer_ptr = peer_head;
		Request_piece *reqt_ptr;
		int           find = 0;
		while(peer_ptr != NULL) {
			reqt_ptr = peer_ptr->Request_piece_head;
			while(reqt_ptr != NULL) {
				if(reqt_ptr->index == index) { 
					find = 1; 
					break; 
				}
				reqt_ptr = reqt_ptr->next;
			}
			if(find == 1) 
				break;

			peer_ptr = peer_ptr->next;
		}
		if(find == 1) 
			continue;

		break; // 程序若执行到此处,说明已经找到一个符合要求的index
	}
	if(i == pieces_length/20) { //??
		if(end_mode == 0) 
		   end_mode = 1;
		for(i = 0; i < pieces_length/20; i++) {
			if( get_bit_value(bitmap,i) == 0 )  { // 第i个piece已经下载
				index = i;
			    break; 
			}
		}
		
		if(i == pieces_length/20) {
			printf("Can not find an index to IP:%s\n",node->ip);
			return -1;
		}
	}

	// 构造piece请求消息
	begin = 0;
	count = 0;
	p = node->Request_piece_head;
	if(p != NULL)
		while(p->next != NULL)  p = p->next;
	while(count < 4) {
		// 如果是构造最后一个piece的请求消息
		if(index == last_piece_index) {
			if(count+1 > last_piece_count) 
				break;
			if(begin == (last_piece_count - 1) * 16 * 1024)
				length = last_slice_len;
		}

		create_request_msg(index,begin,length,node);

		q = (Request_piece *)malloc(sizeof(Request_piece));
		if(q == NULL) { printf("%s:%d error\n",__FILE__,__LINE__); return -1; }
		q->index  = index;
		q->begin  = begin;
		q->length = length;
		q->next   = NULL;
		if(node->Request_piece_head == NULL)  { node->Request_piece_head = q; p = q; }
		else  { p->next = q; p = q; }
		//printf("*** create request index:%-6d begin:%-6x to IP:%s ***\n",
		//	index,q->begin,node->ip);
		begin += 16*1024;
		count++;
	}

	if(rand_num != NULL)  { 
		free(rand_num); rand_num = NULL; 
	}
	return 0;
}