static Variant HHVM_FUNCTION(gmp_scan0, const Variant& data, int64_t start) { if (start < 0) { raise_warning(cs_GMP_INVALID_STARTING_INDEX_IS_NEGATIVE, cs_GMP_FUNC_NAME_GMP_SCAN0); return false; } mpz_t gmpData; if (!variantToGMPData(cs_GMP_FUNC_NAME_GMP_SCAN0, gmpData, data)) { return false; } int64_t foundBit = mpz_scan0(gmpData, start); mpz_clear(gmpData); return foundBit; }
static PyObject * GMPy_MPZ_bit_scan0_method(PyObject *self, PyObject *args) { mp_bitcnt_t index, starting_bit = 0; if (PyTuple_GET_SIZE(args) == 1) { starting_bit = mp_bitcnt_t_From_Integer(PyTuple_GET_ITEM(args, 0)); if (starting_bit == (mp_bitcnt_t)(-1) && PyErr_Occurred()) { return NULL; } } index = mpz_scan0(MPZ(self), starting_bit); if (index == (mp_bitcnt_t)(-1)) { Py_RETURN_NONE; } else { return PyIntOrLong_FromMpBitCnt(index); } }
static PyObject * GMPy_MPZ_bit_scan0_function(PyObject *self, PyObject *args) { mp_bitcnt_t index, starting_bit = 0; MPZ_Object *tempx = NULL; if (PyTuple_GET_SIZE(args) == 0 || PyTuple_GET_SIZE(args) > 2) { goto err; } if (!(tempx = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL))) { goto err; } if (PyTuple_GET_SIZE(args) == 2) { starting_bit = mp_bitcnt_t_From_Integer(PyTuple_GET_ITEM(args, 1)); if (starting_bit == (mp_bitcnt_t)(-1) && PyErr_Occurred()) { goto err_index; } } index = mpz_scan0(tempx->z, starting_bit); Py_DECREF((PyObject*)tempx); if (index == (mp_bitcnt_t)(-1)) { Py_RETURN_NONE; } else { return PyIntOrLong_FromMpBitCnt(index); } err: TYPE_ERROR("bit_scan0() requires 'mpz',['int'] arguments"); err_index: Py_DECREF((PyObject*)tempx); return NULL; }
void hex_random_scan_op (enum hex_random_op op, unsigned long maxbits, char **ap, unsigned long *b, unsigned long *r) { mpz_t a; unsigned long abits, bbits; unsigned signs; mpz_init (a); abits = gmp_urandomb_ui (state, 32) % maxbits; bbits = gmp_urandomb_ui (state, 32) % (maxbits + 100); mpz_rrandomb (a, state, abits); signs = gmp_urandomb_ui (state, 1); if (signs & 1) mpz_neg (a, a); switch (op) { default: abort (); case OP_SCAN0: *r = mpz_scan0 (a, bbits); break; case OP_SCAN1: *r = mpz_scan1 (a, bbits); break; } gmp_asprintf (ap, "%Zx", a); *b = bbits; mpz_clear (a); }
static void e_mpz_scan0 (mpz_ptr w, mpz_srcptr x, unsigned long start) { mpz_set_ui (w, mpz_scan0 (x, start)); }
void check_ref (void) { static const int offset[] = { -2, -1, 0, 1, 2, 3 }; mpz_t z; int test, neg, sought, oindex, o; mp_size_t size, isize; unsigned long start, got, want; mpz_init (z); for (test = 0; test < 5; test++) { for (size = 0; size < 5; size++) { mpz_random2 (z, size); for (neg = 0; neg <= 1; neg++) { if (neg) mpz_neg (z, z); for (isize = 0; isize <= size; isize++) { for (oindex = 0; oindex < numberof (offset); oindex++) { o = offset[oindex]; if ((int) isize*GMP_NUMB_BITS < -o) continue; /* start would be negative */ start = isize*GMP_NUMB_BITS + o; for (sought = 0; sought <= 1; sought++) { if (sought == 0) { got = mpz_scan0 (z, start); want = refmpz_scan0 (z, start); } else { got = mpz_scan1 (z, start); want = refmpz_scan1 (z, start); } if (got != want) { printf ("wrong at test=%d, size=%ld, neg=%d, start=%lu, sought=%d\n", test, size, neg, start, sought); printf (" z 0x"); mpz_out_str (stdout, -16, z); printf ("\n"); printf (" got=%lu, want=%lu\n", got, want); exit (1); } } } } } } } mpz_clear (z); }