Skip to content

Commit

Permalink
Minor optimization for linear search
Browse files Browse the repository at this point in the history
Add a small loop unrolling to when BGEN_MAYBELESSEQUAL is set.
  • Loading branch information
tidwall committed Dec 20, 2024
1 parent 5fbb5a1 commit 1e2ae70
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 11 additions & 0 deletions bgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -920,13 +920,24 @@ static int BGEN_SYM(search_bsearch)(BGEN_ITEM *items, int nitems,
return i;
}
#else

#if defined(BGEN_MLESIMD)
#include <immintrin.h>
#endif

BGEN_INLINE
static int BGEN_SYM(search_linear)(BGEN_ITEM *items, int nitems, BGEN_ITEM key,
void *udata, int *found)
{
int i = 0;
*found = 0;
#ifdef BGEN_MAYBELESSEQUAL
while (nitems-i >= 4) {
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
}
for (; i < nitems; i++) {
if (BGEN_SYM(maybelessequal)(key, items[i], udata)) {
goto compare;
Expand Down
4 changes: 2 additions & 2 deletions tests/bench_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int C = 0; // -1 = worse-case, 0 = average, +1 = best-case
// #define USECOMPARE

struct point {
uint32_t curve;
int32_t curve;
int id;
double x;
double y;
Expand Down Expand Up @@ -195,7 +195,7 @@ int main(void) {
point.x = rand_double()*360.0-180.0;
point.y = rand_double()*180.0-90.0;
point.curve = curve_hilbert(point.x, point.y,
(double[4]){-180, -90, 180, 90});
(double[4]){-180, -90, 180, 90})/2;
// point.curve = curve_z(point.y, point.x);
keys[i] = point;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/test_spatial2x.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "curve.h"

struct point {
int id;
uint32_t curve;
int id;
double x;
double y;
};
Expand Down Expand Up @@ -40,8 +40,9 @@ static void shuffle_points(struct point *array, size_t numels) {
#define BGEN_FANOUT 4
#define BGEN_COUNTED
#define BGEN_SPATIAL
#define BGEN_ITEMRECT { point_rect(item, min, max); }
#define BGEN_COMPARE { return point_compare(a, b); }
#define BGEN_ITEMRECT point_rect(item, min, max);
#define BGEN_MAYBELESSEQUAL return a.curve <= b.curve;
#define BGEN_COMPARE return point_compare(a, b);
#include "../bgen.h"


Expand Down

0 comments on commit 1e2ae70

Please sign in to comment.