struct var *add(struct array *args) { struct var *v, *ret; unsigned i; ret = calloc(1, sizeof(struct var)); ret->name = "__add__"; ret->type = ((struct var *)arrobj(args, 0))->type; if (ret->type == V_INT || ret->type == V_DOUB) return arith(args, ADD); else if (ret->type == V_STR) { for (i = 0; i < arrcnt(args); i++) { v = arrobj(args, i); if (!ret->val.sval) ret->val.sval = calloc(1, 1); ret->val.sval = realloc(ret->val.sval, strlen(ret->val.sval) + strlen(v->val.sval) + 1); strcat(ret->val.sval, v->val.sval); } } else if (ret->type == V_ARR) { ret->val.aval = ((struct var *)arrobj(args, 0))->val.aval; for (i = 1; i < arrcnt(args); i++) { v = arrobj(args, i); arradd(ret->val.aval, v); } } return ret; }
/* * Expand arithmetic expression. */ static char * expari(char *p, int flag, struct worddest *dst) { char *q, *start; arith_t result; int begoff; int quoted; int adj; quoted = *p++ == '"'; begoff = expdest - stackblock(); p = argstr(p, 0, NULL); STPUTC('\0', expdest); start = stackblock() + begoff; q = grabstackstr(expdest); result = arith(start); ungrabstackstr(q, expdest); start = stackblock() + begoff; adj = start - expdest; STADJUST(adj, expdest); CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest); fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result); adj = strlen(expdest); STADJUST(adj, expdest); if (!quoted) reprocess(expdest - adj - stackblock(), flag, VSNORMAL, 0, dst); return p; }
/* * The exp(1) builtin. */ int letcmd(int argc, char **argv) { const char *p; char *concat; char **ap; arith_t i; if (argc > 1) { p = argv[1]; if (argc > 2) { /* * Concatenate arguments. */ STARTSTACKSTR(concat); ap = argv + 2; for (;;) { while (*p) STPUTC(*p++, concat); if ((p = *ap++) == NULL) break; STPUTC(' ', concat); } STPUTC('\0', concat); p = grabstackstr(concat); } } else p = ""; i = arith(p); out1fmt(ARITH_FORMAT_STR "\n", i); return !i; }
bool check(misc::runner const & i_runner, host::generic_program i_program) { chrono::steady_clock::time_point tp = chrono::steady_clock::now(); host::buffer<pfm::int_> bufWrite(i_runner.m_context, item_count); typedef host::buffer<pfm::int_>::const_iterator iterator; i_runner.m_queue( run_kernel(i_program, fill_index(bufWrite), item_count)); i_runner.m_queue( run_kernel(i_program, twice(bufWrite), item_count)); auto future = i_runner.m_queue( bufWrite.with_range( [](iterator i_begin, iterator i_end) { return std::accumulate(i_begin, i_end, 0); })); std::future_status result = future.wait_until(tp + chrono::seconds(5)); assert(result == std::future_status::ready); assert(future.get() == arith(2, item_count)); return true; }
/* * Expand arithmetic expression. * Note that flag is not required as digits never require CTLESC characters. */ static char * expari(char *p) { char *q, *start; arith_t result; int begoff; int quoted; int adj; quoted = *p++ == '"'; begoff = expdest - stackblock(); p = argstr(p, 0); removerecordregions(begoff); STPUTC('\0', expdest); start = stackblock() + begoff; q = grabstackstr(expdest); result = arith(start); ungrabstackstr(q, expdest); start = stackblock() + begoff; adj = start - expdest; STADJUST(adj, expdest); CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest); fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result); adj = strlen(expdest); STADJUST(adj, expdest); if (!quoted) recordregion(begoff, expdest - stackblock(), 0); return p; }
void bv2int_rewriter_ctx::collect_power2(goal const& s) { ast_manager& m = m_trail.get_manager(); arith_util arith(m); bv_util bv(m); for (unsigned j = 0; j < s.size(); ++j) { expr* f = s.form(j); if (!m.is_or(f)) continue; unsigned sz = to_app(f)->get_num_args(); expr* x, *y, *v = 0; rational n; vector<rational> bounds; bool is_int, ok = true; for (unsigned i = 0; ok && i < sz; ++i) { expr* e = to_app(f)->get_arg(i); if (!m.is_eq(e, x, y)) { ok = false; break; } if (arith.is_numeral(y, n, is_int) && is_int && (x == v || v == 0)) { v = x; bounds.push_back(n); } else if (arith.is_numeral(x, n, is_int) && is_int && (y == v || v == 0)) { v = y; bounds.push_back(n); } else { ok = false; break; } } if (!ok || !v) continue; SASSERT(!bounds.empty()); lt_rational lt; // lt is a total order on rationals. std::sort(bounds.begin(), bounds.end(), lt); rational p(1); unsigned num_bits = 0; for (unsigned i = 0; ok && i < bounds.size(); ++i) { ok = (p == bounds[i]); p *= rational(2); ++num_bits; } if (!ok) continue; unsigned log2 = 0; for (unsigned i = 1; i <= num_bits; i *= 2) ++log2; if(log2 == 0) continue; expr* logx = m.mk_fresh_const("log2_v", bv.mk_sort(log2)); logx = bv.mk_zero_extend(num_bits - log2, logx); m_trail.push_back(logx); TRACE("bv2int_rewriter", tout << mk_pp(v, m) << " |-> " << mk_pp(logx, m) << "\n";); m_power2.insert(v, logx); }
/* * Expand arithmetic expression. Backup to start of expression, * evaluate, place result in (backed up) result, adjust string position. */ void expari(shinstance *psh, int flag) { char *p, *start; int result; int begoff; int quotes = flag & (EXP_FULL | EXP_CASE); int quoted; /* ifsfree(); */ /* * This routine is slightly over-complicated for * efficiency. First we make sure there is * enough space for the result, which may be bigger * than the expression if we add exponentation. Next we * scan backwards looking for the start of arithmetic. If the * next previous character is a CTLESC character, then we * have to rescan starting from the beginning since CTLESC * characters have to be processed left to right. */ #if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10 #error "integers with more than 10 digits are not supported" #endif CHECKSTRSPACE(psh, 12 - 2, psh->expdest); USTPUTC(psh, '\0', psh->expdest); start = stackblock(psh); p = psh->expdest - 1; while (*p != CTLARI && p >= start) --p; if (*p != CTLARI) error(psh, "missing CTLARI (shouldn't happen)"); if (p > start && *(p-1) == CTLESC) for (p = start; *p != CTLARI; p++) if (*p == CTLESC) p++; if (p[1] == '"') quoted=1; else quoted=0; begoff = (int)(p - start); removerecordregions(psh, begoff); if (quotes) rmescapes(psh, p+2); result = arith(psh, p+2); fmtstr(p, 12, "%d", result); while (*p++) ; if (quoted == 0) recordregion(psh, begoff, (int)(p - 1 - start), 0); result = (int)(psh->expdest - p + 1); STADJUST(psh, -result, psh->expdest); }
/* * Expand arithmetic expression. Backup to start of expression, * evaluate, place result in (backed up) result, adjust string position. */ void expari(int flag) { char *p, *start; intmax_t result; int adjustment; int begoff; int quotes = flag & (EXP_FULL | EXP_CASE); int quoted; /* ifsfree(); */ /* * This routine is slightly over-complicated for * efficiency. First we make sure there is * enough space for the result, which may be bigger * than the expression if we add exponentation. Next we * scan backwards looking for the start of arithmetic. If the * next previous character is a CTLESC character, then we * have to rescan starting from the beginning since CTLESC * characters have to be processed left to right. */ /* SPACE_NEEDED is enough for all digits, plus possible "-", plus 2 (why?) */ #define SPACE_NEEDED ((sizeof(intmax_t) * CHAR_BIT + 2) / 3 + 1 + 2) CHECKSTRSPACE((int)(SPACE_NEEDED - 2), expdest); USTPUTC('\0', expdest); start = stackblock(); p = expdest - 1; while (*p != CTLARI && p >= start) --p; if (*p != CTLARI) error("missing CTLARI (shouldn't happen)"); if (p > start && *(p-1) == CTLESC) for (p = start; *p != CTLARI; p++) if (*p == CTLESC) p++; if (p[1] == '"') quoted=1; else quoted=0; begoff = p - start; removerecordregions(begoff); if (quotes) rmescapes(p+2); result = arith(p+2); fmtstr(p, SPACE_NEEDED, "%"PRIdMAX, result); while (*p++) ; if (quoted == 0) recordregion(begoff, p - 1 - start, 0); adjustment = expdest - p + 1; STADJUST(-adjustment, expdest); }
int main(int argc, char *argv[]) { int x = atoi(argv[1]); int y = atoi(argv[2]); int a = arith(x,y); int b = optarith(x,y); if (a != b) printf("Whoops! "); printf("x = %d, y = %d, normal --> %d, opt --> %d\n", x, y, a, b); }
/* Process integer exponent. */ void level4(double* result){ double hold; level5(result); if(*bas_token== '^') { get_token(); level4(&hold); arith('^', result, &hold); } }
/* Multiply or divide two factors. */ void level3(double* result){ char op; double hold; level4(result); while((op = *bas_token) == '*' || op == '/' || op == '%' || op == '@' || op == '$' || op == '|' || op == '&') { get_token(); level4(&hold); arith(op, result, &hold); } }
void CMPolynomial::level5(double& result) // powers { double hold; level6(result); if (token == Power) { get_token(); level6(hold); arith(Power,result,hold); } }
void level4(double *hold) { double h; level5( hold); if (*(token) == '^') { get_token(); level5( &h); arith( '^', hold, &h); } }
static cfloat *CSSum( cfloat *op1, cfloat *op2, int (*arith)( int, int ) ) /****************************************************************************/ { int carry; int pos; int length; int op1left; int op2left; int farleft; int op1right; int op2right; int farright; cfloat *result; op1left = op1->exp; op2left = op2->exp; op1right = op1left - op1->len; op2right = op2left - op2->len; farleft = Max( op1left, op2left ); farright = Min( op1right, op2right ); length = farleft - farright + 1; /* result length + extra digit*/ pos = farright + 1; result = CFAlloc( length ); result->exp = farleft + 1; result->len = length; carry = 0; length--; while( pos <= farleft ) { if( pos > op1right && pos <= op1left ) { carry += CFAccess( op1, op1left - pos ); } if( pos > op2right && pos <= op2left ) { carry = arith( carry, CFAccess( op2, op2left - pos ) ); } if( carry < 0 ) { CFDeposit( result, length, carry + 10 ); carry = -1; } else if( carry > 9 ) { CFDeposit( result, length, carry - 10 ); carry = 1; } else { CFDeposit( result, length, carry ); carry = 0; } pos++; length--; } CFDeposit( result, length, carry ); result->sign = op1->sign; CFClean( result ); return( result ); }
void CMPolynomial::level4(double& result) // multiplication, division, assign { register int op; double hold; level5(result); while (token==Assign || token == Mul || token == Div || token == Mod) { op = token; get_token(); level5(hold); arith(op,result,hold); } }
void level3(double *hold) { char op; double h; level4( hold); while ((op=*(token)) == '*' || op == '/' || op == '%') { get_token(); level4( &h); arith( op, hold, &h); } }
struct var *minus(struct array *args) { struct var *v, *ret; ret = arrobj(args, 0); if (ret->type == V_INT || ret->type == V_DOUB) return arith(args, SUB); else if (ret->type == V_ARR) { v = arrobj(args, 1); arrrm(ret->val.aval, (v->type == V_INT) ? v->val.ival : v->val.fval); } return ret; }
void expand_literals(expr_ref_vector& conjs) { arith_util arith(m); datatype_util dt(m); bv_util bv(m); expr* e1, *e2, *c, *val; rational r; unsigned bv_size; TRACE("pdr", tout << "begin expand\n"; for (unsigned i = 0; i < conjs.size(); ++i) { tout << mk_pp(conjs[i].get(), m) << "\n"; });
void level2(double *hold) { char op; double h; level3( hold); while ((op=*(token)) == '+' || op == '-') { get_token(); level3( &h); arith(op, hold, &h); } }
void compileBinExp(Expression &exp1, Expression &exp2, Token binOp) { Expression exp; if (exp1.kindOf == Expression::NUM) if (exp2.kindOf == Expression::NUM) { exp1.kindOf = Expression::NUM; exp1.value = arith(exp1.value, exp2.value, binOp); } //print(binOp.kindOf); // if (exp1.kindOf == ) }
void CMPolynomial::level3(double& result) //plus or minus { register int op; double hold; level4(result); while (token == Plus || token == Minus) { op = token; get_token(); level4(hold); arith(op,result,hold); } }
/* * Expand arithmetic expression. Backup to start of expression, * evaluate, place result in (backed up) result, adjust string position. */ void expari(int flag) { char *p, *q, *start; arith_t result; int begoff; int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); int quoted; /* * This routine is slightly over-complicated for * efficiency. First we make sure there is * enough space for the result, which may be bigger * than the expression. Next we * scan backwards looking for the start of arithmetic. If the * next previous character is a CTLESC character, then we * have to rescan starting from the beginning since CTLESC * characters have to be processed left to right. */ CHECKSTRSPACE(DIGITS(result) - 2, expdest); USTPUTC('\0', expdest); start = stackblock(); p = expdest - 2; while (p >= start && *p != CTLARI) --p; if (p < start || *p != CTLARI) error("missing CTLARI (shouldn't happen)"); if (p > start && *(p - 1) == CTLESC) for (p = start; *p != CTLARI; p++) if (*p == CTLESC) p++; if (p[1] == '"') quoted=1; else quoted=0; begoff = p - start; removerecordregions(begoff); if (quotes) rmescapes(p+2); q = grabstackstr(expdest); result = arith(p+2); ungrabstackstr(q, expdest); fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result); while (*p++) ; if (quoted == 0) recordregion(begoff, p - 1 - start, 0); result = expdest - p + 1; STADJUST(-result, expdest); }
static arith_t run_arith(const char *s) { arith_state_t math_state; arith_t result; math_state.lookupvar = getenv; math_state.setvar = setvar; math_state.endofname = arith_endofname; result = arith(&math_state, s); if (math_state.errmsg) printf("let: %s\n", math_state.errmsg); return result; }
/* * Expand arithmetic expression. * Note that flag is not required as digits never require CTLESC characters. */ static char * expari(char *p) { char *q, *start; arith_t result; int begoff; int quoted; int c; int nesting; int adj; quoted = *p++ == '"'; begoff = expdest - stackblock(); argstr(p, 0); removerecordregions(begoff); STPUTC('\0', expdest); start = stackblock() + begoff; q = grabstackstr(expdest); result = arith(start); ungrabstackstr(q, expdest); start = stackblock() + begoff; adj = start - expdest; STADJUST(adj, expdest); CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest); fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result); adj = strlen(expdest); STADJUST(adj, expdest); if (!quoted) recordregion(begoff, expdest - stackblock(), 0); nesting = 1; while (nesting > 0) { c = *p++; if (c == CTLESC) p++; else if (c == CTLARI) nesting++; else if (c == CTLENDARI) nesting--; else if (c == CTLVAR) p++; /* ignore variable substitution byte */ else if (c == '\0') return p - 1; } return p; }
int main(int argc, char**argv) { itv_t a,b,c; bound_t bound; itv_internal_t* intern; ap_fpu_init(); mpfr_set_default_prec(4046); intern = itv_internal_alloc(); itv_init(a); itv_init(b); itv_init(c); bound_init(bound); /* Positive or negative intervals */ bound_set_int(b->inf,-3); bound_set_int(b->sup,5); bound_set_int(c->inf,-1); bound_set_int(c->sup,5); bound_set_int(bound,4); arith(intern,a,b,c,bound); itv_neg(c,c); arith(intern,a,b,c,bound); itv_neg(b,b); arith(intern,a,b,c,bound); itv_neg(c,c); arith(intern,a,b,c,bound); /* general intervals */ bound_set_int(b->inf,3); bound_set_int(b->sup,5); bound_set_int(c->inf,7); bound_set_int(c->sup,11); bound_set_int(bound,3); arith(intern,a,b,c,bound); bound_set_int(bound,-3); arith(intern,a,b,c,bound); /* aliases */ bound_set_int(b->inf,3); bound_set_int(b->sup,5); bound_set_int(c->inf,7); bound_set_int(c->sup,11); bound_set_int(bound,3); arith(intern,b,b,b,bound); bound_set_int(bound,-3); arith(intern,b,b,b,bound); itv_clear(a); itv_clear(b); itv_clear(c); bound_clear(bound); itv_internal_free(intern); }
bool check(misc::runner const & i_runner, host::generic_program i_program) { chrono::steady_clock::time_point tp = chrono::steady_clock::now(); host::buffer<pfm::int_> bufWrite(i_runner.m_context, item_count); typedef host::buffer<pfm::int_>::const_iterator iterator; // kernel内で使用できる事の確認 i_runner.m_queue( run_kernel( i_program, fill_index(bufWrite), item_count)); auto future = i_runner.m_queue( bufWrite.with_range( [](iterator i_begin, iterator i_end){ return std::accumulate(i_begin, i_end, 0); })); std::future_status result = future.wait_until(tp + chrono::seconds(5)); assert(result == std::future_status::ready); assert(future.get() == arith(2, item_count)); // ホストから呼べない事の確認 try { int const a = 1; //gcc-4.7.2 twice(1)と書くと内部エラー i_runner.m_queue( host::run_kernel( i_program, twice(a), 1) ); assert(false); } catch (cl::Error err) { assert(err.err() == CL_INVALID_KERNEL_NAME); } return true; }
/* Add or subtract two terms. */ void level2(double* result){ char op; double hold; // printf("level2: came with bas_token %s\n", bas_token ); level3(result); //printf("level2: ret lev3 bas_token %s, result=%lf\n", *result); // == >= <= != // ~ : _ ! while((op = *bas_token) == '+' || op == '-' || op == '<' || op == '>' || op == '~' || op == ':' || op == '_' || op == '!' ) { //printf("arith: op=%s \n", bas_token ); get_token(); //printf("arith: op=%s, \n", bas_token ); level3(&hold); arith(op, result, &hold); } }
void mean::entry() { sc_signed i(24); // Will hold input a sc_signed j(24); // Will hold input b sc_signed k(24); // Will hold input c sc_signed l(24); // Will hold input d sc_signed arith(24); // Arithmetic mean sc_signed geom(24); // Geometric mean sc_signed harmonic(24); // Harmonic mean while (true) { // read all inputs send_input.write(true); do { wait(); } while (data_available != true); i = in.read().to_int(); wait(); j = in.read().to_int(); wait(); k = in.read().to_int(); wait(); l = in.read().to_int(); send_input.write(false); // Calculate arithmetic mean arith = (i + j + k + l) / 4; // Calculate geometric mean calculate_geometric_mean(i, j, k, l, geom); // Calculate harmonic mean calculate_harmonic_mean(i, j, k, l, harmonic); // write all outputs do { wait(); } while (receiver_ready != true); data_ready.write(true); wait(); out.write(arith); wait(); out.write(geom); wait(); out.write(harmonic); wait(); data_ready.write(false); } } // end of entry function
/** issue a query filling the model storage * this will change when I will learn how to call SWI-Prolog completion interface */ void Completion::initialize(QStringList &strings) { SwiPrologEngine::in_thread _int; try { PlTerm p,m,a,l,v; PlQuery q("setof", PlTermv(p, quv(m, quv(a, join(PlCompound("current_predicate", mod(m, arith(p, a))), neg(C("sub_atom", PlTermv(p, zero, one, _V, A("$")))) ))), l)); if (q.next_solution()) for (PlTail x(l); x.next(v); ) strings.append(CCP(v)); } catch(PlException e) { qDebug() << CCP(e); } }
bool is_value_sort(ast_manager& m, sort* s) { arith_util arith(m); datatype_util data(m); bv_util bv(m); ptr_vector<sort> sorts; ast_mark mark; sorts.push_back(s); while (!sorts.empty()) { s = sorts.back(); sorts.pop_back(); if (mark.is_marked(s)) { continue; } mark.mark(s, true); if (arith.is_int_real(s)) { // simple } else if (m.is_bool(s)) { // simple } else if (bv.is_bv_sort(s)) { // simple } else if (data.is_datatype(s)) { ptr_vector<func_decl> const& cs = *data.get_datatype_constructors(s); for (unsigned i = 0; i < cs.size(); ++i) { func_decl* f = cs[i]; for (unsigned j = 0; j < f->get_arity(); ++j) { sorts.push_back(f->get_domain(j)); } } } else { return false; } } return true; }