Exemplo n.º 1
0
/*
 * Inverse A and output to B
 */
static void fieldInv(const uint32_t *A, const uint32_t *modulus, const uint32_t *reducer, uint32_t *B){
	uint32_t u[8],v[8],x1[8],x2[8];
	uint32_t tempm[8];
	uint32_t tempm2[8];
	setZero(tempm, 8);
	setZero(tempm2, 8);
	setZero(u, 8);
	setZero(v, 8);

	uint8_t t;
	copy(A,u,arrayLength); 
	copy(modulus,v,arrayLength); 
	setZero(x1, 8);
	setZero(x2, 8);
	x1[0]=1; 
	/* While u !=1 and v !=1 */ 
	while ((isOne(u) || isOne(v))==0) {
		while(!(u[0]&1)) { 					/* While u is even */
			rshift(u); 						/* divide by 2 */
			if (!(x1[0]&1))					/*ifx1iseven*/
				rshift(x1);					/* Divide by 2 */
			else {
				fieldAddAndDivide(x1,modulus,reducer,tempm); /* tempm=x1+p */
				copy(tempm,x1,arrayLength); 		/* x1=tempm */
				//rshift(x1);					/* Divide by 2 */
			}
		} 
		while(!(v[0]&1)) {					/* While v is even */
			rshift(v); 						/* divide by 2 */ 
			if (!(x2[0]&1))					/*ifx1iseven*/
				rshift(x2); 				/* Divide by 2 */
			else
			{
				fieldAddAndDivide(x2,modulus,reducer,tempm);	/* tempm=x1+p */
				copy(tempm,x2,arrayLength); 			/* x1=tempm */ 
				//rshift(x2);					/* Divide by 2 */
			}
			
		} 
		t=sub(u,v,tempm,arrayLength); 				/* tempm=u-v */
		if (t==0) {							/* If u > 0 */
			copy(tempm,u,arrayLength); 					/* u=u-v */
			fieldSub(x1,x2,modulus,tempm); 			/* tempm=x1-x2 */
			copy(tempm,x1,arrayLength);					/* x1=x1-x2 */
		} else {
			sub(v,u,tempm,arrayLength); 			/* tempm=v-u */
			copy(tempm,v,arrayLength); 					/* v=v-u */
			fieldSub(x2,x1,modulus,tempm); 			/* tempm=x2-x1 */
			copy(tempm,x2,arrayLength);					/* x2=x2-x1 */
		}
	} 
	if (isOne(u)) {
		copy(x1,B,arrayLength); 
	} else {
		copy(x2,B,arrayLength);
	}
}
Exemplo n.º 2
0
int main(){
int c;
int i=0;
int odd=1;
//take each input separated by spaces
//odd: for leaving spaces
while((c=getchar())!='\n'){
if(odd){
a[i]=c-48;
i++;
odd=0;}
else{
odd=1;
}
}
int check = 0;
for(i=0;i<100;i++){
//if 1 has not occured yet
if(check==0){
check = isOne(i);
}
else{
printf("%d\n",i-1);
return 0;
}
}
printf("-1\n");
return 0;
}
Exemplo n.º 3
0
Arquivo: P0.c Projeto: ngravic/AC2014
void printBin (long int n) {
	int i;
	for(i = LONG_BITS - 1; i >= 0; i--) {
		printf("%d", isOne(n, i));
	}
	puts("");
}	
Exemplo n.º 4
0
CEvaluationNode* CDerive::multiply(CEvaluationNode* n1, CEvaluationNode* n2, bool simplify)
{
  if (simplify)
    {
      if (isZero(n1) || isZero(n2))
        {
          deleteBranch(n1);
          deleteBranch(n2);
          return new CEvaluationNodeNumber(CEvaluationNodeNumber::INTEGER, "0");
        }

      if (isOne(n1))
        {
          if (isOne(n2))
            {
              deleteBranch(n1);
              deleteBranch(n2);
              return new CEvaluationNodeNumber(CEvaluationNodeNumber::INTEGER, "1");
            }
          else
            {
              deleteBranch(n1);
              return n2;
            }
        }

      if (isOne(n2))
        {
          deleteBranch(n2);
          return n1;
        }
    }

  CEvaluationNode * tmpNode = NULL;
  tmpNode = new CEvaluationNodeOperator(CEvaluationNodeOperator::MULTIPLY, "*");
  tmpNode->addChild(n1);
  tmpNode->addChild(n2);
  //tmpNode->compile(NULL);

  return tmpNode;
}
Exemplo n.º 5
0
CEvaluationNode* CDerive::power(CEvaluationNode* n1, CEvaluationNode* n2, bool simplify)
{
  if (simplify)
    {
      if (isOne(n2))
        {
          deleteBranch(n2);
          return n1;
        }

      if (isOne(n1))
        {
          deleteBranch(n1);
          deleteBranch(n2);
          return new CEvaluationNodeNumber(CEvaluationNodeNumber::INTEGER, "1");
        }

      if (isZero(n2) && !isZero(n1))
        {
          deleteBranch(n1);
          deleteBranch(n2);
          return new CEvaluationNodeNumber(CEvaluationNodeNumber::INTEGER, "1");
        }

      if (isZero(n1) && !isZero(n2))
        {
          deleteBranch(n1);
          deleteBranch(n2);
          return new CEvaluationNodeNumber(CEvaluationNodeNumber::INTEGER, "0");
        }
    }

  CEvaluationNode * tmpNode = NULL;
  tmpNode = new CEvaluationNodeOperator(CEvaluationNodeOperator::POWER, "^");
  tmpNode->addChild(n1);
  tmpNode->addChild(n2);
  //tmpNode->compile(NULL);

  return tmpNode;
}
Exemplo n.º 6
0
Arquivo: P1.c Projeto: ngravic/AC2014
nro mult(nro n1, nro n2) {
	int i;
	nro toRet;
	if (isZero(n2)) {		
		for(i = 0; i < 16; i++)
			toRet.n[i] = 0;
		return toRet;
	}
	if (isOne(n2))
		return n1;		
	if (isEven(n2))
		return mult(bwLeft(n1), bwRight(n2));
	return add(mult(bwLeft(n1), bwRight(n2)), n1);
}
Exemplo n.º 7
0
	void swap(ParamItem& rhs)
	{
		bool b1 = isOne();
		bool b2 = rhs.isOne();

		std::swap(type, rhs.type);
		std::swap(lookedup, rhs.lookedup);
		std::swap(oneString, rhs.oneString);
		std::swap(ptr, rhs.ptr);

		if (b2)
			ints = &oneInt;

		if (b1)
			rhs.ints = &rhs.oneInt;
	}
Exemplo n.º 8
0
Arquivo: char.c Projeto: liangx8/cpp
void show_section(int i){
    wprintf(L"\033[31m%02x\033[0m \033[32m00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\033[0m\n",i);
    for(int x=0;x<16;x++){
      wprintf(L"\033[33m%02x\033[0m ",x*16);
      for(int y=0;y<16;y++){
		wchar_t c;
		wprintf(L"\033[0;34;47m");
		c=i*256+x*16+y;
		if (isPrintable(c)){
		  if (isOne(c))
			wprintf(L"%lc \033[0m ",c);
		  else
			wprintf(L"%lc\033[0m ",c);
		}else
		  wprintf(L"NA\033[0m ");
      }
      wprintf(L"\n");
    }
    wprintf(L"\n");
}
Exemplo n.º 9
0
CEvaluationNode* CDerive::divide(CEvaluationNode* n1, CEvaluationNode* n2, bool simplify)
{
  if (simplify)
    {
      if (isZero(n1))
        {
          deleteBranch(n1);
          deleteBranch(n2);
          return new CEvaluationNodeNumber(CEvaluationNodeNumber::INTEGER, "0");
        }

      /*if (isOne(n1))
      {
        if (isOne(n2))
        {
          deleteBranch(n1);
          deleteBranch(n2);
          return new CEvaluationNodeNumber(CEvaluationNodeNumber::INTEGER, "1");
        }
        else
        {
          deleteBranch(n1);
          return n2;
        }
      }*/

      if (isOne(n2))
        {
          deleteBranch(n2);
          return n1;
        }
    }

  CEvaluationNode * tmpNode = NULL;
  tmpNode = new CEvaluationNodeOperator(CEvaluationNodeOperator::DIVIDE, "/");
  tmpNode->addChild(n1);
  tmpNode->addChild(n2);
  //tmpNode->compile(NULL);

  return tmpNode;
}
Exemplo n.º 10
0
Func1& newRatioFunction(Func1& f1, Func1& f2)
{
    if (isOne(f2)) {
        return f1;
    }
    if (isZero(f1)) {
        return *(new Const1(0.0));
    }
    if (f1.isIdentical(f2)) {
        delete &f1;
        delete &f2;
        return *(new Const1(1.0));
    }
    if (f1.ID() == PowFuncType && f2.ID() == PowFuncType) {
        return *(new Pow1(f1.c() - f2.c()));
    }
    if (f1.ID() == ExpFuncType && f2.ID() == ExpFuncType) {
        return *(new Exp1(f1.c() - f2.c()));
    }
    return *(new Ratio1(f1, f2));
}
Exemplo n.º 11
0
TEST_F(ResolveConditionTest, BranchConditionsAreResolved) {
  {
    // No runtime information is set in the specialization context. Thus the resolution of the condition must fail.
    const auto resolved_condition = ResolveCondition(_branch_instruction->getCondition(), _specialization_context);
    ASSERT_FALSE(resolved_condition);
  }
  {
    // Create runtime information (i.e., an instance of the SomeStruct parameter).
    const auto runtime_parameter = SomeStruct{1, 2};
    // Initialize the first function argument to this value in the specialization context. This make the value
    // accessible to the specialization engine.
    _specialization_context.runtime_value_map.clear();
    _specialization_context.runtime_value_map[_branch_instruction_fn->arg_begin()] =
        std::make_shared<JitConstantRuntimePointer>(&runtime_parameter);

    const auto resolved_condition = ResolveCondition(_branch_instruction->getCondition(), _specialization_context);

    // The condition should now be replaced by a "false" constant (which is represented as a 1-bit integer in LLVM),
    // since the resolved expression "s.a + s.b > 5" = "1 + 2 > 5" is false.
    ASSERT_TRUE(resolved_condition);
    ASSERT_EQ(resolved_condition->getType(), llvm::Type::getInt1Ty(*_repository->llvm_context()));
    const auto resolved_int_condition = llvm::dyn_cast<llvm::ConstantInt>(resolved_condition);
    ASSERT_TRUE(resolved_int_condition->isZero());
  }
  {
    // We repeat the above procedure with a value that will cause the condition expression to evaluate to "true".
    const auto runtime_parameter = SomeStruct{4, 5};
    _specialization_context.runtime_value_map.clear();
    _specialization_context.runtime_value_map[_branch_instruction_fn->arg_begin()] =
        std::make_shared<JitConstantRuntimePointer>(&runtime_parameter);

    const auto resolved_condition = ResolveCondition(_branch_instruction->getCondition(), _specialization_context);
    ASSERT_TRUE(resolved_condition);
    ASSERT_EQ(resolved_condition->getType(), llvm::Type::getInt1Ty(*_repository->llvm_context()));
    const auto resolved_int_condition = llvm::dyn_cast<llvm::ConstantInt>(resolved_condition);
    ASSERT_TRUE(resolved_int_condition->isOne());
  }
}
Exemplo n.º 12
0
Arquivo: deriv.c Projeto: edzer/cxxr
static SEXP simplify(SEXP fun, SEXP arg1, SEXP arg2)
{
    SEXP ans;
    if (fun == PlusSymbol) {
	if (isZero(arg1))
	    ans = arg2;
	else if (isZero(arg2))
	    ans = arg1;
	else if (isUminus(arg1))
	    ans = simplify(MinusSymbol, arg2, CADR(arg1));
	else if (isUminus(arg2))
	    ans = simplify(MinusSymbol, arg1, CADR(arg2));
	else
	    ans = lang3(PlusSymbol, arg1, arg2);
    }
    else if (fun == MinusSymbol) {
	if (arg2 == R_MissingArg) {
	    if (isZero(arg1))
		ans = Constant(0.);
	    else if (isUminus(arg1))
		ans = CADR(arg1);
	    else
		ans = lang2(MinusSymbol, arg1);
	}
	else {
	    if (isZero(arg2))
		ans = arg1;
	    else if (isZero(arg1))
		ans = simplify(MinusSymbol, arg2, R_MissingArg);
	    else if (isUminus(arg1)) {
		ans = simplify(MinusSymbol,
			       PP(simplify(PlusSymbol, CADR(arg1), arg2)),
			       R_MissingArg);
		UNPROTECT(1);
	    }
	    else if (isUminus(arg2))
		ans = simplify(PlusSymbol, arg1, CADR(arg2));
	    else
		ans = lang3(MinusSymbol, arg1, arg2);
	}
    }
    else if (fun == TimesSymbol) {
	if (isZero(arg1) || isZero(arg2))
	    ans = Constant(0.);
	else if (isOne(arg1))
	    ans = arg2;
	else if (isOne(arg2))
	    ans = arg1;
	else if (isUminus(arg1)) {
	    ans = simplify(MinusSymbol,
			   PP(simplify(TimesSymbol, CADR(arg1), arg2)),
			   R_MissingArg);
	    UNPROTECT(1);
	}
	else if (isUminus(arg2)) {
	    ans = simplify(MinusSymbol,
			   PP(simplify(TimesSymbol, arg1, CADR(arg2))),
			   R_MissingArg);
	    UNPROTECT(1);
	}
	else
	    ans = lang3(TimesSymbol, arg1, arg2);
    }
    else if (fun == DivideSymbol) {
	if (isZero(arg1))
	    ans = Constant(0.);
	else if (isZero(arg2))
	    ans = Constant(NA_REAL);
	else if (isOne(arg2))
	    ans = arg1;
	else if (isUminus(arg1)) {
	    ans = simplify(MinusSymbol,
			   PP(simplify(DivideSymbol, CADR(arg1), arg2)),
			   R_MissingArg);
	    UNPROTECT(1);
	}
	else if (isUminus(arg2)) {
	    ans = simplify(MinusSymbol,
			   PP(simplify(DivideSymbol, arg1, CADR(arg2))),
			   R_MissingArg);
	    UNPROTECT(1);
	}
	else ans = lang3(DivideSymbol, arg1, arg2);
    }
    else if (fun == PowerSymbol) {
	if (isZero(arg2))
	    ans = Constant(1.);
	else if (isZero(arg1))
	    ans = Constant(0.);
	else if (isOne(arg1))
	    ans = Constant(1.);
	else if (isOne(arg2))
	    ans = arg1;
	else
	    ans = lang3(PowerSymbol, arg1, arg2);
    }
    else if (fun == ExpSymbol) {
	/* FIXME: simplify exp(lgamma( E )) = gamma( E ) */
	ans = lang2(ExpSymbol, arg1);
    }
    else if (fun == LogSymbol) {
	/* FIXME: simplify log(gamma( E )) = lgamma( E ) */
	ans = lang2(LogSymbol, arg1);
    }
    else if (fun == CosSymbol)	ans = lang2(CosSymbol, arg1);
    else if (fun == SinSymbol)	ans = lang2(SinSymbol, arg1);
    else if (fun == TanSymbol)	ans = lang2(TanSymbol, arg1);
    else if (fun == CoshSymbol) ans = lang2(CoshSymbol, arg1);
    else if (fun == SinhSymbol) ans = lang2(SinhSymbol, arg1);
    else if (fun == TanhSymbol) ans = lang2(TanhSymbol, arg1);
    else if (fun == SqrtSymbol) ans = lang2(SqrtSymbol, arg1);
    else if (fun == PnormSymbol)ans = lang2(PnormSymbol, arg1);
    else if (fun == DnormSymbol)ans = lang2(DnormSymbol, arg1);
    else if (fun == AsinSymbol) ans = lang2(AsinSymbol, arg1);
    else if (fun == AcosSymbol) ans = lang2(AcosSymbol, arg1);
    else if (fun == AtanSymbol) ans = lang2(AtanSymbol, arg1);
    else if (fun == GammaSymbol)ans = lang2(GammaSymbol, arg1);
    else if (fun == LGammaSymbol)ans = lang2(LGammaSymbol, arg1);
    else if (fun == DiGammaSymbol) ans = lang2(DiGammaSymbol, arg1);
    else if (fun == TriGammaSymbol) ans = lang2(TriGammaSymbol, arg1);
    else if (fun == PsiSymbol){
       if (arg2 == R_MissingArg) ans = lang2(PsiSymbol, arg1);
       else ans = lang3(PsiSymbol, arg1, arg2);
    }
    else ans = Constant(NA_REAL);
    /* FIXME */
#ifdef NOTYET
    if (length(ans) == 2 && isAtomic(CADR(ans)) && CAR(ans) != MinusSymbol)
	c = eval(c, rho);
    if (length(c) == 3 && isAtomic(CADR(ans)) && isAtomic(CADDR(ans)))
	c = eval(c, rho);
#endif
    return ans;
}/* simplify() */
Exemplo n.º 13
0
int ecc_isOne(const uint32_t* A)
{
	return isOne(A);
}
Exemplo n.º 14
0
static Tree simplification (Tree sig)
{
	assert(sig);
	int		opnum;
	Tree	t1, t2, t3, t4;

	xtended* xt = (xtended*) getUserData(sig);
	// primitive elements
	if (xt)
	{
		//return 3;
		vector<Tree> args;
		for (int i=0; i<sig->arity(); i++) { args.push_back( sig->branch(i) ); }

        // to avoid negative power to further normalization
        if (xt != gPowPrim) {
            return xt->computeSigOutput(args);
        } else {
            return normalizeAddTerm(xt->computeSigOutput(args));
        }

	} else if (isSigBinOp(sig, &opnum, t1, t2)) {

		BinOp* op = gBinOpTable[opnum];

		Node n1 = t1->node();
		Node n2 = t2->node();

		if (isNum(n1) && isNum(n2)) 		return tree(op->compute(n1,n2));

		else if (op->isLeftNeutral(n1)) 	return t2;

		else if (op->isRightNeutral(n2)) 	return t1;

		else 								return normalizeAddTerm(sig);

	} else if (isSigDelay1(sig, t1)) {

		return normalizeDelay1Term (t1);

	} else if (isSigFixDelay(sig, t1, t2)) {

		return normalizeFixedDelayTerm (t1, t2);

	} else if (isSigIntCast(sig, t1)) {

		Tree 	tx;
		int		i;
		double 	x;
		Node 	n1 = t1->node();

		if (isInt(n1, &i)) 			return t1;
		if (isDouble(n1, &x)) 		return tree(int(x));
		if (isSigIntCast(t1, tx)) 	return t1;

		return sig;

	} else if (isSigFloatCast(sig, t1)) {

		Tree 	tx;
		int		i;
		double 	x;
		Node 	n1 = t1->node();

		if (isInt(n1, &i)) 				return tree(double(i));
		if (isDouble(n1, &x)) 			return t1;
		if (isSigFloatCast(t1, tx)) 	return t1;

		return sig;

     } else if (isSigSelect2(sig, t1, t2, t3)){

        Node n1 = t1->node();

        if (isZero(n1)) return t2;
        if (isNum(n1))  return t3;

        if (t2==t3) return t2;

        return sig;

    } else if (isSigSelect3(sig, t1, t2, t3, t4)){

        Node n1 = t1->node();

        if (isZero(n1)) return t2;
        if (isOne(n1))  return t3;
        if (isNum(n1))  return t4;

        if (t3==t4) return simplification(sigSelect2(t1,t2,t3));

        return sig;

	} else {

		return sig;
	}
}
Exemplo n.º 15
0
static PyObject *
Pympq_qdiv(PyObject *self, PyObject *args)
{
    PyObject *other = 0;
    PyObject *s = 0;
    int wasone;

    if ( self && Pympq_Check(self)) {
        if (!PyArg_ParseTuple(args, "|O", &other))
            return NULL;
    }
    else {
        if (!PyArg_ParseTuple(args, "O|O", &self, &other))
            return NULL;
    }
    wasone = isOne(other);
    /* optimize if self must be returned unchanged */
    if (Pympq_Check(self) && wasone) {
        /* optimize if self is mpq and result must==self */
        if (mpz_cmp_ui(mpq_denref(Pympq_AS_MPQ(self)), 1) != 0) {
            Py_INCREF(self);
            return self;
        }
        else {
            /* denominator is 1, optimize returning an mpz */
            s = Pympz_new();
            mpz_set(Pympz_AS_MPZ(s), mpq_numref(Pympq_AS_MPQ(self)));
            return s;
        }
    }
    else if (Pympz_Check(self) && wasone) {
        /* optimize if self is mpz and result must==self */
        Py_INCREF(self);
        return self;
    }
    /* normal, non-optimized case: must make new object as result */
    self = (PyObject*)Pympq_From_Rational(self);
    if (!self) {
        if (!PyErr_Occurred())
            TYPE_ERROR("first argument cannot be converted to 'mpq'");
        return NULL;
    }
    if (wasone) { /* self was mpf, float, int, long... */
        s = self;
    }
    else {     /* other explicitly present and !=1... must compute */
        other = (PyObject*)Pympq_From_Rational(other);
        if (!other) {
            Py_DECREF(self);
            if (!PyErr_Occurred())
                TYPE_ERROR("second argument cannot be converted to 'mpq'");
            return NULL;
        }
        if (mpq_sgn(Pympq_AS_MPQ(other))==0) {
            PyObject* result = 0;
            ZERO_ERROR("division or modulo by zero in qdiv");
            Py_DECREF(self);
            Py_DECREF(other);
            return result;
        }
        s = Pympq_new();
        mpq_div(Pympq_AS_MPQ(s), Pympq_AS_MPQ(self), Pympq_AS_MPQ(other));
        Py_DECREF(self);
        Py_DECREF(other);
    }
    if (mpz_cmp_ui(mpq_denref(Pympq_AS_MPQ(s)), 1) != 0) {
        return s;
    }
    else {
        /* denominator is 1, return an mpz */
        PyObject* ss = Pympz_new();
        if (ss)
            mpz_set(Pympz_AS_MPZ(ss), mpq_numref(Pympq_AS_MPQ(s)));
        Py_DECREF(s);
        return ss;
    }
}
Exemplo n.º 16
0
Func1& newProdFunction(Func1& f1, Func1& f2)
{
    if (isOne(f1)) {
        delete &f1;
        return f2;
    }
    if (isOne(f2)) {
        delete &f2;
        return f1;
    }
    if (isZero(f1) || isZero(f2)) {
        delete &f1;
        delete &f2;
        return *(new Const1(0.0));
    }
    if (isConstant(f1) && isConstant(f2)) {
        doublereal c1c2 = f1.c() * f2.c();
        delete &f1;
        delete &f2;
        return *(new Const1(c1c2));
    }
    if (isConstant(f1)) {
        doublereal c = f1.c();
        delete &f1;
        return newTimesConstFunction(f2, c);
    }
    if (isConstant(f2)) {
        doublereal c = f2.c();
        delete &f2;
        return newTimesConstFunction(f1, c);
    }

    if (isPow(f1) && isPow(f2)) {
        Func1& p = *(new Pow1(f1.c() + f2.c()));
        delete &f1;
        delete &f2;
        return p;
    }

    if (isExp(f1) && isExp(f2)) {
        Func1& p = *(new Exp1(f1.c() + f2.c()));
        delete &f1;
        delete &f2;
        return p;
    }

    bool tc1 = isTimesConst(f1);
    bool tc2 = isTimesConst(f2);

    if (tc1 || tc2) {
        doublereal c1 = 1.0, c2 = 1.0;
        Func1* ff1 = 0, *ff2 = 0;
        if (tc1) {
            c1 = f1.c();
            ff1 = &f1.func1_dup();
            delete &f1;
        } else {
            ff1 = &f1;
        }
        if (tc2) {
            c2 = f2.c();
            ff2 = &f2.func1_dup();
            delete &f2;
        } else {
            ff2 = &f2;
        }
        Func1& p = newProdFunction(*ff1, *ff2);

        if (c1* c2 != 1.0) {
            return newTimesConstFunction(p, c1*c2);
        } else {
            return p;
        }
    } else {
        return *(new Product1(f1, f2));
    }
}