ecl_long_long_t ecl_to_long_long(cl_object x) { if (ECL_FIXNUMP(x)) { return (ecl_long_long_t)ecl_fixnum(x); } else if (!ECL_BIGNUMP(x)) { (void)0; } else if (mpz_fits_slong_p(x->big.big_num)) { return (ecl_long_long_t)mpz_get_si(x->big.big_num); } else { cl_object copy = _ecl_big_register0(); int i = ECL_LONG_LONG_BITS - FIXNUM_BITS; mpz_fdiv_q_2exp(copy->bit.big_num, x->big.big_num, i); if (mpz_fits_ulong_p(copy->big.big_num)) { volatile ecl_long_long_t output; output = mpz_get_si(copy->big.big_num); for (i -= FIXNUM_BITS; i; i-= FIXNUM_BITS) { output = (output << FIXNUM_BITS); output += mpz_get_ui(x->big.big_num); } return output; } } FEwrong_type_argument(cl_list(3,ECL_SYM("INTEGER",437), ecl_negate(ecl_ash(ecl_make_fixnum(1), ECL_LONG_LONG_BITS-1)), ecl_one_minus(ecl_ash(ecl_make_fixnum(1), ECL_LONG_LONG_BITS-1))), x); }
gfc_constructor * gfc_constructor_lookup (gfc_constructor_base base, int offset) { gfc_constructor *c; splay_tree_node node; if (!base) return NULL; node = splay_tree_lookup (base, (splay_tree_key) offset); if (node) return (gfc_constructor *) node->value; /* Check if the previous node has a repeat count big enough to cover the offset looked for. */ node = splay_tree_predecessor (base, (splay_tree_key) offset); if (!node) return NULL; c = (gfc_constructor *) node->value; if (mpz_cmp_si (c->repeat, 1) > 0) { if (mpz_get_si (c->offset) + mpz_get_si (c->repeat) <= offset) c = NULL; } else c = NULL; return c; }
/** * Recombine les racines modulo mod1 et mod2 pour trouver les racines finales. * rac_modi[0]contient les valeurs en x des racines et rac_modi[1] celles en y. * @param rac_mod1 tableau des couples (x, y) de racines modulo mod1 * @param rac_mod2 tableau des couples (x, y) de racines modulo mod2 * @param nb1 nombre de racines (x, y) dans rac_mod1 * @param nb2 nombre de racines (x, y) dans rac_mod2 */ void find_roots(mpz_t *rac_mod1[2], mpz_t *rac_mod2[2], int nb1, int nb2, mpz_t mod1, mpz_t mod2, mpz_t **PY, mpz_t **QY, int *degres_PY, int *degresQY, int deg_P, int deg_Q, mpz_t mod){ int i, j, nb_racines=0; mpz_t rx, ry, isroot; mpz_inits(rx, ry, isroot, NULL); for(i=0; i<nb1; i++){ for(j=0; j<nb2; j++){ /* crt sur la i eme racine mod1 et la j eme mod2 */ crt(rx, rac_mod1[0][i], rac_mod2[0][j], mod1, mod2); crt(ry, rac_mod1[1][i], rac_mod2[1][j], mod1, mod2); /* on evalue en le resultat (rx, ry) trouve */ eval_bivXY(isroot, rx, ry, PY, degres_PY, deg_P, mod); /* on teste si le resultat est bien racine */ if (!mpz_cmp_si(isroot, 0)){ printf("(%ld, %ld) est racine\n", mpz_get_si(rx), mpz_get_si(ry)); nb_racines++; } } } printf("%d racines au total\n", nb_racines); }
void tollingProverExo::baseline(const mpq_t* input_q, int num_inputs, mpq_t* output_recomputed, int num_outputs) { struct In input; struct Out output; // Fill code here to prepare input from input_q. int inp = 0; for(int i = 0; i < NUM_CK_BITS/8; i++) { input.commitmentCK.bit[i] = mpz_get_ui(mpq_numref(input_q[inp++])); } for(int i = 0; i < NUM_COMMITMENT_CHUNKS; i++) { input.commitment.bit[i] = mpz_get_ui(mpq_numref(input_q[inp++])); } for(int i = 0; i < tolling_cons::MAX_SPOTCHECKS; i++) { tuple_t* tuple = &input.verifier_in.spotchecks[i]; tuple->time = mpz_get_si(mpq_numref(input_q[inp++])); tuple->toll_booth_id = mpz_get_ui(mpq_numref(input_q[inp++])); tuple->toll = mpz_get_ui(mpq_numref(input_q[inp++])); } input.verifier_in.time_threshold = mpz_get_si(mpq_numref(input_q[inp++])); // Do the computation compute(&input, &output); // Fill code here to dump output to output_recomputed. mpq_set_si(output_recomputed[0], 0, 1); mpq_set_si(output_recomputed[1], output.rejected, 1); mpq_set_ui(output_recomputed[2], output.cost, 1); }
gboolean random_boolean_with_probability(mpq_t p) { int i = g_random_int_range(0, mpz_get_si(mpq_denref(p))); if(i < mpz_get_si(mpq_numref(p))) { return TRUE; } return FALSE; }
/** * Applique euclide etendu a nb1 et nb2 * @param resultat tableau de 3 entiers : u, v et r tels que r=pgcd(nb1, nb2) et a*u+b*v=r */ void euclide_etendu(mpz_t nb1, mpz_t nb2, mpz_t *resultat){ mpz_t r, r1, u, u1, v, v1; mpz_inits(r, r1, NULL); mpz_set(r, nb1); mpz_set(r1, nb2); mpz_init_set_si(u, 1); mpz_init_set_si(u1, 0); mpz_init_set_si(v, 0); mpz_init_set_si(v1, 1); /* printf("u=%ld, v=%ld, pgcd=%ld\n",mpz_get_si(u),mpz_get_si(v), mpz_get_si(r)); */ mpz_t q, r_temp, u_temp, v_temp, tmp; mpz_inits(q, r_temp, u_temp, v_temp, tmp, NULL); while (mpz_get_si(r1) != 0){ mpz_cdiv_q(q,r,r1); /* Division entiere */ mpz_set(r_temp,r); mpz_set(u_temp,u); mpz_set(v_temp,v); mpz_set(r,r1); mpz_set(u,u1); mpz_set(v,v1); mpz_mul(tmp, q, r1); mpz_sub(r1, r_temp, tmp); /* r1=r_temp-q*r1; */ mpz_mul(tmp, q, u1); mpz_sub(u1, u_temp, tmp); mpz_mul(tmp, q, v1); mpz_sub(v1, v_temp, tmp); } if(mpz_get_si(r)<0){ mpz_neg(u,u); mpz_neg(v,v); mpz_neg(r,r); } mpz_set(resultat[0],u); mpz_set(resultat[1],v); mpz_set(resultat[2],r); /* Creer une fonction */ /* Clear */ mpz_clear(r); mpz_clear(r1); mpz_clear(u); mpz_clear(u1); mpz_clear(v); mpz_clear(v1); }
M2_bool rawLLL(MutableMatrix *M, MutableMatrix /* or null */ *U, gmp_QQ threshold, int strategy) { if (strategy == 0) { return LLLoperations::LLL(M,U,threshold); } long a = mpz_get_si(mpq_numref(threshold)); long b = mpz_get_si(mpq_denref(threshold)); return ntl_LLL(M,U,a,b,strategy); }
/** * Renvoie l'indice de la ligne du maximum de la colonne column * a partir de son element ligne r */ int bound_Max(mpz_t *M, int column, int matrix_length, int r){ int i, indice=r; mpz_t max; mpz_init(max); mpz_set(max, M[r*matrix_length+column]); for(i=r+1; i<matrix_length; i++){ if(mpz_get_si(M[i*matrix_length+column])>mpz_get_si(max)){ mpz_set(max, M[i*matrix_length+column]); indice=i; } } mpz_clear(max); return indice; }
int64_t xgcd_mpz_s64(int64_t* out_s, int64_t* out_t, const int64_t in_u, const int64_t in_v) { // NOTE: We hack the structure of an mpz_t so that // it uses stack memory. We use 128-bits instead of 64-bits // since it seems that some intermediate numbers use more than // 64-bits. We hope that 128-bits is enough. mpz_t g; mpz_t s; mpz_t t; mpz_t u; mpz_t v; uint64_t g_[2]; uint64_t s_[2]; uint64_t t_[2]; uint64_t u_[2]; uint64_t v_[2]; g->_mp_alloc = 128 / GMP_LIMB_BITS; g->_mp_size = 0; g->_mp_d = (mp_limb_t*)g_; s->_mp_alloc = 128 / GMP_LIMB_BITS; s->_mp_size = 0; s->_mp_d = (mp_limb_t*)s_; t->_mp_alloc = 128 / GMP_LIMB_BITS; t->_mp_size = 0; t->_mp_d = (mp_limb_t*)t_; u->_mp_alloc = 128 / GMP_LIMB_BITS; u->_mp_size = 0; u->_mp_d = (mp_limb_t*)u_; v->_mp_alloc = 128 / GMP_LIMB_BITS; v->_mp_size = 0; v->_mp_d = (mp_limb_t*)v_; mpz_set_ui(g, 0); mpz_set_ui(s, 0); mpz_set_ui(t, 0); mpz_set_si(u, in_u); mpz_set_si(v, in_v); mpz_gcdext(g, s, t, u, v); *out_s = mpz_get_si(s); *out_t = mpz_get_si(t); return mpz_get_si(g); }
int qsieve_getsize(mpz_t x) { int t; t = mpz_get_si(x); if(mpz_fits_slong_p(x)) return t; return (t<0 ? -0x7FFFFFFF : +0x7FFFFFFF); }
void power_mpz(ElementType &result, const ElementType& a, mpz_ptr n) const { if(mpz_fits_sint_p(n)) { int m = static_cast<int>(mpz_get_si(n)); power(result,a,m); } else throw exc::engine_error("expected small integer"); }
long camlidl_custom_mpz2_hash(value val) { CAMLparam1(val); __mpz_struct** mpz = (__mpz_struct**)(Data_custom_val(val)); long hash = mpz_get_si(*mpz); CAMLreturn(hash); }
mccp_result_t mccp_str_parse_int16(const char *buf, int16_t *val) { mccp_result_t ret = MCCP_RESULT_ANY_FAILURES; if (IS_VALID_STRING(buf) == true && val != NULL) { MP_INT v; mpz_init(&v); if ((ret = s_parse_bigint_in_range(buf, &v, &s_SHRT_MIN, &s_SHRT_MAX)) == MCCP_RESULT_OK) { #ifdef MCCP_ARCH_64_BITS int64_t v64 = (int64_t)mpz_get_si(&v); *val = (int16_t)v64; #else int64_t v64 = (int64_t)s_bignum_2_uint64_t(&v); *val = (int16_t)v64; #endif /* MCCP_ARCH_64_BITS */ } mpz_clear(&v); } return ret; }
mccp_result_t mccp_str_parse_int64_by_base(const char *buf, int64_t *val, unsigned int base) { mccp_result_t ret = MCCP_RESULT_ANY_FAILURES; if (IS_VALID_STRING(buf) == true && val != NULL && base > 1) { MP_INT v; mpz_init(&v); if ((ret = s_parse_bigint_by_base_in_range(buf, &v, base, &s_LLONG_MIN, &s_LLONG_MAX)) == MCCP_RESULT_OK) { #ifdef MCCP_ARCH_64_BITS *val = (int64_t)mpz_get_si(&v); #else *val = (int64_t)s_bignum_2_uint64_t(&v); #endif /* MCCP_ARCH_64_BITS */ } mpz_clear(&v); } return ret; }
/* This function is used by Miller */ int step3(int j, mpz_t m, mpz_t x, mpz_t a, mpz_t p, mpz_t p1){ int newm; int somethi = (int) pow(2.0,(double)j); printf("something = %d\n", somethi); if(mpz_fits_slong_p(m) == 0){ printf("this is problematic cause m is too huge for gmp to handle"); return 0; } else{ newm = mpz_get_si(m); } somethi = somethi*newm; printf("something (after new M) = %d\n", somethi); gmp_printf("x (before mod): %Zd\t & p1: %Zd\n", x, p1); mpz_powm_ui(x, a, somethi, p); gmp_printf("x (step 3) = %Zd\n", x); if(mpz_cmp_ui(x, 1) == 0){ printf("entered if\n"); return 0; } else if(mpz_cmp(x, p1) == 0){ printf("enter else if\n"); return 1; } else{ printf("enter else, let's see j's value, %d\n", j); return ++j; } }
size_t gfc_merge_initializers (gfc_typespec ts, gfc_expr *e, unsigned char *data, unsigned char *chk, size_t length) { size_t len = 0; gfc_constructor * c; switch (e->expr_type) { case EXPR_CONSTANT: case EXPR_STRUCTURE: len = expr_to_char (e, &data[0], &chk[0], length); break; case EXPR_ARRAY: for (c = e->value.constructor; c; c = c->next) { size_t elt_size = gfc_target_expr_size (c->expr); if (c->n.offset) len = elt_size * (size_t)mpz_get_si (c->n.offset); len = len + gfc_merge_initializers (ts, c->expr, &data[len], &chk[len], length - len); } break; default: return 0; } return len; }
/** *Affiche les racines de P */ void print_racines(mpz_t *P, int deg_P, mpz_t mod){ int i, nb_racines=0; mpz_t rac[deg_P+1]; for(i=0; i<deg_P+1; i++){ mpz_init_set_str(rac[i], "-1", 10); } printf("\n\nEtude du Polynome : "); print_P(P, deg_P); racines(rac, P, deg_P,&nb_racines, mod); for(i=0; i<deg_P+1; i++){ if (!mpz_cmp_si(rac[i],-1)){ printf("Polynome : "); print_P(P, deg_P); printf("%d racine(s)\n", nb_racines); return; } printf("%ld est racine\n", mpz_get_si(rac[i])); } printf("Polynome : "); print_P(P, deg_P); printf("%d racines\n", nb_racines); }
Term Yap_MkBigIntTerm(MP_INT *big) { CACHE_REGS Int nlimbs; MP_INT *dst = (MP_INT *)(H+2); CELL *ret = H; Int bytes; if (mpz_fits_slong_p(big)) { long int out = mpz_get_si(big); return MkIntegerTerm((Int)out); } // bytes = big->_mp_alloc * sizeof(mp_limb_t); // nlimbs = ALIGN_YAPTYPE(bytes,CELL)/CellSize; // this works, but it shouldn't need to do this... nlimbs = big->_mp_alloc; bytes = nlimbs*sizeof(CELL); if (nlimbs > (ASP-ret)-1024) { return TermNil; } H[0] = (CELL)FunctorBigInt; H[1] = BIG_INT; dst->_mp_size = big->_mp_size; dst->_mp_alloc = nlimbs*(CellSize/sizeof(mp_limb_t)); memmove((void *)(dst+1), (const void *)(big->_mp_d), bytes); H = (CELL *)(dst+1)+nlimbs; H[0] = EndSpecials; H++; return AbsAppl(ret); }
value largeint_to_si(value src) { signed long int tmp = mpz_get_si(Large_val(src)); value res = Val_long(tmp); if (Long_val(res) != tmp) { raiseprimitive0(SYS__EXN_OVERFLOW); } return res; }
std::pair<bool, int> RingZZ::get_si(mpz_t n) { if (not mpz_fits_slong_p(n)) return std::make_pair<bool, int>(false, 0); long a = mpz_get_si(n); int b = static_cast<int>(a); if (a == b) return std::make_pair<bool, int>(true, std::move(b)); return std::make_pair<bool, int>(false, 0); }
void g95_resolve_char(g95_expr *d, g95_expr *o, g95_expr *k) { d->ts.type = BT_CHARACTER; d->ts.kind = (k == NULL) ? g95_default_character_kind() : mpz_get_si(k->value.integer); d->value.function.name = g95_get_string(PREFIX "char_%d", d->ts.kind); }
void g95_resolve_floor(g95_expr *y, g95_expr *b, g95_expr *k) { y->ts.type = BT_INTEGER; y->ts.kind = (k == NULL) ? g95_default_integer_kind() : mpz_get_si(k->value.integer); y->value.function.name = g95_get_string(PREFIX "floor_%d", b->ts.kind); }
void g95_resolve_nint(g95_expr *v, g95_expr *n, g95_expr *kind) { v->ts.type = BT_INTEGER; v->ts.kind = (kind == NULL) ? g95_default_integer_kind() : mpz_get_si(kind->value.integer); v->value.function.name = g95_get_string(PREFIX "nint_%d", n->ts.kind); }
void g95_resolve_ceiling(g95_expr *t, g95_expr *d, g95_expr *k) { t->ts.type = BT_INTEGER; t->ts.kind = (k == NULL) ? g95_default_integer_kind() : mpz_get_si(k->value.integer); t->value.function.name = g95_get_string(PREFIX "ceiling_%d", d->ts.kind); }
static int node_copy_and_insert (splay_tree_node node, void *base) { int n = mpz_get_si (((gfc_constructor*)node->value)->offset); gfc_constructor_insert ((gfc_constructor_base*)base, node_copy (node, base), n); return 0; }
/* mpz -> pyint/pylong conversion; if the value fits in a python int, it returns a python int (optimised for that pathway), otherwise returns a python long */ PyObject * mpz_get_pyintlong(mpz_srcptr z) { if (mpz_fits_slong_p(z)) return PyInt_FromLong(mpz_get_si(z)); return mpz_get_pylong(z); }
static gfc_constructor * find_con_by_offset (splay_tree spt, mpz_t offset) { mpz_t tmp; gfc_constructor *ret = NULL; gfc_constructor *con; splay_tree_node sptn; /* The complexity is due to needing quick access to the linked list of constructors. Both a linked list and a splay tree are used, and both are kept up to date if they are array elements (which is the only time that a specific constructor has to be found). */ gcc_assert (spt != NULL); mpz_init (tmp); sptn = splay_tree_lookup (spt, (splay_tree_key) mpz_get_si (offset)); if (sptn) ret = (gfc_constructor*) sptn->value; else { /* Need to check and see if we match a range, so we will pull the next lowest index and see if the range matches. */ sptn = splay_tree_predecessor (spt, (splay_tree_key) mpz_get_si (offset)); if (sptn) { con = (gfc_constructor*) sptn->value; if (mpz_cmp_ui (con->repeat, 1) > 0) { mpz_init (tmp); mpz_add (tmp, con->n.offset, con->repeat); if (mpz_cmp (offset, tmp) < 0) ret = con; mpz_clear (tmp); } else ret = NULL; /* The range did not match. */ } else ret = NULL; /* No pred, so no match. */ } return ret; }
int main (int argc, char **argv) { char str[1000]; gmp_randstate_t rnd; mpz_t x, y, z; int i, j, k, s; tests_start (); gmp_randinit_default (rnd); mpz_init(x); mpz_init(y); mpz_init(z); for( i = 0 ; i < sizeof(tests1) / sizeof(tests1[0]) ; ++i ) { mpz_ui_pow_ui(x, 10, tests1[i].pow10); mpz_next_prime_candidate(y, x, rnd); mpz_sub(y, y, x); j = mpz_get_ui(y); if(j != tests1[i].np_off) { printf("\nnext_likely_prime(10^%d) - 10^%d: expected %d but got %d", tests1[i].pow10, tests1[i].pow10, tests1[i].np_off, j); abort(); } } for( i = 0 ; i < sizeof(tests2) / sizeof(tests2[0]) ; ++i ) { mpz_ui_pow_ui(x, 10, tests2[i].pow10); mpz_set(y, x); s = j = 0; for( ; ; ) { mpz_next_prime_candidate(y, y, rnd); mpz_sub(z, y, x); k = mpz_get_si(z); if(k >= 1000) break; j++; s += k * k; } if(j != tests2[i].count || s != tests2[i].sumsq) { printf("\nnext_likely_prime failed test2[%d], expected %d and %d but got %d and %d", i, tests2[i].count, tests2[i].sumsq, j, s); abort(); } } gmp_randclear (rnd); mpz_clear(z); mpz_clear(y); mpz_clear(x); tests_end (); exit (0); }
void g95_resolve_anint(g95_expr *h, g95_expr *b, g95_expr *kind) { h->ts.type = b->ts.type; h->ts.kind = (kind == NULL) ? b->ts.kind : mpz_get_si(kind->value.integer); h->value.function.name = g95_get_string(PREFIX "anint%d_%d", h->ts.kind, b->ts.kind); }
void g95_resolve_cmplx(g95_expr *k, g95_expr *r, g95_expr *e, g95_expr *k0) { k->ts.type = BT_COMPLEX; k->ts.kind = (k0 == NULL) ? g95_default_real_kind() : mpz_get_si(k0->value.integer); k->value.function.name = g95_get_string(PREFIX "cmplx"); }