This commit is contained in:
Gregory Burd 2024-04-24 16:25:13 -04:00
parent e36f5699fe
commit 5adabf5a64
2 changed files with 22 additions and 12 deletions

View file

@ -65,7 +65,7 @@ void __attribute__((format(printf, 4, 5))) __sm_diag_(const char *file, int line
expr expr
#endif #endif
#define IS_8_BYTE_ALIGNED(addr) (((uintptr_t)(addr) & 0x7) == 0) #define IS_8_BYTE_ALIGNED(addr) (((uintptr_t)(addr)&0x7) == 0)
enum __SM_CHUNK_INFO { enum __SM_CHUNK_INFO {
/* metadata overhead: 4 bytes for __sm_chunk_t count */ /* metadata overhead: 4 bytes for __sm_chunk_t count */
@ -108,7 +108,7 @@ enum __SM_CHUNK_INFO {
SM_NEEDS_TO_SHRINK = 2 SM_NEEDS_TO_SHRINK = 2
}; };
#define SM_CHUNK_GET_FLAGS(from, at) (((from)) & ((sm_bitvec_t)SM_FLAG_MASK << ((at) * 2))) >> ((at) * 2) #define SM_CHUNK_GET_FLAGS(from, at) (((from)) & ((sm_bitvec_t)SM_FLAG_MASK << ((at)*2))) >> ((at)*2)
typedef struct { typedef struct {
sm_bitvec_t *m_data; sm_bitvec_t *m_data;
@ -1337,6 +1337,10 @@ sparsemap_rank_vec(sparsemap_t *map, size_t x, size_t y, bool value, sm_bitvec_t
size_t amt, gap, pos = 0, result = 0, prev = 0, count; size_t amt, gap, pos = 0, result = 0, prev = 0, count;
uint8_t *p; uint8_t *p;
if (x > y) {
return 0;
}
count = __sm_get_chunk_map_count(map); count = __sm_get_chunk_map_count(map);
if (count == 0) { if (count == 0) {
@ -1350,7 +1354,8 @@ sparsemap_rank_vec(sparsemap_t *map, size_t x, size_t y, bool value, sm_bitvec_t
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
sm_idx_t start = *(sm_idx_t *)p; sm_idx_t start = *(sm_idx_t *)p;
gap = start - prev; /* [prev, start), prev is the last bit examined 0-based */ /* [prev, start + pos), prev is the last bit examined 0-based. */
gap = start - (prev + pos);
/* Start of this chunk is greater than the end of the desired range. */ /* Start of this chunk is greater than the end of the desired range. */
if (start > y) { if (start > y) {
/* This chunk starts after our range [x, y]. */ /* This chunk starts after our range [x, y]. */
@ -1368,9 +1373,13 @@ sparsemap_rank_vec(sparsemap_t *map, size_t x, size_t y, bool value, sm_bitvec_t
result += gap - x; result += gap - x;
x = 0; x = 0;
} }
} else {
if (x > gap) {
x -= gap;
}
} }
} }
prev = start - 1; prev = start;
p += sizeof(sm_idx_t); p += sizeof(sm_idx_t);
__sm_chunk_t chunk; __sm_chunk_t chunk;
__sm_chunk_map_init(&chunk, p); __sm_chunk_map_init(&chunk, p);
@ -1383,7 +1392,7 @@ sparsemap_rank_vec(sparsemap_t *map, size_t x, size_t y, bool value, sm_bitvec_t
/* Count any additional unset bits that fall outside the last chunk but /* Count any additional unset bits that fall outside the last chunk but
within the range. */ within the range. */
if (value == false) { if (value == false) {
size_t last = prev + pos; size_t last = prev - 1 + pos;
if (y > last) { if (y > last) {
result += y - last - x; result += y - last - x;
} }

View file

@ -775,7 +775,7 @@ test_api_rank_true(const MunitParameter params[], void *data)
sparsemap_set(map, hole, false); sparsemap_set(map, hole, false);
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
for (int j = i; j < 10000; j++) { for (int j = i; j < 10000; j++) {
int amt = j - i + 1 - ((hole >= i && j >= hole) ? 1 : 0); int amt = (i > j) ? 0 : j - i + 1 - ((hole >= i && j >= hole) ? 1 : 0);
int r = sparsemap_rank(map, i, j, true); int r = sparsemap_rank(map, i, j, true);
assert_true(r == amt); assert_true(r == amt);
} }
@ -825,7 +825,7 @@ test_api_rank_false(const MunitParameter params[], void *data)
sparsemap_set(map, hole, true); sparsemap_set(map, hole, true);
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
for (int j = i; j < 10000; j++) { for (int j = i; j < 10000; j++) {
int amt = j - i + 1 - ((hole >= i && j >= hole) ? 1 : 0); int amt = (i > j) ? 0 : j - i + 1 - ((hole >= i && j >= hole) ? 1 : 0);
r = sparsemap_rank(map, i, j, false); r = sparsemap_rank(map, i, j, false);
assert_true(r == amt); assert_true(r == amt);
} }
@ -1201,22 +1201,23 @@ test_perf_span_solo(const MunitParameter params[], void *data)
int located_at, placed_at, amt = 500; int located_at, placed_at, amt = 500;
assert_ptr_not_null(map); assert_ptr_not_null(map);
return MUNIT_OK; // TODO
for (int i = 1; i < amt; i++) { for (int i = 1; i < amt; i++) {
for (int j = 1; j <= 100; j++) { for (int length = 1; length <= 100; length++) {
sparsemap_clear(map); sparsemap_clear(map);
placed_at = sm_add_span(map, amt, j); placed_at = sm_add_span(map, amt, length);
// logf("i = %d, j = %d\tplaced_at %d\n", i, j, placed_at); // logf("i = %d, length = %d\tplaced_at %d\n", i, length, placed_at);
// sm_whats_set(map, 5000); // sm_whats_set(map, 5000);
// start = nsts(); // start = nsts();
located_at = sparsemap_span(map, 0, j, true); located_at = sparsemap_span(map, 0, length, true);
// stop = nsts(); // stop = nsts();
// double amt = (stop - start) * 1e6; // double amt = (stop - start) * 1e6;
// if (amt > 0) { // if (amt > 0) {
// fprintf(stdout, "%0.8f\n", amt); // fprintf(stdout, "%0.8f\n", amt);
// } // }
if (placed_at != located_at) if (placed_at != located_at)
logf("a: i = %d, j = %d\tplaced_at %d located_at %d\n", i, j, placed_at, located_at); logf("a: i = %d, length = %d\tplaced_at %d located_at %d\n", i, length, placed_at, located_at);
} }
} }
return MUNIT_OK; return MUNIT_OK;