Example #1
0
// Reserva memoria para la matriz de la imagen
void buildmatrix() {
	int i;

	// Límite superior - límite inferior
	dimx = _div(par.c - par.a, par.s) +1;
	dimy = _div(par.d - par.b, par.s) +1;

					// printf("Construyendo matriz de puntos [%d, %d] ... ", dimx, dimy);
	
	// Creacion de la memoria compartida
	shm_fd = shm_open("/matrix", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
	if(shm_matrix == -1) {
		printf("\n* Error fatal al intentar crear la memoria compartida\n");
		exit(1);
	}

	if (ftruncate(shm_fd, sizeof(double) * dimx * dimy) != 0) {
		printf("\n* Error fatal al intentar truncar la memoria compartida\n");
		exit(1);
	}
		

	// Mappeo de la memoria compartida para darle el tamaño
	matrix = (double *) mmap(NULL, sizeof(double) * dimx * dimy, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
	if(matrix == MAP_FAILED) {
		printf("\n* Error fatal al mapear la memoria compartida\n");
		exit(1);
	}

					// printf("OK\n");
}
Example #2
0
/* Printf an integer */
int printi(char** dst, int i, int b, int sg, int width, int pad, int letbase)
{
    char print_buf[PRINT_BUF_LEN];
    char *s;
    int t, neg = 0, pc = 0;
    unsigned int u = i;

    if (i == 0) {
       print_buf[0] = '0';
       print_buf[1] = '\0';
       return prints (dst, print_buf, width, pad);
    }

    if (sg && b == 10 && i < 0) {
       neg = 1;
       u = -i;
    }

    s = print_buf + PRINT_BUF_LEN-1;
    *s = '\0';

    while (u) {
       if ( b == 16 )    t = u & 0xf;                  /* hex modulous */
       else              t = u - ( _div (u, b) * b );  /* Modulous */
       
       if( t >= 10 )
          t += letbase - '0' - 10;
       *--s = t + '0';
       
    /*   u /= b;  */
       if ( b == 16)  u = u >> 4;    /* divide by 16 */
       else           u = _div(u, b);
    }
Example #3
0
static CRATIONAL *_divf(CRATIONAL *a, double f, bool invert)
{
    my_mpq_set_d(_tmp.n, f);
    if (invert)
        return _div(&_tmp, a, FALSE);
    else
        return _div(a, &_tmp, FALSE);
}
Example #4
0
static CRATIONAL *_divo(CRATIONAL *a, void *o, bool invert)
{
    if (GB.Is(o, CLASS_BigInt))
    {
        mpq_set_z(_tmp.n, ((CBIGINT *)o)->n);
        if (invert)
            return _div(&_tmp, a, FALSE);
        else
            return _div(a, &_tmp, FALSE);
    }
    else
        return NULL;
}
Example #5
0
static
Value *
eval(const Ast *expr) {
	switch(expr->class) {
	case N_CALL:
		return call(expr);
	case N_ASSIGNMENT:
		return assignment(expr);
	case N_IDENTIFIER:
		return identifier(expr);
	case N_NEG:
		return _neg(expr);
	case N_NOT:
		return _not(expr);
	case N_EQ:
		return _eq(expr);
	case N_NEQ:
		return _neq(expr);
	case N_AND:
		return _and(expr);
	case N_IOR:
		return _ior(expr);
	case N_XOR:
		return _neq(expr); // alias
	case N_LT:
		return _lt(expr);
	case N_LE:
		return _le(expr);
	case N_GE:
		return _ge(expr);
	case N_GT:
		return _gt(expr);
	case N_ADD:
		return _add(expr);
	case N_SUB:
		return _sub(expr);
	case N_MUL:
		return _mul(expr);
	case N_DIV:
		return _div(expr);
	case N_POW:
		return _pow(expr);
	case N_MOD:
		return _mod(expr);
	case N_BOOLEAN:
		return _bool(expr);
	case N_INTEGER:
		return _int(expr);
	case N_FLOAT:
		return _float(expr);
	case N_STRING:
		return _string(expr);
	case N_SET:
		return _set(expr);
	case N_R:
		return _relation(expr);
	}
printf("EVALFAIL %d ", expr->class); pn(expr);
	assert(false && "should not be reached");
}
Example #6
0
static CBIGINT *_divf(CBIGINT *a, double f, bool invert)
{
	if (invert)
	{
		CBIGINT *b;
		mpz_t n;

		mpz_init_set_d(n, f);
		b = BIGINT_create(n);

		return _div(b, a, FALSE);
	}
	else
	{
		if (f > 0)
		{
			return BIGINT_make_int(a, f, mpz_tdiv_q_ui);
		}
		else if (f < 0)
		{
			a = BIGINT_make_int(a, (-f), mpz_tdiv_q_ui);
			mpz_neg(a->n, a->n);
			return a;
		}
		else
		{
			GB.Error(GB_ERR_ZERO);
			return NULL;
		}
	}
}
Example #7
0
void tast02()
{
    int a, b;
    char op;

    do
    {
        printf("Type qeuation: ");
        scanf("%d%c%d", &a, &op, &b);

        switch (op)
        {
        case '+':
            printf("res: %d\n", sum(a, b));
            break;
        case '-':
            printf("res: %d\n", sub(a, b));
            break;
        case '*':
            printf("res: %d\n", mult(a, b));
            break;
        case '/':
            printf("res: %f\n", _div(a, b));
            break;
        case 'q':
            printf("Exit\n");
            break;

        default: printf("I didn't understend operator %c\n", op);
        }
    } while (op != 'q');
}
Example #8
0
 template <class _Tp2> __device__ _Self& operator/= (const complex<_Tp2>& __z) {
   value_type __r;
   value_type __i;
   _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
   _M_re = __r;
   _M_im = __i;
   return *this;
 }
Example #9
0
 __device__ 
 _Self& operator/= (const _Self& __z) {
   value_type __r;
   value_type __i;
   _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
   _M_re = __r;
   _M_im = __i;
   return *this;
 }
Example #10
0
 __device__ 
 complex<double>& operator/= (const complex<_Tp2>& __z) {
   double __r;
   double __i;
   _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
   _M_re = __r;
   _M_im = __i;
   return *this;
 }
Example #11
0
 __device__ 
 complex<float>& operator/= (const complex<_Tp2>& __z) {
   float __r;
   float __i;
   _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
   _M_re = __r;
   _M_im = __i;
   return *this;
 }
Example #12
0
/*
 * Executes a given instruction, or errors out
 * ins - instruction to run
 */
void
execute (unsigned short int ins)
{
    /* seperate instruction into parts */
    short int a0 = (ins >> 12) % 16;
    short int a1 = (ins >> 8) % 16;
    short int a2 = (ins >> 4) % 16;
    short int a3 = (ins >> 0) % 16;

    /* Run associated instruction */
    switch(a0) {
    case 0x0:
        _add (a1, a2, a3);
        break;
    case 0x1:
        _sub (a1, a2, a3);
        break;
    case 0x2:
        _mul (a1, a2, a3);
        break;
    case 0x3:
        _div (a1, a2, a3);
        break;
    case 0x6:
        _beq (a1, a2, a3);
        break;
    case 0x7:
        _bgt (a1, a2, a3);
        break;
    case 0x8:
        _ld (a1, a2, a3);
        break;
    case 0x9:
        _st (a1, a2, a3);
        break;
    case 0xa:
        _lc (a1, a3);
        break;
    case 0xb:
        _jmp (a1, a3);
        break;
    case 0xc:
        _inc (a1, a3);
        break;
    case 0xf:
        _sys (a1, a3);
        break;
    default:
        printf ("Error: invalid opcode %#x.\n", a0);
        sys_dump (0x0);
        sys_halt (0x0);
    }
}
Example #13
0
/*
 * Compute the largest value no greater than y which is on a
 * grid row
 */
xFixed
RenderSampleFloorY (xFixed y, int n)
{
    xFixed   f = xFixedFrac(y);
    xFixed   i = xFixedFloor (y);
    
    f = _div(f - Y_FRAC_FIRST(n), STEP_Y_SMALL(n)) * STEP_Y_SMALL(n) + Y_FRAC_FIRST(n);
    if (f < Y_FRAC_FIRST(n))
    {
	f = Y_FRAC_LAST(n);
	i -= xFixed1;
    }
    return (i | f);
}
Example #14
0
ISTBOOL GetParams(struct sphere_model *model, ISTFLOAT rad)
{
	ISTSHORT i, j;
	ISTFLOAT min[3] = {0};
	ISTFLOAT max[3] = {0};
	ISTFLOAT mean[3] = {0};
	ISTFLOAT var[3] = {0};
	ISTFLOAT tmpf;
	ISTFLOAT two = _float(2);

	for (j = 0; j < 3; j++) {
		min[j] = model->offsets[0][j];
		max[j] = model->offsets[0][j];
		for (i = 1; i < IST_SPHERE_OFFSET_NUM; i++) {
			if (_lt(model->offsets[i][j], min[j])) {
				min[j] = model->offsets[i][j];
			}
			if (_gt(model->offsets[i][j], max[j])) {
				max[j] = model->offsets[i][j];
			}
		}
		mean[j] = _div(_add(max[j], min[j]), two);
		var[j] = _sub(max[j], min[j]);
	}
	tmpf = _mul(rad, model->ratio);
	if (_ge(var[0], tmpf) || _ge(var[1], tmpf) || _ge(var[2], tmpf)) {
		goto EXIT;
	}
	for (j = 0; j < 3; ++j) {
		model->mean[j] = mean[j];
		model->var[j] = var[j];
	}
	model->rad = rad;
	return ISTTRUE;

EXIT:
	return ISTFALSE;
}
Example #15
0
int main () {
	int i1 = 20, i2 = 4, i3;
	float f1 = 1.5,f2 = 0.3,f3;

	i3 = _add(i1, i2);
	printf("add(%d, %d)=%d\n", i1, i2, i3);
	i3 = _sub(i1, i2);
	printf("sub(%d, %d)=%d\n", i1, i2, i3);
	i3 = _mul(i1, i2);
	printf("mul(%d, %d)=%d\n", i1, i2, i3);
	i3 = _div(i1, i2);
	printf("div(%d, %d)=%d\n", i1, i2, i3);

	f3 = _fadd(f1, f2);
	printf("fadd(%f, %f)=%f\n", f1, f2, f3);
	f3 = _fsub(f1, f2);
	printf("fsub(%f, %f)=%f\n", f1, f2, f3);
	f3 = _fmul(f1, f2);
	printf("fmul(%f, %f)=%f\n", f1, f2, f3);
	f3 = _fdiv(f1, f2);
	printf("fdiv(%f, %f)=%f\n", f1, f2, f3);
    return 0;
}
Example #16
0
/*
 * TKGetNextToken returns the next token from the token stream as a
 * character string.  Space for the returned token should be dynamically
 * allocated.  The caller is responsible for freeing the space once it is
 * no longer needed.
 *
 * If the function succeeds, it returns a C string (delimited by '\0')
 * containing the token.  Else it returns 0.
 *
 * You need to fill in this function as part of your implementation.
 */
TokenT *TKGetNextToken(TokenizerT *tk) {
    clearBuffer(tk);
    char curr = tk->inputIter[0];

    // skip all whitespace before next token
    while(isspace(curr)) {
        nextChar(tk);
        clearBuffer(tk);
        curr = tk->inputIter[0];
    }

    if(curr == '\0') {
        return NULL;
    } else if(isalpha(curr) || curr == '_') {
        return _word(tk);
    } else if(curr == '0') {
        return _zero(tk);
    } else if(isdigit(curr)) {
        return _decimal(tk);
    } else if(curr == '!') { // neq
        return _neq(tk);
    } else if(curr == '"') { // double_quote
        return _double_quote(tk);
    } else if(curr == '#') {
        return _pound(tk);
    } else if(curr == '$') { // INVALID
        return _invalid(tk);
    } else if(curr == '%') { // mod, mod_eq
        return _mod(tk);
    } else if(curr == '&') { // bit_and, log_and, address (?)
        return _bit_and(tk);
    } else if(curr == '\'') { // single_quote
        return _single_quote(tk);
    } else if(curr == '(') { // open_paren
        return _open_paren(tk);
    } else if(curr == ')') { // close_paren
        return _close_paren(tk);
    } else if(curr == '*') { // mult, mult_eq, pointer (?)
        return _mult(tk);
    } else if(curr == '+') { // plus, plus_eq, inc
        return _plus(tk);
    } else if(curr == ',') { // comma
        return _comma(tk);
    } else if(curr == '-') { // minus, minus_eq, dec, struct_pointer
        return _minus(tk);
    } else if(curr == '.') { // dot
        return _dot(tk);
    } else if(curr == '/') { // div, div_eq
        return _div(tk);
    } else if(curr == ':') { // ternary_colon
        return _ternary_colon(tk);
    } else if(curr == ';') { // semicolon
        return _semicolon(tk);
    } else if(curr == '<') { // lt, lshift, lt_eq
        return _lt(tk);
    } else if(curr == '=') { // eq, assign
        return _eq(tk);
    } else if(curr == '>') { // gt, rshift, gt_eq
        return _gt(tk);
    } else if(curr == '?') { // ternary_qmark
        return _ternary_qmark(tk);
    } else if(curr == '@') { // INVALID
        return _invalid(tk);
    } else if(curr == '[') { // open_bracket
        return _open_bracket(tk);
    } else if(curr == '\\') { // backslash (?)
        return _invalid(tk);
    } else if(curr == ']') { // close_bracket
        return _close_bracket(tk);
    } else if(curr == '^') { // bit_xor
        return _bit_xor(tk);
    } else if(curr == '`') { // INVALID
        return _invalid(tk);
    } else if(curr == '{') { // open_brace
        return _open_brace(tk);
    } else if(curr == '|') { // bit_or, log_or
        return _bit_or(tk);
    } else if(curr == '}') { // close_brace
        return _close_brace(tk);
    } else if(curr == '~') { // bit_not
        return _bit_not(tk);
    } else {
        return _invalid(tk);
    }
}
Example #17
0
void VMDriver::execute(){
	if( this->m_state == STATE_IDLE ){
		printf( "Error: state is idle. \n");
		return;
	}

	assert( this->currentAssembly() );
	while( this->isActive() ){
		if( this->isBreak() ){
			break;
		}
		unsigned char content = this->getByte( m_funcAddr , m_pc );
		m_pc++;
		switch( content ){
			case EMnemonic::MovPtr :
				_mov_ptr();
				break;
			case EMnemonic::Mov :
				_mov();
				break;
			case EMnemonic::Add :
				_add();
				break;
			case EMnemonic::Sub :
				_sub();
				break;
			case EMnemonic::Mul :
				_mul();
				break;
			case EMnemonic::Div :
				_div();
				break;
			case EMnemonic::Rem :
				_rem();
				break;
			case EMnemonic::Inc :
				_inc();
				break;
			case EMnemonic::Dec :
				_dec();
				break;
			case EMnemonic::Push :
				_push();
				break;
			case EMnemonic::PushPtr :
				_push_ptr();
				break;
			case EMnemonic::Pop :
				_pop();
				break;
			case EMnemonic::Call :
				_call();
				break;
			case EMnemonic::ST :
				_st();
				break;
			case EMnemonic::LD :
				_ld();
				break;
			case EMnemonic::EndFunc :
				_endFunc();
				break;

			case EMnemonic::CmpGeq : 
			case EMnemonic::CmpG :
			case EMnemonic::CmpLeq : 
			case EMnemonic::CmpL :
			case EMnemonic::CmpEq : 
			case EMnemonic::CmpNEq :
				_cmp( content );
				break;
			case EMnemonic::Not :
				_not();
				break;
			case EMnemonic::Minus :
				_minus();
				break;
			case EMnemonic::LogOr :
			case EMnemonic::LogAnd :
				_log( content );
				break;
			case EMnemonic::Jmp :
				_jmp();
				break;
			case EMnemonic::JumpZero :
				_jumpzero();
				break;
			case EMnemonic::JumpNotZero :
				_jumpnotzero();
				break;
			case EMnemonic::RET :
				_ret();
				break;
		}
	}
}
Example #18
0
File: op.hpp Project: nthend/flow
inline int divmod(T &num, const T &den)
{
	T ret = _div(num,den);
	num = _mod(num,den);
    return ret;
}
Example #19
0
// Complex to matrix (Número complejo a posición de la matriz)
int * ctom(double x, double y) {
	int * res = (int *) malloc(sizeof(int) * 2);
	res[0] = _div(x - par.a, par.s);
	res[1] = _div(y - par.b, par.s);
	return res;
}
Example #20
0
ISTBOOL GetSphereFrom4Points(struct sphere_model *model)
{
	ISTFLOAT a[4][4] = {{0}};
	ISTFLOAT ho[3] = {0};
	ISTFLOAT norm = 0;
	ISTFLOAT rad = 0;
	ISTFLOAT tmpf = 0;
	ISTFLOAT two = _float(2);
	ISTFLOAT m11, m12, m13, m14, m15;
	ISTINT i, j;

	// Get sphere from 4 points
	for (i = 0; i < 4; i++) { /* find minor 11 */
		a[i][0] = model->p4s[i][0];
		a[i][1] = model->p4s[i][1];
		a[i][2] = model->p4s[i][2];
		a[i][3] = _one;
	}
	m11 = GetDet(a, 4);
	for (i = 0; i < 4; i++) { /* find minor 12 */
		a[i][0] = _add(_add(_mul(model->p4s[i][0], model->p4s[i][0]),
			_mul(model->p4s[i][1], model->p4s[i][1])),
			_mul(model->p4s[i][2], model->p4s[i][2]));
		a[i][1] = model->p4s[i][1];
		a[i][2] = model->p4s[i][2];
		a[i][3] = _one;
	}
	m12 = GetDet(a, 4);
	for (i = 0; i < 4; i++) { /* find minor 13 */
		a[i][0] = _add(_add(_mul(model->p4s[i][0], model->p4s[i][0]),
			_mul(model->p4s[i][1], model->p4s[i][1])),
			_mul(model->p4s[i][2], model->p4s[i][2]));
		a[i][1] = model->p4s[i][0];
		a[i][2] = model->p4s[i][2];
		a[i][3] = _one;
	}
	m13 = GetDet(a, 4);
	for (i = 0; i < 4; i++) { /* find minor 14 */
		a[i][0] = _add(_add(_mul(model->p4s[i][0], model->p4s[i][0]),
			_mul(model->p4s[i][1], model->p4s[i][1])),
			_mul(model->p4s[i][2], model->p4s[i][2]));
		a[i][1] = model->p4s[i][0];
		a[i][2] = model->p4s[i][1];
		a[i][3] = _one;
	}
	m14 = GetDet(a, 4);
	for (i = 0; i < 4; i++) { /* find minor 15 */
		a[i][0] = _add(_add(_mul(model->p4s[i][0], model->p4s[i][0]),
			_mul(model->p4s[i][1], model->p4s[i][1])),
			_mul(model->p4s[i][2], model->p4s[i][2]));
		a[i][1] = model->p4s[i][0];
		a[i][2] = model->p4s[i][1];
		a[i][3] = model->p4s[i][2];
	}
	m15 = GetDet(a, 4);
	if (_eq(m11, 0)) {
		rad = 0;
	} else { /* center of sphere */
		ho[0] = _div(_div(m12, m11), two);
		ho[1] = _neg(_div(_div(m13, m11), two));
		ho[2] = _div(_div(m14, m11), two);
		norm = _sub(_add(_add(_mul(ho[0], ho[0]),
			_mul(ho[1], ho[1])),
			_mul(ho[2], ho[2])),
			_div(m15, m11));
		if (_ge(norm, 0)) {
			rad = _sqrt(norm);
		}
	}
	if (_le(rad, 0)) {
		goto EXIT;
	}
	// Check distance
	for (i = 0; i < 4; i++) {
		for (j = (i + 1); j < 4; j++) {
			tmpf = GetDistance(model->p4s[i], model->p4s[j]);
			if (_lt(tmpf, rad) || _lt(tmpf, model->rad_min)) {
				goto EXIT;
			}
		}
	}
	// Update offset 
	for (i = 1; i < IST_SPHERE_OFFSET_NUM; i++) {
		for (j = 0; j < 3; ++j) {
			model->offsets[IST_SPHERE_OFFSET_NUM - i][j] = model->offsets[IST_SPHERE_OFFSET_NUM - i - 1][j];
		}
	}
	for (j = 0; j < 3; ++j) {
		model->offsets[0][j] = ho[j];
	}
	for (i = (IST_SPHERE_DATAMAX >> 1); i < IST_SPHERE_DATAMAX; i++) {
		for (j = 0; j < 3; ++j) {
			model->data[i][j] = _max;
		}
	}
	// Check offset buffer full
	if (IsInitedVector(model->offsets[IST_SPHERE_OFFSET_NUM - 1])) {
		goto EXIT;
	}
	// Calculate mean bias and radius
	if (!GetParams(model, rad)) {
		goto EXIT;
	}
	return ISTTRUE;

EXIT:
	return ISTFALSE;
}