| 1 |
/********************************************************************* |
| 2 |
Blosc - Blocked Shuffling and Compression Library |
| 3 |
|
| 4 |
Copyright (c) 2021 Blosc Development Team <[email protected]> and Jerome Kieffer <[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 "shuffle-altivec.h" |
| 12 |
#include "shuffle-generic.h" |
| 13 |
#include <stdlib.h> |
| 14 |
|
| 15 |
/* Make sure ALTIVEC is available for the compilation target and compiler. */ |
| 16 |
#if defined(__ALTIVEC__) && defined(__VSX__) && defined(_ARCH_PWR8) |
| 17 |
|
| 18 |
#include "transpose-altivec.h" |
| 19 |
|
| 20 |
#include <altivec.h> |
| 21 |
|
| 22 |
#include <stdint.h> |
| 23 |
|
| 24 |
/* Routine optimized for shuffling a buffer for a type size of 2 bytes. */ |
| 25 |
static void |
| 26 |
shuffle2_altivec(uint8_t* const dest, const uint8_t* const src, |
| 27 |
const int32_t vectorizable_elements, const int32_t total_elements){ |
| 28 |
static const int32_t bytesoftype = 2; |
| 29 |
uint32_t i, j; |
| 30 |
__vector uint8_t xmm0[2]; |
| 31 |
|
| 32 |
for (j = 0; j < vectorizable_elements; j += 16){ |
| 33 |
/* Fetch 16 elements (32 bytes) */ |
| 34 |
for (i = 0; i < bytesoftype; i++) |
| 35 |
xmm0[i] = vec_xl(bytesoftype * j + 16 * i, src); |
| 36 |
|
| 37 |
/* Transpose vectors */ |
| 38 |
transpose2x16(xmm0); |
| 39 |
|
| 40 |
/* Store the result vectors */ |
| 41 |
for (i = 0; i < bytesoftype; i++) |
| 42 |
vec_xst(xmm0[i], j + i * total_elements, dest); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/* Routine optimized for shuffling a buffer for a type size of 4 bytes. */ |
| 47 |
static void |
| 48 |
shuffle4_altivec(uint8_t* const dest, const uint8_t* const src, |
| 49 |
const int32_t vectorizable_elements, const int32_t total_elements){ |
| 50 |
static const int32_t bytesoftype = 4; |
| 51 |
int32_t i, j; |
| 52 |
__vector uint8_t xmm0[4]; |
| 53 |
|
| 54 |
for (j = 0; j < vectorizable_elements; j += 16) |
| 55 |
{ |
| 56 |
/* Fetch 16 elements (64 bytes, 4 vectors) */ |
| 57 |
for (i = 0; i < bytesoftype; i++) |
| 58 |
xmm0[i] = vec_xl(bytesoftype * j + 16 * i, src); |
| 59 |
|
| 60 |
|
| 61 |
/* Transpose vectors */ |
| 62 |
transpose4x16(xmm0); |
| 63 |
|
| 64 |
/* Store the result vectors */ |
| 65 |
for (i = 0; i < bytesoftype; i ++){ |
| 66 |
vec_xst(xmm0[i], j + i*total_elements, dest); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/* Routine optimized for shuffling a buffer for a type size of 8 bytes. */ |
| 73 |
static void |
| 74 |
shuffle8_altivec(uint8_t* const dest, const uint8_t* const src, |
| 75 |
const int32_t vectorizable_elements, const int32_t total_elements) { |
| 76 |
static const uint8_t bytesoftype = 8; |
| 77 |
int32_t i, j; |
| 78 |
__vector uint8_t xmm0[8]; |
| 79 |
|
| 80 |
for (j = 0; j < vectorizable_elements; j += 16) |
| 81 |
{ |
| 82 |
/* Fetch 16 elements (128 bytes, 8 vectors) */ |
| 83 |
for (i = 0; i < bytesoftype; i++) |
| 84 |
xmm0[i] = vec_xl(bytesoftype * j + 16 * i, src); |
| 85 |
|
| 86 |
/* Transpose vectors */ |
| 87 |
transpose8x16(xmm0); |
| 88 |
|
| 89 |
/* Store the result vectors */ |
| 90 |
for (i = 0; i < bytesoftype; i++) |
| 91 |
vec_xst(xmm0[i], j + i*total_elements, dest); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/* Routine optimized for shuffling a buffer for a type size of 16 bytes. */ |
| 96 |
static void |
| 97 |
shuffle16_altivec(uint8_t* const dest, const uint8_t* const src, |
| 98 |
const int32_t vectorizable_elements, const int32_t total_elements) { |
| 99 |
static const int32_t bytesoftype = 16; |
| 100 |
int32_t i, j; |
| 101 |
__vector uint8_t xmm0[16]; |
| 102 |
|
| 103 |
for (j = 0; j < vectorizable_elements; j += 16) |
| 104 |
{ |
| 105 |
/* Fetch 16 elements (256 bytes, 16 vectors) */ |
| 106 |
for (i = 0; i < bytesoftype; i++) |
| 107 |
xmm0[i] = vec_xl(bytesoftype * j + 16 * i, src); |
| 108 |
|
| 109 |
// Do the job ! |
| 110 |
transpose16x16(xmm0); |
| 111 |
|
| 112 |
/* Store the result vectors */ |
| 113 |
for (i = 0; i < bytesoftype; i ++) |
| 114 |
vec_xst(xmm0[i], j + i * total_elements, dest); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
/* Routine optimized for shuffling a buffer for a type size larger than 16 bytes. */ |
| 120 |
static void |
| 121 |
shuffle16_tiled_altivec(uint8_t* const dest, const uint8_t* const src, |
| 122 |
const int32_t vectorizable_elements, const int32_t total_elements, |
| 123 |
const int32_t bytesoftype) { |
| 124 |
int32_t j, k; |
| 125 |
const int32_t vecs_per_el_rem = bytesoftype & 0xF; |
| 126 |
__vector uint8_t xmm[16]; |
| 127 |
|
| 128 |
for (j = 0; j < vectorizable_elements; j += 16) { |
| 129 |
/* Advance the offset into the type by the vector size (in bytes), unless this is |
| 130 |
the initial iteration and the type size is not a multiple of the vector size. |
| 131 |
In that case, only advance by the number of bytes necessary so that the number |
| 132 |
of remaining bytes in the type will be a multiple of the vector size. */ |
| 133 |
int32_t offset_into_type; |
| 134 |
for (offset_into_type = 0; offset_into_type < bytesoftype; |
| 135 |
offset_into_type += (offset_into_type == 0 && |
| 136 |
vecs_per_el_rem > 0 ? vecs_per_el_rem : 16)) { |
| 137 |
|
| 138 |
/* Fetch elements in groups of 256 bytes */ |
| 139 |
const uint8_t* const src_with_offset = src + offset_into_type; |
| 140 |
for (k = 0; k < 16; k++) |
| 141 |
xmm[k] = vec_xl((j + k) * bytesoftype, src_with_offset); |
| 142 |
// Do the Job! |
| 143 |
transpose16x16(xmm); |
| 144 |
/* Store the result vectors */ |
| 145 |
for (k = 0; k < 16; k++) { |
| 146 |
vec_xst(xmm[k], j + total_elements * (offset_into_type + k), dest); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
/* Routine optimized for unshuffling a buffer for a type size of 2 bytes. */ |
| 152 |
static void |
| 153 |
unshuffle2_altivec(uint8_t* const dest, const uint8_t* const src, |
| 154 |
const int32_t vectorizable_elements, const int32_t total_elements) { |
| 155 |
static const int32_t bytesoftype = 2; |
| 156 |
uint32_t i, j; |
| 157 |
__vector uint8_t xmm0[2], xmm1[2]; |
| 158 |
|
| 159 |
for (j = 0; j < vectorizable_elements; j += 16) { |
| 160 |
/* Load 16 elements (32 bytes) into 2 vectors registers. */ |
| 161 |
for (i = 0; i < bytesoftype; i++) |
| 162 |
xmm0[i] = vec_xl(j + i * total_elements, src); |
| 163 |
|
| 164 |
/* Shuffle bytes */ |
| 165 |
/* Note the shuffling is different from intel's SSE2 */ |
| 166 |
xmm1[0] = vec_vmrghb(xmm0[0], xmm0[1]); |
| 167 |
xmm1[1] = vec_vmrglb(xmm0[0], xmm0[1]); |
| 168 |
|
| 169 |
/* Store the result vectors*/ |
| 170 |
for (i = 0; i < bytesoftype; i++) |
| 171 |
vec_xst(xmm1[i], bytesoftype * j + 16 * i, dest); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/* Routine optimized for unshuffling a buffer for a type size of 4 bytes. */ |
| 176 |
static void |
| 177 |
unshuffle4_altivec(uint8_t* const dest, const uint8_t* const src, |
| 178 |
const int32_t vectorizable_elements, const int32_t total_elements) { |
| 179 |
static const int32_t bytesoftype = 4; |
| 180 |
uint32_t i, j; |
| 181 |
__vector uint8_t xmm0[4], xmm1[4]; |
| 182 |
|
| 183 |
for (j = 0; j < vectorizable_elements; j += 16) { |
| 184 |
/* Load 16 elements (64 bytes) into 4 vectors registers. */ |
| 185 |
for (i = 0; i < bytesoftype; i++) |
| 186 |
xmm0[i] = vec_xl(j + i * total_elements, src); |
| 187 |
|
| 188 |
/* Shuffle bytes */ |
| 189 |
for (i = 0; i < 2; i++) { |
| 190 |
xmm1[i ] = vec_vmrghb(xmm0[i * 2], xmm0[i * 2 + 1]); |
| 191 |
xmm1[i+2] = vec_vmrglb(xmm0[i * 2], xmm0[i * 2 + 1]); |
| 192 |
} |
| 193 |
/* Shuffle 2-byte words */ |
| 194 |
for (i = 0; i < 2; i++) { |
| 195 |
/* Compute the low 32 bytes */ |
| 196 |
xmm0[i] = (__vector uint8_t) vec_vmrghh((__vector uint16_t)xmm1[i * 2], |
| 197 |
(__vector uint16_t) xmm1[i * 2 + 1]); |
| 198 |
/* Compute the hi 32 bytes */ |
| 199 |
xmm0[i+2] = (__vector uint8_t) vec_vmrglh((__vector uint16_t)xmm1[i * 2], |
| 200 |
(__vector uint16_t)xmm1[i * 2 + 1]); |
| 201 |
} |
| 202 |
/* Store the result vectors in proper order */ |
| 203 |
vec_xst(xmm0[0], bytesoftype * j, dest); |
| 204 |
vec_xst(xmm0[2], bytesoftype * j + 16, dest); |
| 205 |
vec_xst(xmm0[1], bytesoftype * j + 32, dest); |
| 206 |
vec_xst(xmm0[3], bytesoftype * j + 48, dest); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/* Routine optimized for unshuffling a buffer for a type size of 8 bytes. */ |
| 211 |
static void |
| 212 |
unshuffle8_altivec(uint8_t* const dest, const uint8_t* const src, |
| 213 |
const int32_t vectorizable_elements, const int32_t total_elements) { |
| 214 |
static const uint8_t bytesoftype = 8; |
| 215 |
uint32_t i, j; |
| 216 |
__vector uint8_t xmm0[8], xmm1[8]; |
| 217 |
|
| 218 |
// Initialize permutations for writing |
| 219 |
for (j = 0; j < vectorizable_elements; j += 16) { |
| 220 |
/* Load 16 elements (64 bytes) into 4 vectors registers. */ |
| 221 |
for (i = 0; i < bytesoftype; i++) |
| 222 |
xmm0[i] = vec_xl(j + i * total_elements, src); |
| 223 |
/* Shuffle bytes */ |
| 224 |
for (i = 0; i < 4; i++) { |
| 225 |
xmm1[i] = vec_vmrghb(xmm0[i * 2], xmm0[i * 2 + 1]); |
| 226 |
xmm1[4 + i] = vec_vmrglb(xmm0[i * 2], xmm0[i * 2 + 1]); |
| 227 |
} |
| 228 |
/* Shuffle 2-byte words */ |
| 229 |
for (i = 0; i < 4; i++) { |
| 230 |
xmm0[i] = (__vector uint8_t)vec_vmrghh((__vector uint16_t)xmm1[i * 2], |
| 231 |
(__vector uint16_t)xmm1[i * 2 + 1]); |
| 232 |
xmm0[4 + i] = (__vector uint8_t)vec_vmrglh((__vector uint16_t)xmm1[i * 2], |
| 233 |
(__vector uint16_t)xmm1[i * 2 + 1]); |
| 234 |
} |
| 235 |
/* Shuffle 4-byte dwords */ |
| 236 |
for (i = 0; i < 4; i++) { |
| 237 |
xmm1[i] = (__vector uint8_t)vec_vmrghw((__vector uint32_t)xmm0[i * 2], |
| 238 |
(__vector uint32_t)xmm0[i * 2 + 1]); |
| 239 |
xmm1[4 + i] = (__vector uint8_t)vec_vmrglw((__vector uint32_t)xmm0[i * 2], |
| 240 |
(__vector uint32_t)xmm0[i * 2 + 1]); |
| 241 |
} |
| 242 |
/* Store the result vectors in proper order */ |
| 243 |
vec_xst(xmm1[0], bytesoftype * j, dest); |
| 244 |
vec_xst(xmm1[4], bytesoftype * j + 16, dest); |
| 245 |
vec_xst(xmm1[2], bytesoftype * j + 32, dest); |
| 246 |
vec_xst(xmm1[6], bytesoftype * j + 48, dest); |
| 247 |
vec_xst(xmm1[1], bytesoftype * j + 64, dest); |
| 248 |
vec_xst(xmm1[5], bytesoftype * j + 80, dest); |
| 249 |
vec_xst(xmm1[3], bytesoftype * j + 96, dest); |
| 250 |
vec_xst(xmm1[7], bytesoftype * j + 112, dest); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
/* Routine optimized for unshuffling a buffer for a type size of 16 bytes. */ |
| 256 |
static void |
| 257 |
unshuffle16_altivec(uint8_t* const dest, const uint8_t* const src, |
| 258 |
const int32_t vectorizable_elements, const int32_t total_elements) { |
| 259 |
static const int32_t bytesoftype = 16; |
| 260 |
uint32_t i, j; |
| 261 |
__vector uint8_t xmm0[16]; |
| 262 |
|
| 263 |
for (j = 0; j < vectorizable_elements; j += 16) { |
| 264 |
/* Load 16 elements (64 bytes) into 4 vectors registers. */ |
| 265 |
for (i = 0; i < bytesoftype; i++) |
| 266 |
xmm0[i] = vec_xl(j + i * total_elements, src); |
| 267 |
|
| 268 |
// Do the Job! |
| 269 |
transpose16x16(xmm0); |
| 270 |
|
| 271 |
/* Store the result vectors*/ |
| 272 |
for (i = 0; i < 16; i++) |
| 273 |
vec_st(xmm0[i], bytesoftype * (i+j), dest); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/* Routine optimized for unshuffling a buffer for a type size larger than 16 bytes. */ |
| 279 |
static void |
| 280 |
unshuffle16_tiled_altivec(uint8_t* const dest, const uint8_t* const orig, |
| 281 |
const int32_t vectorizable_elements, const int32_t total_elements, |
| 282 |
const int32_t bytesoftype) { |
| 283 |
int32_t i, j, offset_into_type; |
| 284 |
const int32_t vecs_per_el_rem = bytesoftype & 0xF; |
| 285 |
__vector uint8_t xmm[16]; |
| 286 |
|
| 287 |
|
| 288 |
/* Advance the offset into the type by the vector size (in bytes), unless this is |
| 289 |
the initial iteration and the type size is not a multiple of the vector size. |
| 290 |
In that case, only advance by the number of bytes necessary so that the number |
| 291 |
of remaining bytes in the type will be a multiple of the vector size. */ |
| 292 |
|
| 293 |
for (offset_into_type = 0; offset_into_type < bytesoftype; |
| 294 |
offset_into_type += (offset_into_type == 0 && |
| 295 |
vecs_per_el_rem > 0 ? vecs_per_el_rem : 16)) { |
| 296 |
for (i = 0; i < vectorizable_elements; i += 16) { |
| 297 |
/* Load the first 128 bytes in 16 XMM registers */ |
| 298 |
for (j = 0; j < 16; j++) |
| 299 |
xmm[j] = vec_xl(total_elements * (offset_into_type + j) + i, orig); |
| 300 |
|
| 301 |
// Do the Job ! |
| 302 |
transpose16x16(xmm); |
| 303 |
|
| 304 |
/* Store the result vectors in proper order */ |
| 305 |
for (j = 0; j < 16; j++) |
| 306 |
vec_xst(xmm[j], (i + j) * bytesoftype + offset_into_type, dest); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/* Shuffle a block. This can never fail. */ |
| 312 |
void |
| 313 |
shuffle_altivec(const int32_t bytesoftype, const int32_t blocksize, |
| 314 |
const uint8_t *_src, uint8_t *_dest) |
| 315 |
{ |
| 316 |
int32_t vectorized_chunk_size; |
| 317 |
vectorized_chunk_size = bytesoftype * 16; |
| 318 |
|
| 319 |
|
| 320 |
/* If the blocksize is not a multiple of both the typesize and |
| 321 |
the vector size, round the blocksize down to the next value |
| 322 |
which is a multiple of both. The vectorized shuffle can be |
| 323 |
used for that portion of the data, and the naive implementation |
| 324 |
can be used for the remaining portion. */ |
| 325 |
const int32_t vectorizable_bytes = blocksize - (blocksize % vectorized_chunk_size); |
| 326 |
const int32_t vectorizable_elements = vectorizable_bytes / bytesoftype; |
| 327 |
const int32_t total_elements = blocksize / bytesoftype; |
| 328 |
|
| 329 |
/* If the block size is too small to be vectorized, |
| 330 |
use the generic implementation. */ |
| 331 |
if (blocksize < vectorized_chunk_size) { |
| 332 |
shuffle_generic(bytesoftype, blocksize, _src, _dest); |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
/* Optimized shuffle implementations */ |
| 337 |
switch (bytesoftype) { |
| 338 |
case 2: |
| 339 |
shuffle2_altivec(_dest, _src, vectorizable_elements, total_elements); |
| 340 |
break; |
| 341 |
case 4: |
| 342 |
shuffle4_altivec(_dest, _src, vectorizable_elements, total_elements); |
| 343 |
break; |
| 344 |
case 8: |
| 345 |
shuffle8_altivec(_dest, _src, vectorizable_elements, total_elements); |
| 346 |
break; |
| 347 |
case 16: |
| 348 |
shuffle16_altivec(_dest, _src, vectorizable_elements, total_elements); |
| 349 |
break; |
| 350 |
default: |
| 351 |
if (bytesoftype > 16) { |
| 352 |
shuffle16_tiled_altivec(_dest, _src, vectorizable_elements, total_elements, bytesoftype); |
| 353 |
} |
| 354 |
else { |
| 355 |
/* Non-optimized shuffle */ |
| 356 |
shuffle_generic(bytesoftype, blocksize, _src, _dest); |
| 357 |
/* The non-optimized function covers the whole buffer, |
| 358 |
so we're done processing here. */ |
| 359 |
return; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/* If the buffer had any bytes at the end which couldn't be handled |
| 364 |
by the vectorized implementations, use the non-optimized version |
| 365 |
to finish them up. */ |
| 366 |
if (vectorizable_bytes < blocksize) { |
| 367 |
shuffle_generic_inline(bytesoftype, vectorizable_bytes, blocksize, _src, _dest); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/* Unshuffle a block. This can never fail. */ |
| 372 |
void |
| 373 |
unshuffle_altivec(const int32_t bytesoftype, const int32_t blocksize, |
| 374 |
const uint8_t *_src, uint8_t *_dest) { |
| 375 |
const int32_t vectorized_chunk_size = bytesoftype * 16; |
| 376 |
/* If the blocksize is not a multiple of both the typesize and |
| 377 |
the vector size, round the blocksize down to the next value |
| 378 |
which is a multiple of both. The vectorized unshuffle can be |
| 379 |
used for that portion of the data, and the naive implementation |
| 380 |
can be used for the remaining portion. */ |
| 381 |
const int32_t vectorizable_bytes = blocksize - (blocksize % vectorized_chunk_size); |
| 382 |
const int32_t vectorizable_elements = vectorizable_bytes / bytesoftype; |
| 383 |
const int32_t total_elements = blocksize / bytesoftype; |
| 384 |
|
| 385 |
/* If the block size is too small to be vectorized, |
| 386 |
use the generic implementation. */ |
| 387 |
if (blocksize < vectorized_chunk_size) { |
| 388 |
unshuffle_generic(bytesoftype, blocksize, _src, _dest); |
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
/* Optimized unshuffle implementations */ |
| 393 |
switch (bytesoftype) { |
| 394 |
case 2: |
| 395 |
unshuffle2_altivec(_dest, _src, vectorizable_elements, total_elements); |
| 396 |
break; |
| 397 |
case 4: |
| 398 |
unshuffle4_altivec(_dest, _src, vectorizable_elements, total_elements); |
| 399 |
break; |
| 400 |
case 8: |
| 401 |
unshuffle8_altivec(_dest, _src, vectorizable_elements, total_elements); |
| 402 |
break; |
| 403 |
case 16: |
| 404 |
unshuffle16_altivec(_dest, _src, vectorizable_elements, total_elements); |
| 405 |
break; |
| 406 |
default: |
| 407 |
if (bytesoftype > 16) { |
| 408 |
unshuffle16_tiled_altivec(_dest, _src, vectorizable_elements, total_elements, bytesoftype); |
| 409 |
} |
| 410 |
else { |
| 411 |
/* Non-optimized unshuffle */ |
| 412 |
unshuffle_generic(bytesoftype, blocksize, _src, _dest); |
| 413 |
/* The non-optimized function covers the whole buffer, |
| 414 |
so we're done processing here. */ |
| 415 |
return; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
/* If the buffer had any bytes at the end which couldn't be handled |
| 420 |
by the vectorized implementations, use the non-optimized version |
| 421 |
to finish them up. */ |
| 422 |
if (vectorizable_bytes < blocksize) { |
| 423 |
unshuffle_generic_inline(bytesoftype, vectorizable_bytes, blocksize, _src, _dest); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
const bool is_shuffle_altivec = true; |
| 428 |
|
| 429 |
#else /* defined(__ALTIVEC__) && defined(__VSX__) && defined(_ARCH_PWR8) */ |
| 430 |
|
| 431 |
const bool is_shuffle_altivec = false; |
| 432 |
|
| 433 |
void shuffle_altivec(const int32_t bytesoftype, const int32_t blocksize, |
| 434 |
const uint8_t *_src, uint8_t *_dest) { |
| 435 |
abort(); |
| 436 |
} |
| 437 |
|
| 438 |
void unshuffle_altivec(const int32_t bytesoftype, const int32_t blocksize, |
| 439 |
const uint8_t *_src, uint8_t *_dest) { |
| 440 |
abort(); |
| 441 |
} |
| 442 |
|
| 443 |
#endif /* defined(__ALTIVEC__) && defined(__VSX__) && defined(_ARCH_PWR8) */ |