Skip to content

Commit 51ff6b7

Browse files
authored
Merge pull request #137 from static-frame/136/first-true-uint
Alternate implementation of `first_true_1d`, `first_true_2d`
2 parents c895c85 + 8e8ddab commit 51ff6b7

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/_arraykit.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,7 +3526,6 @@ resolve_dtype_iter(PyObject *Py_UNUSED(m), PyObject *arg) {
35263526
//------------------------------------------------------------------------------
35273527
// general utility
35283528

3529-
#define AK_FT_MEMCMP_SIZE 8
35303529

35313530
static char *first_true_1d_kwarg_names[] = {
35323531
"array",
@@ -3561,10 +3560,10 @@ first_true_1d(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
35613560
return NULL;
35623561
}
35633562

3564-
static npy_bool zero_buffer[AK_FT_MEMCMP_SIZE] = {0};
3563+
npy_intp lookahead = sizeof(npy_uint64);
35653564

35663565
npy_intp size = PyArray_SIZE(array);
3567-
lldiv_t size_div = lldiv((long long)size, AK_FT_MEMCMP_SIZE); // quot, rem
3566+
lldiv_t size_div = lldiv((long long)size, lookahead); // quot, rem
35683567

35693568
npy_bool *array_buffer = (npy_bool*)PyArray_DATA(array);
35703569

@@ -3580,10 +3579,10 @@ first_true_1d(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
35803579
p_end = p + size;
35813580

35823581
while (p < p_end - size_div.rem) {
3583-
if (memcmp(p, zero_buffer, AK_FT_MEMCMP_SIZE) != 0) {
3584-
break; // found a true
3582+
if (*(npy_uint64*)p != 0) {
3583+
break; // found a true within lookahead
35853584
}
3586-
p += AK_FT_MEMCMP_SIZE;
3585+
p += lookahead;
35873586
}
35883587
while (p < p_end) {
35893588
if (*p) break;
@@ -3594,12 +3593,10 @@ first_true_1d(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
35943593
p = array_buffer + size - 1;
35953594
p_end = array_buffer - 1;
35963595
while (p > p_end + size_div.rem) {
3597-
if (memcmp(p - AK_FT_MEMCMP_SIZE + 1, // go to front
3598-
zero_buffer,
3599-
AK_FT_MEMCMP_SIZE) != 0) {
3600-
break; // found a true
3596+
if (*(npy_uint64*)(p - lookahead + 1) != 0) {
3597+
break; // found a true within lookahead
36013598
}
3602-
p -= AK_FT_MEMCMP_SIZE;
3599+
p -= lookahead;
36033600
}
36043601
while (p > p_end) {
36053602
if (*p) break;
@@ -3699,15 +3696,15 @@ first_true_2d(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
36993696
array_ind = array; // can use array, no decref needed
37003697
}
37013698

3702-
static npy_bool zero_buffer[AK_FT_MEMCMP_SIZE] = {0};
3699+
npy_intp lookahead = sizeof(npy_uint64);
37033700

37043701
// buffer of indicators
37053702
npy_bool *buffer_ind = (npy_bool*)PyArray_DATA(array_ind);
37063703

37073704
npy_intp count_row = PyArray_DIM(array_ind, 0);
37083705
npy_intp count_col = PyArray_DIM(array_ind, 1);
37093706

3710-
lldiv_t div_col = lldiv((long long)count_col, AK_FT_MEMCMP_SIZE); // quot, rem
3707+
lldiv_t div_col = lldiv((long long)count_col, lookahead); // quot, rem
37113708

37123709
npy_intp dims_post = {count_row};
37133710
PyArrayObject *array_pos = (PyArrayObject*)PyArray_EMPTY(
@@ -3743,10 +3740,10 @@ first_true_2d(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
37433740
// scan each row from the front and terminate when True
37443741
// remove from the end the remainder
37453742
while (p < p_end - div_col.rem) {
3746-
if (memcmp(p, zero_buffer, AK_FT_MEMCMP_SIZE) != 0) {
3743+
if (*(npy_uint64*)p != 0) {
37473744
break; // found a true
37483745
}
3749-
p += AK_FT_MEMCMP_SIZE;
3746+
p += lookahead;
37503747
}
37513748
while (p < p_end) {
37523749
if (*p) {break;}
@@ -3764,13 +3761,11 @@ first_true_2d(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
37643761
p_end = buffer_ind + (count_col * r) - 1;
37653762

37663763
while (p > p_end + div_col.rem) {
3767-
// must give memcmp the start of the buffer
3768-
if (memcmp(p - AK_FT_MEMCMP_SIZE + 1, // go to front
3769-
zero_buffer,
3770-
AK_FT_MEMCMP_SIZE) != 0) {
3764+
// must go to start of lookahead
3765+
if (*(npy_uint64*)(p - lookahead + 1) != 0) {
37713766
break; // found a true
37723767
}
3773-
p -= AK_FT_MEMCMP_SIZE;
3768+
p -= lookahead;
37743769
}
37753770
while (p > p_end) {
37763771
if (*p) {break;}

0 commit comments

Comments
 (0)