| 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 |
#include "b2nd.h" |
| 12 |
|
| 13 |
#include <stdint.h> |
| 14 |
|
| 15 |
// copyNdim where N = {2-8} - specializations of copy loops to be used by b2nd_copy_buffer |
| 16 |
// since we don't have c++ templates, substitute manual specializations for up to known B2ND_MAX_DIM (8) |
| 17 |
// it's not pretty, but it substantially reduces overhead vs. the generic method |
| 18 |
void copy8dim(const int32_t itemsize, |
| 19 |
const int64_t *copy_shape, |
| 20 |
const uint8_t *bsrc, const int64_t *src_strides, |
| 21 |
uint8_t *bdst, const int64_t *dst_strides) { |
| 22 |
int64_t copy_nbytes = copy_shape[7] * itemsize; |
| 23 |
int64_t copy_start[7] = {0}; |
| 24 |
do { |
| 25 |
do { |
| 26 |
do { |
| 27 |
do { |
| 28 |
do { |
| 29 |
do { |
| 30 |
do { |
| 31 |
int64_t src_copy_start = 0; |
| 32 |
int64_t dst_copy_start = 0; |
| 33 |
for (int j = 0; j < 7; ++j) { |
| 34 |
src_copy_start += copy_start[j] * src_strides[j]; |
| 35 |
dst_copy_start += copy_start[j] * dst_strides[j]; |
| 36 |
} |
| 37 |
memcpy(&bdst[dst_copy_start * itemsize], &bsrc[src_copy_start * itemsize], copy_nbytes); |
| 38 |
++copy_start[6]; |
| 39 |
} while (copy_start[6] < copy_shape[6]); |
| 40 |
++copy_start[5]; |
| 41 |
copy_start[6] = 0; |
| 42 |
} while (copy_start[5] < copy_shape[5]); |
| 43 |
++copy_start[4]; |
| 44 |
copy_start[5] = 0; |
| 45 |
} while (copy_start[4] < copy_shape[4]); |
| 46 |
++copy_start[3]; |
| 47 |
copy_start[4] = 0; |
| 48 |
} while (copy_start[3] < copy_shape[3]); |
| 49 |
++copy_start[2]; |
| 50 |
copy_start[3] = 0; |
| 51 |
} while (copy_start[2] < copy_shape[2]); |
| 52 |
++copy_start[1]; |
| 53 |
copy_start[2] = 0; |
| 54 |
} while (copy_start[1] < copy_shape[1]); |
| 55 |
++copy_start[0]; |
| 56 |
copy_start[1] = 0; |
| 57 |
} while (copy_start[0] < copy_shape[0]); |
| 58 |
} |
| 59 |
|
| 60 |
void copy7dim(const int32_t itemsize, |
| 61 |
const int64_t *copy_shape, |
| 62 |
const uint8_t *bsrc, const int64_t *src_strides, |
| 63 |
uint8_t *bdst, const int64_t *dst_strides) { |
| 64 |
int64_t copy_nbytes = copy_shape[6] * itemsize; |
| 65 |
int64_t copy_start[6] = {0}; |
| 66 |
do { |
| 67 |
do { |
| 68 |
do { |
| 69 |
do { |
| 70 |
do { |
| 71 |
do { |
| 72 |
int64_t src_copy_start = 0; |
| 73 |
int64_t dst_copy_start = 0; |
| 74 |
for (int j = 0; j < 6; ++j) { |
| 75 |
src_copy_start += copy_start[j] * src_strides[j]; |
| 76 |
dst_copy_start += copy_start[j] * dst_strides[j]; |
| 77 |
} |
| 78 |
memcpy(&bdst[dst_copy_start * itemsize], &bsrc[src_copy_start * itemsize], copy_nbytes); |
| 79 |
++copy_start[5]; |
| 80 |
} while (copy_start[5] < copy_shape[5]); |
| 81 |
++copy_start[4]; |
| 82 |
copy_start[5] = 0; |
| 83 |
} while (copy_start[4] < copy_shape[4]); |
| 84 |
++copy_start[3]; |
| 85 |
copy_start[4] = 0; |
| 86 |
} while (copy_start[3] < copy_shape[3]); |
| 87 |
++copy_start[2]; |
| 88 |
copy_start[3] = 0; |
| 89 |
} while (copy_start[2] < copy_shape[2]); |
| 90 |
++copy_start[1]; |
| 91 |
copy_start[2] = 0; |
| 92 |
} while (copy_start[1] < copy_shape[1]); |
| 93 |
++copy_start[0]; |
| 94 |
copy_start[1] = 0; |
| 95 |
} while (copy_start[0] < copy_shape[0]); |
| 96 |
} |
| 97 |
|
| 98 |
void copy6dim(const int32_t itemsize, |
| 99 |
const int64_t *copy_shape, |
| 100 |
const uint8_t *bsrc, const int64_t *src_strides, |
| 101 |
uint8_t *bdst, const int64_t *dst_strides) { |
| 102 |
int64_t copy_nbytes = copy_shape[5] * itemsize; |
| 103 |
int64_t copy_start[5] = {0}; |
| 104 |
do { |
| 105 |
do { |
| 106 |
do { |
| 107 |
do { |
| 108 |
do { |
| 109 |
int64_t src_copy_start = 0; |
| 110 |
int64_t dst_copy_start = 0; |
| 111 |
for (int j = 0; j < 5; ++j) { |
| 112 |
src_copy_start += copy_start[j] * src_strides[j]; |
| 113 |
dst_copy_start += copy_start[j] * dst_strides[j]; |
| 114 |
} |
| 115 |
memcpy(&bdst[dst_copy_start * itemsize], &bsrc[src_copy_start * itemsize], copy_nbytes); |
| 116 |
++copy_start[4]; |
| 117 |
} while (copy_start[4] < copy_shape[4]); |
| 118 |
++copy_start[3]; |
| 119 |
copy_start[4] = 0; |
| 120 |
} while (copy_start[3] < copy_shape[3]); |
| 121 |
++copy_start[2]; |
| 122 |
copy_start[3] = 0; |
| 123 |
} while (copy_start[2] < copy_shape[2]); |
| 124 |
++copy_start[1]; |
| 125 |
copy_start[2] = 0; |
| 126 |
} while (copy_start[1] < copy_shape[1]); |
| 127 |
++copy_start[0]; |
| 128 |
copy_start[1] = 0; |
| 129 |
} while (copy_start[0] < copy_shape[0]); |
| 130 |
} |
| 131 |
|
| 132 |
void copy5dim(const int32_t itemsize, |
| 133 |
const int64_t *copy_shape, |
| 134 |
const uint8_t *bsrc, const int64_t *src_strides, |
| 135 |
uint8_t *bdst, const int64_t *dst_strides) { |
| 136 |
int64_t copy_nbytes = copy_shape[4] * itemsize; |
| 137 |
int64_t copy_start[4] = {0}; |
| 138 |
do { |
| 139 |
do { |
| 140 |
do { |
| 141 |
do { |
| 142 |
int64_t src_copy_start = 0; |
| 143 |
int64_t dst_copy_start = 0; |
| 144 |
for (int j = 0; j < 4; ++j) { |
| 145 |
src_copy_start += copy_start[j] * src_strides[j]; |
| 146 |
dst_copy_start += copy_start[j] * dst_strides[j]; |
| 147 |
} |
| 148 |
memcpy(&bdst[dst_copy_start * itemsize], &bsrc[src_copy_start * itemsize], copy_nbytes); |
| 149 |
++copy_start[3]; |
| 150 |
} while (copy_start[3] < copy_shape[3]); |
| 151 |
++copy_start[2]; |
| 152 |
copy_start[3] = 0; |
| 153 |
} while (copy_start[2] < copy_shape[2]); |
| 154 |
++copy_start[1]; |
| 155 |
copy_start[2] = 0; |
| 156 |
} while (copy_start[1] < copy_shape[1]); |
| 157 |
++copy_start[0]; |
| 158 |
copy_start[1] = 0; |
| 159 |
} while (copy_start[0] < copy_shape[0]); |
| 160 |
} |
| 161 |
|
| 162 |
void copy4dim(const int32_t itemsize, |
| 163 |
const int64_t *copy_shape, |
| 164 |
const uint8_t *bsrc, const int64_t *src_strides, |
| 165 |
uint8_t *bdst, const int64_t *dst_strides) { |
| 166 |
int64_t copy_nbytes = copy_shape[3] * itemsize; |
| 167 |
int64_t copy_start[3] = {0}; |
| 168 |
do { |
| 169 |
do { |
| 170 |
do { |
| 171 |
int64_t src_copy_start = 0; |
| 172 |
int64_t dst_copy_start = 0; |
| 173 |
for (int j = 0; j < 3; ++j) { |
| 174 |
src_copy_start += copy_start[j] * src_strides[j]; |
| 175 |
dst_copy_start += copy_start[j] * dst_strides[j]; |
| 176 |
} |
| 177 |
memcpy(&bdst[dst_copy_start * itemsize], &bsrc[src_copy_start * itemsize], copy_nbytes); |
| 178 |
++copy_start[2]; |
| 179 |
} while (copy_start[2] < copy_shape[2]); |
| 180 |
++copy_start[1]; |
| 181 |
copy_start[2] = 0; |
| 182 |
} while (copy_start[1] < copy_shape[1]); |
| 183 |
++copy_start[0]; |
| 184 |
copy_start[1] = 0; |
| 185 |
} while (copy_start[0] < copy_shape[0]); |
| 186 |
} |
| 187 |
|
| 188 |
void copy3dim(const int32_t itemsize, |
| 189 |
const int64_t *copy_shape, |
| 190 |
const uint8_t *bsrc, const int64_t *src_strides, |
| 191 |
uint8_t *bdst, const int64_t *dst_strides) { |
| 192 |
int64_t copy_nbytes = copy_shape[2] * itemsize; |
| 193 |
int64_t copy_start[2] = {0}; |
| 194 |
do { |
| 195 |
do { |
| 196 |
int64_t src_copy_start = 0; |
| 197 |
int64_t dst_copy_start = 0; |
| 198 |
for (int j = 0; j < 2; ++j) { |
| 199 |
src_copy_start += copy_start[j] * src_strides[j]; |
| 200 |
dst_copy_start += copy_start[j] * dst_strides[j]; |
| 201 |
} |
| 202 |
memcpy(&bdst[dst_copy_start * itemsize], &bsrc[src_copy_start * itemsize], copy_nbytes); |
| 203 |
++copy_start[1]; |
| 204 |
} while (copy_start[1] < copy_shape[1]); |
| 205 |
++copy_start[0]; |
| 206 |
copy_start[1] = 0; |
| 207 |
} while (copy_start[0] < copy_shape[0]); |
| 208 |
} |
| 209 |
|
| 210 |
void copy2dim(const int32_t itemsize, |
| 211 |
const int64_t *copy_shape, |
| 212 |
const uint8_t *bsrc, const int64_t *src_strides, |
| 213 |
uint8_t *bdst, const int64_t *dst_strides) { |
| 214 |
int64_t copy_nbytes = copy_shape[1] * itemsize; |
| 215 |
int64_t copy_start = 0; |
| 216 |
do { |
| 217 |
int64_t src_copy_start = copy_start * src_strides[0]; |
| 218 |
int64_t dst_copy_start = copy_start * dst_strides[0]; |
| 219 |
memcpy(&bdst[dst_copy_start * itemsize], &bsrc[src_copy_start * itemsize], copy_nbytes); |
| 220 |
++copy_start; |
| 221 |
} while (copy_start < copy_shape[0]); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
void copy_ndim_fallback(const int8_t ndim, |
| 226 |
const int32_t itemsize, |
| 227 |
int64_t *copy_shape, |
| 228 |
const uint8_t *bsrc, int64_t *src_strides, |
| 229 |
uint8_t *bdst, int64_t *dst_strides) { |
| 230 |
int64_t copy_nbytes = copy_shape[ndim - 1] * itemsize; |
| 231 |
int64_t number_of_copies = 1; |
| 232 |
for (int i = 0; i < ndim - 1; ++i) { |
| 233 |
number_of_copies *= copy_shape[i]; |
| 234 |
} |
| 235 |
for (int ncopy = 0; ncopy < number_of_copies; ++ncopy) { |
| 236 |
// Compute the start of the copy |
| 237 |
int64_t copy_start[B2ND_MAX_DIM] = {0}; |
| 238 |
blosc2_unidim_to_multidim((int8_t) (ndim - 1), copy_shape, ncopy, copy_start); |
| 239 |
|
| 240 |
// Translate this index to the src buffer |
| 241 |
int64_t src_copy_start; |
| 242 |
blosc2_multidim_to_unidim(copy_start, (int8_t) (ndim - 1), src_strides, &src_copy_start); |
| 243 |
|
| 244 |
// Translate this index to the dst buffer |
| 245 |
int64_t dst_copy_start; |
| 246 |
blosc2_multidim_to_unidim(copy_start, (int8_t) (ndim - 1), dst_strides, &dst_copy_start); |
| 247 |
|
| 248 |
// Perform the copy |
| 249 |
memcpy(&bdst[dst_copy_start * itemsize], |
| 250 |
&bsrc[src_copy_start * itemsize], |
| 251 |
copy_nbytes); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
int b2nd_copy_buffer2(int8_t ndim, |
| 256 |
int32_t itemsize, |
| 257 |
const void *src, const int64_t *src_pad_shape, |
| 258 |
const int64_t *src_start, const int64_t *src_stop, |
| 259 |
void *dst, const int64_t *dst_pad_shape, |
| 260 |
const int64_t *dst_start) { |
| 261 |
// Compute the shape of the copy |
| 262 |
int64_t copy_shape[B2ND_MAX_DIM] = {0}; |
| 263 |
for (int i = 0; i < ndim; ++i) { |
| 264 |
copy_shape[i] = src_stop[i] - src_start[i]; |
| 265 |
if (copy_shape[i] == 0) { |
| 266 |
return BLOSC2_ERROR_SUCCESS; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// Compute the strides |
| 271 |
int64_t src_strides[B2ND_MAX_DIM] = {0}; |
| 272 |
src_strides[ndim - 1] = 1; |
| 273 |
for (int i = ndim - 2; i >= 0; --i) { |
| 274 |
src_strides[i] = src_strides[i + 1] * src_pad_shape[i + 1]; |
| 275 |
} |
| 276 |
|
| 277 |
int64_t dst_strides[B2ND_MAX_DIM] = {0}; |
| 278 |
dst_strides[ndim - 1] = 1; |
| 279 |
for (int i = ndim - 2; i >= 0; --i) { |
| 280 |
dst_strides[i] = dst_strides[i + 1] * dst_pad_shape[i + 1]; |
| 281 |
} |
| 282 |
|
| 283 |
// Align the buffers removing unnecessary data |
| 284 |
int64_t src_start_n; |
| 285 |
blosc2_multidim_to_unidim(src_start, ndim, src_strides, &src_start_n); |
| 286 |
uint8_t *bsrc = (uint8_t *) src; |
| 287 |
bsrc = &bsrc[src_start_n * itemsize]; |
| 288 |
|
| 289 |
int64_t dst_start_n; |
| 290 |
blosc2_multidim_to_unidim(dst_start, ndim, dst_strides, &dst_start_n); |
| 291 |
uint8_t *bdst = (uint8_t *) dst; |
| 292 |
bdst = &bdst[dst_start_n * itemsize]; |
| 293 |
|
| 294 |
switch (ndim) { |
| 295 |
case 1: |
| 296 |
memcpy(&bdst[0], &bsrc[0], copy_shape[0] * itemsize); |
| 297 |
break; |
| 298 |
case 2: |
| 299 |
copy2dim(itemsize, copy_shape, bsrc, src_strides, bdst, dst_strides); |
| 300 |
break; |
| 301 |
case 3: |
| 302 |
copy3dim(itemsize, copy_shape, bsrc, src_strides, bdst, dst_strides); |
| 303 |
break; |
| 304 |
case 4: |
| 305 |
copy4dim(itemsize, copy_shape, bsrc, src_strides, bdst, dst_strides); |
| 306 |
break; |
| 307 |
case 5: |
| 308 |
copy5dim(itemsize, copy_shape, bsrc, src_strides, bdst, dst_strides); |
| 309 |
break; |
| 310 |
case 6: |
| 311 |
copy6dim(itemsize, copy_shape, bsrc, src_strides, bdst, dst_strides); |
| 312 |
break; |
| 313 |
case 7: |
| 314 |
copy7dim(itemsize, copy_shape, bsrc, src_strides, bdst, dst_strides); |
| 315 |
break; |
| 316 |
case 8: |
| 317 |
copy8dim(itemsize, copy_shape, bsrc, src_strides, bdst, dst_strides); |
| 318 |
break; |
| 319 |
default: |
| 320 |
// guard against potential future increase to B2ND_MAX_DIM |
| 321 |
copy_ndim_fallback(ndim, itemsize, copy_shape, bsrc, src_strides, bdst, dst_strides); |
| 322 |
break; |
| 323 |
} |
| 324 |
|
| 325 |
return BLOSC2_ERROR_SUCCESS; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
// Keep the old signature for API compatibility |
| 330 |
int b2nd_copy_buffer(int8_t ndim, |
| 331 |
uint8_t itemsize, |
| 332 |
const void *src, const int64_t *src_pad_shape, |
| 333 |
const int64_t *src_start, const int64_t *src_stop, |
| 334 |
void *dst, const int64_t *dst_pad_shape, |
| 335 |
const int64_t *dst_start) { |
| 336 |
// Simply cast itemsize to int32_t and delegate |
| 337 |
return b2nd_copy_buffer2(ndim, (int32_t)itemsize, src, src_pad_shape, |
| 338 |
src_start, src_stop, dst, dst_pad_shape, dst_start); |
| 339 |
} |