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

Cycle buffers #530

Merged
merged 2 commits into from
Jul 3, 2023
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
27 changes: 11 additions & 16 deletions blosc/blosc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,12 @@ static int blosc2_intialize_header_from_context(blosc2_context* context, blosc_h
return 0;
}

void _cycle_buffers(void **src, void **dest, void **tmp) {
void *tmp2 = *src;
*src = *dest;
*dest = *tmp;
*tmp = tmp2;
}

uint8_t* pipeline_forward(struct thread_context* thread_context, const int32_t bsize,
const uint8_t* src, const int32_t offset,
Expand Down Expand Up @@ -948,10 +954,7 @@ uint8_t* pipeline_forward(struct thread_context* thread_context, const int32_t b
// No more filters are required
return _dest;
}
// Cycle buffers
_src = _dest;
_dest = _tmp;
_tmp = _src;
_cycle_buffers(&_src, &_dest, &_tmp);
}

/* Process the filter pipeline */
Expand All @@ -964,9 +967,7 @@ uint8_t* pipeline_forward(struct thread_context* thread_context, const int32_t b
shuffle(typesize, bsize, _src, _dest);
// Cycle filters when required
if (j < filters_meta[i]) {
_src = _dest;
_dest = _tmp;
_tmp = _src;
_cycle_buffers(&_src, &_dest, &_tmp);
}
}
break;
Expand Down Expand Up @@ -1025,9 +1026,7 @@ uint8_t* pipeline_forward(struct thread_context* thread_context, const int32_t b

// Cycle buffers when required
if (filters[i] != BLOSC_NOFILTER) {
_src = _dest;
_dest = _tmp;
_tmp = _src;
_cycle_buffers(&_src, &_dest, &_tmp);
}
}
return _src;
Expand Down Expand Up @@ -1344,9 +1343,7 @@ int pipeline_backward(struct thread_context* thread_context, const int32_t bsize
unshuffle(typesize, bsize, _src, _dest);
// Cycle filters when required
if (j < filters_meta[i]) {
_src = _dest;
_dest = _tmp;
_tmp = _src;
_cycle_buffers(&_src, &_dest, &_tmp);
}
// Check whether we have to copy the intermediate _dest buffer to final destination
if (last_copy_filter && (filters_meta[i] % 2) == 1 && j == filters_meta[i]) {
Expand Down Expand Up @@ -1424,9 +1421,7 @@ int pipeline_backward(struct thread_context* thread_context, const int32_t bsize

// Cycle buffers when required
if ((filters[i] != BLOSC_NOFILTER) && (filters[i] != BLOSC_TRUNC_PREC)) {
_src = _dest;
_dest = _tmp;
_tmp = _src;
_cycle_buffers(&_src, &_dest, &_tmp);
}
if (last_filter_index == i) {
break;
Expand Down
45 changes: 45 additions & 0 deletions tests/test_filters.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include <blosc2.h>
#include <stdlib.h>

#define NCHUNKS 1
#define TYPESIZE 2
#define LEN 39
#define CHUNKSIZE (TYPESIZE * LEN)

int main(void) {
blosc2_init();
srandom(0);
uint16_t *ref_data = (uint16_t *)malloc(CHUNKSIZE);
uint16_t *data_dest = (uint16_t *)malloc(CHUNKSIZE);
for (int i = 0; i < LEN; i++) {
ref_data[i] = random() % 118;
}
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
cparams.compcode = BLOSC_ZSTD;
cparams.filters[BLOSC2_MAX_FILTERS - 2] = BLOSC_BITSHUFFLE;
cparams.filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_SHUFFLE;

blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS;

cparams.typesize = TYPESIZE;
blosc2_storage storage = {.contiguous=false, .urlpath=NULL, .cparams=&cparams, .dparams=&dparams};

blosc2_schunk* schunk = blosc2_schunk_new(&storage);
blosc2_schunk_append_buffer(schunk, ref_data, CHUNKSIZE);

blosc2_schunk_decompress_chunk(schunk, 0, data_dest, CHUNKSIZE);
for (int i = 0; i < LEN; i++) {
if (data_dest[i] != ref_data[i]) {
printf("Decompressed data differs from original %d, %d, %d!\n",
i, ref_data[i], data_dest[i]);
return -1;
}
}

printf("Successful roundtrip data <-> schunk !\n");

blosc2_schunk_free(schunk);
blosc2_destroy();
return 0;
}