| 1 |
/********************************************************************* |
| 2 |
Blosc - Blocked Shuffling and Compression Library |
| 3 |
|
| 4 |
Copyright (c) 2021 Blosc Development Team <[email protected]> |
| 5 |
https://blosc.org |
| 6 |
License: BSD 3-Clause (see LICENSE.txt) |
| 7 |
|
| 8 |
See LICENSE.txt for details about copyright and rights to use. |
| 9 |
**********************************************************************/ |
| 10 |
|
| 11 |
/* |
| 12 |
* Bitshuffle - Filter for improving compression of typed binary data. |
| 13 |
* |
| 14 |
* Author: Kiyoshi Masui <[email protected]> |
| 15 |
* Website: http://www.github.com/kiyo-masui/bitshuffle |
| 16 |
* Created: 2014 |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Generic (non-hardware-accelerated) shuffle/unshuffle routines. |
| 21 |
These are used when hardware-accelerated functions aren't available |
| 22 |
for a particular platform; they are also used by the hardware- |
| 23 |
accelerated functions to handle any remaining elements in a block |
| 24 |
which isn't a multiple of the hardware's vector size. */ |
| 25 |
|
| 26 |
#ifndef BLOSC_BITSHUFFLE_GENERIC_H |
| 27 |
#define BLOSC_BITSHUFFLE_GENERIC_H |
| 28 |
|
| 29 |
#include "blosc2/blosc2-common.h" |
| 30 |
|
| 31 |
#include <stddef.h> |
| 32 |
#include <stdint.h> |
| 33 |
|
| 34 |
// Macros. |
| 35 |
#define CHECK_MULT_EIGHT(n) if (n % 8) return -80; |
| 36 |
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) |
| 37 |
|
| 38 |
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) |
| 39 |
#define CHECK_ERR(count) \ |
| 40 |
do { \ |
| 41 |
if ((count) < 0) \ |
| 42 |
return count; \ |
| 43 |
} while (0) |
| 44 |
|
| 45 |
#define CHECK_ERR_FREE(count, buf) if (count < 0) { free(buf); return count; } |
| 46 |
|
| 47 |
/* ---- Worker code not requiring special instruction sets. ---- |
| 48 |
* |
| 49 |
* The following code does not use any x86 specific vectorized instructions |
| 50 |
* and should compile on any machine |
| 51 |
* |
| 52 |
*/ |
| 53 |
|
| 54 |
/* Transpose 8x8 bit array packed into a single quadword *x*. |
| 55 |
* *t* is workspace. */ |
| 56 |
#define TRANS_BIT_8X8(x, t) { \ |
| 57 |
t = (x ^ (x >> 7)) & 0x00AA00AA00AA00AALL; \ |
| 58 |
x = x ^ t ^ (t << 7); \ |
| 59 |
t = (x ^ (x >> 14)) & 0x0000CCCC0000CCCCLL; \ |
| 60 |
x = x ^ t ^ (t << 14); \ |
| 61 |
t = (x ^ (x >> 28)) & 0x00000000F0F0F0F0LL; \ |
| 62 |
x = x ^ t ^ (t << 28); \ |
| 63 |
} |
| 64 |
|
| 65 |
/* Transpose 8x8 bit array along the diagonal from upper right |
| 66 |
to lower left */ |
| 67 |
#define TRANS_BIT_8X8_BE(x, t) { \ |
| 68 |
t = (x ^ (x >> 9)) & 0x0055005500550055LL; \ |
| 69 |
x = x ^ t ^ (t << 9); \ |
| 70 |
t = (x ^ (x >> 18)) & 0x0000333300003333LL; \ |
| 71 |
x = x ^ t ^ (t << 18); \ |
| 72 |
t = (x ^ (x >> 36)) & 0x000000000F0F0F0FLL; \ |
| 73 |
x = x ^ t ^ (t << 36); \ |
| 74 |
} |
| 75 |
|
| 76 |
/* Transpose of an array of arbitrarily typed elements. */ |
| 77 |
#define TRANS_ELEM_TYPE(in, out, lda, ldb, type_t) { \ |
| 78 |
size_t ii, jj, kk; \ |
| 79 |
const type_t* in_type = (const type_t*) in; \ |
| 80 |
type_t* out_type = (type_t*) out; \ |
| 81 |
for(ii = 0; ii + 7 < lda; ii += 8) { \ |
| 82 |
for(jj = 0; jj < ldb; jj++) { \ |
| 83 |
for(kk = 0; kk < 8; kk++) { \ |
| 84 |
out_type[jj*lda + ii + kk] = \ |
| 85 |
in_type[ii*ldb + kk * ldb + jj]; \ |
| 86 |
} \ |
| 87 |
} \ |
| 88 |
} \ |
| 89 |
for(ii = lda - lda % 8; ii < lda; ii ++) { \ |
| 90 |
for(jj = 0; jj < ldb; jj++) { \ |
| 91 |
out_type[jj*lda + ii] = in_type[ii*ldb + jj]; \ |
| 92 |
} \ |
| 93 |
} \ |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
/* Memory copy with bshuf call signature. For testing and profiling. */ |
| 99 |
BLOSC_NO_EXPORT int64_t |
| 100 |
bshuf_copy(const void* in, void* out, const size_t size, const size_t elem_size); |
| 101 |
|
| 102 |
/* Private functions */ |
| 103 |
BLOSC_NO_EXPORT int64_t |
| 104 |
bshuf_trans_byte_elem_remainder(const void* in, void* out, const size_t size, |
| 105 |
const size_t elem_size, const size_t start); |
| 106 |
|
| 107 |
BLOSC_NO_EXPORT int64_t |
| 108 |
bshuf_trans_byte_elem_scal(const void* in, void* out, const size_t size, |
| 109 |
const size_t elem_size); |
| 110 |
|
| 111 |
BLOSC_NO_EXPORT int64_t |
| 112 |
bshuf_trans_bit_byte_remainder(const void* in, void* out, const size_t size, |
| 113 |
const size_t elem_size, const size_t start_byte); |
| 114 |
|
| 115 |
BLOSC_NO_EXPORT int64_t |
| 116 |
bshuf_trans_elem(const void* in, void* out, const size_t lda, |
| 117 |
const size_t ldb, const size_t elem_size); |
| 118 |
|
| 119 |
BLOSC_NO_EXPORT int64_t |
| 120 |
bshuf_trans_bitrow_eight(const void* in, void* out, const size_t size, |
| 121 |
const size_t elem_size); |
| 122 |
|
| 123 |
BLOSC_NO_EXPORT int64_t |
| 124 |
bshuf_shuffle_bit_eightelem_scal(const void* in, void* out, |
| 125 |
const size_t size, const size_t elem_size); |
| 126 |
|
| 127 |
BLOSC_NO_EXPORT int64_t |
| 128 |
bshuf_trans_byte_bitrow_scal(const void* in, void* out, const size_t size, |
| 129 |
const size_t elem_size); |
| 130 |
|
| 131 |
/* Bitshuffle the data. |
| 132 |
* |
| 133 |
* Transpose the bits within elements. |
| 134 |
* |
| 135 |
* Parameters |
| 136 |
* ---------- |
| 137 |
* in : input buffer, must be of size * elem_size bytes |
| 138 |
* out : output buffer, must be of size * elem_size bytes |
| 139 |
* size : number of elements in input |
| 140 |
* elem_size : element size of typed data |
| 141 |
* tmp_buffer : temporary buffer with the same `size` than `in` and `out` |
| 142 |
* |
| 143 |
* Returns |
| 144 |
* ------- |
| 145 |
* nothing -- this cannot fail |
| 146 |
* |
| 147 |
*/ |
| 148 |
|
| 149 |
BLOSC_NO_EXPORT int64_t |
| 150 |
bshuf_trans_bit_elem_scal(const void* in, void* out, const size_t size, |
| 151 |
const size_t elem_size); |
| 152 |
|
| 153 |
/* Unshuffle bitshuffled data. |
| 154 |
* |
| 155 |
* Untranspose the bits within elements. |
| 156 |
* |
| 157 |
* To properly unshuffle bitshuffled data, *size* and *elem_size* must |
| 158 |
* match the parameters used to shuffle the data. |
| 159 |
* |
| 160 |
* Parameters |
| 161 |
* ---------- |
| 162 |
* in : input buffer, must be of size * elem_size bytes |
| 163 |
* out : output buffer, must be of size * elem_size bytes |
| 164 |
* size : number of elements in input |
| 165 |
* elem_size : element size of typed data |
| 166 |
* tmp_buffer : temporary buffer with the same `size` than `in` and `out` |
| 167 |
* |
| 168 |
* Returns |
| 169 |
* ------- |
| 170 |
* nothing -- this cannot fail |
| 171 |
* |
| 172 |
*/ |
| 173 |
|
| 174 |
BLOSC_NO_EXPORT int64_t |
| 175 |
bshuf_untrans_bit_elem_scal(const void* in, void* out, const size_t size, |
| 176 |
const size_t elem_size); |
| 177 |
|
| 178 |
#endif /* BLOSC_BITSHUFFLE_GENERIC_H */ |