Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polynomial evaluation related functions added #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions bitarray/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

Author: Ilan Schnell
"""
from __future__ import absolute_import

from bitarray._bitarray import (bitarray, decodetree, _sysinfo,
get_default_endian, _set_default_endian,
from bitarray._bitarray import (bitarray, decodetree, _sysinfo, tbase,
get_default_endian, _set_default_endian, eval_all_terms,
__version__)


Expand Down
51 changes: 45 additions & 6 deletions bitarray/_bitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Python.h"
#include "pythoncapi_compat.h"
#include "bitarray.h"
#include "bitarray_vct.h"

/* block size used when reading / writing blocks of bytes from files */
#define BLOCKSIZE 65536
Expand Down Expand Up @@ -325,11 +326,17 @@ count(bitarrayobject *self, int vi, Py_ssize_t start, Py_ssize_t stop)
if (stop >= start + 8) {
const Py_ssize_t byte_start = BYTES(start);
const Py_ssize_t byte_stop = stop / 8;
const uint64_t * data64 = (const uint64_t *) (self->ob_item + byte_start);
uint64_t tmp = 0;
Py_ssize_t j;

for (i = start; i < BITS(byte_start); i++)
res += GETBIT(self, i);
for (j = byte_start; j < byte_stop; j++)
for (j = byte_start, i = 0; j + 8 < byte_stop; j += 8, ++i) {
tmp = data64[i];
BITWISE_HW_WP3(tmp, res);
}
for (; j < byte_stop; j++)
res += bitcount_lookup[(unsigned char) self->ob_item[j]];
for (i = BITS(byte_stop); i < stop; i++)
res += GETBIT(self, i);
Expand Down Expand Up @@ -1859,9 +1866,8 @@ enum op_type {
static int
bitwise(bitarrayobject *self, PyObject *arg, enum op_type oper)
{
const Py_ssize_t nbytes = Py_SIZE(self);
bitarrayobject *other;
Py_ssize_t i;
Py_ssize_t i = 0;

if (!bitarray_Check(arg)) {
PyErr_SetString(PyExc_TypeError,
Expand All @@ -1876,23 +1882,37 @@ bitwise(bitarrayobject *self, PyObject *arg, enum op_type oper)
}
setunused(self);
setunused(other);
Py_ssize_t size = Py_SIZE(self);

#if HAS_VECTORS
#define BITWISE_VECTOR_OP(OP) \
for (; (Py_ssize_t)(i + sizeof(vec)) < size; i += sizeof(vec)) \
vector_op(self->ob_item + i, other->ob_item + i, OP)
#else
#define BITWISE_VECTOR_OP(OP)
#endif

switch (oper) {
case OP_and:
for (i = 0; i < nbytes; i++)
BITWISE_VECTOR_OP(&)
for (; i < size; ++i)
self->ob_item[i] &= other->ob_item[i];
break;
case OP_or:
for (i = 0; i < nbytes; i++)
BITWISE_VECTOR_OP(|)
for (; i < size; ++i)
self->ob_item[i] |= other->ob_item[i];
break;
case OP_xor:
for (i = 0; i < nbytes; i++)
BITWISE_VECTOR_OP(^)
for (; i < size; ++i)
self->ob_item[i] ^= other->ob_item[i];
break;
default: /* cannot happen */
return -1;
}
return 0;
#undef VECTOR_BITWISE_OP
}

#define BITWISE_FUNC(oper) \
Expand Down Expand Up @@ -1930,6 +1950,7 @@ BITWISE_IFUNC(and) /* bitarray_iand */
BITWISE_IFUNC(or) /* bitarray_ior */
BITWISE_IFUNC(xor) /* bitarray_ixor */

#include "_bitarray_vct.c"

static PyNumberMethods bitarray_as_number = {
0, /* nb_add */
Expand Down Expand Up @@ -2756,6 +2777,18 @@ static PyMethodDef bitarray_methods[] = {
METH_KEYWORDS,
unpack_doc},

{"eval_monic", (PyCFunction) bitarray_eval_monic, METH_VARARGS |
METH_KEYWORDS,
eval_monic_doc},
{"fast_copy", (PyCFunction) bitarray_fast_copy, METH_O,
fast_copy_doc},
{"fast_hw_and", (PyCFunction) bitwise_fast_hw_and, METH_O,
bitwise_fast_hw_and_doc},
{"fast_hw_or", (PyCFunction) bitwise_fast_hw_or, METH_O,
bitwise_fast_hw_or_doc},
{"fast_hw_xor", (PyCFunction) bitwise_fast_hw_xor, METH_O,
bitwise_fast_hw_xor_doc},

/* special methods */
{"__copy__", (PyCFunction) bitarray_copy, METH_NOARGS,
copy_doc},
Expand Down Expand Up @@ -3291,6 +3324,7 @@ static PyMethodDef module_functions[] = {
{"_set_default_endian", (PyCFunction) set_default_endian, METH_VARARGS,
set_default_endian_doc},
{"_sysinfo", (PyCFunction) sysinfo, METH_NOARGS, sysinfo_doc },
{"eval_all_terms", (PyCFunction) eval_all_terms, METH_VARARGS | METH_KEYWORDS, eval_all_terms_doc },
{NULL, NULL} /* sentinel */
};

Expand Down Expand Up @@ -3325,6 +3359,11 @@ init_bitarray(void)
Py_INCREF((PyObject *) &Bitarray_Type);
PyModule_AddObject(m, "bitarray", (PyObject *) &Bitarray_Type);

if (PyType_Ready(&TBase_Type) < 0)
goto error;
Py_INCREF((PyObject *) &TBase_Type);
PyModule_AddObject(m, "tbase", (PyObject *) &TBase_Type);

if (PyType_Ready(&DecodeTree_Type) < 0)
goto error;
Py_SET_TYPE(&DecodeTree_Type, &PyType_Type);
Expand Down
Loading