Skip to content
Merged
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
8 changes: 4 additions & 4 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-24.04
strategy:
matrix:
python_minor: [ "10" , "11" , "12" , "13" ]
python_minor: [ "10" , "11" , "12" , "13" , "14" ]

steps:
- uses: actions/checkout@v4.2.2
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:
strategy:
fail-fast: true
matrix:
cibw_python: [ "cp310", "cp311", "cp312", "cp313" ]
cibw_python: [ "cp310", "cp311", "cp312", "cp313", "cp314" ]
cibw_arch: [ "x86_64", "arm64" ]

steps:
Expand Down Expand Up @@ -131,7 +131,7 @@ jobs:
matrix:
cibw_buildlinux: [ manylinux, musllinux ]
cibw_arch: [ "x86_64", "aarch64" ]
cibw_python: [ "cp310", "cp311", "cp312", "cp313" ]
cibw_python: [ "cp310", "cp311", "cp312", "cp313", "cp314" ]

steps:
- uses: actions/checkout@v4.2.2
Expand Down Expand Up @@ -183,7 +183,7 @@ jobs:
- name: 🛠 Build Hexhamming Python C extension
run: cibuildwheel
env:
CIBW_BUILD: "cp310-* cp311-* cp312-* cp313-*"
CIBW_BUILD: "cp310-* cp311-* cp312-* cp313-* cp314-*"
CIBW_ARCHS_WINDOWS: "AMD64"
CIBW_TEST_REQUIRES: pytest pytest-benchmark
CIBW_TEST_COMMAND: "pytest -s {project}/test"
Expand Down
33 changes: 17 additions & 16 deletions src/python_hexhamming.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstdlib>
#include <cstring>
#include <string.h>
#define PY_SSIZE_T_CLEAN
Expand Down Expand Up @@ -334,7 +335,7 @@ static PyObject * check_bytes_arrays_first_within_dist_wrapper(PyObject *self, P

Py_END_ALLOW_THREADS

return Py_BuildValue("i", ret);
return Py_BuildValue("L", ret);
}

/**
Expand Down Expand Up @@ -376,7 +377,7 @@ static PyObject * check_bytes_arrays_best_within_dist_wrapper(PyObject *self, Py
return NULL;
}

int64_t best_dist = max_dist + 1;
int64_t best_dist = -1;
int64_t best_index = -1;

Py_BEGIN_ALLOW_THREADS
Expand All @@ -385,23 +386,19 @@ static PyObject * check_bytes_arrays_best_within_dist_wrapper(PyObject *self, Py
uint64_t number_of_elements = big_array_size / small_array_size;
uint8_t* pBig = big_array;
for (uint64_t i = 0; i < number_of_elements; i++, pBig += small_array_size) {
if (ptr__hamming_distance_bytes(pBig, small_array, small_array_size, best_dist - 1) == 0)
int64_t threshold = (best_dist >= 0) ? best_dist - 1 : max_dist;
if (ptr__hamming_distance_bytes(pBig, small_array, small_array_size, threshold) == 0)
continue;
dist = ptr__hamming_distance_bytes(pBig, small_array, small_array_size, -1);
if (dist < best_dist) {
if (best_dist < 0 || (int64_t)dist < best_dist) {
best_dist = dist;
best_index = i;
}
}

// Anything found? If not, set dist to -1
if (best_index == -1) {
best_dist = -1;
}

Py_END_ALLOW_THREADS

return Py_BuildValue("ii", best_dist, best_index);
return Py_BuildValue("LL", best_dist, best_index);
}

/**
Expand Down Expand Up @@ -444,7 +441,11 @@ static PyObject * check_bytes_arrays_all_within_dist_wrapper(PyObject *self, PyO
}

uint64_t number_of_elements = big_array_size / small_array_size;
uint64_t *out = new uint64_t[number_of_elements * 2];
uint64_t *out = (uint64_t *)malloc(number_of_elements * 2 * sizeof(uint64_t));
if (out == NULL) {
PyErr_NoMemory();
return NULL;
}
uint64_t *o = out;

Py_BEGIN_ALLOW_THREADS
Expand All @@ -465,25 +466,25 @@ static PyObject * check_bytes_arrays_all_within_dist_wrapper(PyObject *self, PyO
PyObject *my_list = PyList_New(0);
if (my_list == NULL)
{
delete[] out;
free(out);
PyErr_NoMemory();
return NULL;
}

for (uint64_t *op = out; op < o; op += 2)
{
PyObject *tup = Py_BuildValue("ii", op[0], op[1]);
PyObject *tup = Py_BuildValue("KK", op[0], op[1]);
if (tup == NULL)
{
delete[] out;
free(out);
Py_DECREF(my_list);
PyErr_NoMemory();
return NULL;
}

if (PyList_Append(my_list, tup) == -1)
{
delete[] out;
free(out);
Py_DECREF(tup);
Py_DECREF(my_list);
PyErr_NoMemory();
Expand All @@ -492,7 +493,7 @@ static PyObject * check_bytes_arrays_all_within_dist_wrapper(PyObject *self, PyO
Py_DECREF(tup);
}

delete[] out;
free(out);

return my_list;
}
Expand Down
2 changes: 1 addition & 1 deletion src/python_hexhamming.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ inline uint64_t hamming_distance_loop_string(const char* a, const char* b, const
}
static uint64_t hamming_distance_bytes__extra(const uint8_t* a, const uint8_t* b,
const uint64_t length, const int64_t max_dist) {
if (max_dist > 0)
if (max_dist >= 0)
return hamming_distance_bytes__native(a, b, length, max_dist); //This is faster on ARMs.
uint64_t difference = 0;
uint64_t i = 0;
Expand Down