void mpc_and (mpc_t *rop, mpc_t op1, mpc_t op2) { /* Little did I realize that I would eventually make it * an error to apply bitwise operators to floating point * variables. */ mpc_t temp; temp.precision = (op1.precision < op2.precision) ? op2.precision : op1.precision; mpz_init (temp.object); if (op1.precision == op2.precision) mpz_and (temp.object, op1.object, op2.object); else if (op1.precision > op2.precision) { mpz_set (temp.object, op2.object); power_of_ten (temp.object, op1.precision - op2.precision); mpz_and (temp.object, op1.object, temp.object); } else { mpz_set (temp.object, op1.object); power_of_ten (temp.object, op2.precision - op1.precision); mpz_and (temp.object, temp.object, op2.object); } rop->precision = temp.precision; mpz_set (rop->object, temp.object); }
static object integer_log_op2(fixnum op,object x,enum type tx,object y,enum type ty) { object u=big_fixnum1; object ux=tx==t_bignum ? x : (mpz_set_si(MP(big_fixnum2),fix(x)), big_fixnum2); object uy=ty==t_bignum ? y : (mpz_set_si(MP(big_fixnum3),fix(y)), big_fixnum3); switch(op) { case BOOLCLR: mpz_set_si(MP(u),0);break; case BOOLSET: mpz_set_si(MP(u),-1);break; case BOOL1: mpz_set(MP(u),MP(ux));break; case BOOL2: mpz_set(MP(u),MP(uy));break; case BOOLC1: mpz_com(MP(u),MP(ux));break; case BOOLC2: mpz_com(MP(u),MP(uy));break; case BOOLAND: mpz_and(MP(u),MP(ux),MP(uy));break; case BOOLIOR: mpz_ior(MP(u),MP(ux),MP(uy));break; case BOOLXOR: mpz_xor(MP(u),MP(ux),MP(uy));break; case BOOLEQV: mpz_xor(MP(u),MP(ux),MP(uy));mpz_com(MP(u),MP(u));break; case BOOLNAND: mpz_and(MP(u),MP(ux),MP(uy));mpz_com(MP(u),MP(u));break; case BOOLNOR: mpz_ior(MP(u),MP(ux),MP(uy));mpz_com(MP(u),MP(u));break; case BOOLANDC1:mpz_com(MP(u),MP(ux));mpz_and(MP(u),MP(u),MP(uy));break; case BOOLANDC2:mpz_com(MP(u),MP(uy));mpz_and(MP(u),MP(ux),MP(u));break; case BOOLORC1: mpz_com(MP(u),MP(ux));mpz_ior(MP(u),MP(u),MP(uy));break; case BOOLORC2: mpz_com(MP(u),MP(uy));mpz_ior(MP(u),MP(ux),MP(u));break; default:break;/*FIXME error*/ } return u; }
static PyObject * GMPy_MPZ_And_Slot(PyObject *self, PyObject *other) { MPZ_Object *result; if (CHECK_MPZANY(self)) { if (CHECK_MPZANY(other)) { if (!(result = GMPy_MPZ_New(NULL))) return NULL; mpz_and(result->z, MPZ(self), MPZ(other)); } else { if (!(result = GMPy_MPZ_From_Integer(other, NULL))) return NULL; mpz_and(result->z, MPZ(self), result->z); } } else if (CHECK_MPZANY(other)) { if (!(result = GMPy_MPZ_From_Integer(self, NULL))) return NULL; mpz_and(result->z, result->z, MPZ(other)); } else { Py_RETURN_NOTIMPLEMENTED; } return (PyObject*)result; }
static PyObject * GMPy_XMPZ_IAnd_Slot(PyObject *self, PyObject *other) { if (CHECK_MPZANY(other)) { mpz_and(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } if (PyIntOrLong_Check(other)) { mpz_set_PyIntOrLong(global.tempz, other); mpz_and(MPZ(self), MPZ(self), global.tempz); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static _rs_inline obj bignum_xor( obj a, obj b ) { mpz_t r, a1, b1, a2, b2; OBJ_TO_MPZ(a1, a); OBJ_TO_MPZ(b1, b); mpz_init(r); mpz_init(a2); mpz_init(b2); mpz_com(a2, a1); mpz_com(b2, b1); mpz_and(r, a1, b2); mpz_and(b2, a2, b1); mpz_ior(a1, r, b2); return mpz_to_bignum(a1); }
/* FilterRangeFromMP_INT : make a mask for width and offset, AND the given value with the mask and return that ANDed value shifted right offset bits */ PtrMP_INT FilterRangeFromMP_INT (PtrMP_INT value, Bits width, Bits offset) { PtrMP_INT mask = MakeMaskForRange (width, offset); mpz_and (mask, value, mask); /* mask= value & mask */ mpz_div_2exp (mask, mask, offset); /* mask >>= offset */ return mask; }
iset_t GMPISInterset(iset_t a, iset_t b) { iset_t r; r = GMPISInit(); mpz_and(*r,*a,*b); return r; }
static _rs_inline obj bignum_and( obj a, obj b ) { mpz_t r, a1, b1; OBJ_TO_MPZ(a1, a); OBJ_TO_MPZ(b1, b); mpz_init(r); mpz_and(r, a1, b1); return mpz_to_bignum(r); }
VAL bigAnd(VM* vm, VAL x, VAL y) { idris_requireAlloc(IDRIS_MAXGMP); mpz_t* bigint; VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0); idris_doneAlloc(); bigint = (mpz_t*)(((char*)cl) + sizeof(Closure)); mpz_and(*bigint, GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y))); SETTY(cl, CT_BIGINT); cl -> info.ptr = (void*)bigint; return cl; }
static PyObject * Pyxmpz_inplace_and(PyObject *self, PyObject *other) { mpz_t tempz; if (CHECK_MPZANY(other)) { mpz_and(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(other)); Py_INCREF(self); return self; } if (PyIntOrLong_Check(other)) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_and(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), tempz); mpz_cloc(tempz); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
VALUE method_hash_vl(VALUE self, VALUE bitlength) { int bl = NUM2INT(bitlength); // for hard typecasting unsigned char one_char; char* result; result = malloc(bl*sizeof(char)); unsigned long long len = RSTRING_LEN(self); char *string = RSTRING_PTR(self); if(len == 0){ return 0; } mpz_t x, mask, long_len; mpz_init_set_ui (long_len, len); one_char = RSTRING_PTR(self)[0]; mpz_init_set_ui (x, one_char << 7); int m = 1000003; // generating mask of length bitlength filled with 1 mpz_init (mask); mpz_ui_pow_ui(mask, 2, bl); mpz_sub_ui (mask, mask, 1); mpz_t computations, byte; mpz_init(computations); mpz_init2 (byte, 8); int i = 0; for(i; i < len; i++) { one_char = string[i]; mpz_set_ui(byte, one_char); mpz_mul_ui(computations, x, m); mpz_xor(computations, computations, byte); mpz_and (x, mask, computations); } mpz_xor(x, x, long_len); //gmp_printf ("C xored x is %Zd\n", x); mpz_get_str (result, 10, x); VALUE res = rb_str_new2(result); mpz_clear(x); mpz_clear(byte); mpz_clear(computations); mpz_clear(mask); mpz_clear(long_len); free(result); return res; }
RCP<const UnivariatePolynomial> mul_uni_poly(RCP<const UnivariatePolynomial> a, RCP<const UnivariatePolynomial> b) { //TODO: Use `const RCP<const UnivariatePolynomial> &a` for input arguments, // even better is use `const UnivariatePolynomial &a` unsigned int da = a->degree_; unsigned int db = b->degree_; int sign = 1; if ((--(a->dict_.end()))->second < 0) { a = neg_uni_poly(*a); sign = -1 * sign; } if ((--(b->dict_.end()))->second < 0) { b = neg_uni_poly(*b); sign = -1 * sign; } mpz_class p = std::max(a->max_coef(), b->max_coef()); unsigned int N = bit_length(std::min(da + 1, db + 1)) + bit_length(p) + 1; mpz_class a1 = 1 << N; mpz_class a2 = a1 / 2; mpz_class mask = a1 - 1; mpz_class sa = a->eval_bit(N); mpz_class sb = b->eval_bit(N); mpz_class r = sa*sb; std::vector<mpz_class> v; mpz_class carry = 0; while (r != 0 or carry != 0) { mpz_class b; mpz_and(b.get_mpz_t(), r.get_mpz_t(), mask.get_mpz_t()); if (b < a2) { v.push_back(b + carry); carry = 0; } else { v.push_back(b - a1 + carry); carry = 1; } r >>= N; } if (sign == -1) return neg_uni_poly(*make_rcp<const UnivariatePolynomial>(a->var_, v)); else return make_rcp<const UnivariatePolynomial>(a->var_, v); }
num_t num_int_two_op(optype_t op_type, num_t a, num_t b) { num_t r; r = num_new_z(N_TEMP, NULL); a = num_new_z(N_TEMP, a); b = num_new_z(N_TEMP, b); switch (op_type) { case OP_AND: mpz_and(Z(r), Z(a), Z(b)); break; case OP_OR: mpz_ior(Z(r), Z(a), Z(b)); break; case OP_XOR: mpz_xor(Z(r), Z(a), Z(b)); break; case OP_SHR: if (!mpz_fits_ulong_p(Z(b))) { yyxerror ("Second argument to shift needs to fit into an unsigned long C datatype"); return NULL; } mpz_fdiv_q_2exp(Z(r), Z(a), mpz_get_ui(Z(b))); break; case OP_SHL: if (!mpz_fits_ulong_p(Z(b))) { yyxerror ("Second argument to shift needs to fit into an unsigned long C datatype"); return NULL; } mpz_mul_2exp(Z(r), Z(a), mpz_get_ui(Z(b))); break; default: yyxerror("Unknown op in num_int_two_op"); } return r; }
//------------------------------------------------------------------------------ // Name: bitwise_and //------------------------------------------------------------------------------ knumber_base *knumber_integer::bitwise_and(knumber_base *rhs) { if(knumber_integer *const p = dynamic_cast<knumber_integer *>(rhs)) { mpz_and(mpz_, mpz_, p->mpz_); return this; } else if(knumber_float *const p = dynamic_cast<knumber_float *>(rhs)) { knumber_float *f = new knumber_float(this); delete this; return f->bitwise_and(p); } else if(knumber_fraction *const p = dynamic_cast<knumber_fraction *>(rhs)) { knumber_fraction *f = new knumber_fraction(this); delete this; return f->bitwise_and(p); } else if(knumber_error *const p = dynamic_cast<knumber_error *>(rhs)) { delete this; return p->clone(); } Q_ASSERT(0); return 0; }
static Variant HHVM_FUNCTION(gmp_and, const Variant& dataA, const Variant& dataB) { mpz_t gmpDataA, gmpDataB, gmpReturn; if (!variantToGMPData(cs_GMP_FUNC_NAME_GMP_AND, gmpDataA, dataA)) { return false; } if (!variantToGMPData(cs_GMP_FUNC_NAME_GMP_AND, gmpDataB, dataB)) { mpz_clear(gmpDataA); return false; } mpz_init(gmpReturn); mpz_and(gmpReturn, gmpDataA, gmpDataB); Variant ret = NEWOBJ(GMPResource)(gmpReturn); mpz_clear(gmpDataA); mpz_clear(gmpDataB); mpz_clear(gmpReturn); return ret; }
int main (int argc, char *argv[]) { const char usage[] = "usage: findlc [-dv] m2exp [low_merit [high_merit]]\n"; int f; int v_lose, m_lose, v_best, m_best; int c; int debug = 1; int cnt_high_merit; mpz_t m; unsigned long int m2exp; #define DIMS 6 /* dimensions run in spectral test */ mpf_t v[DIMS-1]; /* spectral test result (there's no v for 1st dimension */ mpf_t f_merit, low_merit, high_merit; mpz_t acc, minus8; mpz_t min, max; mpz_t s; mpz_init (m); mpz_init (a); for (f = 0; f < DIMS-1; f++) mpf_init (v[f]); mpf_init (f_merit); mpf_init_set_d (low_merit, .1); mpf_init_set_d (high_merit, .1); while ((c = getopt (argc, argv, "a:di:hv")) != -1) switch (c) { case 'd': /* debug */ g_debug++; break; case 'v': /* print version */ puts (rcsid[1]); exit (0); case 'h': case '?': default: fputs (usage, stderr); exit (1); } argc -= optind; argv += optind; if (argc < 1) { fputs (usage, stderr); exit (1); } /* Install signal handler. */ if (SIG_ERR == signal (SIGSEGV, sh_status)) { perror ("signal (SIGSEGV)"); exit (1); } if (SIG_ERR == signal (SIGHUP, sh_status)) { perror ("signal (SIGHUP)"); exit (1); } printf ("findlc: version: %s\n", rcsid[1]); m2exp = atol (argv[0]); mpz_init_set_ui (m, 1); mpz_mul_2exp (m, m, m2exp); printf ("m = 0x"); mpz_out_str (stdout, 16, m); puts (""); if (argc > 1) /* have low_merit */ mpf_set_str (low_merit, argv[1], 0); if (argc > 2) /* have high_merit */ mpf_set_str (high_merit, argv[2], 0); if (debug) { fprintf (stderr, "low_merit = "); mpf_out_str (stderr, 10, 2, low_merit); fprintf (stderr, "; high_merit = "); mpf_out_str (stderr, 10, 2, high_merit); fputs ("\n", stderr); } mpz_init (minus8); mpz_set_si (minus8, -8L); mpz_init_set_ui (acc, 0); mpz_init (s); mpz_init_set_d (min, 0.01 * pow (2.0, (double) m2exp)); mpz_init_set_d (max, 0.99 * pow (2.0, (double) m2exp)); mpz_true_random (s, m2exp); /* Start. */ mpz_setbit (s, 0); /* Make it odd. */ v_best = m_best = 2*(DIMS-1); for (;;) { mpz_add (acc, acc, s); mpz_mod_2exp (acc, acc, m2exp); #if later mpz_and_si (a, acc, -8L); #else mpz_and (a, acc, minus8); #endif mpz_add_ui (a, a, 5); if (mpz_cmp (a, min) <= 0 || mpz_cmp (a, max) >= 0) continue; spectral_test (v, DIMS, a, m); for (f = 0, v_lose = m_lose = 0, cnt_high_merit = DIMS-1; f < DIMS-1; f++) { merit (f_merit, f + 2, v[f], m); if (mpf_cmp_ui (v[f], 1 << (30 / (f + 2) + (f == 2))) < 0) v_lose++; if (mpf_cmp (f_merit, low_merit) < 0) m_lose++; if (mpf_cmp (f_merit, high_merit) >= 0) cnt_high_merit--; } if (0 == v_lose && 0 == m_lose) { mpz_out_str (stdout, 10, a); puts (""); fflush (stdout); if (0 == cnt_high_merit) break; /* leave loop */ } if (v_lose < v_best) { v_best = v_lose; printf ("best (v_lose=%d; m_lose=%d): ", v_lose, m_lose); mpz_out_str (stdout, 10, a); puts (""); fflush (stdout); } if (m_lose < m_best) { m_best = m_lose; printf ("best (v_lose=%d; m_lose=%d): ", v_lose, m_lose); mpz_out_str (stdout, 10, a); puts (""); fflush (stdout); } } mpz_clear (m); mpz_clear (a); for (f = 0; f < DIMS-1; f++) mpf_clear (v[f]); mpf_clear (f_merit); mpf_clear (low_merit); mpf_clear (high_merit); printf ("done.\n"); return 0; }
void hex_random_op3 (enum hex_random_op op, unsigned long maxbits, char **ap, char **bp, char **rp) { mpz_t a, b, r; unsigned long abits, bbits; unsigned signs; mpz_init (a); mpz_init (b); mpz_init (r); abits = gmp_urandomb_ui (state, 32) % maxbits; bbits = gmp_urandomb_ui (state, 32) % maxbits; mpz_rrandomb (a, state, abits); mpz_rrandomb (b, state, bbits); signs = gmp_urandomb_ui (state, 3); if (signs & 1) mpz_neg (a, a); if (signs & 2) mpz_neg (b, b); switch (op) { default: abort (); case OP_ADD: mpz_add (r, a, b); break; case OP_SUB: mpz_sub (r, a, b); break; case OP_MUL: mpz_mul (r, a, b); break; case OP_GCD: if (signs & 4) { /* Produce a large gcd */ unsigned long gbits = gmp_urandomb_ui (state, 32) % maxbits; mpz_rrandomb (r, state, gbits); mpz_mul (a, a, r); mpz_mul (b, b, r); } mpz_gcd (r, a, b); break; case OP_LCM: if (signs & 4) { /* Produce a large gcd */ unsigned long gbits = gmp_urandomb_ui (state, 32) % maxbits; mpz_rrandomb (r, state, gbits); mpz_mul (a, a, r); mpz_mul (b, b, r); } mpz_lcm (r, a, b); break; case OP_AND: mpz_and (r, a, b); break; case OP_IOR: mpz_ior (r, a, b); break; case OP_XOR: mpz_xor (r, a, b); break; } gmp_asprintf (ap, "%Zx", a); gmp_asprintf (bp, "%Zx", b); gmp_asprintf (rp, "%Zx", r); mpz_clear (a); mpz_clear (b); mpz_clear (r); }
/* returns 0 on success */ int gen_consts (int numb, int nail, int limb) { mpz_t x, mask, y, last; unsigned long a, b; unsigned long ofl, ofe; printf ("/* This file is automatically generated by gen-fac.c */\n\n"); printf ("#if GMP_NUMB_BITS != %d\n", numb); printf ("Error , error this data is for %d GMP_NUMB_BITS only\n", numb); printf ("#endif\n"); #if 0 printf ("#if GMP_LIMB_BITS != %d\n", limb); printf ("Error , error this data is for %d GMP_LIMB_BITS only\n", limb); printf ("#endif\n"); #endif printf ("/* This table is 0!,1!,2!,3!,...,n! where n! has <= GMP_NUMB_BITS bits */\n"); printf ("#define ONE_LIMB_FACTORIAL_TABLE CNST_LIMB(0x1),CNST_LIMB(0x1"); mpz_init_set_ui (x, 1); mpz_init (last); for (b = 2;; b++) { mpz_mul_ui (x, x, b); /* so b!=a */ if (mpz_sizeinbase (x, 2) > numb) break; printf ("),CNST_LIMB(0x"); mpz_out_str (stdout, 16, x); } printf (")\n"); printf ("\n/* This table is 0!,1!,2!/2,3!/2,...,n!/2^sn where n!/2^sn is an */\n"); printf ("/* odd integer for each n, and n!/2^sn has <= GMP_NUMB_BITS bits */\n"); printf ("#define ONE_LIMB_ODD_FACTORIAL_TABLE CNST_LIMB(0x1),CNST_LIMB(0x1),CNST_LIMB(0x1"); mpz_set_ui (x, 1); for (b = 3;; b++) { for (a = b; (a & 1) == 0; a >>= 1); mpz_swap (last, x); mpz_mul_ui (x, last, a); if (mpz_sizeinbase (x, 2) > numb) break; printf ("),CNST_LIMB(0x"); mpz_out_str (stdout, 16, x); } printf (")\n"); printf ("#define ODD_FACTORIAL_TABLE_MAX CNST_LIMB(0x"); mpz_out_str (stdout, 16, last); printf (")\n"); ofl = b - 1; printf ("#define ODD_FACTORIAL_TABLE_LIMIT (%lu)\n", ofl); mpz_init2 (mask, numb + 1); mpz_setbit (mask, numb); mpz_sub_ui (mask, mask, 1); printf ("\n/* Previous table, continued, values modulo 2^GMP_NUMB_BITS */\n"); printf ("#define ONE_LIMB_ODD_FACTORIAL_EXTTABLE CNST_LIMB(0x"); mpz_and (x, x, mask); mpz_out_str (stdout, 16, x); mpz_init (y); mpz_bin_uiui (y, b, b/2); b++; for (;; b++) { for (a = b; (a & 1) == 0; a >>= 1); if (a == b) { mpz_divexact_ui (y, y, a/2+1); mpz_mul_ui (y, y, a); } else mpz_mul_2exp (y, y, 1); if (mpz_sizeinbase (y, 2) > numb) break; mpz_mul_ui (x, x, a); mpz_and (x, x, mask); printf ("),CNST_LIMB(0x"); mpz_out_str (stdout, 16, x); } printf (")\n"); ofe = b - 1; printf ("#define ODD_FACTORIAL_EXTTABLE_LIMIT (%lu)\n", ofe); printf ("\n/* This table is 1!!,3!!,...,(2n+1)!! where (2n+1)!! has <= GMP_NUMB_BITS bits */\n"); printf ("#define ONE_LIMB_ODD_DOUBLEFACTORIAL_TABLE CNST_LIMB(0x1"); mpz_set_ui (x, 1); for (b = 3;; b+=2) { mpz_swap (last, x); mpz_mul_ui (x, last, b); if (mpz_sizeinbase (x, 2) > numb) break; printf ("),CNST_LIMB(0x"); mpz_out_str (stdout, 16, x); } printf (")\n"); printf ("#define ODD_DOUBLEFACTORIAL_TABLE_MAX CNST_LIMB(0x"); mpz_out_str (stdout, 16, last); printf (")\n"); printf ("#define ODD_DOUBLEFACTORIAL_TABLE_LIMIT (%lu)\n", b - 2); printf ("\n/* This table x_1, x_2,... contains values s.t. x_n^n has <= GMP_NUMB_BITS bits */\n"); printf ("#define NTH_ROOT_NUMB_MASK_TABLE (GMP_NUMB_MASK"); for (b = 2;b <= 8; b++) { mpz_root (x, mask, b); printf ("),CNST_LIMB(0x"); mpz_out_str (stdout, 16, x); } printf (")\n"); mpz_add_ui (mask, mask, 1); printf ("\n/* This table contains inverses of odd factorials, modulo 2^GMP_NUMB_BITS */\n"); printf ("\n/* It begins with (2!/2)^-1=1 */\n"); printf ("#define ONE_LIMB_ODD_FACTORIAL_INVERSES_TABLE CNST_LIMB(0x1"); mpz_set_ui (x, 1); for (b = 3;b <= ofe - 2; b++) { for (a = b; (a & 1) == 0; a >>= 1); mpz_mul_ui (x, x, a); mpz_invert (y, x, mask); printf ("),CNST_LIMB(0x"); mpz_out_str (stdout, 16, y); } printf (")\n"); ofe = (ofe / 16 + 1) * 16; printf ("\n/* This table contains 2n-popc(2n) for small n */\n"); printf ("\n/* It begins with 2-1=1 (n=1) */\n"); printf ("#define TABLE_2N_MINUS_POPC_2N 1"); for (b = 4; b <= ofe; b += 2) { mpz_set_ui (x, b); printf (",%lu",b - mpz_popcount (x)); } printf ("\n"); printf ("#define TABLE_LIMIT_2N_MINUS_POPC_2N %lu\n", ofe + 1); ofl = (ofl + 1) / 2; printf ("#define ODD_CENTRAL_BINOMIAL_OFFSET (%lu)\n", ofl); printf ("\n/* This table contains binomial(2k,k)/2^t */\n"); printf ("\n/* It begins with ODD_CENTRAL_BINOMIAL_TABLE_MIN */\n"); printf ("#define ONE_LIMB_ODD_CENTRAL_BINOMIAL_TABLE "); for (b = ofl;; b++) { mpz_bin_uiui (x, 2 * b, b); mpz_remove_twos (x); if (mpz_sizeinbase (x, 2) > numb) break; if (b != ofl) printf ("),"); printf("CNST_LIMB(0x"); mpz_out_str (stdout, 16, x); } printf (")\n"); ofe = b - 1; printf ("#define ODD_CENTRAL_BINOMIAL_TABLE_LIMIT (%lu)\n", ofe); printf ("\n/* This table contains the inverses of elements in the previous table. */\n"); printf ("#define ONE_LIMB_ODD_CENTRAL_BINOMIAL_INVERSE_TABLE CNST_LIMB(0x"); for (b = ofl; b <= ofe; b++) { mpz_bin_uiui (x, 2 * b, b); mpz_remove_twos (x); mpz_invert (x, x, mask); mpz_out_str (stdout, 16, x); if (b != ofe) printf ("),CNST_LIMB(0x"); } printf (")\n"); printf ("\n/* This table contains the values t in the formula binomial(2k,k)/2^t */\n"); printf ("#define CENTRAL_BINOMIAL_2FAC_TABLE "); for (b = ofl; b <= ofe; b++) { mpz_bin_uiui (x, 2 * b, b); printf ("%d", mpz_remove_twos (x)); if (b != ofe) printf (","); } printf ("\n"); return 0; }
/* Evaluate the expression E and put the result in R. */ void mpz_eval_expr (mpz_ptr r, expr_t e) { mpz_t lhs, rhs; switch (e->op) { case LIT: mpz_set (r, e->operands.val); return; case PLUS: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_add (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; case MINUS: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_sub (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; case MULT: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_mul (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; case DIV: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_fdiv_q (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; case MOD: mpz_init (rhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_abs (rhs, rhs); mpz_eval_mod_expr (r, e->operands.ops.lhs, rhs); mpz_clear (rhs); return; case REM: /* Check if lhs operand is POW expression and optimize for that case. */ if (e->operands.ops.lhs->op == POW) { mpz_t powlhs, powrhs; mpz_init (powlhs); mpz_init (powrhs); mpz_init (rhs); mpz_eval_expr (powlhs, e->operands.ops.lhs->operands.ops.lhs); mpz_eval_expr (powrhs, e->operands.ops.lhs->operands.ops.rhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_powm (r, powlhs, powrhs, rhs); if (mpz_cmp_si (rhs, 0L) < 0) mpz_neg (r, r); mpz_clear (powlhs); mpz_clear (powrhs); mpz_clear (rhs); return; } mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_fdiv_r (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; #if __GNU_MP_VERSION >= 2 case INVMOD: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_invert (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; #endif case POW: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); if (mpz_cmpabs_ui (lhs, 1) <= 0) { /* For 0^rhs and 1^rhs, we just need to verify that rhs is well-defined. For (-1)^rhs we need to determine (rhs mod 2). For simplicity, compute (rhs mod 2) for all three cases. */ expr_t two, et; two = malloc (sizeof (struct expr)); two -> op = LIT; mpz_init_set_ui (two->operands.val, 2L); makeexp (&et, MOD, e->operands.ops.rhs, two); e->operands.ops.rhs = et; } mpz_eval_expr (rhs, e->operands.ops.rhs); if (mpz_cmp_si (rhs, 0L) == 0) /* x^0 is 1 */ mpz_set_ui (r, 1L); else if (mpz_cmp_si (lhs, 0L) == 0) /* 0^y (where y != 0) is 0 */ mpz_set_ui (r, 0L); else if (mpz_cmp_ui (lhs, 1L) == 0) /* 1^y is 1 */ mpz_set_ui (r, 1L); else if (mpz_cmp_si (lhs, -1L) == 0) /* (-1)^y just depends on whether y is even or odd */ mpz_set_si (r, (mpz_get_ui (rhs) & 1) ? -1L : 1L); else if (mpz_cmp_si (rhs, 0L) < 0) /* x^(-n) is 0 */ mpz_set_ui (r, 0L); else { unsigned long int cnt; unsigned long int y; /* error if exponent does not fit into an unsigned long int. */ if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0) goto pow_err; y = mpz_get_ui (rhs); /* x^y == (x/(2^c))^y * 2^(c*y) */ #if __GNU_MP_VERSION >= 2 cnt = mpz_scan1 (lhs, 0); #else cnt = 0; #endif if (cnt != 0) { if (y * cnt / cnt != y) goto pow_err; mpz_tdiv_q_2exp (lhs, lhs, cnt); mpz_pow_ui (r, lhs, y); mpz_mul_2exp (r, r, y * cnt); } else mpz_pow_ui (r, lhs, y); } mpz_clear (lhs); mpz_clear (rhs); return; pow_err: error = "result of `pow' operator too large"; mpz_clear (lhs); mpz_clear (rhs); longjmp (errjmpbuf, 1); case GCD: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_gcd (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; #if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 case LCM: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_lcm (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; #endif case AND: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_and (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; case IOR: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_ior (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; #if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 case XOR: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); mpz_xor (r, lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); return; #endif case NEG: mpz_eval_expr (r, e->operands.ops.lhs); mpz_neg (r, r); return; case NOT: mpz_eval_expr (r, e->operands.ops.lhs); mpz_com (r, r); return; case SQRT: mpz_init (lhs); mpz_eval_expr (lhs, e->operands.ops.lhs); if (mpz_sgn (lhs) < 0) { error = "cannot take square root of negative numbers"; mpz_clear (lhs); longjmp (errjmpbuf, 1); } mpz_sqrt (r, lhs); return; #if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 case ROOT: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); if (mpz_sgn (rhs) <= 0) { error = "cannot take non-positive root orders"; mpz_clear (lhs); mpz_clear (rhs); longjmp (errjmpbuf, 1); } if (mpz_sgn (lhs) < 0 && (mpz_get_ui (rhs) & 1) == 0) { error = "cannot take even root orders of negative numbers"; mpz_clear (lhs); mpz_clear (rhs); longjmp (errjmpbuf, 1); } { unsigned long int nth = mpz_get_ui (rhs); if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0) { /* If we are asked to take an awfully large root order, cheat and ask for the largest order we can pass to mpz_root. This saves some error prone special cases. */ nth = ~(unsigned long int) 0; } mpz_root (r, lhs, nth); } mpz_clear (lhs); mpz_clear (rhs); return; #endif case FAC: mpz_eval_expr (r, e->operands.ops.lhs); if (mpz_size (r) > 1) { error = "result of `!' operator too large"; longjmp (errjmpbuf, 1); } mpz_fac_ui (r, mpz_get_ui (r)); return; #if __GNU_MP_VERSION >= 2 case POPCNT: mpz_eval_expr (r, e->operands.ops.lhs); { long int cnt; cnt = mpz_popcount (r); mpz_set_si (r, cnt); } return; case HAMDIST: { long int cnt; mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); cnt = mpz_hamdist (lhs, rhs); mpz_clear (lhs); mpz_clear (rhs); mpz_set_si (r, cnt); } return; #endif case LOG2: mpz_eval_expr (r, e->operands.ops.lhs); { unsigned long int cnt; if (mpz_sgn (r) <= 0) { error = "logarithm of non-positive number"; longjmp (errjmpbuf, 1); } cnt = mpz_sizeinbase (r, 2); mpz_set_ui (r, cnt - 1); } return; case LOG: { unsigned long int cnt; mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); if (mpz_sgn (lhs) <= 0) { error = "logarithm of non-positive number"; mpz_clear (lhs); mpz_clear (rhs); longjmp (errjmpbuf, 1); } if (mpz_cmp_ui (rhs, 256) >= 0) { error = "logarithm base too large"; mpz_clear (lhs); mpz_clear (rhs); longjmp (errjmpbuf, 1); } cnt = mpz_sizeinbase (lhs, mpz_get_ui (rhs)); mpz_set_ui (r, cnt - 1); mpz_clear (lhs); mpz_clear (rhs); } return; case FERMAT: { unsigned long int t; mpz_init (lhs); mpz_eval_expr (lhs, e->operands.ops.lhs); t = (unsigned long int) 1 << mpz_get_ui (lhs); if (mpz_cmp_ui (lhs, ~(unsigned long int) 0) > 0 || t == 0) { error = "too large Mersenne number index"; mpz_clear (lhs); longjmp (errjmpbuf, 1); } mpz_set_ui (r, 1); mpz_mul_2exp (r, r, t); mpz_add_ui (r, r, 1); mpz_clear (lhs); } return; case MERSENNE: mpz_init (lhs); mpz_eval_expr (lhs, e->operands.ops.lhs); if (mpz_cmp_ui (lhs, ~(unsigned long int) 0) > 0) { error = "too large Mersenne number index"; mpz_clear (lhs); longjmp (errjmpbuf, 1); } mpz_set_ui (r, 1); mpz_mul_2exp (r, r, mpz_get_ui (lhs)); mpz_sub_ui (r, r, 1); mpz_clear (lhs); return; case FIBONACCI: { mpz_t t; unsigned long int n, i; mpz_init (lhs); mpz_eval_expr (lhs, e->operands.ops.lhs); if (mpz_sgn (lhs) <= 0 || mpz_cmp_si (lhs, 1000000000) > 0) { error = "Fibonacci index out of range"; mpz_clear (lhs); longjmp (errjmpbuf, 1); } n = mpz_get_ui (lhs); mpz_clear (lhs); #if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 mpz_fib_ui (r, n); #else mpz_init_set_ui (t, 1); mpz_set_ui (r, 1); if (n <= 2) mpz_set_ui (r, 1); else { for (i = 3; i <= n; i++) { mpz_add (t, t, r); mpz_swap (t, r); } } mpz_clear (t); #endif } return; case RANDOM: { unsigned long int n; mpz_init (lhs); mpz_eval_expr (lhs, e->operands.ops.lhs); if (mpz_sgn (lhs) <= 0 || mpz_cmp_si (lhs, 1000000000) > 0) { error = "random number size out of range"; mpz_clear (lhs); longjmp (errjmpbuf, 1); } n = mpz_get_ui (lhs); mpz_clear (lhs); mpz_urandomb (r, rstate, n); } return; case NEXTPRIME: { mpz_eval_expr (r, e->operands.ops.lhs); mpz_nextprime (r, r); } return; case BINOM: mpz_init (lhs); mpz_init (rhs); mpz_eval_expr (lhs, e->operands.ops.lhs); mpz_eval_expr (rhs, e->operands.ops.rhs); { unsigned long int k; if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0) { error = "k too large in (n over k) expression"; mpz_clear (lhs); mpz_clear (rhs); longjmp (errjmpbuf, 1); } k = mpz_get_ui (rhs); mpz_bin_ui (r, lhs, k); } mpz_clear (lhs); mpz_clear (rhs); return; case TIMING: { int t0; t0 = cputime (); mpz_eval_expr (r, e->operands.ops.lhs); printf ("time: %d\n", cputime () - t0); } return; default: abort (); } }
void check_random (int argc, char *argv[]) { mpz_t x, s0, s1, s2, s3, m; mp_size_t xsize; int i; int reps = 100000; int bit0, bit1, bit2, bit3; unsigned long int bitindex; const char *s = ""; if (argc == 2) reps = atoi (argv[1]); mpz_init (x); mpz_init (s0); mpz_init (s1); mpz_init (s2); mpz_init (s3); mpz_init (m); for (i = 0; i < reps; i++) { xsize = urandom () % (2 * SIZE) - SIZE; mpz_random2 (x, xsize); bitindex = urandom () % SIZE; mpz_set (s0, x); bit0 = mpz_tstbit (x, bitindex); mpz_setbit (x, bitindex); MPZ_CHECK_FORMAT (x); mpz_set (s1, x); bit1 = mpz_tstbit (x, bitindex); mpz_clrbit (x, bitindex); MPZ_CHECK_FORMAT (x); mpz_set (s2, x); bit2 = mpz_tstbit (x, bitindex); mpz_setbit (x, bitindex); MPZ_CHECK_FORMAT (x); mpz_set (s3, x); bit3 = mpz_tstbit (x, bitindex); #define FAIL(str) do { s = str; goto fail; } while (0) if (bit1 != 1) FAIL ("bit1 != 1"); if (bit2 != 0) FAIL ("bit2 != 0"); if (bit3 != 1) FAIL ("bit3 != 1"); if (bit0 == 0) { if (mpz_cmp (s0, s1) == 0 || mpz_cmp (s0, s2) != 0 || mpz_cmp (s0, s3) == 0) abort (); } else { if (mpz_cmp (s0, s1) != 0 || mpz_cmp (s0, s2) == 0 || mpz_cmp (s0, s3) != 0) abort (); } if (mpz_cmp (s1, s2) == 0 || mpz_cmp (s1, s3) != 0) abort (); if (mpz_cmp (s2, s3) == 0) abort (); mpz_ui_pow_ui (m, 2L, bitindex); MPZ_CHECK_FORMAT (m); mpz_ior (x, s2, m); MPZ_CHECK_FORMAT (x); if (mpz_cmp (x, s3) != 0) abort (); mpz_com (m, m); MPZ_CHECK_FORMAT (m); mpz_and (x, s1, m); MPZ_CHECK_FORMAT (x); if (mpz_cmp (x, s2) != 0) abort (); } mpz_clear (x); mpz_clear (s0); mpz_clear (s1); mpz_clear (s2); mpz_clear (s3); mpz_clear (m); return; fail: printf ("%s\n", s); printf ("bitindex = %lu\n", bitindex); printf ("x = "); mpz_out_str (stdout, -16, x); printf (" hex\n"); exit (1); }
void ConstFoldBinop(BinopKind kind, const mpz_t left_val, const mpz_t right_val, mpz_t res) { switch (kind) { case B_Plus: mpz_add(res, left_val, right_val); break; case B_Minus: case B_MinusPP: // TODO: include scaling here? mpz_sub(res, left_val, right_val); break; case B_Mult: mpz_mul(res, left_val, right_val); break; case B_Div: case B_DivExact: // treat like regular division during folding. // leave zero value for division by zero. if (mpz_cmp_si(right_val, 0) != 0) mpz_fdiv_q(res, left_val, right_val); break; case B_Mod: // ditto for modulus by zero or negative values. if (mpz_cmp_si(right_val, 0) > 0) mpz_fdiv_r(res, left_val, right_val); break; case B_ShiftLeft: // ditto for left shifts by negative or large values. if (mpz_cmp_si(right_val, 0) >= 0 && mpz_cmp_si(right_val, 64) <= 0) mpz_mul_2exp(res, left_val, mpz_get_ui(right_val)); break; case B_ShiftRight: // ditto for right shifts by negative values or large values. if (mpz_cmp_si(right_val, 0) >= 0 && mpz_cmp_si(right_val, 64) <= 0) mpz_tdiv_q_2exp(res, left_val, mpz_get_ui(right_val)); break; case B_BitwiseAnd: mpz_and(res, left_val, right_val); break; case B_BitwiseOr: mpz_ior(res, left_val, right_val); break; case B_BitwiseXOr: mpz_xor(res, left_val, right_val); break; case B_Min: if (mpz_cmp(left_val, right_val) < 0) mpz_set(res, left_val); else mpz_set(res, right_val); break; case B_Max: if (mpz_cmp(left_val, right_val) > 0) mpz_set(res, left_val); else mpz_set(res, right_val); break; case B_LessThan: case B_LessThanP: FoldBoolean(res, mpz_cmp(left_val, right_val) < 0); break; case B_GreaterThan: case B_GreaterThanP: FoldBoolean(res, mpz_cmp(left_val, right_val) > 0); break; case B_LessEqual: case B_LessEqualP: FoldBoolean(res, mpz_cmp(left_val, right_val) <= 0); break; case B_GreaterEqual: case B_GreaterEqualP: FoldBoolean(res, mpz_cmp(left_val, right_val) >= 0); break; case B_Equal: case B_EqualP: FoldBoolean(res, mpz_cmp(left_val, right_val) == 0); break; case B_NotEqual: case B_NotEqualP: FoldBoolean(res, mpz_cmp(left_val, right_val) != 0); break; case B_LogicalAnd: FoldBoolean(res, mpz_cmp_si(left_val, 0) != 0 && mpz_cmp_si(right_val, 0) != 0); break; case B_LogicalOr: FoldBoolean(res, mpz_cmp_si(left_val, 0) != 0 || mpz_cmp_si(right_val, 0) != 0); break; default: logout << "ERROR: ConstFoldBinop: Unexpected " << BinopString(kind) << " " << left_val << " " << right_val << endl; Assert(false); } }
static void mpz_andc1_op(cl_object out, cl_object i, cl_object j) { mpz_com(out->big.big_num, i->big.big_num); mpz_and(out->big.big_num, out->big.big_num, j->big.big_num); }
void C_BigInt::operator &= (const C_BigInt inOperand) { mpz_and (mGMPint, mGMPint, inOperand.mGMPint) ; }
num_t num_int_part_sel(pseltype_t op_type, num_t hi, num_t lo, num_t a) { num_t r, m; unsigned long mask_shl, lo_ui; r = num_new_z(N_TEMP, NULL); m = num_new_z(N_TEMP, NULL); a = num_new_z(N_TEMP, a); hi = num_new_z(N_TEMP, hi); if (lo != NULL) lo = num_new_z(N_TEMP, lo); switch (op_type) { case PSEL_SINGLE: lo = num_new_z(N_TEMP, hi); break; case PSEL_FRANGE: break; case PSEL_DRANGE: if (mpz_cmp_si(Z(lo), 0L) < 0) { yyxerror("low index of part select operation must be " "positive"); return NULL; } mpz_sub_ui(Z(lo), Z(lo), 1UL); mpz_sub(Z(lo), Z(hi), Z(lo)); break; } if (mpz_cmp_si(Z(hi), 0L) < 0) { yyxerror("high index of part select operation must be " "positive"); return NULL; } if (mpz_cmp_si(Z(lo), 0L) < 0) { yyxerror("low index of part select operation must be " "positive"); return NULL; } if (!mpz_fits_ulong_p(Z(hi))) { yyxerror("high index of part select operation needs to fit " "into an unsigned long C datatype"); return NULL; } if (!mpz_fits_ulong_p(Z(lo))) { yyxerror("low index of part select operation needs to fit " "into an unsigned long C datatype"); return NULL; } lo_ui = mpz_get_ui(Z(lo)); mask_shl = 1 + (mpz_get_ui(Z(hi)) - lo_ui); /* * We do the part sel by: * (1) shifting right by 'lo' * (2) anding with ((1 << mask_shl)-1) */ mpz_div_2exp(Z(r), Z(a), lo_ui); mpz_set_ui(Z(m), 1UL); mpz_mul_2exp(Z(m), Z(m), mask_shl); mpz_sub_ui(Z(m), Z(m), 1UL); mpz_and(Z(r), Z(r), Z(m)); return r; }